/* Copyright (c) 2009 Astronim* (astronim.com) */


$(function () {
	
	$('#menuTop LI A').click(function () {
		$('LI.current').removeClass('current');
		$(this).parents('LI').addClass('current');
	});
	
	$('#menuProd LI A').click(function () {
		$('LI.current').removeClass('current');
		$(this).parents('LI').addClass('current');
	});

	if (location.hash) {
		$('#menuTop LI A, #menuProd LI A').each(function () {
			
			if (this.href.match(location.hash.replace('#', ''))) {
				$(this).parents('LI').addClass('current');
			}
		});
	}
	
});


var menuProdOpened = false;
var menuProdMouseTopOut = true;
var menuProdMouseBottomOut = true;
var menuProdMouseHeaderOut = true; 
var menuProdTopHeight = null;

var pollOpened = false;
var pollMouseTopOut = true;
var pollMouseBottomOut = true;
var pollMouseHeaderOut = true; 
var pollTopHeight = null;


var menuProdTimerOut = null;
var pollTimerOut = null;

var timerOblako = null;
var timerStartFalsh = null;
var flashIs = true;

function outCheckMenuProd(){
	if (menuProdMouseTopOut == true && menuProdMouseBottomOut == true && menuProdMouseHeaderOut == true && menuClicked == false ){
		clearInterval(menuProdTimerOut);
		hideProdMenu();	
	}

}
function outCheckPoll(){
	if (pollMouseTopOut == true && pollMouseBottomOut == true && pollMouseHeaderOut == true){
		clearInterval(pollTimerOut);
		hidePoll();	
	}
}
function showProdMenu(){
	$('#menuProd .top').animate({top : 583-33-menuProdTopHeight}, 750, 'swing', function(){ menuProdOpened = true;  if (menuProdMouseTopOut == true && menuProdMouseBottomOut == true && menuProdMouseHeaderOut == true && menuClicked == false ){hideProdMenu();}} );
	$('#menuProdHeader').animate({top: 583-7-menuProdTopHeight}, 750, 'swing');
	$('#contentOuter').css('z-index', 600);
};

function hideProdMenu(){
	$('#menuProd .top').animate({top: 583-33}, 750, 'swing', function(){ menuProdOpened = false; menuClicked = false; }  );
	$('#menuProdHeader').animate({top: 583-15}, 750, 'swing', changeContentZindex700);
};
function showPoll(){
	$('#poll .top').animate({top : 683-23-pollTopHeight}, 750, 'swing', function(){pollOpened = true;  if (pollMouseTopOut == true && pollMouseBottomOut == true && pollMouseHeaderOut == true && pollClicked == false ){hidePoll();}} );
	$('#pollHeader').animate({top: 674-pollTopHeight}, 750, 'swing');
	$('#contentOuter').css('z-index', 600);
};

function hidePoll(){
	$('#poll .top').animate({top: 683-23}, 750, 'swing', function(){ pollOpened = false; pollClicked = false; }  );
	$('#pollHeader').animate({top: 674}, 750, 'swing', changeContentZindex700);
};

function changeContentZindex700 (){
	$('#contentOuter').css('z-index', 700);
};
		
function initScroll(){
	$('#content').jScrollPane({showArrows:true, scrollbarWidth: 18, arrowSize: 18});
}





var animationsPlayed = false;
var currentState = false;
var menuClicked = false;
var pollClicked = false;
/**
 * Callback функция для флэшки
 * вызов функции означает, что анимация вся проигралась
 */
var tweenFlashPlayed = function (state) {
			
	if (state=='index'){
		hideContent();
	}
	else {
		showContent();
	}
	
	animationsPlayed = false;
	currentState = state;

}

/**
 * Показываем страницу
 * @param string 	Тип страницы (second -> открытое облако, index -> титульная)
 */
var pageState = function(state) {

	if (flashIs != false){

		if (true == animationsPlayed) {
			setTimeout(pageState.bind(this), 500);
			return;
		}
		
		if (currentState == state) {
			return;
		}
			
		if ('index' == state){
			$('#contentOuter').hide();
			$('#closeBtn').hide();
			$('#logo_s').hide();
		}
		else {
			state = 'second';
			$('#logo_i').hide();
			$('#news').hide();
		}

		animationsPlayed = true;
		
		var checkPageState = function () {
			if (typeof(thisMovie('flashName').pageStateFlash) == 'undefined') {
				setTimeout(checkPageState, 1000);
			}
			else {
				thisMovie('flashName').pageStateFlash(state)
			}
		}
		
		checkPageState();

	}
	else {
		if (state=='index'){
			$('#contentOuter').hide();
			$('#closeBtn').hide();
			$('#noflash_sun_s').hide();
			$('#noflash_sun_i').show();
			$('#noflash_oblako').hide();
			$('#logo_i').show();
			$('#logo_s').hide();
			$('#news').show();
		}
		if (state=='second'){
			$('#noflash_sun_s').show();
			$('#noflash_sun_i').hide();
			$('#noflash_oblako').show();
			$('#news').hide();
			$('#contentOuter').show();
			$('#logo_i').hide();
			$('#logo_s').show();
			initScroll();
		} 

	}
}

function showContent() {

	$('#contentOuter').show();
	$('#logo_s').fadeIn(1000);

	$('#auth_inner').jqTransform({imgPath:'../i/'});
	$('#forms').jqTransform({imgPath:'../i/'});

	repaintTable();
	initScroll();
	$('#closeBtn').fadeIn(500);

}

function hideContent() {
	$('#closeBtn').hide();
	$('#news').show();
	$('#logo_i').show();
	//$('#logo_i').animate({opacity: "0"}, 0)
	//.animate({opacity: "1"}, 1000);
}


		
function flashLoaded(state){
	
	if(!location.hash) {
		pageState('index');
	}
	timerStartFalsh = setInterval(showMenus, 5000);
}

function flashIsNotDetected(state){
	flashIs = false;
	$('#flash').addClass('noflash');
	$('#logo_i').addClass('noflash');
	$('#logo_s').addClass('noflash');
	$('#news').addClass('noflash');
	$('#noflash_cow').show();
	$('#auth').show();
	$('#search').show();
	$('#menuBottom').show();
	$('#langBar').show();
	
	if (state=='index'){
		$('#news').show();
		$('#noflash_cow').show();
		$('#noflash_sun_i').show();
		$('#logo_i').show();
		showMenus();
	}
	if (state=='second'){
		$('#noflash_oblako').show();
		$('#noflash_sun_s').show();
		$('#logo_s').show();
		showMenus();
		initScroll(); // TEMP
	} 
	
}

function showMenus(){
	clearInterval(timerStartFalsh);
	$('#menuProd').show();
	$('#menuProdBottom').show();
	$('#menuProdHeader').show();
	menuProdInit();

	$('#poll').show();
	$('#pollBottom').show();
	$('#pollHeader').show();
	pollInit();

	$('#langBar').show();
	$('#menuTop').show();
	$('#copyright').show();
	$('#logo2').show();
	$('#astronim').show();
	$('#auth').show();
	$('#search').show();
	$('#menuBottom').show();
	
}


function menuProdInit(){
	menuProdTopHeight = $('#menuProd .top').height();
	
	$('#menuProd .top').css('top', 583-33);
	$('#menuProd .top').css('overflow', 'hidden');

	
	$("#menuProd .top").hover(
	  function () {
		menuProdMouseTopOut = false;
		if (menuProdOpened == false ){
			showProdMenu();
		}
		
	  }, 
	  function () {
		menuProdMouseTopOut = true;
		if (menuProdOpened == true){
			clearInterval(menuProdTimerOut);
			menuProdTimerOut = setInterval(outCheckMenuProd, 150);
		}
	  }
	);
	
	$("#menuProdBottom").hover(
	  function () {
		menuProdMouseBottomOut = false;
		if (menuProdOpened == false){
			showProdMenu();
		}
		
	  }, 
	  function () {
		menuProdMouseBottomOut = true;
		if (menuProdOpened == true){
			clearInterval(menuProdTimerOut);
			menuProdTimerOut = setInterval(outCheckMenuProd, 150);
		}
	  }
	);
	
	$("#menuProdHeader").hover(
	  function () {
		menuProdMouseHeaderOut = false;
		if (menuProdOpened == false){
			showProdMenu();
		}
		
	  }, 
	  function () {
		menuProdMouseHeaderOut = true;
		if (menuProdOpened == true){
			clearInterval(menuProdTimerOut);
			menuProdTimerOut = setInterval(outCheckMenuProd, 150);
		}
	  }
	);
	// for touchscreens and clickers
	$("#menuProdHeader").click(function(){
		if (menuProdOpened == false){
			menuClicked = true;
			showProdMenu();
		} else {
			hideProdMenu();
		}
	});
	//
}

function pollInit(){
	pollTopHeight = $('#poll .top').height();

	$('#poll .top').css('top', 683-23);
	$('#poll .top').css('overflow', 'hidden');

	pollFormHeight = $('#poll form').height();

	$('#poll form').css('height', pollFormHeight);

	
	$("#poll .top").hover(
	  function () {
		pollMouseTopOut = false;
		if (pollOpened == false ){
			showPoll();
		}
		
	  }, 
	  function () {
		pollMouseTopOut = true;
		if (pollOpened == true){
			clearInterval(pollTimerOut);
			pollTimerOut = setInterval(outCheckPoll, 150);
		}
	  }
	);

	$("#pollBottom").hover(
	  function () {
		pollMouseBottomOut = false;
		if (pollOpened == false){
			showPoll();
		}
		
	  }, 
	  function () {
		pollMouseBottomOut = true;
		if (pollOpened == true){
			clearInterval(pollTimerOut);
			pollTimerOut = setInterval(outCheckPoll, 150);
		}
	  }
	);
	
	$("#pollHeader").hover(
	  function () {
		pollMouseHeaderOut = false;
		if (pollOpened == false){
			showPoll();
		}
	  }, 
	  function () {
		pollMouseHeaderOut = true;
		if (pollOpened == true){
			clearInterval(pollTimerOut);
			pollTimerOut = setInterval(outCheckPoll, 150);
		}
	  }
	);
	// for touchscreens and clickers
	$("#pollHeader").click(
	  function () {
		if (pollOpened == false){
			pollClicked = true;
			showPoll();
		} else {
			hidePoll();
		}
	  }
	);
	//
}







/**
 * Ображение к flash клипу
 */
var thisMovie = function(movieName) {
	if (true == $.browser.msie) {
		return window[movieName]
	} else {
		return document[movieName]
	}
}

/**
 * Раскраска таблицы
 */
var repaintTable = function () {
	$("tr:nth-child(even)").addClass("chet");
	$("tr > td:nth-child(1)").addClass("first");
}
