/*global window, document, 
AsyncHighlight*/

//set global interval var
var refreshInterval;
var isFirstReload = true;


function displaySpecialOfferLink(specialOfferQuery){
	var innerHtml='';
	var specialOffer = '';
	if (specialOfferQuery !== null) {
		specialOffer = specialOfferQuery;
		
		var specId = "";
		if(specialOffer.id){
			specId = '/'+specialOffer.id;
		}
		
		innerHtml+='<div id="specialOfferLink_inner">';
		innerHtml+='<a class="follow3" href="'+specialOffer.path+specId+'">';
		innerHtml+= specialOffer.title+'&nbsp;';
		innerHtml+='</a>';
		innerHtml+='</div>';
	}else if(document.getElementById("tabLeft").className == "active"){
		innerHtml+='<div id="specialOfferLink_inner">';
		innerHtml+='<a class="follow3" href="/specialoffers/">';
		innerHtml+= document.getElementById("tabLeft").getAttribute("title")+'&nbsp;';
		innerHtml+='</a>';
		innerHtml+='</div>';
	}
	document.getElementById('specialOfferLink').innerHTML = innerHtml;
}

function displayHighlights(highlights,gattung){
	for (var i=0; i<4; i++){
	var currency;
	var highlight = highlights[i];

	var innerHtml ='';
	if(highlight !== null){
		var link = highlight.path + '/'+highlight.shopIdentifier;
	
		// Pass Currency via Session or PageContext
		switch(highlight.currencySymbol)
		{
			case "CZK"	: currency = 'K&#269;';break;
			case "GBP"	: currency = '&pound;';break;
			case "PLN"	: currency = 'z&#322;';break;
			default		: currency = highlight.currencySymbol;break;
		}

		innerHtml+='<table width="100%" cellspacing="0" cellpadding="0" border="0" align="center">';
			innerHtml+='<tbody><tr>';
				innerHtml+='<td valign="middle" align="center" style="height: 85px;">';
					innerHtml+='<a title="'+highlight.title+'" href="'+ link +'">';
						innerHtml+='<img border="0" class="special_offer" alt="" src="'+ highlight.pictureUrl +'"/>';
					innerHtml+='</a>';
				innerHtml+='</td>';
			innerHtml+='</tr>';
			innerHtml+='<tr>';
				innerHtml+='<td valign="top" align="center" class="text">';
					innerHtml+='<a title="" href="'+link+'">';
						innerHtml+=highlight.title;
					innerHtml+='</a>';
				innerHtml+='</td>';
			innerHtml+='</tr>';
			innerHtml+='<tr>';
				innerHtml+='<td valign="top" align="center" class="text">';
				
        if(highlight.lrReducedPrice !== null){
			innerHtml+='<span class="smalltext">';
			innerHtml+=highlight.lrStandardPrice +' ';
			
			if(highlight.currencySymbol == "GBP"){
				innerHtml+='<nobr>'+ currency+highlight.price+'</nobr>';
			}
			else{
				innerHtml+='<nobr>'+ highlight.price+' ' +currency+'</nobr>';
			}
		   innerHtml+='</span>';
		   innerHtml+='<br/>';
		   innerHtml+='<span class="specialPrices">';
			innerHtml+=highlight.lrReducedPrice +' '; 
			if(highlight.currencySymbol == "GBP"){
				innerHtml+='<nobr>'+ currency + highlight.specialPrice +'</nobr>';
			}
			else{
				innerHtml+='<nobr>'+ highlight.specialPrice+' ' +currency+'</nobr>';
			}					        
		   innerHtml+='</span>';
		}
		else{
			innerHtml+='<span class="text">';
			if(highlight.currencySymbol == "GBP"){
				innerHtml+='<nobr>'+ currency + highlight.price + '</nobr>';
			}
			else{ 
				innerHtml+='<nobr>'+ highlight.price+' ' +currency+'</nobr>';
			}
		   innerHtml+='</span>';
		}
			
		innerHtml+='<a title="" href="'+link+'">';
		innerHtml+='&nbsp;';
		innerHtml+='<img width="9" height="9" border="0" alt="" src="/images/pfeil.gif"/>';
		innerHtml+='</a>';
		innerHtml+='</td>';
		innerHtml+='</tr>';
	        
		innerHtml+='</tbody></table>';
	}
		document.getElementById('specialOffer_'+i).innerHTML = innerHtml;

	}

	AsyncHighlight.getSpecialOfferLink("/shop","SHOP.SPECIAL_OFFERS",gattung,
				{
					callback:function(specialOfferQuery){
						displaySpecialOfferLink(specialOfferQuery);}}
	);
			
}


function getHighlights(key,gattung,id){
	// click on highlight category  reset the refreshes the AntisessionTimeout interval
	if(refreshInterval){
		window.clearInterval(refreshInterval);
	}

	//set the correct style for tab
	var tabArray = ["tabLeft", "tabdogs", "tabcats", "tabrodents","tabbirds","tabfish","tabhorses","tabreptiles","tabRight"];
	var j = 0;
	while(j<tabArray.length){
		if (document.getElementById(tabArray[j]) !== null) {
			document.getElementById(tabArray[j]).className = "";
			if (tabArray[j] == id){
				document.getElementById(tabArray[j]).className = "active";
			}
		}
		j++;
	}

	//set the waiting animation for all table cells

	var i = 0;
	while(i<4){
	var innerHtml ='';
		innerHtml+='<table width="100%" height="180px" cellspacing="0" cellpadding="0" border="0" align="center">';
		innerHtml+='<tbody><tr>';
		innerHtml+='<td valign="middle" align="center" style="height: 155px;">';
			innerHtml+='<img src="/images/w-z/wait.gif" border="0">';  

		innerHtml+='</td>';
		innerHtml+='</tr>';
		innerHtml+='</tbody></table>';
		document.getElementById('specialOffer_'+i).innerHTML = innerHtml;
		i++;				
	}

	// workaround to get the 'null' string to real NULL - only if tabLeft (TopAngebote)
	if (id=='tabLeft'){ 
		gattung=null;
		}

	 AsyncHighlight.getHighlightsForKey(
	                        key, gattung,
							{callback:function(highlights){
									displayHighlights(highlights, gattung);
	                        }
							});
	return false;
}


