/*  Window JavaScript (use Prototype 1.6.x ), version 0.1
 *--------------------------------------------------------------------------*/

if (!Prototype) throw('Need prototype...'); else
if (parseFloat(Prototype.Version.substr(0,3)) < 1.6) throw('Wrong version of prototype...');

/* Browser/OS details */
if (Prototype.Browser.IE)
	Prototype.Browser.IE = parseFloat(navigator.appVersion.split("MSIE")[1]);

Object.extend(Prototype, {
	OS: {
		Win: !navigator.platform.indexOf('Win'),
		Mac: !navigator.platform.indexOf('Mac'),
		MacIntel: navigator.platform == 'MacIntel',
		MacPPC: navigator.platform == 'MacPPC'
	}
});

/* Detect doctype */
document.detectDoctype = function () {
	var matchDetect = /\s+(X?HTML)\s+([\d\.]+)\s*([^\/]+)*\//gi;

	if (Prototype.Browser.IE)
		return matchDetect.exec(document.all[0].nodeValue);
	else
		return matchDetect.exec(document.doctype.publicId);
};


/* Window */
Object.extend(window, {

	callWindow: function (iteraror) {
		var iWScrool = (Prototype.Browser.IE) ? 16 : 0;

		iteraror = iteraror || {};
		iteraror.url = (iteraror.url) ? iteraror.url : '';
		iteraror.width = (iteraror.width) ? iteraror.width : '100';
		iteraror.height = (iteraror.height) ? iteraror.height : '100';
		iteraror.scrollbars = (iteraror.scrollbars) ? iteraror.scrollbars : 0;

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

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

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

	setResolution: function (element, minWidth, minHeight) {
		element = $(element);

		if (element != null){
			var minWidthBrowser = this.getDimensions().windowWidth;
			var minHeightBrowser = this.getDimensions().windowHeight;
	
			element.width = (minWidthBrowser <= minWidth) ? minWidth : '100%';
			element.height = (minHeightBrowser <= minHeight) ? minHeight : '100%';
		}
	},

	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
		};
	},

	getPageScroll: function (){
		var xScroll, yScroll;

		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		}
		else if (document.documentElement && document.documentElement.scrollTop){ // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		}
		else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}
		return {
			top: yScroll,
			left: xScroll
		};
	}
});