DealFinder = {

EOGnumbersLength : 0,

countForArray : 0,

totalToDisplay : new Array(),

countForArray2 : 0,

togPriceSortedArray : new Array(),

buildDealFinderForm: function(element1, innerHTMLContent){
	
var elem = $$(element1);
if(!elem[0])return false;
if(!innerHTMLContent) return false;
var dealFinderForm  = innerHTMLContent;
	if(elem[0].up(0).nodeName === "P"){
	new Insertion.Before(elem[0].up(0), dealFinderForm);
	} 
	else{
	new Insertion.Before(elem[0], dealFinderForm);
	}
	elem.each(function(dfto){ 					   
	dfto.hide();
		if(dfto.nextSibling.nodeName === "BR"){
		$(dfto.nextSibling).remove();	
		}
		if(dfto.nextSiblings()[0] && dfto.nextSibling.nodeType === 3 && dfto.nextSiblings()[0].nodeName === "BR"){
		dfto.nextSiblings()[0].remove();	
		}
	var optionText = dfto.firstChild.nodeValue.split(":")[0];
	var optionValue = dfto.firstChild.nodeValue.split(":")[1];
	var optionElement = document.createElement("option");
	optionElement.setAttribute("value", optionValue);
	optionElement.innerHTML = optionText;
	$(DealFinder.selectElement).appendChild(optionElement);	
	dfto.remove();
	});
},

submitDealFinder: function(){
if(!$(DealFinder.selectElement)) return false;
var EOG_numbers = $(DealFinder.selectElement).value;
var eogArray = new Array()
	if(typeof(EOG_numbers) != "undefined"){// If EOG_numbers exists in the page
		if(EOG_numbers != ""){// If EOG_numbers has any content
		eogArray = EOG_numbers.split(',');
		}
	}
	DealFinder.EOGNumbersLength = eogArray.length;
	if(eogArray.length > 0){
		for(j=0,jLen=eogArray.length; j<jLen; j++){
		var url1 = DealFinder.ajaxFileURL+eogArray[j]+".html?time="+escape(TogOffers.timestamp);
		new Ajax.Request( url1, { method: 'get', onComplete: DealFinder.showResponseDF });
		}
	}
	setTimeout("TogOffers.clickableElems('div', 'clickBox')", 500);
	setTimeout("TogOffers.alternateOfferStyle()", 500);
},

showResponseDF: function(originalRequest){
var jsonRaw = originalRequest.responseText;
DealFinder.writeTogArray(jsonRaw);
},

writeTogArray: function(jsonRaw){	
jsonContent = eval("(" + jsonRaw + ")");
var ao = jsonContent[DealFinder.ajaxObj];
	if (ao){
		if (ao[DealFinder.ajaxObjPropBool1] === "1" && ao[DealFinder.ajaxObjPropStr4] === ""){
		DealFinder.EOGNumbersLength--;
		}
		else{
		DealFinder.totalToDisplay[DealFinder.countForArray] = jsonContent;
		DealFinder.countForArray++;			
		}
	}
	else{
	DealFinder.totalToDisplay[DealFinder.countForArray] = jsonContent;
	DealFinder.countForArray++;
	}		
	var searchoptions = $(DealFinder.selectSortElement).value;		
	if(searchoptions === DealFinder.optionValueForPriceLowSorting || searchoptions === DealFinder.optionValueForPriceHighSorting || searchoptions === null){
	DealFinder.sortByPrice(searchoptions);
	}
	if(searchoptions === DealFinder.optionValueForDepartureSorting){
	DealFinder.sortByDeparture();
	}
	if(searchoptions === DealFinder.optionValueForDurationShortSorting || searchoptions === DealFinder.optionValueForDurationLongSorting){
	DealFinder.sortByPrice(searchoptions);	
	}
DealFinder.showSorryOrCounter(DealFinder.totalToDisplay);
},

sortByPrice: function(searchoptions){	
var togPrices = new Array;
var togArrayPrice = DealFinder.totalToDisplay;
	for(n=0,nLen=togArrayPrice.length; n<nLen; n++){/* extract the prices and put them in a new array */
		togPrices[n] = togArrayPrice[n][DealFinder.ajaxObj][DealFinder.ajaxObjPropNum1];
	}	
togPrices.sort(function(a,b){return a - b});
var togPricesUniq = new Array;
togPricesUniq = togPrices.uniq();// remove duplicates prices
	for(o=0,oLen=togPricesUniq.length; o<oLen; o++){//Any togs that match the price at [o] should be written to a new Array
		for(p=0,pLen=togArrayPrice.length; p<pLen; p++){	
			if(togArrayPrice[p][DealFinder.ajaxObj][DealFinder.ajaxObjPropNum1] === togPricesUniq[o]){
			DealFinder.writeTogArrayPriceSorted(togArrayPrice[p],DealFinder.totalToDisplay,searchoptions);
			}
			else continue;
		}
	}
},

sortByDeparture: function(){
$(DealFinder.outputOfferHTMLULContainer).innerHTML = "";	
var TogArrayDeparture = new Array;
	for(q=0,qLen=DealFinder.totalToDisplay.length; q<qLen; q++){
		var myDate = new Date();
		if(DealFinder.totalToDisplay[q][DealFinder.ajaxObj]){
			myDate = DealFinder.totalToDisplay[q][DealFinder.ajaxObj][DealFinder.ajaxObjPropDate1]
			var dt = Date.parse(myDate);
			TogArrayDeparture[q] = dt; 
		}
	}
TogArrayDeparture.sort();
var TogArrayDepartureUniq = new Array;
TogArrayDepartureUniq = TogArrayDeparture.uniq();
	for(r=0,rLen=TogArrayDepartureUniq.length; r<rLen; r++){
		for(s=0,sLen=DealFinder.totalToDisplay.length; s<sLen; s++){
			if(Date.parse(DealFinder.totalToDisplay[s][DealFinder.ajaxObj][DealFinder.ajaxObjPropDate1]) === TogArrayDepartureUniq[r]){
			DealFinder.writeTog(DealFinder.totalToDisplay[s]);
			}
			else continue;
		}
	}
},

sortByDuration: function(searchoptions, togPriceSortedArray){
var TogArrayDuration = new Array;
var TogArrayDurationUnder11 = new Array;
var TogArrayDurationOver11 = new Array;
$(DealFinder.outputOfferHTMLULContainer).innerHTML = "";
	for(t=0,tLen=togPriceSortedArray.length; t<tLen; t++){
		if(togPriceSortedArray[t][DealFinder.ajaxObj][DealFinder.ajaxObjPropStr6] < 11){
		TogArrayDurationUnder11[t] = togPriceSortedArray[t][DealFinder.ajaxObj][DealFinder.ajaxObjPropStr6];
		}
		else{
		TogArrayDurationOver11[t] = togPriceSortedArray[t][DealFinder.ajaxObj][DealFinder.ajaxObjPropStr6];
		}
	}
	TogArrayDurationUnder11.sort(function(a,b){return a - b});
	TogArrayDurationOver11.sort(function(a,b){return a - b});	
	if(searchoptions === DealFinder.optionValueForDurationShortSorting){
	TogArrayDuration = TogArrayDurationUnder11.concat(TogArrayDurationOver11);	
	}
	if(searchoptions === DealFinder.optionValueForDurationLongSorting){
	TogArrayDurationOver11.reverse([inline = true]);
	TogArrayDurationUnder11.reverse([inline = true]);
	TogArrayDuration = TogArrayDurationOver11.concat(TogArrayDurationUnder11);
	}		
	var TogArrayDurationUniq = new Array;
	TogArrayDurationUniq = TogArrayDuration.uniq();
	for(u=0,uLen=TogArrayDurationUniq.length; u<uLen; u++){
		for(v=0,vLen=togPriceSortedArray.length; v<vLen; v++){
			if(togPriceSortedArray[v][DealFinder.ajaxObj][DealFinder.ajaxObjPropStr6] === TogArrayDurationUniq[u]){
			DealFinder.writeTog(togPriceSortedArray[v]);
			}
		}
	}
},

writeTogArrayPriceSorted: function(togPriceSorted,TogArray,searchoptions){
DealFinder.togPriceSortedArray[DealFinder.countForArray2] = togPriceSorted;
DealFinder.countForArray2++;	
	if(DealFinder.countForArray2 === TogArray.length){
	$(DealFinder.outputOfferHTMLULContainer).innerHTML = "";
		if(searchoptions === DealFinder.optionValueForPriceLowSorting || searchoptions === null){
			for(l=0,lLen=DealFinder.togPriceSortedArray.length; l<lLen; l++){
			DealFinder.writeTog(DealFinder.togPriceSortedArray[l]);
			}
		}
		if(searchoptions === DealFinder.optionValueForPriceHighSorting){
		DealFinder.togPriceSortedArray.reverse([inline = true]);	
			for(l=0,lLen=DealFinder.togPriceSortedArray.length; l<lLen; l++){
			DealFinder.writeTog(DealFinder.togPriceSortedArray[l]);
			}
		}
		if(searchoptions === DealFinder.optionValueForDurationShortSorting || searchoptions === DealFinder.optionValueForDurationLongSorting){
			DealFinder.sortByDuration(searchoptions, DealFinder.togPriceSortedArray);
		}
	DealFinder.countForArray2 = 0;
	DealFinder.togPriceSortedArray.clear();
	}
},

writeTog: function(elTogo){
var to = elTogo[DealFinder.ajaxObj];
var li = document.createElement('li');
li.style.cursor = "pointer";
var dfResultsTemplate = new Template(DealFinder.outputOfferHTML);
li.innerHTML = eval("dfResultsTemplate.evaluate(" + DealFinder.valueMapping + ")");
var myUl = $(DealFinder.outputOfferHTMLULContainer);
myUl.appendChild(li);
return;
},

showSorryOrCounter: function(){		
	if (DealFinder.totalToDisplay.length < 1){//If there are no results display sorry
	var htmlUl = $(DealFinder.outputOfferHTMLULContainer)
		if(!htmlUl)return false;
		htmlUl.innerHTML = "";	
		if(!$(DealFinder.counterContainer))return false;
		var dfErrorTemplate = new Template(DealFinder.errorHTML);
		$(DealFinder.counterContainer).innerHTML = eval("dfErrorTemplate.evaluate(" + DealFinder.valueMapping2 + ")");		
	}
	else{
		var dfCounterTemplate = new Template(DealFinder.counterHTML);
		$(DealFinder.counterContainer).innerHTML = eval("dfCounterTemplate.evaluate(" + DealFinder.valueMapping2 + ")");
	}
},

captureDFEvents: function(element1){
	var elem1 = $(element1);
	if(!elem1)return false;
	elem1.onclick = function(){
	$(DealFinder.counterContainer).innerHTML = DealFinder.counterContainerHTML;
	$(DealFinder.outputOfferHTMLULContainer).innerHTML = "&nbsp;";
	EOGnumbersLength = 0;
	DealFinder.countForArray = 0;
	DealFinder.totalToDisplay.clear();
	DealFinder.countForArray2 = 0;
	DealFinder.togPriceSortedArray.clear();		
	DealFinder.submitDealFinder();
	return false;
	}
}
	
};