var gwj = jQuery.noConflict();

/* start: select language bubble */
gwj(function () {
	gwj('.bubbleInfo').each(function () {
		// options
		var distance = 10;
		var time = 250;
		var hideDelay = 500;

		var hideDelayTimer = null;

		// tracker
		var beingShown = false;
		var shown = false;

		var trigger = gwj('.trigger', this);
		var popup = gwj('.popup', this).css('opacity', 0);

		// set the mouseover and mouseout on both element
		gwj([trigger.get(0), popup.get(0)]).mouseover(function () {
			// stops the hide event if we move from the trigger to the popup element
			if(hideDelayTimer) clearTimeout(hideDelayTimer);

			// don't trigger the animation again if we're being shown, or already visible
			if(beingShown || shown) {
				return;
			} else {
				beingShown = true;

				// reset position of popup box
				popup.css({
					top: 28,
					left: -10,
					display: 'block' // brings the popup back in to view
				})

				// (we're using chaining on the popup) now animate it's opacity and position
				.animate({
					top: '-=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					// once the animation is complete, set the tracker variables
					beingShown = false;
					shown = true;
				});
			}
		}).mouseout(function () {
			// reset the timer if we get fired again - avoids double animations
			if(hideDelayTimer) clearTimeout(hideDelayTimer);

			// store the timer so that it can be cleared in the mouseover if required
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				popup.animate({
					top: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					// once the animate is complete, set the tracker variables
					shown = false;
					// hide the popup entirely after the effect (opacity alone doesn't do the job)
					popup.css('display', 'none');
				});
			}, hideDelay);
		});
	});
});
/* end: select language bubble */

gwj(document).ready(function() {
	/* start: hide default value on focus */
	gwj('.default-value').each(function() {
		var default_value = this.value;
		gwj(this).focus(function() {
			if(this.value == default_value) {
				this.value = '';
			}
		});
		gwj(this).blur(function() {
			if(this.value == '') {
				this.value = default_value;
			}
		});
	});
	/* end: hide default value on focus */
	
	/* start: vertically align prices and add to cart buttons */
	if(gwj('.bd').length > 1) {
		var items = gwj('.bd').get(); // fetch array of all items in these divs
		prodrows = ( items.length ) / 3; // tells us how many rows we are working with
		for(i=0; i<prodrows; i++ ) {
			// for row zero we want eq 0, eq1 eq 2 for row 1 eq 3 eq 4 eq 5
			col0 = (i * 3); // row(x) item 1
			col1 = (i * 3) + 1; // row(x) item 2
			col2 = (i * 3) + 2; // row(x) item 3
			h0 = gwj('.bd:eq('+col0+')').height(); // height of item 1
			h1 = gwj('.bd:eq('+col1+')').height(); // height of item 2
			h2 = gwj('.bd:eq('+col2+')').height(); // height of item 3
			
			max = Math.max( h0, h1, h2 ); // find the tallest item per row
			
			diff0 = ( max - h0 ) + 'px'; // add padding to the bottom of the divs that are smaller then max
			diff1 = ( max - h1 ) + 'px';
			diff2 = ( max - h2 ) + 'px';
			
			gwj('.bd:eq('+col0+')').css('padding-bottom', diff0);
			gwj('.bd:eq('+col1+')').css('padding-bottom', diff1);
			gwj('.bd:eq('+col2+')').css('padding-bottom', diff2);
		}
	}
	/* end: vertically align prices and add to cart buttons */
	
	/* start: readmore links */
	gwj(".gw-readmore").click(function() {
		gwj(this).hide(); // hide read more link
		gwj(this).parent().next('.gw-more').show(); // show more text
		return false;
	});
	gwj(".gw-readless").click(function() {
		gwj(this).parent().parent().prev().find('.gw-readmore').show(); // show read more link
		gwj(this).parent().parent().hide(); // hide more text
		return false;
	});
	/* end: readmore links */

	/* start: fix z-index bug for IE <=7 because of slider/dropdown combination on homepage */
	if((gwj.browser.msie) && (parseInt(gwj.browser.version, 10) <= 7) && (gwj('#slides').length > 0)) {
		gwj(function() {
		       var zIndexNumber = 1000;
		       // Put your target element(s) in the selector below!
		       gwj("div").each(function() {
		               gwj(this).css('zIndex', zIndexNumber);
		               zIndexNumber -= 10;
		       });
		});
	}
	/* end: fix z-index bug for IE <=7 because of slider/dropdown combination on homepage */
});

/* start: slider */
gwj(function(){
	if(gwj('#slides').length > 0) {
	    gwj("#slides").slides({
			preload: true,
			preloadImage: '/media/slider/loading.gif',
			pagination: false,
			generatePagination: false,
			play: 10000,
			hoverPause: false,
			effect: 'fade',
			crossfade: true,
			fadeSpeed: 1650
	    });
	}
});
/* end: slider */
