var uid = 'uidcombo';
var images_path = 'http://weather.digitair.ro/digiweather/digistation_flash/images/';
var base_path = 'http://weather.digitair.ro/digiweather/digistation_flash/'

function setLocalitate(loc, is_icao, lang, div_id)
{
	if(lang=='EN')
		text_settings = "Change city here";
	else
		text_settings = "Alege localitatea";
	var newHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="244" height="254" align="middle"><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="false" /><param name="movie" value="http://weather.digitair.ro/digiweather/digistation_flash/DigiStation.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><param name="wmode" value="transparent" /><param name="FlashVars" value="localitate='+loc+'&is_icao='+is_icao+'&lang='+lang+'&refresh_interval=5&cookie_uid='+uid+'" /><embed src="http://weather.digitair.ro/digiweather/digistation_flash/DigiStation.swf" quality="high" bgcolor="#ffffff" width="244" height="254" name="banner" align="middle" FlashVars="localitate='+loc+'&is_icao='+is_icao+'&limba='+lang+'&refresh_interval=5&cookie_uid='+uid+'" allowScriptAccess="always" allowFullScreen="false" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
	var comboHTML = '<link rel="stylesheet" type="text/css" href="'+base_path+'settings.css" /><br/><div style="text-align: right; display: block; width: 244px"><a href="#" rel="toggle[digistation_settings]" data-openimage="'+images_path+'settings_expanded.png" data-closedimage="'+images_path+'settings_collapsed.png"><div style="color: #505050; text-align: center; float: left; display: block; width: 169px; margin-left: 40px; font-family: Arial, Verdana; font-size: 12px; font-weight: bold; text-decoration: underline;">'+text_settings+'</div> <img src="'+images_path+'settings_collapsed.png" border="0" /></a></div><div id="digistation_settings"><img src="'+images_path+'search_bk.png" name="img_background" id="img_background" /><a href="javascript:btn_search_press()"><img src="'+images_path+'btn_search.png" name="img_search" id="img_search" /></a><a href="javascript:btn_combo_press()"><img src="'+images_path+'btn_combo.png" name="img_combo" id="img_combo" /></a><a href="javascript:btn_ok_press()"><img src="'+images_path+'ok.png" name="img_ok" id="img_ok" /></a><a href="javascript:btn_cancel_press()"><img src="'+images_path+'cancel.png" name="img_cancel" id="img_cancel" /></a><div id="title_search"></div><div id="title_result"></div><input type="text" name="txt_search" id="txt_search" onkeydown="if (event.keyCode == 13) btn_search_press();"/><input name="txt_results" type="text" id="txt_results" readonly="readonly" /><div id="menu"></div></div>';
	document.getElementById(div_id).innerHTML = newHTML+comboHTML;
	document.cookie = "CurrentLoc=" + escape(loc);
	document.cookie = "CurrentIsIcao=" + is_icao;
	document.cookie = "CurrentLang=" + lang;

	animatedcollapse.addDiv('digistation_settings', 'fade=1,height=200px,hide=1');
	animatedcollapse.init();

	if(lang=='RO')
		document.getElementById('title_search').innerHTML = 'Cauta oras:';
	else
		document.getElementById('title_search').innerHTML = 'Search city:';
}

function getCookieVal(c_name)
{
	if (document.cookie.length>0) {
		var c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			var c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) 
				c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function initDigiStation()
{
	var loc=getCookieVal("CurrentLoc");
	var is_icao=getCookieVal("CurrentIsIcao");
	if(!loc) {
		loc='Buc Centru';
		is_icao='false';
	}
	if(!lang)
		lang=getCookieVal("CurrentLang");
	if(!lang)
		lang='RO';
	setLocalitate(loc, is_icao, lang, 'digistation');
}

window.onload = initDigiStation;


// ------------------------- SETTINGS -------------------------

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function getXMLHttpRequest() 
{
	if (window.XMLHttpRequest) {
		return new window.XMLHttpRequest;
	}
	else {
		try {
			return new ActiveXObject("MSXML2.XMLHTTP.3.0");
		}
		catch(ex) {
			return null;
		}
	}
}

function searchLoc(loc)
{
	if(lang=='RO')
		document.getElementById('title_result').innerHTML = 'Caut...';
	else
		document.getElementById('title_result').innerHTML = 'Searching...';
	document.getElementById('menu').innerHTML='';
	document.getElementById('menu').visibility='hidden';
	document.getElementById('txt_results').value='';

	$.getJSON("http://weather.digitair.ro/digiweather/digistation_flash/flash_get_search.php?city="+loc+"&callback=?", function(vals) {
		for(var i in vals) {
			add_element(vals[i]['station']+", "+vals[i]['country'], vals[i]['icao'], vals[i]['cc']);
			if(i==0) {
				selected_icao=vals[i]['icao'];
				selected_cc=vals[i]['cc'];
				document.getElementById('txt_results').value=vals[i]['station']+", "+vals[i]['country'];
			}
		}
		var menu=document.getElementById('menu');
		if(menu.childNodes.length==0) {
			if(lang=='RO')
				document.getElementById('title_result').innerHTML = 'Orasul nu a fost gasit!';
			else
				document.getElementById('title_result').innerHTML = 'City not found!';
			return;
		}
		menu.style.top = "47px";
		menu.style.visibility="visible";
		
		if(lang=='RO')
			document.getElementById('title_result').innerHTML = 'Rezultate:';
		else
			document.getElementById('title_result').innerHTML = 'Results:';
	} );
}

function btn_ok_press() {
	if(!lang)
		lang='RO';
	if(selected_cc=='RO')
		setLocalitate(selected_icao, 'false', lang, 'digistation');
	else
		setLocalitate(selected_icao, 'true', lang, 'digistation');
}

function btn_cancel_press() {
	var menu=document.getElementById('menu');
	menu.innerHTML='';
	document.getElementById('txt_results').value='';
	document.getElementById('txt_search').value='';
	animatedcollapse.hide('digistation_settings');
}

function btn_search_press() {
	var srcText=document.getElementById('txt_search').value;
	if(!srcText.trim()) {
		if(lang=='RO')
			alert('Introduceti numele unei localitati!');
		else
			alert('Enter a city name first!');
		return;
	}
	searchLoc(srcText.trim());
}

function btn_combo_press() {
	var menu=document.getElementById('menu');
	if(menu.childNodes.length==0)
		return;
	menu.style.top = "109px";
	if(menu.style.visibility=="visible")
		menu.style.visibility="hidden";
	else
		menu.style.visibility="visible";
}

function add_element(text, value, cc) {
	var menu=document.getElementById('menu');
	var newdiv = document.createElement('a');
	newdiv.setAttribute('id', 'menu_'+value);
	newdiv.onclick=function() { menuClick(value, text, cc) };
	newdiv.className='menu_item';
	newdiv.innerHTML = text;
	//newdiv.href="#";
	menu.appendChild(newdiv);
}

function menuClick(value, title, cc) {
	var menu=document.getElementById('menu');
	menu.style.visibility='hidden';
	
	selected_icao=value;
	selected_cc=cc;
	document.getElementById('txt_results').value=title;
}

var selected_icao='';
var selected_cc='';

