var oXML = null;
var HDUser = parseInt(getCookie("SabahUserID"));
var SabahUserID = HDUser;
var HDCity = parseInt(getCookie("SabahHavaSehir"));
var HDCityReported = parseInt(getCookie("SabahHavaSehirReported"));
var HDCities = [];
var HDCityIDs = [];
var arrHDIconUfak = [];

if (isNaN(HDUser)) HDUser = 0;
// default to city Istanbul
if (isNaN(HDCity)) HDCity = 0;
if (isNaN(HDCityReported)) HDCityReported = -1;

function HDProcessChange( responseText ) {
	user_id = parseInt(responseText);
	if (!isNaN(user_id)) {
		SabahUserID = HDUser = user_id;
		var date = new Date(2050,0,1);
		setCookie("SabahHavaSehirReported", HDCityReported, date.toGMTString());
		setCookie("SabahUserID", user_id, date.toGMTString());
	}
}

function reportHDCity(city_id, force) {
	force = force != null && force;

	// report city if not reported yet or forced to report
	if (force || HDCityReported == -1) {
		HDCityReported = city_id;
		var url = "/tools/hava_city_change.php" +
				   "?user_id=" + HDUser +
				   "&city_id=" + HDCityReported + 
				   "&scrw=" + screen.width + 
				   "&scrh=" + screen.height;
		MBGAjax(url, HDProcessChange);
	}
}

function defHDCity(city_id, city_name) {
	HDCities[city_id] = city_name;
	HDCityIDs.push(city_id);
}

function setInnerText(id, text) {
	var tn = document.createTextNode(text);
	var el = document.getElementById(id);
	if (el.firstChild == null)
		el.appendChild(tn);
	else
		el.replaceChild(tn, el.firstChild);
}

function getHDIconUfak(hd_name) {
	var iconURL = I_URL + "hava/meteor_" + hd_name + "_k.gif";
	if (arrHDIconUfak[hd_name] == null) {
		arrHDIconUfak[hd_name] = new Image();
		arrHDIconUfak[hd_name].src = iconURL;
	}
	return iconURL;
}

function setHDCity(city_id, save) {
	if (typeof(hd) == "undefined") return;
	
	var lnk = document.getElementById("lnkHDSehir");
	var icon = document.getElementById("topHDIconImg");
	
	if (city_id == 0) {
		setInnerText("lnkHDSehir", "Hava durumu için şehir seçiniz...");
		lnk.href = "javascript:toggleHDCityPanel();";
		icon.style.visibility = "hidden";
	} else {
		var HDTip = hd[city_id];
		var HDTipBilgi = hd_types[HDTip[1]];
		var caption = HDTip[0] + " " + HDTip[3] + "°C" + " / " + HDTip[2] + "°C, " + HDTipBilgi[1];
	
		setInnerText("lnkHDSehir", caption);
		lnk.href = "http://www.sabah.com.tr/hava," + city_id + ".html";
	
		icon.style.visibility = "visible";
		icon.alt = HDTipBilgi[1];
		icon.title = HDTipBilgi[1];
		icon.src = getHDIconUfak(HDTipBilgi[0]);	
	}
	
	if (save) {
		HDCity = city_id;

		// report to server if there is a change
		reportHDCity(HDCity, true);
		
		var date = new Date(2050,0,1);
		setCookie("SabahHavaSehir", HDCity, date.toGMTString());
	}
}

function toggleHDCityPanel() {
	var lnk = document.getElementById("lnkHDSehir");
	lnk.blur();	
	var pnl = document.getElementById("topHDCityPanel");
	visible = pnl.style.display == "none";
	
	if (visible)
		showHDCityPanel(true);
	else
		cancelHDChange();
}

function showHDCityPanel(visible) {
	var pnl = document.getElementById("topHDCityPanel");
	var lstYazar = document.getElementById("yazar_combo");
	pnl.style.display = visible ? "block" : "none";
	if (lstYazar != null)
		lstYazar.style.display = visible ? "none" : "block";
}

function initHDCity(new_city_id) {
	setHDCity(new_city_id);

	var lst = document.getElementById("topHDCityList");
	if (lst.options.length == 1)
		lst.remove(0);
		
	// listeyi doldur
	for (var city_index in HDCityIDs) {
		var city_id = HDCityIDs[city_index];
		var opt = document.createElement("OPTION");		
		var text = HDCities[city_id];
		if (text == "") {
			opt.text = "--------------------";
			opt.value = "";
		} else {
			opt.text = text;
			opt.value = city_id;
		}
		lst.options.add(opt);
	}
	lst.value = new_city_id;
	
	lst.onchange = function() {
		// if selected item is not a seperator
		if (this.value != "") {
			setHDCity(this.value, false);
		}
	}
	
	// report city if not reported yet
	// reportHDCity(new_city_id);
	
	// lst.ondblclick = confirmHDChange;
}

function confirmHDChange() {
	var lst = document.getElementById("topHDCityList");
	if (lst.selectedIndex == -1) {
		alert("Lütfen listeden şehrinizi seçiniz!");
		return;
	}

	// if selected item is not a seperator
	if (lst.value != "") {
		setHDCity(lst.value, true);
	} else {
		// revert selection
		lst.value = HDCity;
		setHDCity(HDCity);
	}
	showHDCityPanel(false);
}

function cancelHDChange() {
	var lst = document.getElementById("topHDCityList");

	// revert selection
	lst.value = HDCity;
	setHDCity(HDCity);
	showHDCityPanel(false);
}

defHDCity(1,"İSTANBUL");
defHDCity(48,"ANKARA");
defHDCity(32,"İZMİR");
defHDCity(40,"ADANA");
defHDCity(81,"ADIYAMAN");
defHDCity(37,"AFYON");
defHDCity(60,"AKSARAY");
defHDCity(23,"AMASYA");
defHDCity(44,"ANTALYA");
defHDCity(65,"ARDAHAN");
defHDCity(31,"ARTVİN");
defHDCity(34,"AYDIN");
defHDCity(63,"AĞRI");
defHDCity(9,"BALIKESİR");
defHDCity(18,"BARTIN");
defHDCity(82,"BATMAN");
defHDCity(30,"BAYBURT");
defHDCity(14,"BOLU");
defHDCity(13,"BOZCAADA");
defHDCity(46,"BURDUR");
defHDCity(10,"BURSA");
defHDCity(8,"BİLECİK");
defHDCity(70,"BİNGÖL");
defHDCity(71,"BİTLİS");
defHDCity(7,"ÇANAKKALE");
defHDCity(49,"ÇANKIRI");
defHDCity(22,"ÇORUM");
defHDCity(36,"DENİZLİ");
defHDCity(15,"DÜZCE");
defHDCity(76,"DİYARBAKIR");
defHDCity(2,"EDİRNE");
defHDCity(68,"ELAZIĞ");
defHDCity(61,"ERZURUM");
defHDCity(66,"ERZİNCAN");
defHDCity(50,"ESKİŞEHİR");
defHDCity(77,"GAZİANTEP");
defHDCity(12,"GÖKÇEADA");
defHDCity(29,"GÜMÜŞHANE");
defHDCity(26,"GİRESUN");
defHDCity(74,"HAKKARİ");
defHDCity(41,"HATAY");
defHDCity(45,"ISPARTA");
defHDCity(64,"IĞDIR");
defHDCity(43,"K.MARAŞ");
defHDCity(19,"KARABÜK");
defHDCity(59,"KARAMAN");
defHDCity(62,"KARS");
defHDCity(16,"KASTAMONU");
defHDCity(54,"KAYSERİ");
defHDCity(58,"KIRIKKALE");
defHDCity(4,"KIRKLARELİ");
defHDCity(55,"KIRŞEHİR");
defHDCity(5,"KOCAELİ");
defHDCity(51,"KONYA");
defHDCity(39,"KÜTAHYA");
defHDCity(83,"KİLİS");
defHDCity(67,"MALATYA");
defHDCity(33,"MANİSA");
defHDCity(78,"MARDİN");
defHDCity(42,"MERSİN");
defHDCity(35,"MUĞLA");
defHDCity(72,"MUŞ");
defHDCity(56,"NEVŞEHİR");
defHDCity(57,"NİĞDE");
defHDCity(25,"ORDU");
defHDCity(47,"OSMANİYE");
defHDCity(28,"RİZE");
defHDCity(6,"SAKARYA");
defHDCity(21,"SAMSUN");
defHDCity(20,"SİNOP");
defHDCity(53,"SİVAS");
defHDCity(79,"SİİRT");
defHDCity(80,"ŞANLIURFA");
defHDCity(75,"ŞIRNAK");
defHDCity(3,"TEKİRDAĞ");
defHDCity(24,"TOKAT");
defHDCity(27,"TRABZON");
defHDCity(69,"TUNCELİ");
defHDCity(38,"UŞAK");
defHDCity(73,"VAN");
defHDCity(11,"YALOVA");
defHDCity(52,"YOZGAT");
defHDCity(17,"ZONGULDAK");
defHDCity("sep1","");
defHDCity(89,"AMSTERDAM");
defHDCity(108,"ATİNA");
defHDCity(104,"BELGRAD");
defHDCity(94,"BERLİN");
defHDCity(92,"BONN");
defHDCity(90,"BRÜKSEL");
defHDCity(98,"BUDAPEŞTE");
defHDCity(131,"BÜKREŞ");
defHDCity(93,"FRANKFURT");
defHDCity(133,"GÜMÜLCİNE");
defHDCity(117,"GİRNE");
defHDCity(85,"HELSİNKİ");
defHDCity(129,"KIRCAALİ");
defHDCity(88,"KOPENHAG");
defHDCity(96,"KÖLN");
defHDCity(132,"KÖSTENCE");
defHDCity(136,"KİEV");
defHDCity(118,"LEFKOŞE");
defHDCity(87,"LONDRA");
defHDCity(102,"MADRİD");
defHDCity(137,"MINSK");
defHDCity(106,"MOSKOVA");
defHDCity(95,"MÜNİH");
defHDCity(84,"OSLO");
defHDCity(91,"PARİS");
defHDCity(97,"PRAG");
defHDCity(107,"ROMA");
defHDCity(134,"SARAYBOSNA");
defHDCity(126,"SELANİK");
defHDCity(105,"SOFYA");
defHDCity(86,"STOCKHOLM");
defHDCity(130,"TİRAN");
defHDCity(127,"ÜSKÜP");
defHDCity(99,"VARŞOVA");
defHDCity(101,"VİYANA");
defHDCity(100,"ZÜRİH");
defHDCity("sep2","");
defHDCity(112,"AMMAN");
defHDCity(123,"ASTANA");
defHDCity(119,"AŞKABAT");
defHDCity(124,"BAKÜ");
defHDCity(114,"BAĞDAT");
defHDCity(120,"BİŞKEK");
defHDCity(122,"DUŞANBE");
defHDCity(143,"KABİL");
defHDCity(116,"KUVEYT");
defHDCity(111,"MEDİNE");
defHDCity(110,"MEKKE");
defHDCity(115,"RİYAD");
defHDCity(113,"ŞAM");
defHDCity(142,"TAHRAN");
defHDCity(121,"TAŞKENT");
defHDCity(145,"TOKYO");
defHDCity(125,"TİFLİS");
