CH = function(){};
(function() {
	
	// Namespace Declaration
    this.ns = function(fn){
        var ns = {};
        fn.apply(ns);
        return ns;
    };
    
    
	/************************************/
	// Library
	/************************************/
	Lib = {
		toggleClass: function(className) {
			var classElements = $$('.'+className);
    		classElements.invoke('toggle');
		}
	};
	
	
	/************************************/
	// ELEMENT EXTENSIONS
	/************************************/
	var elementExtensions = {
    	swap: function(element, hideElement) {
    		element = $(element);
			
    		$(element).show();
			$(hideElement).hide();
			
			return element;
		},
		
		toggleClass: function(element, className) {
    		element = $(element);
    		
    		classElements = element.getElementsBySelector('.'+className);
    		classElements.invoke('toggle');
			
			return element;
		},
		
		getId: function(element) {
			element = $(element);
			
			return element.id.getId();
		},
		
		insertAfter: function(element, newElement) {
			
			element = $(element);
			newElement = $(newElement);
			
			sib = element.firstChild;
			if(sib) {
				element.insertBefore(newElement, sib);
			} else {
				element.appendChild(newElement);
			}
			
			return element;
		},
		
		reset: function(id, delay) {
			setTimeout("Effect.Fade('"+id+"', {afterFinish: function() {$('"+id+"').className = ''; $('"+id+"').innerHTML = '';$('"+id+"').show();}});", delay);
		},
		
		appendChildren: function(element) {
			element = $(element);

			var args = arguments;
			
			for(var x = 1, cnt = args.length; x < cnt; x++) {
				element.appendChild(args[x]);
			}
			
			return element;
		}
    };
    Element.addMethods(elementExtensions);
	
	
	/************************************/
	// FORM EXTENSIONS
	/************************************/
	var formExtensions = {
		getRadioGroupValue: function(element, radioGroupName) {
			element = $(element);
			input = element[radioGroupName];
			
			for(var i=0, cnt=input.length; i<cnt; i++){
				if(input[i].checked==true) return input[i].value;	
			}
			
			return false;
		}
		
	};
	Element.addMethods(['FORM'], formExtensions);

    
    /************************************/
	// CLASS EXTENSIONS
	/************************************/
	Object.extend(Class, {
    	run: function(properties) {
    		properties.initialize();
    	}
    });
    
	
	/************************************/
	// REQUEST CLASS (ajax wrapper)
	/************************************/
	Req = {};
	
	Req.ajax = Class.create({
		initialize: function(params) {
			this.responseData = '';
			this.params = {};
			
			this.params = params;
			
			if(this.params.options == undefined) {
				this.params.options = {};
			}
			
			this.request();
		},
		
		request: function() {
			this.showLoader();
			
			this.params.options.method = this.params.options.method ? this.params.options.method : 'post';
			
			if(this.params.form) {
				if(this.params.options.parameters == undefined) {
					this.params.options.parameters = Form.serialize(this.params.form);
				} else {
					this.params.options.parameters = this.params.options.parameters.concat('&', Form.serialize(this.params.form));
				}
			}
			
			if(!this.params.url) alert('AJAX: Requires URL');
			if(this.params.debug) console.log('url: %o \npars: %o', this.params.url, pars);
			
			// prep the onComplete function. Add on the hideLoader function
			if(!this.params.options.onComplete) {
				var onComplete = function() { this.hideLoader(); }.bind(this);
			} else if(this.params.onComplete instanceof Array) {
				var funtions = "";
				for(i=0; i < this.params.options.onComplete.length; i++) {
					functions += "this.params.options.onComplete["+i+"]();";
				}
				var onComplete = function(){ eval(functions); this.hideLoader(); }.bind(this);
			} else {
				var func = this.params.options.onComplete;
				var onComplete = function() { func(); this.hideLoader(); }.bind(this);
			}
			
			this.params.options.onComplete = onComplete;
			
			if(this.params.div) {
				new Ajax.Updater(this.params.div, this.params.url, this.params.options);
			} else {
				this.params.options.onSuccess = this.params.options.onSuccess ? this.params.options.onSuccess : this.response;
				new Ajax.Request(this.params.url, this.params.options);
			}
		},
		
		response: function(r) {
			var res=r.responseText;
			if(res=="success")
			{
				Login.hide();
				aftergetImageDetail();
			}
			else
			{
				document.getElementById('login_errors').style.display='block';
				document.getElementById('login_errors').innerHTML=r.responseText;
				Login.enableLoginBtn();
			}
/*			eval('this.responseData = '+r.responseText);
	
			if(this.responseData.debug) {
				console.log(this.responseData);
				console.log(this.responseData.javascript.replace(/;/, "\n"));
			}
		
			if(this.responseData.javascript) eval(this.responseData.javascript); */
		},
		
		showLoader: function() {
			var loader;
			if (loader = $(this.params.loader)) loader.show();
			document.body.style.cursor = "wait";
		},
		
		hideLoader: function() {
			var loader;
			if (loader = $(this.params.loader)) loader.hide();
			document.body.style.cursor = "default";
		}
	});
	
	
	Req.submitForm = Class.create({
		initialize: function(url, frm, btn, xpars) {		
			this.submitForm(url, frm, btn, xpars);
		},
		
		submitForm: function(url, frm, btn, xpars) {
			var btn = $(btn);
			var originalBtnValue = btn.value;
			btn.value = 'Please wait...';
			btn.disabled = true;
			
			var pars;
			if(xpars != undefined) pars += '&' + xpars;
			
			new Req.ajax({
				url: url,
				form: frm,
				options: {
					parameters: pars,
					onFailure: function() { alert('An error occurred. Please try again.'); },
					onComplete: function() { btn.disabled = false; btn.value = originalBtnValue; }
				}
			});
		}
	});
		
	
	/************************************/
	// STRING EXTENSIONS
	/************************************/
	String.prototype.getId = function() {
    	return this.substr(this.indexOf('_') + 1);
    };
    
    String.prototype.trim = function () {
	    return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
	}; 
    
    
    /************************************/
	// ARRAY EXTENSIONS
	/************************************/
	Array.prototype.isEmpty = function() {
    	if(this.length == 0) 
    		return true;
    	 else 
    		return false;
    	
    };
    
    
    /************************************/
	// PROTOTYPE JS AJAX EXTENSIONS
	/************************************/
    // stops queues of now-redundant requests building up / allows you to supercede one request with another easily. 
	// pass in onlyLatestOfClass: 'classname' in the options of the request 
	 
	Ajax.currentRequests = {}; 
	 
	Ajax.Responders.register({ 
	    onCreate: function(request) { 
	        if (request.options.onlyLatestOfClass && Ajax.currentRequests[request.options.onlyLatestOfClass]) { 
	            // if a request of this class is already in progress, attempt to abort it before launching this new request 
	            try { Ajax.currentRequests[request.options.onlyLatestOfClass].transport.abort(); } catch(e) {} 
	        } 
	        // keep note of this request object so we can cancel it if superceded 
	        Ajax.currentRequests[request.options.onlyLatestOfClass] = request; 
	    }, 
	    onComplete: function(request) { 
	        if (request.options.onlyLatestOfClass) { 
	            // remove the request from our cache once completed so it can be garbage collected 
	            Ajax.currentRequests[request.options.onlyLatestOfClass] = null; 
	        } 
	    } 
	}); 

	
    /************************************/
	// BROWSER DETECTION
	/************************************/
	Browser = function() {
		var ua = navigator.userAgent;
		    
		this.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
		this.isMSIE5 = this.isMSIE && (ua.indexOf('MSIE 5') != -1);
		this.isMSIE5_0 = this.isMSIE && (ua.indexOf('MSIE 5.0') != -1);
		this.isMSIE7 = this.isMSIE && (ua.indexOf('MSIE 7') != -1);
		this.isGecko = ua.indexOf('Gecko') != -1; // Will also be true on Safari
		this.isSafari = ua.indexOf('Safari') != -1;
		this.isOpera = window['opera'] && opera.buildNumber ? true : false;
		this.isMac = ua.indexOf('Mac') != -1;
		this.isNS7 = ua.indexOf('Netscape/7') != -1;
		this.isNS71 = ua.indexOf('Netscape/7.1') != -1;
	
		return {
			isMSIE: isMSIE,
			isMSIE5: isMSIE5,
			isMSIE5_0: isMSIE5_0,
			isMSIE7: isMSIE7,
			isGecko: isGecko,
			isSafari: isSafari,
			isOpera: isOpera,
			isMac: isMac,
			isNS7: isNS7,
			isNS71: isNS71
		}
	}();
	
	
	/************************************/
	// BASE64 CLASS
	/************************************/
	Base64 = {
	    // private property
	    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
	
	    // public method for encoding
	    encode : function (input) {
	        var output = "";
	        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
	        var i = 0;
	
	        input = Base64._utf8_encode(input);
	
	        while (i < input.length) {
	
	            chr1 = input.charCodeAt(i++);
	            chr2 = input.charCodeAt(i++);
	            chr3 = input.charCodeAt(i++);
	
	            enc1 = chr1 >> 2;
	            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
	            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
	            enc4 = chr3 & 63;
	
	            if (isNaN(chr2)) {
	                enc3 = enc4 = 64;
	            } else if (isNaN(chr3)) {
	                enc4 = 64;
	            }
	
	            output = output +
	            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
	            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
	
	        }
	
	        return output;
	    },
	
	    // public method for decoding
	    decode : function (input) {
	        var output = "";
	        var chr1, chr2, chr3;
	        var enc1, enc2, enc3, enc4;
	        var i = 0;
	
	        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	
	        while (i < input.length) {
	
	            enc1 = this._keyStr.indexOf(input.charAt(i++));
	            enc2 = this._keyStr.indexOf(input.charAt(i++));
	            enc3 = this._keyStr.indexOf(input.charAt(i++));
	            enc4 = this._keyStr.indexOf(input.charAt(i++));
	
	            chr1 = (enc1 << 2) | (enc2 >> 4);
	            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
	            chr3 = ((enc3 & 3) << 6) | enc4;
	
	            output = output + String.fromCharCode(chr1);
	
	            if (enc3 != 64) {
	                output = output + String.fromCharCode(chr2);
	            }
	            if (enc4 != 64) {
	                output = output + String.fromCharCode(chr3);
	            }
	
	        }
	
	        output = Base64._utf8_decode(output);
	
	        return output;
	
	    },
	
	    // private method for UTF-8 encoding
	    _utf8_encode : function (string) {
	        string = string.replace(/\r\n/g,"\n");
	        var utftext = "";
	
	        for (var n = 0; n < string.length; n++) {
	
	            var c = string.charCodeAt(n);
	
	            if (c < 128) {
	                utftext += String.fromCharCode(c);
	            }
	            else if((c > 127) && (c < 2048)) {
	                utftext += String.fromCharCode((c >> 6) | 192);
	                utftext += String.fromCharCode((c & 63) | 128);
	            }
	            else {
	                utftext += String.fromCharCode((c >> 12) | 224);
	                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
	                utftext += String.fromCharCode((c & 63) | 128);
	            }
	
	        }
	
	        return utftext;
	    },
	
	    // private method for UTF-8 decoding
	    _utf8_decode : function (utftext) {
	        var string = "";
	        var i = 0;
	        var c = c1 = c2 = 0;
	
	        while ( i < utftext.length ) {
	
	            c = utftext.charCodeAt(i);
	
	            if (c < 128) {
	                string += String.fromCharCode(c);
	                i++;
	            }
	            else if((c > 191) && (c < 224)) {
	                c2 = utftext.charCodeAt(i+1);
	                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
	                i += 2;
	            }
	            else {
	                c2 = utftext.charCodeAt(i+1);
	                c3 = utftext.charCodeAt(i+2);
	                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
	                i += 3;
	            }
	
	        }
	
	        return string;
	    }
	
	}
	
}).apply(CH); 

