/*
 * jQuery Functions for
 * Krause Golf Design
 *
 * written by MEDIA DSIGN | Hendrik Meyer 2010
 * www.media-dsign.de
 *
 */
$(function(){
	$('#navi li').each(function(i, item){
		if(i + 1 >= $('#navi li').length){
			$(item).css('background','none');
		}
	});
	/*var lastpos = $('#navi li:eq(' + ($('#navi li').length - 1) + ')').position();
	if(lastpos.left < 400){
		$('#navi li a').css('fontSize','19px');
		$('#telefon').css('fontSize','20px');
		if(lastpos.left < 400){
			$('#navi li a').css('fontSize','18px');
			$('#telefon').css('fontSize','19px');
			if(lastpos.left < 400){
				$('#navi li a').css('fontSize','17px');
				$('#telefon').css('fontSize','18px');
			}
		}
	}*/
	// Move Slogan, for IE
	if($.browser.msie && $('#slogan').length>0){
		$('body').live('mouseover',function(){
			$('#slogan span').each(function(i, spn){
				if(i == 0){
					$(this).css('color','#304925');
					$(this).css('margin-right','25px');
				} else if(i==1){
					$(this).css('color','#8DCC6D');
					$(this).css('margin-right','10px');
					$(this).css('font-size','35px');
				} else if(i==2){
					$(this).css('color','#91D56E');
					$(this).css('margin-right','20px');
					$(this).css('font-size','30px');
				} else if(i==3){
					$(this).css('color','#304925');
					$(this).css('margin-right','0px');
				}
			});
			$('#telefon span').each(function(i, spn){
				if(i == 0 || i == 2){
					$(this).css('marginTop','20px');
				}
			});
		});
	}

	// news & boxes
	$('#boxes ul li[id!=active]').mouseover(function(){
		/*$(this).css('backgroundImage', 'url(/img/teaser_aktiv.png)');*/
		$(this).stop().animate(	{ marginTop: '0px' }, {queue:false, duration:300, easing: 'jswing'}	);
	}).mouseout(function(){
		/*$(this).css('backgroundImage', 'url(/img/teaser_inaktiv.png)');*/
		$(this).stop().animate(	{ marginTop: '15px' },{queue:false, duration:300, easing: 'jswing'}	);
	});
	// boxes small
	$('#boxes.small ul li[id!=active]').mouseover(function(){
		/*$(this).css('backgroundImage', 'url(/img/teaser_small_aktiv.png)');*/
		$(this).stop().animate(	{ marginTop: '0px' }, {queue:false, duration:300, easing: 'jswing'}	);
	}).mouseout(function(){
		/*$(this).css('backgroundImage', 'url(/img/teaser_small_inaktiv.png)');*/
		$(this).stop().animate(	{ marginTop: '15px' },{queue:false, duration:300, easing: 'jswing'}	);
	});
	// box slider
	if($('#boxes ul li').length > 4){
		/*$('#boxes ul li').css('position','relative');
		if($.browser.msie && ($.browser.version=='7.0' || $.browser.version=='6.0')){*/
			$('#boxes ul li').css('position','absolute');
			$('#boxes ul li').each(function(i, item){
				$(this).css('left',(175 * i) + 'px');
			});
			$('#boxes.small ul li').each(function(i, item){
				$(this).css('left',(130 * i) + 'px');
			});
		/*}*/
		$('#boxes .slider_right').click(function(){
			var boxleft = eval($('#boxes ul').css('marginLeft').replace('px',''));
			var boxwidth = eval($('#boxes ul li').css('width').replace('px',''));
			if(boxleft >= ((($('#boxes li').length - 4) * boxwidth) * -1) ){
				$('#boxes ul').stop().animate({ marginLeft: '-=175px' },{queue:false, duration:500, easing: 'jswing'});
				$('#boxes').css('width','788px');
				$('#boxes .slider_left').fadeIn();
			} else {
				$('#boxes .slider_right').fadeOut();
			}
			return false;
		});
		$('#boxes .slider_left').click(function(){
			var boxleft = $('#boxes ul').css('marginLeft').replace('px','');
			if(boxleft < 0 ){
				$('#boxes ul').stop().animate({ marginLeft: '+=175px' },{queue:false, duration:500, easing: 'jswing'});
				if(eval($('#boxes ul').css('marginLeft').replace('px','')) == -175){
					$('#boxes .slider_left').fadeOut('fast');
					$('#boxes').stop().animate({width: '740px'},300);
				}
			}
			return false;
		});
	}

	// mini slider rotation
	function goSlider(){
		var boxleft = eval($('#court_slider ul').css('marginLeft').replace('px',''));
		var sliderleft = eval((($('#court_slider li').length - 7) * -60));
		if (boxleft > sliderleft ){
				$('#court_slider ul').stop().animate({ marginLeft: '-=60px' },{queue:false, duration:500, easing: 'jswing'});
				$('#court_slider .slider_mini_left').fadeIn();
				if((boxleft - 60) <= sliderleft){
					$('#court_slider .slider_mini_right').fadeOut('fast');
				}
		} else if(boxleft < 0 ){
			$('#court_slider ul').stop().animate({ marginLeft: '0px' },{queue:false, duration:500, easing: 'jswing'});
			$('#court_slider .slider_mini_left').fadeOut('fast');
			$('#court_slider .slider_mini_right').fadeIn('fast');
		}
	}
	// mini slider manually
	if($('#court_slider ul li').length >= 7){
		$('#court_slider .slider_mini_right').fadeIn('fast');
		$('#court_slider ul li').css('position','absolute');
		$('#court_slider ul li').each(function(i, item){
			$(this).css('left',(60 * i) + 'px');
		});
		$('#court_slider .slider_mini_right').click(function(){
			var boxleft = eval($('#court_slider ul').css('marginLeft').replace('px',''));
			var sliderleft = eval((($('#court_slider li').length - 7) * -60));
			if(boxleft > sliderleft ){
				$('#court_slider ul').stop().animate({ marginLeft: '-=60px' },{queue:false, duration:500, easing: 'jswing'});
				$('#court_slider .slider_mini_left').fadeIn();
				if((boxleft - 60) <= sliderleft){
					$('#court_slider .slider_mini_right').fadeOut('fast');
				}
			}
			return false;
		});
		$('#court_slider .slider_mini_left').click(function(){
			var boxleft = eval($('#court_slider ul').css('marginLeft').replace('px',''));
			if(boxleft < 0 ){
				$('#court_slider ul').stop().animate({ marginLeft: '+=60px' },{queue:false, duration:500, easing: 'jswing'});
				if(eval($('#court_slider ul').css('marginLeft').replace('px','')) >= -60){
					$('#court_slider .slider_mini_left').fadeOut('fast');
				}
				if(eval($('#court_slider ul').css('marginLeft').replace('px','')) == -420){
					$('#court_slider .slider_mini_right').fadeIn('fast');
				}
			}
			return false;
		});
		if($('#cactive').length>0){
			var cleft = eval($('#cactive').css('left').replace('px',''));
			if(cleft > 360){
				$('#court_slider ul').stop().animate({ marginLeft: '-=' + (cleft - 360) + 'px' },{queue:false, duration:500, easing: 'jswing'})
				$('#court_slider .slider_mini_left').fadeIn('fast');
				var sliderleft = eval((($('#court_slider li').length - 1) * 60));
				if(cleft == sliderleft){
					$('#court_slider .slider_mini_right').fadeOut('fast');
				}
			}
		}
		/*$('#court_slider ul li a img')
		.mouseover(function(){
			$(this).stop().animate({ height: '+=10px',width: '+=10px' },{ duration:200, easing: 'jswing' });
		})
		.mouseout(function(){
			if($(this).height()>35 || $(this).width()>35){
				$(this).stop().animate({ height: '-=10px',width: '-=10px' },{ duration:200, easing: 'jswing' });
			}
		});*/

		/*window.setInterval(goSlider, 3000);*/
	}

	// courses gallery
	if($('#court_gallery').length>0){
		$('#court_gallery ul li').css('position','absolute');
		var liTop = 0;
		var wrapperHeight = 0;
		$('#court_gallery ul li').each(function(i, item){
			$(this).css('top',liTop + 'px');
			liTop += $(this).outerHeight();
			if($.browser.msie && ($.browser.version=='8.0' || $.browser.version=='7.0' || $.browser.version=='6.0')){
				liTop += 4;
			}
			if(i <= 2){
				wrapperHeight += $(this).outerHeight();
				if($.browser.msie && ($.browser.version=='8.0' || $.browser.version=='7.0' || $.browser.version=='6.0')){
					wrapperHeight += 4;
				}
			}
		});
		$('#court_gallery .gallery_wrapper').css('height',wrapperHeight);
		var scrollSize = Math.ceil(liTop / $('#court_gallery ul li').length);
		$('#court_gallery .slider_up').live('click',function(){
			var boxtop = eval($('#court_gallery ul').css('marginTop').replace('px',''));
			if(boxtop < 0){
				$('#court_gallery ul').stop().animate({ marginTop: '+=' + scrollSize + 'px' },{queue:false, duration:500, easing: 'jswing'});
			}
			return false;
		});
		$('#court_gallery .slider_down').live('click',function(){
			var boxtop = eval($('#court_gallery ul').css('marginTop').replace('px',''));
			var slidertop = eval((($('#court_gallery li').length - 3) * (scrollSize * -1)));
			if(boxtop > slidertop ){
				$('#court_gallery ul').stop().animate({ marginTop: '-=' + scrollSize + 'px' },{queue:false, duration:500, easing: 'jswing'});
			}
			return false;
		});

		if($('.newsentry').length>0){
			$('.newsentry').width($('.newsentry').outerWidth() - $('#court_gallery').width() - 20);
		}
	}

	// round corners
	$('.wer-wir-sind #content div img').wrap('<div class="roundcorners" style="background: url(' + $('.wer-wir-sind #content div img').attr('src') + ');width: ' + $('.wer-wir-sind #content div img').css('width') + '; height: ' + $('.wer-wir-sind #content div img').css('height') + ';" />').hide();
	$('.wer-wir-sind #content div#imgoverlay').css('height',$('.wer-wir-sind #content div img').css('height'));
	$('.who-we-are #content div img').wrap('<div class="roundcorners" style="background: url(' + $('.who-we-are #content div img').attr('src') + ');width: ' + $('.who-we-are #content div img').css('width') + '; height: ' + $('.who-we-are #content div img').css('height') + ';" />').hide();
	$('.who-we-are #content div#imgoverlay').css('height',$('.who-we-are #content div img').css('height'));

	/*if($('.design').length==0 && ($('.was-wir-tun').length>0 || $('.what-we-do').length>0)){
		$('.was-wir-tun #content div[id!=subnavi] img').wrap('<div class="roundcorners" style="background: url(' + $('.was-wir-tun #content div:eq(1) img').attr('src') + ');width: ' + $('.was-wir-tun #content div:eq(1) img').css('width') + '; height: ' + $('.was-wir-tun #content div:eq(1) img').css('height') + '; margin:0;" />').hide();
		$('.was-wir-tun #content div#imgoverlay').css('height',$('.was-wir-tun #content div:eq(1) img').css('height'));
		$('.what-we-do #content div[id!=subnavi] img').wrap('<div class="roundcorners" style="background: url(' + $('.what-we-do #content div:eq(1) img').attr('src') + ');width: ' + $('.what-we-do #content div:eq(1) img').css('width') + '; height: ' + $('.what-we-do #content div:eq(1) img').css('height') + '; margin:0;" />').hide();
		$('.what-we-do #content div#imgoverlay').css('height',$('.what-we-do #content div:eq(1) img').css('height'));
	}*/
	if($('#what_we_do_start').length>0){
		$('.was-wir-tun #content div[id!=subnavi] img').wrap('<div class="roundcorners" style="background: url(' + $('.was-wir-tun #content div:eq(1) img').attr('src') + ');width: ' + $('.was-wir-tun #content div:eq(1) img').css('width') + '; height: ' + $('.was-wir-tun #content div:eq(1) img').css('height') + '; margin:0;" />').hide();
		$('.was-wir-tun #content div#imgoverlay').css('height',$('.was-wir-tun #content div:eq(1) img').css('height'));
		$('.what-we-do #content div[id!=subnavi] img').wrap('<div class="roundcorners" style="background: url(' + $('.what-we-do #content div:eq(1) img').attr('src') + ');width: ' + $('.what-we-do #content div:eq(1) img').css('width') + '; height: ' + $('.what-we-do #content div:eq(1) img').css('height') + '; margin:0;" />').hide();
		$('.what-we-do #content div#imgoverlay').css('height',$('.what-we-do #content div:eq(1) img').css('height'));
	}

	/*$('.design #content div[id!=subnavi] img').wrap('<div class="roundcorners" style="background: url(' + $('.design #content div:eq(0) img').attr('src') + ');width: ' + $('.design #content div:eq(0) img').css('width') + '; height: ' + $('.design #content div:eq(0) img').css('height') + '; margin:0;" />').hide();
	$('.design #content div#imgoverlay').css('height',$('.design #content div:eq(0) img').css('height'));
	$('.design-en #content div[id!=subnavi] img').wrap('<div class="roundcorners" style="background: url(' + $('.design-en #content div:eq(0) img').attr('src') + ');width: ' + $('.design-en #content div:eq(0) img').css('width') + '; height: ' + $('.design-en #content div:eq(0) img').css('height') + '; margin:0;" />').hide();
	$('.design-en #content div#imgoverlay').css('height',$('.design-en #content div:eq(0) img').css('height'));*/

	$('#courses_start div img').wrap('<div class="roundcorners" style="background: url(' + $('#courses_start div img').attr('src') + ');width: ' + $('#courses_start div img').css('width') + '; height: ' + $('#courses_start div img').css('height') + ';" />').hide();
	$('#courses_start div#imgoverlay').css('height',$('#courses_start div img').css('height'));
	$('#redesign_start div img').wrap('<div class="roundcorners" style="background: url(' + $('#redesign_start div img').attr('src') + ');width: ' + $('#redesign_start div img').css('width') + '; height: ' + $('#redesign_start div img').css('height') + ';" />').hide();
	$('#redesign_start div#imgoverlay').css('height',$('#redesign_start div img').css('height'));

	// nature items
	$('#nature_item a[title]').poshytip({ className: 'tip-darkgray', followCursor: true});
	$('#nature_item a').click(function(){return false;})

	// courts text adjust
	if($('#court_text').length>0 && $('#court_gallery').length>0){
		if(eval($('#court_text').css('height').replace('px','')) > eval($('#court_gallery').css('height').replace('px',''))){
			$('#court_gallery').css('height',$('#court_text').css('height'));
		}
	}
	if($('#court_menu').length>0){
		$('#court_menu').change(function(){
			window.location.href = $('#court_menu option:selected').val();
		});
	}

	// what we do
	if($('#subnavi').length>0){
		if($.browser.msie && ($.browser.version=='7.0' || $.browser.version=='6.0')){
			$('#subnavi ul li img').each(function(){
				$(this).parent().find('a').css('background','url(' + $(this).attr('src') + ') no-repeat 50% 50%');
				$(this).hide();
			});
		}
		$('#subnavi ul')
			.before('<a href="#" class="slider_up"></a>')
			.after('<a href="#" class="slider_down"></a>')
			.wrap('<div class="wrapper" />');
		$('#subnavi ul li').css('position','absolute');
		$('#subnavi ul li').each(function(i, item){
			$(this).css('top',(68 * i) + 'px');
		});
		$('#subnavi .slider_up').live('click',function(){
			var boxtop = eval($('#subnavi ul').css('marginTop').replace('px',''));
			if(boxtop < 0){
				$('#subnavi ul').stop().animate({ marginTop: '+=68px' },{queue:false, duration:500, easing: 'jswing'});
			}
			return false;
		});
		$('#subnavi .slider_down').live('click',function(){
			var boxtop = eval($('#subnavi ul').css('marginTop').replace('px',''));
			if($.browser.msie && ($.browser.version=='7.0' || $.browser.version=='6.0')){
				var slidertop = eval((($('#subnavi li').length - 3) * -68) + 68);
			} else {
				var slidertop = eval((($('#subnavi li').length - 3) * -68));
			}
			if(boxtop > slidertop ){
				$('#subnavi ul').stop().animate({ marginTop: '-=68px' },{queue:false, duration:500, easing: 'jswing'});
			}
			return false;
		});
		if($('#sactive').length>0){
			var suTop = eval($('#sactive').css('top').replace('px',''));
			if(suTop >= 204){
				if($('.browserChrome')){
					var reduce = 136;
				} else {
					var reduce = 120;
				}
				$('#subnavi ul').stop().animate({ marginTop: '-=' + (suTop - reduce) + 'px' },{queue:false, duration:500, easing: 'jswing'})
			}
		}
	}

	/* Navi Fix for old Browsers without @font-face */
	/*var userAgent = navigator.userAgent.toLowerCase();*/
	if($.browser.mozilla){
		var userAgent = navigator.userAgent.toLowerCase();
        //Is it Firefox?
        if(navigator.userAgent.toLowerCase().indexOf('firefox') != -1){

            // Add the version number
            userAgent = userAgent.substring(userAgent.indexOf('firefox/') +8);
            userAgent = userAgent.substring(0,3);

            if(userAgent < 3.5){
            	$('#navi ul li a').css('padding','7px 11px 5px 11px');
            	$('#boxes ul li a span.newstitle').css('fontSize','20px');
            	$('#telefon').css('fontSize','20px');
            }
        }
    }

	/* courses logo rotation */
	if($('#courses_start_logos').length){
			/* This code is run on page load */

			var deg=0;
			var dif=-0.5;

			/* Assigning the buttons to a variable for speed: */
			var arr = $('#courses_start_logos div');

			/* Storing the length of the array in a viriable: */
			var len = arr.length;

			/* Finding the centers of the animation container: */
			var centerX = $('#courses_start_logos').width()/2 - 60;
			var centerY = $('#courses_start_logos').height()/2 - 30;

			/* Applying relative positioning to the buttons: */
			arr.css('position','absolute');

			/* The function inside the interva is run 25 times a second */
			setInterval(function(){

			/* This forms an area with no activity in the middle of the stage */
			if(Math.abs(dif)<0.5) return false;

			/* Increment the degrees: */
			deg+=dif;

			var cwidth = 290;

			/* Loop through all the buttons: */
			$.each(arr,function(i){

				/* Calculate the sine and cosine */

				var eSin = Math.sin(((360/len)*i+deg)*Math.PI/180);
				var eCos = Math.cos(((360/len)*i+deg)*Math.PI/180);

				/* Setting the css properties */
				$(this).css({
					top:centerY+80*eSin,
					left:centerX+cwidth*eCos,
					zIndex:Math.round(80+eSin*20)
				});
				/*if(!$.browser.msie){
					$(this).css({
						opacity:0.8+eSin*0.1
					});
				}*/
			})

			},50);

			/* Detecting the movements on the mouse and speeding up or reversing the rotation accordingly: */

			/*var over=false;
			$("#courses_start_logos ul").mousemove(function(e){

				if(!this.leftOffset)
				{
					/* This if section is only run the first time the function is executed. */
					/*this.leftOffset = $(this).offset().left;
					this.width = $(this).width();
				}*/

				/* If the mouse is over a button, set dif to 0, which stops the animation */
				/*if(over) dif=0;
				else
				dif = -5+(10*((e.pageX-this.leftOffset)/this.width));*/

				/* In the other case calculate the speed according to the X position of the mouse */
			/*});*/

			var m_over = false;
			$("#courses_start_logos div img").each(function(){
				var tw = $(this).width();
				var twz = $(this).width() + 20;
				var th = $(this).height();
				var thz = $(this).height() + 20;
				$(this).mouseover(function(){
					if(m_over==false){
						m_over = true;
						$(this).stop().animate({ height: thz + 'px', width: twz + 'px' },{queue: false, duration: 500 });
						$(this).parent().parent().parent().find('a').stop().animate({ fontSize: '12px' },{queue: false, duration: 500 });
					}
				}).mouseout(function(){
					if(m_over == true){
						m_over = false;
						$(this).stop().animate({ height: th + 'px', width: tw + 'px' },{queue: false, duration: 500 });
						$(this).parent().parent().parent().find('a').stop().animate({ fontSize: '10px' },{queue: false, duration: 500 });
					}
				});
			});

			/* Detecting whether the mouse is positioned above a share button: */
			$("#courses_start_logos div").hover(
				function(){over=true;dif=0;},
				function(){over=false;dif=-0.5;}
			);

			$('#courses_start_logos div a[title]').poshytip({ className: 'tip-darkgray', followCursor: true});
			/*$('#courses_start_logos_table div a[title]').poshytip({ className: 'tip-darkgray', followCursor: true});*/
	}
});
