var lastClick=null;
var lastClickClass;
var lastJobInList=null;
var lastItemInList=null;
var ajaxLinks=new Array();
var sequence=1;
//======================================================================
function trim(str) 
{
	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}
//======================================================================
function ClickOnAjaxLink(styleName,elementId)
{
	var element=document.getElementById(elementId);
	if(element!=null)
	{
		if(lastClick!=null)
			lastClick.className=lastClickClass;
		lastClickClass=element.className;
		element.className=styleName;
		lastClick=element;
	}
}
//======================================================================
function AjaxSetIndex(index,url,target,evalCode,formToPost)
{
	var link=new Array();
	link['url']=url;
	link['target']=target;
	link['evalCode']=evalCode;
	link['formToPost']=formToPost;
	ajaxLinks[index]=link;
}

function AjaxCallIndex(index)
{
	var link=ajaxLinks[index];
	if(link==null)
		alert('Unbale to perform AJAX call. There is no AJAX call for index: '+index);
	else            
		AjaxCall(link['url'],link['target'],link['evalCode'],link['formToPost']);
}
//======================================================================
function AjaxCall(url,target,evalCode,formToPost)
{
	AjaxCallProcess(window,url,target,evalCode,formToPost);
}
//======================================================================
function CreateHTTPRequest()
{
	var http_request = false;
	if(window.XMLHttpRequest) // Mozilla, Safari, ...
	{ 
		http_request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) // IE
	{
		try
		{
			http_request = new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch(e)
		{
			try
			{
				http_request = new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch(e)
			{}
		}
	}
	return http_request;
}
//======================================================================
function AjaxCallProcess(window,url,target,evalCode,formToPost)
{
	var element;
	var document=window.document;
	if(url=='')
	{
		element=document.getElementById(target);
		if(element!=null)
			element.innerHTML='';
		return;
	}
	url=url.replace(/&amp;/g,'&');
	var http_request=CreateHTTPRequest();
	if (!http_request)
	{
		alert('Ajax is not supported!');
		return;
	}
	try
	{
		ShowContentLoading(true);
		var parameters=null;
		http_request.onreadystatechange = function() { RequestFinished(window,http_request,target,evalCode) ; };
		if(formToPost!=null)
		{
			parameters=FormToPost(formToPost);
			http_request.open('POST', url, true);
			http_request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			http_request.setRequestHeader('Content-length', parameters.length);
		}
		else
			http_request.open('GET', url, true);
		http_request.setRequestHeader('Connection', 'close');
		http_request.setRequestHeader('AjaxRequest', 'true');
		http_request.send(parameters);
	}	
	catch(e)
	{
		alert(e);
	}
}
//======================================================================
function FormToPost(formId)
{
	var form=document.getElementById(formId);
	if(form==null)
		throw 'Cannot submit form with ID='+formId+' since there is no such form.';

	var inputs=form.getElementsByTagName('input');
	var i;
	var post=''; 
	for(i=0;i<inputs.length;i++) 
	{
		if(inputs[i].type.toLowerCase()=='radio' || inputs[i].type.toLowerCase()=='checkbox')
		{
			if(inputs[i].checked)
				post+=inputs[i].name +'=' + encodeURIComponent(inputs[i].value)+'&';
		}
		else
		{
			if(inputs[i].type=='file')
				alert('Files cannot be uploaded this way!');
			else
				post+=inputs[i].name +'=' + encodeURIComponent(inputs[i].value)+'&';
		}
	}
	inputs=form.getElementsByTagName('textarea');
	for(i=0;i<inputs.length;i++) 
	{
		post+=inputs[i].name +'=' + encodeURIComponent(inputs[i].value)+'&';
	}

	inputs=form.getElementsByTagName('select');
	for(i=0;i<inputs.length;i++) 
	{
		if(inputs[i].selectedIndex>=0)
			post+=inputs[i].name +'=' + encodeURIComponent(inputs[i].options[inputs[i].selectedIndex].value)+'&';
	}
	return post; 
}
//======================================================================
function ShowContentLoading(visible)
{
	ShowContentLoadingExt(window,visible);
}
function ShowContentLoadingExt(window,visible)
{
	var document=window.document;
	element=document.getElementById('ajaxContentLoading');
	if(element!=null)
	{
		if(visible)
		{
			var posX=document.body.scrollLeft+document.documentElement.scrollLeft;
			var posY=document.body.scrollTop+ document.documentElement.scrollTop;
			var x=WindowWidth();
			x=(x/2+posX)-25;
			element.style.position='absolute';
			element.style.left=x+'px';					
			element.style.top=posY+'px';					
			element.style.visibility='visible';
			element.style.display='block';
		}
		else
		{
			element.style.visibility='hidden';
			element.style.display='none';
		}
	}
}
//======================================================================
function RequestFinished(window,request,target,evalCode)
{
	var element;
	var document=window.document;
	
	if (request.readyState==4)
	{
		if(request.status == 200 || request.status == 500)
		{
			element=document.getElementById(target);
			if(element!=null)
			{
				var resp='<div style="visibility: hidden; display: none">E</div>'+request.responseText; // Prevent MSIE error that removes script tags
				element.innerHTML=resp;
				var nodes=element.getElementsByTagName("script");
				for(mySpecialIndex=0;mySpecialIndex<nodes.length;mySpecialIndex++)
				{ 
					var cnt=nodes[mySpecialIndex].innerHTML;
					cnt=trim(cnt);
					if(cnt.substring(0,4)=='<!--')
						cnt=cnt.substring(4,cnt.length-3);
					eval(cnt);
				}
			}
			else
			   alert('Unable to find element with id:'+target);

			ShowContentLoading(false);
			if(evalCode!=null)
				eval(evalCode);
		}
		else
		{
			alert('Unable to process AJAX request. Request status='+request.status);
		}
	}
}					
//======================================================================
function GetEventElement(e)
{
	if(!e)
	  e=window.event;
	if(e.target)
	   return e.target;        // Mozilla
	else if (e.srcElement)
	  return e.srcElement;     // MSIE
	if (targ.nodeType == 3)    // Safari bug
		return targ.parentNode;
	return null;
}
//======================================================================
function WindowHeight()
{
	return WindowHeightExt(window)
}
//======================================================================
function WindowHeightExt(window)
{
	/* NN4 */
	if (window.innerHeight)
	  return window.innerHeight;

	/* MSIE6 std. */
	if(document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	
	/* older MSIE + MSIE6 in quirk */                
	if(document.body && document.body.clientHeight)
		return document.body.clientHeight;
	return 0;
}
//======================================================================
function WindowWidth()
{
	return WindowWidthExt(window);
}
function WindowWidthExt(window)
{
	/* NN4 */
	if (window.innerWidth)
	  return window.innerWidth;

	/* MSIE6 std. */
	if(document.documentElement && document.documentElement.clientWidth)
		return document.documentElement.clientWidth;
	
	/* older MSIE + MSIE6 in quirk */                
	if(document.body && document.body.clientWidth)
		return document.body.clientWidth;
	return 0;
}
//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
//======================================================================
function getPageSize()
{
	return getPageSizeExt(window);
}
function getPageSizeExt(window)
{
	var xScroll, yScroll;
	var document=window.document;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight)
		pageHeight = windowHeight;
	else 
		pageHeight = yScroll;

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth)	
		pageWidth = windowWidth;
	else
		pageWidth = xScroll;
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
//======================================================================
function AjaxPopupTop(event, width,height, url,target,evalCode,formToPost)
{
	var d=document;
	var w=window.parent;
	while(w && w.parent!=w)
	{
		d=w.document;
		w=w.parent;
	}
	AjaxPopupProcess(w,event,width,height,url,target,evalCode,formToPost)
}
function AjaxPopup(event, width,height, url,target,evalCode,formToPost)
{
	AjaxPopupProcess(window,event,width,height,url,target,evalCode,formToPost)
}
function AjaxPopupProcess(window,event, width,height, url,target,evalCode,formToPost)
{
	var document=window.document;
	var targetNode=document.getElementById(target);
	if(targetNode==null)
	{
		alert('Unable to show popup window because there is no element with name '+target);
		return;
	}
	if(!event)
	  event=window.event;
	var sourceNode=GetEventElement(event);
	   
	posx = document.body.scrollLeft+document.documentElement.scrollLeft;
	posy = document.body.scrollTop+ document.documentElement.scrollTop;

	if(width!=-1)
		targetNode.style.width=width+'px';
	if(height!=-1)
		targetNode.style.height=height+'px';
	
	var x=WindowWidthExt(window);
	var y=WindowHeightExt(window);
		
	if(width<0)
		x-=400;
	else
		x-=width;
	if(x<0)
		x=0;
	x=(x/2+posx);

	if(height<0)
		y-=200;
	else
		y-=height;
	if(y<0)
		y=0;
	y=(y/2+posy);

	targetNode.style.position='absolute';
	targetNode.style.left=x+'px';
	targetNode.style.top=y+'px';
	targetNode.style.display='block';
	targetNode.style.visibility='visible';
	targetNode.style.zIndex=1002;
	targetNode.style.overflow='auto';
	targetNode.style.border='solid 1px #c0c0c0';
	
	targetNode.innerHTML='<div class="ajaxContentLoadingClass">Moment</div>';
	var overlayNode=document.getElementById("AjaxBlackOverlayDiv");
	if(overlayNode)
	{
		overlayNode.style.display='block';
		overlayNode.style.visibility='visible';
	}
	AjaxCallProcess(window,url,target,evalCode,formToPost);
}
//======================================================================
function AjaxClosePopup(popupname)
{
	var targetNode=document.getElementById(popupname);
	if(targetNode==null)
	{
		alert('Unable to hide popup window because there is no element with name '+popupname);
		return;
	}
	targetNode.style.display='none';
	targetNode.style.visibility='hidden';
	var overlayNode=document.getElementById("AjaxBlackOverlayDiv");
	if(overlayNode)
		overlayNode.style.display='none';
}
//======================================================================
function ClearDiv(divname)
{
	var targetNode=document.getElementById(divname);
	if(targetNode==null)
		return;
	targetNode.style.display='none';
	targetNode.style.visibility='hidden';
	targetNode.innerHTML='';
}
//======================================================================
function EmptyDiv(divname)
{
	var targetNode=document.getElementById(divname);
	if(targetNode==null)
		return;
	targetNode.innerHTML='<!-- clear -->';
}
//======================================================================
function RemoveDiv(divname,reportMissingNode)
{
	var targetNode=document.getElementById(divname);
	if(targetNode==null)
	{
		if(reportMissingNode)
			alert('Unable to remove div	'+divname+', node not found.');
		return;
	}
	targetNode.parentNode.removeChild(targetNode);
}
//======================================================================
function SetDiv(divname,content)
{
	var targetNode=document.getElementById(divname);
	if(targetNode==null)
		return;
	targetNode.innerHTML=content;
}
//======================================================================
function SetMessageToDiv(divname,content)
{
	var targetNode=document.getElementById(divname);
	if(targetNode==null)
		return;
	targetNode.innerHTML='<div class="messageBox">'+content+'</div>';
}
//======================================================================
function AppendContent(sourceDiv, targetDiv)
{
	var targetNode=document.getElementById(targetDiv);
	if(targetNode==null)
	{
		alert('Unable to append node content. There is no div with id: '+targetDiv);
		return;
	}
	var sourceNode=document.getElementById(sourceDiv);
	if(sourceNode==null)
	{
		alert('Unable to append node content. There is no div with id: '+sourceDiv);
		return;
	}
	targetNode.innerHTML=targetNode.innerHTML+'<div id="'+sourceDiv+'">'+sourceNode.innerHTML+'</div>';
}
//======================================================================
function MoveContent(sourceDiv, targetDiv)
{
	var targetNode=document.getElementById(targetDiv);
	if(targetNode==null)
	{
		alert('Unable to move node content. There is no div with id: '+targetDiv);
		return;
	}
	var sourceNode=document.getElementById(sourceDiv);
	if(sourceNode==null)
	{
		alert('Unable to move node content. There is no div with id: '+sourceDiv);
		return;
	}
	targetNode.innerHTML=sourceNode.innerHTML;
}
//======================================================================
function AppendNodeCopy(sourceDiv, languagesDiv)
{
	var targetNode=document.getElementById(languagesDiv);
	if(targetNode==null)
	{
		alert('Unable to add language. There is no div with id: '+languagesDiv);
		return;
	}
	var sourceNode=document.getElementById(sourceDiv);
	if(sourceNode==null)
	{
		alert('Unable to add language. There is no div with id: '+sourceDiv);
		return;
	}
	var node=sourceNode.cloneNode(true);
	targetNode.appendChild(node);
}
//======================================================================
function LoadPage(url)
{
	url=url.replace('&amp;','&');
	window.location=url;
}
//======================================================================
function ReloadParentPage(url)
{
	url=url.replace('&amp;','&');
	window.parent.location=url;
}
//======================================================================
function RecodeAmp(url)
{
	return url.replace('&amp;','&');
}
//======================================================================
function SetLastItemInList(value)
{
	//alert('SetLastItemInList old='+lastJobInList+', new='+value);
	lastJobInList=value;
}
//======================================================================
function JobInListClicked(divName)
{
	var element;
	if(lastJobInList)
	{
		element=document.getElementById(lastJobInList);
		if(element)
			element.className='';
		SetLastItemInList(null);
	}
	SetLastItemInList(divName);
	element=document.getElementById(lastJobInList);
	if(element)
		element.className="activeAd";
}
//======================================================================
function ItemInListClicked(divName,className)
{
	var element;
	if(lastJobInList)
	{
		element=document.getElementById(lastJobInList);
		if(element)
			element.className='';
		SetLastItemInList(null);
	}
	SetLastItemInList(divName);
	element=document.getElementById(lastJobInList);
	if(element)
		element.className=className;
}
//======================================================================
function ShowCityInput()
{
	var elemInput=document.getElementById('query_city_input');
	var elemText=document.getElementById('query_city_div');
	if(!elemInput || !elemText)
		return;
	if(elemInput.style.display=='none')
	{
		elemInput.style.display='inline';
		elemText.style.display='none';
	}
	else
	{
		elemInput.style.display='none';
		elemText.style.display='inline';
		elemText.innerHTML=elemInput.value;
	}
}
//======================================================================
function SwitchVisibility(toHide,toShow,displayType)
{
	var el1=document.getElementById(toHide);
	var el2=document.getElementById(toShow);
	if(el1!=null)
	{
		el1.style.visibility='hidden';
		el1.style.display='none';
	}
	if(el2!=null)
	{
		el2.style.visibility='visible';
		el2.style.display=displayType ? displayType : 'block';
	}
}
//======================================================================
function ToggleVisibility(toShow,displayType)
{
	var el1=document.getElementById(toShow);
	if(el1!=null)
	{
		if(el1.style.visibility=='hidden' || el1.style.display=='none')
		{
			el1.style.visibility='visible';
			el1.style.display=displayType ? displayType : 'block';
		}
		else
		{
			el1.style.visibility='hidden';
			el1.style.display='none';
		}
	}
}
//======================================================================
function CheckSearch(inputId,meesageDiv)
{
	var input=document.getElementById(inputId);
	if(!input)
		return false;
	var val=trim(input.value);
	if(!val)
	{
		var msg=document.getElementById(meesageDiv);
		if(!msg)
			return false;
		msg.style.visibility='visible';
		msg.style.display='block';
		return false;
	}
	return true;
}
//======================================================================
function GetPosition(element)
{
	var 	x=0,y=0;
	while(element)
	{
		x+=element.offsetLeft;
		y+=element.offsetTop;
		element=element.offsetParent;
	}
	return { x: x, y: y };
}
//======================================================================
function ViewMenuBellowElement(viewDiv,dstElem)
{
	var dst=document.getElementById(dstElem);
	var src=document.getElementById(viewDiv);
	if(!dst || !src)
		return;
	var pos=GetPosition(dst);
	src.style.left=pos.x+'px';
	src.style.top=pos.y+dst.offsetHeight+'px';
	src.style.zIndex=100;
	src.style.position='absolute';
	src.style.visibility='visible';
	src.style.display='block';
	
	document.onmousedown=function(event)
		{
			var ee=GetEventElement(event);
			while(ee)
			{
				if(ee==src)
					return;
				ee=ee.parentNode;
			}
			src.style.visibility='hidden';
			src.style.display='none';
			document.onmousedown=null;
		}
}
//======================================================================
function TrackProgress(trackId,targetDiv,size)
{
	var element=document.getElementById(targetDiv);
	if(!element)
		return;
	var http_request=CreateHTTPRequest();
	if (!http_request)
	{
		alert('Ajax is not supported!');
		return;
	}
	setTimeout(function() { TrackProgressEvent(trackId,http_request,element,size); } ,1000);
}
//======================================================================
function TrackProgressEvent(trackId,http_request,element,size)
{
	try
	{
		http_request.onreadystatechange = function()
		{
			if(http_request.readyState!=4 || http_request.status!=200)
				return;
			var status=http_request.getResponseHeader('Navajo-Progress-Status');
			if(!status)
				return;
			var arr=status.split(',');
			if(arr.length!=4)
				return;
			element.style.width=size/arr[0]*arr[1]+"px";
			setTimeout(function() { TrackProgressEvent(trackId,http_request,element,size); } ,1500);
		};
		http_request.open('HEAD', '/js/empty.js', true);
		http_request.setRequestHeader('Connection', 'close');
		http_request.setRequestHeader('AjaxRequest', 'true');
		http_request.setRequestHeader('Navajo-Progress-Status', trackId);
		http_request.send(null);
	}	
	catch(e)
	{
		alert('Error:'+e);
	}
}
//======================================================================
function CopyBodyIntoParentNode(nodeName)
{
	var nodeTarget=window.parent.document.getElementById(nodeName);
	var nodeSource=window.document.body;
	if(nodeTarget==null || nodeSource==null)
	{
		alert('Unable to copy document.body into the parent document '+nodeName+' node.');
		return;
	}
	nodeTarget.innerHTML=nodeSource.innerHTML;
}
//======================================================================
function HelpTip(helpid)
{
	var dt=new Date();
	var id=dt.getMinutes()+''+dt.getSeconds()+''+dt.getMilliseconds()+'_'+sequence;
	document.write('<img id="'+id+'" src="/gfx/help.png" alt="Ikonka nápovědy" title="Klikněte zde pro nápovědu." onclick="ShowHelpWindow(\''+id+'\',\''+helpid+'\')" style="cursor: pointer;" />');
	document.write('<div id="'+id+'div" class="HelpWindow" style="visibility: hidden; display: none; font-size: 100%" ></div>');
}
//======================================================================
function ShowHelpWindow(elemid,helpid)
{
	var cntElem=elemid+'div';
	var elem=document.getElementById(cntElem);
	if(!elem)
	{
		alert('Element not found: '+cntElem);
		return;
	}
	elem.innerHTML='<br/><p style="text-align: center" ><b>Moment, nahrávám.<b/></p>';
	ViewMenuBellowElement(cntElem,elemid);
	var url='/cz/ajax_get_help/'+encodeURIComponent(helpid)+'?ajaxTarget='+encodeURIComponent(cntElem);
	AjaxCall(url,cntElem);
}
//======================================================================
function AddFaceBookWidget()
{
	FB.init("31144c400cfbb975fd2c072ccabd4158");
	document.write('<fb:fan profile_id="28225100769" stream="" connections="10" width="300"></fb:fan>');
}
//======================================================================
