function showLoader(sel) {
	var target= $(sel);
	var loader= $('<div id="loader"></div>');
	loader.fadeTo("fast", 0.9);
	loader.css({
       width: target.width() + 'px',
        height: target.height() + 'px'
 	}).hide().appendTo(target);
 	loader.show();
}
function linksExternal(){
	$('a[@rel=external]').each(function(i){ 
  		this.target = "_blank"; 
  	});
}
function defaultFocus() {
	$('input:text.defaultfocus').each(function(){ 
  		 this.focus();
  	});
}
function resizeImg(sel,newWidth,newHeight){
	var oldWidth= 0;
	var oldHeight= 0;
	var ratio= 0;
	$(sel).each(function(i){
		var pic= $(this);
		pic.load(function(){alert($(this).width())})
		oldWidth= pic.width();
		//alert(oldWidth);
		oldHeight= pic.height(); 
		ratio = oldWidth/oldHeight;
		if (newWidth/newHeight > ratio) {
		   newWidth= Math.round(newHeight*ratio);
		} else {
		   newHeight= Math.round(newWidth/ratio);
		}
		pic.attr({ width: newWidth, height: newHeight });
	});
}
var modalWindow = {  
    parent:"body",  
    windowId:null,  
    content:null,  
    width:null,  
    height:null,
	playerId:null,  
    close:function()  
    {  
    	if(typeof swfobject === 'object' && this.playerId !== null){
	   			swfobject.removeSWF(this.playerId);
		}
        $(".modal-window").remove();  
        $(".modal-overlay").remove();  
    },  
    open:function()  
    {  
        var modal = "";  
        modal += "<div class=\"modal-overlay\"></div>";  
        modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;\">";  
        modal += this.content;  
        modal += "</div>";      
  
        $(this.parent).append(modal);  
  
        $(".modal-window").append("<a class=\"close-window\"></a>");  
        $(".close-window").click(function(){modalWindow.close();});  
        $(".modal-overlay").click(function(){modalWindow.close();});  
    }  
}; 
var openMyModal = function(content)
	{
		modalWindow.windowId= 'mymodal';
		modalWindow.playerId= 'myytplayer';
		modalWindow.width= 425;
		modalWindow.height= 381;
		modalWindow.content= content;
		modalWindow.open();
};	
 
function youtube(){
	var url= 'http://www.bracelli.net/youtube';
	var links= $('.box-content a').filter(function(){
    	return this.href.match(/https?:\/\/www\.youtube\.com\/watch/);
	});
	links.each(function(i){ 
		xhr(this);
	});
	function xhr(link){
		$.ajax({
	   		type: "POST",
	   		timeout:20000,
	   		url:  url,
	   		dataType: 'json',
	   		data: "url="+link.href,
	   		success: function(msg){
	   			if(typeof msg.error!== 'undefined'){
	   				return;	
	   			}
	   			$(link).html('<img src="'+msg.thumbs[0].url+'" alt="'+$(link).text()+'" width="40" height="30" title="'+msg.description+'" />');
	   			$(link).click(function(){
	   				flash(msg.urlplayer);
				   	return false;	
	   			});
     		},
     		beforeSend: function(){
     			$(link).html('<img src="images/small-loader.gif" alt="'+$(link).text()+'" width="40" height="30" title="sto caricando il video" />');
     		},
	   		error:function(xhr, str, er){
				return;
			}
 		});	
	}
	function flash(urlplayer){
		openMyModal('<div id="ytapiplayer">Devi avere Flash player 8+ e JavaScript attivo per vedere questo video.</div>');
		var params = { allowScriptAccess: "always",bgcolor: "#cccccc"};
		var atts = { id: "myytplayer" };
		swfobject.embedSWF(urlplayer+'&enablejsapi=1',
                         "ytapiplayer", "425", "344", "8", null, null, params, atts);
	}
}
$(function() {
	$('#container').css('-moz-border-radius','5px');
	$('#container').css('-webkit-border-radius','5px'); 
	linksExternal();
	$('#navigation a').hover(function(){ 
			$(this).css({background:'#8398A9'});
  			$(this).fadeTo('fast', 0.9);
		  },function(){ 
			$(this).css({background:'#6791B7'});
	}
	);
	$('a[@rel*=lightbox]').lightBox({
	overlayBgColor: '#282619',
	overlayOpacity: 0.9,
	imageBlank: '/images/lightbox-blank.gif',
	imageLoading: '/images/lightbox-loading.gif',
	imageBtnClose: '/images/lightbox-close.gif',
	imageBtnPrev: '/images/lightbox-prev.gif',
	imageBtnNext: '/images/lightbox-next.gif',
	containerResizeSpeed: 350,
	txtImage: 'Immagine',
	txtOf: 'di'
   });
youtube();
       $('ul.gallery li').fadeTo("slow", 0.6);  
       $('ul.gallery li').hover(function(){  
	   $(this).fadeTo("slow", 1.0);  
	   },function(){  
	   $(this).fadeTo("slow", 0.6);  
       });  
});