/*********************************************
 *** Copyright Luca Canella 2009		   ***
 *********************************************
 Sono vietati la riproduzione e l'uso senza l'autorizzazione dell'autore.
 @author Luca Canella, www.lucacanella.org
 */

//height of head and foot divs
var headHeight = 150; //10 bottom margin
var footHeight = 170; //10 top margin
var contentDivIdentifier = 'div#content,div#main-content';
var contentDivMinHeight = 420;

//funzione che ridimensiona il contentDiv
function contentResize() {
	var screenHeight = document.body.clientHeight;	
	var contentHeight = screenHeight - headHeight - footHeight;
	if(contentHeight < contentDivMinHeight) contentHeight = contentDivMinHeight;
	$(contentDivIdentifier).css("min-height",contentHeight+"px");
}

function getImgPE(path) {
	if( path == null )
		return;
	var pne = null;
	var regexp = null;
	regexp = new RegExp('^([a-zA-Z0-9_\\\/-]+)\.(jpg|gif|png)$');
	pne = path.match(regexp);
	pne['path'] = pne[1];
	pne['ext'] = pne[2];
	return pne;
}

//bind della funzione di ridimensionamento con eventi di ridimensionamento della finestra e ready del document
$(window).resize( function () { contentResize(); });

function message(msg) { $("#content").html($("#content").html()+msg+"<br>"); }
var btt = new Array();
var idx = -1;

function bttEnter(_btt) { _btt.bttImg.attr('src',_btt.srcMouseEnter); }
function bttOut(_btt) { _btt.bttImg.attr('src',_btt.originalSrc); }

function imgPreload(_btt) {
	var imgEnt = new Image();
	imgEnt.src = _btt.srcMouseEnter;
	var imgClk = new Image();
	imgClk.src = _btt.srcClick;
}

function bindFunctions(_btt) {
	_btt.bttImg.mouseenter(function () { bttEnter(_btt); });
	_btt.bttImg.mouseout(function () { bttOut(_btt); });
}

//document.ready
$(document).ready( function () { 
 	contentResize();

	$(".btt,.registrati,.logout").each( function () {
		idx++;
		btt[idx] = new Array();
		btt[idx]._link = $(this).children('a');
			btt[idx]._link.attr('title',""); //tolgo title
		btt[idx].bttImg = btt[idx]._link.children('img');
			btt[idx].bttImg.attr('alt',""); //tolgo l'alt
		btt[idx].originalSrc = btt[idx].bttImg.attr('src');
		btt[idx].srcPNE = getImgPE( btt[idx].originalSrc );
		if( btt[idx].srcPNE == null )
			return;
		btt[idx].srcMouseEnter = btt[idx].srcPNE['path']+"_h."+btt[idx].srcPNE['ext'];
		imgPreload(btt[idx]);
		bindFunctions(btt[idx]);
	});
	
	$("ul#sottomenu li").each( function () {
		var menuLink = $(this).children("a").attr('href');
		if(document.location.href.match(menuLink+"$"))
			$(this).addClass("selected");
		$(this).click(function () { document.location.href = menuLink; });
	});
	
	$(".datepicker").datepicker({
		dateFormat: "yy-mm-dd",
		changeMonth: true,
		changeYear: true,
		yearRange: '-99:+0'
	});

	var $tabs= $("#tabs").tabs(); 
	$('.toModAcc').click(function() { // bind click event to link
    $tabs.tabs('select', 2); // switch to third tab
    return false;
});

});
