/*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;
		
		innerHtml+='<div id="specialOfferLink_inner">';
		innerHtml+='<a class="follow3" href="'+specialOffer.path+'">';
		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<highlights.length; i++){
	var highlight = highlights[i];

	var innerHtml ='';
	if(highlight !== null){
		var link = highlight.path + '/'+highlight.shopIdentifier;
		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 +' ';
			innerHtml+='<nobr>'+ formatNumber(highlight.price, highlight.currencySymbol, highlight.locale) +'</nobr>';
			innerHtml+='</span>';
			innerHtml+='<br/>';
			innerHtml+='<span class="specialPrices">';
			innerHtml+=highlight.lrReducedPrice +' '; 
			innerHtml+='<nobr>'+ formatNumber(highlight.specialPrice, highlight.currencySymbol, highlight.locale) +'</nobr>';					        
		   innerHtml+='</span>';
		}
		else{
			innerHtml+='<span class="text">';
			innerHtml+='<nobr>'+ formatNumber(highlight.price, highlight.currencySymbol, highlight.locale) +'</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;

	}
	
	/*cleanup "wait"-images if not max results are retrieved*/
	var parentContainer = document.getElementById("specialOffersTabs"),
    elementsToCheck = parentContainer.getElementsByTagName('img');

	for(var i=0; i<elementsToCheck.length; i++){
		var str = elementsToCheck[i].id;
		if(str.match("wait")){
			var matchedElement = elementsToCheck[i];
			matchedElement.parentNode.removeChild(matchedElement);
		}    
	}

	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 id="wait_'+i+'" 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;
}



