(function($){
  //trying to position S compass related to the left corner content div
  $(document).ready(function(){
    backgroundPosition();

    $(window).resize(function(){
      backgroundPosition();
    });

    $('.overlay').click(launchDialog);
   });

  function backgroundPosition() {
     $("#compassWrapper").css("background-position", function() {
    	//Background image. if ever changed change this line!!
    	imageWidth = 535;
    	backgroundPos = (($(document).width() / 2) - 500) - (imageWidth / 2);

  		return backgroundPos + "px 100%";
	  });
  }

  //for the overlay dialog for terms and conditions on deals
  function launchDialog() {
    var action  = $(this).attr("href");
    var title   = $(this).attr("title");
    var inlineId   = $(this).attr("id") +"Div";
    var h       = parseInt( $.urlParam('height', action) );   // must be of type int, or the height/width doesn't take
    var w       = parseInt( $.urlParam('width', action) );
    if ( h == 0 ) h = '400';
    if ( w == 0 ) w = '400';

    var opts = {                        // options for dialog
      height: h,
      width: w,
      title: title,
      modal: true                      // always modal to trigger the overlay
    }

    if ( inlineId ) { // display contents of an inline div instead of external page url
      var inlineContent = ($("#" + inlineId).clone()).attr('class','');
      $("#uiDialog").html(inlineContent).dialog(opts);
    } else {
      $("#uiDialog").load(action, function(data){}).dialog(opts);
    }
    return false;
  }

  function closeDialog() {
    $("#uiDialog").dialog('close');
    return false;
  }

/* helper function */
$.urlParam = function(name, url){
  if ( !url || url == "" ) { url = window.location.href; }
  if ( url && url != "" ) {
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(url);
    if (!results || results == "") {
      url = $("#mainNav a:first").attr("href"); // grab the first link in the main nav
      results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(url);
    }
    if ( results && results.length > 1 ) {
      return results[1] || 0;
    }
  }
  return 0;
}
})(window.jQuery);
