var currentMovedPage = false;

jQuery.noConflict();
(function($) { 
	$(function() {

if(!jQuery.browser.webkit) {
	jQuery('body.home #menuBox').css({display: 'none'})
}


// moreTag
var moreTag = $('#content a.moreTag');
if(moreTag.length == 1) {
	moreTag.parent().find('~ *').hide();
}

$(moreTag).click(function() {
	moreTag.parent().find('~ *').slideDown();
	moreTag.css({'visibility': 'hidden'});
	return false;
})



//	jQuery('body.home #menuBox').hide();

		// hack fuer formular fehler
		$("h2:contains('@')").css("display", "none");

		$('.hcolumn').click(function() {
			$.each($(this).find('a'), function(index, el) {
				attr = $(el).attr('href');
				if (typeof attr !== 'undefined' && attr !== false) {
					window.location.href = attr;
				}

			})
//			window.location.href = $(this).find('a').attr('href');
		});

		$('.countryItem').click(function() {
			$('.languagesC').hide();
			$('.countryItem').removeClass('selected');
			$(this).addClass('selected');
			$('.' + $(this).attr('id') + 'Sel').show();
		})


		// homepage
		var homeVideoCount = $('.homeVideo .tx-flvplayer2-pi1').length;

		if(homeVideoCount > 1) {
			$('div.videoNavigation').show();
			$('div.videoNavigation').css('display', 'block');
			// we need to switch left and rigth
			
			$('a.videoForward').addClass('active');
			$('a.videoForward').click(function() {
				$('a.videoBack').addClass('active');
			
				var projektImgWidth = -512;
				var currentPos = parseInt($('div.homeVideo').css('margin-left'));
				if((currentPos / projektImgWidth) < parseInt(homeVideoCount)-1) {
					var newPos = (currentPos / projektImgWidth + 1) *  projektImgWidth;
					$('div.homeVideo').animate({
						marginLeft: newPos + 'px'
					})
				}
				if(!((currentPos / projektImgWidth) < parseInt(homeVideoCount)-2)) {
					$('a.videoForward').removeClass('active');
				}
			})

			$('a.videoBack').click(function() {
				var projektImgWidth = 512;
				$('a.videoForward').addClass('active');

				var currentPos = parseInt($('div.homeVideo').css('margin-left'));
				if((currentPos / projektImgWidth) < 0) {
					var newPos = (currentPos / projektImgWidth + 1) *  projektImgWidth;
					$('div.homeVideo').animate({
						marginLeft: newPos + 'px'
					})
				}
				if((currentPos / projektImgWidth) == -1) {
					$('a.videoBack').removeClass('active');
				}
			})
		}

		var homeTeaserCount = $('#productsTeaserContainer .teaser').length;
		if(homeTeaserCount > 2) {
			$('div.teaserNavigation').show();
			// we need to switch left and rigth
			
			$('a.teaserForward').addClass('active');
			
			$('a.teaserForward').click(function() {
				var teaserHeight = -160;
				
				$('a.teaserBack').addClass('active');
				
				var currentPos = parseInt($('#productsTeaserContainer').css('margin-top'));
				if((currentPos / teaserHeight) < parseInt(homeTeaserCount)-2) {
					var newPos = (currentPos / teaserHeight + 1) *  teaserHeight;
					$('#productsTeaserContainer').animate({
						marginTop: newPos + 'px'
					})
				}
				if(!((currentPos / teaserHeight) < parseInt(homeTeaserCount)-3)) {
					$('a.teaserForward').removeClass('active');
				}

			})
			
			$('a.teaserBack').click(function() {
				var teaserHeight = 160;
				
				$('a.teaserForward').addClass('active');
				
				var currentPos = parseInt($('#productsTeaserContainer').css('margin-top'));
				if((currentPos / teaserHeight) < 0) {
					var newPos = (currentPos / teaserHeight + 1) *  teaserHeight;
					$('#productsTeaserContainer').animate({
						marginTop: newPos + 'px'
					})
				}
				
				if(currentPos / teaserHeight == -1) {
					$('a.teaserBack').removeClass('active');
				}
			})
		}

		// homepage END
		
		if($("a.lightbox").length > 0) {

			$("a.lightbox").click(function() {

				return false;
			});

			$("a.lightbox").fancybox({
				'transitionIn'		: 'elastic',
				'transitionOut'		: 'elastic',
				'titlePosition' 	: 'over'
	//			'type'				: 'ajax'
			});
		}
		
		
		
		if($("a.openinline").length > 0) {



			$("a.openinline").click(function() {
				mhref = getAbsolutePath() + $(this).attr('href');
				console.log(mhref+'?type=200');
			$.fancybox({
				'transitionIn'		: 'elastic',
				'transitionOut'		: 'elastic',
				'titlePosition' 	: 'over'
	//			'type'				: 'ajax'
			});
			return false;
			});
			

			

		}
	
	
		$('div.historyimages div.csc-default').hover(function() {

			var singleImage = $(this).find('.csc-textpic-single-image');
			var text = $(this).find('.csc-textpic-text');
			var top = singleImage.position().top;
			var left = singleImage.position().left;

			//text.attribute('left', left);
			text.css('top', top);
			text.css('left', left);
			text.fadeIn();
			
			
		}, function() {
			var text = $(this).find('.csc-textpic-text');
			text.fadeOut();
		});
		
	
		$('.swTrigger, .swClose').click(function() {
			toggleSwitcher();
		});

		$('#content.faq .faqHeader a').click(function() {
			l = $(this).parent().parent().next('.faqText');
			if($(this).hasClass('active')) {
				$(this).removeClass('active');
			} else {
				$(this).addClass('active');
			}
			l.children('p').toggle();
		})

		
		$('.accordion').click(function() {
			$(this).nextAll('.filelinks');
		})
		
		$.each($('.multimediaContainer ul li a'), function(index, el) {
			link = 'http://www.messer-cs.com/' + $(el).attr('href');
			$(el).fancybox({src: link, type: 'iframe', width: 465, height: 277});
		});

		
		
//		$(".multimediaContainer ul li a").fancybox();
		
		// init components
		jQuery('div.rightsidebar div.components ul li ul').first().show();
		jQuery('div.rightsidebar div.components ul li a').first().addClass('selected');

		$('div.rightsidebar div.components a').click(function() {

			var url = $(this).attr('href');
			var loadUrl = url + '?type=200';
			var toMove = $(this).parent().attr('id');
			var curWidth = parseInt($('#content').css('width'));

			// animation is only for ie8+, ff and safari
			//if(!$.browser.msie || ($.browser.msie && jQuery.browser.version > 7)) {
			if(!$(this).parent().hasClass('hasSub')) {
				$('#'+currentMovedPage+' a').removeClass('active');
				$(this).addClass('active');
								
				if(currentMovedPage !== false) {
					if(currentMovedPage != toMove) {
						// component is shown and another component is clicked
						// check if component is already loaded
						if($('#content_' + toMove).length) {
							$('#content_' + toMove).css('marginLeft', 0);
							$('#content_' + toMove).appendTo('#content');
							movePageLeft($('#content_' + currentMovedPage));
							currentMovedPage = toMove
						} else {
							$('#content').css('width', curWidth + 500);
							clone = $('#content_' + currentMovedPage).clone();
							clone.attr('id', 'content_'+toMove);
							clone.appendTo('#content');
							$('.loader').show();
							$('#content_'+toMove).load(loadUrl, function() {
								movePageLeft($('#content_' + currentMovedPage));
								currentMovedPage = toMove
								$('.loader').hide();
							});
						}
					} 
				} else {
					$('#content').css('width', curWidth + 500);
					clone = $('#contentBox .completeContentBlock').clone();
					clone.attr('id', 'content_'+toMove);
					clone.appendTo('#content');
					// load content for clicked site
					$('.loader').show();
					$('#content_'+toMove).load(loadUrl, function() {
						movePageLeft($('#contentBox .completeContentBlock:first'));
						currentMovedPage = toMove
						$('.loader').hide();
					});
				}
			}
			return false;
		});
		
		$('div.rightsidebar ul li.hasSub >a').click(function() {
			if($(this).next('ul').is(':visible')) {
				$(this).removeClass('selected');
			} else {
				$(this).addClass('selected');
			}
			$(this).next('ul').slideToggle();
			return false;
		});
		
		$('div.rightsidebar h1.csc-firstHeader').click(function() {
			$(this).parent().next('div').toggle();
			return false;
		
		});

		
		
		/*
		jQuery('div.rightsidebar ul li a').click(function() {
			jQuery('div.rightsidebar ul li ul li ul').slideUp();
			jQuery('div.rightsidebar ul li ul li  a').removeClass('selected');
			var $div = jQuery(this).next('ul');
			var height = jQuery(this).next('ul').height();
			jQuery(this).next('ul').slideToggle();
			
			jQuery(this).next('ul').animate({
				
			});
			
			jQuery(this).addClass('selected');
			return false;
		});
	*/
	
	/*
		$('#languageSwitcher').hover(
			function() {
				$('#languageSelector').show();
			},
			function() {
				$('#languageSelector').hide();			
			}
		);
		*/
		$('#menuitem217 a').click(function() {
			// load the content
			//return false;
		});
		
	
	$(".woetrailer").click(function() {



		jQuery.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'		: 1024,
			'height'		: 596,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {
			'wmode'		: 'transparent',
			'allowfullscreen'	: 'true'
			}
		});
		return false;
	});
	
	

	
	});
	

	
	
})(jQuery);


function movePageRight() {

	jQuery('#contentBox').animate({
		left: 791
	}, 500);
	
	jQuery('.compSlider').animate({
		left: 768,
		width: 0
	}, 500);

}

function movePageLeft(el) {
	el.animate({
		marginLeft: '-503px'
	}, 500);	
}

function moveNextLeft(el1, el2) {
	jQuery(el1).css('z-index', '5');
	jQuery(el1).animate({
		width: 0,
		left: 0
	}, 500);
	
	jQuery(el2).animate({
		left: 224,
		width: 544	
	}, 500);
}

function toggleSwitcher() {
	
	if(jQuery('#switcher').css('left') != '-224px') {
		jQuery('#overlay').animate({
			opacity: 0
		}, 300, '', function() {
			jQuery('#overlay').hide();	
		});
		
		jQuery('#switcher').animate({
			left: -224
		});
		jQuery('.container').animate({
			marginLeft: 0
		});
	} else {
		jQuery('#overlay').css({
			left: 0,
			opacity: 0
		});
		
		jQuery('#overlay').show().animate({
			opacity: 0.8
		}, 300);
		
		
		jQuery('#switcher').animate({
			left: 0
		}, 300);
		jQuery('.container').animate({
			marginLeft: 224
		}, 300);
		
	}
}

function startPage() {
	jQuery('body.home #menuBox').fadeIn();
//	jQuery('body.home #menuBox').show();
}

function getAbsolutePath() {
    var loc = window.location;
    var pathName = loc.pathname.substring(0, loc.pathname.indexOf('/') + 1);
    return loc.href.substring(0, loc.href.length - ((loc.pathname + loc.search + loc.hash).length - pathName.length));
}
