var main = {
  blocks:[],
  openBlock:null,
  animationDuration:0,
  videoSize:{w:null, h:null, ratio:1280/720},
  videoIsLoaded:false,
  WINDOW:{
    width:null,
    height:null,
    wunit:null,
    hunit:null},
  gutter:10,
  getParams:function(key){
    var hashes = window.location.href.split('#');
    var params;
    if(typeof(hashes[1]) !== "undefined"){
      params = hashes[1].split("&");
      for(var i in params){
        if(params[i].indexOf(key) != -1)
          return params[i].match(/\d+/)[0];}
        return "0";
    }},
  init:function(){
    jQuery(window).resize();
    jQuery(".block").each(function(){
      main.blocks.push(new Block(jQuery(this)));});
    main.getUI();
    main.build();
    main.animationDuration = 0;},
  build:function(){
    jQuery('#wrapper').masonry({
      singleMode:false,
      itemSelector:'.block',
      columnWidth:main.WINDOW.wunit+main.gutter})},
  saveUI:function(){
    var UIstate = "[";
    for(var i in main.blocks){
      UIstate += "{.id.:"+main.blocks[i].id+",.UIopen.:"+main.blocks[i].UIopen+"},";}
    UIstate = UIstate.slice(0,UIstate.length-1) + "]";
    jQuery.cookie('ELECTROSANNE-UI', UIstate, {expires: 30});},
  getUI:function(){
    var UIstate;
    var params = main.getParams();
    //if(!jQuery.cookie("ELECTROSANNE-UI")) 
      UIstate = main.blocks;
    /*else
      UIstate = JSON.parse(jQuery.cookie("ELECTROSANNE-UI").replace(/\./g,"\""));*/
    for(var i in UIstate){
      main.findBlockById(UIstate[i].id).initTemplate();
      UIstate[i].UIopen 
      ? main.findBlockById(UIstate[i].id).open()
      : main.findBlockById(UIstate[i].id).close();}},
  hideBlocks:function(){
    jQuery("#wrapper").hide();},
  findBlockById:function(id){
    for(var i in main.blocks){
      if(main.blocks[i].id == id){
        return main.blocks[i];}};}
};

var Block = function(node){
  this.UIopen = 0;
  this.id = node.attr("id").match(/[0-9]+/);
  var that = this;
  var template = null;
  var node = {
    parent:node,
    top:node.find(".top"),
    body:node.find(".body"),
    bottom:node.find(".bottom")};
  var sizes = {
    minheight:null,
    minwidth:null,
    maxheight:null,
    maxwidth:null};
  this.getNodes = function(){
    return node};
  this.initTemplate = function(fn){
    var templateName = node.parent.attr("class").match(/template_\w+/);
    this.template = blocks.templates[templateName];
    if(this.template)this.template.init(that, fn);
    this.getNodes().parent.css({opacity:"1"})};
  this.updateSizes = function(){
    sizes.minheight = node.parent.css("height").match(/[0-9]+/);
      /*node.parent.css("height").match(/[0-9]+/)*main.WINDOW.hunit+
      ((node.parent.css("height").match(/[0-9]+/)-1)*10);*/
    sizes.minwidth = 
      (node.parent.css("width").match(/[0-9]+/)*main.WINDOW.wunit)+
      ((node.parent.css("width").match(/[0-9]+/)-1)*main.gutter);
    node.parent.addClass("max");
    sizes.maxheight = node.parent.css("height").match(/[0-9]+/);
    sizes.maxwidth = 
      (node.parent.css("width").match(/[0-9]+/)*main.WINDOW.wunit)+
      ((node.parent.css("width").match(/[0-9]+/)-1)*main.gutter);
    node.parent.removeClass("max");};
  this.maximize = function(){
    node.parent
      .stop()
      .css({height:"auto"})
      .animate(
        {width:sizes.maxwidth},
        {step:function(){main.build();},
        duration:main.animationDuration,
        complete:function(){main.build();}});};
  this.open = function(){
    this.UIopen = 1;
    main.openBlock = this;
    //node.parent.prependTo(jQuery("#wrapper"));
    node.parent.addClass("max");
    node.body.show();
    if(that.template)that.template.open();
    that.maximize();};
  this.close = function(){
    this.UIopen = 0;
    node.parent
      .stop()
      .css({height:"auto"})
      .animate(
        {width:sizes.minwidth},
        {step:function(){main.build();},
        duration:main.animationDuration,
        complete:function(){
          if(that.template)that.template.close();
          node.parent.removeClass("max");
          if(sizes.minheight==0)node.body.hide();
          main.build();}});};
  node.top.click(function(){
    if(main.openBlock && that.id !== main.openBlock.id)main.openBlock.close();
    that.UIopen ? that.close() : that.open();
    main.saveUI()});
  this.updateSizes();
};

jQuery(window).resize(function(){  
  main.WINDOW.width = jQuery(window).width() >= 1240 
    ? jQuery(window).width()/*scroll*/-15/*scroll*//*padding*/-40/*paddding*/-main.gutter*6
    : 1240;
  main.WINDOW.height = jQuery(window).height()-40-3*10;
  main.WINDOW.wunit = Math.floor(main.WINDOW.width/6);
  main.WINDOW.hunit = Math.floor(main.WINDOW.height/4);
  
  /*main.videoSize.h = jQuery(window).height() + (jQuery(window).height()/10),
  main.videoSize.w = main.videoSize.h*main.videoSize.ratio;*/
  
  main.videoSize.h = jQuery(window).height();
  main.videoSize.w = jQuery(window).width();
    
  //ytplayer = document.getElementById("ytPlayer");
  ytplayer = document.getElementById("videoDiv");
  
  if(ytplayer){
    ytplayer.style.height = main.videoSize.h + "px";
  }

});
