// From : http://www.west-wind.com/Weblog/posts/509108.aspx
// Original : http://ejohn.org/blog/javascript-micro-templating/
//
this.tmpl = function tmpl(str, data){
    if (this.cache === undefined)
        this.cache = {};
    
    // Figure out if we're getting a template, or if we need to
    // load the template - and be sure to cache the result.
    var fn = !/\W/.test(str) ?
      this.cache[str] = this.cache[str] ||
        tmpl(document.getElementById(str).innerHTML) :
     
      // Generate a reusable function that will serve as a template
      // generator (and which will be cached).
      new Function("obj",
        "var p=[],print=function(){p.push.apply(p,arguments);};" +
       
        // Introduce the data as local variables using with(){}
        "with(obj){p.push('" +
       
        // Convert the template into pure JavaScript
        str
          .replace(/[\r\t\n]/g, " ")
          .split("<%").join("\t")
          .replace(/((^|%>)[^\t]*)'/g, "$1\r")
          .replace(/\t=(.*?)%>/g, "',$1,'")
          .split("\t").join("');")
          .split("%>").join("p.push('")
          .split("\r").join("\\'")
      + "');}return p.join('');");

    // Provide some basic currying to the user
    return data ? fn( data ) : fn;
};

function urlChangeParam(param, value, url)
{
    var i, url, urlPrefix, paramsSegments, params, paramsPos, paramsStr, paramsArr;
    
    if (url === undefined) {
        url = document.location.href;
    }
    
    // If there is an anchor in this url - we must move it to the back
    anchorStr = '';
    anchorStrPos = url.indexOf('/#');
    if (anchorStrPos >= 0)
    {
        anchorStr = url.slice(anchorStrPos);
        url = url.slice(0, anchorStrPos);
    }
    
    paramsPos = url.indexOf(',');
    if (paramsPos < 0)
        paramsPos = url.length;
    urlPrefix = url.substr(0, paramsPos);
    paramsStr = url.substr(paramsPos);
    paramsStr = paramsStr.replace(/^\//, '').replace(/\/$/, '');
    
    paramsSegments = paramsStr.split('/');
    params  = {};
    if (paramsSegments.length > 1) {
        for (i = 0; i < paramsSegments.length;)
        {
            params[paramsSegments[i]] = paramsSegments[i + 1];
            i += 2;
        }
    }
    
    if (value !== false)
        params[param]   = value;
    else
        delete params[param];
    
    paramsArr       = [];
    jQuery.each(params, function (key, value)
    {
        paramsArr.push(key + '.' + value);
    });
    paramsStr = paramsArr.join(',');
    
    return urlPrefix + (paramsStr ? ',' + paramsStr : '') + anchorStr;
}



(function($)
{
    $(function()
    {
        $(".catalog3importList tr td.start a").click(function ()
        {
            var a = $(this)
            jConfirm("Ar tikrai norite pradėti šį importą?", "Patvirtinimas", function (r)
            {
                if (r)
                    document.location = $(a).attr('href');
            });
            
            return false;
        });
        
        $(".tasks_list tr").each(function ()
        {
            var tr = $(this);
            
            $("td.completed a", tr).click(function ()
            {
                var a = $(this)
                jConfirm($.app.lang.task_completed_confirm + '<br />' + $("td.name", tr).html()
                       , $.app.lang.confirmation
                       , function (r)
                {
                    if (r)
                        document.location = $(a).attr('href');
                });
                
                return false;
            });
        });
        
        $(".harms_block_filter select[name='block']").change(function ()
        {
            document.location = urlChangeParam('block', $(this).val());
        });
    });
})(jQuery);
