$(document).ready(function() {	

	$('#downloadcenter').fileTree({
		root: '/some/folder/', 
		script: '/ajax/downloadcenter_list.php',
		expandSpeed: 1000,
        collapseSpeed: 1000,
        multiFolder: false
	 },
	 function(file) {
		 var getString = file;
		 var getArray = getString.split('||');
		 var downloadLink = "/tmpDownload/"+getArray[1];
		 var fileID = getArray[0]
		 $.ajax({
			   type: "POST",
			   url: "/ajax/downloadcenter_file.php",
			   data: ({id: fileID}),
			   dataType: "html",
			   success: function(html) {
				    $('#mediaInfo').html(html);
			   }
			 });
		 $('#mediaInfo').dialog({
				height: 300,
				width: 400,
				modal: true,
				buttons: {
					"Download": function() {
						window.open(downloadLink);
					},
					"Abbrechen": function() {
						$( this ).dialog( "close" );
					}
				}
		 
		 });
	}
	);
		
	$("a").live('focus', function(event){
		this.blur();
	});
	$(".zoom").lightBox();
	$(".detailZoom").lightBox();	
	$("#navigation ul.menulevel1").find('a').first().addClass("home");
	$("#navigation ul.menulevel1").find('a').first().html("");
	$("#navigation ul.menulevel1 li:last-child").find("a").addClass("last");
	
	$("#navigation li.menulevel1").hover(
		function(){
			$(this).find('ul').first().slideDown(150);
		},
		function(){
			$(this).find('ul').first().css("display","none");
		}
	);
	
	$("#navigation li.menulevel2").live('mouseover', function() {		
		$(this).addClass("showLevel3");		
	});
	$("#navigation li.menulevel2").live('mouseout', function() {		
		$(this).removeClass("showLevel3");		
	});	
	
	
	$("#navigation li").live('mouseover', function() {
		$(this).addClass("active");
	});
	$("#navigation li").live('mouseout', function() {
		$(this).removeClass("active");
	});
	
	/* Navigation Linke Seite */
	
	if ($("#leftNavigation").find('ul.menulevel1 li.menucurrent ul.menulevel2').length > 0){
		$("#leftNavigation li.menulevel1").css("display","none");
		$("#leftNavigation li.menucurrent").css("display","block");
		$("#leftNavigation #expand").css("display","block");
	}
	
	$("#leftNavigation #expand").live('click', function() {
		$("#leftNavigation li.menulevel1").css("display","block");
		$("#leftNavigation #close").css("display","block");
		$("#leftNavigation #expand").css("display","none");
	});
	
	$("#leftNavigation #close").live('click', function() {
		$("#leftNavigation li.menulevel1").css("display","none");
		$("#leftNavigation li.menucurrent").css("display","block");
		$("#leftNavigation #close").css("display","none");
		$("#leftNavigation #expand").css("display","block");
	});
	
	/* EOF Linke Seite */
	
	$('#headerImage img').attr('rel','');
	$('#headerImage a:first').addClass('show');
	slideShow();
	
	$("#themenBox").tabs({
		fx:{
			opacity: "toggle"
		},
		event: "mouseover"
	})
	.tabs("rotate", 8000, true); 
	
	$("input[type=text]").live('click', function (){
		this.value = "";
	});
	$("input[type=password]").live('click', function (){
		this.value = "";
	});
	
	$("input[type=text]").addClass("inputText");
	$("input[type=password]").addClass("inputText");
	$("input[type=checkbox]").addClass("inputCheckbox");
	$("input[type=radio]").addClass("inputRadio"); 
	$("input[type=submit]").addClass("inputButton");
	$("input[type=reset]").addClass("inputButton");
	$("input[type=button]").addClass("inputButton");
	$("select").addClass("selectMenu");
	
});

function rmTooltip(imgUrl,txt) {
	$("div#tooltip p").empty();
	$('.ext_sonst a').tooltip({
		delay: 0,
		showURL: false,
		bodyHandler: function() {
			return true;
		}
	});
	$('.ext_pdf a').tooltip({
		delay: 0,
		showURL: false,
		bodyHandler: function() {
			return $("<img/>").attr({src: "/images/icon_pdf.png",style: "width: 128px"});
		}
	});
	$('.ext_zip a').tooltip({
		delay: 0,
		showURL: false,
		bodyHandler: function() {
			return $("<img/>").attr({src: "/images/icon_zip.png",style: "width: 128px"});
		}
	});
	$('.ext_jpg a').tooltip({
		delay: 0,
		showURL: false,
		bodyHandler: function() {
			return $("<img/>").attr({src: imgUrl,style: "width: 128px"});
		}
	});
	$('.ext_png a').tooltip({
		delay: 0,
		showURL: false,
		bodyHandler: function() {
			return $("<img/>").attr({src: imgUrl,style: "width: 128px"});
		}
	});
	$('.ext_gif a').tooltip({
		delay: 0,
		showURL: false,
		bodyHandler: function() {
			return $("<img/>").attr({src: imgUrl,style: "width: 150px"});
		}
	});
	$('#tooltip').append('<p id="tooltipDescription">'+txt+'</p>');
}

var canAnimate = 1;
function showHideSchnelleinstieg(){
	if ($("#quickNavigationEinstieg").hasClass("close")) {
		$("#quickNavigationEinstieg").removeClass("close");
		$("#quickNavigationEinstieg").addClass("open");
		$("#schnelleinstieg").css("display","none");
	}else{
		$("#quickNavigationEinstieg").removeClass("open");
		$("#quickNavigationEinstieg").addClass("close");
		$("#schnelleinstieg").css("display","block");
	}
}


function checkMitgliedsnummer(elementID) {
	var mglNr = $("#"+elementID).val();
	if (mglNr.match(/^08.*/)) {
		$("#"+elementID).attr('value',mglNr.replace(/^08/,""));		
		$( "#mitgliedNrMessage" ).dialog({
			draggable: false,
			modal: true,
			resizeable: false
		});
		setTimeout(500);
	}
	return true;	
}

function showHilfe(){
	$( "#hilfe" ).dialog({
		draggable: false,
		modal: true,
		resizeable: false
	});
}

function rm_swap_image (obj, show) {
	var basePath = "/images";
	var baseName = obj.src;
	
	var elements = baseName.split("/");
	var len = elements.length;
	var element = elements[len-1];
	
	var fileDis = element.split(".");
	var fileName = fileDis[0];
	var fileExt = fileDis[1];
	
	if (show == 0) {
		var newFileName = fileName.split("__");
		obj.src = basePath + "/" + newFileName[0] + "." + fileExt;
	} else {
		obj.src = basePath + "/" + fileName + "__hover." + fileExt;
	}
}

function followValue(id) {
		var url = $('select#'+id).val();
		location.href = url;
		return false;
}

function followURL(url) {
	location.href = url;
	return false;
}

function passwortVergessen(){
	var mglnr = $( "#mglnr" ),
	mail = $( "#mail" ),
	vorname = $( "#vorname" ),
	nachname = $( "#nachname" ),
	strasse = $( "#strasse" ),
	plz = $( "#plz" ),
	postort = $( "#postort" ),
	telefon = $( "#telefon" ),
	allFields = $( [] ).add( mglnr ).add( mail ).add( vorname ).add( nachname ).add( strasse ).add( plz ).add( postort ).add( telefon ),
	tips = $(".validateTips");

	$( "#passwort-vergessen" ).dialog({
		height: 300,
		width: 400,
		modal: true,
		buttons: {
			"Senden": function() {
				checkMitgliedsnummer('mglnr');
				var bValid = true;
				allFields.removeClass("ui-state-error");
				bValid = bValid && checkMglNrPwV(mglnr,"Bei Mitgliedsnummer ");
				bValid = bValid && checkLengthMGL(mglnr,"Die Mitgliedsnummer ","9");
				bValid = bValid && checkFilled( mglnr, "Die Mitgliedsnummer ");
				bValid = bValid && checkFilled( mail, "Die E-Mail Adresse ");
				bValid = bValid && isValidEmailAddress( mail, "Die E-Mail Adresse "  );
				bValid = bValid && checkFilled( vorname, "Der Vorname ");
				bValid = bValid && checkFilled( nachname, "Der Nachname ");
				bValid = bValid && checkFilled( strasse, "Die Strasse ");
				bValid = bValid && checkFilled( plz, "Die Postleitzahl ");
				bValid = bValid && checkFilled( postort, "Der Postort ");
				bValid = bValid && checkFilled( telefon, "Die Telefonnummer ");

				if (bValid) {					
					$('#passForgot').submit();	
				}
			},
			"Abbrechen": function() {
				$( this ).dialog( "close" );
			}
		},
		close: function() {
			allFields.val( "" ).removeClass( "ui-state-error" );
		}
	});
	
	$("#passwort-vergessen").keyup(function(e) {
	    if (e.keyCode == 13) {
	    	checkMitgliedsnummer('mglnr');
	    	var bValid = true;
			allFields.removeClass("ui-state-error");
			bValid = bValid && checkMglNrPwV(mglnr,"Bei Mitgliedsnummer ");
			bValid = bValid && checkLengthMGL(mglnr,"Die Mitgliedsnummer ","9");
			bValid = bValid && checkFilled( mglnr, "Die Mitgliedsnummer ");
			bValid = bValid && checkFilled( mail, "Die E-Mail Adresse ");
			bValid = bValid && checkFilled( vorname, "Der Vorname ");
			bValid = bValid && checkFilled( nachname, "Der Nachname ");
			bValid = bValid && checkFilled( strasse, "Die Strasse ");
			bValid = bValid && checkFilled( plz, "Die Postleitzahl ");
			bValid = bValid && checkFilled( postort, "Der Portort ");
			bValid = bValid && checkFilled( telefon, "Die Telefonnummer ");
	    	if (bValid) {					
				$('#loginForm').submit();	
			}
	    }
	});
	return false;
}

function updateTips( t ) {
	$(".validateTips")
		.text(t)
		.addClass( "ui-state-highlight" );
	setTimeout(function() {
		$(".validateTips").removeClass( "ui-state-highlight", 1500 );
	}, 500 );
}

function checkLengthMGL( o, n, max ) {
	if ( o.val().length > max || o.val().length < max ) {
		o.addClass( "ui-state-error" );
		updateTips( "" + n + " muss " + max + " Zeichen enthalten." );
		return false;
	} else {
		return true;
	}
}

function isValidEmailAddress(emailAddress, text) {
	var pattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;	
	var mailCheck =  pattern.test(emailAddress.val());
	if (mailCheck == false) {
		emailAddress.addClass( "ui-state-error" );
		updateTips( "Bitte tragen Sie eine korrekte E-Mailadresse ein." );
		return false;
	}else{
		return true;
	}
};

function checkLength( o, n, min, max ) {
	if ( o.val().length > max || o.val().length < min ) {
		o.addClass( "ui-state-error" );
		updateTips( "" + n + " muss zwischen " +
			min + " und " + max + " Zeichen enthalten." );
		return false;
	} else {
		return true;
	}
}

function checkFilled( o, n) {
	if ( o.val()== "" ) {
		o.addClass( "ui-state-error" );
		updateTips( n + "ist ein Pflichtfeld" );
		return false;
	} else {
		return true;
	}
}

function checkMglNrPwV( o, n) {
	var eingabe = o.val();	
	if (isNaN(eingabe)===true) {
		o.addClass( "ui-state-error" );
		updateTips( n + "bitte nur Zahlen eintragen" );
		return false;
	} else {
		return true;
	}
}

function showEmpfehlenDialog() {
	
	var re_name = $("#re_name"),
	re_mail = $("#re_mail"),
	name = $("#ihr_name"), 
	mail = $("#ihre_email")
	kommentar = $("#kommentar")
	allFields = $([]).add(re_name).add(re_mail).add(name).add(mail).add(kommentar),
	tips = $(".validateTips");
	
	$("#weiterempfehlen" ).dialog({ 
		modal: true,
		title: "Beitrag empfehlen",
		width: 580,
		buttons: {
			"Seite weiterempfehlen": function() {
				var bValid = true;
				allFields.removeClass("ui-state-error");
				bValid = bValid && checkFilled(re_name,"Der Name ");
				bValid = bValid && checkFilled(re_mail,"Die E-Mail Adresse ");
				bValid = bValid && checkFilled(name,"Ihr Name ");
				bValid = bValid && checkFilled(mail,"Ihre E-Mail Adresse ");
				bValid = bValid && checkFilled(kommentar,"Das Kommentar ");
		    	if (bValid) {					
					$('#artikelversand').submit();	
				}
			},
			"Abbrechen": function() {
				$( this ).dialog( "close" );
			}
		},
		close: function() {
			allFields.val( "" ).removeClass( "ui-state-error" );
		}
	});
}

function bbvScrollRight(items,container,list) {
	if (!canAnimate) {
		return;
	}
	canAnimate = 0;
	var numItems = $('ul#'+list).children().length;
	var numScreens = Math.ceil(numItems / items);
	
	var containerWidth = $('div#'+container).width();
	var currentPos = $('ul#'+list).position().left;
	
	//alert(currentPos + ' - ' + (numScreens*containerWidth));
	if (currentPos == 0 - ((numScreens-1)*containerWidth)) {
		//alert("foo");
		$('ul#'+list).css('left', containerWidth);
		currentPos = containerWidth;
	}
	
	var newPos = currentPos - containerWidth;
	$('ul#'+list).animate({
		left: newPos
	}, function() {
		canAnimate = 1
	});
}

function bbvScrollLeft(items,container,list) {
	if (!canAnimate) {
		return;
	}
	canAnimate = 0;
	var numItems = $('ul#'+list).children().length;
	var numScreens = Math.ceil(numItems / items);
	
	var containerWidth = $('div#'+container).width();
	var currentPos = $('ul#'+list).position().left;
	
	if (currentPos == 0) {
		var newPos = 0 - (numScreens*containerWidth);
		$('ul#'+list).css('left', newPos);
		currentPos = $('ul#'+list).position().left;
		
	}
	var newPos = currentPos + containerWidth;
	$('ul#'+list).animate({
		left: newPos
	}, function() {
		canAnimate = 1
	});
}

function slideShow() {
    //Set the opacity of all images to 0
    $('#headerImage a').css({opacity: 0.0});

    //Get the first image and display it (set it to full opacity)
    $('#headerImage a:first').css({opacity: 1.0});

    //Set the caption background to semi-transparent
   // $('#headerImage .caption').css({opacity: 0.7});

    //Resize the width of the caption according to the image width
    $('#headerImage .caption').css({width: $('#headerImage a').find('img').css('width')});

    //Get the caption of the first image from REL attribute and display it
    $('#headerImage .content').html($('#headerImage a:first').attr('rel'))
    .animate({opacity: 0.7}, 400);

    //Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
    setInterval('slideShowGallery()',11000);
}

function slideShowGallery() {
    //if no IMGs have the show class, grab the first image
    var current = ($('#headerImage a.show')?  $('#headerImage a.show') : $('#headerImage a:first'));

    //Get next image, if it reached the end of the slideshow, rotate it back to the first image
    var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#headerImage a:first') :current.next()) : $('#headerImage a:first'));   

    //Get next image caption
    var caption = next.attr('rel'); 

    //Set the fade in effect for the next image, show class has higher z-index
    next.css({opacity: 0})
    .addClass('show')
    .animate({opacity: 1.0}, 1000)
    .css("display","block");

    //Hide the current image
    current.animate({opacity: 0}, 1000)
    .removeClass('show')
    .css("display","none");

    //Set the opacity to 0 and height to 1px
   // $('#gallery .caption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 }); 

    //Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
    $('#headerImage .caption').animate({opacity: 1.0},100 ).animate({height: '30px'},1 );

    //Display the content
    $('#headerImage .content').html(caption);
}
