/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
weekly_specs.js
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
References in other files:
(1) garages.js fetchGaragesAroundBounds (parseXML_WeeklySpecs)
(2) garages.js updateGarages // hide C & S windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
// v.12x sort by distance implemented
// images fix
// v.21m  versions (get variable)

/// create mini window
document.write('<table id="week_sp_min" class="exp_win_mini do_not_print_imp" cellspacing="0" ' +
' onClick="Expand(this);HideGreenDetails();" style="display:none;" title="Click to open">' +
'<tr><td class="top" width="100%">Weekly Specials</td></tr>' +
'<tr><td  id="week_sp_frame_min" class="bottom"></td></tr>' +
'</table>');


// create expanded window

CreateStandardPopup("week_sp", "CloseWeekWin();return true;", 330); /* defined in garagesX.js */
document.getElementById("week_sp").style.zIndex = 3004;
document.getElementById("week_sp_head").innerHTML  = '<b>Weekly Parking Specials</b>';
document.getElementById("week_sp_cont").innerHTML = 
'<div class="bl_pop_yel"><div class="br_pop_yel"><div class="tl_pop_yel"><div class="tr_pop_yel">' +
'<div id="week_sp_frame"></div>' +
'</div></div></div></div>';


var arrAllWeeklySpecs = new Array(); // all 

///// WeekSpec object
function WeekSpec()
{
this.id = null;
this.rate = null;
this.desc = "";
this.desc2 = "";
this.garages = ""; // related garages IDs
}

////     parseXML_WeeklySpecs
function parseXML_WeeklySpecs(xmlDoc)
{
//alert("parseXML_WeeklySpecs");

arrAllWeeklySpecs = Array();

var xmlWeekSpecsElem = xmlDoc.documentElement.getElementsByTagName("WeeklySpecials")[0];

if(!xmlWeekSpecsElem)return;

var xmlWeekSpecs = xmlWeekSpecsElem.getElementsByTagName("WeekSpec");

     //// go through Weekly specs 
     for(s=0; s < xmlWeekSpecs.length; s++)
     {
      var xmlWeekSpec = xmlWeekSpecs[s];
      
      var spec_id = xmlWeekSpec.getAttribute("id");
      var rate = xmlWeekSpec.getAttribute("rate");
      var desc = xmlWeekSpec.getAttribute("descr");
      var desc2 = xmlWeekSpec.getAttribute("descr2");
      var str_gars = xmlWeekSpec.getAttribute("garages");
      
      
      var oWSpec = new WeekSpec();
      oWSpec.id = spec_id;
      oWSpec.rate = rate;
      oWSpec.desc = desc;
      oWSpec.desc2 = desc2;
      oWSpec.garages = str_gars;
      
      arrAllWeeklySpecs[s] = oWSpec;
            
     //alert(arrAllWeeklySpecs[spec_id].garages.toString());
     }

}



////     DisplayWeekExpWins
function DisplayWeekExpWins()
{
/// CURRENTLY ALL specials should be displayed   
if(parking_isMonthly /*|| arrDisplayedGreenGarages.length == 0*/) return;


PositionExpWin("week_sp");

if(!PopulateWeekExpWin())
DisplayElem("week_sp_min", "none");
else
{
// display maxi for searched period more than 4 days
if(!parking_isMonthly && parking_isCase)
if((getGMTTime(parking_departure) - getGMTTime(parking_arrival))>=96*3600*1000)
{
//DisplayElem("week_sp", "block");
var week_id = -1;

for(var i =0; i < arrExpWins.length; i++)
{if(arrExpWins[i] == 'week_sp')week_id = i;}

currMaximizedExpWin = week_id;
}
}


}


////     PopulateWeekExpWin
function PopulateWeekExpWin()
{
   var selected_wspecs = " ";
    
// go through all garages in arrDisplayedGreenGarages
/// CURRENTLY ALL specials should be displayed
/*  for(var lg=0; lg < arrDisplayedGreenGarages.length; lg++)
  { */
  
   for(var s=0; s < arrAllWeeklySpecs.length; s++)
   {
   if(typeof(arrAllWeeklySpecs[s]) == 'undefined')continue;
   /* 
   var str_gars = " " + arrAllWeeklySpecs[s].garages + " ";
   var str_g = arrDisplayedGreenGarages[lg];
   */
   //alert("str_gars " + str_gars);
   
/// CURRENTLY ALL specials should be displayed   
    selected_wspecs += s + " " ; 
    
    /* 
   if(str_gars.indexOf(" " + str_g + " ") > -1)
   {
   if(selected_wspecs.indexOf(" " + s + " ") == -1)
   selected_wspecs += s + " " ; 
   }
   */
   }
   
 /* } */
  
  
  selected_wspecs  = trim(selected_wspecs);
 
  // no Week Specs for this area , so return
  if(selected_wspecs == "" ) return false
  
  var arr_selected = selected_wspecs.split(" ");
  
  var min_rate = 999999;
 
  var strContents = "";
  
  // go through selected Week Specs
  for(var i = 0; i < arr_selected.length; i++)
  {
  loc_id = arr_selected[i];
  loc_week_sp = arrAllWeeklySpecs[loc_id];
  
  min_rate = Math.min(min_rate, loc_week_sp.rate);
 
 var loc_link = 'href="javascript:;" onClick="popExpWinDetails('
      + trim(loc_week_sp.garages) + ', ' + loc_id + ')"';
            
 strContents += '<tr><td class="rte">$' + loc_week_sp.rate 
            + '</td><td><a ' + loc_link + '>' +
            '<span class="descr">' + loc_week_sp.desc + '</span><br>' + loc_week_sp.desc2 + '</a><br></td></tr>'; 
  
 //alert(strContents);
  
  }

    var contents_min = 'From $' + min_rate;
    if(CITY_TAX)
    contents_min += '&nbsp;(tax incl.)';

GetElement("week_sp_frame_min").innerHTML = contents_min;

GetElement("week_sp_frame").innerHTML = 
'<center><table cellspacing="0">' + strContents + '</table></center>';  
      /*
      strContents = strContents.replace(/</g, "&lt;");
      strContents = strContents.replace(/>/g, "&gt;");
      strContents = strContents.replace(/<br>/g, "\n");
      document.write(strContents);
      */
GetElement("week_sp").style.height = "";
  
return arr_selected.length;
}

function OpenWeekly(gar_id, wspec_id)
{

var URL = XML_GET_WEEKLY_URL + "?v=" + VERSION + "&id=" + gar_id + "&ws=" + arrAllWeeklySpecs[wspec_id].id;

//alert(URL);
oPopWin = window.open(URL,"","width=480,height=430,resizable=1 ,toolbar=0, location=0, status=0, menubar=0, directories=0, scrollbars=0");


}


////      CloseWeekWin
function  CloseWeekWin()
{
DisplayElem("week_sp", "none");
DisplayElem("expwin_detail_label", "none");
DisplayElem("expwin_map_pop", "hidden");

}

/*
********************* ********************* *********************
                    pop Detail window
********************* ********************* *********************
*/


// create popup for pop Detail window

CreateStandardPopup("expwin_detail_label", "HideExpWinDetailsDirect();", 432); /* defined in garagesX.js */
document.getElementById("expwin_detail_label").style.zIndex = 3000;
document.getElementById("expwin_detail_label_head").innerHTML  = '';
//document.getElementById("expwin_detail_label_cont").innerHTML = 



// create map popup 
document.write('<div id="expwin_map_pop" class="map_pop" style="visibility:hidden;position:absolute; left:0px; top:0px;z-index:3000;width:450px;">' +
'<center><div id="coup_google_map_pop" style="width:100%; height:300px; cursor:pointer;"></center></div>' +
'</div>');


///////////////////////////////////////////////////
//// Expandable window garage detail and pop map 

var gPopExpWinMap = null; // map popup for Exp win, obj

var popMapExpWinIcon = null; // icon in Exp Win pop map

/* *********************
    popExpWinDetails
********************* */
function popExpWinDetails(gar, num, type)
{ // the function displays C & S garage detail popup (with popup map below) 
  // gar - garage licence
  
        var garage = garages[gar]; //get garage object
       
        var listDetailLabel = GetElement("expwin_detail_label");
        listDetailLabel.style.display = "none";
        // get content
        GetElement("expwin_detail_label_cont").innerHTML = getExpWinDetailHTML(garage, num, type);

        // set the popup position next to Exp window (oExpWin)
        var oExpWin = GetElement("week_sp");
        
        listDetailLabel.style.left = parseInt(oExpWin.style.left) + oExpWin.offsetWidth + 1; 
        
        listDetailLabel.style.top = oExpWin.style.top;

        listDetailLabel.style.display = "block";

        // map popup (oMapPop)
        var oMapPop = GetElement("expwin_map_pop");

        
        // place below detail popup (listDetailLabel)
        oMapPop.style.top = 
        parseInt(listDetailLabel.style.top) + parseInt(listDetailLabel.offsetHeight) + 1;
        
        oMapPop.style.left =
        listDetailLabel.style.left; 
        
        // the width of map pop
        var loc_pop_width = GetElement("expwin_detail_label").offsetWidth;
        
        if(globJSBrowser!="IE")loc_pop_width -= 4;
        GetElement("expwin_map_pop").style.width = loc_pop_width + "px";        
        
        

        oMapPop.style.visibility = "visible";

 
 if(gPopExpWinMap == null)
 { // create map for coupon map popup if doesn't exist

 gPopExpWinMap = new GMap2(GetElement("coup_google_map_pop"), {mapTypes:[G_NORMAL_MAP]});
 
 gPopExpWinMap.addControl(new GLargeMapControl(),
                new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(3,3)));
 
 GEvent.addListener(gPopExpWinMap, 'click', HideExpWinDetailsDirect);
 }
     // the garage location
     var latLng = garage.primaryEntrance.latLng;

     // move the popup map to display the garage
     gPopExpWinMap.setCenter(latLng, defaultPointZoom);
       
       // display the garage (garage) icon on the map popup
       if(popMapExpWinIcon!=null)
       gPopExpWinMap.removeTLabel(popMapExpWinIcon);
       
       // create and add garage icon to the map
       popMapExpWinIcon = AddGarageIcon(gPopExpWinMap, garage, "cs_pop_ico"); // [garages9b.js]
       
       
return false;

}


/* *********************
    getExpWinDetailHTML
********************* */
function getExpWinDetailHTML(garage, num, spec_type)
{ // combines HTML content for garage details popup for C & S win
  // it's analog of garage's getDetailHTML [garagesX.js]
  // num - index of the spec(coup) in array
  // spec_type - 's' (special) or 'c'(coupon) 
  
  var detailHTML = '';
     
        ///// G E N E R A L  garage INFO
       
       var locparam = '';
       if(CITY_NAME == 'Chicago' && garage.company.toLowerCase().indexOf('next')>-1)locparam = 'phn_mon';
       
       detailHTML += garage.GetGeneralInfoHTML(locparam);

       if(CITY_NAME == 'Chicago')
       detailHTML += "<div class='gentax'>Tax Included</div>";
       else
       detailHTML += "<div class='gentax'>" + DTax + "% Tax Included</div>";
 
       var loc_SUV = '';
       
       if(garage.company.toLowerCase().indexOf('hertz')>-1)
       loc_SUV = '50';
       else if (garage.company.toLowerCase().indexOf('champion')>-1)
       loc_SUV = '50';
       else if (garage.company.toLowerCase().indexOf('five star')>-1)
       loc_SUV = '0';
      
      if(loc_SUV != '')
      detailHTML += "<center style='margin-bottom:6px;'><div class='gentax'>" + 
      'For SUV/Oversize, Add $' + loc_SUV + ' Per Week' + "</div></center>";
      
      
      detailHTML += '<table border="0" cellspacing="0" cellpadding="0" width="100%" class="srch">';
     
      detailHTML += '<tr><td><img src="' + PATH_IMG + 'images/pop_yellow_tl.gif"></td>';
      detailHTML += '<td width="100%" colspan="2"></td><td width="8"><img src="' + PATH_IMG + 'images/pop_yellow_tr.gif"></td></tr>';
     
      // combine content
      detailHTML += '<tr><td></td><td colspan="2" style="padding-bottom:7px;"><center><span class="name"><b>WEEKLY SPECIAL</b></span></center></td><td width="8"></td></tr>'; 
      
      if(garage.green == 1 || garage.green == 0)
      var loc_Btn = 'Get Free RATE GUARANTEE';
      else if(garage.green == 2)
      var loc_Btn = 'Get Free RESERVATION'; 
           

     var GetRateStr = '<table class="btn_get_free_rrg" cellspacing="0" ' +
                      'onClick="return OpenWeekly(' + garage.licenceNumber + ', ' + num + ');">' +
                      '<tr><td>' + loc_Btn + '</td></tr></table>';      
      
      
      loc_week_sp = arrAllWeeklySpecs[num]; // select weekly spec
            
      detailHTML += '<tr><td></td><td valign="middle" width="100%" style="font-size:12px;color:#191919;padding-left:6px;' +
      '"><b>Stay up to 7 Days ... $';
      detailHTML += loc_week_sp.rate + '</b></td><td>';
      
      detailHTML += GetRateStr;
      
      detailHTML += '</td><td></td></tr>';
      
      detailHTML += '<tr class="srch"><td><img src="' + PATH_IMG + 'images/pop_yellow_bl.gif"></td>' +
      '<td  colspan="2"></td><td width="8"><img src="' + PATH_IMG + 'images/pop_yellow_br.gif"></td></tr>';

      
      detailHTML += '</table><div class="space" style="height:6px;">&nbsp;</div>';

     //detailHTML += garage.GetGarageAttributesHTML();
         
return detailHTML; 
}


/* *********************
  HideExpWinDetailsDirect
********************* */
function HideExpWinDetailsDirect()
{// hide detail popup of C & S win

DisplayElem("flick6", "none");
DisplayElem("expwin_detail_label", "none");
DisplayElem("expwin_map_pop", "hidden");

if(typeof(CloseWeekWin)=="function")
CloseWeekWin();

DisplayElem("day_sp_float", "none");

}



