var Telek = {
	xml:null,
	tvData : {
		caption : "",
		gitems:[]
	},
	groupId:0,
	grLabel:"",
	catLabel:"",
	
	catId:1,
	catNum:1,
	lid:0,
	
	isLentaItems:0,
	isLentaCat:0,
	currContent : "movie",
	pages:1,
	currPage:1,

	lentaTimer :null,
	lentaTop : 0,
	beforeAndAfterTimer : null,
	beforeAndAfterTime : 2000,
	beforeAndAfterStandTime : 6000,

	hvost : null,
	
	init : function(id, lid)
	{
		clearTimeout(this.beforeAndAfterTimer);
		if(lid)
			this.lid=lid;
		else
			this.lid=0;
		//init lenta
		$("#tv-clenta").createAppend(
			'div', {className:'topArrow', id:"topArrow"}
			);
		$("#tv-clenta").createAppend(
			'div', {className:'bottomArrow', id:"bottomArrow"}
			);
		$("#topArrow").bind("mouseover", function(){
			Telek.runDown();
		});
		$("#bottomArrow").bind("mouseover", function(){
			Telek.runUp();
		});
		$("#topArrow").bind("mouseout", function(){
			clearTimeout(Telek.lentaTimer);
		});
		$("#bottomArrow").bind("mouseout", function(){
			clearTimeout(Telek.lentaTimer);
		});
		//--------

		$("#pg_start").unbind('click').bind('click', function(){Telek.back();});
		$("#pg_end").unbind('click').bind('click', function(){Telek.forward();});
		this.groupId = id;
		if(!this.xml)
		{
			$.get("/data.xml", function(xml){
				Telek.xml = xml;
				Telek.renderTv();
				}
			);
		}
		else
		{
			Telek.renderTv();
		}
	},

	renderTv : function()
	{
		if(this.isLentaItems)
			$('#podloga').SlideOutUp(500);
		this.isLentaItems=1;
		this.grLabel = $("//group[@id='"+this.groupId+"']", this.xml).attr("name");
		if(this.lid)
			this.catId =$("//group[@id='"+this.groupId+"']/category[@id='"+this.lid+"']", this.xml).attr("id");
		else
			this.catId =$("//group[@id='"+this.groupId+"']/category:first", this.xml).attr("id");
		var items = $("//group[@id='"+this.groupId+"']/category", this.xml);
		this.tvData.gitems=[];
		this.catNum = items.length;
		for(var i=0;i<items.length;i++)
		{
			var node = $("//group[@id='"+this.groupId+"']/category:eq("+(i)+")", this.xml);
			this.tvData.gitems[this.tvData.gitems.length]=[node.attr("id"), node.attr("thumb"), node.attr("name")];
		}
		$("#podloga").html("");
		for(i=0;i<this.tvData.gitems.length;i++)
		{
			var item = this.tvData.gitems[i];
			$("#podloga").createAppend('div', {className:"item", id:"cat_"+item[0]},[
				'img', {src:'/i/img/items/'+item[1], width:"86",height:"65",id:"cat_img_"+item[0],
					onclick:function()
					{
						Telek.pages =Telek.currPage=1;
						var id = this.id.replace("cat_img_", "");
						Telek.renderCategory(id);
					}
				}
				]
			);
			$("#cat_"+item[0]).createAppend(
				'div', {className:"capt"},[
					'a', {href:"/category-"+item[0]+".html", onclick:function()
						{
							$(this).parent().parent().find("img").click();
							return false;
						}
					}, item[2]
				]
				);
		}
		$('#podloga').SlideInUp(500);
		this.renderCategory(this.catId);
	},

	renderCategory : function(id)
	{
		if(document.getElementById("myvideo"))
		{
			try
			{
				document.getElementById("myvideo").stop();
			}
			catch (ex)
			{
			}
		}
		this.catId=id;
		if(this.isLentaCat)
		{
			$('#tv-ilenta').SlideOutRight(500);
			setTimeout("Telek.renderCategoryTimer()", 500);
		}
		else
		{
			this.isLentaCat=1;
			Telek.renderCategoryTimer();
		}
	},

	printVideo : function(src, w, h)
	{
//		$("#loading").css({display:"none"});
		return printVideo(src, w, h);
	},
	
	renderCategoryTimer : function()
	{
		clearTimeout(this.beforeAndAfterTimer);
		if(document.getElementById("myvideo"))
		{
			try
			{
				document.getElementById("myvideo").stop();
			}
			catch (ex)
			{
			}
		}
		$("#video").stop();
		var items=$("//group[@id='"+this.groupId+"']/category[@id='"+this.catId+"']/item", this.xml);
		if($(items[0]).attr("type")=="movie")
		{
			$("#sound").css({display:"none"});
//			$("#loading").css({display:"none"});
		}
		else
		{
			$("#sound").css({display:"block"});
		}
		this.pages=parseInt(items.length/5)+((items.length%5)?1:0);
		if(this.currPage==this.pages)
		{
			$("#pg_end").css({background:"url(i/prw.gif)", cursor:"default"});
		}else if(this.currPage==1)
		{
			$("#pg_start").css({background:"url(i/plw.gif)", cursor:"default"});
			$("#pg_end").css({background:"url(i/pry.gif)", cursor:"pointer"});
		}
		else
		{
			$("#pg_end").css({background:"url(i/pry.gif)", cursor:"pointer"});
			$("#pg_start").css({background:"url(i/ply.gif)", cursor:"pointer"});
		}
		$("#paging").css({visibility:Telek.pages==1?"hidden":"visible"});
		$("#pg_sep").html(this.currPage+" / "+this.pages);

		///------
		this.catLabel=$("//group[@id='"+this.groupId+"']/category[@id='"+this.catId+"']", this.xml).attr("name");
		$("#tv-ilenta").html("");
		for(i=(this.currPage-1)*5;i<items.length&&i<(this.currPage-1)*5+5;i++)
		{
			var node = $("//group[@id='"+this.groupId+"']/category[@id='"+this.catId+"']/item:eq("+i+")", this.xml);
			$("#tv-ilenta").createAppend(
				'div', {className:"imgCont", id:"th_img_"+i},[
					'img', {src:"i/img/items/"+node.attr("thumb"), className:"thumb", width:"86",height:"66", id:"img_cat_"+node.attr("id")}
				]
				);
			if(!$.browser.mozilla)
			{
				$("#th_img_"+i).createAppend('div', {className:"layer1", id:"l0_"+i}).SlideInLeft(1000);
				$("#th_img_"+i).createAppend('div', {className:"layer2", id:"l1_"+i}).SlideInLeft(1000);
				$("#th_img_"+i).createAppend('div', {className:"layer3", id:"l2_"+i}).SlideInLeft(1000);
				$("#th_img_"+i).createAppend('div', {className:"layer4", id:"l3_"+i}).SlideInLeft(1000);
			}
			$("#th_img_"+i).createAppend('div', {className:"layer", id:"la_"+i, 
				onmouseover: function(){
						if($.browser.mozilla) return;
						$("#"+(this.id.replace("a", "0"))).SlideOutUp(1000);
						$("#"+(this.id.replace("a", "1"))).SlideOutRight(1000);
						$("#"+(this.id.replace("a", "2"))).SlideOutDown(1000);
						$("#"+(this.id.replace("a", "3"))).SlideOutLeft(1000);
					},
				onmouseout: function()
				{
						if($.browser.mozilla) return;
						$("#"+(this.id.replace("a", "0"))).SlideInUp(1000);
						$("#"+(this.id.replace("a", "1"))).SlideInRight(1000);
						$("#"+(this.id.replace("a", "2"))).SlideInDown(1000);
						$("#"+(this.id.replace("a", "3"))).SlideInLeft(1000);
				},
				onclick : function()
				{
					if(document.getElementById("myvideo"))
					{
						try
						{
							document.getElementById("myvideo").stop();
						}
						catch (ex)
						{

						}
					}
					$("#video").stop();
//					$("#loading").css({display:"block"});
					var id =$(this).parent().children().attr('id').replace('img_cat_','');
					var item = $("//item[@id='"+id+"']", Telek.xml);
					try
					{
						document.getElementById("click").goMovie();
					}
					catch(e){}
					if(item.attr("type")=="movie")
					{
//						$("#loading").css({display:"none"});
						Telek.currContent="movie";
						$("#video").html(printVideo("/i/video/"+item.attr("file"), 448, 410));
					}
					else
					{
						$("#video").stop();
						$("#video").animate({opacity:0}, 1000);
						clearTimeout(Telek.beforeAndAfterTimer);
						if(item.attr("file").indexOf("http://") != -1)
						{
							Telek.loadOrderForm(item.attr("hvost"));
							Telek.loadImageTimer(item.attr("file"));
						}
						else
						{
							Telek.loadImageTimer("/i/img/pics/"+item.attr("file"));
						}
					}
				}
			});
		}
		var item=$("//category[@id='"+this.catId+"']/item:eq("+(this.currPage-1)*5+")", this.xml);
		if(item.attr("type")=="movie")
		{
			this.currContent="movie";
			setTimeout('$("#video").html(printVideo("/i/video/'+item.attr("file")+'", 448, 410))', 600);
		}
		else
		{
//			$("#loading").css({display:"block"});
//debugger;
			if(item.attr("file").indexOf("http://") != -1)
			{
				Telek.loadOrderForm(item.attr("hvost"));
				Telek.loadImageTimer(item.attr("file"));
			}
			else
			{
				Telek.loadImageTimer("/i/img/pics/"+item.attr("file"));
			}
		}
		$("#h2").text(this.grLabel+" :: "+this.catLabel);
		$('#tv-ilenta').SlideInRight(500);
	},
	
	loadOrderForm : function(hvost)
	{
		if(hvost)
		{
			var url = "data.php";
			url += "?"+hvost;
			$.get(url,
				function(data)
				{
					
					$("#orderForm").html(data);
					csel();
				}				
			);
		}
		else
		{
			$("#orderForm").html("");
		}
	},

	loadImageTimer : function(src)
	{
		var img = new Image();
		this.hvost = Math.random();
		img.src=src+"?"+Telek.hvost;
		img.onload = function()
		{
			$("#video").stop();
			$("#video").html("");
//			$("#loading").css({display:"none"});
			$("#video").html("<img id=\"imgTarget\" src='"+src+"?"+Telek.hvost+"' />");
			$("#video").UnFold(500, 30, function(){
					if(Telek.catId==11)
					{
						Telek.beforeAndAfterTimer=setTimeout("Telek.beforeAndAfter()", Telek.beforeAndAfterStandTime);
					}
				}
			);
			$("#video").animate({opacity:1}, 10);
		}
	},

	beforeAndAfter : function()
	{
		$("#video").animate({opacity:0},Telek.beforeAndAfterTime, function()
			{
				var top = ($("#imgTarget").css("top") == "0px")?"-336px":"0px";
				$("#imgTarget").css("top", top);
				$("#video").stop();
				$("#video").animate({opacity:1},Telek.beforeAndAfterStandTime, Telek.beforeAndAfter);
			}
		);
//		this.beforeAndAfterTimer=setTimeout("Telek.beforeAndAfter()", this.beforeAndAfterStandTime);
	},
	
	back : function()
	{
		if(this.currPage>1)
		{
			$("#video").stop();
			$("#video").show();
			this.currPage--;
			this.renderCategory(this.catId);
		}
	},

	forward: function()
	{
		if(this.pages>this.currPage)
		{
			$("#video").stop();
			$("#video").show();
			this.currPage++;
			this.renderCategory(this.catId);
		}
	},
	
	runUp : function()
	{
		if(Math.abs(this.lentaTop)>85*this.catNum-286)
		{
			clearTimeout(this.lentaTimer);
			return;
		}
		this.lentaTop -= 10;
		$("#podloga").css({top:Telek.lentaTop+"px"});
		this.lentaTimer = setTimeout("Telek.runUp()", 50);
	},

	runDown : function()
	{
		if(this.lentaTop==0)
		{
			clearTimeout(this.lentaTimer);
			return;
		}
		this.lentaTop += 10;
		$("#podloga").css({top:Telek.lentaTop+"px"});
		this.lentaTimer = setTimeout("Telek.runDown()", 50);
	}
}

function csel() {

	// -- ajax form
	//AForm.init();
//	debugger;
	// Selects
	$('select').combobox( { comboboxContainerClass: "comboboxContainer", comboboxValueContainerClass: "comboboxValueContainer", comboboxValueContentClass: "comboboxValueContent", comboboxDropDownClass: "comboboxDropDownContainer", comboboxDropDownButtonClass: "comboboxDropDownButton", comboboxDropDownItemClass: "comboboxItem", comboboxDropDownItemHoverClass: "comboboxItemHover", comboboxDropDownGroupItemHeaderClass: "comboboxGroupItemHeader", comboboxDropDownGroupItemContainerClass: "comboboxGroupItemContainer", animationType: "slide", width: "200px" });

}

function printVideo(src, w, h)
{
	s="";
	if(src && w && h)
	{
		src='http://'+document.location.host+"/"+src;
		var s = '<object id="mediaPlayer" classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715" standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject" height="'+h+'" width="'+w+'">';
		s += '<param name="url" value="'+src+'">';
		s += '<param name="id" value="myvideo">';
		s += '<param name="autoStart" value="1">';
		s += '<param name="bgColor" value="#000000">';
		s += '<param name="DisplayBackColor" value="1">';
		s += '<param name="showControls" value="true">';
		s += '<param name="TransparentatStart" value="1">';
		s += '<param name="AnimationatStart" value="true">';
		s += '<param name="StretchToFit" value="true">';
		s += '<param name="showstatusbar" value="true">';
		s += '<embed id="EMBEDmediaPlayer" src="'+src+'" type="application/x-mplayer2" bgcolor="000000" name="EMBEDmediaPlayer" autostart="1" showcontrols="1" showstatusbar="1" autorewind="0" height="'+h+'" width="'+w+'"/>';
		s += '</object>';
	}
	return s;
};


function cacheImg(arr)
{
	var imgs=[];
	for(i=0;i<arr.length;i++)
	{
		imgs[imgs.length]=new Image();
		imgs[imgs.length].src=arr[i];
	}
};
