/*
	Global variables
*/
var _APP_URL = 'http://my-site.ro'

/*
	Login function
*/
function login(){
	/*
		Sterge ce exista deja
	*/
	$('#auth_container').hide('slow');
	
	/*
		Incarca pagina de login
	*/
	var url = '/auth/login' ;
	$.get(url, {}, function(data){	
		$('#auth_container').html(data).show('slow');		
	});
}

function register(){
	$('#auth_container').hide('slow');
	
	var url = '/auth/register' ;
	$.get(url, {}, function(data){
		$('#auth_container').html(data).show('slow');
	});
		
}

function forgotPassword(){
	$('#auth_container').hide('slow');
	
	var url = '/auth/forgot-password' ;
	$.get(url, {}, function(data){
		$('#auth_container').html(data).show('slow');
	});
		
}

function close(){

	$('#auth_container').hide('slow');
	
}

/*
	Ajax loader
*/
var ignore_ajax_loader = 0;

function addAjaxLoader(){

	$('body').append('<div id="ajax-loader"><img src="/images/ajax_loader.gif" border="0" align="absmiddle" /><span>Va rugam asteptati ...</span></div>');
	
}

$(document).ready(function(){
	addAjaxLoader();
	
	if(ignore_ajax_loader == 0){
	
		$('#ajax-loader').ajaxStart(function(){
			$(this).show();
		});
		$('#ajax-loader').ajaxStop(function(){
			$(this).hide();
		});		
	}
	
});

/*
	Search functionality
*/

$(document).ready(function(){
	
	$('#search_text').click(function(){
		if($('#search_text').val() == 'Cauta in site'){
			$('#search_text').val('');
		}
	});
	
	$('#search_button').click(function(){
		/*
			Do search action
		*/
	});
	
});

var recentSearchesFirstLoad = 1;
var recent_searches_timer;

function showRecentSearches(){
	var url = '/search/recent-searches';
	
	if(recentSearchesFirstLoad == 1){
		$.get(url, {}, function(data){
			if(data.length > 0){
				recentSearchesFirstLoad = 0;
				$('#recent-searches').html(data).slideDown('slow');				
			}
		});		
	}
	else{
		$('#recent-searches').slideDown('slow');
	}
	
	recent_searches_timer = setTimeout('closeRecentSearchesBox()', 2000);
		
}

function closeRecentSearchesBox(){
	$('#recent-searches').slideUp('slow');
}

$(document).ready(function(){		
	
	$('#recent-searches').hover(
			function(){
				clearTimeout(recent_searches_timer);
			},
			function(){
				recent_searches_timer = setTimeout('closeRecentSearchesBox()', 2000);
			}
	);
	
});

/* 
	Messenger actions
*/

function hideMessenger(objId){
	
	$('div#' + objId).hide('slow');
	
}

/*
	Top menu hover functionality
*/

$(document).ready(function(){
	
	$('ul.top_menu_list li').not('.selected').not('.first').not('.ignore').hover(
		function(){
			$(this).addClass('selected');
		},
		function(){
			$(this).removeClass('selected');
		}
	);
	
});

/*
 * Safe Route
 */
var replace_char = '_';	

function safeRoute(basePath, paramName, paramId){
	
	if(basePath && paramName){
		
		return '/' + basePath + '-' + paramName.replace(/[^a-zA-Z0-9]/gi, replace_char) + (paramId?'-' + paramId:'');
		
	}
	else{
		
		return '/';
		
	}
	
}

function search(){
	
	document.location = '/cauta-in-site?search=' + URLEncode($('#search_text').attr('value'));
	
}

/*
	Other functionalities
*/
function URLEncode(mystr) {
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" + // Numeric
	"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
	"abcdefghijklmnopqrstuvwxyz" +
	"-_.!~*'()"; // RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";
	
	var plaintext = mystr;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
		if (ch == " ") {
			encoded += "+"; // x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
			encoded += ch;
		} else {
			var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	return encoded;
}	

