/**
 * Kaizen Framework - JavaScript
 * 
 * @author Vasil Georgiev Dinkov - Kaizen Web-Productions (http://www.kaizen-web.com)
 * @version 1.0
 * @copyright Copyright(C), Kaizen Web-Productions, 2004-2008, All Rights Reserved.
 * @package KaizenFramework
 */

/**
 * Core Functionality
 * Must be included as the first JS file on all pages.
 *
 * Define core functionality - e.g. browser detection, window size detection...
 *  
 * @author Vasil Georgiev Dinkov - Kaizen Web-Productions (http://www.kaizen-web.com)
 * @version 1.0
 * @copyright Copyright(C), Kaizen Web-Productions, 2004-2008, All Rights Reserved.
 * @package KaizenFramework
 * @subpackage Core
 */

Kaizen = {

	// put any default init code in this function
	init: function() {

		// if using the Kaizen.Tip module
		if (Kaizen.Tip) {
			// create the 'default' tooltip family
			Kaizen.Tip.addTooltipFamilies([]);
		}

		// add target="_blank" to external links
		Kaizen.initExternalLinks();

		// if using the Kaizen.DOM module
		if (Kaizen.DOM) {
			// init link icons in IE6
			Kaizen.DOM.initLinkIconsIE6();
		}
	},

	Browser: {
		IE: !window.innerWidth && /msie/i.test(navigator.userAgent),
		Gecko: /gecko/i.test(navigator.userAgent) && !/khtml/i.test(navigator.userAgent),
		Opera: /opera/i.test(navigator.userAgent) ? parseFloat(navigator.userAgent.replace(/.*opera[ \/]/i, '')) : 0,
		Webkit: /webkit/i.test(navigator.userAgent) ? parseFloat(navigator.userAgent.replace(/.*bkit\//i, '')) : 0,
		Konqueror: /konqueror/i.test(navigator.userAgent) ? parseFloat(navigator.userAgent.replace(/.*eror\//i, '')) : 0
	},

	Viewport: {

		// Returns the viewport's inner dimentions (excluding scrollbars) - {w:width, h:height}
		getSize: function() {
			var d, b, i, w = 'clientWidth', h = 'clientHeight', A, B, c;
			d = document.documentElement[h];
			b = document.body[h];
			i = window.innerHeight;
			A = {h:b, w:document.body[w]};
			B = {h:d, w:document.documentElement[w]};
			if (!i) {
				c = document.compatMode != 'CSS1Compat' ? A : B;
			} else if (d && b && (!Kaizen.Browser.Konqueror || Kaizen.Browser.Konqueror >= 4) && (!Kaizen.Browser.Webkit || Kaizen.Browser.Webkit >= 420)) {
				// this approach might seem a bit weird but fixes issues in Safari3 (where document.compatMode misses) and in Opera<9.5
				c = d > b ? d > i ? A : B : b > i ? B : A;
			} else {
				c = {h:i, w:window.innerWidth};
			}
			return c;
		},

		// Returns the page's scrolling offsets - {x:scroll_x, y:scroll_y}
		getScrollOffsets: function() {
			if (typeof window.pageXOffset != 'undefined')
				return {x:window.pageXOffset, y:window.pageYOffset};

			return {x:Math.max(document.documentElement.scrollLeft, document.body.scrollLeft),
				y:Math.max(document.documentElement.scrollTop, document.body.scrollTop)};
		}
	},

	// Extends an object with new properties
	extendObject: function(object, extentions) {
		for (var i in extentions)
			object[i] = extentions[i];
		return object;
	},

	returnFalse: function() {
		return false;
	},

	// takes size in bytes and returns formatted size in proper unit
	formatSize: function(bytes) {
		if (bytes > 1073741824)
			return Math.round(parseInt(bytes) / 1073741824) + ' GB';
		if (bytes > 1048576)
			return Math.round(parseInt(bytes) / 1048576) + ' MB';
		if (bytes > 1024)
			return Math.round(parseInt(bytes) / 1024) + ' KB';
		return bytes + ' bytes';
	},

	// add target="_blank" to external links
	initExternalLinks: function() {
		var links = document.getElementsByTagName('a');
		var cur;
		for (var i = 0, l = links.length; i < l; i++) {
			cur = links[i];
			if (!cur.getAttribute('target') && (' ' + cur.getAttribute('rel') + ' ').indexOf(' newpage ') > -1)
				cur.target ='_blank';
		}
	}

};

// allows binding some of the functions in Kaizen.DOM as methods to arbitrary elements (i.e. allows reuse)
Function.prototype.applyAsMethod = function() {
	if(this.__asMethod)
		return this.__asMethod;
	var ref = this;
	this.__asMethod = function() {
		var args = [];
		args[0] = this;
		for(var i = 0, l = arguments.length; i < l; i++)
			args[i+1] = arguments[i];
		return ref.apply(null, args)
	}
	return this.__asMethod;
}

Function.prototype.bind = function() {
	var __method = this, args = [], object = arguments[0];
	for(var i = 1, l = arguments.length; i < l; i++)
		args[i-1] = arguments[i];
	return function() {
		return __method.apply(object, args);
	}
}

Array.prototype.indexOf = function(item) {
	for (var i = 0, l = this.length; i < l; i++)
		if (this[i] == item)
			return i;
	return -1;
}
