/*
 * Copyright (c) Gestio i Programes 2009-2010, Albert Filella
 *
 * FUNCTIONS    : formatDate , formatValue
 * Revision     : 7-1-2010
 */

(function($){
    $.fn.formatDate = function(props){
        ///////////////////////////////////////////////
        ////////// VARIABLES //////////////////////////
        ///////////////////////////////////////////////
		if($(this).length==0)return;

        var YYYY,YY,MMMM,MMM,MM,M,DDDD,DDD,DD,D;
        var hhh,hh,h,mm,m,ss,s,ampm,AMPM,dMod,th,time;
        var date,copy_format;


     ///////////////////////////////////////////////
	////////// DEFAULT INIT ///////////////////////
	///////////////////////////////////////////////

        var defaults = {
            language    : "spanish", // english
            format      : "#DD#-#MM#-#YYYY#",
            lower_case  : true,
            new_date    : false
	};
	var config = jQuery.extend(defaults, props);
        
        ///////////////////////////////////////////////
	///////////////////////////////////////////////

        if(config.language=="english"){
            MMMM = ["January","February","March","April","May","June","July","August","September","October","November","December"];
            DDDD = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
            time = ["am","pm"];
        }

        if(config.language=="spanish"){
            MMMM = ["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"];
            DDDD = ["Domingo","Lunes","Martes","Miercoles","Jueves","Viernes","Sabao"];
            time = [" ma–ana"," tarde"];
        }
		var true_date;
        this.each(function(){
			true_date=1;
            if (config.new_date==true){
	 				date = new Date();
			}
            else{
                date = $(this).html();
                date = new Date(date.split("/")[1]+"/"+date.split("/")[0]+"/"+date.split("/")[2]);
				if(date=="Invalid Date") true_date=0;
            }

			if(true_date==1){
            	copy_format = config.format;
	            YY  = ((YYYY=date.getFullYear())+"").slice(-2);
	            MM  = (M=date.getMonth()+1)<10?('0'+M):M;
	            MMM = (MMMM[M-1]).substring(0,3);
	            DD  = (D=date.getDate())<10?('0'+D):D;
	            DDD = (DDDD[date.getDay()]).substring(0,3);
	            th  = (D>=10&&D<=20)?'th':((dMod=D%10)==1)?'st':(dMod==2)?'nd':(dMod==3)?'rd':'th';
            
	            copy_format = config.format
	                .replace("#YYYY#",YYYY)
	                .replace("#YY#",YY)
	                .replace("#MMMM#",MMMM[M-1])
	                .replace("#MMM#",MMM)
	                .replace("#MM#",MM)
	                .replace("#M#",M)
	                .replace("#DDDD#",DDDD[date.getDay()])
	                .replace("#DDD#",DDD)
	                .replace("#DD#",DD)
	                .replace("#D#",D)
	                .replace("#th#",th);

	            h=(hhh=date.getHours());
	                if (h==0) h=24;
	                if (h>12) h-=12;
	            hh = h<10?('0'+h):h;
	            mm=(m=date.getMinutes())<10?('0'+m):m;
	            ss=(s=date.getSeconds())<10?('0'+s):s;
	            AMPM=(ampm=hhh<12?time[0]:time[1]).toUpperCase();

	            copy_format = copy_format
	                .replace("#hhh#",hhh)
	                .replace("#hh#",hh)
	                .replace("#h#",h)
	                .replace("#mm#",mm)
	                .replace("#m#",m)
	                .replace("#ss#",ss)
	                .replace("#s#",s)
	                .replace("#ampm#",ampm)
	                .replace("#AMPM#",AMPM);

	            if(config.lower_case) copy_format = copy_format.toLowerCase();
	            $(this).html(copy_format);
			}
        });
    }

    $.fn.formatValue = function(props){
        ///////////////////////////////////////////////
        ////////// VARIABLES //////////////////////////
        ///////////////////////////////////////////////

        var i;
        var the_value, number, number_array,number_arrayCPY, decimal;
        var negative,decimals,splitted;

        ///////////////////////////////////////////////
	////////// DEFAULT INIT ///////////////////////
	///////////////////////////////////////////////

        var defaults = {
            currency        : "&euro;",
            currency_show   : true,
            mile_point      : ",",
            decimal_total   : 2,
            decimal_point   : ".",
            decimal_show    : true
	};
	var config = jQuery.extend(defaults, props);

        ///////////////////////////////////////////////
	///////////////////////////////////////////////

        $(this).each(function(){
            decimals    = false;
            negative    = false;
            the_value   = $(this).html();

            if($(this).html().indexOf("-")!=-1){
                the_value = the_value.slice(1,the_value.length);
                negative=true;
            }

            if(the_value.indexOf(".")!=-1){
                the_value = the_value/1;
                the_value = the_value.toFixed(config.decimal_total);
                splitted = the_value.split(".");
                decimals=true;
            }
            else splitted = [the_value,"00"];

            number  = splitted[0];
            decimal = splitted[1];
            number_array = number.split('');

            //INVERTIR
            number_array = number_array.reverse();
            number_arrayCPY = [];
            var cnt=1;
            for(i=0; i<number_array.length; i++){
                number_arrayCPY.push(number_array[i]);
                if(cnt%3==0){
                    if(i<number_array.length-1)
                        number_arrayCPY.push(config.mile_point);
                }
                cnt++;
            }
            if(negative) number_arrayCPY.push("-");

            //INVERTIR
            number_arrayCPY = number_arrayCPY.reverse();
            if(config.decimal_show){
                if(decimals){
                    number_arrayCPY.push(config.decimal_point);
                    number_arrayCPY.push(decimal);
                }
            }
            if(config.currency_show) number_arrayCPY.push("&nbsp;"+config.currency);
            $(this).html(number_arrayCPY.join(''));
        });
    }
})(jQuery);
