var xmlhttp;
var xmlDoc;
var rrreee=0;
var active_clip=undefined;
var clip_link= '';
var currentState1 = ''; 
var currentState2 = ''; 
var variable_one = 0;
var player = null;
var namevalue = '';
var mailvalue = '';
var mesbody = '';
var currentS1 = '';
var currentS2 = '';
var category=1;
var keyw='';
var vid='';


function keywoo(keii){
keyw=keii;
category=1;
loadXMLDoc('video/swhtv_xml.php?show=keyword&keyword='+keii+'');
pageTracker._trackPageview("video_search:"+keii);
parkraso(1);
button_cmd("k");
		 try
       {
        setTimeout("play(0,Idds[0])",500);
        } catch (e){}

}

function keywoo_akcija(keii){
keyw=keii;
category=3;
loadXMLDoc('video/swhtv_xml.php?show=k_vip&keyword='+keii+'');
pageTracker._trackPageview("video_search:"+keii);


button_cmd("k");
		 try
       {
        setTimeout("play(0,Idds[0])",500);
        
        setTimeout("parkraso(3)",1500);
        } catch (e){}

}

function video_vid(value){
vid=value;
category=1;		
loadXMLDoc('video/swhtv_xml.php?show=vid&vid='+value+'');
pageTracker._trackPageview("video_id:"+value);
parkraso(1);
button_cmd("k");
		 try
       {
        //setTimeout("play(0,Idds[0])",500);
        } catch (e){}
}

function playerReady(thePlayer) {
	player = window.document[thePlayer.id];
	addListeners();
}

function addListeners() {
    if (player) { 
      addAllControllerlListeners();
        addAllModelListeners();
	//  addAllViewListeners();
    } else {
        setTimeout("addListeners()",100);
    }
}

function addAllModelListeners() {
    player.addModelListener("LOADED", "stateListener");//{newstate,oldstate,id,client,version}.
    player.addModelListener("STATE", "statusaListener");//{newstate,oldstate,id,client,version}. 
}

function addAllControllerlListeners() {
    player.addControllerListener("PLAY", "doNothing"); //{state,id,client,version}. 
    player.addControllerListener("ITEM", "doNothing"); //{state,id,client,version}. 
}

function stateListener(obj) { 
	currentState1 = obj.loaded; 
	currentState2 = obj.total; 
    if ((currentState1==currentState2) && (variable_one==0)) { 
		something('load','100',clip_link);
		variable_one=1;
	}
}



function button_cmd(command){
	if (command=="k")
	{
	namevalue = '';
  mesbody = '';
		//tv_list('k');
		document.getElementById("writeroot").style.display = "";
		document.getElementById("commroot").style.display = "none";
		 try
       {
		document.getElementById('bt_slavet').src='video/bt_slavet.gif';
		document.getElementById('ln_slavet').href='javascript:button_cmd(\'comment\');';		
		       }
     catch (e)
      {}
	}
	if (command=="comment")
	{
		tv('comment');
		document.getElementById("writeroot").style.display = "none";
		document.getElementById("commroot").style.display = "";
		 try
       {
      	document.getElementById('bt_slavet').src='video/bt_atpakal.gif';
      	document.getElementById('ln_slavet').href='javascript:button_cmd(\'k\');';
       }
     catch (e)
      {}

	}


          pageTracker._trackPageview("swhtv:click_on_video_page");
}


if (window.XMLHttpRequest) { http=new XMLHttpRequest(); } 
else if (window.ActiveXObject) { http=new ActiveXObject("Microsoft.XMLHTTP"); }

function tv(fnc,pg) {
	var rand_no = Math.random();
  http.abort();
    element="commroot";
	//setTimeout ( document.getElementById(element).innerHTML = "<img src='/swh/image/loading.gif'>", 500 );
if (fnc=="comment"){
  http.open("GET", "video/swhtv_playlist.php?show=comment&id="+active_clip+"&start="+pg+"&data=" + rand_no, true);
  element="commroot";
} else if (fnc=="commentsave"){
  namevalue = encodeURIComponent(namevalue);
  mailvalue = encodeURIComponent(mailvalue);
  mesbody = encodeURIComponent(mesbody);
  http.open("GET", "video/swhtv_playlist.php?show=commentsave&id="+active_clip+"&name="+namevalue+"&mail="+mailvalue+"&mesbody="+mesbody+"&start="+pg+"&data=" + rand_no, true);
  element="commroot";
  setTimeout('mesbody=""; namevalue="";', 500);
  
} else if (fnc=="pedejie"){
  http.open("GET", "video/swhtv_playlist.php?show=pedejie&data=" + rand_no, true);
  element="writeroot";
} else if (fnc=="keywords"){
  http.open("GET", "video/swhtv_playlist.php?show=popular&data=" + rand_no, true);
  element="writeroot";
}

  http.onreadystatechange=function() {
    if(http.readyState == 4) {
	document.getElementById(element).innerHTML = "";
	//document.getElementById("writeroot").innerHTML = "";
    document.getElementById(element).innerHTML = http.responseText;
    }
  }
  http.send(null);
}

function tv_list (fnc,pg){
	var rand_no = Math.random();
	http.abort();
	button_cmd('k');
    element="writeroot";
	document.getElementById(element).innerHTML = "<img src='/swh/image/loading.gif'>";
	if (fnc=="koncerts"){
		theFile='video/swhtv_xml.php?show=koncerts&start='+pg+'&data=';
		loadXMLDoc(theFile);
		document.getElementById('writeroottable').style.background="#628980";
	} else if (fnc=="k"){
		theFile='video/swhtv_xml.php?show=k&start='+pg+'&data=';
		loadXMLDoc(theFile);
	} else if (fnc=="k_pop"){
		theFile='video/swhtv_xml.php?show=k_pop&start='+pg+'&data=';
		category=2;
		loadXMLDoc(theFile);
		parkraso(2);
		 try
       {
        //setTimeout("play(0,Idds[0])",500);
        } catch (e){      
        }
	} else if (fnc=="k_new"){
		theFile='video/swhtv_xml.php?show=k_new&start='+pg+'&data=';
		category=1;		
    loadXMLDoc(theFile);
		parkraso(1);
		 try
       {
        //setTimeout("play(0,Idds[0])",500);
        } catch (e){}
 } else if (fnc=="k_vip"){
		theFile='video/swhtv_xml.php?show=k_vip&start='+pg+'&data=';
		category=3;
		loadXMLDoc(theFile);
		parkraso(3);
		 try
       {
        //setTimeout("play(0,Idds[0])",500);
        } catch (e){}
	} else if (fnc=="keyword"){
		theFile='video/swhtv_xml.php?show=keyword&keyword='+keyw+'&start='+pg+'&data='+rand_no;
		category=1;		
    loadXMLDoc(theFile);
		parkraso(1);
	} else if (fnc=="vid"){
		theFile='video/swhtv_xml.php?show=vid&vid='+vid+'&start='+pg+'&data='+rand_no;
		category=1;		
    loadXMLDoc(theFile);
		parkraso(1);
 	} else if (fnc=="initialize3"){
		theFile='video/swhtv_xml.php?show=mp4&start='+pg+'&data=';
		loadXMLDoc(theFile);
	}
}


function loadXMLDoc(theFile) {
  xmlhttp=null;
  if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } 
  else if (window.ActiveXObject) { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }

  if (xmlhttp!=null) {
    xmlhttp.onreadystatechange=loaded;
    xmlhttp.open("GET",theFile,true);
    xmlhttp.send(null);
    		 try
       {
        setTimeout("play(0,Idds[0])",1000);
        } catch (e){}
  } else { alert('Sorry, your browser can\'t handle this script'); return;  }
}

function checkReadyState(obj) {
  if(obj.readyState == 4) {
    if(obj.status == 200) { return true; }
    else { alert("Problem retrieving XML data"); }
  }
}

function loaded() {	
  if(checkReadyState(xmlhttp)) {
    xmlDoc=xmlhttp.responseXML.documentElement;
    showTags('track','writeroot');

	if (rrreee!=1)
	{
	createPlayer(Urls[0], Imgs[0], false, true, Idds[0]);
	}	
	rrreee=1;
	showTitle('track','titleroot');

  }
}



if (window.XMLHttpRequest) { http_title=new XMLHttpRequest(); } 
else if (window.ActiveXObject) { http_title=new ActiveXObject("Microsoft.XMLHTTP"); }

function showTitle(theTag,thePlace) {

	var rand_no = Math.random();
  http.abort();
//document.getElementById(thePlace).innerHTML = "<div class='title_temp'><div align=center><img src='/swh/image/loading.gif' height='' width=''></div></div>";
if (theTag=="track"){
  http_title.open("GET", "video/swhtv_title.php?show=track&id="+active_clip+"&data=" + rand_no, true);
  element=thePlace;

} else if (theTag=="comment"){
  http_title.open("GET", "video/swhtv_playlist.php?show=pedejie&data=" + rand_no, true);
  element=thePlace;
} else if (theTag=="keywords"){
  http_title.open("GET", "video/swhtv_playlist.php?show=popular&data=" + rand_no, true);
  element=thePlace;
}

  http_title.onreadystatechange=function() {
    if(http_title.readyState == 4) { //4
	  document.getElementById(element).innerHTML = "";
      document.getElementById(element).innerHTML = http_title.responseText;
    }
  }
  http_title.send(null);

}

function parkraso (numb){
var krasa='zals';
  switch (numb) {
    case 1:
    krasa="zals";  
    break;
    case 2:
    krasa="sarkans";
    break;
    case 3:
    krasa="zils";
    break; 
    default:
    krasa="zals";
    break; 
  }
  try{
    document.getElementById('bt_cat1').src='video/bt_jaun_'+krasa+'.gif';
    document.getElementById('bt_cat2').src='video/bt_zel_'+krasa+'.gif';
    document.getElementById('bt_cat3').src='video/bt_akc_'+krasa+'.gif';
    } catch(e){
    }
}



var Urls = new Array();
var Imgs = new Array();
var Idds = new Array();


function showTags(theTag,thePlace) {

  function cat_color (numb){
    switch (numb) {
      case 1:
      var krasa="#628980";  
      break;
      case 2:
      var krasa="#983624";
      break;
      case 3:
      var krasa="#4D75A0";
      break; 
    }
  return krasa;
  }
  function cat_krasa (numb){
    switch (numb) {
      case 1:
      var krasa="zals";  
      break;
      case 2:
      var krasa="sarkans";
      break;
      case 3:
      var krasa="zils";
      break; 
    }
return krasa;
  }
  function getTag(tag) { 
    var tmp='';
    xx=x[i].getElementsByTagName(tag);     
    try { tmp=xx[0].firstChild.data; } 
    catch(er) { tmp=''; }    
    return(tmp); 
  }

  var xx; var x; var txt;
  x = xmlDoc.getElementsByTagName(theTag);
  txt='<table border="0" cellSpacing="0" cellPadding="0" width="100%">';
  

   
  txt+='<tr><td width="" align="left"><a href="javascript:tv_list(\'k_new\');"><img id="bt_cat1" src="video/bt_jaun_'+cat_krasa(category)+'.gif" onmouseover="parkraso(1)" onmouseout="parkraso(category)" border="0" /></a></td><td width="" align="center"><a href="javascript:tv_list(\'k_pop\');"><img id="bt_cat2" src="video/bt_zel_'+cat_krasa(category)+'.gif" onmouseover="parkraso(2)" onmouseout="parkraso(category)" border="0" /></a></td><td width="33%" align="center"><a href="javascript:tv_list(\'k_vip\');"><img id="bt_cat3" src="video/bt_akc_'+cat_krasa(category)+'.gif"  onmouseover="parkraso(3)" onmouseout="parkraso(category)" border="0" /></a></td></tr>';
  txt_pages='';
    
		txt+='</table><table  border="0" cellPadding="0" bgcolor="'+cat_color(category)+'" id="writeroottable"><tr>';  
  
  for (i=0; i<x.length; i++) { 
		Urls[i]=getTag("location"); Imgs[i]=getTag("image"); Idds[i]=getTag("idd"); //getTag("title"); getTag("creator"); getTag("info");

	if (getTag("creator")=="Span"){
		total_items=getTag("title");
		total_pages=getTag("location");
		total_cat=getTag("info");
		ppp=getTag("image");
		if (getTag("title")==0){
    		txt+='<table width="100%" height="60px"><tr><td align="center" valign="middle">Nekas netika atrasts!</td></tr></table>';
    }
	} else if (getTag("creator")=="Spand"){
		txt+='<tr><td colspan="3">neko neatrada!<hr><!--<table width="299"><tr><td>-->';
		txt+='<!--</td></tr></table>--></td></tr>';
	} else {
		txt+='<tr><td colspan="3"><table width="456" onclick="play('+i+','+getTag("idd")+')" class="playlistlo" onmouseover="this.className = \'playlisthi\'; getElementById(\'desrciption'+i+'\').className = \'laukshi\';" onmouseout="this.className = \'playlistlo\'; getElementById(\'desrciption'+i+'\').className = \'lauks\';"><tr>';
		txt+='<td><img src="'+getTag("image2")+'" width="74px" height="48px" border="1" alt="Skatīties..." onmouseover="getElementById(\'desrciption'+i+'\').className = \'laukshi\';" onmouseout="getElementById(\'desrciption'+i+'\').className = \'lauks\';"></td><td width="370px">';
		txt+='<div class="lauks" id="desrciption'+i+'" onmouseover="this.className = \'laukshi\';" onmouseout="this.className = \'lauks\';">&nbsp;<b>'+getTag("creator")+'</b><br>';
		txt+='&nbsp;'+getTag("title")+'';
		txt+='<div class="info">';
		if (getTag("info")!="") txt+='<a href="http://'+getTag("info")+'" target="_blank"><img src="video/info.gif" border="0"></a>';
		txt+='</div><!--<div class="lejupielade"><a href="javascript:popup3(\'/swh/video/download.php?id='+getTag("idd")+'\',\'Pop\',\'370\',\'200\')"><img src="video/lejupielade.gif" alt="Lejupielādēt..." border="0"></a></div>-->';
		txt+='<div class="pievienots">Pievienots '+getTag("mesdate")+'</div>';
		txt+='<div class="cipari">Skatīts '+getTag("act_stat")+'x</div>';
		txt+='</div></td></tr></table></td></tr>';		

	}
  }
  txt+="</table><table>";
	if (total_pages>1)
	{
		for (i=0;i<total_pages;i++)
		{
			if (ppp==i){
			txt_pages+='<strong>'+(i+1)+'</strong>, ';
			} else if (ppp==''){
			txt_pages+='<a href=\'javascript:tv_list("'+total_cat+'",'+i+')\'>'+(i+1)+'</a>, ';
			} else if ((i=='0')&&(ppp!=0)&&(ppp!=1)&&(ppp!=2)&&(ppp!=3)){
			txt_pages+='<a href=\'javascript:tv_list("'+total_cat+'",'+i+')\'>'+(i+1)+'</a>...';
			} else if (((i+1)==total_pages)&&(ppp!=i)&&(total_pages>7)){
			txt_pages+='...<a href=\'javascript:tv_list("'+total_cat+'",'+i+')\'>'+(i+1)+'</a>. ';
      } else if (ppp==(i-1)){
			txt_pages+='<a href=\'javascript:tv_list("'+total_cat+'",'+i+')\'>'+(i+1)+'</a>, ';
      } else if (ppp==(i+1)){
			txt_pages+='<a href=\'javascript:tv_list("'+total_cat+'",'+i+')\'>'+(i+1)+'</a>, ';
      } else if (ppp==(i-2)){
			txt_pages+='<a href=\'javascript:tv_list("'+total_cat+'",'+i+')\'>'+(i+1)+'</a>, ';
      } else if (ppp==(i+2)){
			txt_pages+='<a href=\'javascript:tv_list("'+total_cat+'",'+i+')\'>'+(i+1)+'</a>, ';
      } else if (ppp==(i-3)){
			txt_pages+='<a href=\'javascript:tv_list("'+total_cat+'",'+i+')\'>'+(i+1)+'</a>, ';
      } else if (ppp==(i+3)){
			txt_pages+='<a href=\'javascript:tv_list("'+total_cat+'",'+i+')\'>'+(i+1)+'</a>, ';
      } else if ((ppp==(i-4))&&(ppp<3)){
			txt_pages+='<a href=\'javascript:tv_list("'+total_cat+'",'+i+')\'>'+(i+1)+'</a>, ';   
      } else if ((ppp==(i-5))&&(ppp<2)){
			txt_pages+='<a href=\'javascript:tv_list("'+total_cat+'",'+i+')\'>'+(i+1)+'</a>, ';   
      } else if ((ppp==(i-6))&&(ppp<1)){
			txt_pages+='<a href=\'javascript:tv_list("'+total_cat+'",'+i+')\'>'+(i+1)+'</a>, ';   
      }
		}
		
  	bt_atpakal=ppp-1;
		bt_prieksha=(ppp*1)+(1*1);
		if (bt_atpakal>=0){
		  atp_links = '<a href=\'javascript:tv_list("'+total_cat+'",'+bt_atpakal+')\'>';
		} else { 
      atp_links = "";
    }
		if (bt_prieksha<(total_pages*1)){
      pri_links = '<a href=\'javascript:tv_list("'+total_cat+'",'+bt_prieksha+')\'>';
		} else {
      pri_links = "";
    }    
	  txt+='<tr><td colspan="3"><div><table><tr><td><span class="lapu_cipars">&nbsp;Lapas:&nbsp;</span></td><td>'+atp_links+'<img src="video/bt_previous.gif" border="0" /></a></td><td id="lapu_cipari">&nbsp;&nbsp;'+txt_pages+'</td><td>&nbsp;'+pri_links+'<img src="video/bt_next.gif" border="0" /></a></td></tr></table></div></td></tr></table>';
	  //txt+='<tr><td colspan="3"><div id="lapu_cipari">&nbsp;Lapas:&nbsp;'+txt_pages+'</div></td></tr></table>';
	}
  document.getElementById(thePlace).innerHTML=txt; 
}


var thePlayer;
var thePlayerRoot;
var theList;
var theListRoot;

function initialize() {
	thePlayer = document.getElementById("playerhandle");
	theList = document.getElementById("listhandle");
	thePlayerRoot  = document.getElementById("playerroot");
	theListRoot  = document.getElementById("listroot");


}

var currentState;

	function getUpdate(typ,pr1,pr2,pid) {
		if(pid != "null") {
			if((typ == "load")&&(pr1 != undefined)) { 
				currentState = pr1; 
				if(currentState=="100") { something(typ,currentState,pid); /*document.location.href="index.htm";*/ }
			}
		}
	};

function createPlayer(theFile, theImg, start, icons, theIdd) {
	active_clip=theIdd;
	var s1 = new SWFObject("video/mediaplayer_42.swf","player1","460","258","7"); // 292
	s1.addVariable("skin", "video/black.swf");
	s1.addParam("allowfullscreen", "true");
	s1.addVariable("file", theFile);
  s1.addVariable("controlbar", "over");	
	s1.addVariable("width", "460");
	s1.addVariable("height", "258"); // 292
	s1.addVariable("displaywidth", "460");
	s1.addVariable("displayheight", "258"); // 272
	s1.addVariable('bufferlength','3');
	if (start) s1.addVariable("autostart", "true");
	if (! icons) s1.addVariable("showicons", "false");
	if (theImg != "") s1.addVariable("image", theImg);
	s1.addVariable("enablejs","true");
	s1.addVariable("javascriptid",theFile); 
	s1.addParam("wmode","transparent");
	s1.addVariable("repeat", "list");
	s1.write("placeholder");
playerReady(s1);
}


function playthemovie(){

  // player.sendEvent('LOAD', Imgs[active_clip] );
//   player.sendEvent('LOAD', clip_link );
  //createPlayer(Urls[active_clip], Imgs[active_clip], true, true, Idds[active_clip]);
//  createPlayer(Urls[clip_link], Imgs[clip_link], true, true, Idds[clip_link]);
	player.sendEvent("NEXT","true");
}


function statusaListener(obj) { 
	currentS1 = obj.newstate; 
	currentS2 = obj.oldstate; 
	if (currentS1=="COMPLETED")
	{
//		alert
//setTimeout("playthemovie()", 10);
	}
}

function play(number,idd) { 

// on END next ==>>
		var lst = new Array();
	var rand_no = Math.random();
		lst[0] = {  file:'video/swh_hdv_intro_demo1.mp4',
					image:Imgs[number]
		}

		lst[2] = {  file:'video/swhtv_adxml.php?id='+Idds[number],
					image:Imgs[number]
		}

		lst[1] = {  file:Urls[number],
					image:Imgs[number] 
		}
      try{
  		player.sendEvent('LOAD', lst[2] );
  	player.sendEvent("ITEM","0");
      } catch (e){
     createPlayer('video/swhtv_adxml.php?id='+Idds[number], Imgs[number], false, true, Idds[number]);
      }

		//setTimeout("playthemovie()", 2000);
  pageTracker._trackPageview("video_click_on:"+Idds[number]+" - "+Urls[number]);
		active_clip=Idds[number];
		clip_link=Urls[number];
		variable_one=0;
	showTitle('track','titleroot');
//  alert (idd);
}

function getready() { 
document.getElementById('page_title_background').style.background='url(video/level2_bg2.gif)';
//
 if(checkReadyState(xmlhttp)) {

  //   

  //createPlayer(Urls[number], Imgs[number], true, true, Idds[number]);
  //clip_link=Urls[0];

 }
 setTimeout("play(0,0)", 1000);
  		button_cmd("k");
}

function getready_play(my_idd) { 

 if(checkReadyState(xmlhttp)) {
  createPlayer(Urls[number], Imgs[number], true, true, Idds[number]);
//setTimeout("createPlayer(Urls[number], Imgs[number], true, true)",4000);
 }
  		button_cmd("k");
//setTimeout("play(60,60)",500);
}
//====================================//

var xHttp
function something(fails,fails2,fails3)
{ 
	xHttp=GetXmlHttpObject()
		if (xHttp==null)
			 {
			 alert ("Browser does not support HTTP Request");
			 return;
			 }

	var url="video/something.php";
	xHttp.onreadystatechange=stateChanged 
	xHttp.open("POST",url,true);
xHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
xHttp.send('file='+fails+'&file2='+fails2+'&file3='+fails3);
	pageTracker._trackPageview("loaded:"+fails3);
	return true;

}

function stateChanged() 
{ 
if (xHttp.readyState==4 || xHttp.readyState=="complete")
 { 
 //document.getElementById("txtHint").innerHTML=xHttp.responseText 
 } 
}

function GetXmlHttpObject()
{
var xHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xHttp;
}