﻿jQuery.validator.setDefaults({
	success: "valid"
});;

// Enables jQuery features
$(document).ready(function() {
    $('input').each(function() {
        $(this).watermark($(this).attr('watermark'));
    });
    $(".ValidForm").validate();
    $(".accordion").accordion({ autoHeight: false });
    $("a.colorbox").colorbox({ width: '50%', height: '50%', overlayClose: true, close: "[x] close", opacity: 0.6 });
    $(".colorbox-big").colorbox({ width: '75%', height: '75%', overlayClose: true, close: "[x] close", opacity: 0.6 });
    $(".colorbox-pretest").colorbox({ width: '640px', overlayClose: true, close: '[x] close', opacity: 0.6, open: true });
    
    $(".tabs").tabs(
	    {
	        spinner: '<em>loading...</em>'
	    }
	);
    $('.ajax-table').dataTable({
        "bJQueryUI": true,
        "bPaginate": false,
        "bSortClasses": false
    });
    $('.ajax-table-page').dataTable({
        "bJQueryUI": true,
        "bPaginate": true,
        "sPaginationType": 'full_numbers',
        "bSortClasses": false
    });

    $('a.doc-view').bind('click', function(event) {
        event.preventDefault();
        var url = this.href
        var id = $('#' + this.target).val();
        if ($('#' + this.target).val()) {
            window.open(url + id);
        }
        return false;
    });
    $('a.disc-status').bind('click', function(event) {
        event.preventDefault();
        var currentStatus = $(this).text();
        var status = ['new', 'running', 'complete', 'delete']
        var url = '/Util/DiscStatus.ashx?a={0}&id={1}'
        //alert(currentStatus);
        switch (currentStatus) {
            case status[0]: //New
                {
                    $(this).fadeOut();
                    $(this).text(status[1]);
                    $(this).fadeIn();
                    url = url.replace("{0}", status[1]);
                    url = url.replace("{1}", this.target);
                    $.get(url, {}, function(response) { })
                    return
                }
            case status[1]: //Running
                {
                    $(this).fadeOut();
                    $(this).text(status[2]);
                    $(this).fadeIn();
                    url = url.replace("{0}", status[2]);
                    url = url.replace("{1}", this.target);
                    $.get(url, {}, function(response) { })
                    return
                }
            case status[2]: //Complete
                {
                    $(this).fadeOut();
                    $(this).text(status[0]);
                    $(this).fadeIn();
                    url = url.replace("{0}", status[0]);
                    url = url.replace("{1}", this.target);
                    $.get(url, {}, function(response) { })
                    return
                }
            case status[3]: //Delete
                {
                    if (confirm('Are you sure?')) {
                        url = url.replace("{0}", status[3]);
                        url = url.replace("{1}", this.target);
                        $.get(url, {}, function(response) { })
                        $('#jobrow-' + this.target).fadeOut();
                        return
                    }
                }
            default: { alert('Problem. Reload the page and try again or report the error'); }
        }
    })
});


String.format = function() { 
  var s = arguments[0]; 
  for (var i = 0; i < arguments.length - 1; i++) {        
    var reg = new RegExp("\\{" + i + "\\}", "gm");              
    s = s.replace(reg, arguments[i + 1]); 
  } 
 
  return s; 
}

function closeMe() {
    // closes a colobox from within the colorbox
    parent.$.fn.colorbox.close()
}
