/* jQuery 'qalendar' plugin 

// <script>
// $(function() {
//     $('#txtcal').qalendar(); // $('#txtcal').qalendar({month:11, year:2000}); 
// });
// </script>

 * @author 	Vlad Bazon vlad@sitsco.com
 * @link http://salar.sitsco.com http://www.docere.ro
 */

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length >>> 0;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

(function($) {
    var zile = ["Duminică", "Luni", "Marţi", "Miercuri", "Joi", "Vineri", "Sâmbătă"];
    var luni = ["Ianuarie", "Februarie", "Martie", "Aprilie", "Mai", "Iunie", "Iulie", "August", "Septembrie", "Octombrie", "Noiembrie", "Decembrie"];
    var sz_lu = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    var tdate = new Date();
    var tmonth = tdate.getMonth();
    var tyear = tdate.getFullYear();
    var tday = tdate.getDate(); 

    function qalendar(element, month_year) {
        var options = {
            month: tmonth,
            year: tyear
        };
        $.extend(options, month_year);
        
        var month = parseInt(options.month);
        var year = parseInt(options.year);
        tdate = new Date(year, month);

        var szm = (month == 1 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)) ? 29 : sz_lu[month];

        tdate.setDate(1);
        var prima = tdate.getDay();
        tdate.setDate(31);
        var ultima = tdate.getDay();
        var sap = 0, z;
        var ht = [];
        ht.push("<table border='0' style='background:#efefef;color:#4A4F45;font-family:monospace;'>");
        ht.push("<tr><td><input type='image' src='/static/prev_icon.gif' id='qalepr'/></td>");
        ht.push("<td colspan='5' style='text-align:center;color:#448;font-size:0.9em;'>" + "<span id='qalemh'><b>" + luni[month] + "</b></span><span id='qaleyr'><b> " + year + "</b></span></td>");
        ht.push("<td style='text-align:right'><input type='image' src='/static/next_icon.gif' id='qalenx'/></td></tr>");
        ht.push("<tr>");
        for(z = 0; z < 7; z++) {
            ht.push("<td  align='center'>");
            if(z == 0 || z == 6)
                ht.push("<span style='color:red;'>" + zile[z].substring(0,2) + "</span></td>");
            else
                ht.push("<span style='color:#448;'>" + zile[z].substring(0,2) + "</span></td>");
        }
        ht.push("</tr><tr>");
        for(z = 1; z <= prima; z++) {
            sap++;
            ht.push("<td>&nbsp;</td>");
        }
        z = 1; var q = sap;
        var zl = szm;
        while(z <= szm) {
            if(sap % 7 == 0) { q = 0; ht.push("</tr><tr>"); }
            if(q == 0 || sap == 6) {
                q ++; zl --;
                if(z == tday)
                    ht.push("<td align='center'><span style='color:red;background:yellow;'>" + z + "</span></td>");
                else
                    ht.push("<td align='center'><span style='color:red;'>" + z + "</span></td>");
            }
            else {
                q++;
                if(q == 6) q = 0;
                if(z == tday)
                    ht.push("<td align='center'><span style='color:#000;background:yellow;'>" + z + "</span></td>");
                else
                    ht.push("<td align='center'><span style='color:#000;'>" + z + "</span></td>");
            }
            z ++;
            sap ++;
        }
        ht.push("</tr><tr><td colspan='7'><span style='font-size:0.9em; color: #448;'><b>"+zl+"</b> zile lucrătoare <i>standard</i></span>" + "</td></tr></table>");
                
        element.html(ht.join(''));

        $('#qalepr,#qalenx', element).each(function(i) {
            var idx = luni.indexOf($('#qalemh',element).text());
            var an = parseInt($('#qaleyr',element).text());
            switch(i) {
              case 0: 
                if(idx == 0) {
                    idx = 11; an --;
                }
                else idx --;
                break;
              case 1:
                if(idx == 11) {
                    idx = 0; an ++;
                }
                else idx ++;
            } 
            $(this).click(function() {
                $(element).qalendar({month: idx, year: an});
                //return false;
            });
        });
            
    };

    $.fn.qalendar = function(hash) {
        qalendar(this, hash); 
        return this;
    };

})(jQuery);

