/*
 * Site wide js functions come along here
 */

$(function() {

	/* To support our nice date formats */
    $('input.dateinput').datepicker({ dateFormat: 'dd-mm-yy', changeYear:true, yearRange: '-70:+10' });

});

$.confirm = function (yes_func, no_func, message, title) {
    
    // Customize messages if necessary
    
    if (message) {
        var old_message = $("#confirm-dialog span.message").html();
        $("#confirm-dialog span.message").html(message);
    } 
    
    if (title) {
        var old_title = $("#confirm-dialog").attr('title', title);
        $("#confirm-dialog").attr('title', title);
    } 
    
    // Pop-up dialog
    
    var dialog = $("#confirm-dialog").dialog({
        resizable: false,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
                if (yes_func) {
                    yes_func();
                }
            },
            Cancel: function() {
                $(this).dialog('close');
                if (no_func) {
                    no_func();
                }
            }
        }
    });
	
	
    
    // Reset customized messages
     dialog.bind('dialogclose', function(event) {
        if (old_message) {
            $("#confirm-dialog span.message").html(old_message);
        }
        
        if (old_title) {
            $("#confirm-dialog").attr('title', old_title);
        }
     });
}

/*
 * registers a click handler on a tag with a href attribute
 * which checks for a confirm before redirecting to the url.
 * f.ex.
 * $('a.confirm_delete').confirm('Question','action');
 */
jQuery.fn.confirm = function(title, question) {
	$(this).click(function(){
		url = $(this).attr('href');
		if(!url) return false;
		$.confirm(function(){ window.location.href = url;}, function(){return false;}, question, title);
		return false;
	});
};

jQuery.fn.submit_confirm = function(title, question) {
	$(this).click(function(){
		form = $(this);
		$.confirm(function(){ form.submit();}, function(){return false;}, question, title);
		return false;
	});
};

$.urlconfirm = function(url) {
    $.confirm(function () {
        location.replace(url);
    });
    return false;
}

function bind_highlight() {
    $('.highlight').each(function() {
        $(this).css('cursor', 'pointer');
        
        $(this).hover(function() {
            $(this).addClass('selected');
        }, function () {
            $(this).removeClass('selected');
        });
        
        $(this).click(function() {
            document.location.href = $('a.detail-link', this).attr('href');
        });
    });
}
                        
/* hover highlight and link boxes */
$(function() {
    bind_highlight();
});

/*
$(function() {
    $('.highlight').live('click', function(event) {
        document.location.href = $('a.detail-link', event.target).attr('href');
    });
});
*/
