﻿var iBannerW=120; //do rong cua Banner
var iBodyW=1000; //do rong cua Body
var iMarginLeft=15;// margin left cua Body
var iMarginRight=25;// margin right cua Body
var iMarginTop=10;// margin top cua Banner
var iMarginBottom = 0; //20;
var iPositionType=1; //0 = top; 1 = bottom;
function windowHeight() {
    // Standard browsers (Mozilla, Safari, etc.)
    if (self.innerHeight)
        return self.innerHeight;
    // IE 6
    if (document.documentElement && document.documentElement.clientHeight)
        return y = document.documentElement.clientHeight;
    // IE 5
    if (document.body)
        return document.body.clientHeight;
    // Just in case.
    return 0;
}
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function Banner_Float()
{
    try{
        var iWidth=document.body.offsetWidth;
        var objLeft=document.getElementById("left_banner");
        var objRight=document.getElementById("right_banner");
        //if (objLeft.style.display=="none") return;
        //
        var height = windowHeight();
        var top = f_scrollTop();
        if(iPositionType==1){
            objLeft.style.top=(top+height-iMarginBottom-objLeft.style.height.toString().replace('px',''))+"px";
            objRight.style.top=(top+height-iMarginBottom-objRight.style.height.toString().replace('px',''))+"px";
        }else{
            objLeft.style.top=top+iMarginTop+"px";
            objRight.style.top=top+iMarginTop+"px";
        }
    }catch(ex){alert(ex);}
}
function Banner_Position()
{
    var iWidth=document.body.offsetWidth;
    var objLeft=document.getElementById("left_banner");
    var objRight=document.getElementById("right_banner");
    
    if (iWidth-iBodyW<iBannerW*2)
    {
	    objLeft.style.visibility='hidden';
	    objRight.style.visibility='hidden';
    }
    else
    {
	    objLeft.style.visibility='visible';
	    objRight.style.visibility='visible';
    }
    //
    var iLeft=(iWidth-iBodyW)/2 - iBannerW + iMarginLeft/2;
    if (!self.innerHeight) // Explorer        
	    iLeft=iLeft-20-iMarginRight/2;
    else // all except Explorer
	    iLeft=iLeft-15;
//    objLeft.style.left=iLeft + "px";
objLeft.style.left = '0px';
    //
    var iRight=(iWidth-iBodyW)/2 + iBodyW + iMarginLeft/2;
    if (self.innerHeight) // all except Explorer        
	    iRight+=17;
    else
	    iRight-=-2+iMarginRight/2;
//	objRight.style.left=iRight + "px";
objRight.style.left = (iWidth - iBannerW) + 'px';
    //
    //objLeft.style.top=iMarginTop+"px";
    //objRight.style.top=iMarginTop+"px";
    var height = windowHeight();
    var top = f_scrollTop();
    if(iPositionType==1){
        objLeft.style.top=(top+height-iMarginBottom-objLeft.style.height.toString().replace('px',''))+"px";
        objRight.style.top=(top+height-iMarginBottom-objRight.style.height.toString().replace('px',''))+"px";
    }else{
        objLeft.style.top=top+iMarginTop+"px";
        objRight.style.top=top+iMarginTop+"px";
    }
}
$(document).ready(function(){
    $('div[id^=extensible-banner]').each(function(e){
        $(this).parent(':first').parent(':first').parent(':first').parent(':first')
            .css('width',$(this).parent(':first').css('width'))
            .css('height',$(this).parent(':first').css('height'));
//            .css('border','solid 1px #ccc');        
    });    
    $('#left_banner span').show();
    $('#right_banner span').show();
    Banner_Position();
})
$(window).resize(function(){Banner_Position();});
$(window).scroll(function(){Banner_Float();});
