///////////////////////////////////////////// Window Code
var dialogActive;
var div_name = 'centerTable';
//var container_name = 'screenshotWindowContainer';
//var bar_name = 'screenshotWindowBar';

var msg_displayed = false;
var enable_animation = true;

function windowGetDivName() {
    return div_name;
}

function windowAnimate(animation_on) {
    enable_animation = animation_on;
}

function windowDraw(fromTop) {
	var pixelsFromTop = fromTop || 0;

    msg_displayed = false;

    // black out the background
    windowFadeBlack();
    windowSwitchForms(true);

    // draw the window
    var timeWindow = document.createElement('div');

	var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
    var pageWidth = Math.min(document.documentElement.clientWidth, window.innerWidth ? window.innerWidth : Infinity);


    timeWindow.setAttribute('id',windowGetDivName());
	/*
    timeWindow.style.left = '50%';
    timeWindow.style.marginLeft = '-200px';
    timeWindow.style.top = eval("'" + (scrollTop + pixelsFromTop) + "px';");
    timeWindow.style.display = 'none';
	*/
    //timeWindow.style.left = ((pageWidth / 2) - cwith).toString() + 'px';
    timeWindow.style.top = (scrollTop + pixelsFromTop).toString() + 'px';
    timeWindow.style.display = 'none';
    timeWindow.style.zIndex = 1001;


    document.body.appendChild(timeWindow);
}

function windowAppear() {
	// make it do its Blinddown effect,
	if (enable_animation) {
        new Effect.BlindDown(windowGetDivName(), {duration: 0.7});
	} else {
	    $(windowGetDivName()).show();
	}

	if ( arguments.length ) {
		window.onscroll = function () {
			return false;
		}
	} else {
		window.onscroll = windowLocationOnScrollEvent;
	}
}

function windowDestroy() {
	// get rid of events
    window.onscroll = null;
    window.calendar = null;
	if ( arguments.length ) {
		// an argument exists to skip blind up
		if (enable_animation) {
            new Effect.Fade(windowGetDivName());
		} else {
		    $(windowGetDivName()).hide();
		}
	} else {
	    if (enable_animation) {
            new Effect.BlindUp(windowGetDivName(), {duration: 0.5});
	    } else {
		    $(windowGetDivName()).hide();
	    }
	}
    if (enable_animation) {
        new Effect.Fade('windowBackground', {duration: 0.5, afterFinish: function() {
    		windowDestroyComplete();
    	}});
    } else {
        $('windowBackground').hide();
        windowDestroyComplete();
    }

}

function windowDestroyComplete() {
	$('centerTable').style.background = '';
    if ($('windowBackground')) $('windowBackground').parentNode.removeChild($('windowBackground'));
    if ($(windowGetDivName())) $(windowGetDivName()).parentNode.removeChild($(windowGetDivName()));
    windowSwitchForms(false);
}

function windowFadeBlack() {

    var background = document.createElement("table");
    background.setAttribute('id', 'windowBackground');
    background.setAttribute('width', '100%');
    background.setAttribute('height', windowGetScreenHeight() + 40);
    background.style.display = 'none';
    var backgroundTr = background.insertRow(-1);
    var backgroundTd = backgroundTr.insertCell(-1);
    document.body.insertBefore(background, document.body.firstChild);

    if (enable_animation) {
        new Effect.Appear('windowBackground', {to: 0.4});
    } else {
		$('windowBackground').setStyle({opacity: 0.4});
        $('windowBackground').show();
    }
}

function windowSwitchForms(in_disable) {
	// only do this for IE6 ...
	if ( (/msie 5\.5/gi.test(navigator.userAgent) || /msie 6\.0/gi.test(navigator.userAgent)) && !/opera/gi.test(navigator.userAgent) ) {
		for (var i = 0; i < document.forms.length; i++) {
			if (document.forms[i].name == 'ajaxWindow') continue;
			for (var j = 0; j < document.forms[i].elements.length; j++) {
				document.forms[i].elements[j].style.visibility = in_disable ? 'hidden' : 'visible';
			}
		}
	}
}

function windowGetScreenHeight() {
    var height = 0;
    var heightPropertiesArray = new Array(document.body.clientHeight,document.documentElement.clientHeight,window.innerHeight);
    for (var i = 0; i < heightPropertiesArray.length; i++) {
        if (heightPropertiesArray[i] != undefined && heightPropertiesArray[i] > height) height = heightPropertiesArray[i];
    }
    return height;
}

function windowGetInnerHeight() {
    var height = 0;
    var heightPropertiesArray = new Array(document.documentElement.clientHeight,window.innerHeight);
    for (var i = 0; i < heightPropertiesArray.length; i++) {
        if (heightPropertiesArray[i] != undefined && heightPropertiesArray[i] > height) height = heightPropertiesArray[i];
    }
    return height;
}

function windowDisplayMsg(in_msg) {
    $('windowMsg').innerHTML = in_msg;
    $('windowMsg').style.display = 'block';
    $('windowMsgError').style.display = 'none';
    if (!msg_displayed) {
        new Effect.BlindDown('windowMsgContainer', {duration: 0.7});
    }
    msg_displayed = true;
}
function windowDisplayErrorMsg(in_msg) {
    $('windowMsgError').style.display = 'block';
    $('windowMsgError').innerHTML = in_msg;
    if (!msg_displayed) {
        new Effect.BlindDown('windowMsgContainer', {duration: 0.7});
    }
    msg_displayed = true;
}

function windowClearMsg() {
    if (msg_displayed) {
        new Effect.BlindUp('windowMsgContainer', {duration: 0.7, afterFinish: windowClearComplete });
    } else {
        windowClearComplete();
    }
    msg_displayed = false;
}
function windowClearComplete() {
    $('windowMsg').innerHTML = '';
    $('windowMsg').style.display = 'none';
    $('windowMsgError').innerHTML = '';
    $('windowMsgError').style.display = 'none';
}

function windowLocationOnScrollEvent() {
    var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);

    $(windowGetDivName()).style.top = eval("'" + (scrollTop + 100) + "px';");
}

function windowGetMsgArea() {
    return '<div id="windowMsgContainer" style="display: none;"><div id="windowMsg"></div><div id="windowMsgError"></div><div class="pinstripe">&nbsp;</div></div>';
}

