
/**
 * @short Global events to add on all pages.
 */
$(document).ready(function(){
	//aButton();
	$(".listing").tablesorter({widgets:['zebra']});

})



openUserSelect = function(label){
	userSelect=createOverWindow()
	userSelect.load('/users/select/?inputid='+label)
}

openUserSelectFilter = function(label,filter){
	window.open('/users/select/?filter='+filter+'&inputid='+label,'select_user','width=500,height=330,scrollbars=yes,resizable=yes,location=no')
}

/**
 * @short Opens a user select window, and calls the given function with 
 *        (id, username, full name, usertype)
 */
openUserSelectWithFunc = function(label){
	window.open('/users/select/?set_value_func='+label,'select_user','width=500,height=330,scrollbars=yes,resizable=yes,location=no')
}

/**
 * @short onsubmit handler that opens a file upload progress dialog.
 *
 * to use it you also need to set enctype="multipart/form-data" and action="/mp/upload/upload"
 *
 * if all this is set, then the form saves the file at that action, and send the other form contents
 * (plus <filename>_realname field with the filename) to the original document, like if no
 * action where set.
 *
 * It is only opened if there is really a file.
 */
progressUpload = function(filefield, uploadId){
	if (!uploadId)
		uploadId=filefield

	if (!$(filefield))
		return;
	try{
		filename=$(filefield).value.split(/[\\\/]/g)
		filename=filename[filename.length-1]
	
		if (filename!='')
			progressWindow=window.open('/upload/?uploadId='+uploadId,
					'attachmentprogress','width=400,height=300,scrollbars=yes,resizable=yes,location=no')
	}
	catch(e){
		alert(e)
	}
}

/**
 * @short Trick to close progress windows.. the drawback is that is may reopen a window. (and close)
 */
closeProgressWindow = function(){
/*
// DOES NOT WORK... If you have any ideo on how to make it work...

	window.open('','attachmentprogress',
				'width=400,height=200,scrollbars=yes,resizable=yes,location=no').close();
*/
}


var languageCode='en'
var translations={'es':{'Please wait...':'Por favor espere...'}}


var print_mode=false;

setPrintMode = function(){
	print_mode=true;
}

/**
 * @short Includes another javascript code
 */
include = function(filename){
	inc=$('<script/>').attr('src',filename)
	$('head').append(inc)
}


/**
 * @short Basic user chooser.
 *
 * Opens the /users/ user chooser, with the id of the button to change.
 */
selectUser = function(t){
	userSelect=createOverWindow()
	userSelect.load('/users/',{ userSelect:1, 'id':$(t).attr('id') })
}

/**
 * @short Creates a modal window over all the other content.
 */
createOverWindow = function(){
	mscreen=window.screen
	mheight=mscreen.height
	mwidth=mscreen.width

	var overwindow=$('<div id="overwindow"/>')
	overwindow.attr('style','position:fixed; width: 75%; height: 80%; left:12.5%; top: 10%;  z-index:102; overflow: auto; background:#FFFFFF none repeat scroll 0 0; border: 1px solid black; display: block; padding: 1em; ')
	overwindow.text(_('Please wait...'))

	var grey=$('<div id="greyback"/>')
	var clos=$('<img src="/static/images/close.png">').click(function(){ grey.click() })
	clos.attr('style','position:fixed; right:9.5%; top: 9.5%;  z-index:112; cursor: pointer;')

	grey.attr('style','position: fixed; top:0px; left:0px; width:100%; height:100%; background:#000; opacity:0.8; z-index: 100; ')
	grey.bind('click',function(){
		overwindow.remove()
		grey.remove()
		clos.remove()
	})
	
	$('body').append(grey)
	$('body').append(clos)
	$('body').append(overwindow)
	overwindow.corner()
	return overwindow;
}

/**
 * @short Closes the overwindow
 */
closeOverWindow = function(){
	$('#greyback').trigger('click')
}

/// Counter of nested loadings...
loadingCounter=0
loadingMsg=0

/// Show loading banner
loading = function(){
	if (loadingCounter==0){
		$('#loading').show()
		if (loadingMsg)
			$('#loading h1').text(loadingMsg)
	}
	loadingCounter++;
}

// hide loading banner, only if called as many times as called loading().
stopLoading = function(){
	loadingCounter--;
	if (loadingCounter==0){
		$('#loading').hide()
		loadingMsg=$('#loading h1').text()
		$('#loading h1').text('')
	}
}



/**
 * @short Stuff to do at loading time, like preparing the file choosers.
 */
$(document).ready(function(){
	$('.user_select').bind('click',function(){
		selectUser(this);
	})
})
