// ---------------------
// DiscountWidget script
// ---------------------

var GidPartner;
var GidPrivateWhiteLabel;
var Gdns;

/** Add a discount widget component into a given element **/

function addDiscountWidgetToElement(elementIdToIncludeIn, dns, json) {
	// Initialize variables
	var element = document.getElementById(elementIdToIncludeIn);

	// Initialize varialble
	var selected = json.selected;
	var title1 = json.title1;
	var title2 = json.title2;
	var title3 = json.title3;
	var date1 = json.date1;
	var date2 = json.date2;
	var date3 = json.date3;
	var month1 = json.month1;
	var month2 = json.month2;
	var month3 = json.month3;

	// Set the page content
	var html = [];

	// The tab menu
	html.push('<ul id="titles">');
	html.push('<li id="titleMonth1" class="selected" onclick="changeMonth(1);">');
	html.push(title1);
	html.push('</li>');
	html.push('<li id="titleMonth2" class="notSelected" onclick="changeMonth(2);">');
	html.push(title2);
	html.push('</li>');
	html.push('<li id="titleMonth3" class="notSelected" onclick="changeMonth(3);">');
	html.push(title3);
	html.push('</li>');
	html.push('</ul>');

	// The tab1 content
	html.push('<div id="month1" class="month">');
	html.push('<div class="topLinks">');
	html.push('<a class="weekEndLinkForMonth" href="'+dns+'search2Foreign.jsp#searchEngine=departureMonth~'+date1+';duration~Courts sejours / Week-ends;view~product;sort~Popularity;searchEngineType~foreign;price~0-1500;@page~1">WEEK-END</a>');
	html.push('&nbsp;-&nbsp;');
	html.push('<a class="oneWeekLinkForMonth" href="'+dns+'search2Foreign.jsp#searchEngine=departureMonth~'+date1+';duration~Une semaine;view~product;sort~Popularity;searchEngineType~foreign;price~0-1500;@page~1">8 JOURS</a>');
	html.push('&nbsp;-&nbsp;');
	html.push('<a class="twoWeekLinkForMonth" href="'+dns+'search2Foreign.jsp#searchEngine=departureMonth~'+date1+';duration~Deux semaines;view~product;sort~Popularity;searchEngineType~foreign;price~0-1500;@page~1">15 JOURS</a>');
	html.push('&nbsp;/&nbsp;');
	for (var i = 0, len1 = month1.topCountries.length; i < len1; ++i) {
		if(i != 0) html.push('&nbsp;-&nbsp;');
		var country = month1.topCountries[i];
		html.push('<a class="topCountry'+(i+1)+'" href="'+dns+'search2Foreign.jsp#searchEngine=country~'+country.code+';departureMonth~'+date1+';view~product;sort~Popularity;searchEngineType~foreign;price~0-1500;@page~1">'+country.name+'</a>');
	}
	html.push('</div>');
	html.push('<ul id="products1" class="products">');
	for (var i = 0, len1 = month1.products.length; i < len1; ++i) {
		var product = month1.products[i];
		html.push('<li id="month1product'+i+'" class="product" onclick="goToProduct('+product.id+',\''+dns+'\');" onmouseover="this.className=\'product product-on\'" onmouseout="this.className=\'product\'">');
		html.push('<p class="country">'+product.country+'</p>');
		html.push('<p class="discountPercentage">-&nbsp;'+product.discountPercentage+'&nbsp;%</p>');
		html.push('<p class="image"><img src="http://87.237.187.66/'+product.image+'"/></p>');
		html.push('<p class="productName">'+product.name+' '+product.star+'</p>');
		html.push('<p class="city">'+product.city+'</p>');
		html.push('<p class="station">'+product.station+'</p>');
		html.push('<p class="duration">'+product.duration+'</p>');
		html.push('<p class="basePrice">'+product.basePrice+' <span>&euro; TTC</span></p>');
		html.push('<p class="discountPrice">'+product.discountPrice+' <span>&euro; TTC</span></p>');
		html.push('</li>');
	}
	html.push('</ul>');
	html.push('<div class="bottomLinks">');
	html.push('<p class="allProductLinkForMonth"><a href="'+dns+'search2Foreign.jsp#searchEngine=departureMonth~'+date1+';view~product%3Bsort~Popularity;searchEngineType~foreign;price~0-1500;@page~1">Toutes les dates disponibles en '+title1+'</a></p>');
	html.push('<p class="allDiscountedProductLinkForMonth"><a href="'+dns+'search2Foreign.jsp#searchEngine=departureMonth~'+date1+';discountPercentage~0;view~product;sort~Popularity;searchEngineType~foreign;price~0-1500;@page~1">Toutes les promotions en '+title1+'</a></p>');
	html.push('</div>');
	html.push('</div>');

	// The tab2 content
	html.push('<div id="month2" class="month" style="display:none;" >');
	html.push('<div class="topLinks">');
	html.push('<a class="weekEndLinkForMonth" href="'+dns+'search2Foreign.jsp#searchEngine=departureMonth~'+date2+';duration~Courts sejours / Week-ends;view~product;sort~Popularity;searchEngineType~foreign;price~0-1500;@page~1">WEEK-END</a>');
	html.push('&nbsp;');
	html.push('<a class="oneWeekLinkForMonth" href="'+dns+'search2Foreign.jsp#searchEngine=departureMonth~'+date2+';duration~Une semaine;view~product;sort~Popularity;searchEngineType~foreign;price~0-1500;@page~1">8 JOURS</a>');
	html.push('&nbsp;');
	html.push('<a class="twoWeekLinkForMonth" href="'+dns+'search2Foreign.jsp#searchEngine=departureMonth~'+date2+';duration~Deux semaines;view~product;sort~Popularity;searchEngineType~foreign;price~0-1500;@page~1">15 JOURS</a>');
	html.push('<span> / </span>');
	for (var i = 0, len1 = month2.topCountries.length; i < len1; ++i) {
		var country = month2.topCountries[i];
		html.push('<a class="topCountry'+(i+1)+'" href="'+dns+'search2Foreign.jsp#searchEngine=country~'+country.code+';departureMonth~'+date2+';view~product;sort~Popularity;searchEngineType~foreign;price~0-1500;@page~1">'+country.name+'</a> ');
		html.push('&nbsp;');
	}
	html.push('</div>');
	html.push('<ul id="products2" class="products">');
	for (var i = 0, len1 = month2.products.length; i < len1; ++i) {
		var product = month2.products[i];
		html.push('<li id="month1product'+i+'" class="product" onclick="goToProduct('+product.id+',\''+dns+'\');">');
		html.push('<p class="country">'+product.country+'</p>');
		html.push('<p class="discountPercentage">-&nbsp;'+product.discountPercentage+'&nbsp;%</p>');
		html.push('<p class="image"><img src="http://87.237.187.66/'+product.image+'"/></p>');
		html.push('<p class="productName">'+product.name+' '+product.star+'</p>');
		html.push('<p class="city">'+product.city+'</p>');
		html.push('<p class="station">'+product.station+'</p>');
		html.push('<p class="duration">'+product.duration+'</p>');
		html.push('<p class="basePrice">'+product.basePrice+' <span>&euro; TTC</span></p>');
		html.push('<p class="discountPrice">'+product.discountPrice+' <span>&euro; TTC</span></p>');
		html.push('</li>');
	}
	html.push('</ul>');
	html.push('<div class="bottomLinks">');
	html.push('<p class="allProductLinkForMonth"><a href="'+dns+'search2Foreign.jsp#searchEngine=departureMonth~'+date2+';view~product%3Bsort~Popularity;searchEngineType~foreign;price~0-1500;@page~1">Toutes les dates disponibles en '+title2+'</a></p>');
	html.push('<p class="allDiscountedProductLinkForMonth"><a href="'+dns+'search2Foreign.jsp#searchEngine=departureMonth~'+date2+';discountPercentage~0;view~product;sort~Popularity;searchEngineType~foreign;price~0-1500;@page~1">Toutes les promotions en '+title2+'</a></p>');
	html.push('</div>');
	html.push('</div>');

	// The tab3 content
	html.push('<div id="month3" class="month" style="display:none;" >');
	html.push('<div class="topLinks">');
	html.push('<a class="weekEndLinkForMonth" href="'+dns+'search2Foreign.jsp#searchEngine=departureMonth~'+date3+';duration~Courts sejours / Week-ends;view~product;sort~Popularity;searchEngineType~foreign;price~0-1500;@page~1">WEEK-END</a>');
	html.push('&nbsp;');
	html.push('<a class="oneWeekLinkForMonth" href="'+dns+'search2Foreign.jsp#searchEngine=departureMonth~'+date3+';duration~Une semaine;view~product;sort~Popularity;searchEngineType~foreign;price~0-1500;@page~1">8 JOURS</a>');
	html.push('&nbsp;');
	html.push('<a class="twoWeekLinkForMonth" href="'+dns+'search2Foreign.jsp#searchEngine=departureMonth~'+date3+';duration~Deux semaines;view~product;sort~Popularity;searchEngineType~foreign;price~0-1500;@page~1">15 JOURS</a>');
	html.push('<span> / </span>');
	for (var i = 0, len1 = month3.topCountries.length; i < len1; ++i) {
		var country = month3.topCountries[i];
		html.push('<a class="topCountry'+(i+1)+'" href="'+dns+'search2Foreign.jsp#searchEngine=country~'+country.code+';departureMonth~'+date3+';view~product;sort~Popularity;searchEngineType~foreign;price~0-1500;@page~1">'+country.name+'</a> ');
		html.push('&nbsp;');
	}
	html.push('</div>');
	html.push('<ul id="products3" class="products">');
	for (var i = 0, len1 = month3.products.length; i < len1; ++i) {
		var product = month3.products[i];
		html.push('<li id="month1product'+i+'" class="product" onclick="goToProduct('+product.id+',\''+dns+'\');">');
		html.push('<p class="country">'+product.country+'</p>');
		html.push('<p class="discountPercentage">-&nbsp;'+product.discountPercentage+'&nbsp;%</p>');
		html.push('<p class="image"><img src="http://87.237.187.66/'+product.image+'"/></p>');
		html.push('<p class="productName">'+product.name+' '+product.star+'</p>');
		html.push('<p class="city">'+product.city+'</p>');
		html.push('<p class="station">'+product.station+'</p>');
		html.push('<p class="duration">'+product.duration+'</p>');
		html.push('<p class="basePrice">'+product.basePrice+' <span>&euro; TTC</span></p>');
		html.push('<p class="discountPrice">'+product.discountPrice+' <span>&euro; TTC</span></p>');
		html.push('</li>');
	}
	html.push('</ul>');
	html.push('<div class="bottomLinks">');
	html.push('<p class="allProductLinkForMonth"><a href="'+dns+'search2Foreign.jsp#searchEngine=departureMonth~'+date3+';view~product%3Bsort~Popularity;searchEngineType~foreign;price~0-1500;@page~1">Toutes les dates disponibles en '+title3+'</a></p>');
	html.push('<p class="allDiscountedProductLinkForMonth"><a href="'+dns+'search2Foreign.jsp#searchEngine=departureMonth~'+date3+';discountPercentage~0;view~product;sort~Popularity;searchEngineType~foreign;price~0-1500;@page~1">Toutes les promotions en '+title3+'</a></p>');
	html.push('</div>');
	html.push('</div>');

	element.innerHTML = html.join('');
	changeMonth(parseInt(selected));
}

//create a namespace for this example:
YAHOO.namespace("rpc.DiscountWidget");

YAHOO.rpc.DiscountWidget = function(dns, nbElements) {

	//set up some shortcuts in case our typing fingers 
	//get lazy: 
	var Get = YAHOO.util.Get,
		tIds = {},
		current = null,
		elementIdToIncludeIn = null,
		dns = null;

	// We use the Get Utility's success handler in conjunction with
	// the web service callback in order to detect bad responses
	// from the web service.
	var onSiteExplorerSuccess = function(o) {
		// stop blocking requests
		loading = false;

		// A success response means the script node is inserted.  However, the
		// utility is unable to detect whether or not the content of the script
		// node is correct, or even if there was a bad response (like a 404
		// error).  To get around this, we use the web service callback to
		// verify that the script contents was correct.
		if (o.tId in tIds) {
			YAHOO.log("The Get Utility has fired the success handler indicating that the " + "requested script has loaded and is ready for use.", "info", "example"); 
		} 
		else {
			YAHOO.log("The Get utility has fired onSuccess but the webservice callback did not " + "fire.  We could retry the transaction here, or notify the user of the " + "failure.", "info", "example");
		}
	} 

	var onSiteExplorerFailure = function(o) {
		alert("An error occured while getting datas");
	}

	// Function to retrieve data from RPC web service 
	var getSiteExplorerData = function(nbElements, dns) {
	
		//prepare the URL for the Yahoo Site Explorer API: 
		var sURL = dns+"DiscountController?nbProducts="+nbElements+"&imageType=home&callback=YAHOO.rpc.DiscountWidget.callback";		

		if(GidPartner != null && GidPartner != '' && GidPartner != "undefined")
			sURL = sURL+'&idPartner='+GidPartner;
		if(GidPrivateWhiteLabel != null && GidPrivateWhiteLabel != '' && GidPrivateWhiteLabel != "undefined")
			sURL = sURL+'&idPrivateWhiteLabel='+GidPrivateWhiteLabel;

		sURL = sURL+"&ramdom="+Math.random() ;
		
		//This simple line is the call to the Get Utility; we pass
		//in the URL and the configuration object, which in this case
		//consists merely of our success and failure callbacks:
		var transactionObj = Get.script(sURL, {
			onSuccess: onSiteExplorerSuccess,
			onFailure: onSiteExplorerFailure,
			scope	: this
		});

		// keep track of the current transaction id.  The transaction will be
		// considered complete only if the web service callback is executed.
		current = transactionObj.tId;
	}
	return {
		init: function(elementId, nbElements, dnsURL) {
			elementIdToIncludeIn = elementId;
			dns = dnsURL;
			getSiteExplorerData(nbElements, dns);
		},

		callback: function(results) {
			// Mark the transaction as complete.  This will be checked by the onSuccess
			// handler to determine if the transaction really succeeded.
			tIds[current] = true;

			// Add the widget into the element
			addDiscountWidgetToElement(elementIdToIncludeIn, dns, results);
		}
	}
}();



/** Constructor for DiscountWidget **/
DiscountWidget = function(elementIdToIncludeIn, nbElements, option) {
	
	// Initailize variables
	if(typeof option == 'string') {
		 Gdns = option;
	}
	else {
		if(option.dns != null)
			Gdns = option.dns;
		if(option.idPartner != null)
			GidPartner = option.idPartner;
		if(option.idPrivateWhiteLabel != null)
			GidPrivateWhiteLabel = option.idPrivateWhiteLabel;
	}
	
	// Add '/' at the end of the dns 
	if(Gdns != null && Gdns.charAt(Gdns.length-1) != '/') Gdns = Gdns+'/';

	// Init widget
	YAHOO.rpc.DiscountWidget.init(elementIdToIncludeIn, nbElements, Gdns);
}; 

/** Change the selected month **/
function changeMonth(month) {
	if(month == 1) {
		document.getElementById('titleMonth1').className = 'selected';
		document.getElementById('titleMonth2').className = 'notSelected';
		document.getElementById('titleMonth3').className = 'notSelected';
		document.getElementById('month1').style.display = 'block';
		document.getElementById('month2').style.display = 'none';
		document.getElementById('month3').style.display = 'none';
	}
	if(month == 2) {
		document.getElementById('titleMonth1').className = 'notSelected';
		document.getElementById('titleMonth2').className = 'selected';
		document.getElementById('titleMonth3').className = 'notSelected';
		document.getElementById('month1').style.display = 'none';
		document.getElementById('month2').style.display = 'block';
		document.getElementById('month3').style.display = 'none';
	}
	if(month == 3) {
		document.getElementById('titleMonth1').className = 'notSelected';
		document.getElementById('titleMonth2').className = 'notSelected';
		document.getElementById('titleMonth3').className = 'selected';
		document.getElementById('month1').style.display = 'none';
		document.getElementById('month2').style.display = 'none';
		document.getElementById('month3').style.display = 'block';
	}
}

/** Redirect to the product page **/
function goToProduct(id, dns) {
	var url = dns+'product.do?idProduct='+id+'&location=homeDiscount';
	if(GidPartner != null && GidPartner != '' && GidPartner != "undefined")
		url = url+'&idPartner='+GidPartner;
	if(GidPrivateWhiteLabel != null && GidPrivateWhiteLabel != "" && GidPrivateWhiteLabel != "undefined")
		url = url+'&idPrivateWhiteLabel='+GidPrivateWhiteLabel;
	window.location.href = url;
}

/** Include a JavaScript file **/
function inc_js(fileName) {
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = fileName;
	document.getElementsByTagName("head")[0].appendChild(script);
}

/** Include a CSS file **/
function inc_css(fileName) {
	var script = document.createElement("link");
	script.type = "text/css";
	script.rel = "stylesheet";
	script.href = fileName;
	document.getElementsByTagName("head")[0].appendChild(script);
}