/*
INCLUDES
**********************************************/
// specify which javascripts to include on the webpage
//js('/files/javascript/SCRIPTNAME.js');
js('/files/javascript/jquery.mousewheel.min.js');
js('/files/javascript/jScrollPane_techniwrite_specialized.js');
js('/files/javascript/jquery.cycle.all.min.js');

var isResizing, isResizing2;



(function($,sr){
  // debouncing function from John Hann
  // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
  var debounce = function (func, threshold, execAsap) {
      var timeout;
 
      return function debounced () {
          var obj = this, args = arguments;
          function delayed () {
              if (!execAsap)
                  func.apply(obj, args);
              timeout = null; 
          };
 
          if (timeout)
              clearTimeout(timeout);
          else if (execAsap)
              func.apply(obj, args);
 
          timeout = setTimeout(delayed, threshold || 100); 
      };
  }
	// smartresize 
	jQuery.fn[sr] = function(fn){  return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };
 
})(jQuery,'smartresize');


/*
ON LOAD
*********************************************/
// jQuery 'onReady' script - runs when the document have been loaded, but before images
$(function(){
	validateForms();
	toggleMainMenu();
	toggleAreaMenu();
	activateAjaxShopping();
	autocompletePostalCode();
	productlistImage();
	postPolls();
	quizWizzard();
	tracktrace();
	feedReader();
	changeProductImage();
	flashLoad();
	openPopups();
	showInlineHelp();
	//variantSelection();

	
	// resize the jScrollPane as the window resizes
	setContainerHeight();
	// it seems like you need to call this twice to get consistantly correct results cross browser...
	setContainerHeight();
	
	// position the footer correctly on resize and body load 
	//positionFooter();
	
	
	// newsletter signup and unsubscribe
	$('#newslettersignupform').ajaxForm({
		resetForm: true,
		success: function(){
			$('#newslettersignedup').fadeIn().siblings('span').fadeOut();
		}
		
	});
	$('#newsletterunsubscribe').click(function(){
		var strEmail = $('#newslettersignupemail').val();
		$('#newsletterunsubscribeemail').val(strEmail);
		
		$('#newsletterunsubscribeform').ajaxSubmit({
			resetForm: true,
			success: function(){
				$('#newslettersignupform').get(0).reset();
				$('#newsletterunsubscribed').fadeIn().siblings('span').fadeOut();
			}
		})
	});
	
	
	// rotate the quotes
	$('.quoteRotation').cycle({
		cleartypeNoBg: true
	});
	
});

	$(window).smartresize(function(){
		setContainerHeight();
  	//positionFooter();
  });

// setting and resizing the scrollpane
var setContainerHeight = function()
{
	// IE triggers the onResize event internally when you do the stuff in this function
	// so make sure we don't enter an infinite loop and crash the browser
	if (!isResizing) { 
		isResizing = true;
		$w = $('#lqPage');
		$c = $('#lqPagecontainer');
		var p = (parseInt($c.css('paddingLeft')) || 0) + (parseInt($c.css('paddingRight')) || 0);
		$('#lqPage>.jScrollPaneContainer').css({'height': $w.height() + 'px', 'width': $w.width() + 'px'});
		$c.css({'height': ($w.height()-p) + 'px', 'width': ($w.width() - p) + 'px', 'overflow':'auto'});
		$c.jScrollPane({
			scrollbarWidth:8,
			scrollbarMargin:25,
			showArrows:false
		});
		isResizing = false;	
	}
}

// position and reposition the footer when rezising
var positionFooter = function(){
	if (!isResizing2){
		isResizing2 = true;
		$footer = $('#lqFooter');
		
		var intDocHeight = $(document).height();
		var intContentHeight = $('#lqHeader').height() + $('#lqBody').height();
		var intFooterHeight = $footer.outerHeight();
		
		// if footer starts to jump, disable debugging and samples
		
		if ($footer.css('position') == 'static' && (intDocHeight - (intFooterHeight) > intContentHeight)){
			$('#lqFooter').css('position','fixed');
		}
		else if ($footer.css('position') == 'fixed' && (intDocHeight - intFooterHeight) <= intContentHeight){
			$('#lqFooter').css('position','static');
		}
		
		isResizing2 = false;
	}
}

