$(document).ready(function() {
	
	/* 
	$(".anyClass").jCarouselLite({
			btnNext: ".nextbtn",
			btnPrev: ".prevbtn"
	});
	*/
	setTimeout("correggiAltezzaDetail()", 500);
	
	jQuery.data(document.body, 'scroller', 0);
	$('.gallery .scroller .prev').click(function(){
		if (jQuery.data(document.body, 'scroller') > 0){
			jQuery.data(document.body, 'scroller', jQuery.data(document.body, 'scroller') - 1);
			$('.gallery .scroller ul').animate({left: '+=55'}, 200, 'linear');
		}
		return false;
	});
	$('.gallery .scroller .next').click(function(){
		if ($('.gallery .scroller ul li').length > 4 && jQuery.data(document.body, 'scroller') <= $('.gallery .scroller ul li').length - 4){
			jQuery.data(document.body, 'scroller', jQuery.data(document.body, 'scroller') + 1);
			$('.gallery .scroller ul').animate({left: '-=55'}, 200, 'linear');
		}
		return false;
	});
	
	$('.gallery .scroller .toc').click(function(){
		$('.gal_item').hide();
		$($(this).attr('href')).show();
		$('.screen').animate({'height': $($(this).attr('href')).height()}, 500, 'linear');
		return false;
	});
	
	$('.clickup').click(function(){
		$('html, body').animate({scrollTop: 0}, 1000);
		return false;
	});
	
	$('.gridlist a').click(function(){
		$('div.prods ul.listinglist, div.prods ul.listingprods')
			.removeClass($(this).hasClass('list') ? "listingprods" : "listinglist")
			.addClass($(this).hasClass('list') ? "listinglist" : "listingprods");
		return false;
	});
	
	$('.showCheckout').fancybox({
		'href'	: '../ajax/checkout.php',
		'modal'	: true,
		'width'	: 800,
		'height': 400,
		'centerOnScroll': true,
		'hideOnOverlayClick': true,
		'autoDimensions'	: true,
		'overlayOpacity': 0.8
	});
	
	$('.fancy_close').live('click', function(){
		$.fancybox.close();
	});
	
	$('.req').blur(function(){
		$(this).removeClass("warning");
		if ($(this).val() == ''){
			$(this).addClass("warning");
		}
	});
	
});

function correggiAltezzaDetail(){
	var h = $($('.gallery .screen .gal_item').get(0)).height();
	$('.gallery .screen').height(h);
}

function aggiungiAlCarrello(id, tg, ds_tg, cl, qt, max_qt, pr, prpp, tt, mr, img){
	if (tg == ""){ alert("Scegli la taglia del prodotto"); return false; }
	if (qt <= 0){ alert("Scegli la quantità di articoli da aggiungere al carrello"); return false; }
	$('#carrello_sidebar').show();
	$('#sidebar_cart').addClass("loading");
	$.post("../ajax/carrello.php", {"task": "add", "id": id, "taglia": tg, "ds_taglia": ds_tg, "colore": cl, "quant": qt, "max_disp": max_qt, "prezzo": pr, "prezzo_pieno": prpp, "titolo": tt, "marca": mr, "thumb": img}, function(data){
		$('#sidebar_cart').html(data).removeClass("loading");
		ricaricaCarrelloTop();
		vaiAlCarrello();
	});
	
	return false;
}

function ricaricaCarrelloTop() {
	$.ajax({
		  url: "../_include/_carrello_top.php",
		  success: function(data){
			$("#cont_carrello_top").html(data).fadeIn("slow");
		  }

	});
	}

function svuotaCarrello(){
	$('#sidebar_cart').addClass("loading");
	$.post("../ajax/carrello.php", {"task": "empty"}, function(data){
		$('#sidebar_cart').html(data).removeClass("loading");
		ricaricaCarrelloTop();
	});
	
	return false;
}
function rimuoviDalCarrello(id, tg, cl){
	$('#sidebar_cart').addClass("loading");
	$.post("../ajax/carrello.php", {"task": "removeItem", "id": id, "taglia": tg, "colore": cl}, function(data){
		$('#sidebar_cart').html(data).removeClass("loading");
		ricaricaCarrelloTop();
	});
	
	return false;
}

function cambiaTaglia(obj){
	if (obj.value != ""){
		var val = obj.options[obj.selectedIndex].lang;
		var old_sel = document.po_form.quant;
		var new_sel = eval('document.po_form.quant_'+val);
		for (var i = old_sel.options.length - 1; i >= 0; i--){ old_sel.options[i] = null; }
		if (new_sel.length == 0){
			old_sel.length =  1;
			old_sel.options[0].text = "---";
			old_sel.options[0].value = "";
		} else {
			old_sel.length =  new_sel.length;
			for (i = 0; i < new_sel.length; i++){
				old_sel.options[i].text = new_sel.options[i].text;
				old_sel.options[i].value = new_sel.options[i].value;
			}
		}
	}
}

function checkSearch(val){
	if (document.searchForm.q.value != val && document.searchForm.q.value != ''){
		document.searchForm.submit();
	}
	return false;
}

// Inizializzo gli accordion
ddaccordion.init({ //top level headers initialization
	headerclass: "expandable", //Shared CSS class name of headers group that are expandable
	contentclass: "categoryitems", //Shared CSS class name of contents group
	revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
	mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
	collapseprev: true, //Collapse previous content (so only one open at any time)? true/false 
	defaultexpanded: [0], //index of content(s) open by default [index1, index2, etc]. [] denotes no content
	onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
	animatedefault: false, //Should contents open by default be animated into view?
	persiststate: true, //persist state of opened contents within browser session?
	toggleclass: ["", "openheader"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
	togglehtml: ["prefix", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
	animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
	oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
		//do nothing
	},
	onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
		//do nothing
	}
});

ddaccordion.init({ //2nd level headers initialization
	headerclass: "subexpandable", //Shared CSS class name of sub headers group that are expandable
	contentclass: "subcategoryitems", //Shared CSS class name of sub contents group
	revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click" or "mouseover
	mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
	collapseprev: true, //Collapse previous content (so only one open at any time)? true/false 
	defaultexpanded: [], //index of content(s) open by default [index1, index2, etc]. [] denotes no content
	onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
	animatedefault: false, //Should contents open by default be animated into view?
	persiststate: true, //persist state of opened contents within browser session?
	toggleclass: ["opensubheader", "closedsubheader"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
	togglehtml: ["none", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
	animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
	oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
		//do nothing
	},
	onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
		//do nothing
	}
});

// Inizializzo il menu
ddsmoothmenu.init({
	mainmenuid: "smoothmenu1", //menu DIV id
	orientation: 'h', //Horizontal or vertical menu: Set to "h" or "v"
	classname: 'ddsmoothmenu', //class added to menu's outer DIV
	//customtheme: ["#1c5a80", "#18374a"],
	contentsource: "markup" //"markup" or ["container_id", "path_to_menu_file"]
});


if (false){
	// Inizializzo i banner scorrevoli
		featuredcontentslider.init({
			id: "slider1",  //id of main slider DIV
			contentsource: ["inline", ""],  //Valid values: ["inline", ""] or ["ajax", "path_to_file"]
			toc: "#increment",  //Valid values: "#increment", "markup", ["label1", "label2", etc]
			nextprev: ["&nbsp;", "&nbsp;"],  //labels for "prev" and "next" links. Set to "" to hide.
			revealtype: "click", //Behavior of pagination links to reveal the slides: "click" or "mouseover"
			enablefade: [true, 0.2],  //[true/false, fadedegree]
			autorotate: [true, 3000],  //[true/false, pausetime]
			onChange: function(previndex, curindex){  //event handler fired whenever script changes slide
				//previndex holds index of last slide viewed b4 current (1=1st slide, 2nd=2nd etc)
				//curindex holds index of currently shown slide (1=1st slide, 2nd=2nd etc)
			}
		});
}


