
//$ = jQuery.noConflict();

function changeStyle(relId, style) {
	$('#'+relId).attr('href','styles/css/styles/' + style + '.css');
	$('#ui_css').attr('href','styles/css/jquery-themes/ui-lightness/jquery-ui-1.8-' + 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() {
   $('#navcontainer ul').first().addClass('topLevel');
   $("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 {
    	var ancestors = $('#navcontainer a.current').parents('ul').not($('ul.topLevel'));
    	ancestors.prev("a").addClass("currentAncestor");

        //hide all lists not having the current selection
        $("#navcontainer ul ul").not(ancestors).not($("a.current").next("ul")).animate({height: "hide", opacity: "hide"}, 450);
    
        //show all relevant lists
        ancestors.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);
            var hasPlace = $("hasPlace", xml).text();
            if (hasPlace == "1") {
            	$(function(){
                	$.include(["styles/css/googlemaps.css"]);
					LoadMapSearchControl();
            	});
            }
		}
        $("a.email").click(function() {
            return makeEmailReadable(this);
        });
		//alert("5");
		track("/get_content.php?id="+id);
  	},
       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
	});
  }
}

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();
		}
	}
}

function track(url) {
	if (/localhost/.test(window.location) || /192\.168/.test(window.location)) {
		return;
	}
	try {
		var pageTracker = _gat._getTracker("UA-15786793-1");
		pageTracker._setDetectFlash(false);
		pageTracker._trackPageview(url);
	} catch(err) {}
}