(function($) {
	$.fn.___init = function() {
		var $this = $(this);
		
		function init() {

			var plugins = [ 'product' , 'addtocart' , 'addtowishlist' , 'changediameter' , 'openproduct' ];
			var prefix = "tvs-";
			var l = plugins.length ;
		
			for( i = 0 ; i < l ; i++ ) {
				var p = plugins[i];
				if ($.fn[p]) {
					$this.find("." + prefix + p).each(function(i) {
						eval('$(this).'+p+'()');
					})
				}
			}
			
		}
		
		init();
		
		return $this ;
	}
	
})(jQuery);


/* product */
(function($) {
	$.fn.product = function() {
		var $this = $(this);
		
		function init() {
			
			/* slideshow */
			$('#slideshow').cycle({
				fx:      'scrollLeft',
				timeout:  4000,
				pager:   '#nav',
				size: $('#slideshow').find('img').length
			});
			
			/* similar products */
			globalFatherId = product['kca:id'] ;
			var TVScatalogue = new CatalogueProxy;
			TVScatalogue.getSimilarProductList({ tipology : product.typology , price : product.price , id : product.id , family : globalFatherId , line : product.line} , buildSimilarProductListNew );

		}
		
		init();
		
		return $this ;
	}
	
})(jQuery);


/* openproduct */
(function($) {
	$.fn.openproduct = function() {
		var $this = $(this);
		
		function init() {
			
			$this.colorbox({width:977, height: 450, opacity:.5,onCleanup:function() { buildTotalCart() },onComplete:function() {__init()}});

		}
		
		init();
		
		return $this ;
	}
	
})(jQuery);

/* addtocart */
(function($) {
	$.fn.addtocart = function() {
		var $this = $(this);
		
		function init() {
			
			$this.click(function(e) {
				e.preventDefault();
				if( $this.hasClass('check-quantity') ) {
					quantity = $('#quantity').val() ;
				} else {
					quantity = 1 ;	
				}
				addToCart( $(this).attr('rel') , quantity , true ) ;
			})

		}
		
		init();
		
		return $this ;
	}
	
})(jQuery);


/* addtowishlist */
(function($) {
	$.fn.addtowishlist = function() {
		var $this = $(this);
		
		function init() {
			
			$this.click(function(e) {
				e.preventDefault();
				var rel = $this.attr('rel');
				checkLogin( function(data) {
					addToWishlist( rel );
				}, function(data) {
					$.colorbox.close();
					$('.login_menu').find('a').trigger('click');
				}) ;
			})

		}
		
		init();
		
		return $this ;
	}
	
})(jQuery);

/* changediameter */
(function($) {
	$.fn.changediameter = function() {
		var $this = $(this);
		
		function init() {
			
			$this.change(function(e) {
				e.preventDefault();
				document.location.href = $this.val() ;
			})

		}
		
		init();
		
		return $this ;
	}
	
})(jQuery);


/* totalcart */
(function($) {
	$.fn.totalcart = function() {
		var $this = $(this);
		
		function init() {
			
			buildTotalCart() ;

		}
		
		init();
		
		return $this ;
	}
	
})(jQuery);

function buildSimilarProductListNew(list) {
	var l = list.length;
	var h = [];
	$('.boSimilarProductList').empty();
	for( i = 0 ; i < l ; i++ ) {
		var name = list[i].name[globalLang];
		var lineName = list[i].line.name
		var image = list[i].thumb_medium;
		if(image) {
			imageSrc = image["kca:src"] ;
		} else {
			imageSrc = '' ;
		}
		var line = list[i].$ref;
		h[i] = '<li><a href="/it/catalogo/'+list[i]['timeo:uri']+'.jsp" rel="'+list[i].line['kca:id']+'" title="'+lineName+' - '+name+'" class="boByLine"><img src="'+imageSrc+'" alt="'+lineName+' - '+name+'" width="138" height="100"/></a></li>';
	}
	$('.boSimilarProductList').append(h.join(''));
	$('div.tvs-product').fadeIn();
	$('.boSimilarProductList').find('a').each(function(i) {
		$(this).unbind('click');
		$(this).colorbox({width:977, height: 450, opacity:.5,onCleanup:function() { buildTotalCart() },onComplete:function() {__init()}});
	});
}

function buildRandomProductListByLineNew(list) {
	buildProductListByLineNew(list,true);
}

function buildProductListByLineNew(list,r) {
	var l = list.length;
	var h = [];
	$('#mycarousel').empty();
	if( r ) {
		list.sort(function() {return 0.5 - Math.random()});
	}
	for( i = 0 ; i < l ; i++ ) {
		if( !list[i].family ) {
			var name = list[i].name[globalLang];
			var image = list[i].thumb_medium;
			if(image) {
				imageSrc = image["kca:src"] ;
			} else {
				imageSrc = '' ;
			}
			var line = list[i].$ref;
			var lineName = ( list[i].line ? list[i].line.name : '' );
//			h[i] = '<li><a href="/it/catalogo/'+list[i]['kca:id']+'.jsp" title="" class="tvs-openproduct"><img src="'+imageSrc+'" alt="'+lineName+' - '+name+'" width="138" height="100"/></a></li>';
			h[i] = '<li><img src="'+imageSrc+'" alt="'+lineName+' - '+name+'" width="138" height="100"/></li>';
		}
	}
	$('#mycarousel').append(h.join(''));
	$("#mycarousel").jcarousel({
		scroll : 1,
		visible : 5,
		animation : "fast",
		easing : "linear"
	});
	$('a.tvs-openproduct').each(function(i) { 
		$(this).unbind('click');
		$(this).colorbox({width:977, height: 450, opacity:.5,onCleanup:function() { buildTotalCart() },onComplete:function() {__init()}});
	});
}

function buildTotalCart(n) {
	var total = 0 ;
	if( !n ) {
		var TVScatalogue = new CatalogueProxy;
		TVScatalogue.getCart( function(data) {
			if(data.success == 'true' ) {
				if( data.details && data.details[0] ) {
					total = data.details[0].total;
				} else {
					total = 0 ;						
				}
			}
			console.log('totalcart: ' +total);
			$('li.basket_menu').find('span').html('('+total+')') ;
		} );
	} else {
		total = n ;	
		console.log('totalcart: ' +total);
		$('li.basket_menu').find('span').html('('+total+')') ;
	}
}

function emptySearchFields() {
	$('input[name="line"]').removeAttr('checked');
	$('input[name="typology"]').removeAttr('checked');
	$('input[name="use"]').removeAttr('checked');
	$('input[name="min-price"]').val('');
	$('input[name="max-price"]').val('');
}

function successImage() {
	var $img = $('#check-ok') ;
	$img.fadeIn(1000,function() { $(this).fadeOut(500) });
}

function __init() {
	$('span[class^="lang-label-"]').each(function(i) {
		( $(this).hasClass('lang-label-'+globalLang) ? '' : $(this).hide() );
	})
	//$(document).totalcart();
	$(document).___init();
}


$(document).ready(function() {
	__init();
});

