/**
 * Javascript functions for COSMOS
 * @ver 1.0
 * @author Razvan Cojocaru
 */

var popup_opened;
function openPopup( _url, _id, _title, _width )
{
	popup_opened = false;
	// create
	$.ajax({ url: _url , 
		async : false,
		success: function( data ){
		createPopup( data, _id, _title, _width );
      }});
      
}

function createPopup( div_content, div_id , div_title, div_width )
{
	var div_element = null;
	if( $("#"+div_id) ){
		$("#"+div_id).remove();
	}
	
	div_element = document.createElement('div');
	
	if( !div_width )
	{
		div_width = 400;
	}
	
	div_element.setAttribute('id', div_id);
	div_element.setAttribute('title', div_title);
//	div_element.setAttribute('class', 'modal');
	div_element.innerHTML = div_content;

	document.body.appendChild(div_element);

	$( "#" + div_id ).dialog({
		modal: true,
		autoOpen: true,
		width: div_width,
		//resizable: false,
		position: 'center',
		close: function(event, ui){
			//window.location.reload();
			popup_opened = false;
		}
	});
	popup_opened = true; 		
	
	div_element.setAttribute('style', div_element.getAttribute('style') + ';max-height:800px;overflow:auto;');
	popup_opened = true;
}

function changeHomepageHeader( _obj )
{
	var _parent = $(_obj).parent();
	$("#featured li").each( function(i, v){
		$(this).children('div').hide();
		$(this).children('h2').css({'font-size':'23px'});
		var old_text = $(this).children('h2').text();
		$(this).children('h2').html('+' + old_text.replace(/\++/,''));
	});
	
	var li_text = $(_obj).text();
	$(_obj).html(li_text.replace(/\++/,''));
	$(_obj).css({'font-size':'30px'});
	$(_parent).children('div').fadeIn('slow');
//	$(_parent).addClass('active');
}

/*
 * Replace the text onfocus/onblur events
 * @param string _id field id
 * @param string _text 
 * @param bool _mod
 */
function clearText( _id, _text, _mod ){
	var field = $('#' + _id);
	
	if( _mod == true && jQuery.trim(field.val()) == _text ){
		//onfocus event - clear text
		field.val('');
	}
	else if( _mod == false && jQuery.trim(field.val()) == '' ){
		field.val(_text);
	}
}

function openPrintPopup( data )
{
	var print_window = window.open('','print_window',
			  'width=500,height=450,left=10,top=10'
			   +',menubar=1'
			   +',toolbar=0'
			   +',status=1'
			   +',scrollbars=1'
			   +',resizable=1');
	   
	print_window.document.writeln(data);
	print_window.document.close();
	print_window.focus();
}

function openLogin() {
	destroyAllDialogs();
	$( "#login_form" ).dialog({
		modal: true,
		width: 400
		
	});
}

function openRecover()
{
	$( "#modal-forgot-password" ).dialog({
		height: 'auto',
		width: 475,
		modal: true,
		resizable: false,
		close : function(){ destroyAllDialogs(); }
	});
}

function destroyAllDialogs()
{
	$('.modal').dialog("destroy");
}



