$.getScript(location.protocol+'//'+location.host+'/js/jquery.base64.js', function() {
	// done loading.
});

//ajax stuff
var htmlstuff = '';

if(slidemode == 'undefined')
{
	var slidemode = false;
}

if(urlbase64coded == 'undefined')
{
	var urlbase64coded = false;
}

// content ersetzen
function setGetContent(elementID, uri, getVars) {

 if(urlbase64coded == true) call_uri = $.base64_decode(uri);
 else call_uri = uri;
 	
 $.ajax({
   
  type: "GET",
  url: call_uri,
  data: getVars,
	cache: false,

  beforeSend: function(){
    // Handle the beforeSend event
  },
  
  success: function(data){
     evalScript(data);

    // Handle the complete event
    $("#"+elementID).replaceWith(data);
    $("#"+elementID).fadeIn(2000);
    initialFancyBox();
  },

  complete: function()
  {
  	setHeadDocument(uri,getVars+'&reloadDocumentInfo=1');
  }
  
 });
	
}

// content ersetzen
function setGetHTMLContent(elementID, uri, getVars, fadeMode, slider) {

 if(urlbase64coded == true) call_uri = $.base64_decode(uri);
 else call_uri = uri;

 $.ajax({
   
  type: "GET",
  url: call_uri,
  data: getVars,
	cache: false,

  beforeSend: function(){
    // Handle the beforeSend event
		
    if(slider == undefined)
    {

			if($.browser.name !== 'microsoft internet explorer' && fadeMode == 'true')
			{
				  	$("#"+elementID).fadeOut(1500);
			}
	  }
	  else
	  {
			
			if($("#"+elementID).css("display") == 'block')
			{

				$("#"+elementID).slideToggle('slow');			

			}

	  	slideshow('@','true');

	  }
	  
  },
  
  success: function(data){

    if(fadeMode == 'true' || slider == 'true') {

	   	htmlstuff = data;
    	fadeInChecker(elementID,slider);

    } else {

			$('#'+elementID).html(data);

			if($.browser.name !== 'microsoft internet explorer' && fadeMode == 'true')
			{
				$("#"+elementID).fadeIn(2000);
			}
	
		}
    initialFancyBox();

  },
  
  complete: function()
  {
  	if (elementID == 'video_box') 
  	{
  		var winWidth = $(".overlayinner").width(); 
			var winHeight = $(".overlayinner").height(); 
  		
  		document.getElementById('video_player_opener').width = winWidth; //$("overlayinner").width();
  		document.getElementById('video_player_opener').height = winHeight; //$("overlayinner").height();
  		$('#inner').empty();
  		
  	}
		
		var pid = call_uri.substr(call_uri.indexOf('?p=', 0)+3, 3);
		//Aktuelles
  	if (pid == '242')
  	{
  		unsetMenuHover();
  		$("#aktuelles a").css("background-position", "left -12px");
    }
    //Termine
    if (pid == '246')
  	{
  		unsetMenuHover();
  		$("#termine a").css("background-position", "left -12px");
    }
    //Clueso&Band
    if (pid == '248')
  	{
  		unsetMenuHover();
  		$("#clueso a").css("background-position", "left -12px");
    }
    //Projekte
    if (pid == '250')
  	{
  		unsetMenuHover();
  		$("#projekte a").css("background-position", "left -12px");
    }
    //Musik
    if (pid == '252')
  	{
  		unsetMenuHover();
  		$("#musik a").css("background-position", "left -12px");
    }
    //Video
    if (pid == '254')
  	{
  		unsetMenuHover();
  		$("#videos a").css("background-position", "left -12px");
    }
    //Shop
    if (pid == '257')
  	{
  		unsetMenuHover();
  		$("#shop a").css("background-position", "left -12px");
    }
  	if (elementID != 'video_box' && elementID != '') $('#video_box').empty();
  	setHeadDocument(uri,getVars+'&reloadDocumentInfo=1');
  }

 });
	
}

function unsetMenuHover() 
{
	$("#aktuelles a").css("background-position", "");
	$("#termine a").css("background-position", "");
	$("#clueso a").css("background-position", "");
	$("#projekte a").css("background-position", "");
	$("#musik a").css("background-position", "");
	$("#videos a").css("background-position", "");
	$("#shop a").css("background-position", "");
}

function fadeInChecker(elementID,slider)
{

	if($("#"+elementID).css("display") != 'none') 
	{
		
		window.setTimeout("fadeInChecker('"+elementID+"','"+slider+"')",1000);

	} 
	else
	{

		if($.browser.name == 'microsoft internet explorer')
		{

    	document.getElementById(elementID).innerHTML = htmlstuff;
			evalScript(htmlstuff);
		}
		else
		{
			$('#'+elementID).html(htmlstuff);
		}
		

		if(slider == 'undefined')
		{

			if($.browser.name !== 'microsoft internet explorer')
			{

					$("#"+elementID).fadeIn(2000);

			}

		}
		else
		{

			$("#"+elementID).slideToggle('slow');
	    initialFancyBox();

		}
		
	}

}
		

function loadImage(elementID,data)
{

 	bgimage_small = data.small;
 	bgimage_medium = data.medium;
 	bgimage_large = data.large;

	$('<img />')
	.attr('src', bgimage_large)
	.attr('style', 'display: none;')
	.load(function() {
		$('<img />')
		.attr('src', bgimage_medium)
		.attr('style', 'display: none;')
		.load(function() {
			$('<img />')
			.attr('src', bgimage_small)
			.attr('style', 'display: none')
			.load(function() { 	
				$("#bgimage").fadeOut(1000);
	        // Your other custom code

			    	setTimeout(
			            function()
			            {
				        BGsize(function () {
					    setTimeout(
				            	function()
						{
					 	       $("#"+elementID).fadeIn(1000);
						},
						1000
					    )
					});
			            },
			            1000
				); 
			});
		});
	});
}

// umleiten
function setRedirect(uri, getVars) {
	 
	 if(urlbase64coded == true) call_uri = $.base64_decode(uri);
	 else call_uri = uri;
	 	
	 $.ajax({
	   
	  type: "GET",
	  url: call_uri,
	  data: getVars,
		cache: false,

	  beforeSend: function(){
	    // Handle the beforeSend event
	  },
	  
	  success: function(data){
	    // Handle the complete event
		if ( data ) {
		  window.location.href = data;
		}
	  }

	 });
		
}

// content ersetzen
function sendPostTEData(elementID, uri) {

 if(urlbase64coded == true) call_uri = $.base64_decode(uri);
 else call_uri = uri;
 	
 $.ajax({
  type: "POST",
  data: $("#"+elementID).serialize(),
  url: call_uri,
	cache: false,
  success: function(data){
  	eval(data);
  }
 });
 
}
 
// content ersetzen
function sendPostData(elementID, uri) {

 if(urlbase64coded == true) call_uri = $.base64_decode(uri);
 else call_uri = uri;
 	
 $.ajax({
  type: "POST",
  data: $("#"+elementID).serialize(),
  url: call_uri,
	cache: false,

  beforeSend: function(){
    // Handle the beforeSend event
        $("#"+elementID).fadeOut(1500, function () {
            //testing
            $(this).html("Loading...").fadeIn(100);
        });
  },

  error: function() {
      $("#"+elementID).fadeOut(1);
  },
  
  success: function(data){
    // Handle the complete event
    $("#"+elementID).fadeOut(100);
    $("#"+elementID).replaceWith(data);
    $("#"+elementID).fadeIn(2000);
    initialFancyBox();
  }
 });
 
}


function slideshow(pos,slider) {
	
	if(slider !== undefined) slidemode = slider;
	
	if(slidemode == "true")
	{
		$.ajax({
		   
		  type: "GET",
		  url: "http://"+location.host+"/cms/?s=clueso_bgimage&mode=aj_getImage",
		  dataType: "json",
		  data: "pos="+pos,
			cache: false,
		
		  beforeSend: function(){
		    // Handle the beforeSend event
			  // $("#bgimage").fadeOut(1500);
		  },
		  
		  success: function(data){

				if(data.pos == 'false')
				{

			  	slidemode = false;
					$(".slideshow_link").attr('slider','false');
		   		loadImage('bgimage',data);

				}
				else
				{					

	     		$(".slideshow_link").attr('sliderpos',data.pos);
		   		loadImage('bgimage',data);
				
					if(!isNaN(data.pos))
					{
	
				    setTimeout
				    (
				            function()
				            {
				            	slideshow(data.pos);
				            },
				            6000
				    ); 
	
				  }
				}

		  }
		
		});	
	}
}


function setHeadDocument(uri,getVars)
{

 if(urlbase64coded == true) uri = $.base64_decode(uri);
 else call_uri = uri;

 $.ajax({
   
  type: "GET",
  url: call_uri,
  dataType: "json",
  data: getVars,
	cache: false,
  
  success: function(data){
  	if(data.title)
  	{
  		document.title = data.title;
  	}
  	if(data.metatags)
  	{
  		document.keywords = data.metatags;
  	}
  	if(data.description)
  	{
  		document.description = data.description;
  	}  	  	
  }

 });	
}

function evalScript(scripts)
{	try
	{	if(scripts != '')	
		{	var script = "";
			scripts = scripts.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, function(){
	       	                         if (scripts !== null) script += arguments[1] + '\n';
 	        	                        return '';});
			if(script) (window.execScript) ? window.execScript(script) : window.setTimeout(script, 0);
		}
		return false;
	}
	catch(e)
	{	alert(e)
	}
}
/** Fix for google chrome 4 beta, which for some reason doesn't send the
  * DOMContentLoaded event to jQuery in large applications.
 **/
(function(){
  if ( /chrome\/4/.test(navigator.userAgent.toLowerCase()) ) {
    jQuery.ready = function(){
      // Make sure that the DOM is not already loaded
      if ( !jQuery.isReady ) {
        // Remember that the DOM is ready
        jQuery.isReady = true;
        // If there are functions bound, to execute
        if ( jQuery.readyList ) {
          var readyList = jQuery.readyList;
          // Reset the list of functions NOW to avoid race conditions
          jQuery.readyList = null;
          // Execute all of them
          jQuery.each( readyList, function(){
              this.call( document, jQuery );
          });
        }
        // Trigger any bound ready events
        jQuery(document).triggerHandler("ready");
      }
    };
    document.addEventListener( "DOMContentLoaded", function(){
        jQuery.ready();
    }, false);
  }
})();

