soundManager.debugMode = false;
var zInd = 1;
var countPage = 1;
var countPagePortf = 1;
var main_div_width = 0;

function changeHeight()
    {
    $('#crs-cont-id').css('height',$('#item_id_'+$('#current_item_id').val()).innerHeight()+'px');
    }

function changeTimeHeight()
    {
    //setTimeout("changeHeight()",100);
    //setTimeout("changeHeight()",500);
    setTimeout("changeHeight()",1000);
    setTimeout("changeHeight()",2000);
    setTimeout("changeHeight()",5000);
    }

//position info box
function infobox_position(){
  var active_a = $('li.active:first a').attr('class');
  var asx = 0;
  var wdth = 0;
  
  switch(active_a)
    {
    case "label4":
      {
      wdth = $('a.label4').innerWidth();
      asx = wdth/2 + 7.5;
      }
    case "label3":
      {
      wdth = $('a.label3').innerWidth();
      if (active_a == "label3") asx = wdth/2 + 7.5;
      else asx = asx + wdth + 15;
      }
    case "label2":
      {
      wdth = $('a.label2').innerWidth();
      if (active_a == "label2") asx = wdth/2 + 7.5;
      else asx = asx + wdth + 15;
      }
    case "label1":
      {
      wdth = $('a.label1').innerWidth();
      if (active_a == "label1") asx = wdth/2 + 7.5;
      else asx = asx + wdth + 15;
      }
    }
  asx = asx - 12.5;
  
  $('#info_button').css('margin','0 0 0 '+asx+'px');
  $('div.infobox').css('background','url(/files/14/images/qbox.gif) '+asx+'px top no-repeat');
  }

/*moveToPage (beta)
$.fn.moveToPage = function(){
  
  var mainCont = $('div.main-cont:first','body');
  var links = $(this);
  var windowWidth = $(window).width();
  
  function ajaxLoadPage(){
    var load_page = $(this).attr('id');
    $(links).unbind('click',ajaxLoadPage);
    $.ajax({
      type: "GET",
      cache: false,
      url: '/'+load_page,
      data: '',
      success: function(data){
        $('body').animate({'backgroundPosition':-windowWidth+'px -29px'},1000,'linear');
        $(mainCont).animate({'marginLeft':-windowWidth+'px'},1000,'linear',function(){
          $('*','body').remove();
          $('body')
          .append(data)
          .css('overflow','hidden');
          var mainContNew = $('div.main-cont:first','body');
          $(mainContNew).css('margin-left',windowWidth+'px');
          $('body').animate({'backgroundPosition':-(windowWidth*2)+'px -29px'},500,'linear',function(){
            $('body').css('backgroundPosition','20px -29px');
          });
          $(mainContNew).animate({'marginLeft':'0px'},500,'linear',function(){
            $('body').css('overflow','visible');
          });
        });
      }
    });
  };
  
  $(links).each(function(){
    $(this)
    .click(function(){return false;})
    .bind('click',ajaxLoadPage);
  });
  
}*/

//moveToPage (che)
$.fn.moveToPage = function(portf){
  
  if (portf === '1') var mainCont = $('#main_div_portf:first','body');
  else var mainCont = $('div.main-cont:first','body');
  
  var links = $(this);
  var windowWidth = $(window).width();
  if (windowWidth < 997) windowWidth = 997;
  main_div_width = main_div_width + windowWidth;
  
  if (countPage == 1 && portf !== '1') $('#div_page_1').css('width',windowWidth+'px');
  
  if (countPagePortf == 1 && portf === '1') $('#div_page_1_portf').css('width',windowWidth+'px');
  
  //$('#main_div').css('width',main_div_width+'px');
  //$('#div_page_'+countPage).css('width',windowWidth+'px');
  
  // загрузка категории портфолио с главной
  function ajaxLoadPage(){
    var num_old_page = countPage
    var num_new_page = countPage + 1;
    var load_page = $(this).attr('id');
    $(links).unbind('click',ajaxLoadPage);
    
    $('#main_div').css('width',(windowWidth+main_div_width)+'px');
    $('#div_page_'+num_old_page).css('width',windowWidth+'px');
    $('#div_page_'+num_new_page).css('width',windowWidth+'px');
    
    $('#div_page_'+num_new_page).css('marginLeft',0+'px');
    $('#div_page_'+num_new_page).load('/ajax_'+load_page, function(){
      $('body').animate({'backgroundPosition':-(64*15*countPage-21)+'px -29px'},1000,'swing');
      $('#main_div').animate({'marginLeft':-(main_div_width)+'px'},1000,'swing',function(){
        var cont_main_div = $('#main_div').html();
        $('#main_div').html(cont_main_div+'<div id="div_page_'+(countPage+1)+'"  style="float:left; overflow:hidden; "></div>');
        $('#div_page_'+num_old_page).html('!');
        $('#infBox').openInfoBox();
	$('#infBoxClose').closeInfoBox();
        $('a','#menu-lvl-2').moveToPage('1');
	infobox_position();
        main_div_width = main_div_width - windowWidth;
        });
      });
    countPage++;
    
  }
  
  // загрузка категории портфолио из портфолио
  function ajaxLoadPagePortf(){
    
    var num_old_page = countPagePortf;
    var num_new_page = countPagePortf + 1;
    var load_page = $(this).attr('id');
    $(links).unbind('click',ajaxLoadPage);
    $('#main_div_portf').css('width',(windowWidth+main_div_width)+'px');
    $('#div_page_'+num_old_page+'_portf').css('width',windowWidth+'px');
    $('#div_page_'+num_new_page+'_portf').css('width',windowWidth+'px');
    
    $('#div_page_'+num_new_page+'_portf').css('marginLeft',0+'px');
    $('#div_page_'+num_new_page+'_portf').load('/ajax_portfolio_'+load_page, function(){
      
      //$('body').animate({'backgroundPosition':-(64*15*countPage-21)+'px -29px'},1000,'swing');
      $('#main_div_portf').animate({'marginLeft':-(main_div_width)+'px'},1000,'swing',function(){
        var cont_main_div = $('#main_div_portf').html();
        
        $('#main_div_portf').html(cont_main_div+'<div id="div_page_'+(countPagePortf+1)+'_portf"  style="float:left; overflow:hidden; "></div>');
        $('#div_page_'+num_old_page+'_portf').html('!');
        $('#infBox').openInfoBox();
	$('#infBoxClose').closeInfoBox();
        $('a','#menu-lvl-2').moveToPage('1');
	infobox_position();
        });
      });
    countPagePortf++;
    $('li.active').removeClass('active');
    $('#'+load_page).parent().addClass('active');
  }
  
  if (portf == '1')
  {
  $(links).each(function(){
    $(this)
    .click(function(){return false;})
    .bind('click',ajaxLoadPagePortf);
  });
  }
  else
  {
  $(links).each(function(){
    $(this)
    .click(function(){return false;})
    .bind('click',ajaxLoadPage);
  });
  }
}





//cubes action
$.fn.setCubeAction = function(){
  $(this)
  .draggable({
    grid: [32,19],
    //cursor: 'move',
    start: function(event, ui){
      $(this).css('z-index',zInd+1);
      zInd++;
    },
    drag: function(event, ui) {
      //alert('drag');
    },
    stop: function(event, ui) {
      soundManager.play('sound1');
    }
  })
  .click(function(){
    soundManager.play('sound2');
    $(this).fadeOut(300,function(){
      $(this).remove();
    });
  });
  return this;
};




//cube field
$.fn.cubeField = function(){
  
  var mainCont = $('div.main-cont:first','body');
  
  soundManager.url = '/files/14/swf/';
  
  soundManager.onload = function() {
    soundManager.createSound({
      id: 'sound1',
      url: '/files/14/sound/Arcade_Beep_03.mp3',
      volume: 80
    });
    soundManager.createSound({
      id: 'sound2',
      url: '/files/14/sound/Arcade_Beep_04.mp3',
      volume: 80
    });
  }
  
  var arrStartLeft = new Array(628,628,628,628,628,660,724,788,852,916,980,1044,1108,1172);
  var arrStartTop = new Array(560,446,332,218,104,47,47,47,47,47,47,47,47,47);
  
  var arrFinLeft = new Array(628,692,756,820,884,948,1012,1076,1140,1172,1172,11872,1172,1172);
  var arrFinTop = new Array(560,560,560,560,560,560,560,560,560,503,389,275,161,47);
  
  var cubeColorPos = new Array();
  var posLeft,posTop;
  
  for(var i=0;i<=14;i++){
    cubeColorPos[i] = new Array();
    var arrLength = ((arrFinLeft[i] - arrStartLeft[i])/32)+1;
    for(var ii=0;ii<arrLength;ii++){
      posLeft = ii==0 ? arrStartLeft[i] : posLeft+32;
      posTop = ii==0 ? arrStartTop[i] : posTop+57;
      cubeColorPos[i][ii] = posLeft+'/'+posTop;
      //$('body').append(cubeColorPos[i][ii]);
    }
  }
  
  function getCubeColor(posLeft,posTop,outType){
    var out = -1;
    var output;
    for(var i=0;i<cubeColorPos.length;i++){
      if($.inArray(posLeft+'/'+posTop,cubeColorPos[i])>-1){
        out = i;
        break;
      }
    }
    if(outType==2)return out;
  
    if(out > -1)
      output = out%2==0 ? (out+2)/2 : (out+1)/2;
    else
      output = -1;
    
    return output;
  }

  $(this).click(function(e){
    var posX = (e.pageX - e.pageX%32)-12;
    var posY = (e.pageY - e.pageY%57)-10;
    if(getCubeColor(posX,posY,1)==-1)
      posX -= 32;
    var colorIndex = getCubeColor(posX,posY,1)>-1 ? getCubeColor(posX,posY,1) : -1;
    if(colorIndex>-1){
      //$('body').append(posX+'/'+posY+'<br />');
      $(mainCont).append('<div class="pixel cube'+colorIndex+'" style="left:'+posX+'px; top:'+posY+'px; z-index:'+(zInd+1)+';"></div>');
      soundManager.play('sound1');
      $('div:last',mainCont).setCubeAction();
      zInd++;
    }
  });
  return this;
};





function loadPortfolio()
  {
  var crsCont = $('div.crs-cont:first');
  var curDiv = $('div:eq(0)',crsCont);
  
  for(i=0;i<count_num;i++)
    {
    curDiv = $('div:eq('+i+')',crsCont);
    if (curDiv.html() == "&nbsp;" || curDiv.html() == $("#nbsp").html())
      {
      var div_id = curDiv.attr('id').split('_');
      var item_id = div_id[2];
      $('#item_id_'+item_id).load("/item", {id:item_id} );
      }
    }
  }

//portfolio carousel
$.fn.portfoCarousel = function(){
  soundManager.url = '/files/14/swf/';
  soundManager.onload = function() {
    soundManager.createSound({
      id: 'sound1',
      url: '/files/14/sound/Arcade_Beep_04.mp3',
      volume: 80
    });
  }
  
  var parent = $(this);
  var butNext = $('div.crs-next:first a',parent);
  var butPrev = $('div.crs-prev:first a',parent);
  var crsCont = $('div.crs-cont:first',parent);
  var num = count_num;//$('div',crsCont).size();
  //alert(num);
  var stepSize = $('div:first',crsCont).innerWidth();
  var limitMargin = 1;
  var firstHeight = $('#item_id_'+item_id).innerHeight();
  var curPage = parseInt($(butPrev).text());
  
  $(crsCont).css({'marginLeft': (-curPage*stepSize)+'px','overflow':'hidden','width':(stepSize*num)+'px','height':'auto'});
  
  if (be_prev == 0) $(butPrev).hide();
  if (be_next == 0) $(butNext).hide();
  
  function moveNext(){
    var cur_page = parseInt($(butNext).text())-1;
    var curDiv = $('div:eq('+(cur_page)+')',crsCont);
    var div_id = curDiv.attr('id').split('_');
    var item_id = div_id[2];
    $('#current_item_id').val(item_id);
    var aLoad = false;
    
    if (curDiv.html() == "&nbsp;" || curDiv.html() == $("#nbsp").html())
      {
      aLoad = true;
      $('#item_id_'+item_id).load("/item", {id:item_id}, function(){
        var curHeight_ = $('#item_id_'+item_id).innerHeight();
        $(crsCont).css('height',curHeight_+'px');
        changeTimeHeight();
        });
      }
    
    var posLeft = $(crsCont).css('marginLeft');
    posLeft = parseInt(posLeft.replace('px',''))-stepSize;
    $(butNext).unbind('click', moveNext);
    //soundManager.play('sound1');
    $(crsCont).animate({'marginLeft':posLeft+'px'},500,'linear',function(){
      var curPage = parseInt($(butNext).text());
      $(butNext).text(curPage+1);
      $(butPrev).text(curPage-1);
      $('a:eq('+(num-(curPage-1))+')','#pagesNav').removeClass('active');
      $('a:eq('+(num-curPage)+')','#pagesNav').addClass('active');
      if(posLeft==-((num-limitMargin)*stepSize)){
        $(butNext).hide();
      }
      if(posLeft!=0){
        $(butPrev).show();
      }
      
      //var curHeight = $('div:eq('+(curPage-1)+')',crsCont).innerHeight();
      var curHeight = $('#item_id_'+item_id).innerHeight();
      $(crsCont).animate({'height':curHeight+'px'},300,'linear',function(){
        $(butNext).bind("click",moveNext);
      });
      if (aLoad == false) changeTimeHeight();
    });
    return false;
  }

  function movePrevious(){
    var cur_page = parseInt($(butPrev).text());
    var curDiv = $('div:eq('+(cur_page-1)+')',crsCont);
    var div_id = curDiv.attr('id').split('_');
    var item_id = div_id[2];
    $('#current_item_id').val(item_id);
    var aLoad = false;
    if (curDiv.html() == "&nbsp;" || curDiv.html() == $("#nbsp").html())
      {
      aLoad = true;
      $('#item_id_'+item_id).load("/item", {id:item_id}, function(){
        var curHeight_ = $('#item_id_'+item_id).innerHeight();
        $(crsCont).css('height',curHeight_+'px');
        
        changeTimeHeight();
        });
      }
      
    var posLeft = $(crsCont).css('marginLeft');
    posLeft = parseInt(posLeft.replace('px',''))+stepSize;
    $(butPrev).unbind("click",movePrevious);
    //soundManager.play('sound1');
    $(crsCont).animate({'marginLeft':posLeft+'px'},500,'linear',function(){
      var curPage = parseInt($(butPrev).text()) + 2;
      $(butNext).text(curPage-1);
      $(butPrev).text(curPage-3);
      $('a:eq('+(num-(curPage-1))+')','#pagesNav').removeClass('active');
      $('a:eq('+(num-(curPage-2))+')','#pagesNav').addClass('active');
      
      if(posLeft==0){
        $(butPrev).hide();
      }
      if(posLeft!=-(num*stepSize-limitMargin)){
        $(butNext).show();
      }
      //var curHeight = $('div:eq('+(curPage-3)+')',crsCont).innerHeight();
      var curHeight = $('#item_id_'+item_id).innerHeight();
      $(crsCont).animate({'height':curHeight+'px'},300,'linear',function(){
        $(butPrev).bind("click",movePrevious);
      });
      if (aLoad == false) changeTimeHeight();
    });
    return false;
  }
  
  $(butNext).click(function(){return false;}).bind("click",moveNext);
  $(butPrev).click(function(){return false;}).bind("click",movePrevious);
  
  $('a','#pagesNav').each(function(){
    $(this).click(function(){
      var a_id = $(this).attr('id').split('_');
      var item_id = a_id[1];
      $('#current_item_id').val(item_id);
      var curDiv = $('#item_id_'+item_id);
      var aLoad = false;
      if (curDiv.html() == "&nbsp;" || curDiv.html() == $("#nbsp").html())
        {
        aLoad = true;
        curDiv.html('<center><img src="/files/14/images/loader.gif" /></center>');
        var div_id = curDiv.attr('id').split('_');
        var item_id = div_id[2];
        $('#item_id_'+item_id).load("/item", {id:item_id}, function(){
          var curHeight_ = curDiv.innerHeight();
          $(crsCont).css('height',curHeight_+'px');
          changeTimeHeight(item_id);
          });
        }
      
      if($(this).is('.active')==false){
        var curPage = parseInt($(this).text())-1;
        var posLeft = -(stepSize*curPage);
        $('a','#pagesNav').removeClass('active');
        $(this).addClass('active');
        $(crsCont).css('marginLeft',posLeft+'px');
        $(crsCont).animate({'marginLeft':posLeft+'px'},300,'linear',function(){
          
          $(butNext).text(curPage+2);
          $(butPrev).text(curPage);
          if(posLeft==0){
            $(butPrev).hide();
            $(butNext).show();
          }else if(posLeft==-(num-1)*stepSize){
            $(butPrev).show();
            $(butNext).hide();
          }else{
            $(butPrev).show();
            $(butNext).show();
          }
          
          var curHeight = curDiv.innerHeight();
          $(crsCont).animate({'height':curHeight+'px'},300,'linear',function(){
          });
          if (aLoad == false) changeTimeHeight(item_id);
        });
      }
      return false;
    });
  });
  
  return this;
};




//open help box
$.fn.openInfoBox = function(options){
  $(this).click(function(){
    $('div:first',this).removeClass('ib-closed').addClass('ib-opened');
    
    var active_a = $('li.active:first a').attr('class');
    var wdth = parseFloat($('a.'+active_a).innerWidth());
    var margin_left = parseFloat($('#info_button').css('margin-left'));
    var asx = margin_left - wdth/2 + 7.5;
    
    $('#info_button').css('margin','0 0 0 '+asx+'px');
    return false;
  });
};

//close help box
$.fn.closeInfoBox = function(options){
  $(this).click(function(){
    $('div:first','#infBox').removeClass('ib-opened').addClass('ib-closed');
    
    infobox_position();
    
    return false;
  });
};



function flashObjActivate(){
  var noCorrectBrowser = ($.browser.opera) || ($.browser.msie && $.browser.version=='7.0');
  if(noCorrectBrowser){
    $('object').each(function(i){
      var src = $(this).attr('data');
      var width = $(this).attr('width');
      var height = $(this).attr('height');
      var id = $(this).attr('id');
      var wmode = !$.browser.msie ? $('param:last',this).attr('value') : 'opaque';
      var flashka = '<object type="application/x-shockwave-flash" data="'+src+'" width="'+width+'" height="'+height+'" id="'+id+'">'
      +'<param name="movie" value="'+src+'" />'
      +'<param name="wmode" value="'+wmode+'" />'
      +'</object>';
      $(this).after(flashka);
      $(this).remove();
    });
  }
};


