var _lang_def = {
		IT: {
			buttons: {
				close: "Chiudi",
				send: "Invia"
			},
			formContatti: {
				nome: "nome",
				cognome: "Cognome",
				indirizzo: "Indirizzo",
				citta: "Citt&agrave",
				stato: "Stato",
				email: "Email",
				cell: "Cell.",
				tel: "Telefono",
				note: "Note",
				validation: {missingField: "inserire i campi richiesti!"}
			}
		},
		EN: {
			buttons: {
				close: "Close",
				send: "Send"
			},
			formContatti: {
				nome: "Name",
				cognome: "Surname",
				indirizzo: "Address",
				citta: "City",
				stato: "State",
				email: "Email",
				cell: "Cell.number.",
				tel: "Telephone",
				note: "Note",
				validation: {missingField: "enter the required fields!"}
			}
		}
	}
	

var scroll_is_on=false;
var scroll_time;
var scroll_element;
var scroll_offset=1;

function forgotUser(_url) {
	
	var dialog = $("<div style='background-color: black'></div>").append("<iframe backgroundColor='black' allowTransparency='true' src='"+_url+"' width='100%' height='200px'></iframe>").dialog({modal:true, width: 700, title: "reg"});;
	
	return;
	$.ajax({
			async: false,
			contentType: "application/x-www-form-urlencoded",
			dataType: 'text',
			data: null,
			
			url: _url,
			complete: function (data) {
				var dialog = $("<div></div>").append(data.responseText);
				$(dialog).dialog({modal:true, width: 400, title: "reg"});
			}
	});
}

function reg() {
	var _url = "/Common/FOControls/User/User_Registration_Container.aspx?Cod_Oggetto=&PAR1=&PAR2=&REGCOMPLETE_MESS_CODE=ECOMM_CARRELLO";
	
	var dialog = $("<div style='background-color: black'></div>").append("<iframe backgroundColor='transparent' allowTransparency='true' src='"+_url+"' width='100%' height='600px'></iframe>").dialog({modal:true, width: 700});;
	
	return;
	$.ajax({
			async: false,
			contentType: "application/x-www-form-urlencoded",
			dataType: 'text',
			data: null,
			
			url: _url,
			complete: function (data) {
				var dialog = $("<div></div>").append(data.responseText);
				$(dialog).dialog({modal:true, width: 400, title: "reg"});
			}
	});
}
$(function () {
	if (_language) LanguageDefinition= eval("_lang_def."+_language.toUpperCase())
	
	//impostaDominio(); //per ovviare ad accesso negato nel carrello ecommerce
	
	
	$("."+window._language).addClass("magenta");
	if($("#right *").length>0) 
		$("#right").show()
	else
		$("#cont").width("100%");
		
	//pagina corrente
	var slash = document.location.pathname.lastIndexOf("/");
	var pag = document.location.pathname.slice(slash+1).toLowerCase().replace(".aspx", "");
	if (!pag) pag="home";
	$("#"+pag).addClass("current")
	var kpub = getStringVar("keypub");
	if (kpub) {$("#UL"+kpub.replace("|","").replace("%7c", "")).addClass("current")}

	$(".areatext, .advertising .description").each(areatext)
	$("body").mouseup(function(){clearTimeout(scroll_time)})
 	
 	
 	
 	//preimpostazione slide 
	$(".slides").each(function(){
		if ($(this).find("#slides").length>0) return;
		
		var slides = $("<div></div>");
		$(this).find(".slide, .slides>img").each(function(){
			//$(this).appendTo(slides);
			if ($(this).hasClass("slide")) $(this).appendTo(slides);
			else{
				var s = $("<div class='slide'></div>");
				$(this).appendTo(s);
				s.appendTo(slides);
			}

		})
		var paging = '<a class="prev" href="#"><img src="skins/immagini/arrow-prev.png" alt="Arrow Prev" /></a> <a class="next" href="#"><img src="skins/immagini/arrow-next.png" alt="Arrow Next" /></a>';
		var _new = '<div id="slides"><div class="slides_container">'+slides.html()+'</div>'+paging+'</div>';
		$(this).html(_new);
		
	})
	
	
 	
 	

	$('.slides>div').slides({
		preload: true,
		preloadImage: 'skins/img/loading.gif',
		play: 10000,
		pause: 10000,
		hoverPause: true,
		animationStart: function(current){
			$('.caption').animate({
				bottom:-35
			},100);
			if (window.console && console.log) {
				// example return of current slide number
				//console.log('animationStart on slide: ', current);
			};
		},
		animationComplete: function(current){
			$('.caption').animate({
				bottom:0
			},200);
			if (window.console && console.log) {
				// example return of current slide number
				//console.log('animationComplete on slide: ', current);
			};
			
		},
		slidesLoaded: function() {
			$('.caption').animate({
				bottom:0
			},200);
			custom_slides();
		}
	});

	//pagina collezione
	$(".collezione td").click(function(){
		var dialog = $("<div style='text-align: center'></div>");
		var img = $(this).find("img").clone();
		img.attr("src", img.attr("src").replace("_ico.", "."));
		img.width("300").height("400").css("align", "center").appendTo(dialog)
		var title = img.attr("title");
		var description = "<div>"+img.attr("alt")+"</div>";
		dialog.append(description)
		//$(this).find(".description").clone().appendTo(dialog);
		$(dialog).dialog({modal:true, width: 500, title: title});
	})
	
	
	//pagina contatti
		$(".contatti form #invia").click(email);
		$(".contatti form input[name=note]").each(function(){
			$(this).replaceWith("<textarea name='note' style='"+$(this).attr("style")+"'></textarea>")
	})
 	
 	
 	
 	//pagina shop
 	ShopPage();


});


function Catalogo(){
	if (window.catalog){
		window.catalog.dialog();
		return;
	}
	//1.jpg --> 40.jpg
	var h = '<div><div class="book"><div class="b-load">';
	for (var i=0; i<=38; i++)
		h += '<div title="'+i+'"><img src="common/media/CatalogoSfogliabile/'+i+'.jpg"/></div>';
	
	
	h +="</div></div></div>";
	//alert(h)
	
	window.catalog = $(h)
	catalog.dialog({modal: true, title: 'catalogo - catalog', width: 900, height: 400});
	
	//BOOKLET 	
	$(".book").booklet({
		startingPage: 1,
		width:  820,
		height: 290,
		
		//auto:true,
		overlays: true,
		hash: true,

		keyboard: true,

		tabs:  true,
		tabWidth:  180,
		tabHeight:  20,

		manual: false,
		cursor: 'pointer' ,

		overlays: true,
		hovers:   true
	});
	$('#custom-next').click(function(e){
		e.preventDefault();
		$('#mybook').booklet("next");
	});

	$('#custom-prev').click(function(e){
		e.preventDefault();
		$('#mybook').booklet("prev");
	});
}
function custom_slides(){
	 	$(".advertising").each(function(){SlideIconPagination()})
}

function Home(){
	$("#container").addClass("home");
	
	//nascondo collezione, shop e map
	//$("#header, #collezione, #shop, #map").hide();
	$("#ULM_RG10060878, #ULM_RG10060876, #ULM_RG10060890, #header, #collezione, #shop, #map").hide();
	
	
	var m=$("#menu").html();
	$("#menu").remove();
	
	
	$("#footer").html('<div id="menu">'+m+'</div>');
	$("#menu").addClass("home");
	
	
}


function areatext(){
	if (this.clientHeight>=this.scrollHeight){
		$(this).css({"overflow-y": "hidden"})
		return;
	
	}
	var _h = '<div class="areascroll"><div class="arrowUp"></div><div class="areatextcontainer"><div class="areatext">'+	$(this).html() + '</div></div><div class="arrowDown"></div></div>';
	
	$(this).replaceWith(_h);
	
	$(".arrowDown").mousedown(function(){
		scroll_offset=1;
		scroll_element = $(".areatext");
		scrollStart();
	});
	
	$(".arrowUp").mousedown(function(){
	
		scroll_offset=-1;
		scroll_element = $(".areatext");
		scrollStart();
	});
	
	
}


function scrollStart(){
	scroll_is_on=true;
	cur=$(scroll_element).scrollTop();
	
	$(scroll_element).scrollTop(cur+scroll_offset);
	scroll_time = setTimeout(scrollStart);

}


function SlideIconPagination(){

	//crea paginazione iconizzando le immagini
	 	var _index = 0;
	 	var icons = new Array();
	 	$(".slides  img").each(function(i){
	 	
	 		icons[i] = '<img  class="paging_ico" src="'+this.src+'"/>';
	 		
	 		//$(this).parents("#slides").find(".pagination li a").html(image);
	 		//_index++;
	 	})
	 	$(".slides .pagination li a").each(function(i){
	 		$(this).html(icons[i])
	 	})
 	$(".slides .pagination").addClass("pagination_icon").show()
}


function email(){
	
	var _Req = ["nome", "cognome", "email", "note"];
	
	var validate = true;
	var _dialog = LanguageDefinition.formContatti.validation.missingField;
	$(".contatti form input, .contatti form textarea").each(function(){
		var _name = $(this).attr("name");
		$(this).removeClass("required");
		if (_Req.contains(_name) && $(this).val()=="") {
			_dialog += "<br/>- " + eval("LanguageDefinition.formContatti."+$(this).attr("name"));//$(this).attr("name");
			validate = false;
			$(this).addClass("required");
			return; 
		}
	});
	
	if (!validate) {
		$("<div>"+_dialog+"</div>").dialog({title: "alert", modal:true});
		return;
	}
	
	var _body = "";
	$(".contatti form input, .contatti form textarea").each(function(){
		_body += $(this).attr("name") + ":"+ encodeURIComponent($(this).val().replace(/\n/gi, "<br/>"))+ "\n<br/>";
	})
	
	var options = {
			smtp: "smtp.fastweb.it", 
			from: $("form input[name=email]").val(),
			//to: "avanzo@itechnologies.it", 
			to: "info@robertagandolfi.com",
			bcc: "contatto_gandolfi@multiportal.it",
			subject: "Contatto dal portale Roberta Ganfolfi",
			body: _body
	};
	$.ajax({
		async: true,
		contentType: "application/x-www-form-urlencoded",
		dataType: 'json',
		data: options,
		
		url: 'common/services/Service.aspx?Mode=sendmail',
		complete: function (data) {
			var resp = $.parseJSON(data.responseText).data;
			if (resp.result=="true") alert("richiesta inviata correttamente!");
			else{
				alert("Invio richiesta fallito, verificare l'indirizzo email inserito!")
				if (window.console && console.log && resp.message!=undefined)
					console.log(resp.message)

				if (window.console && console.log && resp.error!=undefined)
					console.log(resp.error['#cdata-section']);
			}
		}
	});
}


function ShopPage(){
	var divShopWidth= $(".divShop .product").length*(150+40);
 	if (divShopWidth>0){
 		$(".divShop").width(divShopWidth);
 		scroll($(".divShop-container"), $(".divShop-container .divShop"))
 		
 		
 	}
	
}
function scroll(scrollPane, scrollContent){
	//scrollpane parts
	//var scrollPane = $( ".scroll-pane" ),
	//	scrollContent = $( ".scroll-content" );

	//build slider
	var scrollbar = $( ".scroll-bar" ).slider({
		slide: function( event, ui ) {
			if ( scrollContent.width() > scrollPane.width() ) {
				scrollContent.css( "margin-left", Math.round(
					ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
				) + "px" );
			} else {
				scrollContent.css( "margin-left", 0 );
			}
		}
	});

	//append icon to handle
	var handleHelper = scrollbar.find( ".ui-slider-handle" )
	.mousedown(function() {
		scrollbar.width( handleHelper.width() );
	})
	.mouseup(function() {
		scrollbar.width( "100%" );
	})
	.append( "<span class='ui-icon ui-icon-grip-dotted-vertical'></span>" )
	.wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();

	//change overflow to hidden now that slider handles the scrolling
	scrollPane.css( "overflow", "hidden" );

	//size scrollbar and handle proportionally to scroll distance
	function sizeScrollbar() {
		var remainder = scrollContent.width() - scrollPane.width();
		var proportion = remainder / scrollContent.width();
		var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );
		scrollbar.find( ".ui-slider-handle" ).css({
			width: handleSize,
			"margin-left": -handleSize / 2
		});
		handleHelper.width( "" ).width( scrollbar.width() - handleSize );
	}

	//reset slider value based on scroll content position
	function resetValue() {
		var remainder = scrollPane.width() - scrollContent.width();
		var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
			parseInt( scrollContent.css( "margin-left" ) );
		var percentage = Math.round( leftVal / remainder * 100 );
		scrollbar.slider( "value", percentage );
	}

	//if the slider is 100% and window gets larger, reveal content
	function reflowContent() {
			var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
			var gap = scrollPane.width() - showing;
			if ( gap > 0 ) {
				scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
			}
	}

	//change handle position on window resize
	$( window ).resize(function() {
		resetValue();
		sizeScrollbar();
		reflowContent();
	});
	//init scrollbar size
	setTimeout( sizeScrollbar, 10 );//safari wants a timeout

}
