//sync height of Content and Sidebar
function syncheight(){
	$('#sidebar').height('auto');
	$('#content').height('auto');
	$('.product_details').height('auto');
	$('.product_info').height('auto');

	var sidebar_h = $('#sidebar').height();
	var content_h = $('#content').height();
	var proddet_h = $('.product_details').height();
	var prodinfo_h = $('.product_info').height();

	var site_h;
	var page_h;
	var full_h;
	if ($('#last_widget').length > 0) {
		var subscribe_h = $('#last_widget').outerHeight();
	} else {
		var subscribe_h = $('#subscribe').outerHeight(true);
	};
	$('#sidebar').css('padding-bottom',subscribe_h);
	sidebar_h = sidebar_h+subscribe_h;
	prodinfo_h = prodinfo_h+116;
	
	//Abgleich aller möglichen Elemente (sidebar/proddet/prodinfo)
	if (sidebar_h >= content_h){
		site_h = sidebar_h;
	} else {
		site_h = content_h;
	}
	
	if (proddet_h >= prodinfo_h){
		page_h = proddet_h;
	} else {
		page_h = prodinfo_h;
	}
	
	page_h = page_h+68;
	
	if (site_h >= page_h){
		full_h = site_h;
	} else {
		full_h = page_h;
	}
	
	//setze alles auf gleiche Hoehe (+Anpassungen)
	$('#sidebar').height(full_h-subscribe_h);
	$('#content').height(full_h);
	$('.product_details').height(full_h-68);
	$('.product_info').height(full_h-68);

}

//Popup
$(function(){
    $('.largeImg a').click(function(){
      
	var image = '<img alt="largeimage" src="' + $('.largeImg a').attr('href') + '"/>';
	var legend = '<div class="sub_legend">' + $('.sub_legend').html() + '</div>';
	var content;
	
	content = image + legend;
	
	$('.popup').html(content);
	$('.popup').show();
	
	return false;
    });
});

$(function(){
    $('.popup').click(function(){
	$('.popup').hide();
	return false;
    });
});

var tabs = null;
var tabs2 = null;

//---------------------------------------------------
//			Accordion
//---------------------------------------------------
$(document).ready(function(){
	//alle Container verstecken beim Seitenstart
	$(".toggle_container").hide();
	
	//alle leeren Keyvisuals entfernen
	$(".slideshow > div").each(function() {
		var $this = $(this);
		var $children = $this.children();
		if ($children.length < 2) {
			$this.remove();
		};
	});
		
	//wenn klick, dann auf-/zuklappen
	$(".trigger").click(function(){
		//warten bis Animation fertig, dann erneut syncheight
		$(this).next(".toggle_container").slideToggle("slow,",function(){syncheight();});
	});
	
	//Klasse setzen/entfernen je nach Zustand
	$(".trigger").toggle(function(){
		$(this).addClass("active"); 
		}, function () {
		$(this).removeClass("active");
	});
	
	//erstes mit oeffnen-klasse versehen
	$("#page_content h2.trigger:first").addClass("toggle_head");
	
	//Container mit dieser Klasse werden geoeffnet 
	$(".toggle_head").click();
	
	//Zubehoer Produkte (nur, wenn auch tatsaechlich vorhanden)
	if ($('.spec1').length > 0) {
		var productlist = $('.product_details .browse_products');
		productlist.after('<ul class="browse_products browse_accessories"/>');
		productlist.find('.spec1').appendTo('.browse_accessories');
	};
	
	//Tabs
	tabs = $('#tabs').tabs();
	tabs2 = $('.tabsystem').tabs();


	//Gallery
	$(".thumbs a").click(function(){
	  var largePath = $(this).attr("href");
	  $("#largeImg a img").attr({ src: largePath });
	  return false;
	});

	//leere divs und uls löschen
	$('.sub_advantages h2:empty,.sub_advantages ul:empty,.sub_legend dl:empty').remove();
	$('.sub_advantages:empty,.sub_legend:empty').remove();
	$('.toggle_container a:empty').remove();
	$('.toggle_container li:empty,.toggle_container p:empty').remove();	
	//danach syncheight
	setTimeout(function(){ syncheight() },1000);

	$('.tabmenu li').click(function(){
	  setTimeout(function(){ syncheight() },1000);
	});
});

//---------------------------------------------------
//		NEWS - Welcome - Slider
//---------------------------------------------------
$(document).ready(function() {	

	if($('.slideshow').length)
	$('.slideshow').cycle({ 
		fx: 'scrollHorz', 
		speed: 1500, 
		timeout: 6000,
		prev: '#prev2', 
		next: '#next2'
	});

});

//---------------------------------------------------
//		D/I -Search
//---------------------------------------------------
$(document).ready(function() {	

	//Aktivlogo löschen, wenn mehrere vorhanden
	$('.company_detail .aplogo').each(function(i,e){ if(i>0) $(e).remove(); });
	
	// Keine Suche wenn Suchergebnisse angezeigt werden.
	if ($('#dealersearch_list').children().length > 0) {
		$('#dealersearch').hide();
		$('#dealersearched').show();
	}
	
	//Neue Suche Link füllen
	if ($('#dealersearch').length > 0) {
		var dsurl = $('#dealersearch').attr('action');
		$('#dealersearchbacklink').attr('href' , dsurl.substring(0, dsurl.indexOf('?')))
	}
	
});
