//REEL
$(document).ready(function(){
	//Get total
	totalReel = $('#reel_content ul li').size();
	//Set random, have to +1 so we don't get 0
	currentReel = 1+(Math.floor(Math.random()*totalReel));
	//Find xPos with random num, -1 to account for the previously mentioned +1
	xPos = 0 - ((currentReel-1)*770);
	//Set new position and show (Otherwise we get a flash of content)
	$('#reel_content ul').show().css({left:xPos});
	//Uncomment to remove randomness
	//xPos = 0;
	//currentReel = 1;
	moose = true;
	autoMoose = true;
	reelDelay = 6000;
	$('#reel_controls a').show();//.css({opacity:0.2});
	$('#reel_controls .next a').click(function(){
		if(moose==true && totalReel>currentReel){
			xPos = xPos - 770;
			currentReel = currentReel + 1;
			slideTo(xPos);
		}else if(moose==true){
			currentReel = 1;
			infiniteLoop('right');
		}
		return false;
	});
	$('#reel_controls .prev a').click(function(){
		if(moose==true && currentReel>1){
			xPos = xPos + 770;
			currentReel = currentReel - 1;
			slideTo(xPos);
		}else if(moose==true){
			currentReel = totalReel;
			infiniteLoop('left');
		}
		return false;
	});
	function infiniteLoop(dir){
		moose = false;
		totalX = 770 - (770 * totalReel);
		if(dir == 'left'){
			$('#reel_content ul').prepend('<li>'+$('#reel_content ul li:last').html()+'</li>').css({left:-770}).animate({left:0},770,'easeInOutExpo',function(){
				$('#reel_content ul').css({left:totalX});
				$('#reel_content ul li:first').remove();
				moose = true;
			});
			xPos = totalX;
		}else if(dir == 'right'){
			$('#reel_content ul').append('<li>'+$('#reel_content ul li:first').html()+'</li>').animate({left:totalX-770},770,'easeInOutExpo',function(){
				$('#reel_content ul').css({left:0});
				$('#reel_content ul li:last').remove();
				moose = true;
			});
			xPos = 0;
		}
	}
	$('#reel').hover(function(){
	//	$('#reel_controls a').stop([]).animate({opacity:1},200);
		autoMoose = false;
		$(this).stop([]);
	},function(){
		//$('#reel_controls a').stop([]).animate({opacity:0.2},200);
		autoMoose = true;
		$(this).stop([]);
		autoMagical();
	});
	function autoMagical(){
		$('#reel').animate({opacity:1},reelDelay,function(){
			if(autoMoose == true){
				$('#reel_controls .next a').click();
			}
			autoMagical();
		});
	}
	autoMagical();
	function slideTo(newX){
		moose = false;
		$('#reel_content ul').animate({left:newX},770,'easeInOutExpo',function(){
			moose = true;
		})
	}	
//END REEL
});
//SIMPLE PAUSE FUNCTION
$.fn.pause = function(milli,type) {
	milli = milli || 1000;
	type = type || "fx";
	return this.queue(type,function(){
		var self = this;
		setTimeout(function(){
			$.dequeue(self);
		},milli);
	});
};
$.fn.clearQueue = $.fn.unpause = function(type) {
	return this.each(function(){
		type = type || "fx";
		if(this.queue && this.queue[type]) {
			this.queue[type].length = 0;
		}
	});
};
jQuery.easing['jswing'] = jQuery.easing['swing'];
jQuery.extend( jQuery.easing,
{
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	}
});
// FORM VALIDATION
// SLIDING FUNCTIONS
$(document).ready(function() {
	$('.primary li a').click(function()
	{
		$(this).addClass('scrolling').siblings('ul').children('li').children('a').removeClass('scrolling');
		$(this).parents('li').children('ul').slideDown();
		$(this).parents('li').siblings('li').children('ul').slideUp();
		$(this).parents('li').siblings('li').children('a').removeClass('scrolling');
	});			   
	//feedback validation ajax 
	$('#validation').hide();
	$('#confirmation').hide();
    var options = { 
        target:        '#confirmation',   // target element(s) to be updated with server response 
        beforeSubmit:  validate,  // pre-submit callback 
        success:       function() { 
        	$('#validation').fadeOut('slow');
        	$('#commentForm').fadeOut('slow');
            $('#confirmation').fadeIn('slow'); 
        }  
    }; 
    $('#commentForm').ajaxForm(options); 
	//home product list ajax
	/*
	//page foot flash ajax 
	$('div#flash_overlay div').flash({
			src: 'flash/flash_advert_.swf',
			width: 200,
			height: 247,
			scale: 'noscale',
			wmode: 'transparent'
		},{
			update: false
		}
	);
	*/
}); 
function validate(formData, jqForm, options) { 
 	$("#validation").html('');
    var form = jqForm[0]; 
    err="";
    if (!form.name.value)
    {
    	err+="<li>Please tell us your name, or we'll just have to call you Bob!</li>";
    }
    if (!form.email.value)
    { 
    	err+="<li>We'll be needing a valid e-mail address or phone number please.</li>";
    }
    if (!form.comment.value) 
    { 
        err+="<li>You forgot to let us know what you wanted! Please complete the big white box.</li>";
    } 
    if (err>'')
    {
    	$("#validation").append("<ul>" + err + "</ul>");
        $("#validation").slideDown();
        return false; 
    }
}
/*
function openFlash(){
	$('div#flash_overlay').css({width:1000, height:680, top:66});
	$('div#flash_overlay div').css({width:1000, height:680});
	$('div#flash_overlay div embed').attr('width', '1000');
	$('div#flash_overlay div embed').attr('height', '680');
}
function closeFlash(){
	$('div#flash_overlay').css({width:200, height:247, top:499});
	$('div#flash_overlay div').css({width:200, height:247});
	$('div#flash_overlay div embed').attr('width', '200');
	$('div#flash_overlay div embed').attr('height', '247');
}*/
jQuery.extend( jQuery.easing,
{
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	}
});

