$(document).ready(function() {

   $('html').removeClass('no-js');

   getLoginStatus();
  
// login box move-remove for member content
if($('#prompt_login').length){
   $('#prompt_login').prependTo('#right');
   if($('body.member_access_No').length) {
      $('#prompt_login').remove();
   };
 if($('#right #SQ_LOGIN_USERNAME').length == 0) {
      $('#prompt_login').remove();
   };
};
  
  
  // auto add padding to images with align left or right applied
  $('#main-content img[align="left"] ').css('padding','0 10px 10px 0');
  $('#main-content img[align="right"] ').css('padding','0 0 10px 10px');

  if($('#add-this.hide-Yes').length > 0){
    $('#add-this.hide-Yes').remove();
    $('#main-content').css('padding-top','20px');
    $('#right').css('padding-top','20px');
  };
  
  
  
   //Check if right col exists, if not then expand middle col
   if($('#right>div').length < 1){
     $('#right').hide();
     $('#main-content').addClass('wide');
   }
 
   // hide login/register box
   $("#members-close a").click(function() {
      $("#members-wrapper").slideUp("slow");
      return false;
   });

  // hide leftcol if no menu element exist
  if($('#sub-nav ul').length == 0){
     if($('#main-content').hasClass('wide')) {
  $('#main-content').addClass('full-col');
} else {
$('#main-content').addClass('wide');
};
    $('#left').hide();
  };


  $('#members-wrapper').append('<iframe src="/_designs/pages/login" onload="" height="350" width="992" frameborder="0" scrolling="no"/>');

  // improve main-menu spacing
  $("#main-menu > li").addClass("spaced");
  $("#main-menu > li:last").addClass("last");

  // remove dropdown last item border
  $("#main-menu ul").find("li:last").addClass("last");

  // add li.hover class for ie6
  $("#main-menu > li").hover(
    function () {
      $(this).addClass("hover");
    },
    function () {
      $(this).removeClass("hover");
    }
  );

  // add button.hover class for ie6
  $("button").hover(
    function () {
      $(this).addClass("hover");
    },
    function () {
      $(this).removeClass("hover");
    }
  );

  // remove arrow if there are no sub-level items
  $("#sub-nav > ul > li").each(function() {
    if ($("ul", this).length == 0) {
      $("a", this).css("background-image","none");
    }
  });

  //Initialise tabs on the page if found
  iniTabs();

  //Add table row classes
  $("#main-wrapper table").each(function(){
    $(this).find('tr:odd').each(function(){
      $(this).addClass('odd');
    });
  });
  
  //Load shopping cart into right col
  if($('#hide-cart').length < 1){
    loadCart();
  }

  //fix nested uls
  $('#main-content ul ul').each(function(){
    if($(this).parent().css('list-style-type') == 'none'){
       $(this).parent().css({'list-style-type' : '', 'display' : '', 'background-image' : 'none'});
    }
  });

}); /* end ready function*/

//Function for loading the cart into right col
function loadCart(){
  $("#cart-wrapper").load('/_designs/pages/nested-shopping-cart #cart', function(){
    //if($("#cart-wrapper .remove").length < 1){
    //  $('#right').show();
    //  $('#main-content').removeClass('wide').addClass('cart-friendly');
    //}
  });
}

//Initialise tabs on the page if found
function iniTabs(){
  
	 var selectActiveTab = $.cookie('MBactiveTab');
	 if (selectActiveTab) {
	   $('.content-tabs li a[href="'+selectActiveTab+'"]').addClass('selected');
           var hideDiv = $('.content-tabs li a').not('.selected').attr('href');
           $(hideDiv).hide();
	   $(selectActiveTab).show();
	 } else {
                    $('.content-tabs li a').each(function(i){
                        if(i==0){
                          $(this).addClass('selected');
                        }else{
                          $($(this).attr('href')).hide();
                        };
                      });

                };


  $('.content-tabs li a').click(function(){
    $($(this).parent().parent().find('.selected').attr('href')).hide();
    $(this).parent().parent().find('.selected').removeClass('selected');
    $(this).addClass('selected');
    $($(this).attr('href')).show();

	// set  active tab cookie
	var thisId = $(this).attr('href');

  var substr = thisId.split('#');
  thisId = '#' + substr[1];

	$.cookie('MBactiveTab', thisId);

    return false;
  });
}

function justLoggedIn(){
  window.location = location.protocol + '//' + location.host;
}

//Load login and register links into top links
function getLoginStatus(){
  $('#ajax-temp-holder').load('/_designs/pages/login-and-register-links/_nocache #ajax-content', function(){
    $('#top-links ul').append($('#ajax-temp-holder #ajax-content').html());
    $('#ajax-temp-holder').html('');
    // show login/register box
    $("#top-links li.members a").click(function() {
      $("#members-wrapper").slideToggle("slow");
      return false;
    });
  });
}

function memberInit() {
  var thisURL = document.location;
  var newAction = thisURL + '?SQ_ACTION=login';
  $('#members-wrapper form').attr('action', newAction);
};

function calcHeight(theIframeId){
  //find the height of the internal page
  var the_height = document.getElementById(theIframeId).contentWindow.document.body.scrollHeight;
  //change the height of the iframe
  document.getElementById(theIframeId).height = the_height + 40;
}


/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
/******************************************************************/
