$(document).ready(function(){
	$('.contact').ajaxStop(function(){
		//$(this).css('bottom', '10px');
	});
});

Animated = false;
FirstTime = true;
Active = 'none';       

buttonsLang = "ru";   

lineBulba = '';
nameGallery = '';
lineBulbaExit='';
bulbaExit = '';

                  
$.Animated = true;
Width = 320;
Delta = 23;
WidthSmall = Width / 2;
WidthBig = Width * 2;
TimeBox = 2000;    
isBubbleShow = false;

FirstL = 10;

SecondL = 180;
SecondR = 660;

ThirdL = 350;
ThirdR = 830;
                              
function registerRemotes()
{
	$('a.'+'part_events').remote('#eventsBox .inner .innerText .content', function() {openBox('events');  HideBubble(); registerRemotes();});
	$('a.'+'part_art').remote('#artBox .inner .innerText .content', function() {openBox('art');  HideBubble(); registerRemotes()});            
	$('a.'+'part_records').remote('#recordsBox .inner .innerText .content', function() {openBox('records');  HideBubble(); registerRemotes()});
	$('a.'+'part_bubble').remote('#bubbleBox .bubbleInner', function() {ShowBubble(); registerRemotes()});
	addHomeLink("a.homeLink"); 
	
}                              

function addHomeLink(selector)
{
	$(selector).remote('#eventsBox .inner .innerText .content', function() 
	{                                              
    $('#artBox .inner .innerText .content').load($('#artHomeLink').attr('title'), function ()
		{
    	$('#recordsBox .inner .innerText .content').load($('#recordsHomeLink').attr('title'), function ()
			{  
				if (!isBubbleShow)
					HideBubble(); 
				else
					isBubbleShow = false;
					
				returnHome(); registerRemotes();	
  			Height();                              									
			});
		});  
	});                     
	
}

$.userFunc = function(hash)
{
//  $.ajaxHistory.update(hash);
  partName = hash.slice(hash.indexOf("/part/")).split("/")[2];

  url = hash.slice(1);
//  alert(partName);
  switch (partName)
  {
    case "events":
      $('#eventsBox .inner .innerText .content').load(url, function (){	registerRemotes();openBox('events');});        
      break;
    case "art":
      $('#artBox .inner .innerText .content').load(url, function (){	registerRemotes();openBox('art');});        
      break;
    case "records":
      $('#recordsBox .inner .innerText .content').load(url, function (){	registerRemotes(); openBox('records');});              
      break;
    case "bubble":                 
			isBubbleShow = true;
      $('#bubbleBox .bubbleInner').load(url, function (){	registerRemotes(); $("a.logo").click(); ShowBubble();});
      break;
  }         
}


$(document).ready(function()
{       
    Height();    
    $('body').append('<div id="screen"><a href="#"></a></div>');
    $('#screen a').click(HideBubble);        
    Height();                               
    registerRemotes();          

		addHomeLink("a.logo");
		
    Height();                              
    $.ajaxHistory.initialize(function (){});    		            

		if (window.location.hash == "" || window.location.hash == "#")
		$("a.logo").click();
	
						
//    registerRemotes('', 'art');
//    registerRemotes('', 'records');    
});


function openBox(index)
{                     
    Height();                               
  
    if (Active == index) return;
    if (Animated) return;
    if ($.Animated == false) return;
    Animate = true;
    
    
    /*jQuery.fn.jcarousel = function() {
        return null;
    };*/
    
    
    $('.innerMenu').show();
    $('#' + index + 'Box .innerMenu').hide();
    $('.content').hide();
    $('#' + index + 'Box .content').show();
    $('.menu ul').hide();
    $('#' + index + 'Box .menu ul').show();        
    
    if (FirstTime)
    {
        $('#eventsBox').animate({width: (index == 'events' ? WidthBig : WidthSmall)}, TimeBox);
        $('#artBox').animate({width: (index == 'art' ? WidthBig : WidthSmall), left: '+=' + (index == 'events' ? Width+Delta : -WidthSmall+Delta)}, TimeBox);
        $('#recordsBox').animate({width: (index == 'records' ? WidthBig : WidthSmall), left: '+=' + (index == 'records' ? -Width-Delta : WidthSmall-Delta)}, TimeBox, CallbackFunc);
    }
    else
    {
        $('#eventsBox').animate({width: (index == 'events' ? WidthBig : WidthSmall)}, TimeBox);
        $('#artBox').animate({width: (index == 'art' ? WidthBig : WidthSmall), left: '+=' + (index == 'events' ? Width + WidthSmall : (Active == 'events' ? -(Width + WidthSmall) : 0))}, TimeBox);
        $('#recordsBox').animate({width: (index == 'records' ? WidthBig : WidthSmall), left: '+=' + (index == 'records' ? -(Width + WidthSmall) : (Active == 'records' ? Width + WidthSmall : 0))}, TimeBox, CallbackFunc);
    }
    Active = index;
    FirstTime = false;         
    
    return false; 
}


function returnHome()
{              
	  index = Active;
//    Height();                               
    if (Animated) return;
		if (Active == 'none') return
    Animate = true;
    
		BackWidth = Width;
    
    /*jQuery.fn.jcarousel = function() {
        return null;
    };*/
    
    $('.innerMenu').hide();
    $('.content').show();
    $('.menu ul').hide();
    
     $('#eventsBox').animate({width: Width-Delta}, TimeBox);
     $('#artBox').animate({width: Width+2*Delta, left: '+=' + (index == "art" ? WidthSmall-Delta : index == "events"? -Width-Delta : WidthSmall-Delta)}, TimeBox);
     $('#recordsBox').animate({width: Width-Delta, left: '+=' + (index == 'art' ? -WidthSmall+Delta : (Active == 'events' ? -WidthSmall+Delta : Width+Delta))}, TimeBox, CallbackFunc);
		Active = 'none';
		FirstTime = true;
		
    return false; 
}


// function Height() must be called after ajax content is loaded. (ajaxSuccess in jQuery)
function CallbackFunc()
{ 
    //$('#mycarousel').jcarousel({easing: 'easeOutBounce', animation : 2000});	
        
    $('#box' + Active + ' .menu ul').show();
    Height();
}

function Height()
{                                       
//	if (FirstTime)                                   
	$('ul.jcarousel-skin-tango').attr("id","mycarousel");
    $('div.content:visible #mycarousel').jcarousel({easing: 'easeOutBounce', animation : 2000, initCallback: function(){}});
    $('.jcarousel-skin-tango a').lightBox({imageBtnNext:			'/images/'+buttonsLang+'/lightbox-btn-next.gif',
							 imageBtnPrev:			'/images/'+buttonsLang+'/lightbox-btn-prev.gif',
							 imageBtnClose:			'/images/'+buttonsLang+'/lightbox-btn-close.gif',
							 lineBulba:				lineBulba,
							 nameGallery:			nameGallery,
							 lineBulbaExit:			bulbaExit });


    max = 0;
    $('.innerText').css('minHeight', 10);
    if ($.browser.msie && parseInt($.browser.version) <= 6)
        $('.innerText').css('height', 10);
    $('.innerText').each(function(){ max = Math.max(max, $(this).height());});
    $('.innerText').css('minHeight', max);
    if ($.browser.msie && parseInt($.browser.version) <= 6)
        $('.innerText').css('height', max);
        
    $('.main').height($('.box').height());
    
    $('.contact').css('top', $('.main').height() - $('.contact').height() - 18);
}

function ShowBubble()
{
    if ($.browser.msie && parseInt($.browser.version) <= 6)
	{
	    $(document).pngFix();
	}
    $('#screen').css('height', getyScroll());
    $('#screen').show();    
    $('#bubbleBox').slideDown(2000);
    
    return false;
}

function HideBubble()
{
    $('#bubbleBox').slideUp(2000, function(){ $('#screen').hide(); } );
    
    return false;
}

function getyScroll()
{
      yScroll = 0;

      if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX)
       {
        yScroll = window.innerHeight + window.scrollMaxY;
        xScroll = window.innerWidth + window.scrollMaxX;

        var deff = document.documentElement;
        var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
        var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;

        xScroll -= (window.innerWidth - wff);
        yScroll -= (window.innerHeight - hff);
       } 
      else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth)
       { // all but Explorer Mac
        yScroll = document.body.scrollHeight;
        xScroll = document.body.scrollWidth;
       } 
      else 
       { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        yScroll = document.body.offsetHeight;
        xScroll = document.body.offsetWidth;
       }

      return yScroll;
}              

