if(typeof jQuery != "undefined") {
	jQuery.noConflict();
	
	jQuery(window).load(
		function() {
			if(jQuery('#gallery div.large').length>0){
				var currImgHeight = 0;
				var currHeight = 0;
				
				currImgHeight = jQuery('#gallery div.large a img').height();
				currHeight = jQuery('#gallery div.large').height();
				
				if(currImgHeight > currHeight){
					jQuery('#gallery div.large').height(currImgHeight);
				}
			}
		}
	);
	
	jQuery(window).ready(function($) {
	
		if($('#gallery div.large').length>0){

			var currImgHeight = 0;
			var currHeight = 0;
			
			$('#gallery div.large').after('<ul class="thumbs" id="navthumbs"></ul>'); 
			
			$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) {
				$(pager).find('li').removeClass('active').filter('li:eq('+currSlideIndex+')').addClass('active');
			};
					
			$('#gallery div.large').cycle({
				//timeout: 3000,
				timeout: 0,
				pager:  '#navthumbs',
				cssBefore:{ display: 'none'},
				pagerAnchorBuilder: function(idx, slide) {
					var src = slide.href;
					src = src.replace('&w=936&zc=0&q=75','&w=144&h=89&zc=1&q=75');
					return '<li><a href="#"><img src="' + src + '" width="144" height="89" /></a></li>';
				},
				before:	function(currSlideElement, nextSlideElement, options, forwardFlag) {
					//$('#gallery div.large').height($(nextSlideElement).height());
					currImgHeight = $(nextSlideElement).height();
					currHeight = $('#gallery div.large').height();
					if(currImgHeight > currHeight){
						$('#gallery div.large').height(currImgHeight);
					}
					
				},
				after: function(currSlideElement, nextSlideElement, options, forwardFlag){
					currImgHeight = $('#gallery div.large a img').height();
					currHeight = $('#gallery div.large').height();
					if(currImgHeight > currHeight){
						$('#gallery div.large').height(currImgHeight);
					}
				}
				,	containerResize: 0
				
			});
			
			currImgHeight = $('#gallery div.large a img').height();
			currHeight = $('#gallery div.large').height();
			if(currImgHeight > currHeight){
				$('#gallery div.large').height(currImgHeight);
			}

						
			$('#gallery .large a').lightBox();
		}
		
		//It fix the width problem in IE7 for the select box with fixed width
		if($('#material-swatch select').length>0){
			$('#material-swatch select').bind('focus mouseenter', function(event){
				var $$	= $(event.target).closest('select'),
					width;
					
				$$.removeClass('clicked');
					
				if($$.css("width") != "auto") {
					width = $$.width();
					
					$$.data("origWidth", $$.css("width"))
						   .css("width", "auto");
						   
					// if the width is now less than before then undo 
					if($$.width() < width) {
						$$.css("width", $$.data("origWidth"));
					}
					
				}
			}).bind('click', function(event){
				var $$	= $(event.target).closest('select');
				$$.addClass('clicked');
			}).bind('blur change', function(event) {
				var $$	= $(event.target).closest('select');
				$$.css("width", $$.data("origWidth"));
			}).bind('mouseout', function(event){
				var $$	= $(event.target).closest('select');
				if(!$$.is('.clicked')) {
					$$.css("width", $$.data("origWidth"));
				}
			});
			
		}
		
	});
	
	
	jQuery(document).ready(function($) {
	
		if($('#gallery div.large').length>0){
			var currImgHeight = 0;
			var currHeight = 0;
			
			currImgHeight = $('#gallery div.large a img').height();
			currHeight = $('#gallery div.large').height();
			if(currImgHeight > currHeight){
				$('#gallery div.large').height(currImgHeight);
			}
		}
	
		var $body, $nav, $content, $product,
			hash, $hash, $hashLink,
			$gallery, $galleryImage, galleryImageFading;

		/**
		* General
		*/
		$body		= $('body').addClass('jquery');
		$nav		= $('#nav');
		$content	= $('#content');
		$product	= $('#product');
		
		
		hash = window.location.hash;
		if(hash.length > 0) {
			$hash = $(hash);			
			if($hash.length > 0) {
				$hash.show();
			}
		}
			
		/**
		* @section Default input value replacement
		* --------------------------------------------------------------------------------------------------
		*/
		$(':text').each(function(i, element){
			var $$, value;

			$$		= $(this);
			value	= $$.attr('title');
			$$.removeAttr('title');
			
			if($$.is('.default') && $$.val().length > 0) {
				value = $$.val();
			}
			
			$$.data('default', value);

		}).bind('focus', function(event){
			var $$, value;

			$$		= $(this);
			value	= $$.val();

			if(value === $$.data('default')) {
				$$.val('');
			}
		}).bind('blur', function(event){
			var $$, value;

			$$		= $(this);
			value	= $$.val();

			if(value === '') {
				$$.val($$.data('default'));
			}
		});
		
			
		/**
		* @section Input default text
		* @desc Moves the associated label text inside the input as "default" value
		* @uses labelify()
		* @see http://www.kryogenix.org/code/browser/labelify/
		* --------------------------------------------------------------------------------------------------
		*/
		if($.fn.labelify) {
			$('#search, #newsletter').find('input:not(:submit,:hidden)').labelify({
				text: 'label'
			});
			$('body.cart #content-primary input:not(:submit,:hidden)').labelify();
		}
		
			
		/**
		* @section Carousel
		* @uses carousel()
		* @uses jcarousellite()
		* @see http://www.gmarwaha.com/jquery/jcarousellite/
		* --------------------------------------------------------------------------------------------------
		*/
		if($.fn.carousel && $.fn.jCarouselLite) {
			$('.scroll ul').carousel({
				'auto':			false, // 3000
				'circular':		true,
				'speed':		1500,
				'visible':		4,
				'scroll':		4,
				'btnPrev':		'#sLeft',
				'btnNext':		'#sRight'
			});
		}
		
			
		/**
		* @section Fitted / Clickable boxes
		* @uses fitted()
		* @see http://www.trovster.com/lab/plugins/fitted/
		* --------------------------------------------------------------------------------------------------
		*/
		if($.fn.fitted) {
			$('div.featured:not(.scroll), ul.categories, ul.products').find('li:not(.advert)').fitted({
				'class': {
					'container':	'fitted',
					'hover':		'hover'
				},
				'title':	true,
				'status':	true
			}).each(function(i){
				var $$, $popup, $inner, text;
				
				$$		= $(this);
				text	= $$.attr('title');

				if(text.length === 0) {
					text = 'Product Info';
				}
				$popup	= $('<span />').addClass('popup');
				$inner	= $('<span />').text(text);
				
				$$.append($popup.append($inner)).removeAttr('title');
			});
		}
		
			
		/**
		* @section Gallery Images
		* @uses crossfade() (optionally)
		* --------------------------------------------------------------------------------------------------
		*/
		/*
		$gallery			= $('#gallery');
		$galleryImage		= $('div.large img', $gallery);
		galleryImageFading	= false;
		
		$gallery.bind('click.gallery', {active: 'active'}, function(event){
			var $$, $a, src;
			
			$$		= $(event.target).closest('li');
			$a		= $('a', $$);
			src		= $a.attr('href');
			
			if($$.is(':not(.' + event.data.active + ')')) {
				$$.addClass(event.data.active).siblings().removeClass(event.data.active);
				
				if($.fn.crossfade && galleryImageFading === false) {
					galleryImageFading = true;
					$('div.large img', $gallery).crossfade({
						target:		src,
						animated:	true,
						duration:	1250,
						callback:	function() {
							galleryImageFading = false;
						}
					});
				}
				else {
					$galleryImage.attr('src', src);
				}
			}
			event.preventDefault();
			$a.blur();
		});
		*/
		
		
		/**
		* @section Product Review Rating
		* @uses rating()
		* --------------------------------------------------------------------------------------------------
		*/
		if($.fn.rating) {
			$('.rating').find(':radio').rating({
				'required':		false,
				'callback':		function(value, link){
					$(this).closest('.rating').find('input.rating-value').val(value);
				}
			}).end().find('label').hide();
		}

		
			
		/**
		* @section General click events
		* --------------------------------------------------------------------------------------------------
		*/
			
		$('a.toggle, a.popup, a.close').live('click.toggle', function(event){
			var $$, href, $div,
				duration = 500;
			
			$$ = $(event.target).closest('a');
			if($$.is('a')) {
				href	= $$.attr('href');
				$div	= $(href);
				
				if($div.length > 0) {
					if($$.is('.popup')) {
						if($div.is(':hidden')){
							$div.fadeIn(duration);
							if($$.is('#deposit_link')){
								$$.children('span').text('Hide Info');
							}
						}
						else {
							$div.fadeOut(duration);
							if($$.is('#deposit_link')){
								$$.children('span').text('More Info');
							}
						}
					}
					else if($$.is('.toggle')) {
						$div.slideToggle(duration);
					}
					else if($$.is('.close')) {
						$('a[href^=' + href + ']:not(.close)').trigger('click');
					}
					$$.blur();
					event.preventDefault();
				}
			}
		}).filter('.popup').each(function(i){
			var $$, href, $div, $close;
			
			$$		= $(this);
			href	= $$.attr('href');
			$div	= $(href);
			
			$close	= $('<a />').attr('href', href).addClass('close').attr('title', 'Close').text('×');
			
			$div.prepend($close);
		});
		
		$('a.info').live('click.info', function(event){
			var $$;
			
			$$ = $(event.target).closest('a');
			
			if($$.is('a')) {
				$$.blur();
				event.preventDefault();
			}
		});
		
		if($('#search').length>0 && $('#q-cat').length>0) {
			$('#q-cat').change(function(){
				if($('#q-cat').val() != ''){
					$("#siteSearch").attr("value", ""); 
					$('#search').submit();
				}
			});
		}
		
		
		$('a.toggle_order').live('click.toggle', function(event){
			var $$, href, $div;
			
			$$ = $(event.target).closest('a');
			
			if($$.is('a')) {
				event.preventDefault();
				
				href = $$.attr('href');
				href = href.replace('#','.');
				
				$tr	= $(href);
			
				if($tr.length > 0) {
					$tr.toggle();
				}
			}
		});
		
		/**
		* @section Homepage
		* --------------------------------------------------------------------------------------------------
		*/

		if($('#banners #bannersInner').length>0) {
			$('#banners #bannersInner').cycle({ 
				fx:				'fade',
				timeout:		5000,
				delay:			0,
				startingSlide:	0
				 });
		}
			
		/**
		* External links
		* --------------------------------------------------------------------------------------------------
		*/
		$('a[rel*=external]').click( function() {
			window.open(this.href);
			return false;
		});
		
		if($('#sortMethod1').length>0) {
			$("#sortMethod1").bind("change", callUrl);
			$('#jsOrderBtn1').hide();
		}
		
		
	});
}

function incBasket(elm, basket) {
	if(jQuery(elm)) {
		jQuery(elm).value=(parseInt(jQuery(elm).value))+1;
		if(basket==true) {
			submitDoc('cart');
		}
	}
}
function decBasket(elm, basket, limit) {
	if(jQuery(elm)) {
		if(parseInt(jQuery(elm).value)>limit) {
			jQuery(elm).value=(parseInt($(elm).value))-1;
		}
		if(basket==true) {
			submitDoc('cart');
		}
	}
}

function callUrl() {
	goUrl(this.id);
}

function goUrl(element) {
	var url = document.getElementById(element).options[document.getElementById(element).selectedIndex].value;
	window.location = url;
}


