//tickets
$(document).ready(function(){
	initMapareaEvent();
	getTablePtice();
	getTableAllowsell();
	$.getJSON('/tickets/countofhome.json',function(data){
		$('td.count_games p.font_size33').html(data.Content);
	});
});


function initMapareaEvent(){
var ticketDelay;
var block = $("#block_left")
var dopinfo_tickets = $("#dopinfo_popup"),
dopinfo_square = dopinfo_tickets.find("span.square"),
dopinfo_placeName = dopinfo_tickets.find("span.place_name"),
dopinfo_view = dopinfo_tickets.find("img"),
/*dopinfo_tariffMinPrice = dopinfo_tickets.find("span.tariff_min_price"),
dopinfo_ticketPrice = dopinfo_tickets.find("span.ticket_price"),
dopinfo_tariffList = dopinfo_tickets.find("span.tariff_list");*/
dopinfo_ticketPriceA = dopinfo_tickets.find("span.ticket_catA");
dopinfo_ticketPriceB = dopinfo_tickets.find("span.ticket_catB");
dopinfo_phoneName = dopinfo_tickets.find("span.phone_name");
dopinfo_phoneNumber = dopinfo_tickets.find("span.phone_number");

dopinfo_tickets.mouseenter(function(){
	clearTimeout(ticketDelay);
});
dopinfo_tickets.mouseleave(function(){
	clearTimeout(ticketDelay);
	ticketDelay = setTimeout(function(){dopinfo_tickets.css("left","-10000px");current_target = '';},300);
});
var current_target;
$("area").each(function(){
	var area = $(this);
	area.mousemove(function(event){
	clearTimeout(ticketDelay)
		if(current_target != event.target){
			ticketDelay = setTimeout(function(){
				current_target = event.target;
				var placeId = area.attr("rel");
				var placeName = area.attr("alt");
				var placeView = (placeId>51)?'test_seat':placeId;
				$.getJSON('/tickets/popup/'+placeId+'.json', function(data) {
					/*var tariffList = "";
					var tariffMaxDiscount = 0;
					for (g in data.Content.tariffs){
						if (g != 0) {tariffList += ", "}
						tariffMaxDiscount = Math.max(tariffMaxDiscount,data.Content.tariffs[g].discount)
						tariffList += data.Content.tariffs[g].name;
					}
					dopinfo_tariffMinPrice.text(data.Content.prices[0].price * (100 - tariffMaxDiscount) / 100);
					dopinfo_ticketPrice.text(data.Content.prices[0].price);*/
					dopinfo_ticketPriceA.text(data.Content.prices[1].price);
					dopinfo_ticketPriceB.text(data.Content.prices[0].price);
					dopinfo_placeName.text(placeName);
					dopinfo_square.css('background-color','#'+getColor(data.Content.prices[0].id));
					dopinfo_view.attr('src','/img/arena/'+placeView+'.jpg');
					//dopinfo_tariffList.text(tariffList);
					dopinfo_phoneName.text((data.Content.arena_id[0].arena_id == "1")? "Касса ДС \"Ледовый\"":"Касса СК \"Юбилейный\"");
					dopinfo_phoneNumber.html((data.Content.arena_id[0].arena_id == "1")? "<b>(812) 718-66-20</b>":"<b>(812) 498-60-33</b>");
					dopinfo_tickets.css({top:Math.round(event.pageY) - Math.round(block.offset().top) - Math.round(dopinfo_tickets.outerHeight())+"px",left:Math.round(event.pageX) - Math.round(block.offset().left)+"px"});
				});
			},100);
		}
	});
	area.mouseout(function(){
		clearTimeout(ticketDelay);
		ticketDelay = setTimeout(function(){dopinfo_tickets.css("left","-10000px");current_target = '';},300);
		//console.log("out");
	});
});
}


function setColor(){
	$('td[rel^=priceId_]').each(function(){
		$(this).css("background","#"+getColor($(this).attr("rel").substr(8)))
	});
}

function getTablePtice(){
	/*$.ajax({
		url: "/tickets/prices.html",
		success: function(data){
        	$("#ajax_table_price").html(data);
        	setColor();
		}});*/
}
function getTableAllowsell(){
	$.ajax({
		url: "/championship/"+lng.lang+"/allowsell.html",
		success: function(data){
        	$("#ajax_table_allowsell").html(data);
        	setColor();
		}});
}
