$(document).ready(function() {
	// Navigation
	$("#headerNavi").dropmenu()
	$('.supreme').mouseenter(function() {
		var rel = $(this).children('a').attr('rel');
		$('#' + rel).css('display', 'block');
	});
	$('.supremehover').mouseover(function(){
		$('.supremehover').css('display', 'none');
		$(this).css('display', 'block');
	}).mouseleave(function() {
		$(this).css('display', 'none');
	}).click(function() {
		var loc = $(this).parent('li').children('a').attr('href');
		window.location.href = base + loc;
	});
	$('.submenu').mouseover(function(){
		$(this).parent().children('span').css('display', 'block');
	}).mouseleave(function(){
		$(this).parent().children('span').css('display', 'none');
	});
	
	$('#content').mouseenter(function(){
		$('.supremehover').css('display', 'none');
	});
	$('#navigation > h1').mouseenter(function(){
		$('.supremehover').css('display', 'none');
	});
	$('#subnavi').mouseenter(function(){
		$('.supremehover').css('display', 'none');
	});
	$('#sidebar').mouseenter(function(){
		$('.supremehover').css('display', 'none');
	});
	$('#headerImg').mouseenter(function(){
		$('.supremehover').css('display', 'none');
	});
	
	
	
	
	//Sprachwahl
	var langClickable = true;
	$('#sprachwahl').click(function() {
		if(langClickable){
			langClickable = false
			$('.hiddenLang').slideDown('fast', function(){
					$(this).css('display', 'block');
			});
		}
	}).mouseleave(function() {
		$('.hiddenLang').each(function() {
			if (!langClickable) {
				$(this).slideUp('fast', function(){
					$(this).css('display', 'none');
					langClickable = true;
				});
			}
		});
	});
	
	
	
	//Startseiten Slideshow
	if($('#slideshow')[0]){
		$('#slideshow > ul > li:first').addClass('show').css('display','list-item');
		var timer = setInterval('rotate(\'slideshow\')',6000);
		$('#slideshownext').click(function(){
			rotate('slideshow');
			clearTimeout(timer);
			timer = setInterval('rotate(\'slideshow\')',6000);
		});
		$('#slideshowprev').click(function(){
			rotateBack('slideshow');
			clearTimeout(timer);
			timer = setInterval('rotate(\'slideshow\')',6000);
		});
	}
	
	
	//StartseitenTicker
	if($('.horizontal_scroller')[0]){
		$('.horizontal_scroller').SetScroller({
			velocity: 	 60,
			direction: 	 'horizontal',
			startfrom: 	 'right',
			loop:		 'infinite',
			movetype: 	 'linear',
			onmouseover: 'pause',
			onmouseout:  'play',
			onstartup: 	 'play',
			cursor: 	 'pointer'
		}).hover(
				function(e){
					var left = e.pageX - (($(window).width()-1000) / 2);
					//console.log($(window).width())
					$('.fwMKhover').css('display', 'block').css('left', left).css('top', e.pageY);
				},
				function(){
					$('.fwMKhover').css('display', 'none');
				}
		);
	}
	//Startseitenticker Archiv
	if($('#metallArchiv')[0]){
		$('#archive').children('.jahr').css('display','none');
		$('#archive').children('.jahr:last').css('display','block');
		var archiveMargin = ($('.jahr:last li').size()-1) * -416;
		$('.jahr:last').css('left', archiveMargin +'px');
		$('.jahreszahlen').click(function(){
			var rel = $(this).text();
			$('#archive').children('.jahr').css('display','none');
			$('#j-'+rel).css('left', '20px').css('display','block');
			$('#metallArchiv #archive .fwNext').fadeIn();
			$('#metallArchiv #archive .fwPrev').fadeOut();
		});
		$('.horizontal_scroller').click(function(){
			/*$('body').prepend('<div id="fwCanvas" class="closeArchive"></div>');
			jQuery.each(jQuery.browser, function(i) {
				if(i=="mozilla" && jQuery.browser.version.substr(0,3)=="1.9"){
					$("#fwCanvas").css('height', $("html").innerHeight());
				}else{
					$("#fwCanvas").css('height', $("body").innerHeight());
				}
			});
			$('#fwCanvas').fadeIn();
			$('#archive').fadeIn();
			$('#fwCloseArchive').fadeIn();

			$('.closeArchive').click(function(){
				$('#archive').fadeOut();
				$('#fwCanvas').fadeOut(function(){
					$('#fwCanvas').remove();
				});
				$('#fwCloseArchive').fadeOut();
			});*/
			window.location.href = $('#locationHref').text();
		});
		$('#archive .fwNext').click(function(){
			if(!$('.jahr').is(':animated')){
				if($('#archive .fwPrev').css('display') == 'none'){
					$('#archive .fwPrev').fadeIn();
				}
				var akt
				$('.jahr').each(function(){
					if($(this).css('display') == 'block'){
						akt = $(this).attr('id');
					}
				});
				var oldMargin = $('#'+ akt).css('left');
				var newMargin = parseInt(oldMargin) - parseInt(420);
				var last = (parseInt($('#'+akt).css('width')) - 440) * -1
				//console.log(akt +' o:'+ oldMargin +' n:'+ newMargin +' w:'+ last);
				if(newMargin >= last){
					newMargin = newMargin +'px';
					$('#'+ akt).animate({opacity: .5}, 200).animate({"left": newMargin}, 200).animate({opacity: 1}, 200);
				}
				if(parseInt(newMargin) == last){
					$(this).fadeOut();
				}
			}
		});
		$('#archive .fwPrev').click(function(){
			if(!$('.jahr').is(':animated')){
				if($('#archive .fwNext').css('display') == 'none'){
					$('#archive .fwNext').fadeIn();
				}
				var akt
				$('.jahr').each(function(){
					if($(this).css('display') == 'block'){
						akt = $(this).attr('id');
					}
				});
				var oldMargin = $('#'+ akt).css('left');
				var newMargin = parseInt(oldMargin) + parseInt(420);
				//console.log(akt +' '+ oldMargin +' '+ newMargin);
				if(newMargin <= 28){
					newMargin = newMargin +'px';
					$('#'+ akt).animate({opacity: .5}, 200).animate({"left": newMargin}, 200).animate({opacity: 1}, 200);
				}
				if(parseInt(newMargin) == 28){
					$(this).fadeOut();
				}
			}
		});
	}
	
	/* Archiv Unterseite */
	if($('#fullArchive')[0]){
		$('#quickSelection > .jahr > li').css('display', 'none');
		$('#quickSelection > .jahreszahlen').click(function(){
			var rel = $(this).text();
			$('#j-'+ rel).children('li').slideToggle();
		});
		$('#quickSelection > .jahr > li').click(function(){
			//console.log($(this).children('span').text());
			var temp = $(this).children('span').text().split(';');
			$('#fd').val('1');
			$('#fm').val(temp[1]);
			$('#fy').val(temp[2]);
			$('#td').val(temp[0]);
			$('#tm').val(temp[1]);
			$('#ty').val(temp[2]);
			$('#fullArchive > form').submit();
		});
	}
	
	
	//Teaser
	var oldId = 0;
	if(oldId == 0){
		var to = $('#teaser > h3:first').attr('id').split('_');
		oldId = to[1];
		$('#teaser > .teaserbody:first').css('display', 'block');
		$('#teaser > h3:first').addClass('open');
	}
	$('#teaser > h3').click(function(){
		var tRel = $(this).attr('id').split('_');
		var rel = tRel[1];
		if(oldId != rel){
			oldId = teaserSlide(oldId, rel);
		}
	});
	
	
	
	//Sidbear Downloadbild Imagewechsel
	$('#sb-singel-teaserbild').mouseenter(function(){
		$('#sb-singel-teaserbild > img').each(function(){
			if($(this).hasClass('hide')){
				$(this).removeClass('hide');
			}else{
				$(this).addClass('hide');
			}
		});
	}).mouseleave(function(){
		$('#sb-singel-teaserbild > img').each(function(){
			if($(this).hasClass('hide')){
				$(this).removeClass('hide');
			}else{
				$(this).addClass('hide');
			}
		});
	});
	
	
	
	//Downloads Imagewechsel
	var filetype = '';
	var obj;
	$('.files').mouseenter(function(){
		obj = $(this).children('div').children('img');
		//console.log($(this).children('div').children('img') + ' ' + obj);
		filetype = obj.attr('alt'); 
		obj.attr('src', 'fileadmin/templates/img/downloads-file-'+ filetype +'_hover.gif');
	}).mouseleave(function(){
		obj.attr('src', 'fileadmin/templates/img/downloads-file-'+ filetype +'.gif');
	});
	
	
	//Produktslider
	if($('#produktSlider')[0]){
		var pSTimer = setInterval('produktSliderAction()',6000);
		$('#produktSlider > .products > ul > li:first').addClass('active');
		$('#produktSlider > .next').click(function(){
			clearTimeout(pSTimer);
			produktSliderAction();
			pSTimer = setInterval('produktSliderAction()',6000);
		});
		$('#produktSlider > .prev').click(function(){
			clearTimeout(pSTimer);
			produktSliderAction('prev');
			pSTimer = setInterval('produktSliderAction()',6000);
		});
	}
	
	
	
	//Mitarbeiter
	if($('#mitarbeiter')[0]){
		//erst alle schließen und erstes öffnen
		$('.mitarbeiter > div').css('display', 'none');
		$('.header > a').addClass('closed');
		//default Verkauf öffene (ID: 3)
		//var relFirst = $('#mitarbeiter').children('div:first').children('a').attr('rel');
		var relOpen = 3
		if($('#mitarbeiter').children('div').length >= relOpen){
			$('#mitarbeiter').children('div').each(function(){
				if($(this).children('a').attr('rel') == relOpen){
					$(this).children('a').removeClass('closed');
				}
			});
		}else{
			relOpen = $('#mitarbeiter').children('div:first').children('a').attr('rel');
			$('#mitarbeiter').children('div:first').children('a').removeClass('closed');
		}
		$('#m-'+ relOpen +' > div').slideDown('fast');
		$('.controlMitarbeiter').click(function(){
			var rel = $(this).attr('rel');
			if (!$('#m-'+ rel +' > div').is(':animated') && $('#m-'+ relOpen +' > div').attr('id') != 'map_canvas') {
				$('#m-'+ rel +' > div').slideToggle('slow');
				if($(this).hasClass('closed')){
					$(this).removeClass('closed');
				}else{
					$(this).addClass('closed');
				}
			}
		});
	}

	//Kontaktformular Zeiten
	var zeitClickable = true;
	$('.zeit').click(function() {
		if(zeitClickable){
			zeitClickable = false;
			$(this).children('.hiddenTime').slideDown('fast', function(){
				$(this).css('display', 'block')
			});
		}
	}).mouseleave(function() {
		$(this).children('.hiddenTime').each(function() {
			if (!$(this).is(':animated')) {
				$(this).slideUp('fast', function(){
					$(this).children('.hiddenTime').css('display', 'none');
					zeitClickable = true;
				});
			}
		});
	});
	$('.zeit > span').click(function(){
		if(!zeitClickable){
			/*$(this).parent('.zeit').children('span').each(function(){
				if($(this).hasClass('selectedTime')){
					$(this).addClass('hiddenTime').removeClass('selectedTime');
				}
			});*/
			$(this).parent('.zeit').children('.selectedTime').css('display', 'block').addClass('hiddenTime').removeClass('selectedTime');
			$(this).addClass('selectedTime').removeClass('hiddenTime');
		}
	});
	$('.callback > form').submit(function(){
		var tf = '';
		var tt = '';
		$('#tf').children('span').each(function(){
			if($(this).hasClass('selectedTime')){
				tf = $(this).text();
			}
		});
		$('#tt').children('span').each(function(){
			if($(this).hasClass('selectedTime')){
				tt = $(this).text();
			}
		});
		$('#rueckrufzeit').val(tf +' - '+ tt);
	});
	
	
	/* galerie */
	var clicked = '';
	$('#fwGalerie .fwThumb').click(function(){
		jQuery.each(jQuery.browser, function(i) {
			if(i=="mozilla" && jQuery.browser.version.substr(0,3)=="1.9"){
				$("#fwCanvas").css('height', $("html").innerHeight());
			}else{
				$("#fwCanvas").css('height', $("body").innerHeight());
			}
		});
		var temp = $(this).attr('id').split('-')
		clicked = temp[1];
		$('#fwCanvas').fadeIn('fast');
		$('#fwGalerieLayer').fadeIn();
		$('#fwB-'+ clicked).fadeIn('fast').addClass('show');
		$('html, body').animate({
			scrollTop: 100
		}, 1000);
	});
	$('.close').click(function(){
		$('#fwCanvas').fadeOut('fast');
		$('#fwGalerieLayer').fadeOut();
		$('#fwGalerieLayer ul li').fadeOut('fast');
		$('#fwGalerieLayer ul li').each(function(){
			if($(this).hasClass('show')){
				$(this).removeClass('show');
			}
		});
	});
	$('.fwNext').click(function(){
		rotate('fwGalerieLayer');
	});
	$('.fwPrev').click(function(){
		rotateBack('fwGalerieLayer');
	});
});



function produktSliderAction(direction){
	if (!$('#produktSlider > .products > ul').is(':animated')) {
		var iw = 116;	//imagewidth
		var l = $('#produktSlider > .products > ul').css('left').replace('px', '');
		var w = $('#produktSlider > .products > ul').css('width').replace('px', '');
		if(direction == 'prev')
			l = l*1 + iw*1;
		else{
			l = l - iw;
		}
		var r = -1 * (l - (iw+6));  //return at this point
		//var r = (-1 * (l - 2*iw)) +'px';  //return at this point
		//console.log(r +' - '+ w);
		if(r == w){
			l = iw + 4;
		}
		if(r < 0){
			l = (-1*w) + 2*iw + 6;
		}
		//console.log(l);
		var act = $('#produktSlider > .products > ul > .active');
		act.removeClass('active');
		$('#produktSlider > .products > ul').animate({opacity: 0.5, left : l}, 500, function(){
			if(direction == 'prev'){
				if(act.prev().length == 0) {
					$('#produktSlider > .products > ul > li:last').addClass('active');
				}else {
					act.prev().addClass('active');
				}
			}else{
				if(act.next().length == 0) {
					$('#produktSlider > .products > ul > li:first').addClass('active');
				}else {
					act.next().addClass('active');
				}
			}
			$('#produktSlider > .products > ul').animate({opacity: 1});
		});
	}
}


function teaserSlide(oldId, rel){
	$('#teaser_'+ oldId).slideUp(1000);	
	$('#header_'+ oldId).removeClass('open');
	$('#teaser_'+ rel).slideDown(1000);
	$('#header_'+ rel).addClass('open');
	return rel;
}

function rotate(id) {
	var current = ($('div#'+ id +' ul li.show')?  $('div#'+ id +' ul li.show') : $('div#'+ id +' ul li:first'));
    if ( current.length == 0 ) current = $('div#'+ id +' ul li:first');
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#'+ id +' ul li:first') :current.next()) : $('div#'+ id +' ul li:first'));
	next.fadeIn(1000).addClass('show');
	current.fadeOut(1000).removeClass('show');
};

function rotateBack(id) {	
	var current = $('div#'+ id +' ul li.show');
    if ( current.length == 0 ) current = $('div#'+ id +' ul li:first');
	var prev = ((current.prev().length) ? ((current.prev().hasClass('show')) ? $('div#'+ id +' ul li:last') :current.prev()) : $('div#'+ id +' ul li:last'));
	prev.fadeIn(1000).addClass('show');
	current.fadeOut(1000).removeClass('show');
};


//Google maps


function createMarker(point,html,icon) {
	var marker = new GMarker(point,icon);
	GEvent.addListener(marker,"click",function(){
		marker.openInfoWindowHtml(html);
	});
	return marker;
}

function showAddress(address) {
	var zoom = 11;
	if(address == 'Deutschland' || address == 'Belgien'|| address == 'England'){
		zoom = 5;
		$('.selectCity').val('0');
	}
	geocoder.getLatLng(address, function(point) {
		if (!point) {
	    	alert(address + " not found");
	    } else {
	        map.setCenter(point, zoom);
	    }
	});
}
