jQuery.noConflict();

jQuery(document).ready(function($) {
	// Superscript all registration marks
	$('body *').replaceText(/\u00ae/gi, '<sup>&reg;<\/sup>');
	
	$('#signInBtn').click(function(){
		$('table.mod_login').css('display', $('table.mod_login').css('display')=='none'?'block':'none');
	});
	
	var ch = $('#content').height();
	var rh = $('#right').height()
	if(ch != 0 && rh != 0 && ch < rh) {
		$('#content').height(rh);
	}
	
	// First make sure we are not in print mode or on MyStik pages (richness hack)
	if($('.menu').css('display')!='none' && $('.boxShadow').css('richness')!='67') {
		// Box Shadow effect for  Internet Explorer
		$('.ie6 .boxShadow, .ie7 .boxShadow, .ie8 .boxShadow').boxShadow(0, 5, 7, '#003d60');	
		$('.ie6 .moduletable_intro, .ie7 .moduletable_intro, .ie8 .moduletable_intro').boxShadow(0, 5, 7, '#003d60');	
		$('.moduletable_various').boxShadow(0, 5, 7, '#003d60');	
		$('.ie6 .moduletable_product_categories, .ie7 .moduletable_product_categories, .ie8 .moduletable_product_categories').boxShadow(0, 5, 7, '#003d60');	
		$('.ie6 .moduletable_cart, .ie7 .moduletable_cart, .ie8 .moduletable_cart').boxShadow(0, 5, 7, '#003d60');	
		
		// Cross-browser hover effect for navigation
		jQuery('#navigation li').each(function(element){
			jQuery(this).mouseenter(function(){
				jQuery(this).addClass('hover');
				if(jQuery('li', this).length>0) {
					jQuery('#payoff, .search').css('display', 'none');
				}
			});
			jQuery(this).mouseleave(function(){
				jQuery(this).removeClass('hover');
				if(jQuery('li', this).length>0) {
					jQuery('#payoff, .search').css('display', 'block');
				}							
			});
		});
	}
	
	// Blog categories collapsable
	$('.widget_categories .module-content > ul > li').each(function(i, el) {
		if ( $('li', el).length > 0 ) {
			$(el).addClass('superCategory');
			if(!$(el).hasClass('current-cat-parent')) {
				$('.children', el).hide();
			} else {
				$(el).addClass('open');	
			}
			$('a', el).first().click(function(e){
				e.preventDefault();
				if($(el).hasClass('open')) {
					$(el).removeClass('open');
				} else {
					$(el).addClass('open');				
				}
				$('.children', el).slideToggle();
			});
		};
	});
	
	$('.widget_categories .module-content').css('visibility', 'visible');
	
	
	
	// quadratic crop and resize for images (used on blog)
	$('.qcrop').each(function(i, el) {
	
		// get first <img> element that is not empty
		var img = false;
		$('img', el).each(function(i, iel) {
			if ($(iel).attr('src') != '') {
				img = $(iel);
				return;
			}
		});
		
		if(img) {
			// set some css on the wrapper
			$(el).css('position', 'relative');
			$(el).css('overflow', 'hidden');
			
			// set some css on the image
			img.css('position', 'absolute');
			img.css('top', '50%');
			img.css('left', '50%');
					
			// Get width and height of the wrapper
			var wrapWidth = $(el).width();
			var wrapHeight = $(el).height();
			
			// Get original width and height of the image
			var imgWidth = img.width();
			var imgHeight = img.height();
	
			// Calculate new dimensions and position for the image
			if(imgHeight >= imgWidth) {
				img.css('width', wrapWidth + 'px');
				img.css('marginLeft', '-' + (wrapWidth / 2) + 'px');
				var scalefactor = wrapWidth / imgWidth;
				
				img.css('height', scalefactor * imgHeight);
				img.css('marginTop', -((scalefactor * imgHeight) / 2));
			} else {
				img.css('height', wrapHeight + 'px');
				img.css('marginTop', '-' + (wrapHeight / 2) + 'px');
				var scalefactor = wrapHeight / imgHeight;
				
				img.css('width', scalefactor * imgWidth);
				img.css('marginLeft', -((scalefactor * imgWidth) / 2));
			}
			
			// show the wrapper
			$(el).animate({opacity: 1}, 500);
		}
		
	});	
	
	// make the shop cart follow the window scroll 
	var el = $('.moduletable_cart');
	if(el.length != 0) {
		var eloffsetorg =  el.offset().top;
		$(window).scroll(function(){
			var eloffset = el.offset();
			var windowpos = $(window).scrollTop();
			
			if(windowpos<eloffset.top) {
				var finaldestination = eloffsetorg;
			} else {
				var finaldestination = windowpos;
			}
			if(jQuery.support.htmlSerialize) {
				el.stop().animate({'top':finaldestination},200);
			} else {
				el.stop().animate({'top':finaldestination-eloffsetorg},200);
			}
		});
	}
});	
