var isiaRootPageHelper = {

	h2  : null,
	hh2 : null,
	h4  : null,
	h5  : null,
	hhh : null,


	init : function()
	{
	
	if ($.browser.msie && $.browser.version.substring(0,1) === '6') {
  	location.href = "http://2pilota-against-ie6.2pilota.ru/index_ru.html";
}


	$("div.items").disableTextSelect();
    this.h2 = $(document).height();
    this.hh2 = this.h2 - 47;
    this.hhh = $(document).width();
    this.h5 = this.h2 / 2 - 120;
    this.h4 = this.h2 - 47;
    this.initToggler();
    this.initWorksList();
    this.resize();
    window.onresize = this.resize;

	
	},
	initWorksList : function()
	{
		var self = this;

		$('a.click').click(function()
    {
      $("#wrap").animate({left: self.hhh}, 650);
	  $("#kurilko").fadeIn(500);
      $("#kurilko").animate({left: "0"}, 650);
setTimeout(function()
    	{
        $("a.click2").fadeIn("slow");
    	}, 1000);
      return false;
    });

      $('a.click2').click(function()
    {
      $("#wrap").animate({left: "0"}, 650);
     $("#kurilko").fadeOut(1500);     
	 $("#kurilko").animate({left: -self.hhh}, 650);

        $("a.click2").fadeOut("fast");
  
      return false;
    });

		$('#listOfWorks a').click(function()
		{

			$('#listOfWorks a').removeClass("active");
			$(this).addClass("active");
			self.loadWorksList(this.href);

			return false;
		});
	},
	initToggler : function()
	{
		var self = this;
		$("#main_c>div").toggle(
      function()
      {
        var expa = $(this).is('.expand');
        var coll = $(this).is('.collapse');
        if(expa)
        {
          self.collapse(this);
        }
        else if(coll)
        {
          self.expand(this);
        }
      },
      function()
      {
         var expa = $(this).is('.expand');
        var coll = $(this).is('.collapse');

        if(coll)
        {
          self.expand(this);
        }
        else
        {
          self.collapse(this);
        }
      }
    );
	},
	resize : function()
	{
    $(".content").css({height: this.h2});
    $("#test22").css({top: this.h5});
    $("#test33").css({top: this.h5});
    $("#test44").css({top: this.h5});
    $("#desc_weare").css({top: this.h5 + 175});
    $("#desc_what").css({top: this.h5 + 175});
    $("#desc_how").css({top: this.h5 + 175});
    $("#kk").css({top: this.h5 + 240});
    $("#kk2").css({top: this.h5 + 240});
    $("#test4").css({height: this.hh2});
    $(".content").css({height: this.h2});
    var iii = $('#test44').offset();
    var ddd = $(document).width();
    var lef = iii.left;
    var eq = ddd-lef;
    var enn = eq/2;


   
 
    $("#test4").css("width", eq);
    $("#listOfWorks").css("width",eq);

    var rrr = $('.root2').offset();
    var ttt = rrr.top;
    var tttn = ttt + 70;

    $("#main_c div").addClass("expand");
	},
	collapse : function(target)
	{
	
	
	
		$('#listOfWorks a').removeClass("active");
    var fl =  $(target).hasClass("root2");
    var t2 =  $(target).hasClass("root1");
    var t1 =  $(target).hasClass("root0");
    $("#listOfWorks").show();
    $(target).animate({height: this.h2, top: "0"}, 250);
    $("div.root2.collapse").empty();

    $(".collapse").animate({height: 47, top: "50"+"%"}, 250).removeClass("collapse").addClass("expand");
    $(target).removeClass("expand").addClass("collapse");
    $('#workIntro').remove();
     if(fl)
     {
     	  $("#what_weare_doing").css("display", "none");
		  $("#weare").css("display", "none");
    $("li a:first").trigger("click");
    $("#kk").fadeIn(900);

    $("#copyright").animate({left: "5"}, 230);};


    if(!fl)
    {
    	$("#listOfWorks").hide();
    	 $("#kk").fadeOut(900);

    }
    if(!t2)
    {
      $("#test44").animate({left: "190"}, 250);
      $("#kk2").fadeOut(900);
    }
    if(!t1)
    {
      $("#test33").animate({left: "7"}, 250);
    }

    if(t2)
    {

      $("#test44").animate({left: "410"}, 350);
      $("#kk2").fadeIn(900);

      $("#copyright").animate({left: "430"}, 230);
$("#weare").css("display", "none");
      $("#what_weare_doing").css("display", "block");



   $("#what_weare_doing.scrollable").scrollable({
   	  vertical:true,
      size: 2 ,
      clickable: false
    });
    }
    if(t1)

    {
		$("#weare").css("display", "block");
      $("#what_weare_doing").css("display", "none");
    	$("#test44").animate({left: "410"}, 350);
      $("#test33").animate({left: "210"}, 150);

      $("#copyright").animate({left: "230"}, 230);
	  
	  $("#weare.scrollable").scrollable({
   	  vertical:true,
      size: 3 ,
      clickable: false
    });
	  
	  }

  
  },
  expand : function(target)
  {
  	$(target).removeClass("collapse").addClass("expand");
    $('#workIntro').remove();
    $('#listOfWorks a').removeClass("active");
    var fl =  $(target).hasClass("root2");
    var t2 =  $(target).hasClass("root1");
    var t1 =  $(target).hasClass("root0");
    $("#listOfWorks").hide();
    $(target).animate({height: 47, top: "50%"}, 250);
    if(fl)
    {
    	$("#listOfWorks").hide();
    	$(target).empty();
    	$("#kk").fadeOut(900);
    	 setTimeout(function() {
    $("#copyright").animate({left: "5"}, 750);},500);
    }
    if(t2)
    {
    	$("#kk2").fadeOut(900);
      $("#what_weare_doing").css("display", "none");
      $("#test44").animate({left: "190"}, 250);
       setTimeout(function() {
       $("#copyright").animate({left: "5"}, 750);},500);
    }
    if(t1)
    {
		$("#weare").css("display", "none");
      $("#test33").animate({left: "7"}, 250);
      $("#test44").animate({left: "190"}, 150);
       setTimeout(function() {
       $("#copyright").animate({left: "5"}, 750);},500);
    }
  },
  imageOnClickHandler : function(item)
  {
    return function()
    {
      $('#workIntro').remove();
      location.href = item.href;
      return false;
    }
  },
  imageOnLoadHandler : function(item)
  {
  
 //alert ("start");
	if($.browser.opera)
    {
		//alert ("5");
	//setTimeout(500);
    }
  	var self = this;
	
    return function(event)
    {
	  
      var itemImage;
      itemImage = $(this);
      if(itemImage.data("loaded"))
      {
        return true;
      }
      else
      {
        itemImage.data("loaded", true);
      }

      itemImage.click(self.imageOnClickHandler(item));

      $("#ajax_container div.items div").each(function(i, divItem)
      {
		//alert ("before a length");
		//console.log(divItem);
        if($(divItem).children().length > 0)
        {
		 //alert ("before a return");
          return true;
        }
        else
        {
			//alert ("before a remove")
          $('div.items div').removeClass("preloader");

          itemImage.appendTo(divItem);

          $("#ajax_container div.scrollable div.items div").css("opacity", "0.3");
          $(this).hover(
            function()
            {
              $(this).fadeTo(300, 1);
            },
            function()
            {
              var tr = $(this).hasClass("active");
              if(tr)
              {
                $(this).fadeTo(300, 1);
              }
              else
              {
                $(this).fadeTo(100, 0.3);
              }
            }
          );

          return false;
        }
      });
    }
  },
  assignImages : function(container, imageList)
  {
	  for(var i in imageList)
    {
      container.find(".items").append("<div class='preloader'/>");
      var image = new Image();

      image.onload  = this.imageOnLoadHandler(imageList[i]);
      image.src     = imageList[i].image;
      if(image.complete)
      {
        this.imageOnLoadHandler(imageList[i]).call(image);
      }
    }
  },
  renderWorksList : function(list)
  {
  	$('#ajax_container').remove();
  	var container = $("<div id='ajax_container'><div class='scrollable vertical'><div class='items'/></div></div>");
    $('div.root2').append(container);

    if($.browser.msie)
    {
    	var self = this;
    	setTimeout(function()
    	{
        self.assignImages.call(self, container, list);
    	}, 1000);
    }
    else
    {
    	this.assignImages(container, list);
    }



    $("img").attr({ title: "Посмотреть эту работу", alt: "Посмотреть эту работу" });

    $("ajax_container div.scrollable div.items div").click(function(){
      var trr = $(this).hasClass("active");
      if(trr == false)
      {
        $(this).fadeTo (100,1);
      }
      $("ajax_container div.scrollable div.items div").not(this).fadeTo(100,0.3);
      $(this).fadeTo (100,1);
    });

    $("#ajax_container div.scrollable").scrollable({
      vertical:true,
      size: 5 ,
      hoverClass: 'hover',
      prev:   '.prev',
      next:   '.next'
    });
	

  },
  loadWorksList : function(url, source)
  {

  	var self = this;
  	if($.browser.msie)
  	{
  	  url = encodeURI(url);
  	}
  	$.ajax({
      type     : "GET",
      url      : url,
      dataType : "json",
      success : function(result)
      {
      	self.renderWorksList.call(self, result.data);
      }

    });
  }

};


$(document).ready(function()
{
$("#what_weare_doing a").click(function () {
	   var name = $(this).attr("name");
	   var title = $(this).attr("title");
        $("#lg1 span").empty();
	      $("#lg1 span").append(title);
        $("#lg1 a.bb").empty();
	      $("#lg1 a.bb").append(name);
        $("#lg1").fadeIn("slow");
       $("a[rel='example2']").colorbox({transition:"none", width:"700", height:"430"});
        return false;
      });
	  
	  $("#weare a").click(function () {
	   var name1 = $(this).attr("name");
	   var title1 = $(this).attr("title");
        $("#lg1 span").empty();
	      $("#lg1 span").append(title1);
        $("#lg1 a.bb").empty();
	      $("#lg1 a.bb").append(name1);
        $("#lg1").fadeIn("slow");
		var offset = $(this).offset();
		var oftop = offset.top;
		var oftop2 = oftop - 152;
		var ofleft = offset.left;
		
		var ofleft1 = -294;
		$("#lg1").css("top", oftop2);
		$("#lg1").css("left", ofleft1);
		
	
		
		
	
		
		
		
       $("a[rel='example2']").colorbox({transition:"none", width:"700", height:"430"});
        return false;
      }
    );
$(function(){
  $("#lg1").easydrag();

});
$(".close").click(function()
{
	 $("#lg1 span").empty();
        $("#lg1 a.bb").empty();
var par = $(this).parent();
par.fadeOut("slow");
}
);

  isiaRootPageHelper.init();
     if(location.href == "http://2pilota.ru/#1"){
      $(".root0").trigger("click");

    }
    else if(location.href == "http://2pilota.ru/#2"){
      $(".root1").trigger("click");
    }
});

String.prototype.format = function format(args)
{
  var value = this;
  for(i = 0; i < arguments.length; i++)
  {
    value = value.replace('{' + i + '}', arguments[i]);
  }

  return value;
};
