/**
 * Collapses/Expands a box
 *
 * @param event e; click event
 * @param object obj; the box dom object
 * @param string template; the website template skin (e.g. standard)
 * @param string box_id
 * @return void
 */
function toggleBox(e, obj, template, box_id){
	e = e ? e : window.event;
	if (e.target){
		targ = e.target;
	} else if (e.srcElement){
		targ = e.srcElement;
	}
	if (targ.nodeType == 3) { // defeat Safari bug
		targ = targ.parentNode;
	}

	if(targ.tagName == 'A'){
		return;
	}

	while(!obj.className.match("box")){
		obj = obj.parentNode;
	}
	if(obj.className.match("box")){
		var bx = new Box(obj, template);
		bx.toggle(box_id);
	}
}

/**
 * Builds the search url in the required format based on form input, and redirects user to that url
 *
 * @param object fid; the id of the form
 * @return void
 */
function search(fid){
	var search_val = escape(document.getElementById(fid).value);
	document.location.href = "/cautare/" + search_val;
}

/**
 * Checks if the key pressed in an input field is the enter key and if so calls the search function
 *
 * @param event e; the key event
 * @param string fid; the id of the html input field
 * @return void
 */
function enter(e, fid){
	e = e ? e : window.event;
	if(e.keyCode){
		code = e.keyCode;
	}else if(e.which){
		code = e.which;
	}
	if(code == 13){
		search(fid);
	}
}

/**
 * Loads the output of some url in a dom object
 *
 * @param string url;
 * @param string target; the id of the dom object
 * @param string ajaxError; the id of the dom object thet will show the error
 * @param string ajaxLoader; the id of the dom object that will act as a preloader
 * @return void
 */
function loadUrl(url, target, ajaxError, ajaxLoader){
	Ajax.request(url, null, null, null, "populateDOM('" + target + "', result);", null, null, ajaxError, ajaxLoader);
}

/**
 * Populates a dom object with some content
 *
 * @param string obj_id; the id of the dom object
 * @param string result; the content
 * @return void
 */
function populateDOM(obj_id, result){
	document.getElementById(obj_id).innerHTML = result;
}

/**
 * Executes on windows load
 *
 * @return void
 */
jQuery(document).ready(
	function(){
		slideShow('slide', '/slideshow', 1, 'slideShowAjaxLoader');
		Tab = new Tab('tabs', 'divs');
		jQuery("#divs").height(jQuery("#divs").height());
		for (var i=0; i<document.images.length; i++) {
			document.images[i].oncontextmenu = function (){ return false;};
        }
	}
);

var slide_show_speed = 5000;
var slide_show_timer;
var slide_show_photos = new Array();
var slide_pages = new Array();
var slide_show_static = false;
var slide_show_max = 1;

/**
 * Advances to the next image in the header slide
 *
 * @param string target; the id of the dom object that holds the image
 * @param string url;
 * @param integer image; the image number
 * @param string ajaxLoader; the id of the dom object that will act as a preloader
 * @return void
 */
function slideShow(target, url, image, ajaxLoader){		
	if(!inArray(image, slide_pages)){
		Ajax.request(url + "/" + image, null, null, null, "nextPhoto('" + target + "', result, '" + url + "', " + image + ", '" + ajaxLoader + "');", null, null, null, ajaxLoader);
	} else {
		staticSlideShow(target, url, image, ajaxLoader);
	}
}

/**
 * Displays the next slideshow photo
 *
 * @param string target; the id of the dom object
 * @param string result; the content
 * @param string url;
 * @param integer image; the image number
 * @param string ajaxLoader; the id of the dom object that will act as a preloader
 * @return void
 */
function nextPhoto(target, result, url, image, ajaxLoader){
	//ajaxLoader = null;
	if(result){
		var pieces = result.split("|");
		document.getElementById(target).src = pieces[1];
		slide_show_photos[slide_show_photos.length] = pieces[1];
		if(!inArray(image, slide_pages)){
			slide_pages[slide_pages.length] = image;
		}
		slidePage(parseInt(pieces[0]));	
		if(image > slide_show_max){
			slide_show_max = image;
		}
		slide_show_timer = setTimeout("slideShow('" + target + "', '" + url + "', " + ++image + ", '" + ajaxLoader + "')", slide_show_speed);
	} else {	
		slideShow(target, url, 1, ajaxLoader);
	}
	
}

/**
 * Advances to the next image in the header slide staticaly after the images have allready been preloaded
 *
 * @param string target; the id of the dom object that holds the image
 * @param string url;
 * @param integer image; the image number
 * @param string ajaxLoader; the id of the dom object that will act as a preloader
 * @return void
 */
function staticSlideShow(target, url, image, ajaxLoader){
	if(image > slide_show_max){
		image = 1;
	}
	document.getElementById(target).src = slide_show_photos[image - 1];
	slidePage(image);
	//var func = ajax ? "slideShow" : "staticSlideShow";
	//slide_show_timer = setTimeout(func + "('" + target + "', " + ++image + ")", slide_show_speed);
	slide_show_timer = setTimeout("slideShow('" + target + "', '" + url + "', " + ++image + ", '" + ajaxLoader + "')", slide_show_speed);
}

/**
 * Advances to a custom image in the header slide
 *
 * @param object obj; the current dom object
 * @param string target; the id of the dom object that holds the image
 * @param string url;
 * @param integer image; the image number
 * @param string ajaxLoader; the id of the dom object that will act as a preloader
 * @return void
 */
function customSlide(obj, target, url, image, ajaxLoader){
	if(obj.className != 'active'){
		clearTimeout(slide_show_timer);
		slideShow(target, url, image, ajaxLoader);
	}
}


/**
 * Displays the next slideshow number
 *
 * @param integer page
 * @return void
 */
function slidePage(page){
	var obj = document.getElementById('slideshow');
	if(obj){
		var a = obj.getElementsByTagName("A");
		for(i=0; i<a.length; i++){
			a[i].className = (i+1) == page ? 'active' : '';
		}
	}
}

/**
 * Checks if the key pressed in an input field is the enter key and if so calls the login function
 *
 * @param event e; the key event
 * @param string url;
 * @param string usr; username
 * @param string pwd; password
 * @param string target; target dom object
 * @param string uri; url to redirect to in case of success
 * @return void
 */
function enterLogin(e, url, usr, pwd, target, uri){
	e = e ? e : window.event;
	if(e.keyCode){
		code = e.keyCode;
	}else if(e.which){
		code = e.which;
	}
	if(code == 13){
		login(url, usr, pwd, target, uri);
	}
}

/**
 * Fires the login action to check the credentials
 *
 * @param string url;
 * @param string usr; username
 * @param string pwd; password
 * @param string target; target dom object
 * @param string uri; url to redirect to in case of success
 * @return void
 */
function login(url, usr, pwd, target, uri){
	jQuery("#loginAjaxLoader").css('display', 'block');
	var params = {
		username: document.getElementById(usr).value,
		password: document.getElementById(pwd).value
	};
	Ajax.request(url, params, 'POST', null, "processLogin('" + target + "', result, '" + uri + "');");
}

/**
 * Populates the login target with the result
 *
 * @param string obj_id; the id of the dom object
 * @param string result; the content
 * @param string uri; url to redirect to in case of success
 * @return void
 */
function processLogin(obj_id, result, uri){
	document.getElementById(obj_id).innerHTML = result;
	var ls = document.getElementById("login_success");
	if(ls){
		if(ls.value){
			var ok = true
			document.location.href = uri;
			return;
		}
	}
	var f = document.getElementById("usr");
	if(f){
		f.focus();
	}
	jQuery("#loginAjaxLoader").css('display', 'none');
	setTimeout(clearLoginMessage, 2000);
}

/**
 * Clears the error on login
 *
 * @return void
 */
function clearLoginMessage(){
	var obj = document.getElementById("login_error");
	if(obj){
		obj.innerHTML = "";
	}
}

/**
 * Sets cookie
 *
 * @param string name
 * @param string value
 * @param integer expires
 * @param string path
 * @param string domain
 * @param boolean secure
 * @return void
 */
function Set_Cookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ){
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

/**
 * Deletes cookie
 *
 * @param string name
 * @param string path
 * @param string domain
 * @return void
 */
function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

/**
 * Returns cookie value
 *
 * @return void
 */
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

/***** CHANGE STYLESHEETS *****/

function setcookie( name, value, expiry, path ) {
	if(expiry) {
		var now = new Date();
		now.setTime( now.getTime() + Math.round(86400000*expiry) );
		expiry = now.toGMTString();
	}
	expiry = expiry ? '; expires=' + expiry : '';
	path = path ?'; path=' + path:'';
	document.cookie = name + '=' + escape(value) + expiry + path;
}

function getcookie( name ) {
	var cookie = document.cookie;
	if( cookie.indexOf( name + '=' ) < 0 ) {
		return null;
	}
	var start = cookie.indexOf( name + '=' ) + name.length + 1;
	var finish = cookie.substring( start, cookie.length );
	finish = ( finish.indexOf( ';' ) < 0 ) ? cookie.length : start + finish.indexOf( ';' );
	return unescape( cookie.substring( start, finish ) );
}

function setsheet( name ) {
	var l = document.getElementsByTagName( 'link' ), i, o, t;
	for( i=0; (o = l[i]); i++ ) {
		if( 'alternate stylesheet'===o.getAttribute( 'rel' ) && ( t = o.getAttribute( 'title' ) ) ) {
			o.disabled = true; // browser bug: will not enable on next line otherwise
			o.disabled = ( t !== name );
		}
	}
}

function setstyle( name ) {
	var name;
	setcookie( 'style', name, 90, '/' );
	setsheet( name );
}
  // do this before the <body> opening tag and the user will see no flickering
if( ( name = getcookie( 'style' ) ) ) {
	setsheet( name );
}

var menu_timer;
var wait = 1000;

function menuTimer(obj, ul_id, url, nm){
	if(jQuery("#" + obj.id + " A").hasClass("active")){
		return;
	}
	resetTimer();
	menu_timer = setTimeout("menuAction('" + obj.id + "', '" + ul_id + "', '" + url + "', '" + nm + "')", wait);
}

function contentTimer(obj, nm){
	if(jQuery("#" + obj.id + " A").hasClass("active")){
		return;
	}
	resetTimer();
	menu_timer = setTimeout("menuAction('" + obj.id + "', null, null, '" + nm + "', true)", wait);
}

function resetTimer(){
	clearTimeout(menu_timer);
}

function menuAction(obj_id, ul_id, url, nm, sub){
	jQuery("#v_menu_parent .level1 A").removeClass("active");
	if(!sub){
		jQuery(".submenu").css('display', 'none');
		jQuery("#" + ul_id).css('display', 'block');
		jQuery("#" + obj_id + " A").addClass("active");
		jQuery("#" + obj_id + " UL LI A").removeClass("active");
	} else {
		jQuery(".level2").removeClass("active");
		jQuery("#" + obj_id + " A").addClass("active");
	}
	if(document.getElementById(ul_id) && !jQuery("#" + ul_id).hasClass("loaded")){
		Ajax.request(url + nm, null, null, null, "populateMenu('" + ul_id + "', result);", null, null, null, null);
	}
	Ajax.request("/a/" + nm, null, null, null, "populateDOM('middle', result);", null, null, null, 'ajaxContentLoader');
}

/**
 * Populates a submenu ul object with some content and marks it as loaded
 *
 * @param string obj_id; the id of the dom object
 * @param string result; the content
 * @return void
 */
function populateMenu(obj_id, result){
	var o = document.getElementById(obj_id);
	if(o){
		o.innerHTML = result;
	}
	jQuery("#" + obj_id).addClass("loaded");
}

/**
 * Checks if a value is in an array
 *
 * @param mixed val; the value to search for
 * @param array arr; the array to search in
 * @return boolean
 */
function inArray(val, arr){
	for(var i=0; i<arr.length; i++){
		if(arr[i] == val){
			return true;
		}
	}
	return false;
}