/* JavaScript Copyright (C) 2006-2008 by Alexandru DIMA for Go4Celebrity.com. */

function escape_search_query(search_query) {
	var query = search_query;
	
	if(query === null) {
		return '';
	}
	
	query = query.toLowerCase();
	query = query.replace(/\'/gi, ' ');
	query = query.replace(/\"/gi, ' ');
	query = query.replace(/\./gi, ' ');
	query = query.replace(/,/gi, ' ');
	query = query.replace(/!/gi, ' ');
	query = query.replace(/;/gi, ' ');
	query = query.replace(/\n/gi, ' ');
	query = query.replace(/\t/gi, ' ');

	return query;
}

var searchresults = null;
var searchresults_has_focus = false;

function getSearchResults() {
	return document.getElementById('searchresults');
}

function hideSearchResults() {
	if(!searchresults) {
		searchresults = getSearchResults();
	}
	searchresults.style.display = 'none';
}

function setSearchResults(txt) {
	if(!searchresults) {
		searchresults = getSearchResults();
	}
	searchresults.innerHTML = txt;
}

function showSearchResults() {
	if(!searchresults) {
		searchresults = getSearchResults();
	}
	searchresults.style.display = 'block';
}

function focusSearchResults(search_query) {
	searchresults_has_focus = true;
	search_query_update(search_query);
}

function TOhideSearchResults() {
	if(searchresults_has_focus === false) {
		hideSearchResults();
	}
}

function blurSearchResults() {
	searchresults_has_focus = false;
	window.setTimeout("TOhideSearchResults();",300);
}

function search_query_update(search_query) {
	if(search_query === '' || search_query === null) {
		hideSearchResults();
		return;
	}
	search_query = escape_search_query(search_query);

	var search_words = search_query.split(' ');

	var celebrity_words = [];
	var celebrity_name, found, found_word, i, j, k;
	var txt = '<ul>\n';
	var found_at_least_one = false;
	
	for(i=0; i<C.length; i++)
	{
		celebrity_name = C[i][C_name];
		celebrity_words = (celebrity_name.toLowerCase()).split(' ');
		
		// check words so that all of them are at least the beginning
		found = true;
		for(j=0; found===true && j<search_words.length; j++) {
			found_word = false;
			for(k=0; found_word===false && k<celebrity_words.length; k++) {
				if(celebrity_words[k].indexOf(search_words[j]) === 0) {
					found_word = true;
				}
			}
			if(found_word === false) {
				found = false;
			}
		}
		
		if(found) {
			txt += '<li><a href="'+celebrity_url(celebrity_name)+'">'+celebrity_name+'</a></li>\n';
			found_at_least_one = true;
		}
	}
	
	txt += '</ul>\n';
	if(found_at_least_one === false) {
		txt = 'No results';
	}
	setSearchResults(txt);
	showSearchResults();
}

window.onload = function() { init(); };

var call_initI = false;

function init() {
	//try {
		//initC();
		initScrollLayers();
	//} catch(err) {}
	//try {
		if(call_initI) {
			initI();
		}
	//} catch(err) {}
}

function get_screen_width() {
	try {
		return screen.width;
	} catch(err) { return 1024; }
}

function get_screen_height() {
	try {
		return screen.height;
	} catch(err) { return 768; }
}

function image_random_src(celebrity_name) {
	return '/wallpapers/random/'+(celebrity_name.replace(/ /gi, '-'))+'.jpg';
}

function image_thumb_src(celebrity_directory, image_file) {
	return '/wallpapers/'+celebrity_directory+'/thumb/'+image_file;
}

function image_url(image_name) {
	return '/' + image_name.toLowerCase() + '.htm';
}

function celebrity_url(celebrity_name) {
	return '/' + (celebrity_name.replace(/ /gi, '-')).toLowerCase() + '.htm';
}

function rating(r, votes) {
	r = (parseInt(r*2+0.5,10))*5;
	var stars = ['empty', 'empty', 'empty', 'empty', 'empty'];
	
	var i = 0;
	while(r>0 && i<5) {
		if(r >= 10) {
			stars[i] = 'full';
			r-=10;
		} else if(r >= 5) {
			stars[i] = 'half';
			r-=5;
		}
		i++;
	}
	txt = '';
	for(i=0; i<5; i++) {
		txt += '<img src="/img/rating/star_' + stars[i] + '.gif" alt="" />';
	}
	return txt;
}

//////////////////////////
// ::Celebrity Browse:: //
//////////////////////////
function initC() {
	var txt = '';
	for(var i=0; i<C.length; i++) {
		txt += '<li onmouseover="show_categories_hover(\''+C[i][C_name]+'\');" onmouseout="hide_categories_hover();"><a href="'+celebrity_url(C[i][C_name])+'">'+C[i][C_name]+'</a> ('+C[i][C_entries]+')</li>';
	}
	//try {
		var e = document.getElementById('browsecategories');
		e.innerHTML = txt;
	//} catch(err) {}
	//setTimeout('initScrollLayers()', 100);
}

function initScrollLayers() {
	var wndo1 = new dw_scrollObj('browsewn', 'browselayer', null);
	wndo1.setUpScrollbar("browsedragbar", "browsetrack", "v", 1, 1);
	dw_showLayers("browsescrollbar");
}

function dw_showLayers() {
	if ( document.getElementById )
	{
		var lyr, i;
		for (i=0; arguments[i]; i++)
		{
			lyr = document.getElementById( arguments[i] );
			lyr.style.visibility = "visible";
		}
	}
}

///////////////////////////
// ::CELEBRITIES HOVER:: //
///////////////////////////

var is_shown = 0;
var time_hide = 1;
var countdown = 0;
var mouseY = 0;

function show_categories_hover(celebrity_name) {
	var extension = 'png';
	if (document.all) {
		extension = 'gif';
	}

	show_hover();
	
	var txt = '';
	

	var get_random = name.replace(/ /gi, '-');

	document.getElementById('celebrityhover').innerHTML =
'<a href="'+celebrity_url(celebrity_name)+'"><img src="'+image_random_src(celebrity_name)+'" alt="'+celebrity_name+' random image" /></a>';
	is_over = 1;
	countdown = 0;
}

function hide_categories_hover() {
	countdown = 1;
	time_hide = new Date();
	time_hide = time_hide.valueOf() + 500;
}

if (!document.all) {
	document.captureEvents(Event.MOUSEMOVE);
}
document.onmousemove = getMouseXY;

var celebrityhover = 0;

function show_hover() {
	if(!celebrityhover)	{
		celebrityhover = getcelebrityhover();
	}
	celebrityhover.style.display = 'block';
	update_hover();
	is_shown = 1;
}

function update_hover() {
	if(!celebrityhover)	{
		celebrityhover = getcelebrityhover();
	}
	celebrityhover.style.top = (mouseY-30) + 'px';
}

function hide_hover() {
	if(!celebrityhover)	{
		celebrityhover = getcelebrityhover();
	}
	celebrityhover.style.display = 'none';
	is_shown = 0;
}

function getcelebrityhover() {
	return document.getElementById('celebrityhover');
}

function getMouseXY(e)
{
	if(is_shown) {
		if(countdown == 1) {
			var temp_date = new Date();
			temp_date = temp_date.valueOf();
			if(temp_date > time_hide) {
				countdown = 0;
				hide_hover();
			}
		} else {
			update_hover();
		}
	}
	if (!e) {
		e = window.event;
	}
	if (document.all) {
		mouseY = event.clientY + document.documentElement.scrollTop;
	} else {
		mouseY = e.pageY;
	}
	if (mouseY < 0) { mouseY = 0;} 
	return true;
}
//////////////////////////////
// ::END Celebrity Browse:: //
//////////////////////////////