/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie=function(name,value,options){
    var cookieValue=null;
    if(typeof value!='undefined'){
        options=options||{};

        if(value===null){
            value='';
            options.expires=-1;
        }
        var expires='';
        if(options.expires&&(typeof options.expires=='number'||options.expires.toUTCString)){
            var date;
            if(typeof options.expires=='number'){
                date=new Date();
                date.setTime(date.getTime()+(options.expires*24*60*60*1000));
            }
            else{
                date=options.expires;
            }
            expires='; expires='+date.toUTCString();
        }
        var path=options.path?'; path='+(options.path):'';
        var domain=options.domain?'; domain='+(options.domain):'';
        var secure=options.secure?'; secure':'';
        document.cookie=[name,'=',value,expires,path,domain,secure].join('');
    }else{
        if(document.cookie&&document.cookie!=''){
            var cookies=document.cookie.split(';');
            for(var i=0;i<cookies.length;i++){
                var cookie=jQuery.trim(cookies[i]);
                if(cookie.substring(0,name.length+1)==(name+'=')){
                    cookieValue=cookie.substring(name.length+1);
                    break;
                }
            }
        }
    }
    return cookieValue;
};
/**
 *
 *  UTF-8 data encode / decode
 *  http://www.webtoolkit.info/
 *
 **/
jQuery.encodeUtf8=function(string){
    string=string.replace(/\r\n/g,"\n");
    var utftext="";
    for(var n=0;n<string.length;n++){
        var c=string.charCodeAt(n);
        if(c<128){
            utftext+=String.fromCharCode(c);
        }
        else if((c>127)&&(c<2048)){
            utftext+=String.fromCharCode((c>>6)|192);
            utftext+=String.fromCharCode((c&63)|128);
        }
        else{
            utftext+=String.fromCharCode((c>>12)|224);
            utftext+=String.fromCharCode(((c>>6)&63)|128);
            utftext+=String.fromCharCode((c&63)|128);
        }
    }
    utftext=escape(utftext);
    return utftext;
},jQuery.decodeUtf8=function(utftext){
    utftext=unescape(utftext);
    var string="";
    var i=0;
    var c=0, c2=0;
    while(i<utftext.length){
        c=utftext.charCodeAt(i);
        if(c<128){
            string+=String.fromCharCode(c);
            i++;
        }
        else if((c>191)&&(c<224)){
            c2=utftext.charCodeAt(i+1);
            string+=String.fromCharCode(((c&31)<<6)|(c2&63));
            i+=2;
        }
        else{
            c2=utftext.charCodeAt(i+1);
            c3=utftext.charCodeAt(i+2);
            string+=String.fromCharCode(((c&15)<<12)|((c2&63)<<6)|(c3&63));
            i+=3;
        }
    }
    return string;
}

jQuery.fn.reverse = [].reverse;

$(function() {
    $("body").delegate("a", "click", function(event) {
        var that = $(this);
        var disabled = that.data('click-disable');
        if(disabled) {
            event.preventDefault();
        } else {
            that.data('click-disable', true);
            window.setInterval(function() {
                that.data('click-disable', false);
            }, 5000);
        }
    });
});

window.onerror = function (msg, url, line) {
    $.ajax({
        type: "GET",
        cache: false,
        url: wavkySettings.webroot + 'core/jsLog/',
        data: $.param({
            'message': msg,
            'page': window.location.href,
            'url': url,
            userAgent: navigator.userAgent,
            'line': line
        })
    });
};

var wvTestAndRun = function(condition, code) {
    function testRun(condition, code) {
        var result = condition();
        if(result) {
            code();
        } else {
            setTimeout(function() {
                testRun(condition, code);
            }, 250);
        }
    }
    testRun(condition, code);
}

