/**
 * @author bdgeorge
 * Whitepixels common Javascript routines - requires jQuery
 */

jQuery(document).ready(
function(){
	//Preload any hover images
	jQuery('img.wh-roll').each(function(){
		this.ExtraImage= new Image();
		this.ExtraImage.src = addSuffix(this.src,'_on');
	});
	//Create roll over images on any image with wh-roll as its class
	jQuery('img.wh-roll').hover(
		function(){
			//Over function
			this.src = addSuffix(this.src,'_on');
		},
		function(){
			//Out function
			this.src = remSuffix(this.src,'_on');
		});	
	//jQuery Cycle for home page
	jQuery('.features-main .scroller')
		.cycle({
			fx:	'scrollHorz',
			speed: 1000,
			timeout: 8500,
			delay: 0,
			pause: 1,
			next: '.features-main .next',
			prev: '.features-main .prev'
			
		});

	jQuery('.post-image-links a').each(function(i){
		jQuery(this).bind('click', {index:i},function(){
			imgArray = jQuery('.post-images li');
			jQuery('.post-images li').hide();
			jQuery(imgArray[i]).show(); //IE doesn't like index addressing the jQuery array directly
			jQuery('.post-image-links li').removeClass('active');
			jQuery(this).parent().addClass('active');
			return false;
		})
	})
	
	//Post Slider
	jQuery('#slider').Horinaja({
		capture:'slider',delai:0.3,
		duree:4,pagination:true});

	//Menu hover
	jQuery('#menu-top-menu > li').hover(
		function(){
			jQuery(this).addClass('open');
		},
		function(){
			jQuery(this).removeClass('open');
		}
	);
	//Menu hover suppression - 
	jQuery('#menu-top-menu > li:not(:has(ul))').addClass('childless');
	jQuery('#menu-top-menu > li:first').addClass('first');
	jQuery('#menu-top-menu > li:last').addClass('last');
	jQuery('#menu-top-menu li ul li:last').addClass('last');
	
	//Footer form value clearing
	jQuery('.footer-forms .input-text').one('click', function(){
		jQuery(this).attr('value','');
	})
	
	//Category thumbnail animation
	if(!(jQuery.browser.msie && jQuery.browser.version=="6.0")) {
		jQuery('.category-list li').hover(
			function(){
			    jQuery(this).find('.mask').fadeOut('fast');
			},
			function(){
			    jQuery(this).find('.mask').fadeIn('fast');
			}
		)
	}
			

	
		
	//General helpers		
	function remSuffix(a,b){
		//removes suffix b from filename a without removing the file extension
		var lastdot = a.lastIndexOf(b + '.');
		if (lastdot==-1){
			return a;
		} else {
			var ext = a.slice(lastdot+b.length,a.length);
			return a.slice(0,lastdot) + ext;
		}		
	}
	function addSuffix(a,b){
		//adds suffix b to filename a without removing the file extension
		var lastdot = a.lastIndexOf(".");
		if (lastdot==-1){
			return a;
		} else {
			var ext = a.slice(lastdot,a.length);
			return a.slice(0,lastdot) + b + ext;
		}
	}		
});

