/*
 * 	Snapple Page 1.0 - jQuery plugin
 *	written by Mike Fey (Hello Monday)	
 *	http://hellomonday.com
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 *  Controls opening, closing, and functionality
 *  of all footer pages for snapple.com
 */
 (function($) {

  var pageToLoad = '';
  var pageData;
  var curBrowser = navigator.userAgent.toLowerCase();
  
  var methods = {
    init: function(options) {

      var defaults = {

      }

      var options = $.extend(defaults, options);

      return this.each(function() {
        var mainDiv = $(this);

        $('#footerButtons li').each(function() {
          if ($(this).attr('class').indexOf('footerLinkExternal') == -1) {
            $(this).bind('click', footerButtonClick);
          }
        });
      });
       
			//sets the proper hash when a button in the footer is clicked
      function footerButtonClick() {
        var nAd = $(this).attr('id');
        $.address.value('/page/' + nAd);
        return false;
      }

    },
    
		//finds the proper page to load based on a string
		//from the hash mark
    loadPage: function(ad) {
      $('#thumbHoverInfo').remove();
      $('#footerButtons li').each(function() {
        if ($(this).attr('id') == ad) {
          pageToLoad = $(this).find('a').attr('href');
        }
      });

      getPage();
    }
  };
  
	//loads the correct page via AJAX
  function getPage() {
    if ($('#mainLoader').length == 0) {
      $('body').append('<div id="mainLoader"></div>');
    }

    $('#moduleOverlay').show();
    $('#moduleOverlay').css({
      opacity: 0.8
    });

    positionLoader();

    $.ajaxSetup({
      cache: false
    });
    $.ajax({
      type: "GET",
      url: pageToLoad,
      success: function(data) {
        pageData = $(data).find('#genericPageContent').html();
        setUpPageContent();
        $('#moduleOverlay').css({
          opacity: 0.0
        });
      }
    });
  }
  
	//positions the preloader
  function positionLoader() {
    var tpos = ($(window).height() / 2) - 16;
    if ($(window).height() < 945) {
      tpos = (945 / 2) - 16;
    }

    var lpos = ($(window).width() / 2) - 32;
    if ($(window).width() < 1210) {
      lpos = (1210 / 2) - 32;
    }
    $('#mainLoader').css({
      'top': tpos + 'px',
      'left': lpos + 'px'
    });
  }
  
	//sets up and positions the loaded page content
  function setUpPageContent() {
    var trackBgColor = 'rgba(0, 0, 0, 0.7)';
    if (curBrowser.indexOf('msie 8.0') != -1) {
      trackBgColor = '#333';
    }

    $('#pageContent').html(pageData);

    $('#genericPageData').jScrollPane({
      draggerColor: '#fff',
      draggerThickness: 8,
      trackColor: trackBgColor,
      verticalDragMinHeight: 100,
      verticalDragMaxHeight: 100,
      autoReinitialise: true,
      draggerShadow: 'true'
    });

    $('#pageContent').css({
      opacity: 0.0
    });
    $('#pageContent').stop(true, true).animate({
      opacity: 1.0
    },
    {
      duration: 500,
      specialEasing: {
        opacity: 'easeOutSine'
      }
    });

    var cIs = $('#gPageImage img').attr('src');

    if (curBrowser.indexOf('msie 8.0') == -1 && curBrowser.indexOf('msie 7.0') == -1) {
      $('#gPageImage').css({
        opacity: 0.0
      });
    } else {
      $('#gPageImage').hide();
    }

    $('#mainLoader').remove();

    var img = new Image();
    $(img).load(function() {
      setTimeout(fadeInImage, 500);
    }).error(function() {

      }).attr('src', cIs);


    setTimeout(function() {

      },
    500);


    if ($('.siteMapPageLink').length > 0) {
      setupSiteMap();
    }
  }
  
	//fades in the page image
  function fadeInImage() {
    positionGenericPageImage();

    if (curBrowser.indexOf('msie 8.0') == -1 && curBrowser.indexOf('msie 7.0') == -1) {
      $('#gPageImage').stop(true, true).animate({
        opacity: 1.0
      },
      {
        duration: 500,
        specialEasing: {
          opacity: 'easeOutSine'
        }
      });


    } else {
      $('#gPageImage').show();
    }
  }
  
	//sets up the site map page
  function setupSiteMap() {
    $('.siteMapPageLink').bind('click', smplClick);
    $('.siteMapProductLink').bind('click', smtagClick);
    $('.siteMapFlavorLink').bind('click', smtagClick);
  }
  
	//sets the proper address when a link in the site map is clicked
  function smplClick(e) {
  	if ($(this).attr('class').indexOf('siteMapPageLinkExternal') == -1) {
      var cid = $(this).attr('id');
      var nad = cid.substr(4, cid.length);
      $.address.value('/page/' + nad);
      return false;
    }
  }
  
	//sets the proper address when a tag button link in the site map is clicked 
  function smtagClick(e) {
    var cid = $(this).attr('id');
    var nad = cid.substr(4, cid.length);
    $.address.value('/products/sort/' + nad);
    return false;
  }
  
	//positions the page image 
  function positionGenericPageImage() {
    var lpPosL = (1220 - $('#gPageImage img').attr('width')) + $('#pageContentWrapper').position().left;
    if ($('#gPageImage img').attr('width') == undefined) {
      var lpPosL = (1220 - $('#gPageImage img').width()) + $('#pageContentWrapper').position().left;
    }
    var lpPosT = $('footer').position().top - $('#gPageImage img').attr('height') - 85;
    if ($('#gPageImage img').attr('height') == undefined) {
      var lpPosT = $('footer').position().top - $('#gPageImage img').height() - 85;
    }

    $('#gPageImage').css({
      'left': lpPosL + 'px',
      'top': lpPosT + 'px'
    });
  }

  //calls the proper method based on the string passed 
  $.fn.snapplePage = function(method) {
    if (methods[method]) {
      return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
    } else if (typeof method === 'object' || !method) {
      return methods.init.apply(this, arguments);
    } else {
      $.error('Method ' + method + ' does not exist on snapplePage');
    }
  };

})(jQuery);
