//vars
var cookieData = null;
var go_showOverlay = false;
var override_showOverlay = false;
var JSONData = null;

$(document).ready(function() {
  
  go();
  
});

function go(){
  //get config settings
  getConfig();
  
  //hash commands (overrides)
  if(location.hash=='#showOverlay'){
    override_showOverlay = true;
  }
}
function getConfig(){
  
  $.ajax({
    type: 'GET',
    url: '/webservice?config&nocache='+Date(),
    success: function(data){
      JSONData = jQuery.parseJSON(data);
      getConfig_process();
    }
  });
  
}
function getConfig_process(){
  if(typeof JSONData.config !== 'undefined'){
    
    var fullDayInMilliSeconds = 86400000;
    
    //get date ranges
    var dateStart = 0;
    var dateEnd = 0;
    if(
      typeof JSONData.config.date_start !== 'undefined' &&
      typeof JSONData.config.date_end !== 'undefined'
    ){
      if(
        JSONData.config.date_start &&
        JSONData.config.date_end
      ){
        dateStart = Date.parse( JSONData.config.date_start.replace(/-/g,'/') );
        dateEnd = Date.parse( JSONData.config.date_end.replace(/-/g,'/') );
      }
    }
    dateEnd += fullDayInMilliSeconds; //extend to end of day
    
    //get today's date
    var today = new Date();
    today = today.getTime();
    
    //is today within config date range?
    if(dateStart <= today && today <= dateEnd){
        
      //get lastViewDate from cookie
      cookieData = getCookie();
      var lastViewDate_milliseconds = 0;
      if(cookieData){ //if not null
        if(typeof cookieData.lastViewDate !== 'undefined'){ //if exists
          lastViewDate_milliseconds = Date.parse(cookieData.lastViewDate);
        }
      }
      
      /*
      is lastViewDate within config date range?
      If yes, don't show overlay.
      If no, write cookie and show overlay.
      */
      if(dateStart <= lastViewDate_milliseconds && lastViewDate_milliseconds <= dateEnd){
        
        go_showOverlay = false;
        
      } else {
        
        //write cookie
        cookieData = {
          lastViewDate:Date()
        };
        writeCookie(cookieData);
        
        //check if cookie was written. If not, don't show overlay (to prevent showing it all the time)
        cookieData = getCookie();
        if(cookieData){
          if(typeof cookieData.lastViewDate !== 'undefined'){
            go_showOverlay = true;
          }
        }
        
      }
      
    } else {
      //out of range
    }
    
    //override?
    if(override_showOverlay){
      go_showOverlay = true;
    }
    
    //show overlay?
    if(go_showOverlay){
      showOverlay();
    }
    
  }
}
function getCookie(){
  return $.cookies.get( 'AW_AnnouncementOverlay');
}
function writeCookie(data){
  $.cookies.set( 'AW_AnnouncementOverlay', JSON.stringify(data), {expiresAt: new Date(2199,0,1)} ); //1-19-2012 - added expiration date (before it was none, which deletes when browser closes)
}
function showOverlay(){
  //create hidden fancybox link
  $('body').append('<a style="display:none" id="fancybox_link" href="/webservice?content&nocache='+Date()+'">link</a>');
  
  //show overlay
  var w = JSONData.config.w;
  var h = JSONData.config.h;
  var url = JSONData.config.url;
  $("#fancybox_link").fancybox({
    overlayColor:'#009ddc',
    autoDimensions:false,
    width:w,
    height:h
  });
  $('#fancybox_link').animate({opacity:0},100,function(){
    $("#fancybox_link").trigger('click');
  });
  
  //add buttons inside content div
  var html = '';
  html += '<div id="fancybox-bottom-buttons">';
    html += '<div class="inner inline-block">';
      html += '<a class="btn" href="'+ url +'"><span>Find Out More</span></a>';
      html += '<a class="btn" href="#" onClick="parent.jQuery.fancybox.close();return false;"><span>No Thanks</span></a>';
      html += '<div class="clearfloat"></div>';
    html += '</div>';
  html += '</div>';
  $('#fancybox-outer').append(html);
}
