/**
 * General Init Functions
 * 
 * @author dahito
 */

jQuery(function($) {
	$(document).ready(function(){
		/* Splash screen */
		if(is_home){
			var splash = $.cookie('splash');
			if (splash == null){
				$('#splash-wrapper').show(0);
				$('#footer').hide(0);
			}
			$('#splash-enter').click(function(event){
				$.cookie('splash', 1);
				$('#splash-wrapper').fadeOut('slow');
				$('#footer').fadeIn('slow');
				event.preventDefault();
			});			
			/* Home scroll */
			try {
				$("#gk_is-homeslide > ul").MakiScroll();
			}catch(err){}
		}
		
		/* Auto Height */
		$('#content').each(function(){
			h = $(this).height();
			sh = $('#sidebar').height();
			if(h < sh){
				$(this).css('min-height', sh+'px');
			}
		});
		
		/* Adjust css menu */
		$('#nav-main  li > ul > li:last-child').css('margin-bottom', '0');
		$('#nav-main > li:first-child, #nav-main > li:eq(3), #nav-main > li:eq(6)').css('margin-right','60px');
		$('#nav-main > li:last-child').css('margin','0');
		$('#nav-main > li:first-child').css('margin-left','27px');
		
		/* IE 7 & IE 6 */
		if($.browser.msie && $.browser.version < 8){
			$('input[type="hidden"]').css('border',0);
			$('#nav-main li ul li a span, .menu-sidenav li a span, .search-box h3 span').each(function(){
				h = $(this).height();
				ph = $(this).parent().height();
				$(this).css('display','block').css('margin-top', (ph-h)/2 + 'px');
			});
			$('#bottom').hide(0).show(0);
			
			if ($.browser.msie && $.browser.version < 7) {
                                // Force IE6 css via script
				$('head').append('<link rel="stylesheet" href="'+template_path+'/css/custom_ie6.css" type="text/css" />');
				$(document).pngFix();
				$('#nav-main > li:first-child').css('margin-left','17px');
				$('#nav-main').hide(0).show(0);
				$('.contentheading.news').css('border-bottom','1px solid #ccc');
				$('.contentheading.news').css('border-top', '2px solid #999');
				$('.contentheading.news').css('background', '#f2f2f2');
				$('.contentheading.news .title').css('background','none');
				$('.news.componentheading').css('text-align','center').css('font-size','1.4em');
				$('table.tab').css('width','98%');
			}
		}
		
		/* Result List */
		$("a.view-detail").click(function(event){
			$(this).next().slideToggle();
			event.preventDefault();
		});	
		
		/* Login Form */
		$("#form-login input").focus(function(event){
			if( $(this).val() == 'username' || $(this).val() == '__pass' )
				$(this).val('');
			event.preventDefault();
		});	

		/* Search Form */
		$.each( $('#search-brand select') , function(){
			opt = $(this).children();
			if( parseInt(opt.size()) <= 1){
			  $(this).hide();
			}
		});		
		
		/* System Messages Auto FadeOut */
		setTimeout(function(){
			$(".fade").fadeOut("slow");	
		}, 20500); //cambiato il tempo di fade da 2,5 sec a 20,5 sec
		
		/* User profile */
		$("#user-profile button#edit").click(function(){
			var edit = $(this);
			var save = $(this).parent().find('#save');		
			if (edit.text() == "Annulla"){
				edit.text("Modifica");	
				save.hide(0);			
			}
			else{
				edit.text("Annulla");
				save.show(0);			
			}
			$('#user-profile form .passwd').toggle(0);
			
			$('#user-profile form span.editable').each(function(){
				if (edit.text() == "Annulla") {
					$(this).hide(0);
					$(this).prev().show(0);
				}
				else{
					$(this).show(0);
					$(this).prev().hide(0);
				}
			});	
		});
		
		/* Tooltip */		
		$("[title]").mbTooltip({
	    	opacity : .80,       //opacity
	    	wait:400,           //before show
	    	cssClass:"default",  // default = default
	    	timePerWord:70,      //time to show in milliseconds per word
	    	hasArrow:false,			// if you whant a little arrow on the corner
	    	hasShadow:true,
	    	imgPath:"images/",
	    	anchor:"mouse", //"parent"  you can ancor the tooltip to the mouse position or at the bottom of the element
	    	shadowColor:"black", //the color of the shadow
	    	mb_fade:200 //the time to fade-in
	    });
	});	
});

