/* Fix ie6 png background */
if(typeof DD_belatedPNG != 'undefined') {
	DD_belatedPNG.fix('.png');
}

function privacyPopup() {
  var left = $("#offer_grid").position().left + 44;
	popUpWindow = window.open(
		'http://www.foodietheapp.com/privacy.html','popUpWindow','height=500,width=867,left='+left+',top=150,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=no')
}

function setInputs() {
  $("input.placeholder").each(function() {
    setInput(this);
    if($(this).attr("id") != "password-placeholder" && $(this).attr("id") != "password") {
      $(this).focus(function() {
        if($(this).val() == $(this).attr("rel")) {
          $(this).css({color: "#000"}).val("");
        }
      });
      $(this).blur(function(){ setInput(this) });   
    }
  }); 
}     

function setPasswordInputs(again) {
  $("#password-placeholder"+again).focus(function() {
    $("#password-placeholder"+again).hide(); $("#password"+again).show().focus();
  });
  $("#password"+again).blur(function() { 
    if($("#password"+again).val() == "") {
      $("#password"+again).hide(); $("#password-placeholder"+again).show();
    }
  });   
}

function setInput(el) {       
  if($(el).val() == "")
    $(el).css({color: "#999"}).val($(el).attr("rel"));
  else
    $(el).css({color: "#000"});                                                     
}      

function bindAjaxLinks() {
	$("a.signup-ajax, a.calendar-view, .view-our-restaurants a, .bookit-link").fancybox({padding:0, ajax:{dataType: "script"}});
	$("a.signup-ajax, .bookit-link").show();         
	$(".popupwindow").popupwindow({height:800, width:800, status:1});
}

function showOfferAsFeatured() {
  alert("TODO: showOfferAsFeatured");
}                                                                                   

function showDay(day) {
  var index;
  var current_index = $("#day_index_holder").val();
  $("#ajax-carousel-loader").show();
  $("#ajax-carousel-loader").animate({ "opacity":0.7 }, 600, 'easeOutCubic');
  $(".day").each( function() { 
    if($(this).attr("rel") == day) {
      index = parseInt($(this).attr('id'));
    }
  });
  $("#day_selected_"+current_index).hide();
  $("#"+current_index+"_day_link").show();
  $("#"+index+"_day_link").hide();
  $("#day_selected_"+index).show();
  $("#day_index_holder").val(index);
  $(".day-offers").each( function() { $(this).hide() });
  $("#"+day).show();
  $.ajax({
    type: "get", url: '/offers/featured', dataType: "html", data:"day="+day+"&index="+index,
    beforeSend : function (xhr) { xhr.setRequestHeader('Accept-Encoding', 'identity'); },
    success: function(html) { 
      $("#ajax-carousel-loader").animate({ "opacity":0.0 }, 600, 'easeOutCubic', function(){
        $("#ajax-carousel-loader").hide();
      });
      $('#featured-slideshow').html(html);
      $('#featured-slideshow').show();
      $(".featuredbox").show();
      $(".offer-dot a").tipsy({gravity:'n', html:true});
      $(".slider-m-dot a").tipsy({gravity:'s', html:true});
    	$(".slider-carousel").slideShowLite({btnPrev:".slider-l", btnNext:".slider-r", btnGo:".slider-m-dot",auto:false});
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      alert("Server Error. Please try again later.");
    }
  });
}

function featuredbox_change_reservation(id) {
  $('#featuredbox-confirmed-content-' + id).hide();
  $('#featuredbox-bookit-content-' + id).show();
}
function featuredbox_cancel_reservation(id) {
  $.ajax({
    type: "put", url: '/offers/'+ id +'/seatings/'+$("#booked_seating_id_" + id).val(), dataType: "xml", data: $('#featuredbox_booked_form_' + id).serialize(),
    beforeSend : function (xhr) { xhr.setRequestHeader('Accept-Encoding', 'identity'); },
    success: function(xml) { 
      $('#featuredbox-confirmed-content-' + id).hide();
      $('#featuredbox-bookit-content-' + id).show();
      alert("Your reservation has been cancelled.");
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      alert("Server Error. Please try again later.");
    }
  });
}

function featuredbox_submit_seating_form(id, classname) {
  if($("#featuredbox_seating_specials_" + id).val() > $("#featuredbox_seating_size_" + id).val()) {
    return alert("Foodie does not support reserving more specials than the number of people in your party.  You're not that hungry anyway.");
  }
  $('#lightbox_status_holder').val(classname);
  $("#form_msg").hide();
  $("#featuredbox_booked_time_" + id).val(new Date());
  $.ajax({
    type: "put", url: '/offers/'+ id +'/seatings/'+$("#featuredbox_seating_id_" + id).val(), dataType: "html", data: $('#featuredbox_seating_form_' + id).serialize(),
    beforeSend : function (xhr) { xhr.setRequestHeader('Accept-Encoding', 'identity'); },
    success: function(html) { 
      $('#featuredbox-bookit-content-' + id).hide();
      $('#featuredbox-confirmed-content-' + id).html(html);
      $('#featuredbox-confirmed-content-' + id).show();
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      alert("Server Error. Please try again later.");
    }
  });     
}
function featuredbox_signin(id) {
  $('#lightbox_status_holder').val('featuredbox');
  if(id > 0){
    $('#offer_id_holder').val(id);
    $('#party_size_holder').val($('#seating_size_'+id+' option:selected').val());   
    $('#specials_amount_holder').val($('#seating_specials_'+id+' option:selected').val()); 
    $('#seating_time_holder').val($('#seating_id_'+id+' option:selected').val());          
  }
  $('<a href="/signin?offer_id='+id+'">signin</a>').fancybox({padding:0, ajax:{dataType: "script"}}).click();
}
function featuredbox_replace_offer(id, rest_id) {
  $(".tipsy").remove();
  var current_id = $("#featuredbox_current_index_"+rest_id).val()
  $('#featuredbox_offer_content_' + current_id).hide();
  $('#featuredbox_offer_content_' + id).show();
  $('#featuredbox-offer-dot-selected-' + current_id).hide();
  $('#featuredbox-offer-dot-' + current_id).show();
  $('#featuredbox-offer-dot-' + id).hide();
  $('#featuredbox-offer-dot-selected-' + id).show();
  $("#featuredbox_current_index_" + rest_id).val(id);
}

function emptyDay(day) {
  alert("There are currently no offers for " + day.charAt(0).toUpperCase() + day.slice(1) + ".\n Please select a different day");
}

/* on ready */
$(function() {
  $(".featuredbox").show();
  $(".offer-dot a").tipsy({gravity:'n', html:true});
	$(".slider-m-dot a").tipsy({gravity:'s', html:true});
	$(".slider-carousel").slideShowLite({btnPrev:".slider-l", btnNext:".slider-r", btnGo:".slider-m-dot",auto:false});
  bindAjaxLinks();
	$(".day").each( function() {
	  var day = $(this).attr('rel');
	  if (day != "selected") {
	    $(this).mouseenter(function() {$(this).addClass(day+"-over").removeClass(day)}).mouseout(function() {$(this).addClass(day).removeClass(day+"-over")});
	    if (parseInt($('#'+day+'_offer_count').val()) > 0)
	      $(this).click(function() {showDay(day); $(this).blur(); return false;});
	    else
	      $(this).click(function() {emptyDay(day); $(this).blur(); return false;});
    }
	});
	//if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
  // var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
  // if (ieversion>=5)
  //  alert("Foodie is now only optimized for Firefox and Chrome on the PC\n GET FIREFOX http://www.mozilla.com/\n GET CHROME http://www.google.com/chrome\n Or check back soon for Internet Explorer version.");
  //}
});  