
//$ = jQuery.noConflict();

function changeStyle(relId, style) {
	$('#'+relId).attr('href','styles/css/styles/' + style + '.css');

	//notify the session of that style change
		$.ajax({
	   type: 'POST',
   	   url: 'snippets/session.php',
   	   data: 'style=' + style
	});
//	}
}

//register adaptMenu with all relevant links
$(document).ready(function() {
   $("a.menuItem").click(function() {
     adaptMenu(this); return false;
   });
   $("a.email").click(function() {
        return makeEmailReadable(this);
   });
   //$("a.email").attr("href", "x" + $(this).attr("href"));
   $("#db_error").dialog({ 
    modal: true, 
    overlay: { 
        opacity: 0.5, 
        background: "black" 
    } 
   });
   $("#testdialog").dialog({ 
    modal: true, 
    overlay: { 
        opacity: 0.5, 
        background: "black" 
    } 
   });
 });

function adaptMenu(selected) {
    var isjquery12 = $().jquery.substr(0,3) == "1.2";
    //alert(isjquery12);
    
	//alert($(selected).attr("id"));
	var oldId = $("a.current").attr("id");
	var newId = $(selected).attr("id");
	//alert(newId);

	//remove old selection
	$("a.current").removeClass("current");
	$("a.currentAncestor").removeClass("currentAncestor");

	//set new selections
	$(selected).addClass("current");
	
	if (isjquery12){
    	$("#navcontainer ul:has(a[class*=current])").prev("a").addClass("currentAncestor");

        //hide all lists not having the current selection
        $("#navcontainer ul ul").not($("#navcontainer ul:has(a[class*=current])")).not($("a.current").next("ul")).animate({height: "hide", opacity: "hide"}, 450);
    
        //show all relevant lists
        $("ul:has(a[class*=current])").add($("a.current").next("ul")).animate({height: "show", opacity: "show"}, 450);
	} else {
    	$("#navcontainer ul[a.current]").prev("a").addClass("currentAncestor");

        //hide all lists not having the current selection
        $("#navcontainer ul ul").not($("#navcontainer ul[a.current]")).not($("a.current").next("ul")).animate({height: "hide", opacity: "hide"}, 450);
    
        //show all relevant lists
        $("ul[a.current]").add($("a.current").next("ul")).animate({height: "show", opacity: "show"}, 450);
	}

	if (oldId != newId) {
	var id = $(selected).attr("id").replace(/_/,"");
	
	$("#content").html("<img src=\"gfx/jquery_tabs/loading.gif\" alt=\"loading\"></img>");
	
	$.ajax({
	   type: "GET",
   	   url: "get_content.php",
   	   data: "id=" + id + "&r=" + Math.random(),
       dataType: "xml",
       success: function(xml){
       		//alert("success");
		//get the title
		//alert (xml);
		var title = $("title", xml).text();
    		if (title) {
    			window.document.title=title;
    		} else {
    			window.document.title='Prayerfestival.de - Gott liebt dich!';
    		}
		
		//get the image url
		var image = $("image", xml).text();
		//alert(image.text());
		if (image) {
			$("#banner #insert").css('background', 'url(' + image + ') no-repeat');
		} else {
			$("#banner #insert").css('background', 'url(gfx/index2.jpg) no-repeat');
		}
		
		//finally get the content
		var content = $("content", xml).text();
		//alert(content.text());
		if (content) {
			$("#content").html(content);
		} else {
			$("#content").html('<p>F&uuml;r diesen Punkt ist leider kein Inhalt definiert oder es trat ein Fehler beim Laden der Seite auf. Einfach sp&auml;ter wieder versuchen.</p>');	
		}
		
		if (id > 20 && id <30) {
		  //this is a pf subpage
            //window.setTimeout(function(){alert('timeout reached');}, 100);
            $(function(){
                //alert(jQuery.browser.msie);
                $.include("styles/css/jquery_tabs/jquery.tabs.css,styles/css/googlemaps.css");
                if (jQuery.browser.msie) {
                    $.include("styles/css/jquery_tabs/jquery.tabs-ie.css");
                }
                $('#tabcontainer > ul').tabs();
                $.include("scripts/cmxforms.js,scripts/jquery.form.js,scripts/jquery.validate.js,scripts/anmeldung_formular.js", {onload: function(){
                            populateDioezesen(); 
                            LoadMapSearchControl();
                        }
                });
            });
		}
        $("a.email").click(function() {
            return makeEmailReadable(this);
        });
		//alert("5");
  		},
       error: function(xml){ $("#content").html('<p>Es trat ein Fehler beim Laden der Seite auf. Bitte versuch es sp&auml;ter noch einmal.</p>');
           //alert(xml);
       },
       complete: resetLightBox
	});
	$.ajax({
	   type: 'POST',
   	   url: 'snippets/session.php',
   	   data: 'lastid=' + id
	});
  }
}

function makeEmailReadable(link) {
    var value = $(link).attr("href");
    value = value.replace(" AT ", "@").replace(/ PUNKT /g, ".");
    
    $(link).attr("href", value);

    return true;
}

function resetLightBox() {
	//alert("resetLightBox called");
	if ($("a[@rel=lightbox]").size()) {
		if (typeof(myLightbox)=="undefined") {
			//if ($("#lightbox").size()) {
			//	alert("Lightbox macht schmarrn");
			//} else {
			//	alert("initializing lightbox");
			//}
			//initLightbox();
		} else {
			//myLightbox.updateImageList();
		}
	}
}