$(function () {
	if (/#lab/.test(window.location.href)) {
		$(".lab").show();
	}

	$(".rounded h2").prepend ('<span class="rounded-left"></span>');
	$(".rounded h2").append ('<span class="rounded-right"></span>');
	var t = $("#login-left form div a");
	for (i=0; i<t.length; i++) {
		t[i].innerHTML += "<span>" + t[i].innerHTML + "</span>";
	}
	tabs = $(".tabs");
	tabs.each (function (){
		var tab = this;
		var list = $(this).find ("li");
		var i = 0;
		list.each (function () {
			var temp = $(tab).parent().find(".tab-element");
			this.list = list;
			this.i = i;
			var cacheID = temp[this.i].parentNode.getAttribute ("id");
			if (Cookies.get (cacheID)>0) {
				$(this.list).find("h2").removeClass ("active");
				$(this).find("h2").addClass ("active");
				temp.css ("display", "none");
				temp[Cookies.get (cacheID)].style.display = "block";
			}
			$(this).click (function (){
				$(this.list).find("h2").removeClass ("active");
				$(this).find("h2").addClass ("active");
				Cookies.set (temp[this.i].parentNode.getAttribute ("id"), this.i);
				temp.css ("display", "none");
				temp[this.i].style.display = "block";
				return false;
			});
			i++;
		});
	});
	jQuery('#drawcarousel').jcarousel({
							        wrap: 'circular',
								  scroll:1,
								  auto:30,
							        itemVisibleInCallback: {onBeforeAnimation: draw_itemVisibleInCallback },
							        itemVisibleOutCallback: {onAfterAnimation: draw_itemVisibleOutCallback }
							    });
	jQuery('#shopcarousel').jcarousel({
							        wrap: 'circular',
								  scroll:1,
								  auto:30,
							        itemVisibleInCallback: {onBeforeAnimation: shop_itemVisibleInCallback },
							        itemVisibleOutCallback: {onAfterAnimation: shop_itemVisibleOutCallback }
	
	});
	$("#hint .link").click (function () {
		hints();
	});
	hints();
	$(window).resize (function () {
		tempWidth = $("#draw").width();
		$(".jcarousel-skin-tango .jcarousel-container-horizontal").css ("width", tempWidth-40);
		$(".jcarousel-item").css ("width", tempWidth+40);
	});
	$("#resources").hover (function () {
			clearTimeout (this.timeout);
			this.timeout = setTimeout (function () {
				$("#resources .more").stop(true, true).slideDown();
			}, 700);
		}, function () {
			clearTimeout (this.timeout);
			this.timeout = setTimeout (function () {
				$("#resources .more").stop(true, true).slideUp();
			}, 1000);
	});

})
function hints () {
	$.getJSON ("/hint.php", "", function (data) {
			$("#hint div").html (data.hint);
		});
}
function textShadow (t) {
	for (i=0; i<t.length; i++) {
		t[i].innerHTML += "<span>" + t[i].innerHTML + "</span>";
	}
}
function draw_itemVisibleInCallback(carousel, item, i, state, evt)
							{
								carousel.remove(i);
							    var idx = carousel.index(i, drawlist.length);
							    var obj = carousel.add(i, draw_getItemHTML(drawlist[idx - 1]));
								tempWidth = $("#draw").width();
								setTimeout (function () {
									$(".jcarousel-item").css ("width", tempWidth+40);
								}, 100);
								
								$(".jcarousel-skin-tango .jcarousel-container-horizontal").css ("width", tempWidth-40);
								$(".jcarousel-list").css ("width", 10000);
								textShadow ($(obj).find(".green-button"));
								
							};

							function draw_itemVisibleOutCallback(carousel, item, i, state, evt)
							{
							    carousel.remove(i);
							};
function shop_itemVisibleInCallback(carousel, item, i, state, evt)
							{
								carousel.remove(i);
							    var idx = carousel.index(i, shoplist.length);
							    var obj = carousel.add(i, shop_getItemHTML(shoplist[idx - 1]));
								tempWidth = $("#draw").width();
								setTimeout (function () {
									$(".jcarousel-item").css ("width", tempWidth+40);
								}, 100);
								
								$(".jcarousel-skin-tango .jcarousel-container-horizontal").css ("width", tempWidth-40);
								$(".jcarousel-list").css ("width", 10000);
								
								textShadow ($(obj).find(".green-button"));
							};

							function shop_itemVisibleOutCallback(carousel, item, i, state, evt)
							{
							    carousel.remove(i);
							};
							

function draw_getItemHTML(item)
							{
								a = "";
								a += '<table class="itemsd">';
									a += '<tr>';
										a += '<td class="image" rowspan="5"><a href="'+item.url+'"><img src="'+item.image+'" alt="'+item.name+'" /></a></td>';
										a += '<td colspan="2"><h3><a href="'+item.url+'">'+item.name+'</a></h3></td>';
									a += '</tr>';
									a += '<tr>';
										a += '<td class="participants"><span>Участников</span></td>';
										a += '<td class="count">'+item.count+'</td>';
									a += '</tr>';
									a += '<tr>';
										a += '<td class="participants"><span>Стоимость</span></td>';
										a += '<td class="count">'+item.cost+'</td>';
									a += '</tr>';
									a += '<tr>';
										a += '<td class="participants"><span>Начнётся через</span></td>';
										a += '<td class="count"><nobr>'+item.start+'</nobr></td>';
									a += '</tr>';
									a += '<tr>';
										a += '<td colspan="2"><a href="'+item.url+'" class="green-button">Участвовать</a></td>';
									a += '</tr>';
								a += '</table>';


								return a;
							};
							
function shop_getItemHTML(item)
							{
								a = "";
								a += '<table class="itemsd">';
									a += '<tr>';
										a += '<td class="image" rowspan="5"><a href="'+item.url+'"><img src="'+item.image+'" alt="'+item.name+'" /></a></td>';
										a += '<td colspan="2"><h3><a href="'+item.url+'">'+item.name+'</a></h3></td>';
									a += '</tr>';
									a += '<tr>';
										a += '<td class="participants"><span>Стоимость</span></td>';
										a += '<td class="count">'+item.cost+'</td>';
									a += '</tr>';
									a += '<tr>';
										a += '<td class="participants"><span>На складе</span></td>';
										a += '<td class="count">'+item.count+'</td>';
									a += '</tr>';
									a += '<tr>';
										a += '<td colspan="2"><a href="'+item.url+'" class="green-button">Купить</a></td>';
									a += '</tr>';
								a += '</table>';


								return a;
							};
Cookies = {
	get: function (name) {
		var cookie = " " + document.cookie;
		var search = " " + name + "=";
		var setStr = null;
		var offset = 0;
		var end = 0;
		if (cookie.length > 0) {
			offset = cookie.indexOf(search);
			if (offset != -1) {
				offset += search.length;
				end = cookie.indexOf(";", offset)
				if (end == -1) {
					end = cookie.length;
				}
				setStr = unescape(cookie.substring(offset, end));
			}
		}
		return(setStr);
	},
	set: function (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
	}
}
