/**
 * @author bdgeorge
 * Whitepixels common Javascript routines
 */

jQuery(document).ready(function($){	
	//Preload any hover images
	$('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
	$('img.wh-roll').hover(
		function(){
			//Over function
			this.src = addSuffix(this.src,'_on');
		},
		function(){
			//Out function
			this.src = remSuffix(this.src,'_on');
		});

	/**
	 * Ajax functions
	 */
	
	/**
	 * Toys home page carousel
	 * The toys home page starts with a grid of toys, this function adds "next" and "previous" links as required
	 * clicking on either then moves forward or back through a slideshow until there are no more toys to load
	 */


	//jQuery Cycle for gallery's
	$('.single ul.gallery')
		.after('<div class="navImage"><a class="prevImage" href="#">&lt; previous</a><span class="countImage"></span><a class="nextImage" href="#">next &gt;</a></div>')
		.cycle({ 
	    fx:     'scrollHorz', 
	    speed:  'slow', 
	    timeout: 0, 
	    next:   'a.nextImage', 
	    prev:   'a.prevImage',
		after: onAfter 
	});
	/**
	 * After a slide is complete, resize the slide container to fit the displayed element
	 */
	function onAfter(currSlide,nextSlide,opts){
		$('span.countImage').html(opts.currSlide+1+" of " + opts.elements.length);
		$('ul.gallery').animate({
				height: $(nextSlide).height() + "px"
			},1500);
	}
	//jQuery Cycle for home page
	$('#frontFeature')
		.cycle({
			fx:	'fade',
			speed: 1500,
			timeout: 9500,
			pause: 1
		});

});


/**
* Helper functions
*/
function popupWindow(myurl, myheight, mywidth, mywindowID) {
 	if(window.open){
		window.open(myurl,mywindowID,'alwaysRaised=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=' + mywidth
		+ ',height=' + myheight
		+ ',screenX=150,screenY=150,top=150,left=150');
		return false;
 	}else{
 		return true;
 	}
}
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;
	}
}

/**
 * Add this configuration and invocation
 */
var addthis_config =
{
   "data_track_clickback":true
}
var addthis_share = {
  templates: {
       twitter: 'check out {{url}} from lmnop',
    }

}

