/*----------------------------------------------------------

* File Name: home.js

----------------------------------------------------------*/
(function($){
	var a = return_heights("section#side-products article.side-product"),
		p; //Var value is the height array.
	function return_heights(node /*We are passing a HTML node.*/){
		var b = [], //Set up an array.
			c = 0, //Set an incrementing var.
			d;
		$(node).each(function(){
			d = $(this).height(); //Nodes height.
			b.push(d); //Push the value onto the array.
			c += 1;
		});
		return b; //Return the array.
	};
	
	function style_products(node){
		var e = a.length,
			f = 440;
		$(node).parent("section").css({
			"position": "relative",
			"width": (e * f),
			"height": a[0],
			"top": 0,
			"left": 0,
			"overflow": "hidden"
		});
	};
	style_products("section#side-products article.side-product");
	
	function auto_animate(int, node /*We are passing an integer and node.*/) {
		var i = parseInt(int) || 0,
			m = a.length;
		p = setTimeout(function() {
			if(i < (m - 1)){
				i += 1;
			} else {
				i = 0;
			}
			$(node).eq(i).trigger('click');
			auto_animate(i, node);
		}, 10000);
		console.log(i);
	};
	auto_animate(0, "ul#navigate-desc li a");
	
	function activate_click(node /*We are passing a HTML node.*/){
		$(node).click(function(){
			var g = $(this).attr("rel") || null; //Set up a var with the node.rel value.
			clearTimeout(p);
			highlight_selection(g, node);
			animate_products(g);
			auto_animate(g, node);
			return false;
		});
	};
	activate_click("ul#navigate-desc li a");
	
	function highlight_selection(int, node /*We are passing an integer and node.*/){
		if(int === null){
			alert("This just isn't working out... It's me not you.");
			return false;
		}
		$(node).each(function(){
			var h = $(this);
			if(h.attr("rel") === int){
				h.addClass("active");
			} else {
				h.removeClass("active");
			}
		});
	};
	
	function animate_products(int /*We are passing an integer.*/){
		if(int === null){
			alert("This just isn't working out... It's me not you.");
			return false;
		}
		$("section#side-products").animate({
			"left": "-" + (440 * int) + "px",
			"height": a[int]
		},{queue: false});
	};
	
})(jQuery);
