var content ="";
var id_typ = 0;
var id_utyp = 0;
var id_uutyp = 0;
var id = 0;


/**
*
*  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:"black" }, { queue:true, duration:250 } );
						$(this).next().animate( { backgroundColor:"#ff6633", color:"white" }, { queue:true, duration:250 } );
						$(this).next().animate( { backgroundColor:"white", color:"black" }, { queue:true, duration:250 } );
						$(this).next().animate( { backgroundColor:"#ff6633", color:"white" }, { queue:true, duration:300 } );
					} catch(e){}
					show_hinweis("Bitte überprüfen Sie die 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:"#ff6633", color:"white" }, { queue:true, duration:250 } );
						$(this).animate( { backgroundColor:"white", color:"black" }, { queue:true, duration:250 } );
						$(this).animate( { backgroundColor:"#ff6633", 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( $("#formular_forward_blank").attr("action") != undefined )$("#formular_forward_blank").submit();		
		
		$(".fancy").fancybox();
		
		$("#formular_forward").submit(function(){
			$.fancybox({
				'width'		: 850,
				'height'		: '80%',
				'padding'		: 0,
				'href'			: 'about:blank',
				'type'				: 'iframe',
				'showCloseButton' : false,
				'hideOnOverlayClick' : false,
				'hideOnContentClick' : false,
				'enableEscapeButton' : false
				
			});
			
			$("#formular_forward").attr("target",$('#fancybox-frame').attr('name'));
			
			
			
			
			return true;
		});
		
		
		if( $("#formular_forward").attr("action") != undefined )$("#formular_forward").submit();
		
		$('.btn_start').click(function (){
			window.location.href="/";
			return false;
		});
		
		$('.btn_shoppingcart').click(function (){
			window.location.href="index.php?content=warenkorb";
			return false;
		});
		
		$('.btn_login_logout').click(function (){
			window.location.href="index.php?content=login&logout=1&kasse=1";
			return false;
		});		
		
		$('.btn_logout').click(function (){
			window.location.href="index.php?content=login&logout=1&kasse=1";
			return false;
		});				
		
		$('.btn_checkout').click(function (){
			window.location.href="index.php?content=kasse";
			return false;
		});		
		
		$('.btn_register').click(function (){
			window.location.href="index.php?content=registrierung&kasse=1";
			return false;
		});		
		
		$('.btn_guest').click(function (){
			window.location.href="index.php?content=login&guest=1&kasse=1";
			return false;
		});		
		
		$('.btn_sendorder').click(function (){
			window.location.href="index.php?content=bestellung";
			return false;
		});		
		
		//$(".ShoppingCartDelete").click(deleteFromShoppingCart);
		//$(".ShoppingCartChange").click(changeShoppingCart);
			
//  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("Bitte überprüfen Sie die Pflichtfelder.");
		return false;
	});

//  kontaktformular ende___________________________________________


		$("#formular").submit(function(){
			var fehler = checkMandatories(this);
			if(fehler)show_hinweis("Bitte überprüfen Sie die Pflichtfelder.");
			return !fehler;
		});
		if($("#hinweis_text").text() != "")show_hinweis($("#hinweis_text").html());
		
		// --- Abweichende Lieferadresse -------------------
		$("#c_lieferanschrift").click(function(){
			if($(this).attr("checked")){
				$("#input_c_lieferanschrift input").each(function(i){
					$(this).attr("disabled",false);
			
				});
				$("#input_c_lieferanschrift").slideDown("slow");
			} else {
				$("#input_c_lieferanschrift input").each(function(i){
					$(this).attr("disabled",true);
					
				});
				$("#input_c_lieferanschrift").slideUp("slow");
			}
		});
		
		if($("#c_lieferanschrift").attr("checked")){
				$("#input_c_lieferanschrift input").each(function(i){
					$(this).attr("disabled",false);
					
				});
				$("#input_c_lieferanschrift").slideDown("slow");
			} else {
				$("#input_c_lieferanschrift input").each(function(i){
					$(this).attr("disabled",true);
				});
				$("#input_c_lieferanschrift").css("display","none");
			}
		
		// --- /Abweichende Lieferadresse -------------------	

		
}

function initFlash()
{
	var showStartAnimation = 0
	if(content == "artikel" && id_typ == 0 && id_utyp == 0 && id_uutyp == 0 && id == 0)showStartAnimation = 1;

	$('#tagcloud').flash({
	    src: '../flash/cloud.swf',
	    width: 134,
	    height: 134,
	    "wmode":"transparent",
      flashvars: {
	      "showStartAnimation": showStartAnimation, 
	      "wmode": "transparent",
				"tcolor": "0xcc0033",
				"tcolor2": "0xcc00333",
				"hicolor": "0xff3333",
				"tspeed": "200",
				"fontFace":"Arial",
				"xmlpath": "flash/cloud_data.xml"
			}
	}); 
}




function startTabMenue(){
//  tab menue______________________________________________________
  //Get all the LI from the #tabMenu UL
  $('#tabMenu > li').click(function(){
        
    //perform the actions when it's not selected
    if (!$(this).hasClass('selected')) {    

    //remove the selected class from all LI    
    $('#tabMenu > li').removeClass('selected');
    
    //After cleared all the LI, reassign the class to the selected tab
    $(this).addClass('selected');
    
    //Hide all the DIV in .boxBody
    $('.boxBody div').slideUp('1500');
    
    //Look for the right DIV index based on the Navigation UL index
    $('.boxBody div:eq(' + $('#tabMenu > li').index(this) + ')').slideDown('1500');
    
   }

  }).mouseover(function() {

    //Add and remove class, Personally I dont think this is the right way to do it, 
    //if you have better ideas to toggle it, please comment    
    $(this).addClass('mouseover');
    $(this).removeClass('mouseout');   
    
  }).mouseout(function() { 
    
    //Add and remove class
    $(this).addClass('mouseout');
    $(this).removeClass('mouseover');    
    
  });

  
  //Mouseover with animate Effect for Category menu list  :)
  $('.boxBody #category li').mouseover(function() {

    //Change background color and animate the padding
    $(this).css('backgroundColor','#888');
    $(this).children().animate({paddingLeft:"20px"}, {queue:false, duration:300});
  }).mouseout(function() {
    
    //Change background color and animate the padding
    $(this).css('backgroundColor','');
    $(this).children().animate({paddingLeft:"0"}, {queue:false, duration:300});
  });  
	
  //Mouseover effect for Posts, Comments, Famous Posts and Random Posts menu list.
  $('.boxBody li').click(function(){
    window.location = $(this).find("a").attr("href");
  }).mouseover(function() {
    $(this).css('backgroundColor','#888');
  }).mouseout(function() {
    $(this).css('backgroundColor','');
  });  	
//  tab menue ende_________________________________________________




}

function initTop3Slider(){
	$(".t3slider_element_head_inactive").fadeTo(1, 1,function(){
		$(this).removeAttr("style");
	});
	$(".t3slider_element_head_active").fadeTo(1, 0);
	$(".t3slider_element_head_active:first").fadeTo(250, 1,function(){
		$(this).removeAttr("style");
	});
	$(".t3slider_element_head_inactive:first").fadeTo(1, 0);
	startTop3Slider();
}

function startTop3Slider(){
	$('.t3slider_element_head').mouseenter(function() {
		if(!$(this).next().is(':hidden'))return false;
		$('.t3slider_element_head').unbind("mouseenter");
		$(".t3slider_element_head_inactive").fadeTo(250, 1,function(){
		$(this).removeAttr("style");
	});
		$(".t3slider_element_head_active").fadeTo(250, 0);
		$(this).children(".t3slider_element_head_active").stop(true, true);
		$(this).children(".t3slider_element_head_inactive").stop(true, true);
		$(this).children(".t3slider_element_head_active").fadeTo(250, 1,function(){
		$(this).removeAttr("style");
	});
		$(this).children(".t3slider_element_head_inactive").fadeTo(250, 0);
		//$('.t3slider_element_active').stop(true, true);
		$('.t3slider_element_body').stop(true, true);
		$('.t3slider_element_body').slideUp('slow', startTop3Slider);
		$(this).next().slideDown('slow');
		return false;
	})
}




$(document).ready(function(){
	
	$('.rollover').hoverswap({});

	$(".carousel").jCarouselLite({
	    btnNext: ".next",
	    btnPrev: ".prev",
	    auto: 3000,
	    speed: 2000,
	    visible: 1
	});


//  _______________________________________________________________


  startTabMenue();
  init();
  initFlash();
  initTop3Slider();
});

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");
}

// ______________ slider anfang ____________________
$(function() {
    $('#Teaser').cycle({
        fx:      'fade',
        timeout:  6000,
        pager:   '.teaser_but',
        before:  onBefore, 
        pagerAnchorBuilder: pagerFactory
    });
    function pagerFactory(idx, slide) {
        var s = idx > 2 ? ' style="display:none"' : '';
        return '<a href="#"><div id="teaser_but"'+s+'><div class="teaser_but_text">'+(idx+1)+'</div></div></a>';
    };
    
    function onBefore() { 
    	$('#teaser_text').html(this.title); 
    	$('.teaser_sub_text').html(this.alt);
    	
    	if ((this.id) != "") {
				$('#teaser_link').html("<a href='" + (this.id) + "' target='_self'><img src='images/teaser/other/teaser_pfeil.png' width='16' height='16' style='margin-bottom:-2px;margin-left:8px;' /></a>");
			}	
			else {
				$('#teaser_link').html("");				
			}
		} 
});
// ______________ slider ende ____________________
