

var stageURL = StageURL();



if(undefined===window.arrWidgets)
{
	//alert('init the array');
	arrWidgets = new Array();
}

function TravelonomyWidget(id,height,width)
{
	//var UID = 0;
	this.Results = 0;
	this.Locations = "";
	this.DivId = "0";
	this.PortalName = "";
	this.WidgetId =id;
	this.SId = -1;
	this.AccommodationType = 0;
	this.ReadElementsByTagName = "";
	this.ReadElementByID = "";
	this.ReadElementPageTitle = false;
	this.ReadElementKeywords = false;
	this.ReadElementPage = true;
	this.Debug = false;
	this.Title = "";
	//Restrict search to this country only
	this.RCountry = "";
	this.RLocation = "";
	this.URLPrams = "";
	this.WidgetHeight = height;
	//this.WidgetHeight = 0;
	//this.WidgetWidth = 300;
	this.WidgetWidth = width;
	this.SearchBox = false;
	
	
	this.createIframe = function()
	{
	   //alert(widget.Results)
	  var iframe;
	  var scrolling = 'scrolling="no"';
	  //var background = 'background-color="transparent"';/WP/affliateID/widgetID
	  var background ='';
	  var HeaderFooterHeight = 45;
	  var ResultsHeight = 83;
	 
	  //var nCols   = (this.WidgetWidth<230)? 1: Math.floor(this.WidgetWidth/230);
	  var nCols   = Math.floor(this.WidgetWidth/230);
	  
	  var nRows = Math.ceil(this.Results/nCols);
	  
	  if(this.WidgetHeight==0)
		this.WidgetHeight = nRows *ResultsHeight + HeaderFooterHeight;
		
	 
	  //alert(this.URLPrams);
	  iframe = '<iframe src="' + stageURL  + '/widget/widget.php?' + this.URLPrams + '" style="width:' + this.WidgetWidth + 'px;height:' + this.WidgetHeight + 'px;"' +  scrolling + 'marginwidth="0" ' + background +' allowTransparency="true" marginheight="0" frameborder="0"></iframe>';
	 
	  //newdiv = document.getElementById(this.DivId);
	  
	 
	  //if(newdiv)
	 // {
		//newdiv.innerHTML=iframe;
		document.write(iframe);
	 // }
	}
	
	this.readPage = function()
	{
		var contextualContentByTagName = "";
		var contextualContentById = "";
		
		if(document.getElementById(this.ReadElementByID))
		{	
			contextualContentById = (this.ReadElementByID == "") ? "" : document.getElementById(this.ReadElementByID).innerHTML;
			//alert("obj=" + document.getElementById(this.ReadElementByID) + "\n\nDiv content: " + contextualContentById);
		}
	
		var contextualContentByTagNameArray = (this.ReadElementsByTagName == "") ? "" : document.getElementsByTagName(this.ReadElementsByTagName);	
		if(contextualContentByTagNameArray != "")
		{
			var contextualContentByTagName = "";
			for(var y=0; y < contextualContentByTagNameArray.length; y++)
			{
				contextualContentByTagName += contextualContentByTagNameArray[y].innerHTML;
			}
				//alert(contextualContentByTagName);
		}
		
		return contextualContentByTagName += contextualContentById;
	}
	
	this.display = function() //onload=function()
	{
	
	
		//Advanced options: Read ONLY (Div's p span etc etc....) parts of the page
		
		if(this.ReadElementPage == false)
		{
			if(this.ReadElementPageTitle == true)
				this.Locations += "\n\n" + document.title + "\n\n";
				
			if(this.ReadElementKeywords == true)
				this.Locations += getMetaKeywords("keywords") + "\n\n";
			
			this.Locations += this.readPage();
		}
		
		
		this.URLPrams  = "";
		this.URLPrams +=  (this.AccommodationType == 0) ? '' : '&acctype=' + this.AccommodationType;
		this.URLPrams +=  (this.Title == "") ? '' : "&title=" + this.Title;
		this.URLPrams +=  (this.SearchBox) ? '&searchbox=true' : '';
		this.URLPrams +=  '&hosttitle=' + escape(document.title);
		//this.URLPrams +=  '&hosttitle=' + document.title;
		this.URLPrams +=  (this.RCountry == "") ? '' : '&rcountry=' + trim(this.RCountry); 
		this.URLPrams +=  (this.RLocation == "") ? '' : '&rlocation=' + trim(this.RLocation); 
		this.URLPrams +=  (this.Results == 0) ? '' : '&numberResultsDisplay=' + this.Results;
		//this.URLPrams +=  (this.PortalName == "") ? '&portalname=travelonomy' : '&portalname=' + this.PortalName;
		this.URLPrams +=  (this.WidgetId == "") ? '' : '&widgetid=' + this.WidgetId;
		this.URLPrams +=  (this.ReadElementPage) ? '&readpage=true' : '';
		this.URLPrams +=  (this.WidgetWidth>0) ? '&width='+ this.WidgetWidth : '';
		this.URLPrams +=  (this.WidgetHeight>0) ? '&height='+ this.WidgetHeight : '';
		this.URLPrams +=  (this.SId>0) ? '&sid=' + this.SId : '';


		//this.URLPrams +=   (this.ReadElementPage) ? '&readpage=true' : '&readpage=false';
		this.URLPrams +=  (this.Locations == "") ? '' : '&locations=' + this.Locations;
		
		//alert(this.URLPrams);
		
		
		if(this.Debug)
			alert("Locations read from tags and IDS: \n\n" + this.Locations);
		
		
		this.createIframe();
// 		this.createImpression();
	}
}
	
	//window.onload=function() //onload=function()
	
	
function getMetaKeywords(strMetaName)
{
	var Kwords = document.getElementsByTagName('meta');   
	for(var i in Kwords)
	{    
		if(Kwords[i].name == strMetaName)
		{      
			return Kwords[i].content;    
		}   
	} 
}


function StageURL()
{
	var currentstage = '';
	currentstage = window.location.href.split('.')[2];
	
	
	var URL = "http://affiliate.travelonomy.com"
	
	if(currentstage && currentstage == 'local')
	{
	  URL = "http://affiliate.travelonomy.local.doloforge.com"
	}
	else if(currentstage && currentstage == 'test')
	{
	  URL = "http://affiliate.travelonomy.test.doloforge.com"
	}
	return URL;
}
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}




