var lastmenu=0;
var switchid;
var firstmenu=0;
var bar_level=0;
var bar_target=0;
var barpickup=0;
var barlastx=0;
var tp=0;
var lastdiv;
var lastv;
var images_path = '/images/';
var content_path = '/content/';
var shop_images_path = content_path + 'shop/';
var shop_menu_id = 60;

document.CurentSort=1;
document.CurentCat=0;
document.CurentPage=1;
document.CurentId=0;
function showShopId(id,page,thisi) {
		document.CurentPage=page;
		document.CurrentCat=0;
		document.CurrentSort=1;
		document.CurentId=id;

		ShowShop();
}
function hidethem() {

	$(".barcontent:visible").hide();

	$("#barcontent"+lastdiv).css({height: 500});
	$("#barcontent"+lastdiv).show();
	$("#barcontent"+lastdiv).animate({opacity: 1},{queue: false, duration: 200});
	$('#download_client_label').css({backgroundPosition: '0px 0px', color: '#ffffff'});
	$('#download_client').css({backgroundPosition: '0px 0px'});
	
}
function bar_switch_content(divid) {
//	$(".barcontent").hide();
	if( divid!=0) {
	
	
	if (lastdiv!=divid)	{
	
		$(".barcontent:visible").animate({opacity: 0},{queue: false, duration: 200, complete: hidethem});
		lastdiv=divid;
	}
//	$("#barcontent"+divid).show();
//	$("#barcontent"+divid).animate({opacity: 1},{queue: false, duration: 200});

//	$("#barcontent"+divid).show();
//	$(".barcontent").animate({height},200);
}
}
function dotBar(v) {
		$('.barlabel_default').removeClass('barlabel_clicked');
		var g=v+1;
		
		$("barlabel"+g).addClass('barlabel_clicked');
ChangeBar(v);
}
function ChangeBar(v) {
	
	bar_switch_content(v+1);
	
	bar_target=v;
	StepBar();
	
}
function shop_show_about() {
    $("#pageNavigationDiv").hide();
	$(".shop_container").fadeOut(200);
	$(".shop_about").fadeIn(200);
}

function shop_about_back() {
    $("#pageNavigationDiv").show();
	$(".shop_container").fadeIn(200);
	$(".shop_about").fadeOut(200);
}

function StepBar() {
	
	if (bar_level<bar_target) {
		bar_level++;
		Bar_Full(bar_level);
	}
	if (bar_level>bar_target) {
		Bar_Empty(bar_level);
		bar_level--;
	}
	var bleft=bar_level*146-5;
	$("#bararrow").animate({left: bleft},100);
}
function Bar_Full(v) {
	$("#bar"+v).animate({backgroundPosition: '0 0'},100,function(){
   		StepBar();
	}); 
	//var c=document.setTimeout(StepBar(),500);
}
function Bar_Empty(v) {
	$("#bar"+v).animate({backgroundPosition: '-146px 0'},100,function(){
   		StepBar();
	});
	}
function MenuClick(x) {
	$(".menu_clicked").addClass('menu_element');
	$(".menu_clicked").removeClass('menu_clicked');

	var goto;
	$("#"+x).removeClass('menu_element');
	$("#"+x).addClass('menu_clicked');
	
	if (x.substr(0,8)=="submenu_") {
		goto=x.substr(8);
	}
	if (x.substr(0,10)=="clickmenu_") {
		goto=x.substr(10);
	}
	
	var curId=goto.substr(goto.lastIndexOf('_')+1,100);
	goto="#content_"+curId;
	$("#arrow").hide();
	document.newGoto=goto;	

	var url='/ajax/menu/'+curId+'/';
	$.ajax({
	  type: "GET",
	  url: url,
	  dataType: "text",
	  async: false,
	  success: ShowNewContent
	});
//	$(".content").hide();
	
}
function MenuClickHere(x) {
	$(".menu_clicked").addClass('menu_element');
	$(".menu_clicked").removeClass('menu_clicked');

	var goto;
	$("#"+x).removeClass('menu_element');
	$("#"+x).addClass('menu_clicked');
	
	if (x.substr(0,8)=="submenu_") {
		goto=x.substr(8);
	}
	if (x.substr(0,10)=="clickmenu_") {
		goto=x.substr(10);
	}
	
	var curId=goto.substr(goto.lastIndexOf('_')+1,100);
	goto="#content_"+curId;
	$("#arrow").hide();
	document.newGoto=goto;	

	var url='/ajax/menu/'+curId+'/';
	$.ajax({
	  type: "GET",
	  url: url,
	  dataType: "text",
	  async: false,
	  success: ShowNewContentHere
	});
//	$(".content").hide();
	
}
function ShowNews(id){
	document.newGoto='#content_news';
	var url='/ajax/news/'+id+'/';
	$.ajax({
	  type: "GET",
	  url: url,
	  dataType: "text",
	  async: false,
	  success: ShowNewContent
	});
}

function AjaxScreenShots(page){
	document.newGoto='#content_screenshots';
	var url='/ajax/shots/'+page+'/';
	$.ajax({
	  type: "GET",
	  url: url,
	  dataType: "text",
	  async: false,
	  success: ShowNewContentHere
	});
}
function AjaxLoadShopCat(cat){

	AjaxRefreshShop();

}
function AjaxLoadShop(page){
	AjaxRefreshShop();

}
function AjaxRefreshShop() {
	var url='/ajax/shop/';
	url=url+document.CurentPage+'/';
	url=url+document.CurentSort+'/';
	url=url+document.CurentCat+'/';
	url=url+document.CurentId+'/';
	
	$.ajax({
	  type: "GET",
	  url: url,
	  dataType: "text",
	  async: false,
	  success: ShopUpdate
	});
}
function AjaxScreenDesktops(page){
	document.newGoto='#content_desktops';
	var url='/ajax/desktops/'+page+'/';
	$.ajax({
	  type: "GET",
	  url: url,
	  dataType: "text",
	  async: false,
	  success: ShowNewContentHere
	});
}

function AjaxLoadVideo(){
	document.newGoto='#content_video';
	var url='/ajax/video/all/';
	$.ajax({
	  type: "GET",
	  url: url,
	  dataType: "text",
	  async: false,
	  success: ShowNewContentHere
	});
}

function AjaxLoadOther(key){
	document.newGoto='#content_other';
	var url='/ajax/other/'+key+'/';
	$.ajax({
	  type: "GET",
	  url: url,
	  dataType: "text",
	  async: false,
	  success: ShowNewContent 
	});
}

function AjaxLoadShopItem( i ){
	if ( typeof(i) == 'object' ) {
		var d = $('.shop_about .shop_block_about');
		
		if ( d.length == 0 )
			return; // exit function if div not found

		var defaulticon = 'shop_icon.gif';

		// clearing div elements
		$('.shop_icon',d).attr('src', images_path + defaulticon );
		$('.shop_flag',d).removeClass('shop_flag_new').removeClass('shop_flag_hit').removeClass('shop_flag_cool');
		$('.shop_about_title',d).text('');
		$('.shop_about_sex',d).hide();
		$('.shop_about_line2',d).html('');
		$('.shop_about_level .value',d).text('');
		$('.shop_about_cash',d).text('');
		var bpdiv = $('.shop_about_line4',d).html('');

		// filling div
		if ( i.icon )
			$('.shop_icon',d).attr('src', i.icon );

		switch ( Number(i.status) ) {
			case 1:
				$('.shop_flag',d).addClass('shop_flag_new');
				break;
			case 2:
				$('.shop_flag',d).addClass('shop_flag_hit');
				break;
			case 3:
				$('.shop_flag',d).addClass('shop_flag_cool');
				break;
		}

		if ( i.sex )
			$('.shop_about_sex',d).attr('src', images_path+'sex_'+i.sex+'.gif').show();
		$('.shop_about_title',d).text(i.title);
		$('.shop_about_line2',d).html(i.description);
		$('.shop_about_level .value',d).text(i.level);
		$('.shop_about_cash',d).text(i.cost_roubles); // cost in roubles - infinite

		if ( i.bigpics ) {
			var bigpics = i.bigpics.split(',');
			for( i in bigpics )
				bpdiv.append('<img src="' + shop_images_path + bigpics[i] + '" alt="">');
		}

		// showing div
		shop_show_about();

	} else if( !isNaN(i) && i > 0 ) {
		var url='/ajax/shopitem/'+i+'/';
		$.getJSON(url, null, AjaxLoadShopItem);
	} else {
		// ajax returned null or false
	}
}

function ShowNewsArchive(page){
	document.newGoto='#content_newsarchive';
	var url='/ajax/newsarchive/'+page+'/';
	$.ajax({
	  type: "GET",
	  url: url,
	  dataType: "text",
	  async: false,
	  success: ShowNewContent
	});
}

function ShowNewContent(req){
	$('#content_news').hide();
	$('#content_screenshots').hide();
	$('#content_video').hide();
	$('#content_desktops').hide();
	$('#content_other').hide();
	$('#content_newsarchive').hide();
	
	var obj= $('#content_header');
//	alert(req);
	obj.html(req);
	$(document.newGoto).show();
	$("body").scrollTo(1050, 700, {easing:'easeOutQuad'});
	FstyleInit();
}
function ShowNewContentHere(req){
//	$('#content_news').hide();
//	$('#content_screenshots').hide();
//	$('#content_video').hide();
//	$('#content_desktops').hide();
//	$('#content_other').hide();
//	$('#content_newsarchive').hide();
	
	var obj= $('#content_header');
	obj.html(req);
//	alert(req);
//	$(document.newGoto).show();
	FstyleInit();
//alert("VOULA!");
}
function ShopUpdate(req){
	
	var obj= $('#content_header');
	obj.html(req);
	FstyleInit();
}
function SwitchMenu() {
	if (lastmenu==-1) {
		$(".menu_selected").addClass('menu_element');
    	$(".menu_selected").removeClass('menu_selected');
    
		$("#arrow").hide();
		for (var i in document.menuIndexes) {
				$("#mainmenu_"+document.menuIndexes[i]+"_sub").animate({opacity: '0'}, { duration: 250, queue:false}).hide();
		}		
	}
	if (lastmenu>0) {
    	var pos = $("#mainmenu_"+lastmenu).position();
		
		$(".menu_selected").addClass('menu_element');
    	$(".menu_selected").removeClass('menu_selected');
		
    	var arTop=259;
    	var wi=$("#mainmenu_"+lastmenu).width()/2;
		var arLeft=pos.left+wi/2+140;
    	
    	$("#arrow").css({top: arTop});
		$("#arrow").animate({left: arLeft},100);
		$("#arrow").css({display: 'inline'});
		$("#mainmenu_"+lastmenu).removeClass('menu_element');
    	$("#mainmenu_"+lastmenu).addClass('menu_selected');
		
		var leftx=arLeft-50;
		$("#mainmenu_"+lastmenu+"_sub").css({left: leftx});
		if (firstmenu!=0) {
			for (var i in document.menuIndexes) {
					
					$("#mainmenu_"+document.menuIndexes[i]+"_sub").animate({opacity: '0'}, { duration: 250, queue:false}).hide();
			}
			
		}
		firstmenu=1;
		$("#mainmenu_"+lastmenu+"_sub").show();
		
		$("#mainmenu_"+lastmenu+"_sub").animate({opacity: '1'},{ duration: 250, queue:false }).show();
		init();
	}
}
function init() {
	$('.menu_element').hover (function(){
		var x=$(this).attr("id");
		if (x.substr(0,9)=="mainmenu_") {
			lastmenu=x.substr(9);
		}
		if (x.substr(0,10)=="clickmenu_") {
			lastmenu=-1;
		}		
		$(this).addClass('menu_element_selected');
		clearTimeout(switchid);
		switchid=setTimeout(SwitchMenu,400);
	}, function () {
		lastmenu=0;
		$(this).removeClass('menu_element_selected');
	});		
	
}
$(window).scroll(function()
{
  if ($(window).scrollTop()<400) $('#goup').animate({opacity: 0.0 },{queue: false, duration: 300});
  if ($(window).scrollTop()>400) $('#goup').animate({opacity: 1.0 },{queue: false, duration: 300});
  
  $('#goup').animate({height: $(window).height()-20, top:$(window).scrollTop()+"px" },{queue: false, duration: 300});
  
});	

function onLoadInit(){
	$('#goup').animate({opacity: 0.0 },{queue: false, duration: 300});
	$('body').mousemove(function(e) {
		if (barpickup==1) {
			var dif=e.pageX-barlastx;
			var pos=$('#bararrow').position();
			var pleft=pos.left+dif;
			if(pleft<-9) pleft=-9;
			if(pleft>720) pleft=720;
			
			$('#bararrow').css({left: pleft });
			barlastx=e.pageX;
		}
	});
	$('.menu_element').click(function() {
		var x=$(this).attr("id")

		if (x.substr(0,8)=="submenu_") {
			MenuClick(x);
		}
		if (x.substr(0,10)=="clickmenu_") {
			MenuClick(x);
		}
		if (x.substr(0,9)=="mainmenu_") {
			clearTimeout(switchid);
			lastmenu=x.substr(9);
			SwitchMenu();
		}
	});

	$("#bball_label").click(function(){ AjaxLoadOther('start')});
	$("#bball").click(function(){ AjaxLoadOther('start')});
	
	$("#bball_label").hover (function(){
		$('#bball_label').css({backgroundPosition: '0px -20px', color: '#000000'});
		//$('#bball').css({backgroundPosition: '0px -116px'});
		$('#bball').css({'backgroundPositionY':'-116px'});
		//alert(   $('#bball').css('backgroundPositionY')   );
	},function() {
		$('#bball_label').css({backgroundPosition: '0px 0px', color: '#ffffff'});
		//$('#bball').css({backgroundPosition: '0px 0px'});
		$('#bball').css({'backgroundPositionY':'0px'});
	});	
	
	$("#bball").hover (function(){
		$('#bball_label').css({backgroundPosition: '0px -20px', color: '#000000'});
		$(this).css({backgroundPosition: '0px -116px'});
	},function() {
		$('#bball_label').css({backgroundPosition: '0px 0px', color: '#ffffff'});
		$(this).css({backgroundPosition: '0px 0px'});
	});	
	
	$('.upicon').hover(function() {
		var IE='\v'=='v';
		if (!IE) {
			$(this).css({backgroundPosition: '0px 0px'});
		} else {
			$(this).css({backgroundPosition: '0px -42px'});
		}
	},function() {
		var IE='\v'=='v';
		if (!IE) {
			$(this).css({backgroundPosition: '0px -42px'});
		} else {
			$(this).css({backgroundPosition: '0px -84px'});
		}
	});
	$('.upicon').click(function() {
		$("body").scrollTo(0, 700, {easing:'easeOutQuad'});
	});
	var str=window.location+'';
	//loc =loc.indexOf('#content');
	//alert(   loc  );
	//var str="Hello world!";

	init();
}
function ShowShop() {
	AjaxRefreshShop();
	$("body").scrollTo(1000, 700, {easing:'easeOutQuad'});
}
function FstyleInit(){
	//$(document).pngFix(); 
	$("#sign_up_label").click(function(){});
	$("#sign_up").click(function(){});
	
	$("#sign_up_label").hover (function(){
		$('#sign_up_label').css({backgroundPosition: '0px -20px', color: '#000000'});
		$('#sign_up').css({backgroundPosition: '-90px 0px'});
	},function() {
		$('#sign_up_label').css({backgroundPosition: '0px 0px', color: '#ffffff'});
		$('#sign_up').css({backgroundPosition: '0px 0px'});
	});	

	$("#sign_up").hover (function(){
		$('#sign_up_label').css({backgroundPosition: '0px -20px', color: '#000000'});
		$(this).css({backgroundPosition: '-90px 0px'});
	},function() {
		$('#sign_up_label').css({backgroundPosition: '0px 0px', color: '#ffffff'});
		$(this).css({backgroundPosition: '0px 0px'});
	});
	
	$("#download_client_label").click(function(){});
	$("#download_client").click(function(){});
	
	$("#download_client_label").hover (function(){
		$('#download_client_label').css({backgroundPosition: '0px -20px', color: '#000000'});
		$('#download_client').css({backgroundPosition: '-120px 0px'});
	},function() {
		$('#download_client_label').css({backgroundPosition: '0px 0px', color: '#ffffff'});
		$('#download_client').css({backgroundPosition: '0px 0px'});
	});	
	
	$("#download_client").hover (function(){
		$('#download_client_label').css({backgroundPosition: '0px -20px', color: '#000000'});
		$(this).css({backgroundPosition: '-120px 0px'});
	},function() {
		$('#download_client_label').css({backgroundPosition: '0px 0px', color: '#ffffff'});
		$(this).css({backgroundPosition: '0px 0px'});
	});
	
	
	
	$('.barlabel_default').click(function() {
		$('.barlabel_default').removeClass('barlabel_clicked');
		$(this).addClass('barlabel_clicked');
		bar_target=$(this).attr('id').substr(8)-1;
		bar_switch_content(bar_target+1);
//		StepBar();
	});

	$('a[@rel*=lightbox_screenshots]').lightBox(); 
	$('a[@rel*=lightbox_wallpapers]').lightBox(); 
	$('a[@rel*=lightbox_userpics]').lightBox();
	$('a[@rel*=lightbox_specialA]').lightBox(); 
	$('a[@rel*=lightbox_specialB]').lightBox();
	$('a[@rel*=lightbox_specialC]').lightBox();
	$('a[@rel*=lightbox_specialD]').lightBox();
	
	$('a[@rel*=lightbox_specialNEWS1]').lightBox();
	$('a[@rel*=lightbox_specialNEWS2]').lightBox();
	$('a[@rel*=lightbox_specialNEWS3]').lightBox();
	$('a[@rel*=lightbox_specialNEWS4]').lightBox();
	$('a[@rel*=lightbox_specialNEWS5]').lightBox();
	$('a[@rel*=lightbox_specialNEWS6]').lightBox();
	$('a[@rel*=lightbox_specialNEWS7]').lightBox();
	$('a[@rel*=lightbox_specialNEWS8]').lightBox();
	$('a[@rel*=lightbox_specialNEWS9]').lightBox();
	$('a[@rel*=lightbox_specialNEWS10]').lightBox();
	$('a[@rel*=lightbox_specialNEWS11]').lightBox();
										
	ChangeBar(0);
	StepBar();
	for (var i in document.menuIndexes) {
		$("#mainmenu_"+document.menuIndexes[i]+"_sub").hide();
	}
	$('#bararrow').mousedown(function(e) {
		document.onselectstart = function() {return false;} // ie
  		document.onmousedown = function() {return false;} // mozilla
		barpickup=1;
		barlastx=e.pageX;
		
	});
	$('body').mouseup(function(e) {
		if (barpickup==1) {
			barpickup=0;
			var dif=e.pageX-barlastx;
			var pos=$('#bararrow').position();
			var pleft=pos.left+dif;
			var m=Math.round(pleft/150);
			bar_level=m;;
			var bleft=bar_level*146-5;
			for (var i=0;i<=m;i++) {
				$("#bar"+i).animate({backgroundPosition: '0px 0'},100);
			}
			for (var i=m+1;i<=5;i++) {
				$("#bar"+i).animate({backgroundPosition: '-146px 0'},100);
			}
			$("#bararrow").animate({left: bleft},100);	
			bar_target=m;
			var n=m+1;
			bar_switch_content(bar_target+1);
			$('.barlabel_default').removeClass('barlabel_clicked');

			$('#barlabel'+n+" a").addClass('barlabel_clicked');					
			document.onselectstart = function() {return true;} // ie
  			document.onmousedown = function() {return true;} // mozilla
		}
	});
	$('.screenshot').hover(function() {
		$(this).addClass('screenshothover');
	},function() {
		$(this).removeClass('screenshothover');
	});

	$('.shop_element')
	.click (function() {
		var id = String(this.id).replace(/shop-item-/,'');
		AjaxLoadShopItem( id );
	})
	.hover(function() {
		$(this).addClass('shop_element_hover');
	},function() {
		$(this).removeClass('shop_element_hover');
	});

	$('.minishop .element').click(function(){
		//MenuClick('clickmenu_' + shop_menu_id);

	});
	
	$('#video_scroller').mousewheel(function(event, delta) {
	if (delta > 0) {
		if (tp<0) {tp=tp+25;}
	} else if (delta < 0) {
	 	var h1=$('#video_chain').height();
	 	var h2=$('#video_scroller_container').height();

		if (tp> (0-h1+h2) ) {tp=tp-25;}
	}
		$('#video_chain').animate({top: tp},{queue: false, duration: 15});
		event.preventDefault();
	});

	$("#video_arrow_up").click(function() {
		if (tp<0) {tp=tp+250;}
		if (tp>0) tp=0;
		$('#video_chain').animate({top: tp},{queue: false, duration: 150});
	});
	$("#video_arrow_down").click(function() {
		var h1=$('#video_chain').height();
	 	var h2=$('#video_scroller_container').height();
		if (tp> (0-h1+h2) ) {tp=tp-250;}
		if (tp<(0-h1+h2)) {tp=(0-h1+h2);}
		$('#video_chain').animate({top: tp},{queue: false, duration: 150});
	});
	$("#video_arrow_up").hover (function(){
		$(this).css({backgroundPosition: '-30px 0px'});
	},function() {
		$(this).css({backgroundPosition: '0px 0px'});
	});
	$("#video_arrow_down").hover (function(){
		$(this).css({backgroundPosition: '-30px -30px'});
	},function() {
		$(this).css({backgroundPosition: '0px -30px'});
	});
	str=window.location+'';
	init();
}
$(document).ready(function(){
FstyleInit();
	if (str.indexOf("#content")!=-1 ) {
		$("body").scrollTo(1000, 700, {easing:'easeOutQuad',queue:'false'});
	}
onLoadInit();
});

