// Extend array object to check for existence
Array.prototype.has = function(value) {
	
	for (var i = 0, loopCnt = this.length; i < loopCnt; i++) {		
		if (this[i] === value) {
			return true;	
		}
	}
	
	return false;
};

// Requires prototype
if (typeof Prototype=='undefined') {
	throw('Mekin library requires prototype.js');
}


//
// Begin Mekin base definition
//

var Mekin = {

	Version: 1.0,
	
	pageLoaded: false,
	
	debugMode: false,
	
	debugDiv: null,
	
	checkObject: function( name, obj ) {
		if (typeof obj == 'undefined') {
			this.error(name + ' is undefined.\nHave you include its javascript file?');
		}
	},
	
	error: function(msg) {
		if (this.debugMode == true) {
			alert('Mekin Error:\n' + msg);
		}
		
		throw(msg);
	},
	
	warning: function(msg) {
		// Ignore warnings in 'release'
		if (this.debugMode == false) {
			return;
		}
		
		// Create debugDiv on demand, but only if body exists
		if (this.debugDiv == null) {
			if (document && document.body) {
				this.debugDiv = $(document.createElement('div'));
				this.debugDiv.setStyle({
					color: 'gray'
				});
				document.body.appendChild(this.debugDiv);
			}
		}
		
		// Print warning to the debug div
		if (this.debugDiv) {
			this.debugDiv.innerHTML = 'WARNING: ' + msg + '<br />' + this.debugDiv.innerHTML;
		} else {
			// body hasn't loaded yet; use alert instead
			alert('Mekin Warning:\n' + msg);
		}
	},
		
	require: function(libraryPath) {
		// Copied from Script.aculo.us
		// inserting via DOM fails in Safari 2.0, so brute force approach
		
		path = Mekin.Path.getRelativePath( 'Mekin.js' );
				
		// Construct full path from extracted path and library [relative] path
		// Interesting trick: use DOM image element to resolve full path
		img = document.createElement('img');
		img.src = path + libraryPath;
		libraryPath = img.src;

		// Check to see if we've already included this script
		includedScripts = document.getElementsByTagName(libraryPath);
		for (var i=0; i<includedScripts.length; ++i) {
			if (includedScripts[i].src == libraryPath) {
				return;
			}
		}

		document.write('<script type="text/javascript" src="'+libraryPath+'"><\/script>');
	},
	
	// Execute now if page is loaded
	// or else defer until page is loaded
	doOnLoad: function( code ) {
		if (this.pageLoaded) {
			code();
		} else {
			Event.observe(window, 'load', function(e){
				code();
			});
		}
	},
	
	// -- From lightbox:	
	//
	// getPageSize()
	// Returns array with page width, height and window width, height
	// Core code from - quirksmode.com
	// Edit for Firefox by pHaez
	//
	getPageSize: function() {
		
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
	//	console.log(self.innerWidth);
	//	console.log(document.documentElement.clientWidth);
	
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
	//	console.log("xScroll " + xScroll)
	//	console.log("windowWidth " + windowWidth)
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
	//	console.log("pageWidth " + pageWidth)
	
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	},
	
	// Return an associative array of the position information
	// Useful for passing to an element.setStyle call
	assocPosition: function( left, top ) {
	
		return {left: left + 'px', top: top + 'px'}
	
	}
}

Mekin.Path = {

	getRelativePath: function( scriptName ) {
	
		// Extract relative path to given script
		var js  = new RegExp(scriptName + '(.*)$' );
		includes = document.getElementsByTagName('script');
		
		for(var i=0; i<includes.length; ++i) {
			if (includes[i].src.match(js)) {
				return includes[i].src.replace(scriptName, '');
			}
		}
		
		return '';
	},
	
	// Returns a fully qualified path provided a relative path to a script
	getFilename: function( scriptName, filenameRelativeToScript ) {
		return this.getRelativePath( scriptName ) + filenameRelativeToScript;
	}
};

// Onload
Event.observe( window, 'load', function(e){

	Mekin.pageLoaded = true;

});

//
// Add some methods to the DOM elements
//
Element.addMethods( {

	// Set positions depending on what's specified
	setPosition: function( element, data )
	{
		['left','top','right','bottom'].each(function(p){
			if (typeof data[p] == 'number')
			{
				element.setStyle(p+':'+data[p]+'px');	
			}
		});
	},
	
	// Set dimensions of element
	setDimensions: function( element, width, height )
	{
		element.setStyle({ width: Math.floor(width) + 'px', height: Math.floor(height) + 'px' });
	}
});

// Copied from MSDN:
function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
