/* Copyright (c) 2009 Matteo amoK Musci (http://amok.zona13.com)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * $LastChangedDate: Fri Feb 12 17:58:34 CET 2010 @749 /Internet Time/
 *
 * Version 1.0
 */

var obj; // L'oggeto da ridimensionare e spostare, puo' essere una foto o un swf
var picW; // La larghezza iniziale dell'immagine di sfondo
var picH; // L'altezza iniziale dell'immagine di sfondo

// :TODO: Cercare di beccare anche le dimensioni del swf senza hardCodarli

////////////////////////////////////////////////////////////////////////////////

if (document.all) 
	{
		top.window.resizeTo(screen.availWidth, screen.availHeight);
	} 
else if (document.layers || document.getElementById) 
	{ 
		if (top.window.outerHeight < screen.availHeight || top.window.outerWidth < screen.availWidth)
		{
			top.window.outerHeight = top.screen.availHeight; 
			top.window.outerWidth = top.screen.availWidth;
		} 
	}
	
////////////////////////////////////////////////////////////////////////////////

if (jQuery.browser.msie)
{
	  var v = jQuery.browser.version;
	  if(v <= 7)
	  {
	  	  window.location = 'http://'+window.location.host+"/corsi/htdocs/pages/badBrowser.html";
	  }
}


function  getPicDim()
{
	var img = $(obj).children('img');

	img.load(function()
		{
		    // Remove attributes in case img-element has set width and height
		    $(this).removeAttr("width")
			   .removeAttr("height")
			   .css({ width: "", height: "" }); // Remove css dimensions as well
	
		    picW = 1823; //this.width;
		    picH = 1133; //this.height;
		    posSwfAmoK();
		});
	
	var src = img.src;
	img.src = "";
	img.src = src; // Triggers onload if image is cached (see comments)

	$(document).pngFix();
	//jQuery(function(){jQuery(document).pngFix();});
}

function posSwfAmoK()
{	

	var startwidth = ($('#swfBack').length == 1)?720:1823;  
        var startheight = ($('#swfBack').length == 1)?480:1133;	
        
        //var browserwidth = $(window).width();
        //var browserheight = $(window).height();
        
        var browserwidth;
        var browserheight;
        
	if(document.all)
	{
		browserwidth = document.body.clientWidth;
		browserheight = document.body.clientHeight;
	}
	else
	{
		browserwidth = innerWidth;
		browserheight = innerHeight;
	}
        
	// Se siamo sotto il nostro limite, lo sfondo non si ridimensiona piu'
	/*if (browserwidth < 1105) browserwidth = 1105;
	if (browserheight < 580) browserheight = 580;*/
	
	var ratio = startheight/startwidth;
	
	var newWidth;
	var newHeight;
	
        if ((browserheight/browserwidth) > ratio)
	{
		newWidth = (browserheight / ratio);
		newHeight = (browserheight);
        }
	else
	{
		newWidth = (browserwidth);
		newHeight = (browserwidth * ratio);
        }
        
	if($('#swfBack').length == 1)
	{
		$(obj).height(newHeight);
		$(obj).width(newWidth);
	}
	$(obj).children().height(newHeight);
	$(obj).children().width(newWidth);

	if($('#image_center').length == 0)
	{
		$(obj).css('left', (browserwidth - newWidth)/2);
		$(obj).css('top', (browserheight - newHeight)/2);
	}
	
        // Sistemo la larghezza dell'container, dinamica fino a 1105 poi fissa per le barre di scorrimento            
        var contWidth;
        var contHeight;
        var htmlOverFl;
        
        if((browserwidth < 1105) || (browserheight < 580))
        {
        	contWidth = (browserwidth < 1105)?'1105px':browserwidth;
        	contHeight = (browserheight < 580)?'580px':browserheight;
        	htmlOverFl = 'auto'
        }
        else
        {
        	contWidth = '100%';
        	contHeight = '100%';
        	htmlOverFl = 'hidden'
        }
        
        
        $('#container').width(contWidth);
        $('#container').height(contHeight);
	$('html').css({ 'overflow-x':'hidden', 'overflow-y':'auto' }); 
}

$(document).ready (function()
{	
	obj = ($('#swfBack').length == 0)?$('#picBack'):$('#swfBack');
	$(obj).css('opacity', '0');
	$(obj).animate({ opacity: 1}, 1000);
	// Se il backGround e' una foto recupero le dimensioni
	/*if($('#picBack').length == 1)
	{
		getPicDim();
		return;
	}*/
	posSwfAmoK();
});

$(window).bind("resize", function()
	{
           posSwfAmoK();
        });
