/* Stop IE flicker */
if ($.browser.msie == true) {
document.execCommand('BackgroundImageCache', false, true);
}
/* On ready functions */
$(document).ready(function() {
$("#registerform div.large-button-arrow a").click(function(event){
    if ($("#accept-terms-container").length > 0) {
        if ( $("#accept-terms").is(':checked') ) {
            $("#registerform").get(0).submit();
        } else {
            var error = $("#accept-terms-container span.error").text();
            alert(error);
            return false;
        }
    } else {
        $("#registerform").get(0).submit();
    }
});

if($("#sticky-box").size() > 0) {
$("#sticky-box").stickyBox();
}
$('#top-nav').jdMenu();
$(document).bind('click', function() {
	$('#top-nav ul:visible').jdMenuHide();
});
sideNav();
$('a.link-buy-button').click(function() {
this.href = this.href.replace(new RegExp(/sourceok=0/),"sourceok=1");
});
$('a.link-quick-facts').click(function() {
var refElement;
if($(this).parent().parent().is(".product")) {
 	refElement = $(this).parent().parent();
} else if($(this).parent().is("td")) {
	refElement = $(this);
} else {
	refElement = $("div.product",$(this).parent().parent().parent());
}
$(this).loadBox({ref: refElement, trg: $("#popup-quick-facts")});
return false;
});
$('a.link-listing-sorting').click(function() {
var url = $('select.sorting-select').val();
if(document.location.href != url) {
document.location.href = url;
}
return false
});
// style checkboxes.
$().cssCheckbox();
//$("form select").selectBox({css: 'select'});

/* tool tip*/
$("a.tool-tip").toolTip();
});
var sideNav = function() {
$("#side-nav a").click(function(e) {e.stopPropagation();});
$("#side-nav li.has-child").toggle(function(e) {
if(!this.loaded) {
var url = $('a',this)[0].rel;
var oThis = this;
$(this).append('<ul class="pcat show"></ul>');
$('ul',this).load(url,{},function() {
$(this).show();
sideNav();
});
this.loaded = true;
} else {
$("ul",this).show();
}
}, function(e) {
$("ul",this).hide();
});
}

/* plugin dimensions */
jQuery.fn.outerHeight = function() {
	return this.get(0) == window || this.get(0) == document ?
		this.height() :
		this.get(0).offsetHeight;
};
jQuery.fn.outerWidth = function() {
	return this.get(0) == window || this.get(0) == document ?
		this.width() :
		this.get(0).offsetWidth;
};
jQuery.fn.offset = function(refElem) {
if (!this[0]) throw 'jQuery.fn.offset requires an element.';

refElem = (refElem) ? jQuery(refElem)[0] : null;
var x = 0, y = 0, elem = this[0], parent = this[0], sl = 0, st = 0;
do {
if (parent.tagName == 'BODY' || parent.tagName == 'HTML') {
// Safari and IE don't add margin for static and relative
if ((jQuery.browser.safari || jQuery.browser.msie) && jQuery.css(parent, 'position') != 'absolute') {
x += parseInt(jQuery.css(parent, 'marginLeft')) || 0;
y += parseInt(jQuery.css(parent, 'marginTop'))  || 0;
}
break;
}

x += parent.offsetLeft || 0;
y += parent.offsetTop  || 0;

// Mozilla and IE do not add the border
if (jQuery.browser.mozilla || jQuery.browser.msie) {
x += parseInt(jQuery.css(parent, 'borderLeftWidth')) || 0;
y += parseInt(jQuery.css(parent, 'borderTopWidth'))  || 0;
}

// Need to get scroll offsets in-between offsetParents
var op = parent.offsetParent;
do {
sl += parent.scrollLeft || 0;
st += parent.scrollTop  || 0;
parent = parent.parentNode;
} while (parent != op);
} while (parent);

if (refElem) { // Get the relative offset
var offset = jQuery(refElem).offset();
x  = x  - offset.left;
y  = y  - offset.top;
sl = sl - offset.scrollLeft;
st = st - offset.scrollTop;
}

// Safari and Opera do not add the border for the element
if (jQuery.browser.safari || jQuery.browser.opera) {
x += parseInt(jQuery.css(elem, 'borderLeftWidth')) || 0;
y += parseInt(jQuery.css(elem, 'borderTopWidth'))  || 0;
}

return {
top:  y - st,
left: x - sl,
width:  elem.offsetWidth,
height: elem.offsetHeight,
borderTop:  parseInt(jQuery.css(elem, 'borderTopWidth'))  || 0,
borderLeft: parseInt(jQuery.css(elem, 'borderLeftWidth')) || 0,
marginTop:  parseInt(jQuery.css(elem, 'marginTopWidth'))  || 0,
marginLeft: parseInt(jQuery.css(elem, 'marginLeftWidth')) || 0,
scrollTop:  st,
scrollLeft: sl,
pageYOffset: window.pageYOffset || document.documentElement.scrollTop  || document.body.scrollTop  || 0,
pageXOffset: window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0
};
};
/* plugin tooltip */
jQuery.fn.toolTip = function(o) {
return this.each(function() {
var tp = $("#popup-tool-tip");
var jq = $(this);
var title;
jq.hover(function(e) {
title = this.title;
this.title = "";
var offset = jq.offset();
var cssLeft = (offset.left);
var cssTop = (offset.top + offset.height);
if( cssLeft + parseInt(tp.width()) > parseInt($('BODY').width())) {
cssLeft = offset.left - (parseInt(tp.width()));
}
tp.empty().css({left: cssLeft + 'px', top: cssTop + 'px'}).html(title).show();

},function() {
this.title = title;
tp.hide();
});
});
};
/* plugin loadbox */
jQuery.fn.loadBox = function(o) {
return this.each(function() {

var ref = o.ref;
var trg = o.trg;
var align = o.align;
var hrf = this.href;
var offsetHeight = o.offsetHeight;

if(ref == null) {
var offset = $("HTML").offset();
var left = (offset.width - $(trg).width()) / 2;
var top = (offset.pageYOffset + offsetHeight);
} else {

var offset = ref.offset('BODY');
var left = (offset.left + offset.width);
var width = parseInt($(trg).width());
if( left + width > parseInt($('BODY').width())) {
left = offset.left - width;
}
var top = offset.top;

if(align == "cw") {
left = ($('BODY').width() - $(trg).width()) / 2;
}
}
$(trg).hide().css({top: top + 'px', left:  left + 'px'});
$(trg).load(hrf,{},function() {
var target = this;
$(trg).fadeIn('fast');
$('a.link-close',trg).click(function() {
$(trg).hide();
return false
});

// submit form via ajax call.
$("form",target).submit(function(e) {

$("div.sent",target).hide();
$("div.sending",target).show();

e.preventDefault();
var params = $(this).serializeForm();
var url = this.action;

$.ajax({
  type: "POST",
  url: url,
  data: params,
  success: function(data){
$("div.sending",target).hide();
$("div.sent",target).show();
  }
});

return false;
});
$("div.book-button",this).click(function(e) {
$("form",target).submit();
return false;
});
});
});
};
jQuery.fn.serializeForm = function() {
var hash = {};
$(":input",this).each(function() {
hash[this.name] = escape($(this).val());
});
return hash;

};
/* plugin selectbox */
jQuery.fn.selectBox = function(o) {
return this.each(function() {
if(!jQuery(this).is(".no-css")) {

var oThis = this;
var oSelected = null;
var state = 'closed';
var iSelectedIndex = 0;
var paddingRight = 20;
var oSelectOffset = jQuery(this).offset('BODY');
var selectWidth = jQuery(this).width();
var selectHeight = jQuery(this).height();
jQuery(this).hide().css({top:'-1000px',left:'-1000px',position: 'absolute'});
jQuery(this).after('<div></div>');//
var oContainer = jQuery(this).next().addClass(o.css);

// append html inside the container
oContainer.append('<div><p></p></div><ul></ul>');
// assign toggle action

jQuery("div",oContainer).css({width: (selectWidth) + 'px', height: selectHeight+ 'px'}).toggle(function() {
positionListElement();
jQuery("ul",oContainer).slideDown('fast');
jQuery("li",oContainer).removeClass('active');
if(oSelected == null) {
jQuery("li:eq(0)",oContainer).addClass('active');
oSelected = jQuery("li:eq(0)",oContainer);
} else {
oSelected.addClass('active');
}
state = 'opened';
},function() {
jQuery("ul",oContainer).slideUp('fast');
state = 'closed';
})
jQuery().click(function() {
if(state == 'opened') {
jQuery("div",oContainer).trigger("click");
}
});

jQuery('option',this).each(function(i) {
var o = this;
jQuery(this).click(function() {
this.selected = true;
});
jQuery("ul",oContainer).append('<li>' +  jQuery(this).text() + '</li>');
jQuery("li:eq(" + i + ")",oContainer).click(function() {
jQuery(o).click();
jQuery("div",oContainer).trigger("click");
jQuery("div p",oContainer).html(jQuery(o).text())
oSelected = $(this)
}).mouseover(function() {
jQuery(this).addClass('select-active');
/*setFocus();*/
}).mouseout(function() {
jQuery(this).removeClass('select-active');
/*setBlur();*/
});
});
var ulSelectListWidth = jQuery("ul",oContainer).offset().width;
jQuery("ul",oContainer).hide();
// set ul list position
var positionListElement = function() {
var oOffset = jQuery("div",oContainer).offset('BODY');
var iWidth = (selectWidth < ulSelectListWidth) ? ulSelectListWidth :  selectWidth;
jQuery("ul",oContainer).css({
left: oOffset.left  + 'px',
top: oOffset.top + parseInt(oOffset.height) + 'px',
width: iWidth + 'px'
});
};
var setValue = function() {
var val = jQuery("option:eq(" + jQuery(oThis)[0].selectedIndex +")",oThis).text();
jQuery("div p",oContainer).html('').html(val);
oSelected = jQuery("li:eq(" + jQuery(oThis)[0].selectedIndex + ")",oContainer);
jQuery("li",oContainer).removeClass('select-active');
oSelected.addClass('select-active');
};
var getKey = function(e) {
var key = '';
switch(e.keyCode) {
case 13: // return
key = 'enter';
break;
case 27:  // esc
key = 'escape';
break;
}
return key;
};
setValue();
jQuery(window).resize(positionListElement);
}
});
};
/* plugin stickbox*/
jQuery.fn.stickyBox = function(o) {

return this.each(function() {

var oContext = this;
var oNav = 	$("a.sticky-nav",oContext);
var aPlayList = [];
var iPlaySlide = 0;
var iInterval = 5;
var oTimeout;
var playerPause = false;
var iInterval = 50;
var iFadeSpeed = 1400;
var slideCounterLimit = 100;
var slideCounter = slideCounterLimit;
var aSlideList = [];
var oActiveSlide = false;

oNav.each(function(i) {
aPlayList.push(this);
aSlideList.push(this.rel);
this.iSlide = i+1;
$(this).mouseover(function(e) {
setActiveSlide(this.iSlide);
showSlide(this);
playerPause = true;
}).mouseout(function() {
playerPause = false;
});
});

for(var i=0; i < aSlideList.length; i++) {
$(aSlideList[i]).mouseover(function() {
playerPause = true;
}).mouseout(function() {
playerPause = false;
});
if(i > 0) { $(aSlideList[i]).hide(); }
}

function showSlide(o) {

oNav.removeClass('hover');
$(o).addClass('hover');

var slideName = o.rel;
if(oActiveSlide) {
$(oActiveSlide).hide()
}

$(slideName + " img").show(); //fadeIn(iFadeSpeed);
$(slideName).show(); //('slow');

oActiveSlide = $(slideName);
}

function setActiveSlide(iSlide) {
iPlaySlide = iSlide;
if(iPlaySlide > (aPlayList.length - 1)) {
iPlaySlide = 0;
}
}

function getActiveSlide() {
return iPlaySlide;

}

function slideController() {

if(!playerPause && slideCounter >= slideCounterLimit) {
showSlide(aPlayList[getActiveSlide()]);

setActiveSlide(getActiveSlide() + 1);

slideCounter = 0;
} else if(playerPause) {

} else {
slideCounter++;
}
}
setInterval(slideController,iInterval);

//$(aPlayList[getActiveSlide()]);
});
};
/* plugin checkbox */
jQuery.fn.cssCheckbox = function () {
jQuery("input[@type='checkbox'] + label", this)

.each( function(){
if ( jQuery(this).prev()[0].checked )
jQuery(this).addClass("checked");
})
.hover(
function() { jQuery(this).addClass("over"); },
function() { jQuery(this).removeClass("over"); }
)
.click( function() {
jQuery(this)
.toggleClass("checked")
.prev()[0].checked = !jQuery(this).prev()[0].checked;
})
.prev().hide();
};
/*
 * jdMenu 1.1
 *
 * Copyright (c) 2006,2007 Jonathan Sharp (http://jdsharp.us)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://jdsharp.us/
 *
 * Built upon jQuery (http://jquery.com)
 * This also requires the jQuery dimensions plugin
 *
 * Date: 2006-12-29
 */
(function($){
	// This method can be removed once it shows up in core
	if (!$.fn.ancestorsUntil) {
		$.fn.ancestorsUntil = function(match) {
			var a = [];
			$(this[0]).ancestors().each(function() {
				a.push(this);
				return !$(this).is(match);
			});
			return this.pushStack(a, arguments);
		};
	}

	var IFRAME 	= $.browser.msie;
	var cssr 	= 'jd_menu_flag_root';
	var cssb	= 'jd_menu_bar_hover'; //_toolbar';
	var cssh	= 'jd_menu_hover';

	// Public methods
	$.fn.jdMenu = function() {
		return this.each(function() {
			$(this).addClass(cssr);
			addEvents(this);
		});
	};

	$.fn.jdMenuHide = function() {
		return this.each(function() {
			hide(this);
		});
	};

	// Private methods
	function addEvents(ul) {
		$('> li', ul).hover(hoverOver,hoverOut).bind('click', click);
	};

	function removeEvents(ul) {
		$('> li', ul).unbind('mouseover').unbind('click', click);
	};

	function hoverOver() {
		var c = $(this).parent().is('.' + cssr) ? cssb : cssh;
		$(this).addClass(c).find('> a').addClass(c);

		if (this.$timer) {
			clearTimeout(this.$timer);
		}
		this.$size = $('> ul', this).size();
		if (this.$size > 0) {
			var ul = $('> ul', this)[0];
			if (!$(ul).is(':visible')) {
				this.$timer = setTimeout(function() {
					if (!$(ul).is(':visible')) {
						show(ul);
					}
				}, 500);
			}
		}
	};

	function hoverOut() {
		$(this)	.removeClass(cssh).removeClass(cssb)
			.find('> a')
				.removeClass(cssh).removeClass(cssb);

		if (this.$timer) {
			clearTimeout(this.$timer);
		}
		if ($(this).is(':visible') && this.$size > 0) {
			var ul = $('> ul', this)[0];
			this.$timer = setTimeout(function() {
				if ($(ul).is(':visible')) {
					hide(ul);
				}
			}, 500);
		}
	};

	function show(ul) {
		// Hide any existing menues at the same level
		$(ul).parent().parent().find('> li > ul:visible').not(ul).each(function() {
			hide(this);
		});
		addEvents(ul);

		var o = $(ul).offset();
		var bt = o.borderTop;
		var bl = o.borderLeft;

		var x = 0, y = 0;
		var li = $(ul).parent();
		if ($(li).ancestorsUntil('ul.' + cssr).filter('li').size() == 0) {
			x = $(li).offset($(li).ancestors('ul.' + cssr)[0]).left;
			y = $(li).outerHeight();
		} else {
			x = $(li).outerWidth() - (3 * bl);
			y = $(li).offset($(li).parent()).top + bt;
		}
		$(ul).css({left: x + 'px', top: y + 'px'}).show();

		if (IFRAME && ($(ul).ancestorsUntil('ul.' + cssr).filter('li').size() > 0)) {
			// TODO Add in the auto declaration?
			var w = $(ul).outerWidth();	// Needs to be before the frame is added
			var h = $(ul).outerHeight();
			if ($('> iframe', ul).size() == 0) {
				$(ul).append('</iframe>').prepend('<iframe style="position: absolute; z-index: -1;">');
			}
			$('> iframe', ul).css({		opacity:		'0',
										left: 			-bl + 'px',
										top: 			-bt + 'px',
										width: 			w + 'px',
										height: 		h + 'px'});
			if (!ul.style.width || ul.$auto) {
				ul.$auto = true;
				$(ul).css({	width: 	w - (bl * 2) + 'px',
							height: h - (bt * 2) + 'px' });
			}
		}
	};

	function hide(ul, recurse) {
		$('> li > ul:visible', ul).each(function(){
			hide(this, false);
		});
		if ($(ul).is('.' + cssr)) {
			return;
		}

		removeEvents(ul);
		$(ul).hide();
		$('> iframe', ul).remove();

		// If true, hide all of our parent menues
		if (recurse == true) {
			$(ul).ancestorsUntil('ul.' + cssr)
					.removeClass(cssh).removeClass(cssb)
				.not('.' + cssr).filter('ul')
					.each(function() {
						hide(this, false)
				});
		}
	};

	function click(e) {
		e.stopPropagation();
		if (this.$timer) {
			clearTimeout(this.$timer);
		}
		if (this.$size > 0) {
			var ul = $('> ul', this)[0];
			if (!$(ul).is(':visible')) {
				show(ul);
			}
		} else {
			var l = $('> a', this).get(0);
			if (l != undefined) {
				if (!l.onclick) {
					window.open(l.href, l.target || '_self');
				} else {
					$(l).click();
				}
			}

			var ul = $(this).parent();
			if (!$(ul).is('.' + cssr)) {
				hide(ul, true);
			}
		}
	};
})(jQuery);

function popupWindow( strWindowURL, strWindowName, intWidth, intHeight, boolCenterWindow, boolScrollBars ) {
	if( boolCenterWindow == 1 ) {
		if(screen.width > 0) {
			var intLeft = ( screen.width / 2 ) - ( intWidth / 2);
		} else {
			var intLeft = 0;
		}
		if(screen.height > 0) {
			var intTop = ( screen.height / 2 ) - ( intHeight / 2);
		} else {
			var intTop = 0;
		}
	} else {
		var intLeft = 0;
		var intTop = 0;
	}
    var strWindowParams = "width=" + intWidth + ",height=" + intHeight + ",left=" + intLeft + ",top=" + intTop + "toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=" + boolScrollBars + ",resizable=0";
	return window.open( strWindowURL, strWindowName, strWindowParams ) ;
}