//you should put this script in the head area e.g <script type="text/javascript" src="path_in_your_server/referrer_static.js"> </script>
// and after that the script generated from the generator e.g <script type="text/javascript" src="path_in_your_server/generated_script.js"> </script>
var ref_list=new Array();
var recent_headers=new Array();
var phone_numbers=new Array();
var home_ads=new Array();
var make_reservations=new Array();
var call_divs=new Array();


var reff=document.referrer;//the documents real referrer
if(navigator.appVersion.match("Chrome") && reff.length==0)
{
	if(window.loacation.href.match("?ref=su"))
		reff="clicks.superpages.com";
}
var ref;//the referrer we have,or will set in the coockie



//this is the main function
//should be called at the body tag with an onload attribute
//e.g <body onload="changePhonesOnReferrer()">
//OR before the closing body tag
//e.g <script type="text/javascript" defer="defer">changePhonesOnReferrer();</script>
//    </body>
function changePhonesOnReferrer()
{
	if(!reff.match(ref_list[0]) && reff.length>0)//if we got different referrer than the site itself,reff.length>0 to check we got a referrer at all 
	{
		if(get_cookie("refphone").length==0)//if we got a previous valid coockie skips,else (get_cookie("refphone").length==0) we set the coockie
		set_cookie("refphone",reff,60*60*3,"thrillplanet.com");//60*60*x means seconds*minutes*hours (e.g 3 hours=60*60*3),thats the time that the coockie will stay valid on browsers other than IE in seconds.On IE it will last for the browser's session(until the user closes the browser)
		ref=get_cookie("refphone");//we get the value of the previous coockie set,or if not set the value of the existing coockie
	
		setImages();//we set the right images
		setTextPhone();//we set the right phone numbers in the text
		setCallDivs();//we set the right background for the div with id="call"
	
	}
	else//we got the site itself as referrer or direct traffiq
	{
	
		ref=get_cookie("refphone");//we get the coockie if exist,if not we get ""
		if(ref.length>0)//if coockie exist(is valid)
		{
			setImages();
			setTextPhone();
			setCallDivs();
			//we set again the changes so they appear while user navigates in the site(every inner  page has referrer the site itself)
		}
		else//if our coockie is not valid or we have direct traffiq,we set the default images
		{

			setDefaultImages();
			setDefaultTextPhone();
			setDefaultCallDivs();
		}
	}
}



function setImages()
{
	setDefaultImages();//first we set all images with their default value,so we can make the changes later
	var images=document.getElementsByTagName('img');//we got all the images and we will search by the filename,since no id was provided
	
	for(var i=0;i<images.length;i++)
	{
		if(ref.match(ref_list[0]))//if we got referrer the site itself there is no need to make changes,we have set the default images before
			break;
				
		if(images[i].src.match("images/recent_header.gif"))//we change top right image
			for(var j=0;j<ref_list.length;j++)//we search for the referrer from the referrers list we want to track
			{
				if(ref.match(ref_list[j]))//if we find it,we make the change
				{
					images[i].src=recent_headers[j];
					break;
				}
			}
		if(images[i].src.match("images/home_ad.gif"))//we change the image on main page
			for(var j=0;j<ref_list.length;j++)//we search for the referrer from the referrers list we want to track
			{
				if(ref.match(ref_list[j]))
				{
					images[i].src=home_ads[j];
					break;
				}
			}
		if(images[i].src.match("images/make_reservation.gif"))//we change the resrvation image
			for(var j=0;j<ref_list.length;j++)
			{
				if(ref.match(ref_list[j]))
				{
					images[i].src=make_reservations[j];
					break;
				}
			}
	
	}
}

function setTextPhone()
{
	setDefaultTextPhone();//first we set all text phone numbers with their default value,so we can make the changes later
	var phoneText=document.body.innerHTML;//we get the whole document text
	
	if(ref.match(ref_list[0]))//if we have referrer the same site we dont have to do anything
		return;
				
		
		
	for(var j=0;j<ref_list.length;j++)//we search for the referrer from the referrers list we want to track
	{
		if(ref.match(ref_list[j]))//if we find it
		{
			phoneText=phoneText.replace(phone_numbers[0],phone_numbers[j]);//we make the change
			document.body.innerHTML=phoneText;//and set for the documents text the default					
			break;//if we did all that,there is no need to continue the loop,we are ready
		}
	}
}

function setCallDivs()
{
	setDefaultCallDivs();//first we set the background with the default value,so we can make the changes later
	var callDiv=document.getElementById('call');//we get the div with id="call"
	
		if(ref.match(ref_list[0]) || callDiv==null)//if we got referrer the site itself or we don't have that div on that page
			return;//we exit that function
			for(var j=0;j<ref_list.length;j++)//we search for the referrer
			{
				if(ref.match(ref_list[j]))//if we find it
				{
					callDiv.style.backgroundImage="url("+call_divs[j]+")";//we change the background
					callDiv.style.backgroundRepeat="no-repeat";//and we make it not to repeat					
					break;//and thats all we had to do,so we exit the loop
				}
			}
}

function setDefaultImages()
{

	var images=document.getElementsByTagName('img');
	for(var i=0;i<images.length;i++)
	{
		
		//we search what image changed,and if changed we set its default value
		for(var j=0;j<recent_headers.length;j++)
		{
			if(images[i].src.match(recent_headers[j]))
			{
				images[i].src=recent_headers[0];
			}
			if(images[i].src.match(home_ads[j]))
			{
				images[i].src=home_ads[0];
			}
			if(images[i].src.match(make_reservations[j]))
			{
				images[i].src=make_reservations[0];
			}
		}
		
	}
}

function setDefaultTextPhone()
{

	var phoneText=document.body.innerHTML;
	//we search if text phone number changed,and if changed we set its default value
	for(var i=0;i<ref_list.length;i++)
	{
		phoneText=phoneText.replace(phone_numbers[i],phone_numbers[0]);
		
	}
	document.body.innerHTML=phoneText;
}

function setDefaultCallDivs()
{

	var callDiv=document.getElementById('call');
	
	if(callDiv==null || ref_list.length==0)//if we have not that div on the page or we have not set a default
		return;//we exit
	
	callDiv.style.backgroundImage="url("+call_divs[0]+")";
	callDiv.style.backgroundRepeat="no-repeat";
	
}


function set_cookie(c_name,value,expiresecs,vdomain)//function for seting the coockie,c_name:coockie name,value:the value (referrer),expiresecs: the seconds we want the coockie to stay valid,vdomain:the valid domain e.g domain.com to include subdomains
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiresecs);
	document.cookie=c_name+ "=" +escape(value)+
	((expiresecs==null) ? "" : ";max-age="+expiresecs)+"; path=/"+"; domain="+vdomain;
}

function get_cookie(c_name)//function tha returns the value of the coockie with the given name(c_name)
{
	if (document.cookie.length>0)
  	{
  		c_start=document.cookie.indexOf(c_name + "=");
  		if (c_start!=-1)
    		{
    			c_start=c_start + c_name.length+1;
    			c_end=document.cookie.indexOf(";",c_start);
    			if (c_end==-1) c_end=document.cookie.length;
    			return unescape(document.cookie.substring(c_start,c_end));
    		}
  	}
	return "";
}


