var TicketsCollector = new function()
{	
	this.url = "";
	this.Blocks = {};
	this.SelectedSeats = {}; 
	this.currentBlock = null;
	this.LocationID = null;
	this.Images = {};
	
	this.showBlocksNavigation = function()
	{
		JsHttpRequest.query("/Pages/02-ticketpoint/ru/01-koncerty/tickets.php",{act: "getBlocks", LocationID: TicketsCollector.LocationID }, function(r,text)
		{		
			var FirstBlockID = null;
			var html = "";
			var prices;
			var ticketsexist;

			for( var BlockID in r)
			{
			
				if (r[BlockID].FreeTickets > 0 ) 
				
				{
					prices = r[BlockID].Prices.max != r[BlockID].Prices.min 
						? "<br>Цены от "+ r[BlockID].Prices.min  + " &euro; до "+  +r[BlockID].Prices.max +" &euro;"
						: "<br>Цена "+  +r[BlockID].Prices.max +" &euro;";
					

					if (!FirstBlockID && r[BlockID].FreeTickets > 0 )
						FirstBlockID = BlockID;
		
					html += '<a id="BlockLink' + BlockID + '"'
							+ ' style="cursor:pointer; '+(r[BlockID].FreeTickets>0?"":"color:#99bbc7;border:1px solid transparent")+ '"'
							//+  ' class=""' 
							+  ' onMouseOut="hideInfoText()" '
							+  ' onMouseOver="showInfoText(\'' + (r[BlockID].FreeTickets>0
								?'Свободных мест: '+r[BlockID].FreeTickets + prices
								:'Билетов нет')
							+'\')" '
							+  ' onClick="TicketsCollector.showBlock(\''
								+ TicketsCollector.LocationID + '\',\''
								+ BlockID + '\')'
							+'">БЛОК&nbsp;' +  BlockID + '</a>';			
					html += " ";
					
					ticketsexist = 1;
					
				} 
				
			}
			
			if (ticketsexist > 0) {
				$("TicketsBlocksNavigation").innerHTML = html;
				//$("TicketsBlocksNavigation").innerHTML = '<div style="width: 500px; background-image: url(/Images/BlocksNavigationBg.gif)" >'+html+'</div>';
				TicketsCollector.showBlock(TicketsCollector.LocationID, FirstBlockID);
			} else { 			
				$("TicketsBlocksNavigation").innerHTML = "<div class='errormessage'>Извините, билеты на данное мероприятие сейчас отсутствуют.</div><br />"; 
				$("LocationLegend").innerHTML = ""; 
			}
			
		}, true);
	}
	
	this.generateBlock = function(BlockID)
	{
	
		var Row, Seat, htmlSeats;
		var html = '<div class="title01" style="text-align: center;">Блок ' + BlockID + '</h2></div>';	
		//var SesselsCount = 0;
		for( var RowID in TicketsCollector.Blocks[BlockID])
		{
			// Start New Row
			Row = TicketsCollector.Blocks[BlockID][RowID];
			htmlSeats = '<td style="font-weight: bold; background-color: #71a0ae; color: #fff; cursor: auto;  white-space: nowrap;" nowrap="nowrap">'+RowID+'</td>';
			
			Sessels = 0;
			var Seats = 0;
			var FirstSeat = 0;
			var LastSeat = 0;
			var Seatscnt = 0;

			for(SeatI in Row)
			{	
				if (Seats == 0) var FirstSeat = SeatI;
				Seats++;
				LastSeat = SeatI;
			}
			
			//SesselsCount = 0;
			//for(SeatID in Row)
				//SesselsCount++;
			
            for(SeatID in Row)
			{	
				Seatscnt++;
				
				var Seatr = (LastSeat - Seatscnt + 1);

				if (TicketsCollector.LocationID	== 1230 && BlockID !== "Block 3")
				
				{		
				
					Seat = Row[Seatr]; 
					
				} else {
				
					Seat = Row[SeatID]; 
								
				}
				Sessels++;			
			   	
			   	//*/ Beak Row?
               	if ( Sessels % 26 == 0 )
               	{
               		html += "<table class='areablocks' align='center' style='margin-bottom: 2px;'><tr>" + htmlSeats + "</tr></table>";
               		htmlSeats = '<td style="padding: 0"><img src="/Images/RowBreak.gif"></td>'
               		+'<td style="font-weight: bold; background-color: #71a0ae; color: #fff; cursor: auto; white-space: nowrap;" nowrap="nowrap">'+RowID+'</td>';
               	} //*/
             
               	if (TicketsCollector.LocationID	== 1230 && BlockID !== "Block 3") {		

					// Create  Seat	
					if ( TicketsCollector.SelectedSeats[BlockID+"-"+RowID+"-"+Seatr] )
					{
						htmlSeats 	+= '<td style="background-color: #ff9c00; color:#FFFFFF";   '
									+  'onMouseOut="hideInfoText()" '
									+  'onMouseOver="TicketsCollector.generateWindowHTML([\''+BlockID+'\',\''+RowID+'\',\''+Seatr+'\'])" '
									+  'onClick="TicketsCollector.alertlogin()">'
									+  '>'+Seatr+'</td>';
					}
								
					else if (Row[Seatr][1] !== undefined && Row[Seatr][1] == "0")
					{
						htmlSeats 	+= '<td style="" '
									+  'onMouseOut="hideInfoText()" '
									+  'onMouseOver="TicketsCollector.generateWindowHTML([\''+BlockID+'\',\''+RowID+'\',\''+Seatr+'\'])" '
									+  'onClick="TicketsCollector.alertlogin()"'
									+  '>'+Seatr+'</td>';
					}
					else
						htmlSeats += '<td style="background-color: #FFFFFF; color: #555555; cursor: default;">'+Seatr+'</td>';
				
				} else {
					// Create  Seat	
					if ( TicketsCollector.SelectedSeats[BlockID+"-"+RowID+"-"+SeatID] )
					{
						htmlSeats 	+= '<td style="background-color: #ff9c00; color:#FFFFFF";   '
									+  'onMouseOut="hideInfoText()" '
									+  'onMouseOver="TicketsCollector.generateWindowHTML([\''+BlockID+'\',\''+RowID+'\',\''+SeatID+'\'])" '
									+  'onClick="TicketsCollector.alertlogin()">'+SeatID+'</td>';
					}
								
					else if (Row[SeatID][1] == "0" && Seat[4] !== "1" )
					{
						htmlSeats 	+= '<td style="" '
									+  'onMouseOut="hideInfoText()" '
									+  'onMouseOver="TicketsCollector.generateWindowHTML([\''+BlockID+'\',\''+RowID+'\',\''+SeatID+'\'])" '
									+  'onClick="TicketsCollector.alertlogin()">'+SeatID+'</td>';
					}
					else
						htmlSeats += '<td style="background-color: #FFFFFF; color: #555555; cursor: default;">'+SeatID+'</td>';
				
				}				
					
				// DistanceAfter ?
               	if ( Seat[3] !== undefined && Seat[3] == SeatID )
               	{
               		htmlSeats += '<td style="background-color: #FFFFFF; cursor: default; border:0px;"></td>';
				               		
               	}
			}
			
			html += "<table class='areablocks' align='center' style='margin-bottom: 2px;'><tr>" + htmlSeats + "</tr></table>";
		}
		
	
		$('LoadInfo').style.display = "none";
		$('LocationArea').innerHTML = html;
		$('LocationArea').style.display = "";
	}
	
	this.showBlock = function(LocationID, BlockID)
	{
		$('LoadInfo').style.display = "";
		$('LocationArea').style.display = "none";
	
		if (TicketsCollector.currentBlock)
		{
			$('BlockLink'+TicketsCollector.currentBlock).className = "";
		}


		$('BlockLink'+BlockID).className = "ActiveBlock";
		
		TicketsCollector.currentBlock = BlockID;

		if ( !TicketsCollector.Blocks[BlockID] )
		{
			JsHttpRequest.query("/Pages/02-ticketpoint/ru/01-koncerty/tickets.php",{act: "getBlock", LocationID: LocationID, BlockID: BlockID }, function(r,text)
			{		
				TicketsCollector.Blocks[BlockID] = r["Rows"];
				TicketsCollector.generateBlock(BlockID);
			}, true);
		}else
			TicketsCollector.generateBlock(BlockID);
	}
	
	this.seatOnClick = function(obj, Location)
	{	
		// alert(obj.style.backgroundColor);
		
		if (obj.style.backgroundColor)
		{
			obj.Clicked = false;
			obj.style.backgroundColor = "";
			obj.style.color = "";
			TicketsCollector.removeSeats(Location);
		}
		else 
		{//0E41520E4152
			obj.Clicked = true;
			obj.style.backgroundColor = "#ff9c00";
			obj.style.color = "#FFFFFF";
			TicketsCollector.addSeat(Location);
		}
		TicketsCollector.generateWindowHTML(Location);
	}
	this.addSeat = function (a){			
		TicketsCollector.SelectedSeats[a[0]+"-"+a[1]+"-"+a[2]] = a;
		TicketsCollector.showPreorderInfo();
		TicketsCollector.showBlock(TicketsCollector.LocationID, a[0]);
	}
	this.removeSeats = function (a){
		delete 	TicketsCollector.SelectedSeats[a[0]+"-"+a[1]+"-"+a[2]];
		TicketsCollector.showPreorderInfo();
		TicketsCollector.showBlock(TicketsCollector.LocationID, a[0]);
	}	
	
	this.showPreorderInfo = function()
	{
		$("PreOrderInfo").style.fontSize ="9px"
		var html = "";
		var style = "";
		var i = 0;
		for(var k in TicketsCollector.SelectedSeats)
		{
			style = (i%2==0 ) ? "background-color:#e4f1f7":"background-color:#d5e0e6";
	
			html += "<tr style='"+style+"'>"
			   		+ "<td style='cursor: pointer' onclick=\"TicketsCollector.showBlock(TicketsCollector.LocationID,'"+TicketsCollector.SelectedSeats[k][0]+"')\">" + TicketsCollector.SelectedSeats[k][0] + "</td>"
					+ "<td>" + TicketsCollector.SelectedSeats[k][1] + "</td>"
					+ "<td>" + TicketsCollector.SelectedSeats[k][2] + "</td>"
					+ "<td style='color:#FF0000;cursor:pointer; font-size: 11px' onclick=\"TicketsCollector.removeSeats(["
						+ "'" + TicketsCollector.SelectedSeats[k][0] + "',"
						+ "'" + TicketsCollector.SelectedSeats[k][1] + "',"
						+ "'" + TicketsCollector.SelectedSeats[k][2] + "'"
					+"])\" title='удалить' label='удалить'><img src='/Images/x.gif'></td>"
				+ "</tr>";
			i++;
		}	
		
		if (i == 0 )
		{
			$('PreOrderInfo').style.display = "none";
			return;
		}
		
		$('PreOrderInfo').innerHTML = "<div>"+TicketsCollector.Images.InfoLabel+"<img src='/Images/arrow01.gif'></div>"
		
			+"<div class=\"hr\" style='margin:0'></div>" 
			+"<table  width=\"130\" class='PreorderInfo'>" 
			+"<tr>"			
			+"<th>" 
			    + TicketsCollector.Images.Block
				+ "</th>"
			+"<th>" 
			    + TicketsCollector.Images.Row
				+ "</th>"
			+"<th colspan='2'>" 
			    + TicketsCollector.Images.Seat
				+ "</th>"
			+"</tr>"
			+ html
			+"<tr><td colspan=\"4\" class=\"PreorderInfoBottom\"><nobr>Уже выбрано " + TicketsCollector.sumSeats()[1] + " на " + TicketsCollector.sumSeats()[0] + " &euro;</nobr></td></tr>"
			+"</table>";
			
		$('PreOrderInfo').style.display = "";
		
	}
	
	this.generateWindowHTML = function (l)
	{
		var html = "";
		var Seat = TicketsCollector.Blocks[l[0]][l[1]][l[2]];
		
		html 	= "Блок: " + l[0] + "<br>"
				+ "Ряд: " + l[1] + "<br>"
				+ "Место: " + l[2] + "<br>"
				+ "Цена: " + Seat[2] + " &euro;<br>";
	
		var Summ = TicketsCollector.sumSeats();
		if (Summ[1] > 0)
		{
			html += "<div class=\"hr\" style=\"margin:0\"></div>";
			html += "<nobr>Уже выбрано " + Summ[1] + " на " + Summ[0] + " &euro;</nobr>";
		}
	
		showInfoText(html);
	}
	
	this.alertlogin = function (l)
	{
		var html = "<table border=0><tr><td class=alertlogin>Для заказа билетов зарегистрируйтесь или&nbsp;войдите под своим логином в левой колонке!</td></tr></table>";
			
		showInfoText(html);
	}	
	
	this.sumSeats = function ()
	{
		var Summ = 0;
		var Count = 0;
		var Seats = TicketsCollector.SelectedSeats;
		for( k in Seats)
		{
			Summ += TicketsCollector.Blocks[Seats[k][0]][Seats[k][1]][Seats[k][2]][2].toInt();
			Count++;
		}
		return [Summ,Count];
	}
	
	this.orderTickets = function ()
	{
		var Summ =  TicketsCollector.sumSeats();
		if (Summ[1] == 0)
		{
			alert("Для заказа Вам нужно выбрать как минимум одно место!");
			return;
		}
		
		JsHttpRequest.query("/events.php", { 
			act: "addTickets", 
			Tickets:  TicketsCollector.SelectedSeats }, function(req,text)
		{	
			if (req.Result == "OK" )
			{
				$("ShopBar").style.display = "";
				location.href = '/ticketpoint/ru/basket/';
			}
		}, true);
	};
	
}

