var running = false;

function loadCat(id, lang)
{
	if(running) return;
	shopKategoriestamm(id, lang, 0);
	shopKategoriestammArtikel(id, lang);
}

function loadCatExpanded(id, lang)
{
	if(running) return;
	shopKategoriestamm(id, lang, 1);
	shopKategoriestammArtikel(id, lang);
}

function shopKategoriestamm(id, lang, expanded)
{
	var extra = (expanded) ? "&menuExpanded=" : "" ;
	var url = 'index.php?api=cat&shopKategoriestamm='+parseInt(id)+'&lang='+escape(lang)+extra;
	var ret = simpleDownload(url);
	document.getElementById('menu').innerHTML = ret;
	return;
}

function shopKategoriestammArtikel(id, lang)
{
	running = true;
	var dbi = '<div style="text-align:center; padding-top:128px;"><img src="img/progress.gif" alt="Inhalt wird geladen"></div>';
	writeContent(dbi);
	var url = 'index.php?api=cat&shopKategoriestammArtikel='+parseInt(id)+'&lang='+escape(lang);
	var ret = simpleDownload(url);
	writeContent(ret);
	running = false;
	return;
}

function artikelinfo(id, lang)
{
	running = true;
	var dbi = '<div style="text-align:center; padding-top:128px;"><img src="img/progress.gif" alt="Inhalt wird geladen"></div>';
	writeContent(dbi);
	var url = 'index.php?api=cat&shopKategoriestammArtikel=&shopArtikel='+escape(id)+'&lang='+escape(lang);
	var ret = simpleDownload(url);
	writeContent(ret);
	running = false;
	return;
}

function handyFilterModelle(hersteller, lang)
{
	running = true;
	var dbi = '<div style="text-align:center; padding-top:128px;"><img src="img/progress.gif" alt="Inhalt wird geladen"></div>';
	writeContent(dbi);
	var url = 'index.php?api=cat&handyFilterModelle='+escape(hersteller)+'&lang='+escape(lang);
	var ret = simpleDownload(url);
	writeContent(ret);
	running = false;
	return;
}

function handyFilterModelleFehlerMelden(bid, verk, lang)
{
	var retBox = document.getElementById(bid);
	var url = 'index.php?api=cat&handyFilterModelleFehlerMelden='+escape(verk)+'&lang='+escape(lang);
	var ret = simpleDownload(url);
	retBox.innerHTML = ret;
	return;
}

function setHandyFilter(handyID, lang) 
{
	running = true;
	var dbi = '<div style="text-align:center; padding-top:128px;"><img src="img/progress.gif" alt="Inhalt wird geladen"></div>';
	writeContent(dbi);
	var url = 'index.php?api=cat&setHandyFilter='+escape(handyID)+'&lang='+escape(lang);
	var ret = simpleDownload(url);
	running = false;
	location.href = 'index.php';
	return;
}

function deleteHandyFilter(lang) 
{
	running = true;
	var dbi = '<div style="text-align:center; padding-top:128px;"><img src="img/progress.gif" alt="Inhalt wird geladen"></div>';
	writeContent(dbi);
	var url = 'index.php?api=cat&deleteHandyFilter=&lang='+escape(lang);
	var ret = simpleDownload(url);
	running = false;
	location.href = 'index.php';
	return;
}

function displayPBInput(id) {
	document.getElementById(id+'pl').style.display = 'none';
	document.getElementById(id+'plInput').style.display = '';
}
function displayPBLink(id) {
	document.getElementById(id+'pl').style.display = '';
	document.getElementById(id+'plInput').style.display = 'none';
}

var currentlyRunning = new Array();
var currentlyWaitingForClose = new Array();


function resizePermalinkBox(id, way) { 
	v = document.getElementById(id+'permalinkBox');
	curWidth = parseInt(v.style.width);
	curMargin = parseInt(v.style.marginLeft);
	if(way == "p") {
		if(currentlyRunning[id] && currentlyRunning[id] == "n") {
			return;
		}
		currentlyRunning[id] = way;
		displayPBInput(id);
		if(curWidth >= 100) {
			v.style.width = '100%';
			v.style.marginLeft = '0';
			delete(currentlyRunning[id]);
			return;
		} else if(curWidth <= 99 && curWidth >= 95) {
			v.style.width = curWidth+1+'%';
			v.style.marginLeft = 100-parseInt(v.style.width)+'%';
			scrollInPermalinkBox(id, way, 75);
		} else if(curWidth <= 90 && curWidth >= 85) {
			v.style.width = curWidth+5+'%';
			v.style.marginLeft = 100-parseInt(v.style.width)+'%';
			scrollInPermalinkBox(id, way, 50);
		} else if(curWidth <= 80 && curWidth >= 55) {
			v.style.width = curWidth+5+'%';
			v.style.marginLeft = 100-parseInt(v.style.width)+'%';
			scrollInPermalinkBox(id, way, 15);
		} else if(curWidth <= 50 && curWidth >= 45) {
			v.style.width = curWidth+5+'%';
			v.style.marginLeft = 100-parseInt(v.style.width)+'%';
			scrollInPermalinkBox(id, way, 50);
		} else if(curWidth <= 40 && curWidth >= 30) {
			v.style.width = curWidth+5+'%';
			v.style.marginLeft = 100-parseInt(v.style.width)+'%';
			scrollInPermalinkBox(id, way, 50);
		} else if(curWidth <= 29 && curWidth >= 25) {
			v.style.width = curWidth+1+'%';
			v.style.marginLeft = 100-parseInt(v.style.width)+'%';
			scrollInPermalinkBox(id, way, 95);
		} else if(curWidth < 25) {
			v.style.width = '25%';
			v.style.marginLeft = '75%';
			scrollInPermalinkBox(id, way, 1);
		}
	}
	
	if(way == "n") {
		if(currentlyRunning[id] && currentlyRunning[id] == "p") {
			return;
		}
		currentlyRunning[id] = way;
		if(curWidth <= 25) {
			displayPBLink(id);
			v.style.width = '25%';
			v.style.marginLeft = '75%';
			delete(currentlyRunning[id]);
			return
		} else if(curWidth >= 101) {
			v.style.width = '100%';
			v.style.marginLeft = '0';
			scrollInPermalinkBox(id, way, 1);
		} else if(curWidth >= 100 && curWidth >= 95) {
			v.style.width = curWidth-1+'%';
			v.style.marginLeft = 100-parseInt(v.style.width)+'%';
			scrollInPermalinkBox(id, way, 95);
		} else if(curWidth >= 95 && curWidth >= 80) {
			v.style.width = curWidth-5+'%';
			v.style.marginLeft = 100-parseInt(v.style.width)+'%';
			scrollInPermalinkBox(id, way, 75);
		} else if(curWidth >= 80 && curWidth >= 70) {
			v.style.width = curWidth-5+'%';
			v.style.marginLeft = 100-parseInt(v.style.width)+'%';
			scrollInPermalinkBox(id, way, 50);
		} else if(curWidth >= 70 && curWidth >= 40) {
			v.style.width = curWidth-5+'%';
			v.style.marginLeft = 100-parseInt(v.style.width)+'%';
			scrollInPermalinkBox(id, way, 15);
		} else if(curWidth >= 40 && curWidth >= 30) {
			v.style.width = curWidth-5+'%';
			v.style.marginLeft = 100-parseInt(v.style.width)+'%';
			scrollInPermalinkBox(id, way, 26);
		} else if(curWidth >= 30 && curWidth >= 26) {
			v.style.width = curWidth-1+'%';
			v.style.marginLeft = 100-parseInt(v.style.width)+'%';
			scrollInPermalinkBox(id, way, 45);
		} else if(curWidth >= 26 && curWidth >= 24) {
			v.style.width = '25%';
			v.style.marginLeft = '75%';
			scrollInPermalinkBox(id, way, 1);
		}
	}
}

function scrollInPermalinkBox(id, way, speed) {
	if(!speed) speed = 100;
	setTimeout('resizePermalinkBox("'+id+'", "'+way+'")', speed);
}

/*
function load_handys_option(hersteller) {
	if(!hersteller.length) {
		var box = document.getElementById('hftsb');
		box.innerHTML = '<option value="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>';
		document.thf.submit();
		return;
	}
	var url = "index.php?tmphandyfilterhersteller="+escape(hersteller);
	var ret = simpleDownload(url);
	if(ret.substring(0,5) == "error") {
		alert(ret);
		return;
	}
	var box = document.getElementById('hftsb');
	box.innerHTML = ret;
	return;
}
*/

/*
function load_handys_option(hersteller) {
	if(!hersteller.length) {
		var box = document.getElementById('hftsb');
		box.innerHTML = '<option value="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>';
		document.thf.submit();
		return;
	}
	var tx = 'ho_'+hersteller;
	var optionBox = document.getElementById(tx);
	var s = optionBox.innerHTML.split("\n");
	var html = '<option value="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>';
	for (var i in s) {
		var c = s[i];
		var cc = c.split(":", 2);
		var chtml = '<option value="'+cc[0]+'">'+cc[1]+'</option>';
		html += chtml;
	}
	var box = document.getElementById('hftsb');
	box.innerHTML = html;
	return;
}
*/

function load_handys_option(hersteller) {
	if(!hersteller.length) {
		removeOption(document.thf.handyfilter);
		var oOption = document.createElement("OPTION");
		oOption.text="                ";
		oOption.value="";
		try {
			document.thf.handyfilter.add(oOption, null);
		}
		catch(ex) {
			document.thf.handyfilter.add(oOption);
		}
		document.thf.submit();
		return;
	}
	
	removeOption(document.thf.handyfilter);
	
	var oOption = document.createElement("OPTION");
	oOption.text="                ";
	oOption.value="";
	try {
		document.thf.handyfilter.add(oOption, null);
	}
	catch(ex) {
		document.thf.handyfilter.add(oOption);
	}
	
	for (var i in HF[hersteller]) {
		var oOption = document.createElement("OPTION");
		oOption.text=HF[hersteller][i];
		oOption.value=i;
		try {
			document.thf.handyfilter.add(oOption, null);
		}
		catch(ex) {
			document.thf.handyfilter.add(oOption);
		}

	}
	return;
}

function removeOption(obj) {
	while (obj.length > 0) {
		obj.remove(0);
	} 
}







