/* ##### Filmedia - JavaScript Document ##### */

/* 
Methods for resizing the flash stage at runtime.

setFlashWidth(divid, newW)
divid: id of the div containing the flash movie.
newW: new width for flash movie

setFlashWidth(divid, newH)
divid: id of the div containing the flash movie.
newH: new height for flash movie

setFlashSize(divid, newW, newH)
divid: id of the div containing the flash movie.
newW: new width for flash movie
newH: new height for flash movie
*/
function setFlashWidth(divid, newW){
	document.getElementById(divid).style.width = newW+"px";
}
function setFlashHeight(divid, newH){
	document.getElementById(divid).style.height = newH+"px";
    fixheight();		
}
function setFlashSize(divid, newW, newH){
	setFlashWidth(divid, newW);
	setFlashHeight(divid, newH);
}

/* Fix div height */
function fixheight () {
	addDebug();
	
	log('FIXHEIGHT');
	
	if ($('#homepage').length > 0) fixHome(); 
	if ($('#catalogue').length > 0) fixCatalogue();
	
	if ($('#pagebody').length > 0) fixPagebody();
}
$(document).ready(fixheight);
$(window).bind('resize', fixheight);

function fixPagebody() {
	var pbh = $('#pagebody > .maincol').height();// altezza div contenuti in #pagebody
	
	var wraph = $(window).height();// altezza pagina
	var pbtop = $('#pagebody').offset().top;// offset superiore del div #pagebody
	
	var pbottom = $('#pagebody').css('padding-bottom');
	var ptop = $('#pagebody').css('padding-top');
	var totpad = parseInt(pbottom) + parseInt(ptop);// padding complessivo di #pagebody
	
	//log((pbh + totpad) + " - " + (wraph - pbtop));
	
	var endh = Math.max((pbh + totpad), (wraph - pbtop));// verifico l'altezza ottimale
	
	endh = endh - totpad;
	$('#pagebody').css( { 'height': endh } );
}

function fixHome () {
	//alert('homepage');
	
	var contenth = $("#strip > .stripcontent").outerHeight();
	var videoh = $("#stripdetail > .sh_body").outerHeight();
	videoh += 25;//parseInt($('#stripdetail').css('top'));
	log("contenth: " + contenth + " vs videoh: " + videoh);
	
	var endh = Math.max(contenth, videoh);
	$('#strip').css( { 'height': endh } );
}

function fixCatalogue () {
	//alert('catalogue');
	var striph = $('#stripdetail').height();
	var striptop = $('#stripdetail').css('top');
	var listh = $('#stripitems').outerHeight();// altezza div elenco categorie
	
	var h = striph - listh + parseInt(striptop);
	//log(striph + " - " + listh + " - " + parseInt(striptop) + " > " + h);
	
	var dh = $(window).height();
	var toph = $('#pagetop').height();
	var pb = $('#pagebody').css('padding-bottom');
	var h2 = dh - toph - parseInt(pb);
	//log(dh + " - " + toph + " - " + parseInt(pb) + " > " + h2);
	var endh = Math.max(h, h2);
	var mh = $('#mainc').outerHeight();
	//log(endh + " vs " + mh);
	if (endh > 0 && endh > mh) {
		$('#main').css( { 'height': endh } );
	}
	
	// verifico l'altezza del box 'a sbalzo'
	$('#stripdetail > .sh_body').css( { 'height': 'auto' } );
	var bodyh = $('#stripdetail > .sh_body').outerHeight();// altezza div contenuti (thumbnails)
	var bottomh = $('#stripdetail > .sh_bottom').outerHeight();// altezza blocco bottom
	var toph = $('#stripdetail > .sh_top').outerHeight();// altezza blocco top
	var minh = listh - bottomh - toph - parseInt(striptop) + 11;// altezza minima div contenuti (thumbnails)
	
	log(bodyh + " vs. " + + minh);
	
	var endh = Math.max(bodyh, minh);
	$('#stripdetail > .sh_body').css( { 'height': endh } );
}

function addDebug() {
	return false;
	if ($('#debugwindow').length > 0) return;
	div = $("<div id='debugwindow'>").html("##### DEBUG WINDOW #####");
	$("body").prepend(div);
	
	$('#debugwindow').click(function() { $(this).remove(); addDebug(); });
}

function log(msg) {
	$('#debugwindow').append('<p>' + msg + '</p>');
}
function print_mail_link(nome, dominio, label){
    var email = nome+'@'+dominio;
    if(!label) label = email;
    document.write('<a href="mailto:'+email+'">'+label+'</a>');
}



