
/**
 * @short When there is an A inside a boto, botonV or botonVright, predate it
 *        with the A behaviour.
 */
aButton = function(){
	connectButtonToA=function(td){

		connect(td,'onclick',function(src){ 
			href=''
			ok=true
			forEach(src.src().childNodes, function(ch){
				if (ch.tagName=='A'){
					if (href!=''){
						ok=false
						return
					}
					href=ch.href
				}
			})
			if (href!='' && ok)
				window.location=href

		})
		td.style.cursor="pointer"
	}


	ids=getElementsByTagAndClassName("td", "boton");
	forEach(ids, connectButtonToA);
	ids=getElementsByTagAndClassName("td", "botonV");
	forEach(ids, connectButtonToA);
	ids=getElementsByTagAndClassName("span", "botonVright");
	forEach(ids, connectButtonToA);
}


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

})



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

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){
		log(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"/>')
	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()
	})

	$('body').append(grey)
	$('body').append(overwindow)
	return overwindow;
}

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


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