// JavaScript Document
/*! Copyright © 2009 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
 *
 * Version: 3.0.2
 * 
 * Requires: 1.2.2+
 */
if (document.getElementById('homePage')) {

/* homepage feature functionality */
(function($) {

var types = ['DOMMouseScroll', 'mousewheel'];

$.event.special.mousewheel = {
	setup: function() {
		if ( this.addEventListener )
			for ( var i=types.length; i; )
				this.addEventListener( types[--i], handler, false );
		else
			this.onmousewheel = handler;
	},
	
	teardown: function() {
		if ( this.removeEventListener )
			for ( var i=types.length; i; )
				this.removeEventListener( types[--i], handler, false );
		else
			this.onmousewheel = null;
	}
};

$.fn.extend({
	mousewheel: function(fn) {
		return fn ? this.bind("mouse wheel", fn) : this.trigger("mouse wheel");
	},
	
	unmousewheel: function(fn) {
		return this.unbind("mouse wheel", fn);
	}
});


function handler(event) {
	var args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true;
	
	event = $.event.fix(event || window.event);
	event.type = "mouse wheel";
	
	if ( event.wheelDelta ) delta = event.wheelDelta/120;
	if ( event.detail     ) delta = -event.detail/3;
	
	// Add events and delta to the front of the arguments
	args.unshift(event, delta);

	return $.event.handle.apply(this, args);
}

})(jQuery);

/**
 * @version		$Id:  $Revision
 * @package		jquery
 * @subpackage	lofslidernews
 * @copyright	Copyright © JAN 2010 LandOfCoder.com <@emai:landofcoder@gmail.com>. All rights reserved.
 * @website     http://landofcoder.com
 * @license		This plugin is dual-licensed under the GNU General Public License and the MIT License 
 */
// JavaScript Document
(function($) {
	 $.fn.lofJSidernews = function( settings ) {
	 	return this.each(function() {
			// get instance of the lofSiderNew.
			new  $.lofSidernews( this, settings ); 
		});
 	 }
	 $.lofSidernews = function( obj, settings ){
		this.settings = {
			direction	    	: '',
			mainItemSelector    : 'li',
			navInnerSelector	: 'ul',
			navSelector  		: 'li' ,
			navigatorEvent		: 'click',
			wrapperSelector		:'.lof-main-wapper',
			interval	  	 	: 900000000,
			auto			    : false, // whether to automatic play the slideshow
			maxItemDisplay	 	: 4,
			startItem			: 0,
			navPosition			: 'horizontal',
			navigatorHeight		: 36,
			navigatorWidth		: 200,
			duration			: 1000000000,
			navItemsSelector    : '.lof-navigator li',
			navOuterSelector    : '.lof-navigator-outer',
			isPreloaded			: true,
			easing				: 'easeInOutQuad'
		}	
		$.extend( this.settings, settings ||{} );	
		this.nextNo         = null;
		this.previousNo     = null;
		this.maxWidth  = this.settings.mainWidth || 947;
		this.wrapper = $( obj ).find( this.settings.wrapperSelector );	
		this.slides = this.wrapper.find( this.settings.mainItemSelector );
		if( !this.wrapper.length || !this.slides.length ) return;
		// set width of wrapper
		if( this.settings.maxItemDisplay > this.slides.length ){
			this.settings.maxItemDisplay = this.slides.length;	
		}
		this.currentNo      = isNaN(this.settings.startItem)||this.settings.startItem > this.slides.length?0:this.settings.startItem;
		this.navigatorOuter = $( obj ).find( this.settings.navOuterSelector );	
		this.navigatorItems = $( obj ).find( this.settings.navItemsSelector );
		this.navigatorInner = this.navigatorOuter.find( this.settings.navInnerSelector );
		this.navigatorInner.width( this.slides.length * (this.settings.navigatorWidth+10) );			
		
		this.navigatorStep = this.__getPositionMode( this.settings.navPosition );		
		this.directionMode = this.__getDirectionMode();	

		//Move last three slides to beginning for infinite scrolling
		for ( var i=0; i<4; i++) {
			$('.lof-main-wapper li:first').before($('.lof-main-wapper li:last'));
		}
			
		$(this.slides).css('opacity',0.3).eq(this.currentNo).css('opacity',1).addClass("focused").prev().addClass("prev").end().next().addClass("next");
		this.wrapper.css({'left':'-2820px', 'width':( this.maxWidth ) * (this.slides.length+2) } );
		
		this.onComplete();
		
	 }
     $.lofSidernews.fn =  $.lofSidernews.prototype;
     $.lofSidernews.fn.extend =  $.lofSidernews.extend = $.extend;
	 
	 $.lofSidernews.fn.extend({
							  
		startUp:function( obj, wrapper ) {
			sect = this;

			this.navigatorItems.each( function(index, item ){
				$(item).click( function(){
					sect.jumping( index, true );
					sect.setNavActive( index, item );		
				} );
			})
			
			this.setNavActive(this.currentNo );
			
			if( this.settings.buttons && typeof (this.settings.buttons) == "object" ){
				this.registerButtonsControl( 'click', this.settings.buttons, this );

			}
			if( this.settings.auto ) 
			this.play( this.settings.interval,'next', true );
			
			$(this.slides).click(function(e) {
				if ($(this).attr("class")!="focused") {
					e.preventDefault();
					if ($(this).attr("class")=='prev') {
						seft.previous(true);					
					} else if ($(this).attr("class")=='next') {					
						seft.next(true);
					}
					
				}
			});
			
			return this;
		},
		onComplete:function(){
			setTimeout( function(){ $('.preload').fadeOut( 900 ); }, 400 );	this.startUp( );
		},
		
		navigationAnimate:function( currentIndex ) {
			if (currentIndex <= this.settings.startItem || currentIndex - this.settings.startItem >= this.settings.maxItemDisplay-1) {
				this.settings.startItem = currentIndex - this.settings.maxItemDisplay+2;
			    if (this.settings.startItem < 0) this.settings.startItem = 0;
			    	if (this.settings.startItem >this.slides.length-this.settings.maxItemDisplay) {
			        	this.settings.startItem = this.slides.length-this.settings.maxItemDisplay;
			        }
			    }       
			this.navigatorInner.stop().animate( eval('({'+this.navigatorStep[0]+':-'+this.settings.startItem*this.navigatorStep[1]+'})'), {duration:500, easing:'easeInOutQuad'} );  	
		},
		setNavActive:function( index, item ){
			if ( (this.navigatorItems) ) { 
		    	this.navigatorItems.removeClass( 'active' );
		        $(this.navigatorItems.get(index)).addClass( 'active' ); 
		        this.navigationAnimate( this.currentNo );   
		    }
		},
		__getPositionMode:function( position ){
			if(	position  == 'horizontal' ){
				return ['left', this.settings.navigatorWidth];
			}
			return ['top', this.settings.navigatorHeight];
		},
		__getDirectionMode:function(){
			switch( this.settings.direction ){
				case 'opacity': this.maxSize=0; return ['opacity','opacity'];
				default: this.maxSize=this.maxWidth; return ['left','width'];
			}
		},
		registerWheelHandler:function( element, obj ){ 
			 element.bind('mouse wheel', function(event, delta ) {
				var dir = delta > 0 ? 'Up' : 'Down',
					vel = Math.abs(delta);
				if( delta > 0 ){
					obj.previous( true );
				} else {
					obj.next( true );
				}
				return false;
			});
		},
		registerButtonsControl:function( eventHandler, objects, self ){
			for( var action in objects ){ 
				switch (action.toString() ){
					case 'next':
						objects[action].click( function() { self.next( true) } );
						break;
					case 'previous':
						objects[action].click( function() { self.previous( true) } );
						break;
				}
			}
			return this;	
		},
		onProcessing:function( manual, start, end ){	 		
			this.previousNo = this.currentNo + (this.currentNo>0 ? -1 : this.slides.length-1);
			this.nextNo 	= this.currentNo + (this.currentNo < this.slides.length-1 ? 1 : 1- this.slides.length);				
			return this;
		},
		finishFx:function( manual, direction ){
			if( manual ) this.stop();
			if( manual && this.settings.auto ){ 
				this.play( this.settings.interval,'next', true );
			}	
			this.setNavActive( this.currentNo );
		},
		getObjectDirection:function( start, end, navEvent ){
			if (navEvent==='next') {
				return eval("({'"+this.directionMode[0]+"':-"+(start)+"})");
			} else if (navEvent==='previous') {
				return eval("({'"+this.directionMode[0]+"':-1880})");	
			}
		},
		fxStart:function( index, obj, currentObj, navEvent, existingNo ) {
			$(this.slides).removeClass("focused prev next");			
			$(this.slides).stop(true, true).animate({opacity:0.3}, {duration: this.settings.duration, easing:this.settings.easing} );
			$(this.slides).eq(index).addClass("focused").stop(true, true).animate( {opacity:1}, {duration: this.settings.duration, easing:this.settings.easing} );
			this.wrapper.stop(true, true).animate( obj, {duration: this.settings.duration, easing:this.settings.easing, complete:function() {
				if (navEvent==='next') {
					$('.lof-main-wapper li:last').after($('.lof-main-wapper li:first'));
				} else if (navEvent==='previous') {
					$('.lof-main-wapper li:first').before($('.lof-main-wapper li:last'));			
				} else  if (navEvent==='jumping') {
					var itemsToMove;
					//Jumping left to right
					if (index>existingNo) {
						itemsToMove = (index-existingNo);
						for ( var i=0; i<itemsToMove; i++ ) {
							$('.lof-main-wapper li:last').after($('.lof-main-wapper li:first'));	
						}
					//Jumping right to left	
					} else if (index<existingNo) {
						itemsToMove = (existingNo-index);
						for ( var i=0; i<itemsToMove; i++ ) {
							$('.lof-main-wapper li:first').before($('.lof-main-wapper li:last'));	
						}								
					}			
				}
				$(currentObj.wrapper).css("left","-2820px");
				$(currentObj.slides).eq(index).addClass("focused").prev().addClass("prev").end().next().addClass("next");
			}});
			return this;
		},
		jumping:function( no, manual ){
			var jumpDistance;
			var obj;
			this.stop();
			if ( this.currentNo == no ) return;
			if ( this.currentNo < no ) {
				jumpDistance=(no-this.currentNo);
				obj = eval("({'"+this.directionMode[0]+"':-"+((jumpDistance*940)+2820)+"})");
			} else if ( this.currentNo > no ) {	
				jumpDistance=(this.currentNo-no);
				obj = eval("({'"+this.directionMode[0]+"':"+((jumpDistance*940)-2820)+"})");	
			}
			this.onProcessing( null, manual, 0, this.maxSize )
				.fxStart( no, obj, this, 'jumping', this.currentNo )
				.finishFx( manual );	
				this.currentNo  = no;
		},
		next:function( manual , item){
			this.stop(true, true);
			this.currentNo += (this.currentNo < this.slides.length-1) ? 1 : (1 - this.slides.length);
			this.onProcessing( item, manual, 0, this.maxSize )
				.fxStart( this.currentNo, this.getObjectDirection(this.maxSize*4,'', 'next' ), this, 'next' )
				.finishFx( manual );
		},
		previous:function( manual, item ){
			this.stop();
			this.currentNo += this.currentNo > 0 ? -1 : this.slides.length - 1;
			this.onProcessing( item, manual )
				.fxStart( this.currentNo, this.getObjectDirection(this.maxSize,'', 'previous' ), this, 'previous' )
				.finishFx( manual );			
		},
		play:function( delay, direction, wait ){	
			this.stop(); 
			if(!wait){ this[direction](false); }
			var self  = this;
			this.isRun = setTimeout(function() { self[direction](true); }, delay);
		},
		stop:function(){ 
			if (this.isRun == null) return;
			clearTimeout(this.isRun);
            this.isRun = null;
		}
	})
})(jQuery)

jQuery.noConflict();
/* homepage featured functionality extended */
jQuery("#featured").tabs({
    fx: {
        opacity: "toggle"
    }
}).tabs("rotate", 6000, true);
var buttons = {
    previous: jQuery('#lofslidecontent45 .lof-previous'),
    next: jQuery('#lofslidecontent45 .lof-next')
};
jQuery('#lofslidecontent45').lofJSidernews({
    interval: 6000,
    easing: 'easeInOutQuad',
    duration: 800,
    auto: true,
    buttons: buttons
});


/* homepage latest product functionality */
var viewedProductsContainer = jQuery('#latestProducts ul');
var numImages = jQuery('#latestProducts ul li').length;
var containerWidth = 160 * numImages;
var leftButton = jQuery('#left');
var rightButton = jQuery('#right');


jQuery('div.right').click(function() {
    var containerFromRight = parseInt(viewedProductsContainer.css("left"));
        //WebKit fix
        if (isNaN(containerFromRight) == true) {
            containerFromRight = 0;
        }
        //If fully right disable right button
        if ((containerWidth + containerFromRight) <= 1920) {
            jQuery(this).stop(true, true).fadeTo('slow', 0.5).removeClass("enablescrollbutton").addClass("disablescrollbutton");
        }
        //If not fully left fade in left button
        if (containerFromRight >= 0) {
            leftButton.stop(true, true).fadeTo('slow', 1).removeClass("disablescrollbutton").addClass("enablescrollbutton");;
        }
        viewedProductsContainer.stop(true, true).animate({ left: '-=160' }, 400);
    });
    
jQuery('div.left').click(function() {
    var containerFromLeft = parseInt(viewedProductsContainer.css("left"));
        //WebKit fix
        if (isNaN(containerFromLeft) == true) {
            containerFromLeft = 0;
        }
        //If fully left disable left button
        if (containerFromLeft >= -160) {
            jQuery(this).stop(true, true).fadeTo('slow', 0.5).removeClass("enablescrollbutton").addClass("disablescrollbutton");
        }
        //If not fully right fade in right button
        if ((containerWidth + containerFromLeft) > 960) {
            rightButton.stop(true, true).fadeTo('slow', 1).removeClass("disablescrollbutton").addClass("enablescrollbutton");
        }
        viewedProductsContainer.stop(true, true).animate({ left: '+=160' }, 400);
    });

}
