jQuery.noConflict();

var intervalSlideshow = "";
var intervalSlide = "";
var slideCheck = true;

if (window.addEventListener) {
	window.addEventListener('blur', stopSlide );
}else if (window.attachEvent){
  window.attachEvent('blur', stopSlide );
}else{
	document.addEventListener('blur', stopSlide );
}

jQuery(document).ready(function () {
	
	/* When the user focuses back onto the site the slideshow will start again
	===========================================================================*/
	jQuery(window).focus(function(){
		if (!slideCheck) {
			startSlide();
		}
	})
	
	var slideItems;
	if (jQuery('.slideshow').length > 0) {
	
		var slideshow_width = 0;
		slideItems = jQuery('.slide-container li').length;
		
		jQuery('.slide-container li').each(function(i,val){
			slideshow_width += jQuery(val).width();
		});
		
		jQuery('#slide-total').text(slideItems - 2);
		jQuery('#slide-current').text('1');
		
		jQuery('.slide-container').css('width',slideshow_width);
	
	}
	
	/* Slideshow Functionality
	============================*/
	var sliding;
	jQuery('.slide-toggle').click(function(e){	

		e.preventDefault();
		
		if(!e.isAuto){
			stopSlideShow();
		}
		
		if(!sliding) {
		
			sliding = true;
			var active_element = jQuery('.slide-container li.active');
			var active_index = parseInt(jQuery(active_element).attr('id'));		
			var animate = active_element.parent().data('animate');
			
			var current_action = jQuery(this).data('action');
			var next_item;
			
			var list_length = slideItems-2;
			
			if(current_action === "next"){
				if(active_index == list_length) //If current active is the last item
				{
					active_index = 1;
					next_item = jQuery('.slide-container').find("li#1");
				}
				else {
					active_index++;	
					next_item = jQuery(active_element).next();
				}
				
				element_width = '-=';
				
			}else{
				
				if(active_index == 1)
				{
					active_index = list_length;
					next_item = jQuery('.slide-container').find("li#"+list_length);
				}
				else{
					active_index--;
					next_item = jQuery(active_element).prev();
				}	
				
				element_width = '+=';
			}
			
			var title = jQuery('#'+active_index).data('title');
			var desc = jQuery('#'+active_index).data('desc');
			
			if(animate === "scroll")
			{
			
				active_element.parent().animate({
						left: element_width + '955px'
					}, 400, function() {	
					
					if(current_action === "next"){
						if (active_element.next().hasClass('cloned')) {
							active_element.parent().css('left', '-955px');
						}	
					}else{
						if (active_element.prev().hasClass('cloned')) {
							var left_pos = list_length * 955;
							active_element.parent().css('left', '-'+left_pos+'px');
						}
					}
					
					active_element.removeClass('active');
					next_item.addClass('active');
					
					sliding = false;
				});
	
				
			}else{
				active_element.fadeOut().removeClass('active');
				next_item.addClass('active').fadeIn();
			}
		
			jQuery('#slide-current').text(active_index);
			jQuery('#slide-title').text(title);
			jQuery('#slide-desc').html(desc);
		
		}
		
		return false;
	
	});
	
//	/* Hover state on the product colour swatches
//	=====================================================*/
//	jQuery("ul#colour-swatches li").hover(function() {
//		
//		var swatch = jQuery(this);
//		
//		swatch.css({'z-index' : '100'}); /*Add a higher z-index value so this image stays on top*/
//		
//		swatch.find('.swatch').addClass("hover").stop() /* Add class of "hover", then stop animation queue buildup*/
//		    .animate({
		        //marginTop: '-110px', /* The next 4 lines will vertically align this image */
		        //marginLeft: '-110px',
//		        top: '-6px',
//		        left: '-6px',
//		        width: '32px', /* Set new width */
//		        height: '32px' /* Set new height */
//		    }, 0); /* this value of "200" is the speed of how fast/slow this hover animates */
//		} , function() {
//		
//		swatch.css({'z-index' : '0'}); /* Set z-index back to 0 */
//		
//		if (swatch.find('.swatch').hasClass('white-colour-swatch')) {
//		    width = '20px';
//		    height = '20px';
//		} else {
//		    width = '22px';
//		    height = '22px';
//		}
//		
//		swatch.find('.hover').removeClass("hover").stop()  /* Remove the "hover" class , then stop animation queue buildup*/
//		.animate({
//		    marginTop: '0', /* Set alignment back to default */
//		    marginLeft: '0',
//		    top: '0',
//		    left: '0',
//		    width: width, /* Set width back to default */
//		    height: height /* Set height back to default */
//		}, 0);
//	});
	
	/* colourbox popups
	======================*/
	jQuery(".sizeguide-colorbox").colorbox({inline: true, href: '#size-guide'});
	
	
	/* Tabs
	===================*/
	if (jQuery('#tabs').length > 0) {
		jQuery("#tabs").tabs();
	}
	
	/* Starts the slideshow
	========================*/
	if(jQuery('.slideshow').length > 0) {
		startSlide();
	}

});	


function startSlide() {
	intervalSlide = setInterval("autoSocialSlide()",6000);
    intervalSlideshow = setInterval("autoSlide()",6000);
}

function stopSlide() {
    clearInterval(intervalSlide);
    clearInterval(intervalSlideshow);
    slideCheck = false;
}

function stopSlideShow() {
	clearInterval(intervalSlideshow)
}

var fbSlideCount = 1;
var twitterSlideCount = 1;

function autoSlide(isAuto) {

	// event object setup - allows us to set when the slideshow animation is from a user click or not
	var event = jQuery.Event("click");
	event.isAuto = true;
	
	jQuery('.slideshow .next').trigger(event);
	
}

function autoSocialSlide() {
	fbSlide();
	twitterSlide();
}

function fbSlide(e){

	fbSlideCount++;
	
	var listContainer = jQuery('#facebook-box-post ul');
	
	var activeElement = listContainer.find('.active');
	var nextElement = listContainer.find('.next');
	
	var itemHeight = '-=' + listContainer.find('li:first-child').height();
	
	activeElement.animate({
		top: itemHeight
	}, 400, function() { });
		
	nextElement.animate({
		top: itemHeight
	}, 400, function() { 
	
		activeElement.removeClass('active').attr('style', '');
		nextElement.removeClass('next').addClass('active').attr('style', '').next().addClass('next');
		
		if (nextElement.next().length != 1) {
			nextElement = listContainer.find('li:first-child');
			nextElement.addClass('next');
		}
	
	});
	
}

function twitterSlide(e){

	twitterSlideCount++;
	
	var listContainer = jQuery('#twitter-box-tweet ul');
	
	var activeElement = listContainer.find('.active');
	var nextElement = listContainer.find('.next');
	
	var itemHeight = '-=' + listContainer.find('li:first-child').height();

	
	activeElement.animate({
		top: itemHeight
	}, 400, function() { });
		
	nextElement.animate({
		top: itemHeight
	}, 400, function() { 
	
		activeElement.removeClass('active').attr('style', '');
		nextElement.removeClass('next').addClass('active').attr('style', '').next().addClass('next');
		
		if (nextElement.next().length != 1) {
			nextElement = listContainer.find('li:first-child');
			nextElement.addClass('next');
		}
	
	});
}
	

