$(document).ready(function(){
	
	
	
	// FANCYBOX DETECTION AND IMPLEMENTATION
	if ( $.fancybox ) {
		$("#gallery a[rel^='prettyPhoto']").fancybox();
		$('.jqLightbox').fancybox();
	}
	
	if ( $.prettyPhoto ) {
		$("#gallery a[rel^='prettyPhoto']").prettyPhoto();
		$('.jqLightbox').prettyPhoto();
	}
	
	
	
	// MENU SCRIPT
	
	var timeout    = 500;
	var closetimer = 0;
	var ddmenuitem = 0;
	
	function jsddm_open()
	{  jsddm_canceltimer();
	   jsddm_close();
	   ddmenuitem = $(this).find('ul').css('display', 'block');}
	
	function jsddm_close()
	{  if(ddmenuitem) ddmenuitem.css('display', 'none');}
	
	function jsddm_timer()
	{  closetimer = window.setTimeout(jsddm_close, timeout);}
	
	function jsddm_canceltimer()
	{  if(closetimer)
	   {  window.clearTimeout(closetimer);
		  closetimer = null;}}

	$('#menu-main > li').bind('mouseover', jsddm_open)
	$('#menu-main > li').bind('mouseout',  jsddm_timer)

	
	document.onclick = jsddm_close;
	
	
	
	
	
	
	
	// MY BKGALLERY SCRIPT TO MANAGE BACKGROUND IMAGES
	
	// Imposta correttamente gli id delle immagini di sfondo.
	var imgCount = 1;
	$('#bkgallery img').each(function(){
		$(this).attr('id','im'+imgCount);
		imgCount++;
	});
	
	var actualimg 		= 1;
	var totalimages 	= jQuery('#bkgallery img').length;
	var skip_transition = false;
	var imagewidth 		= jQuery('#bkgallery #im1').width();
	var imageheight 	= jQuery('#bkgallery #im1').height();
	var ratio 			= imageheight/imagewidth;
	
	// Nasconde i link alle immagini successive e precedenti se c' solamente un'immagine.  
	if(totalimages == 1){ jQuery('#bkgallery a').hide(); }
	
	supersizeImage(jQuery('#bkgallery #im1'), ratio);
    
    
	jQuery(window).resize(function() {
		
		imagewidth = jQuery('#im'+actualimg).width();
		imageheight = jQuery('#im'+actualimg).height();
		ratio = imageheight/imagewidth; 
			
		supersizeImage(jQuery('#im'+actualimg), ratio);
    });
	
	jQuery(function() {
		
		if(totalimages >1 &&skip_transition == false ) {
			
			setInterval(function() {
				fadeImage('next');
			}, 5000);
			
		}
	});
	
	jQuery('#bkgallery a').click(function(){
		
		this.blur();
		
		skip_transition = false; 
		
		var action = (jQuery(this).attr('class'));
		
		fadeImage(action);
		skip_transition = true;
		
		return false;
		
	});
	
	jQuery(document).keydown(function(event) {
	 
		if (event.keyCode == '39') { //NEXT
			jQuery('#bkgallery .next').click();
			
		} else  if (event.keyCode == '37') { //PREV
			jQuery('#bkgallery .prev').click();
			
		}
		
	});
	
	// SUPERSIZE FUNCTION TO MANAGE SCREEN RATIO FIT FOR BACKGROUND IMAGE  
	function supersizeImage(image, ratios) {
		
		//alert(imagewidth + " - "+imageheight);
		var browserwidth = jQuery(window).width();
		var browserheight = jQuery(window).height();
		
		if ( imageheight == 0 ) { imageheight = jQuery(image).attr('height'); }
		
		if ((browserheight/browserwidth) >= ratios) {
			jQuery(image).height(browserheight);
			jQuery(image).width(browserheight / ratios); 
			
		} else {
			jQuery(image).width(browserwidth);
			jQuery(image).height(browserwidth * ratios);
		}
		
		jQuery(image).css('left', (browserwidth - jQuery(image).width())/2);
		
		if(jQuery(image).height()>0){
			jQuery(image).css('top', (browserheight - jQuery(image).height())/2);
		}
		
	} // -- supersizeImage --
	
	// FADING TRANSITION EFFECT
	function fadeImage(action){
		
		if ( skip_transition == false) {
			
			jQuery('#bkgallery img').fadeOut(2000);
			
			if(action == "prev") {
				actualimg = actualimg-1;
				if(actualimg < 1){actualimg = totalimages;}
				
			} else if(action == "next") {
				actualimg = actualimg+1;
				if(actualimg > totalimages){ actualimg = 1;}
			} 
			
			imagewidth 	= jQuery('#im'+actualimg).width();
       		imageheight = jQuery('#im'+actualimg).height();
			ratio 		= imageheight/imagewidth; 
			
			supersizeImage(jQuery('#im'+actualimg), ratio);
			jQuery('#im'+actualimg).fadeIn(2000);
			
		}
		
		skip_transition = false; 
		
	} // -- fadeImage --
	
	
	
	
	
	
	
	// SCROLL PANEL INITIALIZE		
	$(function(){
		$('.scroll').jScrollPane();
	});
	
	
	
	
	
	// FADE EFFECTS
 
	//portfolio thumb fadein fadeout
	$(".image img").css("opacity",1); 
	$(".image").hover(function(){
		$(this).find("img").fadeTo("slow", .3);
		$(this).find("span.title,span.description").fadeTo("slow", 1); 

	}, function(){
		$(this).find("img").fadeTo("slow", 1);
		$(this).find("span.fadeout,span.description").fadeTo("slow", 0); 
	});	
	
	//blog thumb fadein fadeout
	$(".post img").css("opacity",1); 
	
	$(".show").css("opacity",0);
	$(".post").hover(function(){
		$(this).find("div.extract, div.extract p").fadeTo("slow", 1);
		$(this).find("div.postdata,div.postdata span").fadeTo("slow", 0); 
	}, function(){
		$(this).find("div.extract, div.extract p").fadeTo("slow", 0);
		$(this).find("div.postdata, div.postdata span").fadeTo("slow", 1); 
	});
	
});





