﻿/*
 * Rich JavaScript Framework, (For CJ Website Project)
 * On jQuery(version 1.4)
 * Copyright (c) 2010 Lee Won-Gyoon <richscript@gmail.com>, <@richscript>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * For details, see the RichScript web site: http://www.richscript.net/
 * 
*******************************************************************************/

var richscript = {
	version : "On jQuery v1.5.1, jQueryUi v1.8.5 (For CJ Website Project)",
	lib : {}
};


richscript.browser = {
	isMobile : (navigator.userAgent.toUpperCase().indexOf("MOBILE")==-1)?false:true,
	isIPad : (navigator.userAgent.toUpperCase().indexOf("IPAD")==-1)?false:true,
	isGalaxyTab : (navigator.userAgent.toUpperCase().indexOf("SHW-M180")==-1)?false:true,
	isIE : ($.browser.msie)?true:false,
	isIE6 : ($.browser.msie&&$.browser.version=="6.0")?true:false,
	isStrict : function() {
		var docRoot = document.documentElement;
		return (docRoot!=undefined);
	},
	screenW : function() {
		var w = window.innerWidth ||
			(this.isStrict() && document.documentElement.clientWidth) ||
			document.body.clientWidth || 0;
		if (!this.isIE&&!this.isMobile) {w-=20;}
		return w;
	},
	screenH : function() {
		return window.innerHeight ||
			(this.isStrict() && document.documentElement.clientHeight) ||
			document.body.clientHeight || 0;
	},
	scrollW : function() {
		return (this.isStrict() && document.documentElement.scrollWidth) ||
			document.body.scrollWidth || 0;
	},
	scrollH : function() {
		return (this.isStrict() && document.documentElement.scrollHeight) ||
			document.body.scrollHeight || 0;
	},
	bodyW : function() {
		return document.body.scrollWidth || 0;
	},
	bodyH : function() {
		return document.body.scrollHeight || 0;
	},
	scrollX : function() {
		return window.pageXOffset ||
			(this.isStrict() && document.documentElement.scrollLeft) ||
			document.body.scrollLeft || 0;
	},
	scrollY : function() {
		return window.pageYOffset ||
			(this.isStrict() && document.documentElement.scrollTop) ||
			document.body.scrollTop || 0;
	},
	maxW : function() {
		return Math.max(this.screenW(), this.scrollW());
	},
	maxH : function() {
		return Math.max(this.screenH(), this.scrollH());
	}
};

/* String Methods (Public) */
$.extend(String.prototype, {
	trim : function () {
		return $.trim(this);
	},
	escapeXml : function() {
		return this
		.replace(/&/g,"&amp;")
		.replace(/\'/g,"&#039;")
		.replace(/\"/g,"&#34;")
		.replace(/</g,"&lt;")
		.replace(/>/g,"&gt;")
		.replace(/\n/g,"&#10;")
		.replace(/\r/g,"&#13;")
		.replace(/\t/g,"&#9;");
	},
	escapeJS : function() {
		return this
		.replace(/\\/g,"\\\\")
		.replace(/\//g,"\\/")
		.replace(/\n/g,"\\n")
		.replace(/\r/g,"\\r")
		.replace(/\t/g,"\\t")
		.replace(/\"/g,"\\\"")
		.replace(/\'/g,"\\'");
	},
	escapeCss : function() {
		return this
		.replace(/\'/g,"\\\'")
		.replace(/\"/g,"\\\"")
		.replace(/\,/g,"\\,")
		.replace(/\(/g,"\\(")
		.replace(/\)/g,"\\)");
	},
	appendParameter : function(_param) {
		return this+((_param!=undefined&&_param!="")?
			((this.indexOf("?")>-1) ? "&":"?")+_param : "" );
	},
	toCamelize : function() {
		var s = this;
		if (s.indexOf("-")>-1) {
			var a = s.split("-");
			s = a[0];
			for (var i=1; i<a.length; i++) {
				if (a[i].length>0) {
					s += a[i].charAt(0).toUpperCase() + a[i].substring(1);
				}
			}
		}
		return s;
	},
	toBold : function() {
		return "<strong>"+this+"</strong>";
	},
	toCssBgPng24 : function() {
		return (($.browser.msie&&$.browser.version=="6.0")
		? ' filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+this+' ,sizingMethod=scale ); '
		: ' background:url('+this+') transparent; ');
	},
	toDomNodes : function() {
		var s = this, nodes = [];
		var box = document.createElement("DIV");
		box.innerHTML = s;
		for (var i=0; i<box.childNodes.length; i++) {
			nodes.push(box.childNodes[i]);
		}
		box = null;
		return nodes;
	}
});



/* Math Methods (Static) */
$.extend(Math, {
	isInt : function(_n, _base) {
		var base = (_base==undefined) ? 10 : _base;
		return (""+parseInt(_n, base)!="NaN");
	},
	isFloat : function(_n) {
		return (""+parseFloat(_n)!="NaN");
	},
	toInt : function(_n, _def, _base) {
		var def = (_def==undefined) ? 0 : _def;
		var base = (_base==undefined) ? 10 : _base;
		return this.isInt(_n, base) ? parseInt(_n, base) : def;
	},
	toFloat : function(_n, _def) {
		var def = (_def==undefined) ? 0 : _def;
		return this.isFloat(_n) ? parseFloat(_n) : def;
	}
});


/* Array Methods (Public) */
$.extend(Array.prototype, {
	remove : function(_obj) {
		for (var i=0; i<this.length; i++) {
			if (this[i]===_obj) {
				this[i] = null;
				delete this[i];
			}
		}
	},
	compact : function() {
		var temp = [];
		for (var i=0; i<this.length; i++) {
			if (this[i]!==null&&this[i]!==undefined) {
				temp.push(this[i]);
			}
		}
		return temp;
	}
});


/* Date Methods (Public) */
$.extend(Date.prototype, {
	toFormatted : function(_s) {
		var s = (_s==undefined) ? "YYYYMMDD" : _s;
		var YYYY = this.getYear();
		var MM = this.getMonth()+1;
		var DD = this.getDate();
		var HH = this.getHours();
		var MI = this.getMinutes();
		var SS = this.getSeconds();
		if (YYYY<1000) {
			YYYY += 1900;
		}
		if (MM<10) MM = "0"+MM;
		if (DD<10) DD = "0"+DD;
		if (HH<10) HH = "0"+HH;
		if (MI<10) MI = "0"+MI;
		if (SS<10) SS = "0"+SS;
		return s.replace(/YYYY/gi,YYYY)
			.replace(/MM/gi,MM)
			.replace(/DD/gi,DD)
			.replace(/HH/gi,HH)
			.replace(/MI/gi,MI)
			.replace(/SS/gi,SS);
	},
	addYear : function(_n) {
		this.setYear(this.getYear()+_n);
		return this;
	},
	addMonth : function(_n) {
		this.setMonth(this.getMonth()+_n);
		return this;
	},
	addDate : function(_n) {
		this.setDate(this.getDate()+_n);
		return this;
	},
	addHours : function(_n) {
		this.setHours(this.getHours()+_n);
		return this;
	},
	addMinutes : function(_n) {
		this.setMinutes(this.getMinutes()+_n);
		return this;
	},
	addSeconds : function(_n) {
		this.setSeconds(this.getSeconds()+_n);
		return this;
	}
});


/**
 * Object Event Class
*****************************/
richscript.lib.ObjectEvent = function() {
	this.actions;
	this.returnValue = true;
};
$.extend(richscript.lib.ObjectEvent.prototype, {
	push : function(_type, _group, _func, _count) {
		if (this.actions==undefined) {
			this.actions = [];
		}
		var type = (""+_type).trim().toLowerCase(),
			group = (""+_group).trim().toLowerCase()
		this.actions.push({
			  type	: type
			, group	: group
			, func	: _func
		});
	},
	on : function(_type, _func, _count) {
		if (_type!=undefined&&_func!=undefined) {
			var group = "", type = (""+_type).toLowerCase();
			var dot = type.indexOf(".");
			if (dot>-1) {
				group = type.substring(dot+1);
				type = type.substring(0,dot);
			}
			this.push(type.trim(), group.trim(), _func, _count);
		}
		return this;
	},
	off : function(_type) {
		if (_type==undefined) {
			_type = "";
		}
		var group = "", type = (""+_type).toLowerCase();
		var dot = type.indexOf(".");
		if (dot>-1) {
			group = type.substring(dot+1);
			type = type.substring(0,dot);
		}
		group = group.trim();
		type = type.trim();
		if (this.actions) {
			for (var i=0; i<this.actions.length; i++) {
				if ((group==""||this.actions[i].group==group)&&(type==""||this.actions[i].type==type)) {
					this.actions[i].func = null;
					this.actions[i] = null;
				}
			}
			this.actions = this.actions.compact();
		}
		return this;
	},
	stopDefault : function() {
		this.returnValue = false;
	},
	stop : function() {
		this.stopDefault();
	},
	onEvent : function() {
		var type = (""+arguments[1]).trim().toLowerCase();
		var removed = false;
		if (this.actions) {
			for (var i=0; i<this.actions.length; i++) {
				if (this.actions[i]&&this.actions[i].type==type) {
					if (typeof(this.actions[i].func)=="function") {
						this.actions[i].func.apply(arguments[0], Array.prototype.slice.apply(arguments, [2]));
					} else {
						eval(this.actions[i].func);
					}
					if (this.actions[i]&&Math.isInt(this.actions[i].count)) {
						this.actions[i].count--;
						if (this.actions[i].count<=0) {
							this.actions[i].func = null;
							this.actions[i] = null;
							removed = true;
						}
					}
				}
			}
			if (removed) {
				this.actions = this.actions.compact();
			}
		}
		var returnValue = this.returnValue;
		this.returnValue = true;
		return returnValue;
	}
});


/* Define Request Class */
richscript.lib.Request = function(_query) {
	var search = (_query!=undefined)?""+_query:"";
	this.search = "";
	this.key = {};
	this.keys = [];
	this.values = [];
	this.host = window.location.host;
	this.port = window.location.port;
	this.pathname = window.location.pathname;
	
	if (search.length>1&&search.indexOf("?")==0) search = search.substring(1,search.length);
	var param = search.split("&");
	var paramValue = "";
	for (var i=0; i<param.length; i++) {
		var index = param[i].indexOf("=");
		if (index>-1) {
			var name = param[i].split("=")[0];
			var value = param[i].substring(index+1, param[i].length).trim();
			if (this.key[name]==undefined) {
				this.set(name, value);
			}
		}
	}
};
$.extend(richscript.lib.Request.prototype, {
	size : function() {
		return this.values.length;
	},
	set : function(_name, _value) {
		if (this.key[_name]==undefined) {
			var index = this.values.length;
			this.key[_name] = index;
			this.keys.push(_name);
			this.values.push(_value);
		}
		this.search = this.search.appendParameter(_name+"="+_value);
	},
	get : function(_name) {
		var value = this.values[this.key[_name]];
		if (value==undefined) value = "";
		return value;
	},
	getKey : function(_index) {
		return this.keys[_index];
	}
});

/* Initialize Default Request */
richscript.request = new richscript.lib.Request(window.location.search);

/* Define Cookies Util */
richscript.cookie = {
	get : function(_name) {
		var list = document.cookie.split(";");
		var value = "";
		for (i = 0; i < list.length; i++) {
			if (list[i].indexOf(_name+"=") > -1) {
				if (list[i].split("=")[0].replace(/\s/g,"") == _name) {
					value = decodeURIComponent(list[i].split("=")[1]);
					break;
				}
			}
		}
		return value;
	},
	set : function(_name, _value, _days, _path, _domain) {
		if (_name!=undefined&&_name.trim()!="") {
			if (_value==undefined) _value = "";
			if (_days==undefined) _days = 365;
			if (_path==undefined) _path = "/";
			/* try { if (_domain==undefined) _domain = location.hostname; } catch(e) { } */
			var d = new Date();
			d.setDate(d.getDate()+_days);
			var s = "";
			s += _name+"="+encodeURIComponent(_value) + ";";
			s += "expires=" + d.toGMTString() + ";";
			s += "path=" + _path + ";";
			if (_domain!=undefined) {
				s += "domain=" + _domain + ";";
			}
			document.cookie = s;
		}
	}
};

/* Printer Object */
var out = {
	print : function(_s) {
		document.write(_s);
	},
	println : function(_s) {
		document.writeln(_s);
	},
	printBr : function(_s) {
		document.writeln(_s+"<br/>");
	}
};

var $js = richscript;


/**
 Use BackgroundImageCache For IE
*****************************/
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

