$(document).ready(function() {
	    // Do stuff when ready
	    addPromoScroller();
	    promoSelected();
	    enableContentSelection();
	    checkDeepLink();
        openScreen();
	});
	
	function openScreen() {
		$("#content_container").animate({top: '0px'}, {duration:800, complete:fadeInShareButtonAndSwf});
	}
	
	function fadeInShareButtonAndSwf() {
		$("#share").fadeIn();
		$("#snowflake").fadeIn();
	}
	
function promoSelected(){
	$('ul.promo-list li a').click(function(){
		$('a.selected').removeClass('selected');
		$(this).addClass('selected');
	});
};  

function addPromoScroller() {
    var control = $('.pagination');
    // Add a link for each page that scrolls content to that page
    $('ul.promo-list').each(function(i) {
        // Add the link
        var link = $('<a id="page'+(i+1)+'_btn" class="btn" href="./#"></a>');
        control.append(link);
        // Set up click handler
        link.click(function(e) {
            $('a.selectedbtn', control).removeClass('selectedbtn');
            link.addClass('selectedbtn');
            $('#sliding-list-container').animate({marginLeft: i*-201}, 350)
            e.preventDefault();
        });
    });
 	// Make the first link selected
 	$('a:first-child', control).click();
 }
 
 function enableContentSelection() {
 	$('ul.promo-list').children('li').click(function(e) {
		var link = $(this).children('a').attr('href');
        var hash_re = new RegExp(".*\/([^\/]*)\.html$");
        var result = link.match(hash_re);
        if (result) {
          var hash = result[1];
          loadContent(link, true);
          e.preventDefault();
        }
 	});
 }
 
 function checkDeepLink() {
 	if (location.hash != "") {
 		// Select link on left
        var page = location.hash.substring(1);
 		var targetLink = $('[href=' + page + '.html]');
		$('a.selected-promotion').removeClass('selected-promotion');
		targetLink.addClass('selected-promotion');
		
 		loadContent('member/' + page, false);
 	}
 }
			 
function loadContent(hashlink, animate) {
  if (animate) {
    $('#new-sale-content-item').load(hashlink + " #sale-content-item", {}, function(){
      $('#new-sale-content-item #sale-content-item').children().remove().appendTo('#new-sale-content-item');
      $('#new-sale-content-item #sale-content-item').remove();
      //var url = new RegExp(".*\/([^\/]*)\.html$");
      var hash_re = new RegExp(".*\/([^\/]*)\.html$");
      var result = hashlink.match(hash_re);
      if (result) {
          location.hash = result[1];
      }
      $('#sale-content-item').animate({marginTop: -547}, 500, function() {
        $('#sale-content-item').empty();
        $('#sale-content-item').css({marginTop: 0});
        $('#new-sale-content-item').children().remove().appendTo('#sale-content-item');
      });
    });
  } else {
    $('#new-sale-content-item').load(hashlink + " #sale-content-item", {}, function() {
      $('#new-sale-content-item #sale-content-item').children().remove().appendTo('#new-sale-content-item');
      $('#new-sale-content-item #sale-content-item').remove();
      $('#sale-content-item').children().remove();
      $('#new-sale-content-item').children().remove().appendTo('#sale-content-item');
    });
  }
}
		 

