var ticketpreis = 9.00;

var current_page = new Object();
var menu_map = new Object();

menu_map["news.htm"] = new Object();
menu_map["news.htm"]["page"] = "news.htm";
menu_map["news.htm"]["right_offset"] = "79";
menu_map["news.htm"]["top_offset"] = "0";

menu_map["info.htm"] = new Object();
menu_map["info.htm"]["page"] = "info.htm";
menu_map["info.htm"]["right_offset"] = "114";
menu_map["info.htm"]["top_offset"] = "1";
menu_map["info.htm"]["submenu"] = true;
menu_map["info.htm"]["submenu_height"] = "152px";

menu_map["extras.htm"] = new Object();
menu_map["extras.htm"]["page"] = "extras.htm";
menu_map["extras.htm"]["right_offset"] = "79";
menu_map["extras.htm"]["top_offset"] = "79";
menu_map["extras.htm"]["submenu"] = true;
menu_map["extras.htm"]["submenu_height"] = "152px";

menu_map["lineup.htm"] = new Object();
menu_map["lineup.htm"]["page"] = "lineup.htm";
menu_map["lineup.htm"]["right_offset"] = "0";
menu_map["lineup.htm"]["top_offset"] = "0";
menu_map["lineup.htm"]["submenu"] = true;
menu_map["lineup.htm"]["submenu_height"] = "407px";

menu_map["tickets.htm"] = new Object();
menu_map["tickets.htm"]["page"] = "tickets.htm";
menu_map["tickets.htm"]["right_offset"] = "0";
menu_map["tickets.htm"]["top_offset"] = "1";
menu_map["tickets.htm"]["submenu"] = true;
menu_map["tickets.htm"]["submenu_height"] = "92px";

menu_map["partner.htm"] = new Object();
menu_map["partner.htm"]["page"] = "partner.htm";
menu_map["partner.htm"]["right_offset"] = "0";
menu_map["partner.htm"]["top_offset"] = "79";


function move_menu_entry_back(entry, callback ) {
	var right = parseInt( entry['right'] ) + parseInt( menu_map[ entry["menu_entry"].children().attr('href') ]['right_offset'] );
	var top = parseInt( entry['top'] ) + parseInt( menu_map[ entry["menu_entry"].children().attr('href') ]['top_offset'] );

	entry["menu_entry"].animate( { "right": right, "top": top }, 500, 'swing', function () {
		entry["menu_entry"].css( "right", entry["right"] );
		entry["menu_entry"].css( "top", entry["top"] );
		entry["menu_entry"].css( "height", entry['height']);
		entry["menu_entry"].css( "width", entry['width']);
		entry["menu_entry"].children().css("margin-top", "0");
		entry["menu_entry"].children().css("position", "absolute");
		entry["menu_entry"].css( "background-image", entry["background-image"] );

		if(typeof callback == "function") callback();
	});
}

function show_page(caller, subpage) {
	current_page["menu_map"] = menu_map[ caller.attr('href') ];
	current_page["menu_entry"] = caller.parent();
	current_page["top"] = caller.parent().css('top');
	current_page["right"] = caller.parent().css('right');
	current_page["height"] = caller.parent().css('height');
	current_page["width"] = caller.parent().css('width');
	current_page["background-image"] = caller.parent().css('background-image');
	$(".menuentry").hide();
	$(current_page["menu_entry"]).show();
	caller.parent().css( "height", "61px" );
	caller.parent().css( "width", "145px" );
	caller.css("position", "static");
	caller.css("margin-top", "13px");
	caller.css("margin-left", "auto");
	caller.css("margin-right ", "auto");
	caller.parent().css( "right",
								(parseInt(caller.parent().css('right')) + parseInt(menu_map[ caller.attr('href') ]['right_offset'])) + "px" );
	caller.parent().css( "background-image", "url(menusgl.gif)" );

	caller.parent().animate( { "right": "750px", "top": "0px" }, 500, 'swing', function() {
		$(".logo_text").hide();
		$("#logo").fadeOut("fast", function() {
			$("#contentcontainer").slideDown("slow", function () {
				if( menu_map[ caller.attr('href') ]["submenu"] == true ) {
					$("#submenu").css("height", menu_map[ caller.attr('href') ]["submenu_height"]);
					$("#submenu").slideDown("slow");
					$("#submenucontent").load( caller.attr('href') + ' .asubmenu');
					$(".asubmenu a").live("click", function(event) {
						event.preventDefault();
						change_subpage(this);
					});
				}
				// FB-WALL
				$('#fb_wall').fbWall({ id:'136661883036792',accessToken:'206158599425293|401fe017a4519099d264397a.1-100000101843055|8Pi9d6pJa2fagns9d1q2hRNraRE'});
			});
			if(typeof subpage == "undefined") { mypage = ".content"; }
			else mypage = "#" + subpage;
			$("#content").load( caller.attr('href') + ' ' + mypage);
		});
	});
}

function show_other_page(page) {
	$("#content").load( page.attr('href') + " .content" );
	$("#contentcontainer").slideDown("slow");
	$(".menuentry").hide();
	$("#logo").hide();
	$(".logo_text").hide();
	current_page["menu_entry"] = "other";
	current_page["menu_map"] = new Object;
	current_page["menu_map"]["page"] = new Object;
	current_page["menu_map"]["page"] = page.attr('href');
}

function hide_page( callback ) {
	$("#submenu").slideUp("slow");
	$("#contentcontainer").slideUp("fast", function() {
		$("#logo").slideDown("fast");
		$(".logo_text").show();
		$(".menuentry").slideDown();
		if( current_page["menu_entry"] != "other" ) move_menu_entry_back(current_page, callback);
		// den click-handler entfernen
		$(".asubmenu a").die();
	});
}

function change_subpage(page) {
	$(".asubmenu a").css("text-decoration", "none");
	$(".asubmenu a").css("color", "#a72210");
	$(page).css("text-decoration", "underline");
	$(page).css("color", "#fff");

	// IE7 bugfix, gibt keine relativen href's zurück...   
	// wir haben: .content und #id! deswegen nach beidem spliten.
	var myArray1 = $(page).attr('href').split("#");
	var myHref1 = myArray1[ myArray1.length - 1 ];
	myArray2 = myHref1.split(".");
	if( myArray2.length > 1 ) {
		var myHref = "." + myArray2[ myArray2.length - 1 ];
	}
	else {
		var myHref = "#" + myArray2[ 0 ];
	}

	$("#contentcontainer").slideUp("slow", function() {
		$("#content").load( current_page["menu_map"]["page"] + " " + myHref, function() {
			$("#contentcontainer").slideDown("slow");
		});
	});
}

/* IE7 bugfix
 * vorsicht: gibt nur den dateinamen, nicht den echten relativen pfad aus!
*/
function getHrefFile( link ) {
	var myArray = link.split("/");
	return myArray[ myArray.length - 1 ];
}

$(document).ready(function() {
	/* dont cache! */
	$.ajaxSetup({cache:false});

   $(".menuentry a").click(function(event) {
           event.preventDefault();
                if( $("#contentcontainer").is(":hidden") ) show_page($(this));
   });

	// wir checken nicht mehr nach http://, sondern nach class="outside"
	$("#content a:not([href^='mailto'], [href$='.jpg'], .outside)").live("click", function(event) {
		event.preventDefault();
		var link = $(this).attr('href').split("#");
		link[0] = getHrefFile( link[0] );
		if( link[0] == current_page["menu_map"]["page"] )
			change_subpage( $(".asubmenu a[href$='#" + link[1] + "']") );
		else {
			hide_page(function () {
				show_page($(".menuentry a[href='" + link[0] + "']"), link[1] );
			});
		}
	});

        $("#content area").live("click", function(event) {
                event.preventDefault();
                var link = $(this).attr('href').split("#");
                if( link[0] == current_page["menu_map"]["page"] )
                        change_subpage( $(".asubmenu a[href='#" + link[1] + "']") );
                else {
                        hide_page(function () {
                                show_page($(".menuentry a[href='" + link[0] + "']"), link[1] );
                        });
                }
        });

        $("#content a[href$='.jpg']:not(.outside)").live("click", function (event) {
                event.preventDefault();
                $("#image_overlay").slideDown("fast");
                $("#image_overlay").html('<a href="#" id="close_image"><img src="' + $(this).attr("href") + '" /></a>');

                $("#close_image").live("click", function (event) {
                        event.preventDefault();
                        $("#image_overlay").slideUp();
                        $("#close_image").die();
                });
        });

   $("#close_page").click(function(event) {
           event.preventDefault();
           hide_page();
   });

   $(".other_pages a").click(function(event) {
           event.preventDefault();
           var thispage = this;
           if( $("#contentcontainer").is(":visible") )
                   hide_page( function () {
                                show_other_page($(thispage));
                        });
                else show_other_page($(this));
        });

        $("#bestellform select").live("click", function () {
                $("select option:selected").each(function () {
                        $("#gesamtpreis").text( (parseInt($(this).text()) * ticketpreis).toFixed(2).toString().replace(".",",") );
                        $("#bestellform input[name='quantity_1']").val( $(this).text() );
                        if( parseInt($(this).text()) > 1 )
                                $("#mehrzahl").text("n");
                        else $("#mehrzahl").text("");
                });
        });

        /* doppelt, wegen ie-bug */
        $("#bestellform select").live("change", function () {
                $("select option:selected").each(function () {
                        $("#gesamtpreis").text( (parseInt($(this).text()) * ticketpreis).toFixed(2).toString().replace(".",",") );
                        $("#bestellform input[name='quantity_1']").val( $(this).text() );
                        if( parseInt($(this).text()) > 1 )
                                $("#mehrzahl").text("n");
                        else $("#mehrzahl").text("");
                });
        });

        $("#bestellform input[name='send']").live("click", function (event) {
                var dosubmit = true;
                $("#bestellform input[name='send']").attr("disabled", true);
                $.ajax({
                        async: false,
                        type: "POST",
                        url: "scripts/tickets.php",
                        data: "first_name=" + $("#bestellform input[name='first_name']").val() + "&last_name=" + $("#bestellform input[name='last_name']").val() +
                                        "&adresse=" + $("#bestellform input[name='address_street']").val() + "&plz=" + $("#bestellform input[name='address_zip']").val() +
                                        "&ort=" + $("#bestellform input[name='address_city']").val() + "&mail=" + $("#bestellform input[name='payer_mail']").val() +
                                        "&newsletter=" + $("#bestellform input[name='newsletter']:checked").val() + "&anzahl=" + $("#bestellform select[name='anzahl'] option:selected").text() +
                                        "&zahlungsmethode=" + $("#bestellform input[name='zahlungsmethode']:checked").val(),
                        success: function (answer) {
                                var ok_check = answer.split("#");
                                if(ok_check[0] != "OK") {
                                        dosubmit = false;
                                        $("#response").html(answer);
                                        $("#bestellform input[name='send']").attr("disabled", false);
                                }
                                else {
                                        $("#bestellform input[name='invoice']").val( ok_check[1] );
                                }
                        }
                });

                if(dosubmit) {
                        $("#bestellform").submit();
                }
        });

        $("#newsletter_form").live("submit", function (event) {
                event.preventDefault();
                $("#newsletter_form input[name='send']").attr("disabled", true);

                $.ajax({
                        url: "scripts/newsletter.php",
                        type: "POST",
                        data: "mail=" + $("#newsletter_form input[name='mail']").val(),
                        success: function (answer) {
                                $("#response").html(answer);
                                $("#newsletter_form input[name='send']").attr("disabled", false);
                        }
                })
        });


        $("#gewinnspiel_form input[name='send']").live("click", function (event) {
                $("#gewinnspiel_form input[name='send']").attr("disabled", true);
                $("#response").html('Bitte warten...');
                $.ajax({
                        url: "scripts/gewinnspiel.php",
                        type: "POST",
                        data: "name=" + $("#gewinnspiel_form input[name='name']").val() + "&adresse=" + $("#gewinnspiel_form input[name='adresse']").val() +
                                                                        "&plz=" + $("#gewinnspiel_form input[name='plz']").val() + "&ort=" + $("#gewinnspiel_form input[name='ort']").val() +
                                                                        "&mail=" + $("#gewinnspiel_form input[name='mail']").val() +
                                                                        "&newsletter=" + $("#gewinnspiel_form input[name='newsletter']:checked").val(),
                        success: function (answer) {
                                $("#response").html(answer);
                                $("#gewinnspiel_form input[name='send']").attr("disabled", false);
                        }
                });
        });

        // falls von den einzelseiten weitergeleitet wird, wird hiermit die gefragte seite geladen
        var url = window.location.href.split("?");
        if( url[1] != null ) {
                from = url[1].split("=");
                if(from[0] == "page") {
                        // plakat verstecken
                        /*$("#plakat_overlay").hide();
                        $("#close_plakat").die(); */

                        if(menu_map[ from[1] ] != null)
                                show_page($(".menuentry a[href='" + from[1] + "']") );
                        else show_other_page( $("a[href='" + from[1] + "']") );
                }
        }

        /*
        // PLAKAT
        $("#close_plakat").live("click", function (event) {
                event.preventDefault();
                $("#plakat_overlay").slideUp();
                $("#close_plakat").die();
        });*/

});
