$(window).load(function(){
	$(".img img").each(function() {
		$(this).wrap(function(){
			return '<span style="background:url(' + $(this).attr('src') + ') no-repeat center center; width: ' + $(this).width() + 'px; height: ' + $(this).height() + 'px;" />';
		});
		$(this).css("display","none");
	});
});

$(window).resize(function() {
	if($(window).width() > 1140) {
		$('body').css('paddingTop','20px');
		$('.socialWrap').removeClass('topPosition');
		$('.socialWrap').addClass('sidePosition');
		$('.sideSocial').removeClass('closed');
		$('.topSocial').addClass('closed');

		var $scrollingDiv = $(".socialWrap");			
		$(window).scroll(function(){			
			var wrapheight = $('body').height();
			
			if($(window).scrollTop()>132){
				
				if(wrapheight>($(window).scrollTop() + $('.socialWrap').height())) {
					$scrollingDiv
					.removeClass("sideAbsolutPos")
					.addClass("sideFixedPos");
				}
			} 
			else {
				$scrollingDiv
				.removeClass("sideFixedPos")
				.addClass("sideAbsolutPos");
			}
		});
	} else {
		$('body').css('paddingTop','70px');
		$('.socialWrap').removeClass('sidePosition');
		$('.socialWrap').addClass('topPosition');
		$('.topSocial').removeClass('closed');
		$('.sideSocial').addClass('closed');
	}
});

$(document).ready(function(){
	if($(window).width() > 1140) {
		$('body').css('paddingTop','20px');
		$('.socialWrap').removeClass('topPosition');
		$('.socialWrap').addClass('sidePosition');
		$('.sideSocial').removeClass('closed');
		$('.topSocial').addClass('closed');

		var $scrollingDiv = $(".socialWrap");			
		$(window).scroll(function(){			
			var wrapheight = $('body').height();
			
			if($(window).scrollTop()>132){
				
				if(wrapheight>($(window).scrollTop() + $('.socialWrap').height())) {
					$scrollingDiv
					.removeClass("sideAbsolutPos")
					.addClass("sideFixedPos");
				}
			} 
			else {
				$scrollingDiv
				.removeClass("sideFixedPos")
				.addClass("sideAbsolutPos");
			}
		});
	} else {
		$('body').css('paddingTop','70px');
		$('.socialWrap').removeClass('sidePosition');
		$('.socialWrap').addClass('topPosition');
		$('.topSocial').removeClass('closed');
		$('.sideSocial').addClass('closed');
	}
});


$(document).ready(function(){
	validTarget("a.blank");
	valideForm("form.validateForm");

	$('.noClick').click(function(){ return false; });
	$('.nav2 li').hover(function(){ $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); });

	$("a.fancy").fancybox({
		'titleShow' : true,
		'titlePosition' : 'inside',
		'transitionIn' : 'elastic',
		'transitionOut' : 'elastic'
	});
	
	$("a.fancyFeedback").fancybox({
		'width': 540,
		'height': 455,
		'autoScale' : false,
		'transitionIn' : 'none',
		'transitionOut' : 'none',
		'type' : 'iframe'
	});
	$("a.fancyBox").fancybox({
		'width': '90%',
		'height': '85%',
		'titleShow' : false,
		'autoScale' : false,
		'transitionIn' : 'none',
		'transitionOut' : 'none',
		'type' : 'iframe'
	});

	$(".fieldItalic input").focus(function(){ $(this).parent('div').addClass('write'); if (this.value==this.title){ this.value=''; } });
	$(".fieldItalic input").blur(function(){ $(this).parent('div').removeClass('write'); if (this.value!=this.title){ $(this).parent('div').addClass('write'); } if (this.value==''){ this.value=this.title; $(this).parent('div').removeClass('write'); } });

	var $scrollingDiv = $(".resultsBox");			
	$(window).scroll(function(){			
		var wrapheight = $('.orderForm').height();
		
		if($(window).scrollTop()>250){
			
			if(wrapheight>($(window).scrollTop() + $('.resultsBox').height())) {
				$scrollingDiv
				.addClass("moveBox")
				.css("top","20px")
				.css("left","50%")
				.css("marginLeft","209px");
			}
		} 
		else {
			$scrollingDiv
			.removeClass("moveBox")
			.css("top","0")
			.css("left","0")
			.css("marginLeft","0");
		}
		//alert(wrapheight);
	});
	
	$('a.scroll').click(function(){
		
		str = this.href;
		var tempx = str.indexOf("#");
		str = str.substr(tempx);
		//alert(str);
		
		$('html,body').animate({scrollTop: $(str).offset().top}, 1000);
		return false;
	});
	
	$('.features li p').css('display','none');
	$('.features li h3').click(function(){
		if($(this).parent('li').children('p').is(':hidden')) {
			$(this).parent('li').children('p').css('display','block');
			$(this).parent('li').addClass('up');
		}
		else {
			$(this).parent('li').children('p').css('display','none');
			$(this).parent('li').removeClass('up');
		}
		return false;
	});
	
	// slider aktualit
	
	var news_size = $('.sldNws p').size();
	var go_to_news;
	
	$('.sldNws div p:first').clone().appendTo('.sldNws div');

	rotateNews = function(){
		var news_positon = $('.sldNws div').css('marginLeft');
		var news_positonl = news_positon.length;
		news_positon = news_positon.substring(0,news_positonl-2);
		news_positon = parseInt(news_positon);
		var active_p = news_positon / 725 * -1;
		if(active_p == (news_size-1)) {
			go_to_news = active_p + 1;
			$('.sldNws div').animate({ marginLeft: go_to_news * 725 * -1 + 'px' },500,function(){
				$('.sldNws div').css('marginLeft',0);
			});
		}
		else {
			go_to_news = active_p + 1;
			$('.sldNws div').animate({ marginLeft: go_to_news * 725 * -1 + 'px' },500);
		}
	};

	rotateNewsSwitch = function(){
		play = setInterval(function(){
			rotateNews();
		}, 4000);
	};

	rotateNewsSwitch();
	
	// slider headline
	
	var headline_size = $('.sldWlcBoxIn div').size();
	var go_to_headline;
	
	rotateHeadline = function(){
		$('.sldWlcBoxIn').animate({ marginLeft: go_to_headline * 945 * -1 + 'px' },500);
		$('.sldNav a').removeClass('cur');
		$('.sldNav a').eq(go_to_headline).addClass('cur');
	};

	rotateHeadlineSwitch = function(){  
			play = setInterval(function(){  
			var headline_positon = $('.sldWlcBoxIn').css('marginLeft');
			var headline_positonl = headline_positon.length;
			headline_positon = headline_positon.substring(0,headline_positonl-2);
			headline_positon = parseInt(headline_positon);
			var active_div = headline_positon / 945 * -1;
	
			if(active_div == (headline_size-1)) {
				go_to_headline = 0;
			}
			else {
				go_to_headline = active_div + 1;
			}
			rotateHeadline();
		}, 6000);
	};

	rotateHeadlineSwitch();
	
	$('.sldWlcBox').append('<div class="sldNav"></div>');
	$('.sldWlcBoxIn div').each(function(){
		$('.sldNav').append('<a href="#"></a>');
	});
	$('.sldNav a:first').addClass('cur');
	$('.sldNav a').click(function(){

		if(!$(this).hasClass('cur'))	{
			//stop(true,true);
			clearInterval(play);
			$(this).addClass('cur').siblings().removeClass('cur');
			go_to_headline = $(".sldNav a").index(this);
			rotateHeadline();
			rotateHeadlineSwitch();
		}
		return false; 
	});

	//------------------------------------------------------------------------	
	// SLIDER
	//------------------------------------------------------------------------

	$(".playerBoxImg li").css('opacity','0').css('zIndex','1');
	$(".playerBoxImg li:first").css('opacity','1').css('zIndex','3').addClass("activeSlide");
	$(".playerBoxImg li:first").next().css('opacity','0').css('zIndex','2').addClass("nextActiveSlide");

	rotate = function(){
		$(".playerBoxImg li").removeClass('activeSlide');
		$(".playerBoxImg li").css('zIndex','1');
		$active_slide.addClass('activeSlide');
		$active_slide.css('zIndex','3');
		$active_slide.animate({opacity:1}, 1000, function(){
			if($(this).prev().length!=0){
				$(this).prev().css('opacity','0');
				$(this).next().css('opacity','0');
				$(".playerBoxImg li:first").css('opacity','0');
			}
			else {
				$(".playerBoxImg li:last").css('opacity','0');
				$(this).prev().css('opacity','0');
				$(this).next().css('opacity','0');
			}
		});

		$(".playerBoxImg li.nextActiveSlide").removeClass('nextActiveSlide');
		$next_active_slide.css('zIndex','2');
		$next_active_slide.addClass('nextActiveSlide');
	};

	rotateSwitch = function(){
		play = setInterval(function(){
			$active_slide = $('.playerBoxImg li.activeSlide').next();
			$next_active_slide = $('.playerBoxImg li.activeSlide').next().next();
			
			if ( $active_slide.length == 0) {
				$active_slide = $('.playerBoxImg li:first');
			}
			if ( $next_active_slide.length == 0) {
				$next_active_slide = $('.playerBoxImg li:first');
			}
			

		if($('.playerBoxImg li:first').hasClass('nextActiveSlide')) {
			$('.btnPlayImg').show();
			$('.playerBoxImg').hide();
			clearInterval(play);
		}

			rotate();
		}, 5000);
	};

	//$(".playerBoxImg ").hover(function() {clearInterval(play);},function() {rotateSwitch();});

	$('.playerBoxImg').css('display','none');
	$('.btnPlayImg').click(function(){
		$(this).fadeOut();
		$('.playerBoxImg').fadeIn();
		$('.playerBoxImg li:first').removeClass('nextActiveSlide');
		$('.playerBoxImg li:first').next('li').addClass('nextActiveSlide');
		rotateSwitch();
		return false;
	});

	//------------------------------------------------------------------------
	// Tooltip
	//------------------------------------------------------------------------
	
	var name_title;
	$('.tooltip').click(function(){return false;});
	$('.tooltip').hover(function(){
		var title = $(this).attr('title');
		name_title = $(this).attr('title');
		$(this).attr('title', '');
		
		if(typeof document.body.style.maxHeight === "undefined") {
			$('select').each(function(){ $(this).css('visibility','hidden'); });
		}
		
		var width_window = $(window).width();
		var height_window = $(window).height();
		
		$('body').append('<div id="tooltip" style="position:absolute; top:-1000px; left:-1000px;"><div>' + name_title + '</div></div>');
		$('.tooltip').mousemove(function(e){
			var offset = $('body').offset();
			var width_tooltip = $('#tooltip').width();
			var height_tooltip = $('#tooltip').height();
			var position_left = Math.round(e.pageX - offset.left);
			var position_top = Math.round(e.pageY - offset.top);
			if(width_window>(position_left + width_tooltip + 10)) {
			      $('#tooltip').css('left', position_left - 20 + 'px');
			      $('#tooltip').removeClass('positionLeftReverse');
			      $('#tooltip').removeClass('positionReverse');
			} else {
				$('#tooltip').css('left', position_left - width_tooltip + 30 + 'px');
				$('#tooltip').addClass('positionLeftReverse');
			}
/*			if((height_window>(position_top - $('html').scrollTop() + 5 + height_tooltip))||(height_window>(position_top - $('body').scrollTop() + 5 + height_tooltip))) {*/

			$('#tooltip').css('top', position_top - height_tooltip - 40 + 'px');
			$('#tooltip').removeClass('positionTopReverse').removeClass('positionReverse');

/*			} else {
				$('#tooltip').css('top', position_top - height_tooltip + 20 + 'px');
				$('#tooltip').addClass('positionTopReverse');
			}
*/
			if($('#tooltip').hasClass('positionLeftReverse')&&$('#tooltip').hasClass('positionTopReverse')) {
				$('#tooltip').removeClass('positionLeftReverse').removeClass('positionTopReverse');
				$('#tooltip').addClass('positionReverse');
			}
		});
	}, function(){
		$('#tooltip').remove();
		$(this).attr('title', name_title);
		
		if(typeof document.body.style.maxHeight === "undefined") {
			$('select').each(function(){ $(this).css('visibility','visible'); });
		}
	});

});

function validTarget(handler) {
    $(handler).click(function(){
        window.open(this.href);
        return false;
    });
}

function valideForm(handler) {
	jQuery.validator.addMethod("defaultInvalid", function(value, element) {
		return value != element.defaultValue;
	}, "");
	
	jQuery.validator.messages.required = "";
	$(handler).validate({
		onkeyup: false
	});
}
