﻿
function oOfferGridItem(sDestination,sDestinationUrl,sRegion,sRegionUrl,sMonth,sPrice,sUri){this.destination=sDestination;this.destinationUrl=sDestinationUrl;this.region=sRegion;this.regionUrl=sRegionUrl;this.month=sMonth;this.price=sPrice;this.Uri=sUri}
function getOfferGridData(sUrl,sKey,oOffersGrid,sProductTypeUrl){var sTargetDiv="offers_grid_"+sKey;$("#"+sTargetDiv).hide();$.getJSON(sUrl,function(data){setOfferGridData(data,sKey,oOffersGrid,sProductTypeUrl);});$("#"+sTargetDiv).show();}
function setOfferGridData(oData,sKey,oOffersGrid,sProductTypeUrl){var sOfferRow="";var sMonthsRow="";var aOffers=new Array();var dCounter;var sPrice="";var sTargetTable=sKey+"_deals";var j=0;$.each(oData,function(i,offers){sMonthsRow="<th id=\"th_region\"><span class=\"hide\">Region</span></th>";if(offers.length>0){$.each(offers,function(i,item){if(!aOffers[item.Region.Name])aOffers[item.Region.Name]=new Array();aOffers[item.Region.Name][aOffers[item.Region.Name].length]=new oOfferGridItem(item.Destination.Name,item.Destination.NameUrl,item.Region.Name,item.Region.NameUrl,item.OfferMonth,item.Price,item.Uri);});dCounter=new Date(oOffersGrid.dateStart.getFullYear(),oOffersGrid.dateStart.getMonth(),1);while(dCounter<=oOffersGrid.dateEnd){sMonthsRow+="<th class=\"month_column\">"+$.datepicker.formatDate('M',dCounter)+"</th>";dCounter.setMonth(dCounter.getMonth()+1);}
for(var key in aOffers){if(j<oOffersGrid.maxNumberOffers){sOfferRow+="<tr>";sOfferRow+="<td headers=\"th_region\"><a href=\"/offers/"+sProductTypeUrl+"/"+aOffers[key][0].destinationUrl+"/"+aOffers[key][0].regionUrl+"\">"+key+"</a></td>";dCounter=new Date(oOffersGrid.dateStart.getFullYear(),oOffersGrid.dateStart.getMonth(),1);while(dCounter<=oOffersGrid.dateEnd){sPrice="-";for(i=0;i<aOffers[key].length;i++){if(aOffers[key][i].month==(dCounter.getMonth()+1)){sPrice="<a href=\""+aOffers[key][0].Uri+"\">&pound;"+aOffers[key][i].price+"</a>";break;}}
sOfferRow+="<td>"+sPrice+"</td>";dCounter.setMonth(dCounter.getMonth()+1);}
sOfferRow+="</tr>";j++;}}
$("#"+sTargetTable+" + p").show();setOfferGridMonthNavigation(sKey,oOffersGrid);}});if(j==0){sOfferRow="<tr><td>"+oOffersGrid.errorNoOffersFound+"</td></tr>";$("#offers_grid_"+sKey+"_prev").hide();$("#offers_grid_"+sKey+"_next").hide();}
$("#"+sTargetTable+" > thead > tr:first").empty();$("#"+sTargetTable+" > tbody").empty();$("#"+sTargetTable+" > thead tr:first").append(sMonthsRow);$("#"+sTargetTable+" > tbody").append(sOfferRow);}
function setOfferGridUrl(iMonths,sProductType,oOffersGrid,sSubproductType){oOffersGrid.dateStart.setMonth(oOffersGrid.dateStart.getMonth()+iMonths);oOffersGrid.dateEnd.setMonth(oOffersGrid.dateEnd.getMonth()+iMonths);var sUrl="/offers/json/"+(oOffersGrid.dateStart.getMonth()+1)+"/"+oOffersGrid.dateStart.getFullYear()+"/"+(oOffersGrid.dateEnd.getMonth()+1)+"/"+oOffersGrid.dateEnd.getFullYear()+"/100";if(oOffersGrid.destinationNameUrl!=""){sUrl+="/1/"+oOffersGrid.destinationNameUrl+"/";if(oOffersGrid.regionNameUrl!="")sUrl+=oOffersGrid.regionNameUrl+"/";sUrl+=sProductType;}else{sUrl+="/"+sProductType;if(sSubproductType!="")
sUrl+="/"+sSubproductType;}
return sUrl;}
function setOfferGridMonthNavigation(sKey,oOffersGrid){var dToday=new Date();var dOneYearAhead=new Date();dToday.setMonth(dToday.getMonth()+1);dOneYearAhead.setMonth(11);oOffersGrid.dateStart<dToday?$("#offers_grid_"+sKey+"_prev").hide():$("#offers_grid_"+sKey+"_prev").show();oOffersGrid.dateEnd>dOneYearAhead?$("#offers_grid_"+sKey+"_next").hide():$("#offers_grid_"+sKey+"_next").show();}
