$(document).ready(function()
{
	
	jQuery.fn.foldInOut = function () {
    // hide all
    jQuery(this).find('ul').hide();
    // display if active
    jQuery(this).find('li.active ul').show();
	//alert (jQuery(this).find('li.active ul'));
       
    jQuery(this).find('li').bind("mouseenter", function () {
      if ( jQuery(this).is('.active') ) {		  
		  return false;
	  }
	  jQuery('#nav_main .level_1').find('li.active ul').hide();
	  jQuery('#nav_main .level_1').find('li.active').removeClass("active");
	  	  
	  jQuery(this).addClass('active');
      jQuery("ul", this).show();                
    
	}).bind("mouseleave", function () {           
        //jQuery("UL", this).slideUp(500,function(){jQuery(this).parent().removeClass("active");});
    })
    // unbind sub 
    jQuery(this).find('ul li').unbind('mouseenter');
  	};
	
	jQuery('#nav_main .level_1').foldInOut();
    //-------------------------------------------------------
    /*shows the loading div every time we have an Ajax call*/
    $("#loading").bind("ajaxSend", function(){
   		$(this).show();
		document.getElementById('loadedContent').style.opacity=0.2;
		document.getElementById('loadedContent').style.filter='alpha(opacity=20)';

 	}).bind("ajaxComplete", function(){
   		$(this).hide();
		document.getElementById('loadedContent').style.opacity=1;
		document.getElementById('loadedContent').style.filter='alpha(opacity=100)';
 	});
 	//-------------------------------------------------------
	
	//Get size of images, how many there are, then determin the size of the image reel.
	var imageHeight = $("#pic_flasher").height();
	var imageSum = $("#news_main_list img").size();
	var imageReelHeight = imageHeight * imageSum;
	imageHeight = imageHeight +3.5;

	for (var i = 1; i <= imageSum; i++) {
		$("#pic_flasher_page").append('<a href="#" rel="'+i+'">'+i+'</a>&nbsp;');
	}
	$("#pic_flasher_page").show();
	$("#pic_flasher_page a:first").addClass("active");
	
	//Adjust the image reel to its new size
	$("#news_main_list").css({'height' : imageReelHeight});
	
	//Paging + Slider Function
	rotate = function(){	
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var image_reelPosition = triggerID * imageHeight; //Determines the distance the image reel needs to slide

		$("#pic_flasher_page a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

		//Slider Animation
		$("#news_main_list").animate({ 
			top: -image_reelPosition
		}, 500 );
		
	}; 
	
	//Rotation + Timing Event
	rotateSwitch = function(){		
		play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
			$active = $('#pic_flasher_page a.active').next();
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('#pic_flasher_page a:first'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
		}, 7000); //Timer speed in milliseconds (3 seconds)
	};
	
	//On Click
	$("#pic_flasher_page a").click(function() {
		$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation timer
		return false; //Prevent browser jump to link anchor
	});
	
	rotateSwitch(); //Run function on launch
	imagePreview();
	
})
function ListChange(str){
    var land = '';
	var klasse = '';
	var anz = '';

	land = document.getElementById('SEL_L').value;
	klasse = document.getElementById('SEL_KL').value;
	anz = document.getElementById('SEL_M').value;
	//alert('/cms/templates/erg_rangliste.php?L='+land+'&KL='+klasse+'&TOP='+anz);
	//change the uri inside the load() brackets to load a page relative to your domain
    $("#loadedContent").load('/templates/erg_rangliste.php?L='+land+'&KL='+klasse+'&TOP='+anz+'', function(){
		imagePreview();
	});
}
function ListChangeTurnier(str){
    var land = '';
	var klasse = '';
	var anz = '';
	var bew = '';

	land = document.getElementById('SEL_L').value;
	klasse = document.getElementById('SEL_KL').value;
	anz = document.getElementById('SEL_M').value;
	bew = document.getElementById('SEL_BEW').value;
	//alert('/cms/templates/erg_turnierliste.php?L='+land+'&KL='+klasse+'&TOP='+anz+'&BEW='+bew);
	//change the uri inside the load() brackets to load a page relative to your domain
    $("#loadedContent").load('/templates/erg_turnierliste.php?L='+land+'&KL='+klasse+'&TOP='+anz+'&BEW='+bew);
}
function ListLoadSPr(){
    var land = '';
	var art = '';
	var num = '';

	land = document.getElementById('SEL_L').value;
	art = document.getElementById('SEL_ART').value;
	num = document.getElementById('SEL_NUM').value;
	//change the uri inside the load() brackets to load a page relative to your domain
    $("#loadedContent").load('/templates/erg_spielerpunkte.php?L='+land+'&ART='+art+'&NR='+num);
}
function ListChangeKlasse(){
    var land = '';

	land = document.getElementById('SEL_L').value;
	//change the uri inside the load() brackets to load a page relative to your domain
    $("#loadedContent").load('/templates/erg_klassenwertung.php?L='+land);
}
function ListChangeLiga(){
    var land = '';
	var saison = '';
	var liga = '';
	var runde = '';
	var ansicht = '';

	land = document.getElementById('SEL_L').value;
	saison = document.getElementById('SEL_SA').value;
	liga = document.getElementById('SEL_KZ').value;
	runde = document.getElementById('SEL_RU').value;
	ansicht = document.getElementById('SEL_M').value;
	
	$("#loadedContent").load('/templates/erg_liga.php?L='+land+'&SA='+saison+'&KZ='+liga+'&RU='+runde+'&M='+ansicht);
	
}
this.imagePreview = function(){	
	/* CONFIG */
		
		xOffset = 10;
		yOffset = 30;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.preview").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' width='115px'/>"+ c +"</p>");								 
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};
