/*
 * Author: Armande Bayanes (tuso@programmerspride.com)
 * Description: Define the globally accessible JS methods.
 **/

/* Add trim prototype for the String object. 
 * i.e. myString.trim()
 **/
if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); //replace(/^\s+|\s+$/g, ''); 
  }
}

var GBL = new function() {
	var cmodule_loader = '';
	var _self = this;
	
    this.numOnly = function(e) {
        var key;
        var keychar;

        if(window.event) {key = window.event.keyCode;}
        else
        if(e) {key = e.which;}
        else {return true;}

        keychar = String.fromCharCode(key);

        /* Control keys. */
        if((key == null) ||
           (key == 0) ||
           (key == 8) ||
           (key == 9) ||
           (key == 13) ||
           (key == 27)) {
           return true;
        }
        else
        if((("0123456789").indexOf(keychar) > -1)) {return true;}
        else {return false;}
    }

    this.countChars = function(obj, disp, max) {

        if(obj.val().length > max){
            obj.val(obj.val().substr(0, max));
        }

        var xy = (max - obj.val().length);
        if(disp.is(':input') == false) disp.html(xy);
        else disp.val(xy);
    }
    
    this.toggle_process = function(obj) {
        
        var processor = jQuery('#processor');
        if(processor && processor.length) {
            
            processor.remove();
            
        } else {
            
            obj = jQuery(obj);
            var loader = '<div id="processor" style="position: absolute; background: #FFF; z-index: 99; height: 50px; width: '+ (obj.width() + 50) +'px; left: '+ obj.position().left +'px; top: '+ obj.position().top +'px"><div class="fltLf"><img src="'+ blankGIF +'" class="loader" /></div> <div class="fltLf" style="padding-left: 5px; cursor: default">processing ...</div><div class="clear"></div></div>';        
            jQuery('body').append(loader);
        }
    }
    
    this.get_keypressed = function(e) {
        
        /*
         * Cross-browser "event object" detection.
         * IE -> window.event
         * FF -> event
         * 
         **/
        
        var eventObj = (window.event ? event : e);
        var key = (eventObj.charCode ? eventObj.charCode : eventObj.keyCode);
        
        return key; /* String.fromCharCode(key). */
    }
    
    this.blank = function(obj) {
        
        var blank = jQuery.trim(obj.val()).length;
        
        if(blank == 0) return true;
        else return false;
    }
	
	this.request = function(url,http_data,callback,datatype,added_opt){

		datatype 	= datatype ? datatype : 'json';
		http_data 	= http_data ? http_data : {}; 
		callback 	= callback ? callback : function(){};
		added_opt   = added_opt ? added_opt : {};
		
		added_opt.showloader = added_opt.showloader == null ? true : added_opt.showloader;

		if( added_opt.showloader == true ){
			_self.show_loader();
		}
		var opt = { 
				url: url , 
				data: http_data,
				dataType: datatype,
				type: 'post',
				success: function(e){
					if(e)
					{
						if(e.gotologin){
							//_self.redirect( BASE_URL + 'user/login');
							$('#dialog-login').dialog('option',LOGIN_DIALOG_OPT).dialog('open');
							_self.hide_loader();
							return;
						}
						if(e.alert)
						{
							alert(e.alert);
						}
						
						if(e.log)
						{
							console.log(e.log);
						}
						callback(e);
					}
					_self.hide_loader();
	      		}
		}
		optstr = '';
		for(o in added_opt)
		{
			 optstr += "opt." + o + " = added_opt[o];";
		}
		eval( optstr );
		jQuery.ajax(opt);
	}
	
	this.show_loader = function(){
		if( cmodule_loader.length == 0 ){
			height = $(window).height();
			width = $(window).width();
			$('body').append('<div id="cmodule_loader"><div class="loader-wrapper" style="left:50%;position:fixed;top:50%;z-index:9002;"><img src="'+DOCROOT+'media/images/ajax-loader.gif"></div><div class="ui-widget-overlay" style="width: '+width+'px; height: '+height+'px; z-index: 9001;position:fixed; top:0;left:0"></div></div>');
			cmodule_loader = $('#cmodule_loader');
		}else{
			cmodule_loader.show();
		}
	}
	
	this.hide_loader = function(){
		if( cmodule_loader.length > 0 ){
			cmodule_loader.hide();
		}
	}
	
	this.redirect = function(url){
		if(url){
			window.parent.location = url;	
		}
	}
	
	this.msg_con = function(msg, icon ){
		icon = icon ? icon : 'alert';
		var str = '<div class="ui-widget">\
				<div style="padding: 0 .7em;" class="ui-state-error ui-corner-all"> \
					<p><span style="float: left; margin-right: .3em;" class="ui-icon ui-icon-'+icon+'"></span> \
					<strong>Alert:</strong> '+msg+'.</p>\
				</div>\
			</div>';
		return str;	
	}
}

/* Extend jQuery, add the function named "fixedCenter".
 * 
 * Sample usage:
 * 
 *      1.  $('#element_id').fixedCenter()
 *      2.  jQuery('#element_id').fixedCenter()
 **/
jQuery.fn.fixedCenter = function(){
    
    return this.each(function(){
        
        var element = jQuery(this), win = jQuery(window);
        centerElement();

        jQuery(window).bind('resize', function() {
            centerElement();
        });

        function centerElement(){
            
            var elementWidth, elementHeight, windowWidth, windowHeight, X2, Y2;
            elementWidth = element.outerWidth();
            elementHeight = element.outerHeight();
            windowWidth = win.width();
            windowHeight = win.height();
            X2 = (windowWidth/2 - elementWidth/2) + "px";
            Y2 = (parseInt((windowHeight/3), 10) - elementHeight) +'px'; //((windowHeight/2) - (elementHeight*2) )+ "px";

            jQuery(element).css({
                'left'      : X2,
                'top'       : Y2,
                'position'  : 'fixed'
            });
        }
    });
}

function hide_pretty()
{

	jQuery('#fancybox-wrap, #fancybox-overlay').hide();
	
}
