function init(){
   var a=document.getElementsByTagName('a');
   for (var i=0;i<a.length;i++) {
      a[i].onmousedown = function(){
         this.onfocus = function(){this.blur();}
      }
      a[i].onmouseup = function(){
         this.onfocus = function(){}
      }
      a[i].onmouseout = a[i].onmouseup;
   }
}

window.onload = init;

$(document).ready(function(){
	//del BG in first Meta li
	$("ul.navigation > li:last > a").css({"background": "none"});	
	$("ul.breadcrumb > li:last").css({"background": "none"});	
	$("ul#stagenavigation > li:first").css({"background": "none"});
	
	//th first and last class add
	$("th:first","table > tbody > tr:first").addClass("lr");
	$("th:last","table > tbody > tr:first").addClass("rr");
	$("td:first","table > tbody > tr").css({background: "none"});
	
	//toggle display for subnavigation
	$("ul.navigation > li").bind("mouseenter mouseleave", function(e){
		if(e.type == "mouseenter"){
			$(".subnav",this).show();
			$(".tap",this).show();
		}else{
			$(".subnav",this).hide();
			$(".tap",this).hide();			
		}
	});
	
	//if IE the shadow need some magic
	if($.browser.msie)
		$("table").wrap("<div class='ieu shadow'></div>");	
	
	//col over 
	$("table > tbody > tr").bind("mouseenter mouseleave", function(e){
		if(e.type == "mouseenter") $("td",this).css({"background-color":"#edede6"});
		if(e.type == "mouseleave") $("td",this).css({"background-color":"transparent"});
	});
		
	//stagenavigation magic
	$("ul#stagenavigation > li:first").addClass("act");
	if ($(".scrollable").size() > 0) {
		$(".scrollable").scrollable({
			items: '.item',
			circular: true,
			mousewheel: true
		}).navigator({
			navi: 'ul#stagenavigation',
			naviItem: 'li',
			activeClass: 'act',
			history: false
		});
		var api = $(".scrollable").data("scrollable");
		api.onBeforeSeek(function(event, index){
			switch (index) {
				case 0:
					$(".arrow", "ul#stagenavigation > li:eq(0)").css({
						top: -62
					});
					break;
				case 1:
					$(".arrow", "ul#stagenavigation > li:eq(1)").css({
						top: -79
					});
					break;
				case 2:
					$(".arrow", "ul#stagenavigation > li:eq(2)").css({
						top: -85
					});
					break;
				case 3:
					$(".arrow", "ul#stagenavigation > li:eq(3)").css({
						top: -79
					});
					break;
				case 4:
					$(".arrow", "ul#stagenavigation > li:eq(4)").css({
						top: -62
					});
					break;
			}
		});
	} else{
		//$("#stage").addClass("small");
	}
		
	//Selectfield
	$("ul.select > li.parent").click(function(){
		$(this).children("ul").show();
	});	
	$("ul.value").mouseleave(function(){
		$(this).hide();
	});
	$("ul.value > li:last-child").css("border", "none"); 	
	
	
	if ($('.csc-textpic-imagewrap > ul > li').size() > 1 && $('.csc-textpic-imagewrap').parent().hasClass('csc-textpic-above') && $('.csc-textpic-imagewrap').parent().hasClass('csc-textpic-center')) {
			
			// cach elements
			slideshowWrap = $('.csc-textpic-imagewrap > ul').parent();
			slideshowBox = slideshowWrap.parent();
			slideshowList = $('ul',slideshowWrap);
			anzElements	= slideshowList.children('li').size();
			
			// init wrap width and height
			slideshowWrap.width(410).css({
				'overflow': 'hidden',
				'height': '310px',
				'position':'relative',
				'padding': '2px'
			});
			//slideshowList.width(anzElements * 404);
			
			slideshowList.children('li').css({
				position: 'absolute',
				top: '2px',
				left: '2px',
				display: 'none'
			});
			slideshowList.children('li:first').css({display: 'block'});
			
			// build and init controlpanel
			slideshowBox.append('<div class="slidenav" ><!--a href="#next" class="cparrowright">next</a><a href="#last" class="cparrowleft">last</a--></div>');
			for(i=0; i < anzElements; i++){
				$('.slidenav',slideshowBox).prepend('<span><img src="fileadmin/templates/img/icons/pixel.gif" /></span>');
			}
			$('.slidenav > span:first',slideshowBox).addClass('act');
			//$('.cparrowleft').css({opacity: 0.2}, 500).addClass('blind');
			
			function imgChange(nextIndex,currIndex,option){		
				slideshowList.children("li:eq(" + nextIndex + ")").css({
					'z-index': 5
				}).show();
				slideshowList.children("li:eq(" + currIndex + ")").css({
					'z-index': 10
				}).fadeOut('slow');
				
				$('.slidenav > span:eq(' + currIndex + ')').removeClass('act');
				$('.slidenav > span:eq(' + nextIndex + ')').addClass('act');
				
				if(option == "auto" && slideshowList.hasClass('auto')){
					currIndex = nextIndex;
					nextIndex = currIndex + 1;
					
					if (nextIndex == $('.slidenav > span').size()) {
						// allright this is the end of list - just move to the first
						currIndex = nextIndex - 1;
						nextIndex = 0;
						
						// ok now the visitor is able to control the pictures
						//$('.cparrowright').animate({opacity: 1}, 500).removeClass('blind');
						//$('.cparrowleft').animate({opacity: 1}, 500).removeClass('blind');
					}
					
					setTimeout(function(){
						imgChange(nextIndex, currIndex, "auto");
					}, 4000);		
				}		
			}
						
			$('.cparrowright').add('.cparrowleft').bind("click", function(){
				// stop the auto
				slideshowList.removeClass('auto');
				
				// swap the image
				if( $(this).hasClass('cparrowright') ){
					
					// next image
					currIndex = $('.cpcnt > span.act').index();
					if( $('.cparrowleft').hasClass('blind') ) 
						$('.cparrowleft').animate({opacity: 1}, 500).removeClass('blind');
											
					if (currIndex + 1 != anzElements) {	
						nextIndex = currIndex + 1;

						imgChange(nextIndex,currIndex,"clicked");
						
						if (nextIndex + 1 == anzElements) 
							$('.cparrowright').animate({opacity: 0.2}, 500).addClass('blind');
					}
				}else {
					
					// prev image
					currIndex = $('.cpcnt > span.act').index();
					if( $('.cparrowright').hasClass('blind') )  
						$('.cparrowright').animate({opacity: 1}, 500).removeClass('blind');
					
					if (currIndex != 0) {	
						nextIndex = currIndex - 1;

						imgChange(nextIndex,currIndex,"clicked");
						
						if (nextIndex == 0) 
							$('.cparrowleft').animate({opacity: 0.2}, 500).addClass('blind');
					}					
				}
			});
			
			// run on load
			//$('.cparrowleft').animate({opacity: 0.2}, 500).addClass('blind');
			//$('.cparrowright').animate({opacity: 0.2}, 500).addClass('blind');
			slideshowList.addClass('auto');
			setTimeout(function(){
				imgChange(1,0,"auto");
			}, 2000);
		
	}	
	
	$("a.lightbox").fancybox();

});
