var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
var stop = true;
var themes = new Array("mag","green","gray","blue","yel","dark");
var curTheme = 0;
function resizer()
{
	if($(window).width() < 1004)
	{
		if(isIE6)
		{
			$("#body").css("width", "auto");
		}
		else
		{
			$("#body").css("overflow-x", "visible");
		}
	}
	else
	{
		$("#body").css("overflow-x", "hidden");
		$("#top_black").css("left", "auto");
	}
}
function scrollName()
{
	var els = $("#games_box_in .el .name");
	var max_height = 0;
	for(var i=0; i<els.length; i++)
	{
		h = needHeight($(els[i]).height()) * (-1);

		if(max_height < h)
		{
			max_height = h;
		}
	}
	els.parent().height(max_height);
	$('.table_scroll_name').css('margin-top', max_height * (-1) + 'px')
}
function needHeight(num)
{
	return (num + 6) * (-1);
}

function changeTheme()
{
	var next = (curTheme == 5 ? 0 :  curTheme + 1);
	$('html').removeClass(themes[curTheme]).addClass(themes[next]);
	curTheme = next;
	//alert(curTheme);
	//supersleight.init();
}
$('#top_scrolling').ready(function(){
	$('#games_box .el .bord div').css('opacity', 0.3);
	$("#games_box").scrollable({hoverClass: 'hover',clickable: false}).circular().mousewheel().autoscroll({
		autoplay: false,
		interval: 2000,
		steps: 1
	});
//	$("#sroll_box .prev").click(function()
//		{
//			var w = ($("#games_box_in .el").length * 152 - 912) * (-1);
//			var mw = parseInt($("#games_box_in").css("margin-left"));
//			//alert(mw +" - "+w);
//			if(mw > w && stop){
//				stop = false;
//				$("#games_box_in").animate({marginLeft : "-=152px"}, 300, "linear", function(){stop = true;})
//			}
//			else{
//				if(stop){
//				stop = false;
//				$("#games_box_in").animate({marginLeft : "0px"}, 300, "linear", function(){stop = true;});
//				}
//			}
//		});
//		$("#sroll_box .next").click(function()
//		{
//			var w = ($("#games_box_in .el").length * 152 - 912) * (-1);
//			var mw = parseInt($("#games_box_in").css("margin-left"));
//			//alert(mw);
//			if(mw < (-2) && stop){
//				stop = false;
//				$("#games_box_in").animate({marginLeft : "+=152px"}, 300, "linear", function(){stop = true;})
//			}
//			else{
//				if(stop){
//					stop = false;
//					$("#games_box_in").animate({marginLeft : w +"px"}, 300, "linear", function(){stop = true;});
//				}
//			}
//		});
});
$(document).ready(function()
{
	
	resizer();
	scrollName();
	
	if(isIE6)
	{
		$("#bg_el").css("height", $(document).height() + "px");
	}
	$(window).resize(function()
	{
		resizer();
	});
	$("div", $("#first_level div").not(".act")).fadeOut("fast");
	
	// $("#first_level a.first_level_item").click(function()
	// {
		// var cur_div = $(this).parent();
		// $("#first_level div.act div").fadeOut(
			// "fast",
			// function()
			// {
				// $("#first_level div.act").removeClass("act");
				// cur_div.addClass("act");
				// $("#first_level div.act div").fadeIn("fast");
			// }
		
		// );
		// return false;
	// });	
	
	$("#first_level .el").hover(function()
	{
		var cur_div = $(this);
		$("#first_level div.act").removeClass("act");
		cur_div.addClass("act");
	},function(){
		$("#first_level div.act").removeClass("act");
	});

	$("#ad_show").click(function()
	{
		if ($("#adv_search_cont").css("visibility") == "hidden")
		{
			$("#search").addClass("act");
			$("#adv_search div span").html("[&ndash;]");
		}
		else
		{
			$("#search").removeClass("act");
			$("#adv_search div span").html("[+]");
		}
	});

	$("#search_type").change(function()
	{
		if ($(this).val() != 'games')
		{
			$('#adv_search').css('visibility','hidden');
			$("#search").removeClass("act");
		}
		else
		{
			$('#adv_search').css('visibility','visible');
		}
	});
	
//	$("#games_box_in .el").hover(function()
//	{
//		var name = $(".name", this);
//		name.show();
//	},function()
//	{
//		var name = $(".name", this);
//		name.hide();
//	});
//	$("#games_box_in .el > .bord div").css('opacity', 0.3);
//	$("#games_box_in .el").click(function()
//	{
//		var obj = $('.bord', this); 
//		if(!obj.hasClass('act'))
//		{
//			
//			var price = $('div .price', this).text();
//			var oid = $('div .oid', this).text();
//			var link = $('div .link', this).text();
//			var site_name = $('div .site_name', this).text();
//			var image = $('div .image', this).text();
//			var basket_id = 'games:'+oid+':'+price;
//			var new_image = new Image();
//			new_image.src = image; 
//			$('#main_game strong a').text(site_name).attr('href', link);
//			$('#main_game .add_in_basket').attr('id', basket_id);
//			$('#main_game .add_in_basket span.left').html(price+'<span><span>Br</span></span>');
//			new_image.onload = function(){
//				$('#main_bg').css('background', 'url('+image+') no-repeat scroll 0% 0%');
//			}
//
//			$("#games_box_in .el > .bord.act").removeClass('act');
//			obj.addClass('act');
//			
//		}
//		return false;
//	});
	$(".game").hover(function()
	{
		$(this).css({"background-color":"#efefef", "border-color":"#d5d5d5"});
	},function()
	{
		$(this).css({"background-color":"#fff", "border-color":"#fff"});
	});
	$(".more_block").hover(function()
	{
		$(".more_block_add", this).stop();
		$(".more_block_add", this).animate({marginTop: "-24px"}, 150 );

	},function()
	{
		$(".more_block_add", this).stop();
		$(".more_block_add", this).animate({marginTop: "0px"}, 150 );

	});
	//alert(curTheme);
	
	if(!isIE6)
	{
		//$('select').sSelect();
		// setInterval('changeTheme()', 5000);
	}

	// $(".game_info_screens a").hover(function()
	// {
		// $(".game_info_screens a span").css({"background":"#00a5ff", "opacity":"0.3"});
		// $("span", this).css({"background":"url(img/sc_overlay.gif) no-repeat", "opacity":"1"});
		
	// },function()
	// {
		// $(".game_info_screens a span").css({"background":"none", "opacity":"1"});
	// });
});
