/*  
 * Window functions
 *
 */

var storedHeight;

window.setResolution = function (element, width, height) {
	var set = function() {
		element = $(element);

		if (element.size() < 1)
			return;

		var widthBrowser = window.getDimensions().windowWidth,
			heightBrowser = window.getDimensions().windowHeight;

		element.width((widthBrowser <= width) ? width : '100%');
		element.height((heightBrowser <= storedHeight) ? storedHeight : heightBrowser);
	};

	storedHeight = height;

	$(document).ready(function() { set(); });
	$(window).resize(function() { set(); });
};

window.resizeByFlash = function(element, height) {
	element = $("#" + element);

	if (element.size() < 1)
		return;

	storedHeight = height;
	element.height((window.getDimensions().windowHeight <= height) ? height : heightBrowser);
	$('.site .content').css('minHeight', height);
};


window.getDimensions = function () {
	var body = document.body,
		docElement = document.documentElement,
		xScroll, yScroll, windowWidth, windowHeight;

	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (body.scrollHeight > body.offsetHeight){ // all but Explorer Mac
		xScroll = body.scrollWidth;
		yScroll = body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = body.offsetWidth;
		yScroll = body.offsetHeight;
	}

	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (docElement && docElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = docElement.clientWidth;
		windowHeight = docElement.clientHeight;
	} else if (body) { // other Explorers
		windowWidth = body.clientWidth;
		windowHeight = body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if (yScroll < windowHeight)
		pageHeight = windowHeight;
	else
		pageHeight = yScroll;

	// for small pages with total width less then width of the viewport
	if (xScroll < windowWidth)
		pageWidth = windowWidth;
	else
		pageWidth = xScroll;

	return {
		pageWidth: pageWidth,
		pageHeight: pageHeight,
		windowWidth: windowWidth,
		windowHeight: windowHeight
	};
};

window.getPageScroll = function (){
	var body = document.body,
		docElement = document.documentElement,
		xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (docElement && docElement.scrollTop){
		yScroll = docElement.scrollTop;
		xScroll = docElement.scrollLeft;
	} else if (body) {
		yScroll = body.scrollTop;
		xScroll = body.scrollLeft;	
	}

	return {top: yScroll, left: xScroll};
};

window.callWindow = function (options) {
	var iWScrool = 0;

	options = options || {};
	options.url = options.url || '';
	options.width = options.width || '100';
	options.height = options.height || '100';
	options.scrollbars = options.scrollbars || 0;

	if (options.scrollbars == 1 || options.scrollbars == 'yes')
		options.width += iWScrool;

	var winPosTop = (window.getDimensions().windowHeight - options.height) / 2;
	var	winPosLeft = (window.getDimensions().windowWidth - options.width - iWScrool) / 2;

	window.open(options.url, options.target, 'width=' + options.width + ',height=' + options.height + ',top=' + winPosTop + ',left=' + winPosLeft + ',status=0,toolbar=no,menubar=no,location=no,scrollbars=' + options.scrollbars);
};