var pageUri = document.location.href;

jQuery.fn.extend({
    findPos : function() {
        obj = $(this).get(0);
        var curleft = obj.offsetLeft || 0;
        var curtop = obj.offsetTop || 0;
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
        return {
            x:curleft,
            y:curtop
        };
    }
});

jQuery.fn.center = function () {
    this.css("position", "fixed");
    this.css("top", ( $(window).height() - this.height() ) / 2);
    this.css("left", ( $(window).width() - this.width() ) / 2);
    return this;
}

function core_alert(str) {
    $("#core_dialog").append('<div class="mui-warning-line">'+str+'</div>');
    $("#core_dialog").dialog("open");
}

function mkSjax(target,url,data) {
    $.ajax({
        url: url,
        type: "POST",
        data: data,
        async:false,
        success: function(msg){
            if (target.is('input')) {
                target.val(jQuery.trim(msg));
            } else {
                target.html(msg);
            }
            
            if($('#ajAlert').size() != 0) {
                alert($('#ajAlert').text());
                $('#ajAlert').remove();
            }
        }
    });
}

$(document).ready(function() {	 

    $('.tip').live('mousemove', function(event) {
        var letip = $(this).attr('tip');
        $('#core_tooltip').html(letip);
        $('#core_tooltip').css({
            'display':'block',
            'left':(event.pageX+5)+'px',
            'top':(event.pageY+20)+'px'
            });
    });

    $('.tip').live('mouseout', function(event) {
        $('#core_tooltip').css({
            'display':'none'
        });
    });
        
    $('.bt_user').click(function() {
        $('#user_choice').val($(this).text());
        $('#switchuser').submit();
    });
    
    $(".mui-grid th").click(function ()	{
		var th_url = $(this).children().attr('href');
		if (th_url != undefined) {
			window.location.href = th_url;
		}
		return false;
	});
	
	$(".mui-grid tr").dblclick(function ()	{
		var tr_url = $(this).find(".update").attr('href');
		if (tr_url != undefined) {
			window.location.href = tr_url;
		}
		return false;
	});
    
	$('.show_search_form').click(function() {
		$('.search_form').slideToggle('fast');
		return false;
	});
    

	$("form .inputs").focus(function ()	{
		$(this).removeClass('error');
	});
	
	$(".form_options .advanced").click( function() {
		$(this).hide();
		$('.form_options .pasadvanced').show();
		$(".form_wrapper").find('.optionnal').show();
	});
	
	$(".form_options .pasadvanced").click( function() {
		$(this).hide();
		$('.form_options .advanced').show();
		$(".form_wrapper").find('.optionnal').hide();
	});
	
	/* delete confirmation */
	$(".delete").click(function () {
		if(confirm("Confirm delete ?") == false) {
			return false;
		}
	});
    
    var isCtrl = false;
    document.onkeyup=function(e){
        if(e.which == 17) isCtrl=false;
    }
    document.onkeydown=function(e){
        if(e.which == 17) isCtrl=true;
        if(e.which == 83 && isCtrl == true) {
            //run code for CTRL+S -- ie, save!
            $('.mui-form:first').submit();
            return false;
        }
    }
    
    $('.form_wrapper .date').datepicker({ dateFormat: 'dd/mm/yy', showButtonPanel: true, changeMonth: true, changeYear: true, showWeek: true, firstDay: 1 });

});

