/**
 * common.js
 * 
 * Pioneer common class
 *
 */
var PioneerCommon = Class.create();

var mousePosition = {'x':0, 'y':0};

PioneerCommon.prototype = {
	/**
	 * Parameters
	 */
	ajaxUrl: {},
	ajaxMethod: null,
	ajaxLoadingImg: null,
	ajaxResponseText: null,
	ajaxResult: null,
	elements: {},
	messages: {},
	url: {},
	browser: null,
	browserVersion: null,
	OS: null,
	emptyDivHTML: '<!-- -->',
	pressedKeyCode: null,

	/**
	 * Methods
	 *
	 * Class constructor
	 *
	 * @return PioneerCommon
	 */
	initialize: function() {
		var myPioneerBrowser = new PioneerBrowser();
		
		this.browser = myPioneerBrowser.browser;
		this.browserVersion = myPioneerBrowser.version;
		this.OS = myPioneerBrowser.OS;
	},

	getAjaxResult: function(response) {
		this.ajaxResponseText = response.responseText.replace(/\\u[a-f\d]{4}/gm, this.convertAjaxResult);
		this.ajaxResult = this.ajaxResponseText.evalJSON();
	},

	convertAjaxResult: function(match) {
		var code = match.substring(2);
		return eval('"\\u' + code.toUpperCase() + '"');
	},

	covertPhpJsonData: function(str) {
		var data = null;

		if (str) {
			str.replace(/\\0/g, '\0').replace(/\\(.)/g, '$1');
			str.replace(/\\u[a-f\d]{4}/gm, this.convertAjaxResult);
			data = str.evalJSON();
		}
		return data;
	},

	handleAjaxFailure: function() {
	},

	getSelectedValue: function(element) {
		var sObj = null;

		if (typeof(element) == 'object') {
			sObj = element;
		} else {
			sObj = $(element);
		}
		
		if (sObj && sObj.options) {
			return sObj.options[sObj.selectedIndex].value;
		} else {
			return false;
		}
	},

	updateElement: function(element, value) {
		if ($(element)) {
			$(element).update(value);
		}
	},

	reloadPage: function() {
		window.location.reload(true);
	},

	redirectPage: function(url) {
		if (url) {
			window.location.replace(url);
		}
	},

	jumpToHash: function(hash) {
		if (hash) {
			window.location.hash = hash;
		}
	},

	getMousePosition: function(e) {
		var posx = 0;
		var posy = 0;

		if(!e) var e = window.event;

		if (e) {
			if (e.pageX || e.pageY) {
				posx = e.pageX;
				posy = e.pageY;
			} else if (e.clientX || e.clientY) 	{
				posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
				posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
			}
			mousePosition.x = posx;
			mousePosition.y = posy;
		}
	},
	
	debug: function(str) {
		alert(str);
	}
}
