﻿// Menu-Vars

var act_menu = null;

var act_submenu = null;

// Document-Load

$(document).ready(function() {

	var docRoot = getDocRoot();
	
	$('#menu a').bind('click', function(){
		set_menu(this);
		$(this).blur();
		return false;
	});

	var loc = document.location.href;
	
	if(document.location.href.indexOf('#') != -1){
		var tmp = document.location.href.split("#");
		loc = tmp[0];
	}

	if(loc.indexOf('preview=true') != -1){
	
		var tmp = loc.split('?');
	
		$('#inner_content').load('output_preview.php?' + tmp[1],
			function(data){
				$('#inner_content').attr('class','load_finished');
			}
		);
		
		if(loc.indexOf('preview_type=thema') == -1){
			$('#breadcrumbs').html('');
		}else{
			$('#breadcrumbs').load(docRoot + "output_breadcrumbs.php?" + tmp[1],
				function(){
					$('#inner_content').attr('style', 'top:156px');
				}
			);
		}
	}else{
	
		if(loc.indexOf('html') != -1 && loc != docRoot + "index.html" && loc != docRoot + "index_f.html" && loc != docRoot + "index_e.html"){
		
			var m = new Object();
			m.href = loc;
	
			set_menu(m);
		}else{
			var m = $('#m_home');
	
			set_menu(m);
		}
		}
});

// Menu-Functions

function set_menu(m){

	// Untermenu wird direkt aufgerufen -> redirect

	if($(m).attr('id') == undefined){

		if(m.href.indexOf("#") != -1){
			var t = m.href.split("#");
			m.href = t[0];
		}

		var tmp = m.href.split("\/");
		var tmp2 = tmp[1];
		
		$('#menu a').each(function(index){
		
			var t = $(this).attr('title');
			var l = $(this).attr('href');
			
			if(m.href.indexOf(l) != -1){

				var hm = $(this).parent().parent().attr('id');
				act_menu = hm;
				show_hauptmenu(hm);
				show_submenu(this);
			}
		});
	}else{

		var par = $(m).parent().attr('id');
		
		
		if(par == undefined || par.indexOf('_content') == -1){
		
			// Hauptmenu wurde geklickt

			var tmp = $(m).attr('href').split("\/");
			var menu = "";
			
			tmp[0] == "" ? menu = tmp[1] : menu = tmp[0];
			
			//if(act_menu != menu)
				show_hauptmenu(menu, false);
		}else{
			// Submenu wurde geklickt
			
			show_submenu(m);
		}
	}
}

function show_hauptmenu(hm, condition){
	
	if(act_menu != null && act_menu != hm){
		//$('#' + act_menu + "_content").hide('slow');
		
		$('#' + act_menu + "_content").animate({'height': 'hide'}, 'slow');
		
		$('#m_' + act_menu).attr('class', 'inactive');
	}

	act_menu = hm;		
	//$('#' + act_menu + "_content").show('slow');
	$('#' + act_menu + "_content").animate({'height': 'show'}, 'slow');
	$('#m_' + act_menu).attr('class', 'active');

	if(act_submenu != null)
		$(act_submenu).attr('class', 'inactive');
	
	if(condition == false){
		act_submenu = null;
		
		var to_show = $('#m_' + hm).text();
		
		if(hm == "home")
			to_show = "Start";
		
		show_content(to_show);
	}
}

function show_submenu(sm){
	if(act_submenu != null){
		$(act_submenu).attr('class','inactive');
	}

	act_submenu = sm;
	
	$(sm).attr('class','active');
	if(act_submenu.title != "")
		show_content(act_submenu.title);
}

function choose_internal(which){
	
	var tmp = which.split('?');
	
	$.get('get_menu.php?link=' + tmp[1], 
		function(data){
			$('#menu a').each(function(index){

				var t = $(this).attr('title');
				var l = $(this).attr('href');

				if(t == unescape(data)){

					var hm = $(this).parent().parent().attr('id');
					//act_menu = hm;
					show_hauptmenu(hm, false);
					show_submenu(this);
				}
			});
		}
	);
}

// Content anzeigen

function show_content(which){

	var docRoot = getDocRoot();

	$('#inner_content').attr('class', 'loading');
	$('#inner_content').html('');
	
	//$('#breadcrumbs').attr('class', 'loading');
	$('#breadcrumbs').html('');
	/*
	if(which == "ausstellerverzeichnis" || which == "Liste des exposants" || which == "List of exhibitors"){
		show_ausstellerlisten(which);
	}else if(which.indexOf("Hallenpl") != -1 || which == "Plans des halles" || which == "Hall maps"){
		show_hallenplaene(which);
	}else{
	*/
		var p = which.indexOf("/");
		var ht = which.indexOf(".html");
		
		if(p != -1 && ht != -1){
			tmp = which.split("/");
			tmp2 = tmp[tmp.length - 1];
			
			tmp = tmp2.split(".");
			
			content_id = tmp[0];
			
		}else{
			content_id = which;
		}
		
		var has_breadcrumbs = true;
	
		$('#breadcrumbs').load(docRoot + 'output_breadcrumbs.php?show_link=' + escape(content_id) + "&sprache=" + sprache,
			function(data){
				if(data == "")
					has_breadcrumbs = false;
			
					$.get(docRoot + 'output_content.php?show_link=' + escape(content_id) + "&sprache=" + sprache,
						function(data){
						
							if(which == "ausstellerverzeichnis" || which == "Liste des exposants" || which == "List of exhibitors"){
								if(which == "ausstellerverzeichnis")
									lang = "D";
									
								if(which == "Liste des exposants")
									lang = "F";
									
								if(which == "List of exhibitors")
									lang = "E";
									
								var url = 'm_code=' + m_code + '&lang=' + lang; 	
								
								$.get('aussteller_iframe.php?url=' + url, 
									function(hp_data){
										make_inner_content(data + hp_data, has_breadcrumbs, which);
									}
								);
								
							}else if(which.indexOf("Hallenpl") != -1 || which == "Plans des halles" || which == "Hall maps"){
								if(which.indexOf("Hallenpl") != -1)
									lang = "D";
									
								if(which == "Plans des halles")
									lang = "F";
									
								if(which == "Hall maps")
									lang = "E";
									
								var url = 'm_code=' + m_code + '&lang=' + lang; 	
								
								$.get('hallenplaene_iframe.php?url=' + url, 
									function(hp_data){
										make_inner_content(data + hp_data, has_breadcrumbs, which);
									}
								);	
							}else{
								make_inner_content(data, has_breadcrumbs, which);
							}
							
						}
					);
					/*
					$('#inner_content').load(docRoot + 'output_content.php?show_link=' + escape(content_id) + "&sprache=" + sprache,
						function(){ 
							
							// Bind the href-tag to internal links
							
							$('#inner_content a[rel="internal"]').bind('click', function(){
								choose_internal($(this).attr('href'));
								$(this).blur();
								return false;
							});
							
							$('#inner_content').attr('class', 'load_finished');
							
							if(has_breadcrumbs == true && which != "Start"){
								$('#inner_content').attr('style', 'top:156px');
							}else if(has_breadcrumbs == false && which != "Start"){
								$('#inner_content').attr('style', 'top:126px');
							}
						});
						*/
			}
		);
	//}
}

function make_inner_content(data, has_breadcrumbs, which){
	$('#inner_content a[rel="internal"]').bind('click', function(){
		choose_internal($(this).attr('href'));
		$(this).blur();
		return false;
	});
	
	$('#inner_content').html(data);
	
	$('#inner_content').attr('class', 'load_finished');
	
	if(has_breadcrumbs == true && which != "Start"){
		$('#inner_content').attr('style', 'top:156px');
	}else if(has_breadcrumbs == false && which != "Start"){
		$('#inner_content').attr('style', 'top:126px');
	}
}

function getDocRoot(){

	if(document.location.host == "localhost"){
		return "http://localhost/Kunden-Websites/Kunden/TTW-Site/TTW09/TTW09/";
	}else{
		return "http://www.ttw.ch/";
	}
}


// Window-opener

function MM_openBrWindow(URL, name, specs){
	var newWin = window.open(URL,name,specs);
	//return false;
}


// Ausstellerlisten

var m_code = "3242b4a68270c45b2c885a52ea42f33a";

function get_url(addr){
	var r = $.ajax({
		type: 'GET',
		url: addr,
		async: false
	}).responseText;
	
	return r;
}

function show_hallenplaene(which){
	$('#inner_content').attr('style', 'top:126px');
	
	var lang;

	if(which.indexOf("Hallenpl") != -1)
		lang = "D";
		
	if(which == "Plans des halles")
		lang = "F";
		
	if(which == "Hall maps")
		lang = "E";
		
	var url = 'm_code=' + m_code + '&lang=' + lang; 	
	
	$.get('hallenplaene_iframe.php?url=' + url, 
		function(data){
			window.alert(data);
			/*$('#inner_content').attr('class', 'load_finished');
			$('#inner_content').html(data);
			*/
			
			return data;
		}
	);	
}

function show_ausstellerlisten(which){
	// Ajax call to hallenplaene.net-worker.ch
	
	$('#inner_content').attr('style', 'top:126px');
	
	var lang;

	if(which == "ausstellerverzeichnis")
		lang = "D";
		
	if(which == "Liste des exposants")
		lang = "F";
		
	if(which == "List of exhibitors")
		lang = "E";
		
	var url = 'm_code=' + m_code + '&lang=' + lang; 	
	
	$.get('aussteller_iframe.php?url=' + url, 
		function(data){
		
			return data;
		
			/*$('#inner_content').attr('class', 'load_finished');
			$('#inner_content').html(data);
			*/
		}
	);	
}

function filter_ausstellerliste(formName){
	var serial = $('#' + formName).serialize();
	
	$.post('list_aussteller.php', serial,
		function(data){
			$('#inner_content').html(data);
		}
	);
}

function aussteller_filter_alpha(buchstabe){
	$('#inner_content').load('list_aussteller.php?filter_alpha=' + buchstabe);
	
	return false;
}

function show_halle(halle_nr, id_aussteller){
	$('#inner_content').load('show_halle.php?id_halle=' + halle_nr + "&id_aussteller=" + id_aussteller + "&inline=false",
		function (){
			this_visible = id_aussteller;
			show_aussteller('div_' + id_aussteller,1);
		}
	);
	
	return false;
}

function show_ausstellerliste(){
	$('#inner_content').load('list_aussteller.php?');
	
	return false;
}