/**
 * Validates a cookie set on two different sites ( public & dealer ) by
 * calling the other to see if they have a cookie set.
 *
 * Usage requires the two sites have an apache mapping to /check-id/ from
 * the root, that calls the JsonPSessionCheck handler.
 */
var b2bCheck = {
  thisSite : null,
  sisterSite : null,
  viewSite : null,
  checkComplete : false,
  load : function(a,b){
    var protocol = (("https:" == document.location.protocol) ? "https:" : "http:");
    b2bCheck.thisSite = a.replace("http:", protocol);
    b2bCheck.sisterSite = b.replace("http:", protocol);
    return this;
  },
  getCookieView : function(){
    return $.cookie("viewSite");
  },
  checkView : function(){
    if (b2bCheck.viewSite == null) b2bCheck.viewSite = b2bCheck.getCookieView();
    return b2bCheck.viewSite;
  },
  setSister : function (){
    var pubsid = $.cookie("pubsid", { path:'/' });
    if ( pubsid == null){
      $.getJSON(b2bCheck.sisterSite + "/check-id/?callback=?",
        function(data) {
          $.cookie("pubsid", data.sid, { path:'/' } );
      });
    }
  },
  checkSister : function(){
    if ( b2bCheck.checkView() == null ){

      if ( location.href.indexOf('hammernutrition.com/events') == -1 && location.href.indexOf('hammernutrition.com/calendar') == -1 ){
        $.ajax({
          url : b2bCheck.sisterSite + "/check-id/?callback=?",
          data: {s:"true"},
          dataType : "jsonp",
          success : function(data){
            if ( typeof data.sid !== 'undefined' && data.sid != null ) {
              $.getJSON(b2bCheck.thisSite + "/check-id/?callback=?",
                function(ldata) {
                  if ( data.sid == ldata.sid ){
                    b2bCheck.dialogChoice();
                  }
              });
            }
          },
          statusCode: {
            503: function() {
              /** do nothing -- can't contact remote server **/
            }
          }
        });
      }

    }
    else if ( b2bCheck.checkView() == 'dealer' ){
      b2bCheck.dialogChoice();
    }
  },
  dialogChoice : function(){
    var diag = $("#setSiteChoice");
    if ( ! diag.length ){
      /** TODO this can be hard coded on the appropriate site so disclaimer can change just make sure the id is the same **/
      diag = $("<div id='setSiteChoice' style='text-align:left;'><p><span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 20px 0;'></span>It appears that you are logged in to the Dealer Site.  Would you like to return there? Or stay on the Consumer Site.</p></div>");
      $("body").append(diag);
    } else {
      diag.dialog( "destroy" );
    }

    diag.dialog({
      resizable: true,
      dialogClass : "alert",
      height:180,
      width:400,
      title : "Did You Mean To Come Here?",
      modal: true,
      buttons: {
        "Return To Dealer Site": function() {
          $.cookie("viewSite","dealer", { path:'/' });
          $( this ).dialog( "close" );
          document.location.href=b2bCheck.sisterSite;
        },
        "Stay On Consumer Site": function() {
          $.cookie("viewSite","public", { path:'/' });
          $( this ).dialog( "close" );
        }
      }
    });
  }

};

