var content ="";
var id_typ = 0;
var id_utyp = 0;
var id_uutyp = 0;
var id = 0;

var sliderIsOpened = true;
var initSliderWhenOpened = false;

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var url_encode = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

function checkMandatories(formular){
		var fehler=false;
		$(formular).find(".mandatory").each(function(i){

			if($(this).attr("type")=="checkbox"){

				if($(this).attr("checked")== false && $(this).attr("disabled")==false){
					fehler=true;
					try {
						$(this).next().animate( { backgroundColor:"white", color:"#006bb3" }, { queue:true, duration:250 } );
						$(this).next().animate( { backgroundColor:"#ff0000", color:"white" }, { queue:true, duration:250 } );
						$(this).next().animate( { backgroundColor:"white", color:"#006bb3" }, { queue:true, duration:250 } );
						$(this).next().animate( { backgroundColor:"#ff0000", color:"white" }, { queue:true, duration:300 } );
					} catch(e){}
					show_hinweis("<b>Fehler:</b><br>Bitte überprüfen Sie alle Pflichtfelder!");
				} else {
					try {	
						$(this).next().animate( { backgroundColor:"transparent", color:"black" }, { queue:true, duration:250 } );
					} catch(e){}
				}
			} else {
				if($(this).val()=="" && $(this).attr("disabled")==false){
					fehler=true;
					try {
						$(this).animate( { backgroundColor:"white", color:"black" }, { queue:true, duration:250 } );
						$(this).animate( { backgroundColor:"#ff0000", color:"#006bb3" }, { queue:true, duration:250 } );
						$(this).animate( { backgroundColor:"white", color:"#006bb3" }, { queue:true, duration:250 } );
						$(this).animate( { backgroundColor:"#ff0000", color:"white" }, { queue:true, duration:300 } );
					} catch(e){}
				} else {
					try {
						$(this).animate( { backgroundColor:"white", color:"black" }, { queue:true, duration:500 } );
					} catch(e){}
				}
			}
		});
		return fehler;
}



function init(){

		content = $('#content').val();
		id_typ = $('#id_typ').val();
		id_utyp = $('#id_utyp').val();
		id_uutyp = $('#id_uutyp').val();
		id = $('#id').val();
		
		
		if($(".teaser").height() < 50){
			sliderIsOpened = false;
		} 
		
		if( $("#formular_forward_blank").attr("action") != undefined )$("#formular_forward_blank").submit();		
		
		$(".fancy").fancybox();
		
//  kontaktformular________________________________________________

	$("#kontaktformular").submit(function(){
		var fehler=checkMandatories(this);
		
		if(!fehler){
		 /*
		 var inhalte = $(this).find("input, textarea").serialize();
		 
		 $.ajax({
		   type: "POST",
		   url: "lib/js_functions.php",
		   async: false,
		   data: "function=sendMessage&"+inhalte,
		   success: function(data_str){
		   	 var data = eval(data_str);
		   	 if(data.hinweis != "")show_hinweis(data.hinweis);
		   	 if(data.fehler != "")show_hinweis(data.fehler);
		   	 //else $("#kontaktformular").find("input, textarea").val("");
		   }
		 });

*/
		} else {
			show_hinweis("<b>Fehler:</b><br>Bitte überprüfen Sie alle Pflichtfelder!");
			return false;
		}
		
		//
		
	});
//  kontaktformular ende___________________________________________

	if($("#hinweis_text").text() != "")show_hinweis($("#hinweis_text").html());

		
}

function initFlash()
{
	var showStartAnimation = 0
	if(content == "artikel" && id_typ == 0 && id_utyp == 0 && id_uutyp == 0 && id == 0)showStartAnimation = 1;
	
	if(!$.browser.webkit){
		$('#tagcloud').html("");
		$('#tagcloud').flash({
		    src: '/flash/cloud.swf',
		    width: 206,
		    height: 150,
		    "wmode":"transparent",
	      flashvars: {
		      "showStartAnimation": showStartAnimation, 
		      "wmode": "transparent",
					"tcolor": "0xffffff",
					"tcolor2": "0xffffff",
					"hicolor": "0xffffff",
					"tspeed": "200",
					"fontFace":"Arial",
					"xmlpath": "/flash/cloud_data.xml"
				}
		}); 
	} 
}



$(document).ready(function(){
	
	$('.rollover').hoverswap({});



//  _______________________________________________________________


  init();
  initFlash();
  if(sliderIsOpened){
		$("#teaser").jCarouselLite({
		    btnNext: ".teaser_right",
		    btnPrev: ".teaser_left",
		    auto:3000,
		    speed:500,
		    beforeStart:function(){
		    	setTimeout("deactivateReflection()", 100)
		    	setTimeout("activateReflection()", 300)
		    }
		});
	} else {
		initSliderWhenOpened = true;
	}
	
	$(".teaser_element").hover(function(){
			$(this).find(".teaser_element_text").stop(true,false)
			$(this).find(".teaser_element_text").fadeTo(250,1, function(){
				$(this).removeAttr("style");
			});
			$(this).find(".teaser_element_headline").stop(true,false)
			$(this).find(".teaser_element_headline").fadeTo(250,1, function(){
				$(this).removeAttr("style");
			});			
		}, function(){
			$(this).find(".teaser_element_text").stop(true,false)
			$(this).find(".teaser_element_text").fadeTo(250,0);
			$(this).find(".teaser_element_headline").stop(true,false)
			$(this).find(".teaser_element_headline").fadeTo(250,0);
		});		
		
	
	var img = new Image();
	img.src = "images/teaser_bottom.png";
	var img = new Image();
	img.src = "images/teaser_btn.png";

	
	$(".teaser_btn").click(function(){
		$(".teaser_area").slideToggle(500);			
			setTimeout("changeSlider()",150);				
	});
	
	$(".main_li").children().mouseenter(function(){		
		var img_obj = $(this).parents(".main_li").children("a").children("img");
		var img_src = img_obj.attr("src");
		
		img_obj.attr({
				"src": img_obj.attr("longdesc")
			});
				
	});
	$(".main_li").children().mouseleave(function(){		
		var img_obj = $(this).parents(".main_li").children("a").children("img");
		var img_src = img_obj.attr("src");
		img_obj.attr({
				"src": img_src.replace(/02/g,"01")
			});				
	});	
	
	if(content=="produkte" && !sliderIsOpened ){
		setTimeout("clickSliderBtn()",1000);
	}
  
});

function clickSliderBtn(){
	$(".teaser_btn").trigger("click");
}


function changeSlider(){
		if( sliderIsOpened ){
			sliderIsOpened = false;
			$(".teaser_bottom").css("background","url(/images/teaser_bottom.png) repeat-x");
			$(".teaser_btn img").attr("src", "/images/teaser_btn.png")
		} else {
			sliderIsOpened = true;
			$(".teaser_bottom").css("background","url(/images/teaser_bottom2.png) repeat-x");	
			$(".teaser_btn img").attr("src", "/images/teaser_btn2.png");
			if(!$.browser.msie)$("img.reflect").reflect({/* Put custom options here */});
			if(initSliderWhenOpened){
				$("#teaser").jCarouselLite({
				    btnNext: ".teaser_right",
				    btnPrev: ".teaser_left",
				    auto:8000,
				    speed:750,
				    beforeStart:function(){
				    	if(!$.browser.msie)$("img.reflect").reflect({/* Put custom options here */});
				    },
				    afterEnd:function(){
				    	if(!$.browser.msie)$("img.reflect").reflect({/* Put custom options here */});
				    }
				});
			}
		}
		var session_obj = $("#session_id");
		var session_name = session_obj.attr("name").toString();
		var session_id = session_obj.val().toString();
			$.post("js/toogleTeaser.php",{  session_name : session_id, "teaser": sliderIsOpened, "teaser": sliderIsOpened}, function(data){
		});		
}

function hinweis_aus() {
  //document.getElementById("hinweis").style.visibility = "hidden";
	$("#hinweis").animate({"top":-$("#hinweis").height()},300, "easeInBack");

}

function show_hinweis(hinweis, decode){
	if(decode)hinweis = $.url.decode(hinweis);
	$("#hinweis_text").html(hinweis);
	document.getElementById("hinweis").style.visibility = "visible";
	//$("#hinweis").css("z-index",100);
	var posY =  ($(window).height() / 2) - ($("#hinweis_text").height() / 2);
	$("#hinweis").css("top",-$("#hinweis").height());
	//$("#hinweis").css("z-index",100);
	$("#hinweis").animate({"top":posY},800, "easeOutBounce");
}

function deactivateReflection(){
	//$(".teaser_element_image img").unreflect();
	//$(".teaser_element_image").reflect();
}


function activateReflection(){
	//$(".teaser_element_image img").unreflect();
	if(!$.browser.msie)$(".reflected").reflect({height:.20, opacity:.25});
	if(!$.browser.msie)$(".reflect").reflect({height:.20, opacity:.25});
}

