var animation_status=true;
var getnext_check=0;
var current_query_string="front_pool_json.php?q_where=random&item_title=Homepage4";
var current_signin=false;
var current_splash_teller=false;

var body_hotspotId=null;
var body_hotspotW=null;
var body_hotspotH=null;
var body_hotspotX=null;
var body_hotspotY=null;

var pool_item_array=new Array();
var pool_item_history=new Array();
var pool_item_list=new Array();

var current_title_teller;

var current_tempTitle;
var pool_grid_num=10;

var uk_type="list";
var uk_ext="uk_";

//-----------------------------------

function uk_switch_type()
{
	temp_type=document.getElementById("uk_type_hidden").value;
	if(temp_type=="list")
	{
		uk_type="list";
		pool_grid_num=10;
		uk_ext="uk_";
		document.getElementById("uk_grid_table").style.display='none';
		document.getElementById("uk_list_table").style.display='block';
		document.getElementById("f_pool").innerHTML="";
		document.getElementById("uk_type_hidden").value="grid";
	}
	
	if(temp_type=="grid")
	{
		uk_type="grid";
		pool_grid_num=36;
		uk_ext="";
		document.getElementById("uk_grid_table").style.display='block';
		document.getElementById("uk_list_table").style.display='none';
		document.getElementById("uk_f_pool").innerHTML="";
		document.getElementById("uk_type_hidden").value="list";
	}
		pool_grid();
		body_define_hotspot();

		var temp_item_list_string="";
		for(i=0;i<pool_item_list.length;i++)
		{
			if(pool_item_list[i])
				temp_item_list_string+=i+",";
		}
		temp_item_list_string+='0';

		new net.ContentLoader(uk_ext+"f_pool", current_query_string+"&item_list="+temp_item_list_string, parsePoolJson);
		uk_list_refresh();
		fader_position();
}


function browseFront(tempId, tempTitle, tempFrom){
	var temp_query_string="";
	
	pool_grid(tempTitle);
		
	if(tempId=="")
		temp_query_string="front_pool_json.php?item_title="+tempTitle;
	else if(tempId=="latest" || tempId=="featured" || tempId=="random")
		temp_query_string="front_pool_json.php?q_where="+tempId+"&item_title="+tempTitle;
	else
		temp_query_string="front_pool_json.php?item_id="+tempId+"&item_title="+tempTitle;

	current_query_string=temp_query_string;
	new net.ContentLoader(uk_ext+"f_pool", temp_query_string, parsePoolJson);
	document.getElementById("txtUserInput").value="";
	fader_position();
}
//-----------------------------------

function pool_grid(tempTitle)
{
	if(pool_item_history.length!=0 && tempTitle!=current_tempTitle && tempTitle!=null)
	{
		current_tempTitle=tempTitle;
/*		eval("var regExpString=/<a[^>]*>"+tempTitle+"<\\/a> <br>/gi");
		document.getElementById("f_panel").innerHTML=document.getElementById("f_panel").innerHTML.replace(regExpString,"");
		document.getElementById("f_panel").innerHTML="<a href='javascript:void(0)' onclick=\"show_history("+(pool_item_history.length+1)*1+",'"+tempTitle.replace("'","`")+"')\">"+tempTitle+"</a> <br>"+document.getElementById("f_panel").innerHTML;*/
	}
	pool_item_history[pool_item_history.length]=new Array();
	var temp_item_array=pool_item_array.length;
	for(var i=0; i<temp_item_array;i++)
	{
		if(pool_item_array[i])
			pool_item_array[i].move_to_history();
	}

	var resultContainer=uk_ext+"f_pool";
		
	document.getElementById(resultContainer).innerHTML="";
	var gridColor=new Array('#B3ABAC', '#D5E2F2', '#D5E2F2', '#D58999', '#F3AFB1', '#ACAED2', '#A3ACD3', '#9EC6F5', '#A3ACD3',  '#D5E2F2');
	for(var i=0; i<pool_grid_num; i++)
	{
		var tv_div=document.createElement("div");

		tv_div.id=uk_ext+"f_pool_grid_"+i;
		tv_div.className=uk_ext+"f_pool_grid";
		var tempRandom=Math.round(Math.random()*10);
		tv_div.style.backgroundColor=gridColor[tempRandom];
		tv_div.onclick=buy_this;
		tv_div.innerHTML="<center><br><br><br><span onclick=\"manager_position('Advertisements Manager - Add advertisement', 'external', 'client_manager.php?link=8');\" class='buy_this_place' >use this<br>space</span></center>";
		document.getElementById(resultContainer).appendChild(tv_div);
	}
}
//-----------------------------------
function buy_this(evt)
{
		var e = evt || window.event;
		if(e.target) var target = e.target;
		else if(e.srcElement)var target = e.srcElement;
		if(target.id.indexOf(uk_ext+"f_pool_grid_")==0)
		{
			manager_position('', 'external', 'client_manager.php');
		}
}

function list_refresh()
{
	var temp_list_array=new Array();
	var temp_list=xGetElementById(uk_ext+"f_list");
	if(temp_list.childNodes.length>1)
	{
		for(var i=temp_list.childNodes.length-2; i>=0;i--)
		{
			temp_list_array[i]=temp_list.childNodes[i];
			document.body.appendChild(temp_list.childNodes[i]);
		}
		for(var i=0; i<temp_list_array.length;i++)
		{
			temp_list.appendChild(temp_list_array[i]);
		}
	}
}

function uk_list_refresh()
{
	if(uk_type=="grid")
	{
		var temp_list=xGetElementById("uk_f_list");
		xResizeTo("f_list",xWidth("f_list")+95, null);
	}
	else
		var temp_list=xGetElementById("f_list");


	if(temp_list.childNodes.length>=1)
	{
		for(var i=temp_list.childNodes.length-1; i>=0;i--)
		{
			temp_id_array=temp_list.childNodes[i].id.split("_");
			if(uk_type=="grid")
				xResizeTo("f_list",xWidth("f_list")+95, null);
				
			pool_item_list[temp_id_array[temp_id_array.length-1]].show_in_list();
		}
	}
	temp_list.innerHTML="";
}


function pool_reposition()
{
	arenas_reposition();
	for(var i=0; i<pool_item_array.length;i++)
	{
		if(pool_item_array[i])
			pool_item_array[i].reposition();
	}
}

//-----------------------------------
function show_history(tempId,tempTitle)
{
	pool_grid(tempTitle);
	var temp_counter=0;
	for(i=0;i<pool_item_history[tempId].length;i++)
	{
		if(pool_item_history[tempId][i])
		{
			if(!pool_item_list[i])
			{
				pool_item_array[i]=pool_item_history[tempId][i];
				pool_item_array[i].generate_item(temp_counter);
				temp_counter++;
			}
		}
	}
	document.getElementById("f_title_teller").innerHTML="";
}

function refresher_redirector(eleId,parentId)
{
	if(pool_item_array[eleId])
	{
		if(pool_item_array[eleId].tell_parentId()==parentId)
			pool_item_array[eleId].refresher();
	}
}

function refresher_redirector_pause(eleId, item_list)
{
	new net.ContentLoader(eleId, current_query_string+"&max_limit=1&item_list="+item_list, parseItemJson);
}
//-----------------------------------

function parsePoolJson(){
  
  var jsonTxt=unescape(this.req.responseText);

  current_title_teller="";

  if(jsonTxt!="")
  {
	  var jsonObj=eval("("+jsonTxt+")");
	  var l_items=jsonObj.l_items;
	
		for(j in l_items){  
			var l_title=l_items[j].l_title;

			var l_num_rows=l_items[j].l_num_rows;
			var l_max_limit=l_items[j].l_max_limit;
			var l_current_from=l_items[j].l_current_from;
			var q_where=l_items[j].q_where;
			var q_search=l_items[j].q_search;
			var q_category=l_items[j].item_id;
			
			if(q_where!="random")
				current_title_teller="FOUND <span style='color:#FF6600'>"+l_num_rows+"</span> ADS";
				
			if(q_where=="" && q_search=="" && q_category=="")
				current_title_teller="";
			
			var tv_items=l_items[j].tv_items;
			var temp_counter=0;
			for(i in tv_items){
				var hp4_id=tv_items[i].hp4_id;
				var hp4_img=tv_items[i].hp4_img;
				var hp4_title=tv_items[i].hp4_title;
				var hp4_price=tv_items[i].hp4_price;
				var hp4_type=tv_items[i].hp4_type;
				var hp4_client=tv_items[i].hp4_client;
				var hp4_rating=tv_items[i].hp4_rating;
				 
				if(!pool_item_list[hp4_id])
				{
					pool_item_array[hp4_id]= new pool_item_object(hp4_id, temp_counter, q_where, q_search, q_category, hp4_img, hp4_title, null, hp4_price, hp4_client, hp4_rating, hp4_type);
					temp_counter++;
				}
				
				if(uk_type=="list")
				{
					if(i>pool_grid_num-3)
						break;
				}
			}
		}
  	} else {
		current_title_teller="FOUND <span style='color:#FF6600'>0</span> ADS";
	}
	document.getElementById("f_title_teller").innerHTML=current_title_teller;
	pool_reposition();
	fader_hide("browsing");
	getnext_check=0;
//	HideTheBox();
}

//-----------------------------------
function parseItemJson()
{
  var jsonTxt=unescape(this.req.responseText);
  //alert(jsonTxt);  return;
  if(jsonTxt!="")
  {
	  var jsonObj=eval("("+jsonTxt+")");
	  var l_items=jsonObj.l_items;
	
		for(j in l_items)
		{  
			var tv_items=l_items[j].tv_items;
			
			for(i in tv_items)
			{
				var hp4_id=tv_items[i].hp4_id;
				var hp4_img=tv_items[i].hp4_img;
				var hp4_title=tv_items[i].hp4_title;
				var hp4_price=tv_items[i].hp4_price;
				var hp4_type=tv_items[i].hp4_type;
				var hp4_client=tv_items[i].hp4_client;
				var hp4_rating=tv_items[i].hp4_rating;
				
				if(!pool_item_list[hp4_id] && !pool_item_array[hp4_id] )
				{
					if(pool_item_array[this.parentId])
					{
						pool_item_array[this.parentId].load_new_item(hp4_id, hp4_img, hp4_title, hp4_price, hp4_client, hp4_rating, hp4_type);
						getnext_check--;		
					}
				}
			}
		}
  	}
}

//-----------------------------------
function searchFront(){
		xStrText=document.getElementById("txtUserInput").value.replace(/`/g," ");
		xStrText=xStrText.replace(/'/g," ");
		xStrText=xStrText.replace(/"/g," ");
		xStrTextLocation=document.getElementById("txtUserInputLocation").value.replace(/`/g," ");
		xStrTextLocation=xStrTextLocation.replace(/'/g," ");
		xStrTextLocation=xStrTextLocation.replace(/"/g," ");
		xStrTextType=document.getElementById("txtUserInputType").options[document.getElementById("txtUserInputType").selectedIndex].value;

		current_query_string="front_pool_json.php?q_search="+escape(xStrText)+"&q_search_type="+escape(xStrTextType)+"&q_search_location="+escape(xStrTextLocation);
		pool_grid("Search:"+document.getElementById("txtUserInput").value);

		var temp_item_list_string="";
		for(i=0;i<pool_item_list.length;i++)
		{
			if(pool_item_list[i])
				temp_item_list_string+=i+",";
		}
		temp_item_list_string+='0';

		new net.ContentLoader(uk_ext+"f_pool", current_query_string+"&item_list="+temp_item_list_string, parsePoolJson);
		fader_position();
		
		document.getElementById("f_splash_search").value=document.getElementById("txtUserInput").value;
		document.getElementById("f_splash_location").value=document.getElementById("txtUserInputLocation").value;
		document.getElementById("f_splash_type").selectedIndex=document.getElementById("txtUserInputType").selectedIndex;
}

function searchFrontClick(){
		splash_hide();
		xStrText=document.getElementById("f_splash_search").value.replace(/`/g," ");
		xStrText=xStrText.replace(/'/g," ");
		xStrText=xStrText.replace(/"/g," ");
		xStrTextLocation=document.getElementById("f_splash_location").value.replace(/`/g," ");
		xStrTextLocation=xStrTextLocation.replace(/'/g," ");
		xStrTextLocation=xStrTextLocation.replace(/"/g," ");
		xStrTextType=document.getElementById("f_splash_type").options[document.getElementById("f_splash_type").selectedIndex].value;

		pool_grid("Search:"+document.getElementById("f_splash_search").value);
		
		current_query_string="front_pool_json.php?q_search="+escape(xStrText)+"&q_search_type="+escape(xStrTextType)+"&q_search_location="+escape(xStrTextLocation);

		var temp_item_list_string="";
		for(i=0;i<pool_item_list.length;i++)
		{
			if(pool_item_list[i])
				temp_item_list_string+=i+",";
		}
		temp_item_list_string+='0';

		new net.ContentLoader(uk_ext+"f_pool", current_query_string+"&item_list="+temp_item_list_string, parsePoolJson);
		fader_position();

		document.getElementById("txtUserInput").value=document.getElementById("f_splash_search").value;
		document.getElementById("txtUserInputLocation").value=document.getElementById("f_splash_location").value;
		document.getElementById("txtUserInputType").selectedIndex=document.getElementById("f_splash_type").selectedIndex;
}

function searchFrontEnterPool(e){
	if(window.event){
	  intKey = event.keyCode;
	}
	else{
	  intKey = e.which;
	}
	
	if(intKey == 13)
	{
		searchFront();
		document.getElementById("f_splash_search").value=document.getElementById("txtUserInput").value;
		document.getElementById("f_splash_location").value=document.getElementById("txtUserInputLocation").value;
		document.getElementById("f_splash_type").selectedIndex=document.getElementById("txtUserInputType").selectedIndex;
	}
}

function searchFrontEnter(e){
	if(window.event){
	  intKey = event.keyCode;
	}
	else{
	  intKey = e.which;
	}
	
	if(intKey == 13)
	{
		searchFrontClick();
		document.getElementById("txtUserInput").value=document.getElementById("f_splash_search").value;
		document.getElementById("txtUserInputLocation").value=document.getElementById("f_splash_location").value;
		document.getElementById("txtUserInputType").selectedIndex=document.getElementById("f_splash_type").selectedIndex;
	}
}

function searchAdvEnter(e){
	if(window.event){
	  intKey = event.keyCode;
	}
	else{
	  intKey = e.which;
	}
	
	if(intKey == 13)
		f_adv_search_sender('adv_search_form','block_search_confirm.php');
}

function advsearchFront(params, keyword){
		pool_grid("Search:"+keyword);
		new net.ContentLoader(uk_ext+"f_pool", "front_advsearch_json.php?"+params+"", parsePoolJson);
		current_query_string="front_advsearch_json.php?"+params+"";
		responder_hide();
		fader_position();
}

//--------------------------
function body_define_hotspot()
	{	
		if(uk_type=="grid")
			body_hotspotId="f_list_outer";
		else
			body_hotspotId=uk_ext+"f_list";
		var hotspot_pointer=document.getElementById(body_hotspotId);
		body_hotspotW=xWidth(hotspot_pointer);
		body_hotspotH=xHeight(hotspot_pointer);
		body_hotspotX=xPageX(hotspot_pointer);
		body_hotspotY=xPageY(hotspot_pointer);
	}
//--------------------------
function body_is_hotspot(tempX, tempY)
	{
		var is_hotspot=false;
		if(tempX>body_hotspotX && tempX<(body_hotspotX+body_hotspotW))
			if(tempY>body_hotspotY && tempY<(body_hotspotY+body_hotspotH))
				{
					document.getElementById(body_hotspotId).style.backgroundColor="#FFD0B0";
					is_hotspot=true;
				}
		if(is_hotspot==false)
		{
			document.getElementById(body_hotspotId).style.backgroundColor="#EEEEEE";
		}

		return is_hotspot;

	}

//-------------------------------------


curleft=0;
speedo=5;
flag=1;
flag2=1;
function lefter_over(tspeed)
{
	flag2=0;
	speedo=tspeed;
	setTimeout('lefter('+speedo+')',30);
}
function lefter(tspeed)
{
	if(flag2==0 && ((xWidth("f_list")+xPageX("f_list")*1)>(xPageX("f_list_outer")+600*1)))
	{	
		curleft=curleft-tspeed;
		xMoveTo(document.getElementById("f_list"),curleft,null);
		setTimeout('lefter('+tspeed+')',30);
	}
}
function lefter_out()
{
	flag2=1;
}

function righter_over(tspeed)
{
	flag=0;
	speedo=tspeed;
	setTimeout('righter('+speedo+')',30);
}
function righter(tspeed)
{
	if(flag==0 && xPageX("f_list")<xPageX("f_list_outer"))
	{	
		curleft=curleft+tspeed;
		xMoveTo(document.getElementById("f_list"),curleft,null);
		setTimeout('righter('+tspeed+')',30);
	}
}
function righter_out()
{
	flag=1;
}

//---------------------
function change_top_account(loginType)
{
	if(loginType=="signin")
	{
		document.getElementById("f_button_search").style.display="none";
		document.getElementById("f_button_search2").style.display="block";
		document.getElementById("top_signin").style.display="none";
		document.getElementById("top_signout").style.display="block";
		current_signin=true; 
	}

	if(loginType=="signout")
	{
		document.getElementById("f_button_search").style.display="block";
		document.getElementById("f_button_search2").style.display="none";
		document.getElementById("top_signin").style.display="block";
		document.getElementById("top_signout").style.display="none";
		current_signin=false;
	}
}

//---------------------
function play_pause()
{
	if(animation_status==true)
	{
		document.getElementById("img_play_pause").style.background="url(http://www.homepage4.co.uk/design/front/b_pause.gif)";
		animation_status=false;
	} else
	if(animation_status==false)
	{
		document.getElementById("img_play_pause").style.background="url(http://www.homepage4.co.uk/design/front/b_play.gif)";
		animation_status=true;
	}
}

function pool_refresh()
{
	pool_grid();
	
	var temp_item_list_string="";
	for(i=0;i<pool_item_list.length;i++)
	{
		if(pool_item_list[i])
			temp_item_list_string+=i+",";
	}
	temp_item_list_string+='0';

	new net.ContentLoader(uk_ext+"f_pool", current_query_string+"&item_list="+temp_item_list_string, parsePoolJson);
	fader_position();
}

//----------------------
function show_caption_favourites(evt)
{
	var e = evt || window.event;
	if(e.target) var target = e.target;
	else if(e.srcElement)var target = e.srcElement;
	temp_caption=document.getElementById("d_caption");
	if(target.id.indexOf(uk_ext+"f_pool_item_add")>-1)
	{
		var pageX, pageY;
		if(xOp6Dn) { pageX = e.clientX; pageY = e.clientY; }
		else if(xDef(e.pageX,e.pageY)) { pageX = e.pageX; pageY = e.pageY; }
		else if(xDef(e.clientX,e.clientY)) { pageX = e.clientX + xScrollLeft(); pageY = e.clientY + xScrollTop(); }
		xMoveTo(temp_caption, pageX+5, pageY+5);
		temp_caption.innerHTML="Click to add to favourites";
		temp_caption.style.display="block";
	}
}

function show_caption_remove(evt)
{
	var e = evt || window.event;
	if(e.target) var target = e.target;
	else if(e.srcElement)var target = e.srcElement;
	temp_caption=document.getElementById("d_caption");
	if(target.id.indexOf(uk_ext+"f_pool_item_remove_")>-1)
	{
		var pageX, pageY;
		if(xOp6Dn) { pageX = e.clientX; pageY = e.clientY; }
		else if(xDef(e.pageX,e.pageY)) { pageX = e.pageX; pageY = e.pageY; }
		else if(xDef(e.clientX,e.clientY)) { pageX = e.clientX + xScrollLeft(); pageY = e.clientY + xScrollTop(); }
		xMoveTo(temp_caption, pageX+5, pageY+5);
		temp_caption.innerHTML="Click to remove from favourites";
		temp_caption.style.display="block";
	}
}