function rollover(imgID) {
  // get the image object we're referring to
  var thisimg = document.getElementById(imgID);
  // and add "_on" to its src
  thisimg.src = thisimg.src.replace(/(\.[a-z0-9]+)$/i,'_on$1');
}
function rollout(imgID) {
  // get the image object we're referring to
  var thisimg = document.getElementById(imgID);
  // and remove "_on" from its src
  thisimg.src = thisimg.src.replace(/_on(\.[a-z0-9]+)$/i,'$1');
}

function submitform()
{
	document.searchform.submit();
}
function addKeyWords(id)
{
	document.getElementById('search_text').value+=' '+id;
	submitform();
}



this.imagePreview = function(){

	/* CONFIG */
		xOffset = 20;
		yOffset = 10;

		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
	/* END CONFIG */

	$("a.thickbox").hover(function(e)
	{
		this.removed=false;
		var img=this;

		var objImagePreloader = new Image();
		objImagePreloader.onload = function()
		{
			objImagePreloader.onload=function(){};

			img.initialWidth=objImagePreloader.width+12;
			img.initialHeight=objImagePreloader.height+12;

			if(!img.removed)
			{
				

				img.previewElement=jQuery('<p id="thickbox"><img src="'+ objImagePreloader.src +'" alt="Image preview" /></p>').appendTo("body");
				updatePreviewPosition(img,e);
				img.previewElement.show(300,
					function()
					{
						if(img.removed)
						  img.previewElement.hide().remove();
					}
				);
			}

			if(img.removed && img.previewElement)
				img.previewElement.hide().remove();

		};
		objImagePreloader.src = 'get_image.php?id='+$(this).find("img")[0].id+'&size=2';
    },
	function()
	{
		this.removed=true;
		if(this.previewElement)
			this.previewElement.hide().remove();
    });

	$("a.thickbox").mousemove(function(e)
	{
		if(this.initialWidth && this.initialHeight)
			updatePreviewPosition(this,e);
	});

};

updatePreviewPosition = function(source,e)
{
	if(source && source.previewElement)
	{
		var pLeft=$(source).parents()[7].offsetLeft;
		var pTop=$(source).parents()[7].offsetTop;

		//console.log('parent: '+$(source).parents()[7].offsetLeft);
		//for(i=0; i<$(source).parents().length; i++)
		//	console.log('parent['+i+']: '+$(source).parents()[i].offsetLeft+':'+$(source).parents()[i].offsetTop+'\t | '+$($(source).parents()[i]).get(0).tagName);

		if(pLeft>250 && pTop<200)
		{
			source.previewElement
				.css("top",(e.pageY - yOffset) + "px")
				.css("left",( e.pageX - xOffset - source.initialWidth) + "px");
		}
		else if(pTop>200 && pLeft<250)
		{
			source.previewElement
				.css("top",(e.pageY + yOffset-source.initialHeight) + "px")
				.css("left",(e.pageX + xOffset) + "px");
		}
		else if(pTop>200 && pLeft>250)
		{
			source.previewElement
				.css("top",(e.pageY + yOffset - source.initialHeight) + "px")
				.css("left",( e.pageX - xOffset - source.initialWidth) + "px");
		}
		else
		{
			source.previewElement
				.css("top",(e.pageY - yOffset) + "px")
				.css("left",(e.pageX + xOffset) + "px");
		}
	}
}

var number=0;
var xmlHttp

function requestPayment(user_id,amount)
{
	 xmlHttp=GetXmlHttpObject()
	  if (xmlHttp==null)
	  {
		alert ("Browser does not support HTTP Request")
	   	return
	  }
	  
		Boxy.ask('Сигурни ли сте, че искате да направите заявка за плащане?', ["Да", "Изход"],
			function(val) {
				if(val=='Да')
				{	
					var url="requestPayment.php"
					 url=url+"?user_id="+user_id;
					 url=url+"&amount="+amount;
					 url=url+"&sid="+Math.random()
					 xmlHttp.onreadystatechange=stateChangedP
					 xmlHttp.open("GET",url,true)
					 xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8;");
					 xmlHttp.send(null);
				}	 
			}, {title: ""
		});	  
}

function buyImage(id,data)
{
	 xmlHttp=GetXmlHttpObject()
	  if (xmlHttp==null)
	  {
		alert ("Browser does not support HTTP Request")
	   	return
	  }
	number = id;

	var url="buy_image.php"
	 url=url+"?id="+id+"&data="+data+"&sid="+Math.random()
	 xmlHttp.onreadystatechange=stateChangedB
	 xmlHttp.open("GET",url,true)
	 xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8;");
	 xmlHttp.send(null);
}

function newImages()
{
	 xmlHttp=GetXmlHttpObject()
	  if (xmlHttp==null)
	  {
		alert ("Browser does not support HTTP Request")
	   	return
	  }

	var url="newImages.php"
	 url=url+"?sid="+Math.random()
	 xmlHttp.onreadystatechange=stateChangedI
	 xmlHttp.open("GET",url,true)
	 xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8;");
	 xmlHttp.send(null);
}
function saveImage(num)
 {
	 xmlHttp=GetXmlHttpObject()
	  if (xmlHttp==null)
	  {
		alert ("Browser does not support HTTP Request")
	   	return
	  }

	 number=num;
	var url="save_favorite_img.php"
	 url=url+"?num="+num;
	 url=url+"&sid="+Math.random()
	 xmlHttp.onreadystatechange=stateChanged
	 xmlHttp.open("GET",url,true)
	 xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8;");
	 xmlHttp.send(null)
 }
 
 function checkUser(name,pass)
 {
	 xmlHttp=GetXmlHttpObject()
	  if (xmlHttp==null)
	  {
		alert ("Browser does not support HTTP Request")
	   	return
	  }

	 //number=num;
	var url="check_user.php"
	 url=url+"?username="+name;
	 url=url+"&password="+pass;	 
	 url=url+"&sid="+Math.random()
	 xmlHttp.onreadystatechange=stateChangedCheckUser
	 xmlHttp.open("GET",url,true)
	 xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8;");
	 xmlHttp.send(null)
 }
 
function dellImage(num)
 {
	 xmlHttp=GetXmlHttpObject()
	  if (xmlHttp==null)
	  {
		alert ("Browser does not support HTTP Request")
	   	return
	  }

	 number=num;
	var url="dell_favorite_img.php"
	 url=url+"?num="+num;
	 url=url+"&sid="+Math.random()
	 xmlHttp.onreadystatechange=stateChangedDell
	 xmlHttp.open("GET",url,true)
	 xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8;");
	 xmlHttp.send(null)
 }
 function stateChangedB()
 {
	  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	  {
		//alert(xmlHttp.responseText);
		var jscript = xmlHttp.responseText;

		eval(jscript);
		for (i in points)
		{
			var text = points[i].text;
			var credit = points[i].credit;
		}
		//alert("credit:"+credit+" text:"+text)
		if(credit=='')
		{
			text = text+"<br><br> <table><tr><td>Име: </td><td><input type='text' name='username' id='script_name'/><td></tr><tr><td>Парола: </td><td><input type='password' id='script_pass' name='password'/><td></tr></table><br><br> <a href='registration_form.php'>Регистрация</a> ";
			
			Boxy.ask(text, ["Вход", "Изход"],
				function(val) {
					if(val=='Вход')	
						checkUser(document.getElementById('script_name').value,document.getElementById('script_pass').value)
	    		}, {title: ""
			});
		}
		else if(credit=='null')
		{
			Boxy.alert(text);
		}	
		else
		{
			window.location = "search.php?own=1"	
			if(document.getElementById('user_credit')!=undefined)
				document.getElementById('user_credit').innerHTML=credit;
		}	
		
	  }
 }
 
 function stateChangedCheckUser()
 {
	  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	  {
			if(!xmlHttp.responseText)
			{

					//alert(document.price.radiogroup.length)
					var data='';
					if(document.price.radiogroup.length!=undefined)
					{
						//console.log(document.price.radiogroup.length);
						for (i=0;i<document.price.radiogroup.length;i++) 
						{	
							if (document.price.radiogroup[i].checked) 
								data = document.price.radiogroup[i].value;
						}
					}
					else if (document.price.radiogroup.checked) 
						data = document.price.radiogroup.value;	
						
					buyImage(number,data)	
					  				
			}
			else
			{
				Boxy.alert(xmlHttp.responseText);	
			}
	  }
 }

 function stateChangedI()
 {
	  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	  {
		var jscript = xmlHttp.responseText;
			//alert(eval(jscript));
		eval(jscript);
		for (i in points)
		{
			var user_id = points[i].user_id;
			var img_id = points[i].img_id;
			var desc = points[i].desc;
		}

		document.getElementById('rot').src = '../photos/'+user_id+'/500/'+img_id+'.jpg';
		document.getElementById('desc').innerHTML = desc;
	  }
 }
 function stateChangedP()
 {
	  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	  {
	  	if(xmlHttp.responseText)
	  	{
			document.getElementById('res_text').style.display='inline';
			document.getElementById('link_req').style.display='none';
			//document.getElementById('amount').innerHTML='0.00';
		}
	  }
 }



 function stateChanged()
 {
	  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	  {
		 document.getElementById("favimg"+number+"").style.display = 'inline';
		 document.getElementById("fav"+number+"").style.display = 'none';
		 if(document.getElementById("favimg"+number+"_")!=undefined)
		 {
			 document.getElementById("favimg"+number+"_").style.display = 'inline';
			 document.getElementById("fav"+number+"_").style.display = 'none';
		 }
	  }
 }

 function stateChangedDell()
 {
	  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	  {
		 //document.getElementById("table_"+number+"").style.display = 'none';
		 document.getElementById("favimg"+number+"").style.display = 'none';
		 document.getElementById("fav"+number+"").style.display = 'inline';	
		 if(document.getElementById("favimg"+number+"_")!=undefined)
		 {
			 document.getElementById("favimg"+number+"_").style.display = 'none';
			 document.getElementById("fav"+number+"_").style.display = 'inline';
		 }		 	 
	  }
 }
function GetXmlHttpObject()
{
	var xmlHttp=null;
	 try
	 {
		 // Firefox, Opera 8.0+, Safari
		 xmlHttp=new XMLHttpRequest();
	 }
	 catch (e)
	 {
	 // Internet Explorer
		  try
		  {
		  	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		  }
		  catch (e)
		  {
		  	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
	 }

	return xmlHttp;
}

