
/* start of homecontent*/

var show = true, filter= false, packageResultsData = null;
var cpath;	
var morebtnpath;
var imgpath;
/* some of the filtering related global variables*/
var filter_themeList = new Array();
var filterArray=[];
var filter_priceList = new Array();
var filter_durationList = new Array();
var minFilterPrice = 0;
var maxFilterPrice = 0;
var minFilterDuration = 0;
var maxFilterDuration = 0;

/*some of the filtering related global variables*/

function setGlobalValues(g_cpath,g_morebtnpath,g_imgpath){
	cpath=g_cpath;	
	morebtnpath=g_morebtnpath;
	imgpath=g_imgpath;
}

function packhtml(loc, duration, id, price, image){
	var location=loc+"";
	var place= location.toUpperCase();
	var phtml=''+
	'<table height="130" width="200" border="0" cellpadding="0" cellspacing="0"'+ 
	'style="table-layout: fixed;background-color: #FFFFFF;" id=pk'+id+'>'+
	'<tr>'+
		'<td align="center" valign="middle">'+
		'<table height="126" width="196" border="0" cellpadding="0" cellspacing="0"'+ 
			'style="table-layout: fixed;background-color: #FFFFFF;">'+
			'<tr height="14">'+
				'<td width="150px" colspan="" align="left"><b><font color="#5190ED" size="1">'+place+'</font></b></td><td align="right"><font  size="1">'+duration+' Days</font></td>'+
			'</tr>'+
			'<tr>'+
				'<td colspan="2" valign="middle" align="center">'+
					'<img src="'+image+'" height="90" width="196">'+
				'</td>'+
			'</tr>'+
			'<tr height="14">'+
				'<td align="left"><b><font size="1">INR &nbsp;'+price+'&nbsp;</font></b></td>'+
				//'<td><span class="commissionSpan"></span></td>'+
				'<td align="right"><img class="moreBtn" name=pk'+id+' id="'+id+'" src="'+morebtnpath+'" style="cursor: pointer;"></td>'+
			'</tr>'+
		'</table>'+
		'</td>'+
		'</tr>'+
	'</table>'+
		'';
	
	//fillCommissionSpan();
	
	return phtml;

}


function fillCommissionSpan(fareQoute){
	var commissionHtml='<label onmouseover="Tip('+"'<table cellpadding=0 cellspacing=0 border=0 style=width: 160px; table-layout: fixed;>"+
				"<tr height=20><td width=120>Base Fare</td><td width=5>:</td><td width=35 align=right>"+fareQoute.baseFareAmount+"</td><td width=5>+</td></tr>"+
				"<tr height=20><td>Your Markup</td><td>:</td><td align=right>"+fareQoute.adminMarkup+"</td><td>+</td></tr>"+
				"<tr height=20><td>Your Commission</td><td>:</td><td align=right>"+fareQoute.commission+"</td><td>+</td></tr>"+
				"<tr><td colspan=4><hr></td></tr>"+
				"<tr height=20><td>Net Payable</td><td>:</td><td align=right>"+fareQoute.netAmt+"</td><td>&nbsp;</td></tr>"+
				"</table>'"+', BGCOLOR, '+"'#d8eaf9'"+', HEIGHT, 100,'+
			    ' WIDTH, 160, TITLE,'+"'Net Fare'"+ ', CLICKSTICKY, true, CLOSEBTNCOLORS,'+
			    ' ['+"'#ce6666'"+', '+"'#000000'"+', '+"'#ffffff'"+', '+"'#d8eaf9'"+'], CLICKCLOSE,'+
			    ' true,CLOSEBTN, true, PADDING, 3, JUMPVERT, false, FONTFACE, '+"'Arial,Helvetica, sans-serif'"+','+
			    ' FONTSIZE, '+"'10pt'"+')" onmouseout="UnTip()" valign="top" style="cursor:pointer;"><u><img  height=12 width=12 src="'+earnImage+'"/>INR&nbsp;'+fareQoute.netAmt+'</u></label>';
	return commissionHtml;
}


function prepareFilterData(packageDetails){
		
	var themesOfPackage = packageDetails.themes;
	var pricesOfPackage = packageDetails.prices;
	var duration = parseInt(packageDetails.days, 10);

	if(themesOfPackage!= null && themesOfPackage.length>0){
		for(var t =0; t<themesOfPackage.length;t++){
			if(jQuery.inArray(themesOfPackage[t],filter_themeList)<0){
				filter_themeList.push(themesOfPackage[t]);
			}
		}
	}
	
	if(pricesOfPackage!=null && pricesOfPackage.length>0){
		for(var p =0; p<pricesOfPackage.length;p++){
			if(jQuery.inArray(parseInt(pricesOfPackage[p].doPrice),filter_priceList)<0){
				filter_priceList.push(pricesOfPackage[p].doPrice);
			}
		}
	}
	
	if(duration>0){
		if(jQuery.inArray(duration,filter_durationList)<0){
			filter_durationList.push(duration);
		}
	}
		
}


function fillFilterTD(){
	
	if(filter_themeList!= null &&filter_themeList.length >0){
		var status=0;
		var exists=0;
		for(i=0;i<filter_themeList.length;++i)
		{
			exists=0;
			for(k=0;k<filterArray.length;++k)
			{
				if(filterArray[k].tag==filter_themeList[i].tag)
					exists=1;
			}
			if(exists==0)
				filterArray.push(filter_themeList[i]);
			
		}
		var trClosed = true; 
		var themeHtml = '<table width="359" height="100" bgcolor="#FFFFFF" cellpadding="0" cellspacing="0" style="table-layout: fixed;">'+
								'<tr>'+
									'<td colspan="3" align="left">'+
										'<font style="font-family: sans-serif; font-weight: bold; font-size: 12px;">'+
											'&nbsp;Filter By Theme'+
										'</font>'+
									'</td>'+								
								'</tr><tr>';
		var tdhtml ='';
		for(var t =0; t<filterArray.length;t++){
			
			if(t!=0 && parseInt(t,10)%3==0){
				tdhtml+='</tr><tr>' ;
				
			}
			trClosed =false;
			tdhtml+='<td width="126" align="left">'+
					'<input class="themeFilterCB" type="checkbox" checked="true" id='+filterArray[t].themeId+' style="top:1px; left:1px; position: relative; z-index:0;"/>'+
						'<font style="font-family: sans-serif; font-size: 11px; color: #544E4F;">'+
						filterArray[t].tag
						'</font>'+	
				'</td>';
		}
		if(!trClosed){
			themeHtml+='</tr>';
		}
		
		themeHtml+=tdhtml;
		themeHtml+='</table>';
	}
	
	$("#byTheme").html(themeHtml);
	
	var fps=filter_priceList.sort(function(a,b)	{return a - b;});
	minFilterPrice = parseInt(fps[0],10);
	maxFilterPrice = parseInt((fps[filter_priceList.length-1]),10);
	$("#min_price_filter_td").html("&nbsp;Rs "+minFilterPrice);
	$("#max_price_filter_td").html("Rs "+maxFilterPrice+"&nbsp;");
	$("#filter_priceSlider").slider({
		range: true,
		min: minFilterPrice,
		max: maxFilterPrice,
		values: [minFilterPrice,maxFilterPrice],
		animate: false,
		slide: function( event, ui ) {
			$("#min_price_filter_td").html("Rs "+ ui.values[0]); 
	   		$("#max_price_filter_td").html("Rs "+ ui.values[1]);
	   		minFilterPrice = parseInt(ui.values[0],10);
	   		maxFilterPrice = parseInt(ui.values[1],10);
			//console.log("minFilterPrice:"+minFilterPrice+"\n maxFilterPrice:"+maxFilterPrice);
	   		proceedToFilter();
		}
	});
	
	var fds=filter_durationList.sort(function(a,b)	{return a - b;});
	minFilterDuration = parseInt(fds[0],10); 
	maxFilterDuration = parseInt((fds[filter_durationList.length-1]),10);
	$("#min_duration_td").html(minFilterDuration+" day(s)");
	$("#max_duration_td").html(maxFilterDuration+" day(s)&nbsp;");
	$( "#duration_slider" ).slider({
		range: true,
		min: minFilterDuration,
		max: maxFilterDuration,
		values: [minFilterDuration,maxFilterDuration],
		slide: function( event, ui ) {
			$("#min_duration_td").html(ui.values[ 0 ]+" day(s)"); 
			$("#max_duration_td").html(ui.values[ 1 ]+" day(s)&nbsp;");
			minFilterDuration = parseInt(ui.values[0],10);
			maxFilterDuration = parseInt(ui.values[1],10);
			//console.log("minFilterDuration:"+minFilterDuration+"\n maxFilterDuration:"+maxFilterDuration);
			proceedToFilter();
		}
	});
	
	
	
}


function proceedToFilter(){

	filterThemeIds = new Array();
	var x = $(".themeFilterCB:checked");
	var id;
	$.each($(".themeFilterCB:checked"), function() {
		for(i=0;i<filterArray.length;++i)
			if(filterArray[i].themeId==$(this).attr("id"))
					filterThemeIds.push(filterArray[i].tag);
		
			
	});
	//console.log("="+minFilterPrice+"="+maxFilterPrice+"="+minFilterDuration+"="+maxFilterDuration+"="+filterThemeIds);
	doFiltering(minFilterPrice,maxFilterPrice,minFilterDuration,maxFilterDuration,filterThemeIds);
	
}




var minP=0,maxP=0,minD=0,maxD=0,selectedThemes=null;
function doFiltering(minFilterPrice,maxFilterPrice,minFilterDuration,maxFilterDuration,filterThemeIds){
	//console.log("minfilterPrice:\n"+ minFilterPrice +"maxfilterPrice:\n"+ maxFilterPrice +"mindur:\n"+ minFilterDuration +"maxdur:\n"+ maxFilterDuration +"themeids:\n"+ filterThemeIds);
	minP = parseInt(minFilterPrice,10);
	maxP = parseInt(maxFilterPrice,10);
	minD = parseInt(minFilterDuration,10);
	maxD = parseInt(maxFilterDuration,10);
	selectedThemes =  filterThemeIds;
	
	filter = true;
	showPackResults(packageResultsData, filter);

}		

function filterContains(data){
	var priceSt=true, durSt=true, themeSt=true;
	var price = 0, duration = 0;
	var theme = null;
	data.prices.sort(comparePriority);
	if(data.prices[0]!= null){
		var x = data.prices[0].doPrice;
		price = parseInt(x,10);
	}
	
	var y = data.days;
	duration = parseInt(y,10);
	if(data.themes!= null){
		theme = new Array();
		for(var i=0; i < data.themes.length;i++){
			theme.push((""+data.themes[i].tag));
		}
	}
	
	if(price < minP || price > maxP){
		priceSt=false;
	}
	if(duration < minD || duration > maxD){
		durSt=false;
	}
	
	//console.log("price:  "+price+"<"+ minP+"||"+ price+ ">"+ maxP+"-----"+priceSt);
	//console.log("duration: "+duration +"<"+ minD+"||"+ duration+">"+maxD+"---"+durSt);
	//console.log("selectedThemes:"+selectedThemes+"\n incoming pack themes"+theme);
	$.each(theme, function(k,v){
		if(jQuery.inArray((""+v),selectedThemes)<0){
			themeSt = false;
		}
		//console.log(themeSt);	
	});
	
	var filterStatus=priceSt && durSt && themeSt;
	//console.log(priceSt+" && "+durSt+" && "+themeSt+" -->"+filterStatus);
	priceSt=true; durSt=true; themeSt=true;
	return (filterStatus);
}


function showPackResults(packList, filter){
	$('#dealResult').html("");
	var templatehtml=$("#searchResult_template_1").html();
	var replacetemplate=templatehtml+"";
	var deals = packList;
	var offerCheckList=packList;
	var n=0;
	var htmlString="";
	var count=0;
	var x1="",x2="",x3="";
	for(n=0;n<deals.length;n++){
		//console.log("n---"+n);
		var data="";
		var fareQuote="";		
		data=deals[n];
		fareQuote=deals[n].fareQuoteList[0];
		if(!filter){
			prepareFilterData(data);
		}

		if(filter){
			var filterState=filterContains(data);
			//console.log("Pk id: "+data.packageId+"   filterState:  "+filterState);
			if(filterState!=true){
				//console.log("continued...Pk id: "+data.packageId+"   filterState:  "+filterState);
				continue;
			}
		}
		var name="",duration="",xid="",price="",img="", description="",basePrice="",chat="",callUs="",callLink="";
		var adults=1, childs=0, infants=0, seniors=0;
		var priceId="";
		//console.log(data);
		var id="#";
		//place=data.country;
		name=data.name; 
		duration=data.days;
		xid=data.packageId;
		description=data.teaser;
		data.prices.sort(comparePriority);
		
		price=data.prices[0].doPrice;
		basePrice=data.prices[0].basePrice;		
		priceId=data.prices[0].priceId;
		
		var hotLines=data.hotLines;
		if(hotLines!=null)
		{
			if(hotLines.length==1)
			{
				callUs+=hotLines[0].callUs_Nos;
				
				if(hotLines[0].callUrl!=null)
					callLink+=hotLines[0].callUrl;
				else
					callLink+='#';
				
				if(hotLines[0].chatUrl!=null)
					chat+=hotLines[0].chatUrl;
				else
					chat+='#';
			}
			else
			{
				var status=0;
				for(i=1;i<hotLines.length;++i)
				{			
					callUs+=hotLines[i].callUs_Nos;
					if(i!=(hotLines.length-1))
						callUs+=',';			
					if(status==0 && hotLines[i].hotLine_Id!=0)
					{
						if(hotLines[i].callUrl!=null)
							callLink+=hotLines[i].callUrl;
						else
							callLink+='#';
					
						if(hotLines[i].chatUrl!=null)
							chat+=hotLines[1].chatUrl;
						else
							chat+='#';
						status=1;
					}
				}
			}
		}
		
		img+=imgpath+"thumbnail/"+data.thumbNail;
		
		adults=$("#searchForm_numAdults").val();
		childs=$("#searchForm_numChild").val();
		infants=$("#searchForm_numInfants").val();
		seniors=$("#searchForm_numSeniors").val();
		
		//alert(replacetemplate);
		var chtml=getSingleDataResult(replacetemplate, name, duration, xid, priceId, description, basePrice,price, img, adults, childs, infants, seniors,callUs,chat,callLink,fareQuote);
		htmlString+=chtml;
	}
	if(n==deals.length){
		$('#dealResult').html(htmlString);		
		for(i in offerCheckList)
		{
			offerCheckList[i].prices.sort(comparePriority);
			//alert(offerCheckList.prices[0].doPrice);
			var price=offerCheckList[i].prices[0].doPrice;
			var basePrice=offerCheckList[i].prices[0].basePrice;			
			//alert(offerCheckList.length);
			if(price<basePrice)
			{	
				$('#package_baseprice_'+offerCheckList[i].packageId).show();
				$('#package_Offer_percent_'+offerCheckList[i].packageId).show();
				$('#package_doprice_font_'+offerCheckList[i].packageId).attr({color:'red'});				
			}
			else
			{
				$('#package_baseprice_'+offerCheckList[i].packageId).hide();
				$('#package_Offer_percent_'+offerCheckList[i].packageId).hide();
				$('#package_doprice_font_'+offerCheckList[i].packageId).attr({color:''});				
			}
		}
		
	}
	
}


function getSingleDataResult(replacetemplate, name, duration, xid, priceId, description, basePrice, price, img, adults, childs, infants, seniors,callUs,chat,callLink,fareQuote){
	
	var offerPercent=parseInt(Math.round(100-price/basePrice*100))+'%';
	
	var template_package_image_content=""+img;
	var template_package_name_content=""+name;
	var template_package_duration_content=""+duration;
	
	var template_package_description_content=""+description;
	var template_package_price_content=""+price;
	var template_package_days_content=""+duration;
	var template_package_adults_content=""+adults;
	var template_package_childs_content=""+childs;
	var template_package_infants_content=""+infants;
	var template_package_seniors_content=""+seniors;
	var template_package_baseprice_content=""+basePrice;
	var template_package_chat_content=""+chat;
	var template_package_callUs_content=""+callUs;
	var template_package_callUs_link_content=""+callLink;
	var template_offerPercent_Content=""+offerPercent;
	
	var CommissionHtml=fillCommissionSpan(fareQuote);
	var template_package_image_id="package_image_"+xid;
	var template_package_description_id="package_description_"+xid;
	var template_package_price_id="package_price_"+xid;
	var template_package_baseprice_id="package_baseprice_"+xid;
	
	var template_package_days_id="package_days_"+xid;
	var template_package_adults_id="package_adults_"+xid;
	var template_package_childs_id="package_childs_"+xid;
	var template_package_infants_id="package_infants_"+xid;
	var template_package_seniors_id="package_seniors_"+xid;
	var template_package_morebtn_id=""+xid;
	var package_baseprice_font_id="package_baseprice_font_"+xid;
	var package_doprice_font_id="package_doprice_font_"+xid;
	var template_package_chat_id="package_chat_"+xid;
	var template_package_callUs_id="package_callUs_"+xid;
	var template_package_callUs_link_id="package_callUs_link_"+xid;
	var template_package_offerPercent_id="package_Offer_percent_"+xid;

	var template_package_sendrequest_id="template_package_sendrequest_"+xid;
	var template_package_bookonline_id="template_package_bookonline_"+xid;
	var template_package_emailfriend_id="template_package_emailfriend_"+xid;
	var template_package_print_id="template_package_print_"+xid;	
	var singlePackId="srchResultID_"+xid;
	var commission_markup_id="commission_markup_id_"+xid;
	
	var replacedHtml=replacetemplate;

	replacedHtml=replacedHtml.replace("template_package_image_content", template_package_image_content);
	replacedHtml=replacedHtml.replace("template_package_name_content",template_package_name_content );
	replacedHtml=replacedHtml.replace("template_package_duration_content", template_package_duration_content);
	replacedHtml=replacedHtml.replace("template_package_morebtn_id", template_package_morebtn_id);
	replacedHtml=replacedHtml.replace("template_package_description_content", template_package_description_content);
	replacedHtml=replacedHtml.replace("template_package_price_content", template_package_price_content);
	//replacedHtml=replacedHtml.replace("template_package_days_content", template_package_days_content);
	replacedHtml=replacedHtml.replace("template_package_adults_content", template_package_adults_content);
	replacedHtml=replacedHtml.replace("template_package_childs_content", template_package_childs_content);
	replacedHtml=replacedHtml.replace("template_package_infants_content", template_package_infants_content);
	replacedHtml=replacedHtml.replace("template_package_seniors_content", template_package_seniors_content);
	replacedHtml=replacedHtml.replace("template_package_image_id", template_package_image_id);
	replacedHtml=replacedHtml.replace("template_package_description_id", template_package_description_id);
	replacedHtml=replacedHtml.replace("template_package_price_id", template_package_price_id);
	//replacedHtml=replacedHtml.replace("template_package_days_id", template_package_days_id);
	replacedHtml=replacedHtml.replace("template_package_adults_id", template_package_adults_id);
	replacedHtml=replacedHtml.replace("template_package_childs_id", template_package_childs_id);
	replacedHtml=replacedHtml.replace("template_package_infants_id", template_package_infants_id);
	replacedHtml=replacedHtml.replace("template_package_seniors_id", template_package_seniors_id);
	replacedHtml=replacedHtml.replace("template_package_sendrequest_id", template_package_sendrequest_id);
	replacedHtml=replacedHtml.replace("template_package_bookonline_id", template_package_bookonline_id);
	replacedHtml=replacedHtml.replace("template_package_emailfriend_id", template_package_emailfriend_id);
	replacedHtml=replacedHtml.replace("template_package_print_id", template_package_print_id);
	replacedHtml=replacedHtml.replace("package_baseprice_font_id", package_baseprice_font_id);
	replacedHtml=replacedHtml.replace("package_doprice_font_id", package_doprice_font_id);
	
	
	replacedHtml=replacedHtml.replace("commission_markup_id",commission_markup_id);
	replacedHtml=replacedHtml.replace("template_package_baseprice_id",template_package_baseprice_id);
	replacedHtml=replacedHtml.replace("template_package_baseprice_content",template_package_baseprice_content);
	replacedHtml=replacedHtml.replace("template_package_chat_id",template_package_chat_id);
	replacedHtml=replacedHtml.replace("template_package_chat_content",template_package_chat_content);
	replacedHtml=replacedHtml.replace("template_package_callUs_id",template_package_callUs_id);
	replacedHtml=replacedHtml.replace("template_package_callUs_content",template_package_callUs_content);
	replacedHtml=replacedHtml.replace("template_package_callUs_link_id",template_package_callUs_link_id);
	replacedHtml=replacedHtml.replace("template_package_callUs_link_content",template_package_callUs_link_content);
	replacedHtml=replacedHtml.replace("template_package_offerPercent_id",template_package_offerPercent_id);	
	replacedHtml=replacedHtml.replace("template_offerPercent_Content",template_offerPercent_Content);
	
	
	
	
	
	
	if(price<fareQuote.netAmt)
		replacedHtml=replacedHtml.replace("commission_markup_content",CommissionHtml);
	else 
		replacedHtml=replacedHtml.replace("commission_markup_content","");
	
	replacedHtml=replacedHtml.replace('srchResultID_',singlePackId);	

	if(replacedHtml!=null){
		return replacedHtml;
	}
	else{
		return "";
	}

	
	
}





function comparePriority(price1, price2) {
	return price1.doPrice - price2.doPrice;
	}

function showPackResultsTempblock(packList, filter){
	$('#dealResult').html("");
	var deals = packList;
	var n=0;
	var htmlString="";
	var count=0;
	var x1="",x2="",x3="";
	for(n=0;n<deals.length;n++){
		//console.log("n---"+n);
		var data="";
		data=deals[n];
		if(filter){
			var filterState=filterContains(data);
			//console.log("Pk id: "+data.packageId+"   filterState:  "+filterState);
			if(filterState!=true){
				//console.log("continued...Pk id: "+data.packageId+"   filterState:  "+filterState);
				continue;
			}
		}
		count++;
		if(count==1){
			x1=giveSinglePackHtml(data, filter);
		}else if(count==2){
			x2=giveSinglePackHtml(data, filter);
		}else if(count==3){
			x3=giveSinglePackHtml(data, filter);
			htmlString+=getRow(x1,x2,x3);
			count=0;
		}
	}
	
	if(n==(deals.length) && count<3){
		while(count<3){
			count++;
			if(count==1){
				x1="";
			}else if(count==2){
				x2="";
			}else if(count==3){
				x3="";
				htmlString+=getRow(x1,x2,x3);
			}
		}
	}
	if(count==3){
		//console.log("n---"+n);
		$('#dealResult').html(htmlString);	
	}
	
}


function giveSinglePackHtml(data, filter){
	if(!filter){
		prepareFilterData(data);
	}
	//alert('givecHtml');
	var place="",duration="",xid="",price="",img="";
	//console.log(data);
	var id="#";
	//place=data.country;
	place=data.name;
	duration=data.days;
	xid=data.packageId;
	data.prices.sort(comparePriority);
	price=data.prices[0].doPrice;
	img+=imgpath+"thumbnail/"+data.thumbNail;
	var chtml=packhtml(place, duration, xid, price, img);
	return chtml;
}

function getRow(x1,x2,x3){
	var c1=x1;
	var c2=x2;
	var c3=x3;
	
	var holiday_html = '<tr height="140">'+
	'<td align="center" bgcolor="#FFFFFF" width="680">'+
		'<table width="678" height="40" align="center"'+
				'cellpadding="0" cellspacing="0" border="0"'+
				 'bgcolor="#FFFFFF" style="table-layout: fixed;">'+
			'<tr height="110">'+
				'<td align="left" width="200">'+	c1 +'</td>'+
				'<td align="left" width="39" bgcolor="white">'+
				'&nbsp;</td>'+
				'<td align="center" width="200">'+	c2 +'</td>'+
				'<td align="left" width="39" bgcolor="white">&nbsp;</td>'+
				'<td align="right" width="200">'+	c3 +'</td>'+
			'</tr>'+
		'</table>'+
	'</td>'+
	'</tr>';
	
	return holiday_html;
}


/*------------------------------------ end of homecontent -----------------------------*/
function test(){
	
	alert("holiday.js");
}

/* start of holidaySearchForm*/



	

	
	

