$(function()
{
	var baseurl = $('.brl').html();
	
		// tabs - pannelbox
		$('.pannel_buttons .tabs_mod1_button:eq(0)').addClass('selected');
		$('.pannel_buttons .tabs_mod2_button:eq(0)').addClass('selected');
		$('.pannel_buttons .tabs_mod3_button:eq(0)').addClass('selected');
		$('.pannel_buttons .impr_button:eq(0)').addClass('selected');
		
		$('.pannel_content').find('.p_cont:eq(0)').css('display','inline');
		$('.p_button').click(function(){
			var button_ind = $(this).index();
			$(this).parent().find('.p_button').removeClass('selected');	
			$(this).addClass('selected');
			
			$(this).parent().parent().find('.pannel_content .p_cont').css('display','none');
			$(this).parent().parent().find('.pannel_content .p_cont:eq('+button_ind+')').css('display', 'inline');
		});
	
	
	//search field
		var defaultval = $('.search_defval').html();
		$('#search-inkey').focus(function(){
			if ( this.value == defaultval )
			{
				this.value = "";
			}
		}).blur(function(){
			if ( !this.value.length )
			{
				this.value = defaultval;	
			}
		});
	//end search field
	
	//check if errors div is visible to display the form
	// for detail page form
	if ($("#errors")){
  		if($('#errors').is(':visible') )
		{
			$('.detailbx_commerr_bx').show();
		}
		else
		{
			if($('#sendok').is(':visible') )
			{
				$('.detailbx_commerr_bx').show();	
			}
			else
			{
				$('.detailbx_commerr_bx').hide();
			}
		}
	
		$('#send_comm_topbutton').click(function(){
			$('.detailbx_commerr_bx').toggle();
			return false;
		});
		
		$('.form_comm_button').click(function(){
			$('#ContactForm').submit();
			$('.send_comm_formbx').show();
		});
	}
	
	//featured images module
	if ($(".featured_main_img"))
	{
		//get article link
		var artLink = $(".featured_right_thumbs ul li:first").find('.imgBigLink').attr("href");
		
		$(".featured_main_img .featured_descr_bx").show(); //Show first image
		$(".featured_main_img .featured_descr_block").animate({ opacity: 0.85 }, 1 );
		
		//$(".featured_right_thumbs ul li:first").find('h2').wrap('<span class="active_linktoart" id="'+artLink+'"></span>'); 
		//$(".featured_right_thumbs ul li:first").find('h2').addClass('needs_unwrap');
		
		$(".featured_right_thumbs ul li:first").addClass('active'); 
		
		//Click and Hover events for thumbnail list
		$(".featured_right_thumbs ul li").click(function(){ 
			//Set Variables
			var imgAlt 			= $(this).find('img').attr("alt"); 
			var imgPath 		= $(this).find('a').attr("href");
			var imgTitle 		= $(this).find('img').attr("title");
			var imgDesc 		= $(this).find('.featured_descr_block').html();
			var imgDescHeight 	= $(".featured_main_img").find('.featured_descr_block').height();	
			var imgBigLink 		= $(this).find('.imgBigLink').attr("href");
			
			if ($(this).is(".active")) {  
				return false; 
			} else {
				//$(this).find('h2').wrap('<span class="active_linktoart" id="'+imgBigLink+'"></span>');
				//Animate 				
				$(".featured_main_img .featured_descr_block").animate({ opacity: 0, marginBottom: - imgDescHeight }, 250 , function() {
					$(".featured_main_img .featured_descr_block").html(imgDesc).animate({ opacity: 0.85,	marginBottom: "0" }, 250 );
					$(".featured_main_img img").attr({ src: imgPath , alt: imgAlt , title: imgTitle });
					$(".featured_main_pictlink").attr({ href: imgBigLink });
				});
			}
			
			//$(".needs_unwrap").unwrap();	//unwrap all elements with class needs_unwrap
		
			//$(".featured_right_thumbs ul li h2").removeClass('needs_unwrap');		//remove class needs_unwrap from h2 element
			//$(this).find('h2').addClass('needs_unwrap');	//for selected element find h2 and add class needs_unwrap
			
			$(".featured_right_thumbs ul li").removeClass('active'); 
			$(this).addClass('active');
			return false;
			
		}) .hover(function(){
			$(this).addClass('hover');
			}, function() {
			$(this).removeClass('hover');
		});
		//Toggle Teaser
		$(".collapse").click(function(){
			$(".featured_main_img .featured_descr_block").slideToggle();
			$(".collapse").toggleClass("show");
			$(".featured_link").toggleClass("featured_link_coll");
		});
	}
	
	//end featured images module
	
	
	//Like Buttons
	if ($('.vote_up') && $('.vote_down'))
	{
		var voting_succ = $('.voting_succ');
		var voting_err = $('.voting_err');
		var voting_err2 = $('.voting_err2');
		var voting_err3 = $('.voting_err3');
		var voting_err4 = $('.voting_err4');
		var voting_err5 = $('.voting_err5');
		
		$('.vote_up').click(function(){
			var voted_item_id = $(this).attr("id");
			var voteup_countbox = $('.voteup_countbox');
			var str = 'action=voteup&t=nw&voted_item_id='+ voted_item_id;
			var cookie_up = 'up'+voted_item_id;
			var cookie_down = 'down'+voted_item_id;
			var comment_info;
			
			if( !getCookie(cookie_up) && !getCookie(cookie_down))
			{
				vote(str ,voted_item_id , voteup_countbox);
				comment_info = '00';
				if ( !getCookie('cookie_test') )
				comment_info = '222';
			}
			else
			{
				var cookie_val = getCookie(cookie_up);
				cookie_val = cookie_val.split('|');
				comment_info = cookie_val[cookie_val.length - 1];
			}
			
			if( comment_info == '00')
			{
				$('#done_button').removeClass().addClass('votingup_comm_donebutton');	
				if( $('.voting_comment_bx').is(':visible') != true)
				{
					$('.voting_comment_bx').fadeIn(1500);
				}
				else
				{
					$('.voting_comment_bx').fadeOut(200).fadeIn(200);
				}
			}
			else if ( comment_info == '222')
			{
				voting_err4.dequeue().stop(true,true).fadeIn(2000).delay(1700).fadeOut(1000); 
			}
			else if ( comment_info == '11')
			{
				voting_err5.dequeue().stop(true,true).fadeIn(2000).delay(1700).fadeOut(1000); 	
			}
			else
			{
				voting_err2.dequeue().stop(true,true).fadeIn(2000).delay(1700).fadeOut(1000); 
			}
		});
		
		
		$('.vote_down').click(function(){
			var voted_item_id = $(this).attr("id");
	 		var votedown_countbox = $('.votedown_countbox');
			var str = 'action=votedown&t=nw&voted_item_id='+ voted_item_id;
			var cookie_down = 'down'+voted_item_id;
			var cookie_up = 'up'+voted_item_id;
			var comment_info;
			
			if( !getCookie(cookie_down) && !getCookie(cookie_up))
			{
				vote(str ,voted_item_id , votedown_countbox);
				comment_info = '00';
				if ( !getCookie('cookie_test') )
				comment_info = '222';
			}
			else
			{
				var cookie_val = getCookie(cookie_down);
				cookie_val = cookie_val.split('|');
				comment_info = cookie_val[cookie_val.length - 1];
			}
			
			if( comment_info == "00")
			{
				$('#done_button').removeClass().addClass('votingdown_comm_donebutton');
				if( $('.voting_comment_bx').is(':visible') != true)
				{
					$('.voting_comment_bx').fadeIn(1500);
					
				}
				else
				{
					$('.voting_comment_bx').fadeOut(200).fadeIn(200);
				}
			}
			else if ( comment_info == '222')
			{
				voting_err4.dequeue().stop(true,true).fadeIn(2000).delay(1700).fadeOut(1000); 
			}
			else if ( comment_info == '11')
			{
				voting_err5.dequeue().stop(true,true).fadeIn(2000).delay(1700).fadeOut(1000); 	
			}
			else
			{
				 voting_err3.dequeue().stop(true,true).fadeIn(2000).delay(1700).fadeOut(1000);
			}
		});
	
	
		$('.votingup_comm_donebutton').live('click', function(){
			var voted_item_id = $('.vote_up').attr("id");
			
			var cookie_val = getCookie('up'+voted_item_id);
			cookie_val = cookie_val.split('|');
			var k = cookie_val[cookie_val.length - 2];
			
			var voting_username = $('#voting_username').val();	
			var voting_usercomment = $('#voting_usercomment').val();
			voting_username = $.trim(voting_username);
			voting_usercomment = $.trim(voting_usercomment);
			 
			var str = 'action=add_votecomm&t2=ct&t=nw&voted_item_id='+ voted_item_id +'&username='+voting_username+'&usercomment='+voting_usercomment+'&k='+k;
			
			if( voting_username != "" && voting_usercomment != "" && voting_username.length > 1 && voting_usercomment.length > 5)
			{																										 
				 add_votecomm( str ,voted_item_id , voting_username , voting_usercomment );
			}
			else
			{
				$('.voting_err1').show();
			} 
		});
		
		$('.votingdown_comm_donebutton').live('click', function(){
			var voted_item_id = $('.vote_down').attr("id");
			
			var cookie_val = getCookie('down'+voted_item_id);
			cookie_val = cookie_val.split('|');
			var k = cookie_val[cookie_val.length - 2];
			
			var voting_username = $('#voting_username').val();	
			var voting_usercomment = $('#voting_usercomment').val();
			
			voting_username = $.trim(voting_username);
			voting_usercomment = $.trim(voting_usercomment);
			
			var str = 'action=add_votecomm&t2=ct&t=nw&voted_item_id='+ voted_item_id +'&username='+voting_username+'&usercomment='+voting_usercomment+'&k='+k;
			if( voting_username != "" && voting_usercomment != "" && voting_username.length > 1 && voting_usercomment.length > 5)
			{
				add_votecomm( str ,voted_item_id , voting_username , voting_usercomment );
			}
			else
			{
				$('.voting_err1').show();
			}
		});
		
		function add_votecomm( str ,voted_item_id , voting_username , voting_usercomment )
		{
			$.ajax({
				type: "POST",
				url: baseurl+"vote.php",
				data: str, 
				cache: false,
				success: function(html)
				{
					switch(html)
					{
						case "ok" : 
							$('.voting_comment_bx').fadeOut(200);
							$('.commvoting_succ').stop(true,true).fadeIn(2000).delay(1700).fadeOut(1000); 
						break;
						default: 
						alert("Error!");
						break;
					}
				}
			});
		}
		
		
		function vote(str ,voted_item_id , vote_countbox )
		{
			if( !getCookie('down'+voted_item_id) && !getCookie('up'+voted_item_id))
			{
				$.ajax({
					type: "POST",
					url: baseurl+"vote.php",
					data: str,
					cache: false,
					async: false, 
					
					success: function(html)
					{
						switch(html)
						{
							case "was_voted" : 
								voting_err.dequeue().stop(true,true).fadeIn(2000).delay(1200).fadeOut(1000);
							break;
							
							case "cookie_found" :
								voting_err.dequeue().stop(true,true).fadeIn(2000).delay(1200).fadeOut(1000);
							break;
							
							case "cdisabled" :
								voting_err4.dequeue().stop(true,true).fadeIn(2000).delay(1200).fadeOut(1000);
							break;
							
							default :
								vote_countbox.html(html);
								voting_succ.dequeue().stop(true,true).fadeIn(2000).delay(1200).fadeOut(1000);
						}
					}	
				});
			}
			else
			{
				voting_err.dequeue().stop(true,true).fadeIn(2000).delay(1200).fadeOut(1000);
			}
		}
		
		$('.just_votebuttonbx').click(function(){
			$('.voting_comment_bx').fadeOut(200);
			$('#voting_username').val('');	
			$('#voting_usercomment').val('');
			$('.voting_err1').hide();
			
		});
	
	
	
	}
	//End Like Buttons
	
	
	
	//featured play videos module
	if($("#news_video")){
		$("ul#news_video").ytplaylist();
	}
	
	
	//sliders module
		var right_catbox = '.slider_rightbox';
		var left_catbox = '.slider_leftbox';
		
		if($(right_catbox).length > 0)
		{
			var count_ul = $(right_catbox + ' .slider_cat_box').length;
			if ( count_ul > 1)
			{
				$(right_catbox + ' .slider').each(function(index,element) 
				{	
					$(this).find('ul').hide();
					$(this).addClass('slide_button');
					$(this).find('.slider_title').addClass('slider_arrow_bk');
					
					if( (index == count_ul - 1) || $(this).find('ul').hasClass('static_listbox'))
					{
					 	$(this).find('ul').show();
						$(this).removeClass('slide_button');
						$(this).find('.slider_title').removeClass('slider_arrow_bk');
					}
				});
			}
		}
		
		
		if($(left_catbox).length > 0)
		{
			var count_ul = $(left_catbox + ' .slider_cat_box').length;
			if ( count_ul > 1)
			{
				$(left_catbox + ' .slider').each(function(index,element) 
				{	
					$(this).find('ul').hide();
					$(this).addClass('slide_button');
					$(this).find('.slider_title').addClass('slider_arrow_bk');
					
					if( (index == count_ul - 1) || $(this).find('ul').hasClass('static_listbox'))
					{
					 	$(this).find('ul').show();
						$(this).removeClass('slide_button');
						$(this).find('.slider_title').removeClass('slider_arrow_bk');
					}
				});
			}
		}
		
		$('.slide_button').hover(
			
			function(){
				$(this).find('.slider_listbox').dequeue().stop(true,true).show('400');
			},
			function(){
				$(this).find('.slider_listbox').dequeue().stop(true,true).hide('400');
			}
		);
	//end sliders module
	
	//Top Menu
		$("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled - Adds empty span tag after ul.subnav
		$("ul.topnav li span").click(function() { //When trigger is clicked...
			
			//Following events are applied to the subnav itself (moving subnav up and down)
			$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click
	
			$(this).parent().hover(function() {
			}, function(){	
				$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
			});
	
			//Following events are applied to the trigger (Hover events for the trigger)
			}).hover(function() { 
				$(this).addClass("subhover"); //On hover over, add class "subhover"
			}, function(){	//On Hover Out
				$(this).removeClass("subhover"); //On hover out, remove class "subhover"
		});
	
	//end top menu
	
	
	
	//Tooltips
	if($('.tooltip_trigger'))
	{
		
		$('.tooltip_trigger').hover(function(){
			tip = $(this).find('.tip');
			tip.show(); //Show tooltip
		}, function() {
			tip.hide(); //Hide tooltip
		}).mousemove(function(e) 
			{
				var mousex = e.pageX + 20; //Get X coodrinates
				var mousey = e.pageY + 20; //Get Y coordinates
				var tipWidth = tip.width(); //Find width of tooltip
				var tipHeight = tip.height(); //Find height of tooltip
		
				//Distance of element from the right edge of viewport
				var tipVisX = $(window).width() - (mousex + tipWidth);
				//Distance of element from the bottom of viewport
				var tipVisY = $(window).height() - (mousey + tipHeight);
	
				if ( tipVisX < 20 ) 
				{ //If tooltip exceeds the X coordinate of viewport
					mousex = e.pageX - tipWidth - 20;
				} 
				if ( tipVisY < 20 ) 
				{ //If tooltip exceeds the Y coordinate of viewport
					mousey = e.pageY - tipHeight - 20;
				}
				//Absolute position the tooltip according to mouse position
				tip.css({  top: mousey, left: mousex });
		});
	}
	//End tooltip
	//Prettyphoto - search - videoboxes
	$('.animated_videobx').prettyPhoto({animation_speed:'fast',slideshow:10000, hideflash: true});
});

var mo="0";


function more(el){
	if(!el)return false;
	var orgEl = el;
	while(el && (el=el.nextSibling) ) if( el.nodeType==1 ) break;
	if(!el||!el.style)return false;
	var bActive = el.style.display!="block";
	el.style.display = bActive ? "block" : "none";
	orgEl.className = bActive ? "active" : "";
	return false
}

function setCookie(name, wert, domain, expires, path, secure){
   var cook = name+"="+unescape(wert)
   cook += (domain) ? "; domain="+ domain : ""
   cook += (expires) ? "; expires="+expires : ""
   cook += (path) ? "; path="+path : ""
   cook += (secure) ? "; secure="+secure : ""
   document.cookie = cook
}

function eraseCookie(name, domain, path){
var ProduktAnzahl = 0;
   var cook = name+"=; expires=Thu, 01-Jan-70 00:00:01 GMT"
   cook += (domain) ? "; domain="+domain : ""
   cook += (path) ? "; path="+path : ""
   document.cookie = cook
}

function getCookie(name){
   var i=0  //Suchposition im Cookie
   var suche = name+"="
   while (i<document.cookie.length){
      if (document.cookie.substring(i, i+suche.length)==suche){
         var ende = document.cookie.indexOf(";", i+suche.length)
         ende = (ende>-1) ? ende : document.cookie.length
         var cook = document.cookie.substring(i+suche.length, ende)
         return unescape(cook)
      }
      i++
   }
   return ""
}

function checkCookie(){
   setCookie("CookieTest", "OK")
   if (!getCookie("CookieTest"))
      return false
   else{
      eraseCookie("CookieTest")
      return true
   }
}

function einfuegen(pid, deeplink, bild, titel, preis){
  var ipid = getCookie("PID") + pid + "|"
  var ideeplink = getCookie("Deeplink") + deeplink + "|"
  var ibild = getCookie("Bild") + bild + "|"
  var ititel = getCookie("Titel") + titel + "|"
  var ipreis = getCookie("Preis") + preis + "|"
  setCookie("PID", ipid,"","Thu, 01-Jan-15 00:00:01 GMT","/")
  setCookie("Deeplink", ideeplink,"","Thu, 01-Jan-15 00:00:01 GMT","/")
  setCookie("Bild", ibild,"","Thu, 01-Jan-15 00:00:01 GMT","/")
  setCookie("Titel", ititel,"","Thu, 01-Jan-15 00:00:01 GMT","/")
  setCookie("Preis", ipreis,"","Thu, 01-Jan-15 00:00:01 GMT","/")
   if (!document.cookie)
     alert("Cookies not enabled!")
   else
  {
    var ipid = getCookie("PID") 
    var ProduktAnzahl = 0
    ipid.replace(/\|/g, function () {
     ProduktAnzahl++;
   });
     document.getElementById("menge").innerHTML=ProduktAnzahl;
    alert("Ok!")
  }
}

function menge() {
var ipid = getCookie("PID") 
var ProduktAnzahl = 0
ipid.replace(/\|/g, function () {
   ProduktAnzahl++;
});
 document.write('<span id=menge>'+ProduktAnzahl+'</span>');
}

function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = ""
}
