/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          daily_specs.js (former cs_window.js )
'daily Specials' Expandable windows functions
// v.12n Advance Search (it was 12k while in development)
// v.12u version for Custom compatibility: few functions moved into all_specials.js
// images fix
// v.21m  versions (get variable)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

// SPECIALS_MAP_URL defined in index.php


var currDSwindowDate = null; // Date object, current date for DS window
//var arrCSwindowCoupons = new Array();
//var bCSwinUserMinimized = false; // whether user minimized the window


var bSpecialsMapView = false;
                                                           /* &#13;&#10; */
var arrSpecialsDesc_2nd = new Array("", "<br>Enter Before 11AM", "<br>Enter 11AM-3PM", "<br>Enter After 3PM"); 

////var arrCouponsDescriptions = Array("$10: Theater/Radio City/Carnegie Hall", "$20: Madison Square Garden");

// if user closed DS it will leave closed on other search
//var bDS_UserMinimized = false;
//var bDS_UserMaximized = true;

/// CURRENTLY only 1 coupon assumed, the coupon button to be displayed always
/// and only needed to know whether coupons for this area set
var isCouponsSelected = false;


/* *********************
    MinimizeCoups
********************* */
function MinimizeCoups()
{ // hide DS window; minimized C&S is always below the DS win

DisplayElem("day_sp", "none");
DisplayElem("day_sp_min", "none");
DisplayElem("coupwait", "none");
 
//HideCSwinDetailsDirect(); // hide garage detail popup
}

/* *********************
    MaxmizeCoups
********************* */
function MaxmizeCoups()
{ // display DS window
HideGreenDetails();
DisplayElem("day_sp", "block");
}

/* *********************
    SelectCSdate
********************* */
function SelectCSdate()
{ // called when user clicks DS win calendar, and therefore selects the day

checkShowMonth(dateCalendarDate);

currDSwindowDate = dateCalendarDate;
 
if(LoadingAllSpecs == -1) // the all specials are being downloaded at the moment
{
DisplayElem("coupwait", 'block'); // "Please Wait"
return;
} 
if(LoadingAllSpecs == 0)  // the all specials are not yet downloaded
{
DisplayElem("coupwait", 'block');
LoadXML_AllSpecs(currDSwindowDate);
return;
}
 
var ret_val = PopulateDailyExpWin();

if(!ret_val)alert("There are no Specials for this date.");

//DisplayCoups(); // refresh DS wincontent by current date currDSwindowDate

}


/* *********************
    DisplayDailyExpWins
********************* */
function DisplayDailyExpWins()
{
  /// CURRENTLY DS should displayed always (at least with Coupons buttons) 
  if(parking_isMonthly /*|| arrDisplayedGreenGarages.length == 0*/) return;
     
     currDSwindowDate = currRequestDate;

    // position Daily Exp Wins

   var winPos = PositionExpWin("day_sp");
   
   var oCoupWait = GetElement("coupwait");
   
   oCoupWait.style.left =  parseInt(winPos[0]) + 61;
   oCoupWait.style.top = parseInt(winPos[1]) + 89;

     
     if(!PopulateDailyExpWin())
     {  
        MinimizeCoups();
        DisplayElem("day_sp_min", "none");
     }
   /*  else
     { // open 
    
     if(bDS_UserMaximized && GetElement("week_sp").style.display == "none")
     DisplayElem("day_sp", "block");
     } */


}


/* *********************
    PopulateDailyExpWin
********************* */
function PopulateDailyExpWin()
{ // the function collects and displays DS window content

     //alert("PopulateDailyExpWin");

  var oCoupFrame = GetElement("day_sp_frame");// inner part of Exp win
  
  var oCoupFrameShrnk = GetElement("day_sp_frame_min");
  
  var strDate = currDSwindowDate.getFullYear() +  "-" + currDSwindowDate.getMonth()  + "-" + currDSwindowDate.getDate();
  
       var currWD = currDSwindowDate.getDay();  
       
        // date string on the top of Daily Spec Win
        var strDateCS = arrDaysFull[currWD] + ", "
        +  arrMonths[currDSwindowDate.getMonth()] + " " + currDSwindowDate.getDate();
        
        var month_indx = currDSwindowDate.getFullYear() +  "" + currDSwindowDate.getMonth();
        var day = currDSwindowDate.getDate();

        
        var min_rate = 999999;
        var cachedHTML = "";
 
        var arr_SelectedDescr = Array();
        /*
        var isSUV = 0;
        if(parking_isCase && parking_vehicle == VEHICLE_SUV)isSUV = 1;
        */
        
        var locSelectedGroups =   " ";
        
        /// CURRENTLY only 1 coupon assumed, the coupon button to be displayed always
        /// and only needed to know whether coupons for this area set
        isCouponsSelected = false;
        
        
        // the function goes through all green garages
        // through all Coup Groups (to check garage) and 
        // through all Specs and Coups for every garage
        // and SELECTS descriptions
        
        // go through all garages in arrDisplayedGreenGarages
        for(var lg=0; lg < arrDisplayedGreenGarages.length; lg++)
        {
        
        var curr_gar = garages[arrDisplayedGreenGarages[lg]];
        if(curr_gar.green == 0)continue;

        if(useCouponsButton) //// useCouponsButton
        {
       
        // check the garage in Coup Groups (arrAllCoupGroups)
        for(var id in arrAllCoupGroups)
        {
        var gars = " " + arrAllCoupGroups[id].garages + " ";
        
        // the group already selected
        if(locSelectedGroups.indexOf(" " + id + " ") > -1)continue;
        
        if(gars.indexOf(" " + curr_gar.licenceNumber + " ") > -1)
        { // the garage is in coup group
          // so add to output
        locSelectedGroups += id + " "; 
        
        var description = arrAllCoupGroups[id].description;
       
        if(trim(description)== "")continue;
        
        //var rate = arrAllCoupGroups[id].rate;
        //rate = rate.replace(/\$/g, "");
       
        min_rate = Math.min(min_rate, arrAllCoupGroups[id].rate); 
        
        var found = false;
       
        for(var i = 0; i < arr_SelectedDescr.length; i++)
        {
         if(arr_SelectedDescr[i] == description)found = true;
        }
        
        /// CURRENTLY only 1 coupon assumed, the coupon button to be displayed always
          if(!found)isCouponsSelected = true;
        //if(!found)arr_SelectedDescr[arr_SelectedDescr.length] = description;
        
        }
        
        }

        } // END if(useCouponsButton)
        
         
        //// GET SPECIALS of the garage
        if(curr_gar.arrAllSpecNew[currWD])
        { 
        var loc_arrSpec = selectSpecsShow(curr_gar.arrAllSpecNew[currWD],"s" ,curr_gar, month_indx, day, 2);
        
        // go through specials
        for(var s=0; s < loc_arrSpec.length; s++)
        {
        
        if(typeof(loc_arrSpec[s])=='undefined')continue;
        
        var  description = loc_arrSpec[s].description;
         
        min_rate = Math.min(min_rate, loc_arrSpec[s].rate);
          
        // description for SWP is string of digits (1 - Early-Bird; 2 - Mid-Day; 3 - Evening-Night 
        var arrSpDescr = description.split(" ");
        
        // for each of digits
        for( var d in arrSpDescr)
        {
        var found = false;
       
        for(var i = 0; i < arr_SelectedDescr.length; i++)
        {
         if(arr_SelectedDescr[i] == arrSpDescr[d])found = true;
        }
        
        if(!found)arr_SelectedDescr[arr_SelectedDescr.length] = arrSpDescr[d];
        }
        
        }             
        
        }        
        
       
        //// GET COUPONS of the garage
        if(curr_gar.arrAllCoupNew[currWD])
        {
        
        
        var loc_arrCoup = selectSpecsShow(curr_gar.arrAllCoupNew[currWD],"c" ,curr_gar, month_indx, day, 2);
          
        // go through coupons
        for(var c=0; c < loc_arrCoup.length; c++)
        {
        
        if(typeof(loc_arrCoup[c])=='undefined')continue;
        
        var  description = loc_arrCoup[c].description;
       
        //var rate = loc_arrCoup[c].rate;
        //rate = rate.replace(/\$/g, "");
        
        min_rate = Math.min(min_rate, loc_arrCoup[c].rate);

        var found = false;
       
        for(var i = 0; i < arr_SelectedDescr.length; i++)
        {
         if(arr_SelectedDescr[i] == description)found = true;
        }
        /// CURRENTLY only 1 coupon assumed, the coupon button to be displayed always
          if(!found)isCouponsSelected = true;
        //if(!found)arr_SelectedDescr[arr_SelectedDescr.length] = description;
         
        }        
          
        }   


        
}  // end garages cycle 
        
  //alert("min_rate=" + min_rate);

/// CURRENTLY coupons buttons displayed always and their min = 10
if(min_rate == 999999)min_rate = 10;

//var windowIsEmpty = (min_rate == 999999);

var windowIsEmpty = (!useCouponsButton && arr_SelectedDescr.length==0);
 
// return false if nothing to display in DS win
if(windowIsEmpty)return false;


     /// CURRENTLY only 1 coupon assumed, the coupon button to be displayed always
     // COUPONS buttons

     if(useCouponsButton) //// useCouponsButton
     {     

      
     cachedHTML += '<tr class="coupon"><td><img src="' + PATH_IMG + 'images/pop_red_tl.gif"></td>'; 
     cachedHTML += '<td width="100%"></td><td><img src="' + PATH_IMG + 'images/pop_red_tr.gif"></td></tr>'; 
      
      cachedHTML += '<tr class="coupon"><td></td><td style="padding:2px  0;">';
       
      cachedHTML += 
          '<center><table id="coup_btn" class="ds_big_btn2 plain" cellspacing="0" ' +
          'indx="COUPONS for Theaters/Events" onClick="DisplayCouponButtons(true);"' +
          '>' +
          '<tr><td width="6"><img src="' + PATH_IMG + 'images/ds_big_btn_l_coup.gif"></td>' +
          '<td width="100%" class="c" style="width:239px;">' +
          '<span class="line1st"><nobr>' + DS_COUPON_BTN_TXT_1 + '</nobr></span>' +
          '<br>' +
          '<span class="line2nd"><nobr>' + DS_COUPON_BTN_TXT_2 + '</nobr></span>' +        
          '</td>' +
          '<td width="6"><img src="' + PATH_IMG + 'images/ds_big_btn_r_coup.gif"></td></tr>' +
          '<tr><td></td></tr></table></center>';        
          
      cachedHTML += '</td><td></td></tr>';
      
     cachedHTML += '<tr class="coupon"><td><img src="' + PATH_IMG + 'images/pop_red_bl.gif"></td>'; 
     cachedHTML += '<td></td><td><img src="' + PATH_IMG + 'images/pop_red_br.gif"></td></tr>'; 
      
      
      if(arr_SelectedDescr.length>0)
      cachedHTML += '<tr><td></td><td style="padding:4px; 0">' +
      '<div class="space">&nbsp;</div>' +
      '</td><td></td></tr>';
      

      }
      
      arr_SelectedDescr = arr_SelectedDescr.sort();
      
     
     if(arr_SelectedDescr.length>0)
     cachedHTML += '<tr class="spcl"><td><img src="' + PATH_IMG + 'images/pop_purp_tl.gif"></td>' +
                   '<td width="100%"></td><td><img src="' + PATH_IMG + 'images/pop_purp_tr.gif"></td></tr>'; 
      
      
      for(var i = 0; i < arr_SelectedDescr.length; i++)
      {
      var loc_descr = arr_SelectedDescr[i];
      
      var loc_descr2 = "";
      var loc_descr1 = loc_descr;
      
      if(loc_descr==1 || loc_descr==2 || loc_descr==3)
      {loc_descr1 = arrSpecialsDescriptions[loc_descr];
      loc_descr2 = arrSpecialsDesc_2nd[loc_descr];}

      
      cachedHTML += '<tr class="spcl"><td></td><td style="padding:2px  0;">';
      
      cachedHTML += 
          '<center><table class="ds_big_btn2 plain" cellspacing="0" ' +
          'indx="' + loc_descr1 + '" ' +
          'indx0="' + loc_descr + '" ' +          
          'onClick="clickDSbutton(this);">' +
          '<tr><td width="6"><img src="' + PATH_IMG + 'images/ds_big_btn_l_spcl.gif"></td>' +
          '<td width="100%" class="c" style="width:239px;">' +
          '<span class="line1st"><nobr>' + loc_descr1 + '</nobr></span>' +
          '<span class="line2nd"><nobr>' + loc_descr2 + '</nobr></span>' +        
          '</td>' +
          '<td width="6"><img src="' + PATH_IMG + 'images/ds_big_btn_r_spcl.gif"></td></tr>' +
          '<tr><td></td></tr></table></center>';      
      
      cachedHTML += '</td><td></td></tr>'; 
      }

     if(arr_SelectedDescr.length>0)
     cachedHTML += '<tr class="spcl"><td><img src="' + PATH_IMG + 'images/pop_purp_bl.gif"></td>' +
                   '<td></td><td><img src="' + PATH_IMG + 'images/pop_purp_br.gif"></td></tr>'; 
     
     
     
     //alert(cachedHTML);


 var core_contents = '<center><table border="0" cellspacing="0" cellpadding="2" width="100%">' +
            '<tr><td style="color:#191919;font-size:13px;line-height:14px;padding:10px 10px 7px 10px"><center>' +
            '<b>Make a Choice Below<br>To See Results in New Window:</b></center></td></tr>' +
            '<td id="btns_area"><center><table border="0" cellspacing="0" width="100%">' + cachedHTML +  '</table></center></td></tr>' + 
             '</table></center>';        

  
   var contents = '<center>';
   
   contents += '<div style="text-align:left;"><input id="csDateTemp" type="text" value="' +  strDate  + '" style="visibility:hidden;width:1px;height:1px;"></div>';
  
   contents += '<span class="date">' + strDateCS + '</span>';
   
   if(!parking_isCase)
   contents += ' or <a class="cal" href="javascript:calendPlace=1;calPadding = true;DisplayCal(GetElement(\'csDateTemp\'), SelectCSdate);">calendar</a>';
   
   contents += '</center>';
   
   contents +=  core_contents;
     
   DisplayElem("coupwait", "none");
   
   oCoupFrame.innerHTML = contents;
   
    var contents_min = 'From $' + min_rate;
    if(CITY_TAX)
    contents_min += '&nbsp;(tax incl.)';
     
    oCoupFrameShrnk.innerHTML = contents_min; 
   
   GetElement("day_sp").style.height = "";

// return false if nothing to display in DS win
if(windowIsEmpty)return false; 
else
return true;
   
}


/* *********************
  DisplayCouponButtons
********************* */
//var contentDSbuttons = null;

function DisplayCouponButtons(isDisplay)
{  // display 2nd selection coupons buttons

var buttCoupon = GetElement("coup_btn");
var buttArea = GetElement("btns_area");
var oCoupFloat = GetElement("day_sp_float");

if(oCoupFloat.style.display == "block")isDisplay = false;

if(isDisplay)
{

var strCoupBttnHTML = '';
 
 
  strCoupBttnHTML += '<tr class="coupon"><td><img src="' + PATH_IMG + 'images/pop_red_tl.gif"></td>'; 
  strCoupBttnHTML += '<td width="100%"></td><td><img src="' + PATH_IMG + 'images/pop_red_tr.gif"></td></tr>'; 
 
 
 
        
for(var c in arrCouponsDescriptions)
{

var arrDescr = arrCouponsDescriptions[c].split('<br>');

var loc_descr = '<span class="line1st"><nobr>' + arrDescr[0] + '</nobr></span><br>';
if(arrDescr[1])
    loc_descr += '<span class="line2nd"><nobr>' + arrDescr[1] + '</nobr></span>';


strCoupBttnHTML +=
      '<tr class="coupon"><td></td><td style="padding:2px 0;">' +
          '<table class="ds_big_btn2 plain" cellspacing="0" width="100%" ' +
          'onClick="GoCoupon(0, \'cd\', ' + arrCouponsDescriptionsIndxs[c] + ',  ' + arrCouponsDescriptionsType[c] + ');DisplayCouponButtons(false);">' +
          '<tr><td width="6"><img src="' + PATH_IMG + 'images/ds_big_btn_l_coup.gif"></td>' +
          '<td width="100%" class="c">' +
          loc_descr + 
          '</td>' +
          '<td width="6"><img src="' + PATH_IMG + 'images/ds_big_btn_r_coup.gif"></td></tr>' +
          '<tr><td></td></tr></table>' +      
       '</td><td></td></tr>';
}        
        
  strCoupBttnHTML += '<tr class="coupon"><td><img src="' + PATH_IMG + 'images/pop_red_bl.gif"></td>'; 
  strCoupBttnHTML += '<td></td><td><img src="' + PATH_IMG + 'images/pop_red_br.gif"></td></tr>'; 


GetElement("day_sp_float_cont").innerHTML = '<center><table border="0" width="100%" cellspacing="0">' + strCoupBttnHTML +  '</table></center>';


// position
var pos = getAbsolutePositionOf(buttCoupon);

//buttCoupon.style.borderStyle='inset';
//buttCoupon.parentNode.parentNode.style.background = "#aaaaaa";

var pos_left = parseInt(pos[0]) + buttCoupon.offsetWidth + 10;
var pos_top = parseInt(pos[1]) + 3;

oCoupFloat.style.top = pos_top;
oCoupFloat.style.left = pos_left;

oCoupFloat.style.display = "block";



}
else
{
//buttArea.innerHTML = contentDSbuttons;
//contentDSbuttons = null;
oCoupFloat.style.display = "none";
//buttCoupon.style.borderStyle='outset';
}

}


/* *********************
    clickDSbutton
********************* */

function clickDSbutton(oButton)
{
bSpecialsMapView = oButton;

DisplayDSMiniMap();
return;
}


////     FindGarageInCouponGroup
function FindGarageInCouponGroup(curr_gar)
{
 var ret_val = null;       
       
        //alert("FindGarageInCouponGroup");
        
        for(var id in arrAllCoupGroups)
        {
        var gars = " " + arrAllCoupGroups[id].garages + " ";
        
        // the group already selected
        //if(locSelectedGroups.indexOf(" " + id + " ") > -1)continue;
       
        //alert(gars + " \n " + curr_gar.licenceNumber);
        
        if(gars.indexOf(" " + curr_gar.licenceNumber + " ") > -1)
        { // the garage is in coup group
          // so add to output
        //locSelectedGroups += id + " "; 
         
        var description = arrAllCoupGroups[id].description;
       
        if(trim(description)== "")continue;

       /* var rate = arrAllCoupGroups[id].rate;
        rate = rate.replace(/\$/g, ""); */
       
        ret_val = id;
        }
        }
        
return ret_val;        
}


////     ClosedayWin
function CloseDayWin()
{

DisplayElem("day_sp", "none");
DisplayElem("day_sp_float", "none");

}


/// create mini window
document.write('<table id="day_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%">Daily Specials</td></tr>' +
'<tr><td  id="day_sp_frame_min" class="bottom"></td></tr>' +
'</table>');


// create expanded window
CreateStandardPopup("day_sp", "CloseDayWin();", 330); /* defined in garagesX.js */
document.getElementById("day_sp").style.zIndex = 3004;
document.getElementById("day_sp_head").innerHTML  = '<b>Daily Specials & Coupons</b>';
document.getElementById("day_sp_cont").innerHTML = '<div id="day_sp_frame"></div>';

/// create floating window for Coupons
CreateStandardPopup("day_sp_float", "DisplayCouponButtons(false);", 400);
document.getElementById("day_sp_float").style.zIndex = 3004;
document.getElementById("day_sp_float_head").innerHTML  = '<b>Select Coupon</b>';


var oPopWinMap =  null;

///      DisplayDSMiniMap
function DisplayDSMiniMap()
{

var oMainMapDiv = GetElement('google_map');

var popWidth = parseInt(parseInt(oMainMapDiv.offsetWidth)*0.6);
var popHeight = parseInt(oMainMapDiv.offsetHeight);

if(popWidth < 510)popWidth = 510;
if(popWidth < popHeight)popHeight = popWidth;

var loc_indx = bSpecialsMapView.getAttribute("indx");

if(loc_indx == "COUPONS for Theaters/Events" && !isCouponsSelected)
{ // if no coupons in current area - set West 40s
var strCenter = "-73.990602,40.759903";
}
else
{
var mapCenter = gMap.getCenter();
var strCenter = mapCenter.lng() + "," + mapCenter.lat();
}


var URL = SPECIALS_MAP_URL + "?v=" + VERSION + "&center=" + strCenter;

oPopWinMap = window.open(URL,"","width=" + popWidth + ",height=" + popHeight + ",resizable=1 ,toolbar=0, location=0, status=0, menubar=0, directories=0, scrollbars=1");


//alert("oPopWinMap.window.document.title: " + oPopWinMap.window.document.title);
//oPopWinMap.window.GetElement('pop_map_title').innerHTML = bSpecialsMapView.indx;


return;
                    
}                                                     
 
///      CheckDSCalendar
function CheckDSCalendar()
{ // checks current green garages to see whwther specs exist to display
  // for every particular day. Returns array by weekdays

        var arrReturnWeekdays = new Array(false, false, false, false, false, false, false);
        
        for(currWD=0;currWD<7; currWD++)
        {
        
        // go through all garages in arrDisplayedGreenGarages
        for(var lg=0; lg < arrDisplayedGreenGarages.length; lg++)
        {
        
        var curr_gar = garages[arrDisplayedGreenGarages[lg]];
        
        // if not yet defined (loaded) set to enabled
        if(!curr_gar.arrAllSpecNew[currWD] && !curr_gar.arrAllCoupNew[currWD])
        {arrReturnWeekdays[currWD] = true; break;}
        
        //if(curr_gar.arrAllSpecNew[currWD])
        //alert(currWD + " :: " + curr_gar.arrAllSpecNew[currWD].length);
       
        //// GET SPECIALS of the garage
        if(curr_gar.arrAllSpecNew[currWD])
           if(curr_gar.arrAllSpecNew[currWD].length>0){arrReturnWeekdays[currWD] = true; break;}
        
        if(curr_gar.arrAllCoupNew[currWD])
           {if(curr_gar.arrAllCoupNew[currWD].length>0)arrReturnWeekdays[currWD] = true; break;}
        
        }
        
        }
        
        return arrReturnWeekdays;
        
        
}


