﻿var imBG;
var divBG;
var initialWidth;
var initialHeight;
var windowHeight=0;
var windowWidth=0;

Window = {	
	//Returns an integer representing the width of the browser window (without the scrollbar).
	getWindowWidth : function() {
	return (document.layers||(document.getElementById&&!document.all)) ? window.outerWidth : (document.all ? document.body.clientWidth : 0);
	},
	
	//Returns an integer representing the height of the browser window (without the scrollbar).
	getWindowHeight : function() {	
	return window.innerHeight ? window.innerHeight :(document.getBoxObjectFor ? Math.min(document.documentElement.clientHeight, document.body.clientHeight) : ((document.documentElement.clientHeight != 0) ? document.documentElement.clientHeight : (document.body ? document.body.clientHeight : 0)));
	},	
	
	//Returns an integer representing the scrollWidth of the window. 
	getScrollWidth : function() {
	return document.all ? Math.max(Math.max(document.documentElement.offsetWidth, document.documentElement.scrollWidth), document.body.scrollWidth) : (document.body ? document.body.scrollWidth : ((document.documentElement.scrollWidth != 0) ? document.documentElement.scrollWidth : 0));
	},
	
	//Returns an integer representing the scrollHeight of the window. 
	getScrollHeight : function(){		
		return document.all ? Math.max(Math.max(document.documentElement.offsetHeight, document.documentElement.scrollHeight), Math.max(document.body.offsetHeight, document.body.scrollHeight)) : (document.body ? document.body.scrollHeight : ((document.documentElement.scrollHeight != 0) ? document.documentElement.scrollHeight : 0));
	},			
	
	//Returns an integer representing the scrollLeft of the window (the number of pixels the window has scrolled from the left).
	getScrollLeft : function() {
		return document.all ? (!document.documentElement.scrollLeft ? document.body.scrollLeft : document.documentElement.scrollLeft) : ((window.pageXOffset != 0) ? window.pageXOffset : 0);
	},
	
	//Returns an integer representing the scrollTop of the window (the number of pixels the window has scrolled from the top).
	getScrollTop : function() {
		return document.all ? (!document.documentElement.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop) : ((window.pageYOffset != 0) ? window.pageYOffset : 0);
	}
}
		
window.addEvent('domready', function() {

    imBG = $('imBG');
    divBG = $('backgr');


    
    if (imBG != null){
        initialWidth  = imBG.getSize().x;
        initialHeight = imBG.getSize().y;


//        if (bg_scale == 'match window width'){
//            window.addEvent('resize', ScaleByWidth);
//            ScaleByWidth();
//        }
//        else{
//            if (bg_scale == 'match window height'){
//                window.addEvent('resize', ScaleByHeight);
//                ScaleByHeight();
//            }
//            else{
//                if (bg_scale == 'best fit'){
                    window.addEvent('resize', BestFit);
                    BestFit();
//                }
//            }
//        }
    }
    
    $('login').addEvent('click', showLoginBox);
    $('login').setStyles({
        display:'block',
        opacity: 0.01
    });
    
});

function BestFit(){
    windowHeight = Window.getWindowHeight();
    windowWidth  = Window.getWindowWidth();
    
    var xr = imBG.getSize().x/windowWidth;
    var yr = imBG.getSize().y/windowHeight;
    
    if (xr==0){
        setTimeout(BestFit,10);  
    }
    if (xr<=yr) {
        imBG.setProperty("width", windowWidth);
        imBG.setProperty("height", null);   
    }
    else{
        imBG.setProperty("width", null);
        imBG.setProperty("height", windowHeight);
    }
    
    ResizeBgDiv();
}

function ScaleByWidth(){

    windowHeight = Window.getWindowHeight();
    windowWidth  = Window.getWindowWidth();
    
    if (windowWidth > initialWidth) 
        imBG.setProperty("width", windowWidth);
    else
         imBG.setProperty("width",initialWidth);
         
    ResizeBgDiv();
}

function ScaleByHeight(){

    windowHeight = Window.getWindowHeight();
    windowWidth  = Window.getWindowWidth();
    
    if (windowHeight > initialHeight) 
        imBG.setProperty("height", windowHeight);
    else
        imBG.setProperty("height",initialHeight);
       
    ResizeBgDiv();
}

function ResizeBgDiv(){
    var pageHeight = $('page').getSize().y+10;
    
    if (pageHeight > windowHeight){
        divBG.setStyles({
            height:pageHeight,
            width: Window.getWindowWidth()
        });
    }
    else{
        divBG.setStyles({
            height:'100%',
            width: '100%'
        });
    }
}

function showLoginBox(e){
	e.stop();
	var loginbox = window.open( '/member_login.aspx', '','width=200,height=150,location=no');
}

