// JavaScript Document
$(document).ready(function(){
	main("body");
});

function main(context) {
	//alert('Ready!');
	
	
		
	//TARGET _BLANK
	$("a._blank",context).click(function(){this.target="_blank";});
	
	
	
	//NASCONDI ADVICE
	$("span.close").click(function(){$(this).parent().parent().fadeOut("slow");});
	$("div.info").click(function(){$("div.info").fadeOut("slow");});
	if ($("div.info").html()){setTimeout('$("div.info").fadeOut("slow");',5000);}
	
	
	
	//DISABILITO IL TASTO DESTRO PER LE IMMAGINI DELLA PAGINA
	$("img",context).bind("contextmenu",function(e){return false;});
	
	
	
	//SLIDESHOWS
	$("#inner_slideshows img",context).imgpreload(function(){
		//ATTIVO LO SLIDESHOW
		$("#inner_slideshows",context).cycle({
			fx					: 'fade', 
			sync				: 1,
			speed				: 1000, 
			timeout				: 6000,
			//cssAfter			: "currentSlide",
			//cleartype			: !$.support.opacity,
			cleartypeNoBg		: false,
			pause				: 1
			//prev				: '#prev_slideshow', 
    		//next				: '#next_slideshow',
			//pager				: '#nav_slideshow',
	        //pagerAnchorBuilder	: slideshowPagerFactory
			/*before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
					$(currSlideElement).find(".caption").fadeOut(2000);
					//console.log("prima "+$(currSlideElement).find(".caption").attr("id"));
				},*/
			/*after: function(currSlideElement, nextSlideElement, options, forwardFlag) {
					$(nextSlideElement).find(".caption").fadeIn(2000);
					//console.log("dopo "+$(nextSlideElement).find(".caption").attr("id"));
				}*/
		});
	});
	
	
	
	//MENU
	$(".no_click").click(function(event){
		event.preventDefault;
		return false;	
	});
	var currentMenu = $("ul#mainmenu").find("li.open").children("ul");
	$("ul#mainmenu li").hoverIntent(
		  function () {
			$(this).addClass("hover");
			$(this).children("ul").slideDown(400);
			if(($(this).find("ul:first").attr("id") == "accessori_menu") || ($(this).find("ul:first").attr("id") == "polimeri_menu")) {
				var tmpWidth = $(this).width();
				$(this).find("ul:first").css("left","auto");
				$(this).find("ul:first").css("right",-tmpWidth);
			}
		  }, 
		  function () {
			$(this).removeClass("hover");
			$(this).children("ul").slideUp(400);
		  }
		);
	
	
	
	//GESTIONE SEZIONI INTERNE
	$("#distributors ul#buttons li a").click(function(event){
		event.preventDefault;
		if($(this).hasClass("current")) {
			//do nothing...
		} else {
			$(this).parents("ul").find("a.current").removeClass("current");
			$(this).addClass("current");
			
			var sectionToShow = $(this).attr("href");
			$("#distributors .section:visible").fadeOut("fast",function(){
				$(sectionToShow).fadeIn("fast");	
			});
		}
		return false;	
	});
	
	
	
	//FANCYBOX
	$("a.fancyimage").fancybox({
		'padding'		:	0,
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		//'title'			: 	getTitle,
		'overlayShow'	: 	true,
		'overlayOpacity': 	0.7,
		'overlayColor'	: 	'#000',
		'cyclic'		:	true,
		/*'titlePosition'	: 	'over',*/
		/*'titleFormat'	: 	function getTitle(title, currentArray, currentIndex, currentOpts) {			
			if (title == "") {
				title = $(currentArray[currentIndex]).find(".caption").html();
			}
			return '<div class="fancybox-title-over" id="fancybox-title" style="bottom: 0px; left: 0px; right: 0px; display: block;"><span id="fancybox-title-over">'+ title +'</span></div>';
		},*/
		'onComplete'	:	function() {
			$("#fancybox-wrap").hover(function() {
				$("#fancybox-title").fadeIn("fast");
			}, function() {
				$("#fancybox-title").fadeOut("fast");
			});
		}
	});
	$("a.fancycontent",context).fancybox({
		'padding'		: 	30,
		'autoScale'		: 	false,
		'autoDimensions':	false,
		'width'			:	330,
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'overlayOpacity':	0.7,
		'overlayColor'	:	'#000',
		'speedIn'		:	500,
		'speedOut'		:	500
	});
	$("a.fancyyoutube",context).each(function(event){
		$(this).fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'overlayShow'	: 	true,
			'overlayOpacity': 0.7,
			'overlayColor'	: '#000',
			'speedIn'		: 500,
			'speedOut'		: 500,
			'title'			: '',
			'width'			: 480,
			'height'		: 360,
			'href'			: $(this).attr("href").replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {
				'wmode'				: 'transparent',
				'allowfullscreen'	: 'true',
				'autoplay		'	: 'true',
				'loop'				: 'true'
			}
		});
	});
	$("#credits",context).fancybox({
		'autoDimensions':	false,
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'overlayOpacity':	0.7,
		'overlayColor'	:	'#000',
		'speedIn'		:	500,
		'speedOut'		:	500,
		'width'			:	300,
		'height'		:	400
	});
	$(".company",context).fancybox({
		'padding'		: 	30,
		'autoDimensions':	false,
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'overlayOpacity':	0.7,
		'overlayColor'	:	'#000',
		'speedIn'		:	500,
		'speedOut'		:	500,
		'width'			:	300,
		'height'		:	400
	});
	$("#privacy",context).fancybox({
		'padding'		: 	30,
		'autoDimensions':	false,
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'overlayOpacity':	0.7,
		'overlayColor'	:	'#000',
		'speedIn'		:	500,
		'speedOut'		:	500,
		'width'			:	300,
		'height'		:	400
	});
	$(".gmap_link",context).fancybox({
		'padding'		: 0,
		'titleShow'		:  false,
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'overlayOpacity':	0.7,
		'overlayColor'	:	'#000',
		'speedIn'		:	500,
		'speedOut'		:	500,
		'content'		:	'<div id="fancybox-gmap" style="width:640px; height:480px; overflow:hidden;"></div>',
		'onComplete'	: 	function(element){
				$("#fancybox-gmap").gMap({
							controls: ["GLargeMapControl3D"], 
							markers: [{ address: gmapAddress,
										html: gmapHtml,
										popup: true }],
							icon: { image: url+'site/template/images/google-map-placeholder.png',
									iconsize: [120, 58],
									iconanchor: [60, 0],
									infowindowanchor: [60, 6] },
							zoom: 11 
				});
			}
	});
	
	
	
	//GOOGLE MAPS
	var gmap_iframe = '<iframe width="640" height="480" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.it/maps?f=q&amp;source=s_q&amp;hl=it&amp;geocode=&amp;q=Fimap+s.n.c.+Via+Villa+Garibaldi,+16%2Fb+46027+San+Benedetto+Po+(MN)+-+Italia&amp;aq=&amp;sll=45.036519,10.934834&amp;sspn=0.007309,0.016512&amp;vpsrc=6&amp;ie=UTF8&amp;hq=fimap+snc+via+villa+garibaldi+16-b&amp;hnear=San+Benedetto+Po+Mantova,+Lombardia&amp;t=m&amp;cid=829330017642285425&amp;ll=45.046844,10.935516&amp;spn=0.029108,0.054846&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe>';
	
	$("#gmap_link, #gmap_link2",context).fancybox({
		'padding'		: 0,
		'titleShow'		:  false,
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'overlayOpacity':	0.7,
		'overlayColor'	:	'#000',
		'speedIn'		:	500,
		'speedOut'		:	500,
		'content'		:	'<div style="overflow:hidden;">'+gmap_iframe+'</div>'
	});
	
	var gmap_iframe2 = '<iframe width="640" height="480" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.it/maps?f=q&amp;source=s_q&amp;hl=it&amp;geocode=&amp;q=Via+dei+Chiosi,+18+20040+Cavenago+di+Brianza+(MB)+-+Italia&amp;aq=&amp;sll=45.036519,10.93483&amp;sspn=0.007309,0.016512&amp;vpsrc=6&amp;ie=UTF8&amp;hq=&amp;hnear=Via+dei+Chiosi,+20040+Cavenago+di+Brianza+Monza+e+Brianza,+Lombardia&amp;t=m&amp;ll=45.587374,9.413824&amp;spn=0.028832,0.054846&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe>';
	
	$("#gmap_link3",context).fancybox({
		'padding'		: 0,
		'titleShow'		:  false,
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'overlayOpacity':	0.7,
		'overlayColor'	:	'#000',
		'speedIn'		:	500,
		'speedOut'		:	500,
		'content'		:	'<div style="overflow:hidden;">'+gmap_iframe2+'</div>'
	});
	
	
	
	//SCROLLBAR
	/*$('.scrollpanel').jScrollPane({
		scrollbarWidth: 4,
		scrollbarMargin: 7
	});*/
	
	
	
	//FLOWPLAYER
	if($("#player").length > 0){
		var video = $("#player").attr("title");
		$("#player").attr("title",$("title").text());
		$f("player",url+"files/videos/flowplayer.swf", {
			
			clip: video
			
			// use a minimalistic controlbar
			/*plugins:  {
				controls:  {
					backgroundGradient: 'none',
					backgroundColor: 'transparent',
					all:true
				}
			}*/	
		
		});
	}
	
	
	
	//VALIDAZIONE DELLE FORM
	/*$("form",context).each(function(){
			$(this).validate({
				submitHandler: function(form) {
					$(form).submit();		//submit in postback
					//formSubmit(form);		//submit in ajax
					//return false;
			}
		});
	});*/
	$("form",context).each(function(){
		$(this).validate();
	});
	
	
	
	//RESET DEI CAMPI DELLA FORM
	$("#reset",context).click(
		function () {
			$(this).parent("form").clearForm();
    	}
	);
}



function CDR(requestedUrl,responseType) {
	if((requestedUrl != null) && (responseType != null)) {
		var requestedUrl = url + "proxy.php?url="+encodeURIComponent(requestedUrl);
        
		$.ajax({
			type: "GET",
			url: requestedUrl,
			dataType: responseType,
			success: function(response){
				return response;
			}
		});
	}
}



function ltIE7() {
	var semaphore;
	semaphore = !$.browser.msie || ($.browser.msie && ($.browser.version > 6));
	
	return semaphore;
}



function slideshowPagerFactory(idx, slide) {
	//var s = idx > 2 ? ' style="display:none"' : '';
	//return '<li'+s+'><a href="#">'+(idx+1)+'</a></li>';
	return '<li><a href="#">'+(idx+1)+'</a></li>';
}

