// For debugging
function getObj(e){ 
	var str = "";
	for( x in e )
		str += x+"=" +e[x]+"\n";
	return str;
}
// Loads event to window object
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}
}
function aJaxStatus( title, message, error ){
	$('Status_title').innerHTML = title;
	$('Status_content').innerHTML = message + '<br/>';
        if(error != null && error != -1)
            $('Status_content').innerHTML += error;
	$('Status_pop').show();
}

var  aJaxFormCBs = { 
		error: function( message ){},
		success:function( message ){}
		};

function aJaxFormCBsClean(){
	aJaxFormCBs = { 
			error: function( message ){},
			success:function( message ){}
			};
}
function aJaxFormCBsInit(){
	aJaxFormCBs = { 
			error: function( message ){
						aJaxStatus( 'Error', message );
					},
			success:function( message ){
						popupClose();
						aJaxStatus( 'Status', message );
					}
			};
}
function aJaxFormFinalize( status, message, errors ){
	
	if( status == true || status == 'true'){
		aJaxFormCBs.success( message );
		aJaxFormCBsClean();
	}
	else{
		aJaxFormCBs.error(message, errors);
	}
}

function aJaxForm( form, params ){
	this.msg='aaaa';
	this.form = form;
	this.params = params;
	this.submit = function(){
		if( typeof(this.form) == "string" )
			this.form = $(this.form);
		if( this.params && this.params.success )
			aJaxFormCBs.success = this.params.success;
		if( this.params && this.params.error )
			aJaxFormCBs.error = this.params.error;
		if( this.params && this.params.isPopup )
			aJaxFormCBs.success = popupClose;
		if( !this.params || (!this.params.success && !this.params.error && !this.params.isPopup) )
			aJaxFormCBsInit();
		
		this.form.submit();
	}
}
function popupClose(){
	$('popup_container').hide();
	$('popup_content').hide();
}
function popupShow(){
	$('popup_container').show();
	$('popup_content').show();
}

function inContainerAjax( link, element, params  ){
	this.link = link;
	this.params=params;
	this.element = element;
	this.load = function( params ){
		aJaxShowSpinner('popup_client');
		if( params )
			this.params= params;
		this.params.evalScripts=true;
		new Ajax.Updater( this.element, this.link, this.params );		    	
	}
}





function popupAjax( link, params, width, height, title ){
	this.link = link;
	this.params=params;
	this.width = width;
	this.height = height;
	this.title=title;
	this.load = function( params ){
		this.show();
		aJaxShowSpinner('popup_client');
		if( params )
			this.params= params;
		this.params.evalScripts=true;
		new Ajax.Updater( 'popup_client', this.link, this.params );		    	
	}
	this.show = function (){
		popupShow();
		var size=clientSize();
		$('popup_container').setStyle(
				{ width: size[0]+'px', 
				  height: getDocumentHeight() +'px' }	//height: (size[1] + document.body.cumulativeScrollOffset()[1] )+'px' }
		 		);
		var vOffset = 0;
		if ($('main_body') != undefined)
			vOffset = $('main_body').cumulativeScrollOffset()[1];
		
		var top = Math.round((size[1] -this.height)/2) + vOffset;
		var left = Math.round((size[0] -this.width)/2);
		if( top < 10 ) top =10;
		if( left < 10 ) left =10;
		
		$('popup_title_cont').innerHTML = this.title;
		
		$('popup_content').setStyle(
							{ top: top +'px' , 
		    				  left: left+'px',
		    		 		  width: this.width+'px', 
		    		 		  height: this.height+'px' }
		    		 		);
		$('popup_client').setStyle(
			 				{ width: this.width+'px', 
  		 		   			  height: (this.height-20)+'px' }
  		 		   			  );
	}
	this.hide = function(){
		popupClose();
	}
}

function getDocumentHeight()
{
    return (document.body.scrollHeight > document.body.offsetHeight)?document.body.scrollHeight:document.body.offsetHeight;
}

function aJaxShowSpinner(element){
	$(element).innerHTML = '<table><tr><td style="text-align:center; vertical-align:middle;">Loading...<br/><img src="/collaboration/images/spinner.gif" alt="Spinner" /></td></tr></table>';
}
function showImage( link, width, height, title ){
	var size=clientSize();
	var aspect = 1;
	if( width == 0 ) 
		width = Math.round((size[0]*3)/4);
	if( height == 0 ) 
		height = Math.round((size[1]*3)/4);
	
	if( width && width > size[0] && size[0] != 0 )
		aspect = width/size[0];
	if( height && height > size[1] && size[1] != 0 && aspect < width/size[1] )
		aspect = width/size[1];
	width = width/aspect;
	height = height/aspect;
	
	var pop = new popupAjax( link, {}, width, height+35, title);
	$('popup_client').innerHTML = '<div style="text-align: center; width; 100%;"><img height="'+ height+'" src="'+link+'"/></div>';
	pop.show();
}

function clientSize(){
	var WinHeight = 0;
	var WinWidth = 0;
	if( typeof( window.innerWidth ) == 'number' ){//Non-IE
		WinWidth = window.innerWidth;
		WinHeight = window.innerHeight;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {//IE
		WinWidth = document.documentElement.clientWidth;
		WinHeight = document.documentElement.clientHeight;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {//IE 4 compatible
		WinWidth = document.body.clientWidth;
		WinHeight = document.body.clientHeight;
	}
	return [ WinWidth, WinHeight];
}

function aJaxSubmit ( form ) {
	var params = {
			evalScripts : true,
			parameters : Form.serialize(form)
			};
	
	new Ajax.Updater( 'popup_client', form.action, params );	
	//var myAjax = new Ajax.Request( form.action, {method: 'get', parameters: Form.serialize(form), onLoading: showLoad, onComplete: showResponse} );
}
function showLoad(){
	$('popup_client').innerHTML="Submited!";
}
function showResponse (transport) {
	$('popup_client').innerHTML = transport.responseText;
}
function toggleElement(id, ctrl){
	var elem = document.getElementById(id);
	if ( !elem ) return;
	if ( !elem.style ) return; 
	if ( elem.style.display != 'block' ){
		elem.style.display='block';
		if( ctrl )
			ctrl.innerHTML = "[hide]";	
	}
	else{
		elem.style.display='none';
		if( ctrl )
			ctrl.innerHTML = "[show]";	
	}
}
// Function for Ajax 
function aJaxUpdateState( cntr, params ){
	new Ajax.Request( '/collaboration/'+cntr+'/updateState', {method: 'get', parameters: params} );
}

function reloadPage() {
	var url = window.location.href;
	if(url.indexOf('#') != -1)
            url = url.substring(0, url.indexOf('#'));
        window.location.href = url;
}