/* Globals (leider)*/

var gAktBC = undefined;
var gNewBC = undefined;
var gZoom = 0;
var gKont = 0;

/* Länderarray (Objekte)*/
landListe = new Array();


/* Tarifojekt Konstruktor*/
function Rate(id,name,label,price,interval) {
	this.id = id;
	this.name = name;
	this.label = label;
	this.price = price;	
	this.interval = interval;	
}




/* Landobjekt Konstruktor*/
function landOb(id,name,rate) {
	this.id = id;
	this.name = name;
	this.submitValue = id;

	var bcdata = window.bcdata;
	var data = bcdata.countries[id];
	if (!data) 
		alert('no country for ' + id);
	
	this.kooWelt = data.world;
	this.kooKont = data.cont;
	this.kontinent = data.cid;
	
	this.rate = (bcdata.rates[rate])?bcdata.rates[rate]:null;
}



/* Startfunktion, wird beim Laden der Seite aufgerufen.
Bekommt das akutelle BC übergeben. Ruft folgende Funktionen auf:
drawFlag, populatePulldown, displaySelectInfo*/

function startUp(landName) {
	if(landName != '') {
	var land = convertLandtoNumber(landName);
	gAktBC = land;
	drawFlag(land,0,0); // land = aktuelles Land, 0 = Flagge, 0 = Weltkarte
	displayAktuellesBC(land);
	displaySelectInfo(land);
	} else {var land = undefined;}
	gZoom = 0;
	gNewBC = undefined;
	populatePulldown(land,0); // land = aktuelles Land

//	alert("Die landnummer ist " + land);
}

function convertLandtoNumber(landName) {
	for(var i = 0; i < landListe.length; i++) {
		if(landListe[i].name == landName) {return i;break;}
	}	
}

function populatePulldown(land,kontinent){
	var sLandAuswahl = document.getElementById('selectLandAuswahl');
	sLandAuswahl.length = 0;
	
	for(var i = 0; i < landListe.length; i++) {
		
	if(landListe[i].kontinent == kontinent || kontinent == 0) {	
		
		if(i == land && land != undefined) {var sel = true; var einSel= true;} else {var sel = false;}
		var eineOption = new Option(landListe[i].name, i, sel, sel);
		
		sLandAuswahl.options[sLandAuswahl.options.length] = eineOption;
		
		if(i == gAktBC) {
			var boldness = document.createAttribute("class");
			boldness.nodeValue = "txtBold";
			sLandAuswahl.options[sLandAuswahl.options.length-1].setAttributeNode(boldness);
			
		}
		//sLandAuswahl.options[sLandAuswahl.options.length-1].style.font-weight = "bold";
		}
	}	
	if(!einSel) {
		var eineOption = new Option('Bitte ein Land wählen', 'X', true, true);
		sLandAuswahl.options[sLandAuswahl.options.length] = eineOption;
		sLandAuswahl.options[sLandAuswahl.options.length-1].selected=true;
	}
}

function displaySelectInfo(land) {
	var country = window.landListe[land];
	var rate = country.rate;
	var tarifPreis = rate.price;
	var tarifBezeichnung = rate.label;
	var tarifInterval = rate.interval;
	var tInfo1 = document.getElementById('tInfo1');
	var tInfo2 = document.getElementById('tInfo2');
	
	/*
	tInfo1.innerHTML = "Normalpreis: "+ tarifPreis + " (" + tarifBezeichnung +")";
	tInfo2.innerHTML= "BC preis: "+ (tarifPreis/2);
	*/
	
	var tarifPreisCT = Math.round((tarifPreis*10000))/100;
	
	if (land != gAktBC) {
		tInfo1.innerHTML = 'Normalpreis: <br>' + tarifBezeichnung + ' ('+ tarifPreisCT +' ct/'+tarifInterval+')';
		tInfo2.innerHTML= 'BC Preis (50%): <br>' + (tarifPreisCT*0.5) + ' ct/'+tarifInterval;
	} else {
		tInfo1.innerHTML = '<span class="txtBold" style="color:#F77400">Ihr aktuelles <br> Best Country</span>'
		tInfo2.innerHTML= 'Ihr Preis: <br>' + (tarifPreisCT*0.5) + ' ct/'+tarifInterval;
	}
	
}


function displayAktuellesBC(land) {
	var aktBC = document.getElementById('aktBC');
	aktBC.innerHTML = landListe[land].name;
		
}

function selectCountry() {
	
	var sLandAuswahl = document.getElementById('selectLandAuswahl');	
	var land = sLandAuswahl.options[sLandAuswahl.selectedIndex].value;
	var submitvaluefield = document.getElementById('submitvaluefield');
	if(land != 'X') {	
		gNewBC = land;	
	
		displaySelectInfo(land);
	
		
	
		if(gNewBC != gAktBC) {			
		
			submitvaluefield.value = landListe[land].submitValue;
			drawFlag(land,1,gZoom);
			} else {
				//gNewBC = ''; alert('neee');
				submitvaluefield.value = "";
				document.getElementById('newBCmarker').style.display ='none';
	}
	} else {

		gNewBC = undefined;
		submitvaluefield.value = '';
		document.getElementById('newBCmarker').style.display ='none';
		var tInfo1 = document.getElementById('tInfo1');
		var tInfo2 = document.getElementById('tInfo2');

		tInfo1.innerHTML = '';
		tInfo2.innerHTML= '';
		}
}
var kontinentGlobal = "";
function secToKont() {
	var bcWelt = document.getElementById('bcAuswahlPicSource');
	bcWelt.src = '/psources/img/phone/kontkarte_' + kontinentGlobal + '.gif';
}


function zoomToKont(kont) {
	
	document.getElementById('bcspecialheadline').src = '/psources/img/phone/headline_bc_' + kont + '.gif';


	var bcWelt = document.getElementById('bcAuswahlPicSource');
	bcWelt.attributes["useMap"].nodeValue = "#test";
	//bcWelt.useMap = '';
	//bcWelt.removeAttribute('useMap');
	//document.images["fasuwahlpic"].removeAttribute("useMap", 0);

	bcWelt.src = '/psources/img/phone/kontkarte_' + kont + '.gif';
	kontinentGlobal = kont;
//document.getElementById("bcAuswahlPicSource").removeAttribute("useMap");
	setTimeout('secToKont()', 100)


	if(gNewBC != "") {populatePulldown(gNewBC,kont);}
	else if(gAktBC != "") {populatePulldown(gAktBC,kont);}
	else {populatePulldown("",kont);}	
	gZoom = 1;
	gKont = kont;

	drawFlag(gAktBC,0,gZoom);
	drawFlag(gNewBC,1,gZoom);
	document.getElementById('backButton').style.display = 'Block';
	
}

function hoverKont(kont) {
	document.getElementById('bcAuswahlPicSource').src = '/psources/img/phone/weltkarte_' + kont + '.gif';	
}

function unhoverKont() {
	document.getElementById('bcAuswahlPicSource').src = '/psources/img/phone/weltkarte_' + 0 + '.gif';	
}

function drawFlag(land,art,zoom) {
	if(land != undefined) {
	var aktLand = landListe[land];
	if (aktLand.kontinent == gKont || gKont == 0) {
		
		if (zoom == 0) {var myKoo = aktLand.kooWelt} else {var myKoo = aktLand.kooKont}
		
		if (art == 0) {
			var myMarker = document.getElementById('aktBCmarker');
		
			myx = myKoo[0] + 23;
			myy = myKoo[1] - 6;
			}
		else {
			var myMarker = document.getElementById('newBCmarker');
			myx = myKoo[0] + 23;
			myy = myKoo[1] - 6;
			}
		
		
	
	myMarker.style.top = myy + 'px';
	myMarker.style.left = myx + 'px';
	myMarker.style.display = 'Block';
	
		/*if (art == 0)
		{var myTT = document.getElementById('iBoxSmall4');
		myTT.innerHTML = 'aktuelles Best Country: <br> <span class="txtBold">' + aktLand.name + '</span>';	
		}
		else
		{var myTT = document.getElementById('iBoxSmall5')
		myTT.innerHTML = 'neues Best Country: <br> <span class="txtBold">' + aktLand.name + '</span>';
		}*/
		
	} else if(art == 0) {document.getElementById('aktBCmarker').style.display = 'none';}
		else {document.getElementById('newBCmarker').style.display ='none';}
	} 
}

function backToWorld() {
	document.getElementById('bcspecialheadline').src = '/psources/img/phone/headline_bc_special.gif';
	var bcWelt = document.getElementById('bcAuswahlPicSource');
	bcWelt.src = '/psources/img/phone/weltkarte_0.gif';
	bcWelt.useMap = '#weltkarte_map';
	gZoom = 0;
	gKont = 0;
	drawFlag(gAktBC,0,gZoom);
	drawFlag(gNewBC,1,gZoom);
	
	if(gNewBC != "") {populatePulldown(gNewBC,"");}
	else if(gAktBC != "") {populatePulldown(gAktBC,"");}
	else {populatePulldown("",0);}
	
	document.getElementById('backButton').style.display = 'none';	
}

function showResult() {
	var wert = document.getElementById('submitvaluefield').value;
	if(wert) {document.forms[0].submit; alert('Land: ' + wert)} else {alert('Bitte wählen Sie ein Land aus der Liste, bevor Sie fortfahren.');}
}
