/* SLIDESHOW */

function slideSwitch() {
    var $active = jQuery('#text-fader li.active');

    if ( $active.length == 0 ) $active = jQuery('#text-fader li:last');

    var $next =  $active.next().length ? $active.next()
        : jQuery('#text-fader li:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}
jQuery(function() {
    setInterval( "slideSwitch()", 2500 );
});

/* LATEST OFFERS POPUP */


jQuery(function(){
	/** EY modified below so that the click handler only applies in bottom corner of the image.**/
	jQuery('#latest-link').click(function(e){
		
		var offset = jQuery(this).offset();

		if((e.pageX - offset.left) > 870 && (e.pageY - offset.top) > 280) {
			jQuery('ul#latest-offers').toggle(100);
			return false;
		}
		
		return true;
	});
		
});


jQuery(function() {
	jQuery('select[id^=prescription_info]').change(function(){
		jQuery('form#basket-form').submit();
	});
});

window.addEvent('domready', function() {
	$('search_query').addEvent('focus', function(evnt) {
		if(this.hasClass('default')) {
			this.value = '';
			this.removeClass('default');
		}
	});

});

jQuery(function() {
	jQuery('a.new-window').click(function() {
		window.open(jQuery(this).attr('href'));
		return false;
	});
});

jQuery(document).ready(function() {
	var relatedProds = jQuery('#related-products');
	relatedProds.addClass('js');
	var relatedUl = jQuery('ul', relatedProds);
	var spareHeight = relatedUl.parent().height();
	//This spare height claculation is incomplete!
	spareHeight -= relatedUl.outerHeight(true) - relatedUl.height();
	relatedUl.css('height', spareHeight+'px');
	var liHeight = jQuery('li', relatedUl).outerHeight(true);
	relatedUl.css('height', (Math.floor((relatedUl.height() - (relatedUl.outerHeight(true) - relatedUl.height())) / liHeight) * liHeight)+'px');
	//Hide those that aren't visible
	jQuery('li', relatedUl).each(function() {
		if(jQuery(this).position().top > relatedUl.height()) {
			jQuery(this).addClass('hide');
		}
	});
	//Set up page navigation links.
	jQuery('a.next, a.previous', relatedProds).click(product__changeRelatedProdsPage);
	product__changeRelatedProdsPage();
});

function product__changeRelatedProdsPage(evnt) {
	var liHeight = jQuery('#related-products ul li').outerHeight(true);
	var liWidth = jQuery('#related-products ul li').outerWidth(true);
	var perPage = Math.floor(jQuery('#related-products ul').height() / liHeight)
					* Math.floor(jQuery('#related-products ul').width() / liWidth);
	var visibleLis = jQuery('#related-products ul li:not(li[class~=hide])');
	
	if(jQuery(this).hasClass('next')) {
		var next = jQuery(visibleLis).last().nextUntil();
		if(next.length) {
			next.slice(0, perPage).removeClass('hide');
			visibleLis.addClass('hide');
		}
	}
	else if(jQuery(this).hasClass('previous')) {
		var previous = jQuery(visibleLis).first().prevUntil();
		if(previous.length) {
			var toShow = previous.slice(previous.length-perPage, previous.length);
			toShow.removeClass('hide');
			visibleLis.addClass('hide');
		}
	}
	
	/**
	 * Check which buttons are relevant.
	 */
	visibleLis = jQuery('#related-products ul li:not(li[class~=hide])');
	if(visibleLis.first().prev('li').hasClass('hide')) 
		jQuery('#related-products a.previous').css('cursor', 'pointer');
	else
		jQuery('#related-products a.previous').css('cursor', 'auto');
	
	if(visibleLis.last().next('li').hasClass('hide')) 
		jQuery('#related-products a.next').css('cursor', 'pointer');
	else
		jQuery('#related-products a.next').css('cursor', 'auto');
}
