

function fillDropDownDistricts(mainDistrictID, industryId, replace) {
	var selectedDistrict = document.getElementById("ctl00_selected_district");
	if (selectedDistrict != null && replace == 1)
	{
		selectedDistrict.value = "0";
	}
	var selectedDistrictText = document.getElementById("region");
	if (selectedDistrictText != null && replace == 1)
	{
		selectedDistrictText.innerHTML = "Region";
	}
	
	var ul = document.getElementById("region_ul");
	var selectionInList = 0;
	while (ul.firstChild) ul.removeChild(ul.firstChild);

	jQuery.getJSON("http://www.firmen.tv/Webservices.MainPageVideos/Videos.svc/videos/resulturls/districts/" + mainDistrictID + "/" + industryId,
		function(data) {
			var dropDown = jQuery('#region_ul');
			jQuery.each(data, function(i, item) {
				if (document.getElementById('ctl00_selected_district').value == item.id)
					selectionInList = 1;

				jQuery('<li/>')
					.html(item.name.replace(/&/g,'&amp;'))
					.click(function() {
						document.getElementById('region_ul').style.display = 'none';
						document.getElementById('region').innerHTML = this.innerHTML;
						document.getElementById('ctl00_selected_district').value = this.id.substring(this.id.lastIndexOf('_')+1);
						fillDropDownIndustries(this.id.substring(this.id.lastIndexOf('_')+1));
						setPreviewImages(document.getElementById('ctl00_selected_maindistrict').value,document.getElementById('ctl00_selected_district').value,document.getElementById('ctl00_selected_industry').value);
					})
					.attr('id', 'region_' + item.id)
					.appendTo(dropDown);	
			});
			if (selectionInList == 0)
			{
				selectedDistrict.value = "0";
				selectedDistrictText.innerHTML = "Region";
			}			
			if (!ul.firstChild)
			{
				jQuery('<li/>').html('Bitte w&auml;hlen Sie ein Bundesland aus.').appendTo(dropDown);
			}
	});
	
}

function fillDropDownMainDistricts(industryId) {
	var ul = document.getElementById("bundesland_ul");
	while (ul.firstChild) ul.removeChild(ul.firstChild);
	var selectionInList = 0;
			
	jQuery.getJSON("http://www.firmen.tv/Webservices.MainPageVideos/Videos.svc/videos/resulturls/maindistricts/" + 0,
		function(data) {
			var dropDown = jQuery('#bundesland_ul');
			jQuery.each(data, function(i, item) {
			
			if (document.getElementById('ctl00_selected_maindistrict').value == item.id)
				selectionInList = 1;

			jQuery('<li/>')
				.html(item.name.replace(/&/g,'&amp;'))
				.click(function() {
					document.getElementById('bundesland_ul').style.display = 'none';
					document.getElementById('bundesland').innerHTML = this.innerHTML;
					document.getElementById('ctl00_selected_maindistrict').value = this.id.substring(this.id.lastIndexOf('_')+1);
					fillDropDownDistricts(this.id.substring(this.id.lastIndexOf('_')+1),industryId,1);
					fillDropDownIndustries(this.id.substring(this.id.lastIndexOf('_')+1));
					setPreviewImages(document.getElementById('ctl00_selected_maindistrict').value,document.getElementById('ctl00_selected_district').value,document.getElementById('ctl00_selected_industry').value);
					document.getElementById('region').innerHTML = "Region";
					document.getElementById('ctl00_selected_district').value = 0;
					document.getElementById('region').innerHTML = "Branche";
					document.getElementById('ctl00_selected_industry').value = 0;
				})
				.attr('id', 'bundesland_' + item.id)
				.appendTo(dropDown);
		});
		if (selectionInList == 0)
		{
			document.getElementById('ctl00_selected_maindistrict').value = 0;
			document.getElementById('bundesland').innerHTML = "Bundesland";
		}
	});
}

function fillDropDownIndustries(districtId)
{
	var ul = document.getElementById("branche_ul");
	var selectionInList = 0;
	while (ul.firstChild) ul.removeChild(ul.firstChild);

	jQuery.getJSON("http://www.firmen.tv/Webservices.MainPageVideos/Videos.svc/videos/resulturls/mainindustries/" + districtId,
		function(data) {
			var dropDown = jQuery('#branche_ul');
				jQuery.each(data, function(i, item) {
				if (document.getElementById('ctl00_selected_industry').value == item.id)
					selectionInList = 1;
				
				jQuery('<li/>')
				.html(item.name.replace(/&/g,'&amp;'))
				.click(function() {
					document.getElementById('branche_ul').style.display = 'none';
					document.getElementById('branche').innerHTML = this.innerHTML;
					document.getElementById('ctl00_selected_industry').value = this.id.substring(this.id.lastIndexOf('_')+1);
					fillDropDownMainDistricts(this.id.substring(this.id.lastIndexOf('_')+1));
					fillDropDownDistricts(document.getElementById('ctl00_selected_maindistrict').value,this.id.substring(this.id.lastIndexOf('_')+1),0);
					setPreviewImages(document.getElementById('ctl00_selected_maindistrict').value,document.getElementById('ctl00_selected_district').value,document.getElementById('ctl00_selected_industry').value);
				})
				.attr('id', 'branche_' + item.id)
				.appendTo(dropDown);
		});
		if (selectionInList == 0)
		{
			document.getElementById('ctl00_selected_industry').value = 0;
			document.getElementById('branche').innerHTML = "Branche";
		}
	});
}

function fillAllWhenReady(mainDistrictId, districtId, industryId)
{
	var districtID = districtId;
	if (districtID == "")
	{
		districtID = mainDistrictId;
	}

	jQuery(function() {
		fillDropDownMainDistricts(industryId);
		fillDropDownDistricts(mainDistrictId,industryId,0);
		fillDropDownIndustries(districtId);
	});	
}

function setMaindistrict(districtId)
{
	var listItem = document.getElementById('bundesland_' + districtId);
	if (listItem != null) //if selection exists
	{
		document.getElementById('bundesland').innerHTML = listItem.innerHTML;
		document.getElementById('ctl00_selected_maindistrict').value = districtId;
		fillDropDownDistricts(districtId,0,1);
		fillDropDownIndustries(districtId);
		document.getElementById('region').innerHTML = "Region";
		document.getElementById('ctl00_selected_district').value = 0;
		document.getElementById('region').innerHTML = "Branche";
		document.getElementById('ctl00_selected_industry').value = 0;
		setPreviewImages(document.getElementById('ctl00_selected_maindistrict').value,document.getElementById('ctl00_selected_district').value,document.getElementById('ctl00_selected_industry').value);
	}
	else /* reset all Selection */
	{
		document.getElementById('bundesland').innerHTML = 'Bundesland';
		document.getElementById('ctl00_selected_maindistrict').value = 0;
		document.getElementById('region').innerHTML = "Region";
		document.getElementById('ctl00_selected_district').value = 0;
		document.getElementById('region').innerHTML = "Branche";
		document.getElementById('ctl00_selected_industry').value = 0;
		fillAllWhenReady(0, 0, 0)
		setPreviewImages(0, 0, 0);
	}
}

function fillLinkList(maindistrictid, districtId, mainindustryId, industryId) {
	var mdid = document.getElementById("maindistrictlist");
	var did = document.getElementById("districtlist");
	var miid = document.getElementById("mainindustrylist");
	var iid = document.getElementById("industrylist");
	
	if (mdid == null)
	{
		alert("mdid null");
	}
	if (did == null)
	{
		alert("did null");
	}
	if (miid == null)
	{
		alert("miid null");
	}
	if (iid == null)
	{	
		alert("iid null");
	}
	while (mdid.firstChild) mdid.removeChild(mdid.firstChild);
	while (did.firstChild) did.removeChild(did.firstChild);
	while (miid.firstChild) miid.removeChild(miid.firstChild);
	while (iid.firstChild) iid.removeChild(iid.firstChild);
	
	if (industryId == 0)
	{
		industryId = mainindustryId;
	}
	
	jQuery(document).ready(function() {
	jQuery.getJSON("http://www.firmen.tv/Webservices.MainPageVideos/Videos.svc/videos/resulturls/maindistricts/" + industryId,
			function(data) {
				mdid.innerHTML = "<li<b>Auswahl Bundesland</b></li><li>&nbsp;</li>";
				jQuery.each(data, function(i, item) {
					jQuery("<li />").html("<a href=\"" + item.url + "\">" + item.name + "</a>").appendTo("#maindistrictlist");
				});
			});
	});
	
	jQuery(document).ready(function() {
	jQuery.getJSON("http://www.firmen.tv/Webservices.MainPageVideos/Videos.svc/videos/resulturls/districts/" + maindistrictid + "/" + industryId,
			function(data) {
				did.innerHTML = "<li><b>Auswahl Stadt</b></li><li>&nbsp;</li>";
				jQuery.each(data, function(i, item) {
					jQuery("<li />").html("<a href=\"" + item.url + "\">" + item.name + "</a>").appendTo("#districtlist");
				});
			});
	});
	
	jQuery(document).ready(function() {
	jQuery.getJSON("http://www.firmen.tv/Webservices.MainPageVideos/Videos.svc/videos/resulturls/mainindustries/" + districtId,
		function(data){
			miid.innerHTML = "<li><b>Auswahl Kategorie</b></li><li>&nbsp;</li>";
			jQuery.each(data,function(i, item) {
				jQuery("<li />").html("<a href=\"" + item.url + "\">" + item.name + "</a>").appendTo("#mainindustrylist");
			});		
		});
	});
		
	jQuery(document).ready(function() {
	jQuery.getJSON("http://www.firmen.tv/Webservices.MainPageVideos/Videos.svc/videos/resulturls/industries/" + mainindustryId + "/" + districtId,
			function(data) {
				iid.innerHTML = "<li><b>Auswahl Branche</b></li><li>&nbsp;</li>";
				jQuery.each(data, function(i, item) {
					jQuery("<li />").html("<a href=\"" + item.url + "\">" + item.name + "</a>").appendTo("#industrylist");
				});
			});
	});
}

function fillMainIndustries()
{
	var ul = document.getElementById("mainindustries_ul");
	while (ul.firstChild) ul.removeChild(ul.firstChild);

	jQuery.getJSON("http://www.firmen.tv/Webservices.MainPageVideos/Videos.svc/videos/resulturls/mainindustries/0",
		function(data) {
			var dropDown = jQuery('#mainindustries_ul');
			jQuery.each(data, function(i, item) {
				jQuery('<li/>').html("<a href=\"" + item.url + "\">" + item.name.replace(/&/g,'&amp;').toUpperCase() + "</a>").appendTo(dropDown);
			});
			if (ul.firstChild) ul.removeChild(ul.lastChild);
	});
}

function setPreviewImages(mdid,did,iid)
{
	var Link1 = document.getElementById("PreviewLink1");
	var Link2 = document.getElementById("PreviewLink2");
	var Link3 = document.getElementById("PreviewLink3");
	
	var Image1Link = document.getElementById("ctl00_content_PreviewImage1Link");
	var Image2Link = document.getElementById("ctl00_content_PreviewImage2Link");
	var Image3Link = document.getElementById("ctl00_content_PreviewImage3Link");
	
	var Image1 = document.getElementById("ctl00_content_PreviewImage1");
	var Image2 = document.getElementById("ctl00_content_PreviewImage2");
	var Image3 = document.getElementById("ctl00_content_PreviewImage3");

	var Description1 = document.getElementById("ctl00_content_Description1");
	var Description2 = document.getElementById("ctl00_content_Description2");
	var Description3 = document.getElementById("ctl00_content_Description3");
	
	if (Link1 == null)
		return;
	
	
	
	jQuery.getJSON("http://www.firmen.tv/Webservices.MainPageVideos/Videos.svc/videos/videos/" + mdid + "/" + did + "/" + iid,
		function(data) {
			if (data.length > 2)
			{
				Link3.innerHTML = data[0].headline.replace(/&/g,'&amp;');
				Link3.href = data[0].profileLink;
				Image3Link.href = data[0].profileLink;
				Description3.innerHTML = "";
				Image3.src = data[0].picturePath;
				Image3.alt = "Video " + data[0].headline;
								
				Link1.innerHTML = data[1].headline.replace(/&/g,'&amp;');
				Link1.href = data[1].profileLink;
				Image1Link.href = data[1].profileLink;
				Description1.innerHTML = "";
				Image1.src = data[1].picturePath;
				Image1.alt = "Video " + data[1].headline;
								
				Link2.innerHTML = data[2].headline.replace(/&/g,'&amp;');
				Link2.href = data[2].profileLink;
				Image2Link.href = data[2].profileLink;
				Description2.innerHTML = "";
				Image2.src = data[2].picturePath;
				Image2.alt = "Video " + data[2].headline;
								
				
				
		}
	});
}

function fillHits(mainDistrictID,districtID,industryID,industryName)
{
	var box=document.getElementById("hitlist");
	box.innerHTML="";
	jQuery.getJSON("http://www.firmen.tv/Webservices.MainPageVideos/Videos.svc/videos/hits/"+mainDistrictID+"/"+districtID+"/"+industryID,function(data)
	{
		jQuery.each(data,function(i,item)
		{
			if (item.districtName==""&&industryName=="")
			{
				jQuery("<span/>").html("<a href=\""+item.link+"\">"+item.mainDistrictName+"</a>").attr("class","last nounderline").appendTo("#hitlist");
			}
			else
			{
				jQuery("<span/>").html("<a href=\""+item.link+"\">"+item.mainDistrictName+"</a>").attr("class","nounderline").appendTo("#hitlist");
			}
			if (item.districtName!="")
			{
				if (industryName=="")
				{
					jQuery("<span/>").html("<a href=\""+item.link+"\">"+item.districtName+"</a>").attr("class","last nounderline").appendTo("#hitlist");
				}
				else
				{
					jQuery("<span/>").html("<a href=\""+item.link+"\">"+item.districtName+"</a>").attr("class","nounderline").appendTo("#hitlist");
				}
			}
			if (industryName!="")
			{
				jQuery("<span/>").html("<a href=\""+item.link+"\">"+industryName).attr("class","last nounderline").appendTo("#hitlist");
			}
			jQuery("<a/>").attr("href",item.link).html("<img src=\"res/img/blauer_pfeil_footer_box.gif\" class=\"noborder\" />").appendTo("#hitlist");
		});
		if (box.innerHTML=='')
		{
			document.getElementById("box_hitlist").style.display='none';
		}
	});
}
