/* Katpak website
*  global.js
*  Main site javascript
*  Author: Geniemouse Ltd.
*  Date: 04-2010 */

var GM = {
	Plugins: {},	// browser plugin info
	Class: {},		// GM Class library
	Init: {} 		// hold instances of individual GM Classes
};

GM.Plugins.Flash = 6;	// minimum verion of Flash plugin

GM.Class.Utilities = new Class({
	Implements:[Log],
	ErrorHelper: function(errorObject) {
		/* Object Schema
		* errorObject : {
		* 	method:"MyClass : myFunction()", // string to help you identify which js file and function caused the error
		* 	error:e // the captured error object
		* }
		*/
		this.enableLog();
		if (errorObject.error instanceof TypeError) {
			if(errorObject.method) this.log("JS ERROR : "+errorObject.method+" : TypeError; the type of a variable is not as expected : "+errorObject.error.message);
		} else if (errorObject.error instanceof RangeError) {
			if(errorObject.method) this.log("JS ERROR : "+errorObject.method+" : RangeError; numeric variable has exceeded its allowed range : "+errorObject.error.message);
		} else if (errorObject.error instanceof SyntaxError) {
			if(errorObject.method) this.log("JS ERROR : "+errorObject.method+" : SyntaxError; syntax error occurred while parsing : "+errorObject.error.message);
		} else if (errorObject.error instanceof ReferenceError) {
			if(errorObject.method) this.log("JS ERROR : "+errorObject.method+" : ReferenceError; invalid reference used : "+errorObject.error.message);
		} else {
			if(errorObject.method) this.log("JS ERROR : "+errorObject.method+" : Unidentified Error Type : "+errorObject.error.message);
		}
	}
});


GM.Class.TargetReplacement = new Class({
	// call <a href="http://www.website-url.com" title="A title" rel="external">Link text</a>
	Implements: [GM.Class.Utilities, Options],
	options: {
		warning: 'Opens in a new window',
		warningseparator: ': '
	},
	initialize: function(options){
		try {
			this.setOptions(options);
			$$('a[rel*="external"]').each(function(a) {
				var title = '';
				(a.getProperty('title') != null) ? title = a.getProperty('title') + this.options.warningseparator + this.options.warning.toLowerCase() : title = this.options.warning;
				a.setProperties({
					'title':  title,
					'target': '_blank'
				});
			}, this);	
		} catch(e) {
			//this.ErrorHelper({method:"TargetReplacement Class : initialize()",error:e});
		}
	}
});



GM.Class.OpenPopWins = new Class({
	// call <a href="http://www.website-url.com" title="A title" rel="pop">Link text/a> to use the default settings
	// call via <a href="http://www.website-url.com" title="A title" rel="pop[{width:250,height:250,scrollbars:1,resizable:1, etc}]">Link text/a> to over write certain settings
	Implements: [Events, Options, GM.Class.Utilities],
	options: {// default settings
		winprefs: {
			width: 865,
			height: 400,
			scrollbars: false,
			status: false,
			location: false,
			menubar: false,
			resizable: false,
			toolbar: false	
		},
		warning: 'Opens in a new window',
		warningseparator: ': '
	},
	initialize: function(options){
		try {
			this.setOptions(options);
			this.links = $$('a[rel^=pop]');
			this.links.each(function(a){
				var title = '';
				(a.getProperty('title') != null) ? title = a.getProperty('title') + this.options.warningseparator + this.options.warning : title = this.options.warning;
				a.setProperties({
					'title':  title,
					'target': '_blank'
				}).addEvents({
					'click': function(e){
						e.stop();
						var nwin = '';
						if(!nwin.closed && nwin.location){
							nwin.location.href = a.getProperty('href');
						} else {
							var theseValues = a.getProperty('rel');
							var winProps = '';
							if (theseValues != 'pop') {
								theseValues = eval('theseValues='+theseValues.slice(4,theseValues.length-1));
								for(var i in this.options.winprefs) {
									if(typeof theseValues[i] == 'undefined') theseValues[i] = this.options.winprefs[i];
								}
							}else{
								theseValues = this.options.winprefs;
							}
							for(var i in theseValues) winProps += i+'='+theseValues[i]+',';
							winProps = winProps.slice(0,winProps.length-1);
							nwin=window.open(a.getProperty('href'),'pop',''+winProps+'');
							if(!nwin.opener) nwin.opener = self;
						}
						if(window.focus) nwin.focus(); return false;
					}.bind(this)
				});
			}, this);
		} catch(e) {
			//this.ErrorHelper({method:"OpenPopWins Class : initialize()",error:e});
		}
	}	
});



GM.correctSafariTabbing = function(){
	if (document.id('skip-nav')) {
		document.id('skip-nav').addEvents({
			'focus': function(){
				document.id('skip-nav').setStyle('left', '0');
			},
			'blur': function(){
				document.id('skip-nav').setStyle('left', '-10000px');
			},
			'click': function(){
				if ((navigator.appVersion).match(/safari/i)) {
					var hrf = document.id('skip-nav').getProperty('href');
					if (hrf.indexOf('#') == 0) hrf = hrf.substring(1, hrf.length);
					if (!document.id('faux-target')) {
						var a = new Element('a').setStyles({position: 'absolute', left: '-10000px'
						}).set('html', '&nbsp;').setProperties({
							id: 'faux-target', title: 'Entered main content of page', href: ''}).injectTop(document.id(hrf));
					}
					document.id('faux-target').focus();
				}
			}
		});
	}
	
	if ((navigator.appVersion).match(/safari/i)) {
		$$('.target-top').each(function(e){
			e.addEvent('click', function(){
				if (!document.id('faux-target-top')) {
					var a = new Element('a').setStyles({position: 'absolute', left: '-10000px'
					}).set('html', '&nbsp;').setProperties({
						id: 'faux-target-top', title: 'Top of the page', href: ''
					}).injectTop(document.id('top'));
				}
				document.id('faux-target-top').focus();
			});
		});
	}
};


GM.Class.Facebook = new Class({
	Implements: [GM.Class.Utilities, Options],
	options: {
		container: null,
		url: '',
		querystring: {
			href: '',
			width: 250,
			height: 35,
			colorscheme: 'light',
			additional: ''
		},
		iframeAttributes: {
			scrolling: 'no',
			frameborder: '0',
			allowTransparency: 'false'
		}
	},
	initialize: function(options){
		try {
			this.setOptions(options);		
			this.container = document.id(this.options.container);
			if(this.container) {
				var n;
				var hash = new Hash(this.options.querystring);
				var iframe = '<iframe ';
				iframe += 'src="'+this.options.url+'?';
				hash.each(function(value, key){
					iframe += key+'='+value;
					iframe += '&amp;';	
				});
				iframe += '" ';
				iframe += 'style="border:none;overflow:hidden;width:'+this.options.querystring.width+'px;height:'+this.options.querystring.height+'px;" ';
				iframe += 'scrolling="'+this.options.iframeAttributes.scrolling+'" ';
				iframe += 'frameborder="'+this.options.iframeAttributes.frameborder+'" ';
				iframe += 'allowTransparency="'+this.options.iframeAttributes.allowTransparency+'"';
				iframe += '></iframe>';
				this.container.set('html', iframe);
			} else {
				return false;
			}
		} catch(e) {
			this.ErrorHelper({method:"Facebook Class : initialize()",error:e});	
		}	
	}
});



window.addEvents({
	'domready': function(){
		document.id(document.body).addClass('js');
		GM.correctSafariTabbing();

		GM.Init.RelExternal = new GM.Class.TargetReplacement();
		GM.Init.PopWins = new GM.Class.OpenPopWins();
		
		GM.Init.FacebookLike = new GM.Class.Facebook({
			container:'fblike',
			url: 'http://www.facebook.com/plugins/like.php',
			querystring: {
				href: 'www.katpak.com',
				additional: 'layout=standard&amp;show_faces=false&amp;action=like&amp;font'
			}	
		});

		GM.Init.FacebookFanBox = new GM.Class.Facebook({
			container:'fbfanbox',
			url: 'http://www.facebook.com/plugins/likebox.php',
			querystring: {
				href: 'http%3A%2F%2Fwww.facebook.com%2Fhelp%2F%3Fsearch%3Dadd%2Bfan%2Bbox%23%21%2Fpages%2Fkatpak%2F120040231351997',
				width: 290,
				height: 260,
				additional: 'connections=2&amp;stream=true&amp;header=false'
			}
		});
	}
});
