/*
 * Custom functions for Torinos
 * Author: Armando S. Romero III
 *          yourfriendarmando.com
 * Version: 1.0.1: First release
 * 1.0.2: Make window scroll to Info Ribbon from any caller link
 * 2011-11-09: 1.0.3: External links should be followed if browser is incapable of opening tabs/windows
 * 2011-11-10: 1.0.4: Discovered IE new likes scrolling from the html anchor as opposed to body
 **/

var scrAnchor; // Put into global scope

$(document).ready(function() {
  scrAnchor = "body"; // Will be used by menu scroll pops in menus and catering
  //if(navigator.userAgent.match(/(MSIE)|(Safari)/i)){
  
  if(navigator.userAgent.match(/(MSIE)/i)){
    scrAnchor = "html";
  }
  
  
  // Hide the contact information ribbon
  $(".hideInfoRibbon").click(function(){
    $("#infoRibbon").animate({top: -2000},600);
    $("#showInfoRibbon").fadeIn(300);
  });
  
  // Show the contact information ribbon
  $("#showInfoRibbon, .showInfoRibbon").click(function(){
    $("#infoRibbon").animate({top: 0},600);
    $("#showInfoRibbon").fadeOut(300);
    $(scrAnchor).animate({
      scrollTop: $("#infoRibbon").offset().top
    }, 500);
  })
  
//  $("#infoRibbon").animate({top: 0},600); // Force reveal information ribbon
  
  $(".mainPh").attr("href", "tel:+1-" + hrefRender("mainPhone"));
  $(".mainPh").html(hrefRender("mainPhone"));
  $(".mainMail").attr("href", "mailto:" + hrefRender("email"));
  $(".mainMail").html(hrefRender("email"));

  //  Ensure all links to external sites open in a new window/tab
  $("a[href^='http'],.newWin").click(function(event){
    if(window.open($(this).attr('href'))){
      event.preventDefault();
    }
    
    // newWin = window.open($(this).attr("href"),$(this).attr("title")); // Not compatible with IE9 2011-01-04 Armando S. Romero

    return false; // Added for further compatibility with browsers like Safari 2011-01-04 Armando S. Romero
  });
  
  $("a[href^='#']").click(function(event){
    event.preventDefault();
  });
  
  // logic to allow a user to hide the sub menu, or allow it to fade out after 3.5 seconds
  var fadeOutTimeout;
  $('.cssdropdown li.headlink').click(function(){
    if($('ul', this).is(":visible")){
      $('.cssdropdown li.headlink ul').fadeOut(500);
    }
    else{
      $('ul', this).fadeIn(500);
      clearTimeout(fadeOutTimeout);
      fadeOutTimeout = setTimeout(function(){
        $('.cssdropdown li.headlink ul').fadeOut(500);
      }, 3500);
    }
  });
  
  // Add a down arrow to any menus with sub menus, identified as sub menus
  $('a.submenu').append(' <img src="/images/spacer.gif" alt="sub menu" />');
  
  // Get a Date object
  var dO = new Date();
  // Hide thanksgiving references until they are removed physically
  
  var unixtime_ms = dO.getTime(); // Returns milliseconds since the epoch
  var unixtime = parseInt(unixtime_ms / 1000); // Seconds since epoch
  
  ///unixtime = 1400000000;

  if(unixtime > 1325375999){ // Hide after 2011-12-31 23:59:59
    $("#newYearsMsg").hide();
    $("#newYearsMenuLink").hide();
  }
  

  // Indicate to client if location is open
  
  var dayToday = dO.getDay();
  
  var openStatus = false;
  var currentTime = dO.getHours() * 60 + dO.getMinutes();
  var openStr = "Torinos's @ Home is <label class=\"weAreOpen\">Open</label>";
  var openLinkStr = "see our complete hours";
  var closedStr = "Torinos's @ Home is <label class=\"weAreClosed\">Closed</label>";
  var closedLinkStr = "see when we're open";
  
  var openTime1;
  var closedTime1;
  var openTime2;
  var closedTime2;
  
  //dayToday = 3; // For testing
  //currentTime = 621; // For testing
  
  switch(dayToday){
    case 0: // Sunday
    case 1: // Closed Sundays and Mondays
      break;
    case 2:
    case 3:
    case 4:
      openTime1 = 479;
      closedTime1 = 871;
      if(currentTime > openTime1 && currentTime < closedTime1){
        openStatus = true;
      }
      break;
    case 5: // Two open times on Friday
      openTime1 = 479;
      closedTime1 = 871;
      if(currentTime > openTime1 && currentTime < closedTime1){
        openStatus = true;
      }
      
      openTime2 = 1049;
      closedTime2 = 1261;
      if(currentTime > openTime2 && currentTime < closedTime2){
        openStatus = true;
      }
      break;
    case 6: // Two open times on Saturday, time1 is different from during the week
      openTime1 = 539;
      closedTime1 = 871;
      if(currentTime > openTime1 && currentTime < closedTime1){
        openStatus = true;
      }
      
      openTime2 = 1049;
      closedTime2 = 1261;
      if(currentTime > openTime2 && currentTime < closedTime2){
        openStatus = true;
      }
      break; 
  }
  if(openStatus === true){
    $("#hoursStatusMsg").html(openStr);
    $("#hoursStatusSign").html(openLinkStr);
  }
  else{
    $("#hoursStatusMsg").html(closedStr);
    $("#hoursStatusSign").html(closedLinkStr);
  }
});


