//this method is used for the signout functionality 
function fb_logout() {
  FB.Connect.get_status().waitUntilReady(function(status) {
    switch(status) {
      case FB.ConnectState.connected:
        FB.Connect.logoutAndRedirect("http://" + hostname + "/sign_out");
        break;
      case FB.ConnectState.appNotAuthorized:
      case FB.ConnectState.userNotLoggedIn:
        document.location.href ="http://" + hostname + "/sign_out";
        break;
    }
    return false;
  });
}

// this method shows the alert errors and notice bars
function alert_show(type) {
  if ($(".nav_container").length <= 0)
  {
    // hide the spacer when you make the alert visible such that it doesn't push
    // the page down
    $("#alert_spacer").css("display","none");
   
    // set the margin correctly, since there is no nav and no nav shadow, set
    // the negative margin back to 0
    $("#alert_" + type).css("margin-top", "0px");
    $("#alert_" + type +"_bg").css("top", "81px");
    $("#alert_heading_bg").css("top", "81px");
  }
  else
  {
    $("#alert_" + type).css("margin-top", "-28px");
    $("#alert_" + type +"_bg").css("top", "112px");
    $("#alert_heading_bg").css("top", "112px");
  }
  
  // fade in the alert components
  $("#alert_" + type +"_bg").fadeIn("slow");
  $("#alert_heading_bg").fadeIn("slow");
  $("#alert_" + type ).fadeIn("slow");

  // if the alert is a notice make it disappear after a few seconds
  if (type =="notice") {

    setTimeout("alert_hide('"+type+"')", 5000);
  
  }
}


// this function hides the alert bar components
function alert_hide(type) {

  $("#alert_" + type +"_bg").fadeOut("slow")

  $("#alert_heading_bg").fadeOut("slow");
  $("#alert_" + type ).fadeOut("slow", function () {

    // need to make the spacer visible again when there is no nav to make sure
    // the page doesn't jump back up
    if ($(".nav_container").length <= 0)
    {  
      $("#alert_spacer").css("display","block");
    }
  });
}

//this method will position the skin elements based on the window size
function position_skin(mode) {
        var bg = $('#retailer_bg');
        var skin = $('#skin');
        var bg_offset = bg.offset();
        var top_right_image = $("#skin_top_right_image");
        var bottom_left_image = $("#skin_bottom_left_image");

        skin.appendTo('#retailer_bg'); 

        //bg.height(($('#pagebody').height() - $('#pagefooter').height() ) + "px");
        //bg.height($('.left_module').offset().top + $('.left_module').height() + "px");
        bg.css("padding-top", (( 128 ) +"px"));
        //bg.css("margin-top", "11px");
        $('#pagetitle').prependTo('#retailer_bg'); 
        $('#pagetitle').css('padding', '0px');
        $('#pagetitle').css("color", "#696969");
        var skin_top = bg_offset.top ;
        // if the mode is set to retailer, then make the header small and don't
        // show the logo and chalkboard
        if( mode =="retailer")
        {
          var height = 0;
          height = height +  $('#pagetitle').outerHeight();
          $('.left_module').each(function(idx) {
            height = height + $(this).outerHeight();
          });
          height = height + 22; 
          bg.height(height + "px");
          bg.css("padding-top", (( 15 ) +"px"));

          $('#skin_header').height("55px");
          $('#skin_header_content').css("display", "none");
          $('#skin_merch_logo').css("display", "none");
          $('#skin_merch_title').css("display", "none");
          $('#skin_chalk_board').css("display", "none");
          $('.left_module').css("margin-top","22px"); 
          $('.pagetitle').css("color", $('#skin_merch_title').css("color"));
        }
        else
        {   
          $('#pagetitle').css('display', 'none');
          var height = 0;
          $('.left_module').each(function(idx) {
            height = height + $(this).outerHeight() ; 
          });
          height = height + 22;
          bg.height(  height + "px");
        }
        
        $('#skin_header').width(bg.outerWidth());
        $('#skin_header').css("top", 0);
        $('#skin_header').css("left", 0);
        $("#skin_header").css("display","inline");
        $("#skin_top_right").css("left", bg.outerWidth() - top_right_image.width() );
        $("#skin_top_right").css("top",0);
        $("#skin_top_right").css("display","inline");
        $("#skin_top_right").height(bg.outerHeight());
        $("#skin_bottom_left").css("left",0 );
        var top = bg.outerHeight() - bottom_left_image.height();
        if (top < 0)
        {
          top = 0;
        }
        $("#skin_bottom_left").css("top",top);
        if( $("#skin_bottom_left").height() > bg.outerHeight()){
          $("#skin_bottom_left").height(bg.outerHeight());
        }
        $("#skin_bottom_left").css("display","inline");
};



function set_skin_elements(mode,top_image, bottom_image, title_color, bg_color, color) {

  // hide the nav shadow
  //$('.nav_container').height("30px");

  // set he title color
  $('#skin_merch_title').css("color", "#"+title_color);
  $('#skin_chalk_board').css("color", "#"+title_color);
  
  // set he background color
  $('#retailer_bg').css("background", "#"+bg_color);

  // set the border color
  $('.left_module').css("border-color", "#"+color);

  //set the header background color
  $('#skin_header_bg').css("background", "#"+color);
  

  // set image load event handlers to position the page when the images have been
  //loaded
  $("#skin_top_right_image, #skin_bottom_left_image").load(function() {
    position_skin(mode);
  });
  // set the images
  $("#skin_top_right_image").attr("src", top_image);
  $("#skin_bottom_left_image").attr("src", bottom_image);
  
};

function cs_replace_html(id, html_str) {
  $(id).html(html_str);
  //adding this to reneable the modals after the page has been update via ajax
    $('a.modalInput').overlay({
    closeOnClick: false
  });

};

function cs_hide(id) {
  $(id).hide();
};

function open_twitter(promotion_id){
    window.open( 'http://' + hostname + '/shares?promotion_id=' + promotion_id +
'&service_ident=TWITTER' ,'twitter',
'width=800,height=600,scrollbars=1,status=0,menubar=0,resizable=1');
};

function send_email_share(){
    $.post(
        "/shares", 
        {
          promotion_id: $("#email_share_promotion_id").val(), 
          addresses: $("#email_share_addresses").val(),
          service_ident: "EMAIL"
        }, 
        null,  
        "script"
    ); 
};
$(document).ready(function() {

  // if there is no nav, then add a spacer such that there is room for alert
  // notifications
  if ($(".nav_container").length <= 0)
  {
     $("#pagecontent").prepend('<div id="alert_spacer" style="height:28px;"></div>');
   }

  // make the entire promotion div clickable if there is one
/*
  $('.promo_list_item').click(function (e) {
    document.location.href = "/promotions/" + $(this).attr("id");
  });
*/
  // ajax call to update the promo list
  $(".promo_link").live("click", function() {
    $("#promo_page").html("Page is loading...");
    $.getScript(this.href);
    return false;
  });

    
  $("#search_submit").click(function () {
    $("#nav_search_form").submit();
  });

  $(".print_link").click( function() {
    window.open(this.href, "Print Offer", "location=no,scrollbars=yes,width=870,height=700,menubar=no, toolbar=no, location=no");
    return false;
  });

  $(".location_map").colorbox( { width:"650px", height: "700px", opacity:0.4,
iframe:true}
  );

  $(".facebook_share").click( function() {
    $.post("/shares", {promotion_id: this.id, service_ident: "FACEBOOK"}, null,
"script");    
    return false;
  });
  $(".twitter_share").click( function() {
    open_twitter(this.id);
    return false;
  });

  $(".email_share").click( function() {
    $("#email_share_promotion_id").val(this.id);
    $("#email_share_form_link").click();
    return false;
  });
  $("#email_share_form_link").colorbox( {
    width:"425px", 
    height:"270px",
    inline:true, 
    href:"#email_share_form", 
    opacity:0.75
  }); 

});

