$(document).ready(function(){
	// resize the background

	loadswf();

	
	resizeBackground();
	$(document).pngFix();	
	
	$(window).wresize( function(){  	   
		  resizeBackground();
    	});
        
        
	if ($('#preloader_wrapper').length == 0)
	{
	
		reloadswf();
	}
		
	
});

function LoadTheHomeSwf() {
    if($.cookie('bPreloaderShow') == 'yes')
    {
        return;
    }
    else 
    {
        // create preloader div
        var o_element = '<div id="preloader_wrapper"><div id="preloader"></div></div>';
        $(o_element).insertBefore('#insertmeplease');

        var $_GET = {};

        document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
            function decode(s) {
                return decodeURIComponent(s.split("+").join(" "));
            }
        
            $_GET[decode(arguments[1])] = decode(arguments[2]);
        });
        
        
        var s_preloader_url = "project/files/swf/WildOrangeSplash.swf?release=1"
       
        $.cookie('bPreloaderShow', 'yes');

        // create swf in container
        swfobject.embedSWF(
    		 s_webroot + s_preloader_url
    		,"preloader"
    		,"100%"
    		,"100%"
    		,"9"
    		,false
    		,{
                s_web_root : s_webroot,
                s_release_tag: "01"
                
            }
    		,{
    			wmode : "transparent"
    			,align : "TL"
    			,menu : false
    		 }
    	 );

       resizeBackground();   
    }
}

function reloadswf() {
	resizeBackground();
}
function afterFooterLoaded() {
	resizeBackground();
}
function closeswf()
{
	if($('#preloader_wrapper'))
	{
		$('#preloader_wrapper').remove();
		
		reloadswf();
		
		
		
	}
	
	
	
}
function deletebackground()
{
	if($('#preloader_wrapper'))
	{
		$('#preloader_wrapper').css('background','none');
	}
}
function resizeBackground()
{
	
	// resize container
	
	if ($('#preloader_wrapper').length != 0)
	{  
        wWidth = $(window).width();
    	wHeight = $(window).height();
    
    	// reset the overflow setting
    	//$('html').css('overflow-y','auto');
    	//$('html').css('overflow-x','hidden');
    
    	// set viewport size
    	vpWidth = (minViewportWidth > wWidth)   ? minViewportWidth : wWidth;
    	vpHeight= (minViewportHeight > wHeight) ? minViewportHeight: wHeight;
    	   //alert(vpHeight);
	    //$('#preloader_wrapper').css('background','#000000');
        $('#preloader_wrapper').css('height',vpHeight + 'px');
        //$('#preloader_wrapper').css('width',cWidth + 'px');
    }
	else
	{  
	  	setViewPort();
	    reposFooter();
          
	    var offsetHeight = $('#area_footer').height() + $('#top-container').height();
    	var minHeight = 595 - offsetHeight; // set this automatically from the image ratio
        var minWidth  = vpWidth;
        	
        	// resize container
        var bgHeight = vpHeight - offsetHeight;
        var cHeight = (bgHeight < minHeight) ? minHeight : bgHeight ;
        var cWidth = (vpWidth < minWidth) ? minWidth : vpWidth;
	
	
        $('#resizeable-container').css('height',cHeight + 'px');
    	$('#resizeable-container').css('width', cWidth + 'px');
    	
    	// resize top filler
    	$('#top-container img').css('width', cWidth + 'px');
    		
    	// resize background
    	$('#resizeable-background').css('height', cHeight + 'px');
    	$('#resizeable-background img').css('height', cHeight + 'px');
    	
    	$('#resizeable-container #imgwrapper').css('height',cHeight);
    	$('#resizeable-container #imgwrapper').css('width',$('#resizeable-image').width());
    	
    	
    	var imgX = 2053;
    	var imgY = 900;
    	var imgWidth = cHeight/imgY * imgX;
    	$('#resizeable-container #imgwrapper').css('width',imgWidth);
    	$('#resizeable-image').css('width', imgWidth);
    	
    	
    	if(imgWidth > cWidth)
    	{
    		//var margin = (imgWidth - cWidth) / 2;
    		//alert(cWidth);
    		//$('html').css('overflow-x', 'hidden');
    		//$('#resizeable-container #imgwrapper').css('margin-right', '-' + margin + 'px');
    	}
    	
    	var left = cWidth/2 - imgWidth/2;
    	left = (imgWidth < cWidth) ? 0 : left;
    	$('#resizeable-background').css('left', left + 'px');
    	
    	// resize left-right filler
    	var fillerWidth = (imgWidth < cWidth) ? (cWidth-imgWidth)/2 : 0;
    	fillerwidth = Math.round(fillerWidth);
    	$('#resizeable-left').css('width', fillerWidth + 'px');
    	$('#resizeable-left img').css('width', fillerWidth + 'px');
    	$('#resizeable-left img').css('height', cHeight + 'px');
    	
    	$('#resizeable-right').css('width', fillerWidth  + 'px');
    	$('#resizeable-right img').css('width', fillerWidth+2  + 'px'); 
    	$('#resizeable-right img').css('height', cHeight + 'px');
    	
    
    	// repos payoff
    	var pTop = vpHeight/5 - 25;
    	var pLeft = vpWidth/6 - 100;
    	$('#text-payoff').css('top', pTop);
    	$('#text-payoff').css('left', pLeft);
    	
    	// repos ATA
    	var pTop = cHeight/4 - 15;
    	var pLeft = (cWidth * 7/9) ;
    	$('#ata-box').css('top', pTop);
    	$('#ata-box').css('left', pLeft);
    
    	// TEST 
    	$('#resizeable-background').css('left', '0px');
    	$('#resizeable-background').css('width', '100%');
    	$('#resizeable-container').css('width', '100%');
    	$('#resizeable-container').css('height', '100%');
    	$('#resizeable-container #imgwrapper').css('width','100%');
    	$('#resizeable-container #imgwrapper').css('height','100%');
    	$('#resizeable-image').css('width', '100%');
    	$('#resizeable-image').css('height', '100%');
	}
}
