/**
 * Redirects to some page.
 */
function redirect(page) {
    window.location = page;
}

/**
 * Reloads current page.
 */
function reload() {
    window.location.reload(true);
}

/**
 * Jumps to some page, history not modified.
 */
function jump(page) {
    window.location.replace(page);
}

/**
 * Custom, friendly, alert that replaces native alert (still available with <code>alertalert</code>).
 */
var alertalert = window.alert;
var alertCount = 0;
window.alert = function(message) {
    var id = alertCount;
    alertCount += 1;
    var close = "<div class='alertMsgClose'><a href='#' onclick='$(\"#alert-" + id + "\").remove();'>[X]</a></div>";
    var divText = "<div class='alertBox' id='alert-" + id + "'>" + close + message + "</div>";
    $("body").prepend(divText);
};


/**
 * JS extends.
 */
extend = function(subClass, baseClass) {
    // Create a new class that has an empty constructor with the members of the baseClass
    function inheritance() {
    }

    inheritance.prototype = baseClass.prototype;
    // set prototype to new instance of baseClass _without_ the constructor
    subClass.prototype = new inheritance();
    subClass.prototype.constructor = subClass;
    subClass.baseConstructor = baseClass;
    // enable multiple inheritance
    if (baseClass.base) {
        baseClass.prototype.base = baseClass.base;
    }
    subClass.base = baseClass.protomatype;
};

/**
 * jQuery ajax.
 */
$.ajaxSetup({
    cache: false
});

$(document).ajaxError(function(event, xhr, settings) {
    var status = xhr.status;
    if (xhr.readyState === 0 || status === 0 || status == 200) {
        return;  // it's not really an error
    }
    if (status == 403) {
        redirect('/login.html');
        return;
    }
    if (status === 404 || status === 500) {
        redirect('/error.' + status + '.html?url=' + encodeURI(settings.url));
        return;
    }
    alertalert('Ajax call failed: ' + status + '\n' + settings.url);
});


/**
 * Shows or hides 'please wait' caption bar.
 */
var pwt;
function pleaseWait(show) {
    if (show === false) {
        if (pwt != undefined) {
            clearTimer(pwt);
            pwt = undefined;
        }
        $("#wait").hide();
    } else {
        if (pwt == undefined) {
            pwt = setTimer(500, function() {
                $("#wait").show();
            });
        }
    }
}
/**
 * Shows or hides reload overlay over target element.
 * Using blockui.
 */
function showReload(target, show) {
    if (show === false) {
        target.unblock();
        return;
    }
    target.block({
        message: '<img src="/gfx/reload.gif">',
        overlayCSS: { backgroundColor: '#999', color: '#666' },
        css: { border: 'none', backgroundColor: 'transparent'}
    });
}

/**
 * Creates timer.
 */
function setTimer(time, func, callback) {
    var a = {timer:setTimeout(func, time), callback:null};
    if (callback) {
        a.callback = callback;
    }
    return a;
}

/**
 * Clears timer.
 */
function clearTimer(a) {
    clearTimeout(a.timer);
    if (a.callback) {
        a.callback();
    }
    return this;
}

function showGrowl(headerText, messageText) {
    var growl = $("<div class=\"growlUI\"><h1>" + headerText + "</h1><h2>" + messageText + "</h2></div>");
    $.blockUI({
        message: growl,
        fadeIn: 700,
        fadeOut: 700,
        timeout: 2000,
        showOverlay: false,
        centerY: false,
        css: {
            width: '350px',
            top: '10px',
            left: '',
            right: '10px',
            border: 'none',
            padding: '5px',
            backgroundColor: '#000',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: .8,
            color: '#fff'
        }
    });
}

function showDialog(divToShow) {
    // Add additional css changes if needed. One change here will reflect through the whole application
    divToShow.append("<div id=\"fancy_close\" style=\"display: block;\" onclick=\"cancelBlockUI()\"></div>")
    $.blockUI({ message: divToShow,
        css: {
//            width: '510px',
            backgroundColor: '#FAFAFA',
            cursor: 'auto'
        },
        overlayCSS: {cursor: 'auto' }})
}

function showDialog(divToShow, width) {
    // Add additional css changes if needed. One change here will reflect through the whole application
    divToShow.append("<div id=\"fancy_close\" style=\"display: block;\" onclick=\"cancelBlockUI()\"></div>")
    $.blockUI({ message: divToShow,
        css: {
            width: width,
            backgroundColor: '#FAFAFA',
            cursor: 'auto'
        },
        overlayCSS: {cursor: 'auto' }})
}

function cancelBlockUI() {
    $.unblockUI();
    return false;
}
/**
 * Hack for placeholders (for all browsers that do not support it yet)
 */

//if(!('placeholder' in document.createElement('input'))){
//    $('[placeholder]').focus(function() {
//      var input = $(this);
//      if (input.val() == input.attr('placeholder')) {
//        input.val('');
//        input.removeClass('placeholder');
//      }
//    }).blur(function() {
//      var input = $(this);
//      if (input.val() == '' || input.val() == input.attr('placeholder')) {
//        input.addClass('placeholder');
//        input.val(input.attr('placeholder'));
//      }
//    }).blur().parents('form').submit(function() {
//      $(this).find('[placeholder]').each(function() {
//        var input = $(this);
//        if (input.val() == input.attr('placeholder')) {
//          input.val('');
//        }
//      })
//    });
//}
