function showIframeDialog(title,url,w,h){
	$("#dialogbase_iframe").dialog({ title: title, width:w, height:h });
	$("#dialogbase_iframe").css({padding:'0 0 0 0',background:'url(assets/img/ajax-loader_blue.gif) no-repeat center center'});
	$("#modaldialogiframe").attr("src",url);
	$("#dialogbase_iframe").dialog("open");
	return false;
}
function writeFlash(p) {
	writeEmbed(
		'D27CDB6E-AE6D-11cf-96B8-444553540000',
		'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0',
		'application/x-shockwave-flash',
		p
	);
}
function writeShockWave(p) {
	writeEmbed(
	'166B1BCA-3F9C-11CF-8075-444553540000',
	'http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0',
	'application/x-director',
		p
	);
}
function writeQuickTime(p) {
	writeEmbed(
		'02BF25D5-8C17-4B23-BC80-D3488ABDDC6B',
		'http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0',
		'video/quicktime',
		p
	);
}
function writeRealMedia(p) {
	writeEmbed(
		'CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA',
		'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0',
		'audio/x-pn-realaudio-plugin',
		p
	);
}
function writeWindowsMedia(p) {
	p.url = p.src;
	writeEmbed(
		'6BF52A52-394A-11D3-B153-00C04F79FAA6',
		'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701',
		'application/x-mplayer2',
		p
	);
}
function writeEmbed(cls, cb, mt, p) {
	var h = '', n;

	h += '<object classid="clsid:' + cls + '" codebase="' + cb + '"';
	h += typeof(p.id) != "undefined" ? 'id="' + p.id + '"' : '';
	h += typeof(p.name) != "undefined" ? 'name="' + p.name + '"' : '';
	h += typeof(p.width) != "undefined" ? 'width="' + p.width + '"' : '';
	h += typeof(p.height) != "undefined" ? 'height="' + p.height + '"' : '';
	h += typeof(p.align) != "undefined" ? 'align="' + p.align + '"' : '';
	h += '>';

	for (n in p)
		h += '<param name="' + n + '" value="' + p[n] + '">';

	h += '<embed type="' + mt + '"';

	for (n in p)
		h += n + '="' + p[n] + '" ';

	h += '></embed></object>';

	document.write(h);
}
this.imagePreview = function(){	
	/* CONFIG */
		
		xOffset = 50;
		yOffset = 20;
		
	/* END CONFIG */
	$("img.preview").hover(function(e){
		this.t = this.title;
		this.title = "";
		var c = (this.t != "") ? '<div class="preview_title">' + this.t + '</div>' : "";	
		var _xOffset = xOffset;
		var _yOffset = yOffset;
		$("#preview").remove();
		$("body").append("<div id='preview'><img src='"+ this.alt +"' alt='Image preview' />"+ c +"</div>");								 
		if($(document).width()<e.pageX+xOffset+$("#preview").width()+$("img.preview").width()){
			_xOffset=($("#preview").width()+xOffset)*(-1);
		}
		_yOffset=($("#preview").height()+yOffset)*(-0.5);
		
		$("#preview")
			.css("top",(e.pageY + _yOffset) + "px")
			.css("left",(e.pageX + _xOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });
	
	$("img.preview").mousemove(function(e){
		var _xOffset = xOffset;
		var _yOffset = yOffset;
		if($(document).width()<e.pageX+xOffset+$("#preview").width()+$("img.preview").width()){
			_xOffset=($("#preview").width()+xOffset)*(-1);
		}
		_yOffset=($("#preview").height()+yOffset)*(-0.5);
		
		$("#preview")
			.css("top",(e.pageY + _yOffset) + "px")
			.css("left",(e.pageX + _xOffset) + "px");
	});

};
$(document).ready(function(){
	
	$('.pngfix').pngFix();
	$('.s_icons').pngFix();
	$('#language').pngFix();
	imagePreview();
	
	$(".ellipsis").ellipsis();
	// Menu
	/*
	$('ul.mainnav').superfish({ 
		animation: {height:'show'},   // slide-down effect without fade-in 
		delay:     1200               // 1.2 second delay on mouseout 
	});
	*/

	$("ul.mainnav").supersubs({ 
	    minWidth:    14,   // minimum width of sub-menus in em units 
	    maxWidth:    15,   // maximum width of sub-menus in em units 
	    extraWidth:  1     // extra width can ensure lines don't sometimes turn over 
	                       // due to slight rounding differences and font-family 
	}).superfish({
		delay:       0,                            // one second delay on mouseout 
		animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
		speed:       'fast',
	}).find('ul').bgIframe({opacity:false});  // call supersubs first, then superfish, so that subs are 
	                 // not display:none when measuring. Call before initialising 
	                 // containing tabs for same reason. 
	
	$("ul.mainnav").css("marginLeft",Math.floor(962-$("ul.mainnav").width())/2);
	//alert("marginLeft Menu:"+ Math.floor(962-$("ul.mainnav").width())/2);
	
	// Creating custom :external selector
	$.expr[':'].external = function(obj){
	    return !obj.href.match(/^mailto\:/) && !obj.href.match(/^\.pdf/) && obj.href
	            && (obj.hostname != location.hostname);
	};
	// Creating custom :external selector
	$.expr[':'].maillink = function(obj){
	    return obj.href.match(/^mailto\:/)
	};
	// Creating custom :external selector
	$.expr[':'].pdflink = function(obj){
	    return obj.href.match(/^\.pdf/)
	};

	// Add 'external' CSS class to all special links
	$('a:external').not($('div.s_icons a')).addClass('external-link');
	$('a:maillink').addClass('mail-link');
	$('a:pdflink').removeClass('external-link').addClass('pdf-link');
	$('a[href$=\\.pdf]').removeClass('external-link').addClass('pdf-link');
	$('a[href$=\\.zip]').removeClass('external-link').addClass('pdf-link');
	
	$("a[rel^='lightbox']").prettyPhoto({animation_speed:'normal',theme:'facebook',slideshow:3000, autoplay: true,social_tools:'',default_width: 700, default_height: 400,});
	
	$("#breadCrumb").jBreadCrumb();
	
	$('div.content a[title]').qtip({
	   content: {
	      attr: 'title'
	   },
		position: {
			my: 'top center', 
			at: 'bottom center',
		 	viewport: $(window),
			adjust: {
			         x: -10
			}
		   }
	})
	// categotry list:
	
	$("div.category_commalist").each(function(){
		//$(this).innerWrap('<div class="morecat_container" />');
		var height=$(this).height();
		if(height>50){
			$(this).height(50);
			$(this).addClass('morecategories');
			$(this).css({overflow:'hidden'});
			$(this).hover(function(){
				$(this).removeClass('morecategories');
				$(this).animate({
					height: height
				}, 500, function() {
				    // Animation complete.
				});
				//$(this).css({height:"auto"});
			},function(){
				/*
				$(this).animate({
					height: 45
				}, 500, function() {
				    // Animation complete.
					$(this).addClass('morecategories');
				});
				*/
			});
		}
		
	});

	// FOLDED CONTENT
	$(".foldme p.fold_action").prepend('<img style="margin-top:3px;float:right;" src="assets/img/arrows/white_small_down.gif">');
	$(".foldme p.fold_content:not(:first)").hide();
	$(".foldme p.fold_action").click(function(){
		$(".foldme p.fold_content:visible").slideUp("slow");
		$(this).next("p.fold_content").slideDown("fast");
		return false;
	});
	
	$(".foldme h2.fold_action").prepend('<img style="margin-top:3px;margin-right:10px;float:left;" src="assets/img/arrows/white_small_down.gif">');
	$(".foldme div.fold_content:not(:first)").hide();
	$(".foldme h2.fold_action").click(function(){
		$(".foldme div.fold_content:visible").slideUp("slow");
		$(this).next("div.fold_content").slideDown("fast");
		return false;
	});
	
	$(".foldme2 div.fold_action2").prepend('<img style="margin-top:3px;float:right;" src="assets/img/arrows/white_small_down.gif">');
	$(".foldme2 div.fold_content2").hide();
	
	$(".foldme2 div.fold_content2").each(function() {
			if($(this).height()>200){
				$(this).css("overflow","auto");
				$(this).height(200);
			}
	});
	
	$(".foldme2 div.fold_content2:not(:first)").hide();
	$(".foldme2 div.fold_action2").click(function(){
		$(".foldme2 div.fold_content2:visible").slideUp("slow");
		$(this).next("div.fold_content2").slideDown("fast");
		return false;
	});
	
	$(".foldme3 p.fold_action").prepend('<img style="margin-top:38px;float:right;" src="assets/img/arrows/white_small_down.gif">');
	$(".foldme3 p.fold_content").wrap('<div class="foldcontent200inner">');
	$(".foldme3 div.foldcontent200inner").wrap('<div class="foldcontent200">');
	$(".foldme3 div.foldcontent200:not(:first)").hide();
	
	$(".foldme3 p.fold_action").click(function(){
		$(".foldme3 div.foldcontent200:visible").slideUp("slow");
		$(this).next("div.foldcontent200:not(:visible)").slideDown("fast");
		return false;
	});
	var limitedheight_height=140;
	$("div.limitedcontent").each(function(){
		if($(this).height()>(limitedheight_height)+40){
			$(this).height(limitedheight_height);
			$(this).after('<a class="limitedcontent_toggler">mehr anzeigen</a><br />');
			
			$(this).next('a.limitedcontent_toggler').css({"text-decoration":"underline","cursor":"pointer"}).click(function(){
				if($(this).prev('div.limitedcontent').height()==limitedheight_height){
					$(this).prev('div.limitedcontent').height('auto');
					$(this).html('weniger anzeigen');
				} else {
					$(this).prev('div.limitedcontent').animate({
					    height: limitedheight_height
					  }, 500, function() {
					    // Animation complete.
					});
					$(this).html('mehr anzeigen');
				}
			});
		} 
	});
	$("div.foldlist ul.container_sub li.item_sub:not(:first,:nth-child(1),:nth-child(2),:nth-child(3),:nth-child(4),:nth-child(5))").hide();
	
	$("div.foldlist ul.container_sub").each(function(){
		$(this).addClass('plus');
		$(this).click(function(){
			if($(this).hasClass('plus')){
				$("div.foldlist ul.container_sub li.item_sub:not(:first,:nth-child(1),:nth-child(2),:nth-child(3),:nth-child(4),:nth-child(5))").slideUp();
				$("div.foldlist ul.container_sub").addClass('plus');
				$(this).children('li.item_sub').slideDown();
				$(this).removeClass('plus');
			} else {
				$(this).children('li.item_sub:not(:first,:nth-child(1),:nth-child(2),:nth-child(3),:nth-child(4),:nth-child(5))').slideUp();
				$(this).addClass('plus');
			}
		});
	});
		
	$("div.formerror").each(function(){
		if($(this).html()!=""){
			//$(this).height('500px');
			//$(this).css({'background':'red'});
			//alert('Fehler:'+$(this).html());
			var dialogtext = 'Sie haben nicht alle Pflichtfelder ausgefüllt:<br><b>'+$(this).html()+'</b>';
			var $dialog = $('<div></div>')
					.html(dialogtext)
					.dialog({
						autoOpen: true,
						modal: true,
						title: 'Unvollständiges Formular',
						buttons: {
										'  Zurück zum Formular  ': function() {
											$( this ).dialog( "close" );
										}
						}
			});

			/*$('#opener').click(function() {
				$dialog.dialog('open');
				// prevent the default action, e.g., following a link
				return false;
			});*/
		}
		
		//alert($(this).innerhtml());
	});
		
	/*  messages - fadeout */
	message_interval = setTimeout(message_fade,2000);
	function message_fade() {
		$('div.successmessage,div.errormessage,div.infomessage').effect('pulsate',{},500,function(){
			$(this).fadeOut(800);
		});
	}
	
	
	
	/*  approve dialog */
	$('a.approve').click(function(){ 
		var t= $(this).attr('title')+'?';
		if (confirm(t)) {
			return true;
		} else {
			return false;
		}
	});
	
	
	
	/* Buttons */
	
	$("a.button").button();
	$("div.content input:submit").button({
		icons: {
			primary: 'ui-icon-gear'
		},
		text: false,
		disabled: false
	});
	$("div.content input:reset").button({
		icons: {
			primary: 'ui-icon-gear'
		},
		text: false,
		disabled: false
	});
	
	//$("button, input:submit.button, a.button", ".button").button();
	
	/* modal iframe dialogs*/
	
	$("#dialogbase_iframe").dialog({
		autoOpen: false,
		modal: true,
		height: 500,
		width: 500
	});
	$('a.showIframeDialog').click(function(){ 
		var w=700;
		var h=300;
		var url=$(this).attr('href');
		if($(this).attr('alt')!=""){
			url=$(this).attr('alt');
		}
		if($(this).attr('rel')!=""){
			var dimensions=$(this).attr('rel').split("x");
			if(parseInt(dimensions[0])>100){ w=parseInt(dimensions[0]); }
			if(parseInt(dimensions[1])>100){ h=parseInt(dimensions[1]); }
			//alert('rel: '+$(this).attr('rel')+' w'+dimensions[0]+' h'+dimensions[1]);
		}
		showIframeDialog($(this).attr('title'),url,w,h);
		return false; 
	});
	
	
	/* HEIGHT GROUPS */
	// LAST!!!
	
	//$('div.heightcontainer1a div.container').addClass('hg1');
	//$('div.heightcontainer1b').addClass('hg1');
	//$("div.hg1").equalHeights().css({overflow:'hidden'});
	
	
	$('iframe.fullwidthiframe').each(function(){
		var parentelement = $(this).parent();
		parentelement.css({overflow:'hidden'});
		$(this).parent().parent().css({overflow:'hidden'});
		$(this).parent().parent().parent().css({overflow:'hidden'});
		var height = $(this).height();
		var width = $(this).width();
		var ratio=height/width;  // 0.5
		//alert(parentelement.width());
		$(this).width(parentelement.width());
		$(this).height(parentelement.width()*ratio);
	});
	
	
	var h2=$('div.heightcontainer1b').height();
	var h1=$('div.heightcontainer1a div.container').height();
	var n = $("a.kbcms_dyn_editlink").length;
	//alert(n);
	if(h1<h2){
		if(n>0){
			// editmode
			$('div.heightcontainer1a div.container').height(h2-57);
		} else {
			$('div.heightcontainer1a div.container').height(h2-20);
		}
		
	} 
	
	$(".heightgroup0").equalHeights().css({overflow:'hidden'});
	$(".heightgroup1").equalHeights().css({overflow:'hidden'});
	$(".heightgroup2").equalHeights().css({overflow:'hidden'});
	$(".heightgroup3").equalHeights().css({overflow:'hidden'});
	/*
	(".minhtmleditor").each(function(){
		$(this).htmlarea({
			toolbar: [
				["cut", "copy", "paste"],
	            ["bold", "italic", "underline"],
	            ["p", "h1", "h2", "h3", "h4", "h5", "h6"]
	        ],
			css: "assets/css/ak_text.css",
		});
	});
	*//*
	$(".minhtmleditor").htmlarea({
		toolbar: [
			["cut", "copy", "paste"],
            ["bold", "italic", "underline"],
            ["p", "h1", "h2", "h3", "h4", "h5", "h6"]
        ],
		css: "assets/css/ak_text.css"
	});*/
});   

