/*
	----------------------------------------------------------------------------------------------------
	product preview slider
	----------------------------------------------------------------------------------------------------
*/

count = 1; 
li_length = 0;
setintervalid = 0;
arr = []; 
simple_links = true;
  

jQuery.fn.prodPrev = function( settings ) {
	settings = jQuery.extend({
        interval: 4000,
        speed: "slow",
        pause: 1
    }, settings);      
      
	show_product_list(jQuery("#tab1 .product_prev li.product .frame", this), 'featured' );
	show_product_list(jQuery("#tab2 .product_prev li.product .frame", this), 'new' );
	show_product_list(jQuery("#tab3 .product_prev li.product .frame", this), 'on sale' );
	
	for (i=1; i<=3; i++) {  
		jQuery("#tab"+i+" .product_prev li.product:not(:first)", this ).hide();  
		jQuery("#tab"+i+" .product_list .img_cont:first", this ).addClass("active");
		img_cont = jQuery("#tab"+i+" .product_list .img_cont", this); 
		li_product = jQuery("#tab"+i+" .product_prev li.product", this);
		li_length = li_product.length;
		arr.push([img_cont, li_product, li_length, count, setintervalid]);
	}
	                              
	if ( arr[0][2] > 1 ) { 
		arr[0][4] = setInterval (function(){arr[0][3] = next_slide(arr[0][0], arr[0][1], settings.speed, arr[0][3])}, settings.interval);               
		if (settings.pause != 0) {         
			arr[0][1].parent("ul").mouseleave( function() { 
				if ( arr[0][4] == 0 ) arr[0][4] = setInterval (function(){arr[0][3] = next_slide(arr[0][0], arr[0][1], settings.speed, arr[0][3])},  settings.interval); 
			}).mouseenter( function(){ clearInterval (arr[0][4]); arr[0][4] = 0; });
		}            
	}    

	if ( arr[1][2] > 1 ) {   
		arr[1][4] = setInterval (function(){arr[1][3] = next_slide(arr[1][0], arr[1][1], settings.speed, arr[1][3])}, settings.interval);               
		if (settings.pause != 0) {         
			arr[1][1].parent("ul").mouseleave( function(){ 
				if ( arr[1][4] == 0 ) arr[1][4] = setInterval (function(){arr[1][3] = next_slide(arr[1][0], arr[1][1], settings.speed, arr[1][3])},  settings.interval); 
			}).mouseenter( function(){ clearInterval (arr[1][4]); arr[1][4] = 0; });
		}            
	}


	if ( arr[2][2] > 1 ) {  
		arr[2][4] = setInterval (function(){arr[2][3] = next_slide(arr[2][0], arr[2][1], settings.speed, arr[2][3])}, settings.interval);               
		if (settings.pause != 0) {         
			arr[2][1].parent("ul").mouseleave( function(){ 
				if ( arr[2][4] == 0 ) arr[2][4] = setInterval (function(){arr[2][3] = next_slide(arr[2][0], arr[2][1], settings.speed, arr[2][3])},  settings.interval); 
			}).mouseenter( function(){ clearInterval (arr[2][4]); arr[2][4] = 0; });
		}            
	} 


	jQuery("a.img_cont").click(function() {
		if (simple_links) return true;
		if (!jQuery(this).hasClass("active")) {
			jQuery(this).parents(".tab").find("a.img_cont").removeClass("active");
			jQuery(this).addClass("active");
			i = jQuery(this).attr("href");
			current = parseInt(i.substr(i.length-1, i.length));
			current_tab = jQuery(this).parents(".tab").attr("id");            
			current_tab = parseInt(current_tab.substr(current_tab.length-1, current_tab.length))-1;        
			if (arr[current_tab][2] > 1) { 
				arr[current_tab][3] = current+1; 
				clearInterval (arr[current_tab][4]);
				arr[current_tab][4] = setInterval (function(){ 
					arr[current_tab][3] = next_slide(arr[current_tab][0], arr[current_tab][1], settings.speed, arr[current_tab][3]); 
				}, settings.interval);    
			}
			arr[current_tab][1].css({'position':'absolute', 'top':'0','left':'0'}).fadeOut(settings.speed);          
			arr[current_tab][1].eq(current).css('position','relative').fadeIn(settings.speed);
		};
		return false; 
	});     
};

function show_product_list (list_item, tab_name) {
	if (list_item.length > 1) {
		var output='<div class="product_list" id="product_list1"><ul>';
		list_item.each( function (index) {
			img_url = list_item.children(".img_box").children("A").children("img").eq(index).attr("src");
			item_title = list_item.children(".details").children("h4.name").children("a").eq(index).text();
			item_price = list_item.children(".details").children("div.product").children(".price").children("span").eq(index).text();
		  
			if (simple_links) {
				index = list_item.children(".details").children("h4.name").children("a").eq(index).attr('href');
			}

			output += '<li class="product"><div class="frame"><a href="' + index + '" class="img_cont"><span style="background: url(\'' + img_url + '\') center center no-repeat">&nbsp;<\/span><\/a><h4 class="name">' + item_title + '<\/h4><p class="price">Price: <span>' + item_price + '<\/span><\/p><\/div><\/li>'; 
		});
		output += '<\/ul><\/div>';
		output += '<div class="close_open_container"><a class="open_close_btn"><span>close<\/span> '+tab_name+' products<\/a><\/div>';
		return list_item.parents("ul").after(output);
	}
}
             

function next_slide(img_cont1, li_product1, speed1, counter) {          
	if (counter > li_product1.length - 1) counter = 0;
	if (!img_cont1.eq(counter).hasClass("active")) {
	  img_cont1.removeClass("active").eq(counter).addClass("active");          
	  li_product1.css({'position':'absolute', 'top':'0','left':'0'}).fadeOut(speed1, function() {
		jQuery(this).css("display","none")
	  }).eq(counter).css('position','relative').fadeIn(speed1);
	}
	counter++;
	return counter;
}
