/* warum soviele document-ready konstrukte? - klar geht das, ist aber hier nicht nötig (ep 2010-06-21)*/

function mainmenu(){
$("#nav ul li ul").css({display: "none"}); // Opera Fix
$("#nav li").hover(
	function(){
		//$(this).find('ul:first').css({visibility: "visible",display: "none"}).show(400);
		$(this).find('ul:first').css({visibility: "visible",display: "none"}).fadeIn(600);
	},
	function(){
		$(this).find('ul:first').css({visibility: "hidden"});
	});
}

function topPulldown(){
$("#pulldownTop li ul").css({display: "none"}); // Opera Fix
$("#pulldownTop").hover(
	function(){
		$(this).find('ul:first').css({visibility: "visible",display: "none"}).fadeIn(600);
	},
	function(){
		$(this).find('ul:first').css({visibility: "hidden"});
	});
}

function rtrim ( str, charlist ) {
	charlist = !charlist ? ' \\s\u00A0' : (charlist+'').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\\$1');
	var re = new RegExp('[' + charlist + ']+$', 'g');
	return (str+'').replace(re, '');
}

function ltrim ( str, charlist ) {
	charlist = !charlist ? ' \\s\u00A0' : (charlist+'').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
	var re = new RegExp('^[' + charlist + ']+', 'g');
	return (str+'').replace(re, '');
}

function setActiveState() {
	$('#nav ul ul').hover(
		function() {
			// mouseover
			var mo = $(this).parents("li:eq(0)").find("a").attr('onmouseover').toString();
			var arr1 = mo.split("'");
			if(arr1.length == 1) arr1 = mo.split('"');
			//alert(arr1[1]);
			//mo = mo.replace("/.*'(.*)'.*/iU",'$1');
			//alert(mo);
			/*mo = $.trim(ltrim(mo,'function onmouseover(event) {'));
			mo = ltrim(mo,'over("');

			mo = $.trim(rtrim(mo,'}'));
			mo = rtrim(mo,'");');*/
				
			//var over_imgurl = eval(mo+"_h.src");
			var over_imgurl = eval(arr1[1]+"_h.src");

			//$(this).parents("li:eq(0)").find("a img").attr('src',over_imgurl);
			$(this).parent().find("a img").attr('src',over_imgurl);
			//$(this).parent().parent().find("a img").attr('src',over_imgurl);
		
		},
		function() {
			// mouseout
			var mout = $(this).parents("li:eq(0)").find("a").attr('onmouseover').toString();
			var arr1 = mout.split("'");
			if(arr1.length == 1) arr1 = mout.split('"');
			/*mout = ltrim(mout,'function onmouseover(event) {');
			mout = $.trim(mout);
			mout = ltrim(mout,'over("');
			mout = rtrim(mout,'}');
			mout = $.trim(mout);
			mout = rtrim(mout,'");');*/
			var out_imgurl = eval(arr1[1]+"_n.src");
			$(this).parents("li:eq(0)").find("a img").attr('src',out_imgurl);
		}
	);
}

/*
 * @description finds matching string in string (case insensitive) and returns
 * @return mixed (string or false)
 * @since 2010-06-21
 * @see http://phpjs.org/
 */
function stristr (haystack, needle, bool) {
	var pos = 0;
	haystack += '';
	pos = haystack.toLowerCase().indexOf( (needle+'').toLowerCase() );
	if (pos == -1){
		return false;
	} else {
		if (bool) {
			return haystack.substr( 0, pos );
		} else {
			return haystack.slice( pos );
		}
	}
}

/** 
 * openExternalLinksInNewTabOrWindow()
 * @description Adds target="_blank" to links with the class external-link-new-window and to links that start with http: or https
 * @author Erik Poehler, Navigate AG
 * @since 2010-06-21
 */
function openExternalLinksInNewTabOrWindow() {
	/*var links = $(document.getElementById('content-main-left')).find('p a');
	if (links) { // only do this on links within a containing div
		links.each(function(){
			if ($(this).hasClass('external-link-new-window') || stristr($(this).attr('href'),'http://').length > 0 || stristr($(this).attr('href'),'https://').length >0 ) {
				var newtitle = $(this).attr('title') + ' (öffnet in neuem Fenster)';
				$(this).attr({target:'_blank',title:newtitle});
			}
		});
	}*/
	var links = $("a.external-link-new-window");
	if (links) { // only do this on links within a containing div
		links.each(function(){
				$(this).attr({target:'_blank'});
		});
	}
}
function adjustsize(){
	if($(window).width() <= 1024){
		$("body").addClass("smallresolution");
	}
} 
function mycarousel_initCallback(carousel)
{

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
}
$(document).ready(function(){
	adjustsize(); 
	mainmenu();
	topPulldown();
	$("a[href='#top']").attr('href',document.location.href +'#top');
		$("#accordion").accordion({ header: 'a.header', autoHeight: false,"animated":false,"collapsible": true });
	$("div.product-accordion").accordion({autoHeight: false});
	src = "http://www.facebook.com/plugins/like.php?href="+document.URL;
	$("#facebook-like-button").attr("src",src);
	
	/*$('#accordion .header').click(function() {
		$(this).next().toggle();
		return false;
	});*/

	
	setActiveState();
	$("a.screenshot").fancybox({'titleShow': false});
	$("#content-main a[href*='.jpg'], #content-main a[href*='.gif'], #content-main a[href*='.png']").fancybox({'titleShow': false});
	openExternalLinksInNewTabOrWindow();
	
	// AZ-Box Referenzen
	$("div.topRow > img.abc").mouseover(function() {
		$(this).parent("div").find("div.azBox").fadeIn("fast").mouseleave(function() {
		$(this).fadeOut("fast");})
	});
		var arrLi=[];
		$('#mycarousel li').each(function(){
			arrLi.push($(this).html());
		});
		var arrLiStart = arrLi ;
		//var liHtml = $('.scrollable').html() ;
	
	var config1 = {
		auto: 2,
		scroll:1,
		wrap: 'circular',
		initCallback: mycarousel_initCallback,
		rtl: false,
		animation:'slow'
	};
		var config2 = {
		auto: 2,
		scroll:1,
		wrap: 'circular',
		initCallback: mycarousel_initCallback,
		rtl: true,
		animation:'slow'
	};
	
	$('#mycarousel').jcarousel(config1);
	bindClickAction();
	
	$('a.next').click(function(){
		var carousel = $('#mycarousel').data('jcarousel');
		if(!carousel.options.rtl){
			carousel.next();
			//alert("2");
			return true;
		}
		carousel.lock();
		carousel = null ;
		right = parseInt($('#mycarousel').css("right"));
		reverseScrolling(right,false);
				//alert("2");
		$(".scrollable li").css("margin-right","-3");
				$(".scrollable li").css("margin-right","0");
		$(".scrollable li").css("margin-left","0");
		var carousel = $('#mycarousel').data('jcarousel');
		carousel.next();
		/*$(".scrollable li").css("border-left","0");
		$(".scrollable li").css("border-right","1px solid #84A0B8");
		*/
		});
	$(".prev").click(function(){
		var carousel = $('#mycarousel').data('jcarousel');
		if(carousel.options.rtl){ 
			carousel.next();
					//alert("3");
			return true;
		}
		carousel.lock();
		carousel = null ;
		var arrLi1=[];
		$('#mycarousel li').each(function(){
			arrLi1.push($(this).html());
		});
		left = parseInt($('#mycarousel').css("left"));
		reverseScrolling(left,true);
		$(".scrollable li").css("margin-right","-4px");
		$(".scrollable li").css("margin-left","9px");
		var carousel = $('#mycarousel').data('jcarousel');
		carousel.next();
		//$("a.right").css("position","relative");
		//$("a.right").css("right","2px");
			//alert("32");
		/*$(".scrollable li").css("border-left","1px solid #84A0B8");
		$(".scrollable li").css("border-right","0");
		$(".scrollable li").css("margin-left","-1");*/
	});
	
	function reverseScrolling(ulPosition,rtl){
		curIndex = Math.round(ulPosition / -259) ;
		if(curIndex < 7){
			index1 = curIndex +2 ;
			if (index1 >= 7) index1 = index1 - 7 ;
		}else {
			help = $('#mycarousel li:last').html();
			index1 = 1;
			for(i=0;i<7;i++)
				if(help == arrLi[i])	index1 = i;
		}
		//index1-- ;
		//if(index1 < 0) index1 = 6 ;
		liHtml = '<ul id="mycarousel">';
		for(i=0;i<7;i++){
			liHtml +=  '<li>'+ arrLi[index1] + '</li>' ;
			index1--;
			if(index1 < 0) index1 = 6 ;
		}
		liHtml += '</ul>' ;
		$('.scrollable').html(liHtml);
		arrLi=[];
		$('#mycarousel li').each(function(){
			arrLi.push($(this).html());
		});
		if(rtl)
			$('#mycarousel').jcarousel(config2);
		else
			$('#mycarousel').jcarousel(config1);
		bindClickAction();
		
	}
	
	function bindClickAction(){
	
		$(".scrollable li").click(function(){
			meldungTopId = 1 ;
			help = $(this).html()  ;
			for(i=0;i < arrLiStart.length;i++)
				if( help== arrLiStart[i]) meldungTopId = i+ 1;
				
			changeMeldung(meldungTopId);
		});
	}
	
	/*$(".scrollable li").click(function(){
		meldungTopId = 1 ;
		help = $(this).html()  ;
		for(i=0;i < arrLi.length;i++){
			if( help== arrLi[i]) meldungTopId = i+ 1;
		}
		changeMeldung(meldungTopId);
	});*/
	
	if($("div.content-home-tagcloud")){
		$.get("index.php?id=208", {},
   function(data){
    $("div.content-home-tagcloud").html(data);
     //initiateEvents();
   });
	}
	
	
});

function getNextIdSlideshow(currentId){
	var result = currentId + 1;
	if(result == 8) result = 1 ;
	return result ;
}

function getPreviousIdSlideshow(currentId){
	var result = currentId - 1;
	if(result == 0) result = 7 ;
	return result ;
}

function calculateMeldungId(){
	itemsLeft1 = $("div.items").css("left");
	itemsLeft =  parseInt(itemsLeft1);
	var result = parseInt(itemsLeft/-259);
	if(! result) result = 1;
	return result ;
}

// AZ-Box Referenzen
/*$(document).ready(function() {
	$("div.topRow > img.abc").mouseover(function() {
		$(this).parent("div").find("div.azBox").fadeIn("fast").mouseleave(function() {
		$(this).fadeOut("fast");})
	});
});*/

// Detailbox Referenzen
$(".content-header-ref").ready(function() {
	$("div.innerRow li img").mouseover(function() {
		$(this).parent().parent().mouseleave(function() {
			$(this).find("div").fadeOut("fast");
		}).find("div").fadeIn("fast");
	});
});

function function_exists (function_name) {
	if (typeof function_name == 'string'){
		return (typeof this.window[function_name] == 'function');
	} else{
		return (function_name instanceof Function);
	}
}

// Ref-Slider Startseite
/*if($("#c49 div.scrollable")) {
	if (!function_exists('scrollable')) {
		$.getScript('fileadmin/templates/lib/scrollable.js', function(){
			$("#c49 div.scrollable").scrollable({circular: true});
		//alert($("#c49 div.scrollable").scrollable({circular: true}).autoscroll(5000));
			//$("#c49 div.scrollable").scrollable({circular: true}).autoscroll(5000);
		});
	} else {
		$("#c49 div.scrollable").scrollable({circular: true}).autoscroll(5000);
	}
}*/
/**
$(".news-scroller-home").ready(function() {
	$(".scrollable").scrollable({circular: true}).autoscroll(5000);
});
**/


//function test() {
//var tess = $(".scrollable div").attr("style");
//	if(tess = 'left: -777px;') {$('div.scrollable').addClass(tess);}
	

//}

//setInterval('test()', 2000);

function changeMeldung(id){
//alert(id);
//return ;
	$.get("index.php?id=141&tx_referenz_pi1[controller]=Meldung&tx_referenz_pi1[action]=ajax&tx_referenz_pi1[id]="+id, {},
   function(data){
    $("div.news-on-top").html(data);
     //initiateEvents();
   });
   //IE6 stuf
  	if (typeof document.body.style.maxHeight == "undefined")	return;
   $(".news-on-top-content,.news-on-top-image").fadeOut("slow");
   $(".news-on-top-content,.news-on-top-image").fadeIn(1200);
   
}

function bookmark() {
	var url =   'http://' + location.host;
	var title =location.host;
	// Internet Explorer
	if(document.all) {
		window.external.AddFavorite(url, title);
	}
	// Firefox
	else if(window.sidebar) {
		window.sidebar.addPanel(title, url, '');
	}	
	//return false;
}

             
