﻿/********************************************************************
	created:	2009/09/30
	created:	30:9:2009   10:09
	filename: 	D:\MyWork\Swim.com\swim-frontend\jquery\jquery.ext.js		
	author:		phuc.pham	
	to do:		
*********************************************************************/

String.prototype.breakString = function(limit){
    
    var temp = this.split(' ');
    for( var index = 0; index<temp.length; index ++){
        var str = temp[index];
        if( str.length >  limit  ){
            str = str.split('').join('<wbr/>'); // str.substring(0, limit) + '<wbr/>' + str.substring(limit).breakString(limit);            
        }
        
        temp[index] = str;
    }
    
    return temp.join(' ');
};

String.prototype.isUrl = function(){
			return /^(((https?|ftp):\/\/)|www\.)(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(this);
};



String.prototype.format = function(){
	if( arguments.length < 1 )
		return;
	
	var newthis = this;
	
	for( var index = 0; index < arguments.length; index ++ ){
		var str = '{'+ index +'}';
		
		newthis = newthis.split(str).join(arguments[index]);
	}
	
	return newthis;
};

String.prototype.wrapLinkOverflow = function(limit){
	var linkTemplate = '<a title="{0}" href="{0}" target="_blank">{1}</a>';
	var temp = this.split(' ');
	var ret = [];
	var count = 0;
	var flagUrl = false;
	var flagText = false;
	var result = '';
	
	for( var index = 0; index < temp.length; index ++ ){
		var href = temp[index];
		var text = temp[index];
		
		if( href.isUrl() ){
			if( count + text.length > limit ){
				text = text.substring(0, limit - count);
				if( limit - count > 8 )
					ret.push(linkTemplate.format(href, text));
				else
					ret.push(text);
				
				flagUrl = true;
				break;
			}
			
			ret.push(linkTemplate.format(href, text));
		}
		else{
			if( count + text.length > limit ){
                text = text.substring(0, limit - count);
				
				flagText = true;
			}
			
			ret.push( text );
		}		
		
		count += text.length;
		limit --;
	}	
	
	result = ret.join(' ');
	
	if( flagUrl || flagText ){
	    var $ = jQuery; if($) result = $.trim(result);
	    
	    if( flagText ){
	        var index = result.lastIndexOf(' ');
	        if( index > 0 ) result = result.substring(0, index);
	    }
	    
	    result = result.concat('...');
	}
	
	return result;
}

String.prototype.wrapLink = function(){
    
    var linkTemplate = '<a href="{0}" target="_blank">{1}</a>';
    var temp = this.split(' ');
    
    for( var index = 0; index<temp.length; index ++){
        var href = temp[index].split('<wbr/>').join('');
        var text = temp[index];
        
        if(! href.isUrl() ){            
            continue;
        }
        
        temp[index] = linkTemplate.replace('{0}', href).replace('{1}', text);
    }
    
    return temp.join(' ');
};

String.prototype.casePascal = function(){
    var text = this;
    
    var array = text.split(' ');
    var index =  0;            
    
    for(; index < array.length; index++){
        if( array[index] == "" )
            continue;
            
        array[index] = array[index].substring(0, 1).toUpperCase() + array[index].substring(1, array[index].length).toLowerCase();
    }
    
    return array.join(' ');
};

(function($){
	    $.extend($, {
	    
	        postJSW: function(url, data, callback, type, completeCallback){
	            var newdata = $.getJSONString(data);
	            
	            //return $.post(url, {data: newdata}, callback, type);
	            
	            if( callback == null )
	                callback = function(){};
	                
	            if( completeCallback == null )
	                completeCallback = function(){};
	            
	            return $.ajax({
	                url: url,
	                type: "post",
	                data: ({data: newdata}),
	                dataType: type,
	                success: callback,
	                complete: completeCallback
	            });
	        },
	        
	        getJSONLength: function(json){
	            var count = 0;
	            $.each(json, function(key, item){
	                count ++;
	            });
	            
	            return count;
	        },
	    
	        getJSONString: function(json){
	            var ret = [];        
        
                if(typeof(json) != "object")
                    return "";
                
                for(var key in json){
                    if(json[key] != null){
                        var type = typeof(json[key]);
                        var value = json[key];                
                        
                        switch(type){
                            case "object":
                                if( value.constructor == Array ){
                                    var ars = [];
                                    for(var idx in value){
                                        ars.push($.getJSONString(value[idx]));
                                    }
                                    
                                    value = "[" + ars.join(",") + "]";
                                }
                                else {
                                    value = $.getJSONString(value);
                                }
                                break;
                            case "string":
                                value = "'" + value.replace(/'/g, "\\'") + "'";
                                break;
                            case "number":                        
                                break;
                            case "boolean":
                                break;
                            default:
                                continue;
                        }
                        
                        ret.push(key + ":" + value);
                    }
                    else{
                        ret.push(key + ":'null'");
                    }
                    
                    
                }
                
                return "{" + ret.join(",") + "}"
	        }
	    
	    });
	})(jQuery);
	
	

(function($){
	$.fn.extend({
	    disableTextSelect : function() {
		        return this.each(function(){
			        if($.browser.mozilla){//Firefox
				        $(this).css('MozUserSelect','none');
			        }else if($.browser.msie){//IE
				        $(this).bind('selectstart',function(){return false;});
			        }else{//Opera, etc.
				        $(this).mousedown(function(){return false;});
			        }
		        });
        },
        
        attrs: function(name){
            var values = [];
            
            this.each(function(){
                values.push($(this).attr(name));
            });
            
            return values;
        }
    });
})(jQuery); 

