function initNavi() {
	$('#menu-hauptnavigation li').mouseenter(function() {
		if (!$('body').hasClass('noNavistrich')) {
			obj = $(this);
			var Pos = $(obj).position();
			var Breite = $(obj).width();
			$('#navistrich').stop();
			$('#navistrich').css('visibility','visible');
			$('#navistrich').animate({'left':(Pos.left+Breite/2)+'px'},300);
		}
	});
	$('#menu-hauptnavigation').mouseleave(function () {
		if (!$('body').hasClass('noNavistrich')) { 
			obj = $('#menu-hauptnavigation li.current-menu-item');
			var Pos = $(obj).position();
			var Breite = $(obj).width();
			$('#navistrich').stop();
			$('#navistrich').animate({'left':(Pos.left+Breite/2)+'px'},300);
		}
	});
	$('#navistrich').animate({ left: '50px' }, 1000, 
		function() {
			
			obj = $('#menu-hauptnavigation li.current-menu-item');
			if (obj.length == 0) {
				obj = $('#menu-hauptnavigation li.current-page-ancestor');	
			}			
			if (obj.length != 0) {			
				var Pos = $(obj).position();
				var Breite = $(obj).width();
				$('#navistrich').css('visibility','visible');
				$('#navistrich').css('left',(Pos.left+Breite/2)+'px');
			} else {
				$('body').addClass('noNavistrich');
			}
		}
	);
	$('#menu-hauptnavigation li').click(function() {
		$('#menu-hauptnavigation li').removeClass('current-menu-item');
		$(this).addClass('current-menu-item');
	});
}

function initVideolinks() {
	if ($('body').hasClass('desktop')) {
		$('.videolink').click(function() {
			var videofile = $(this).attr('video');
			var imagefile = $('img',this).attr('src');
			flashvars = { video: videofile, autoplay:true, loop:false, image:imagefile};
			params    = { wmode: "transparent", allowFullScreen:true };		
			swfobject.embedSWF("/wp-content/themes/opium/flash/OpiumVideoPlayer.swf", "videobox", "460", "333", "10.0.0", "/wp-content/themes/opium/flash/flash/expressInstall.swf",flashvars,params);
			$('html').animate({scrollTop: 0},1000);
		});
	} else {
		$('.videolink').click(function() {
			var videofile = $(this).attr('mobilevideo');
			var imagefile = $('img',this).attr('src');
			var output = '<video id="homevideo" width="380" height="214" autoplay="autoplay" poster="'+imagefile+'" controls="controls"><source id="videosrc" src="'+videofile+'" type="video/mp4" /></video>';
			$('#html5player').html(output);
		});
	}
}

function setFlash() {
	// Videoplayer
	if($('body').hasClass('page-id-905')) {
		flashvars = { video: "/wp-content/themes/opium/videos/opium_arabic.f4v", autoplay:false, loop:false, image:"/wp-content/themes/opium/img/videopic/opium_arabic.jpg"};
	} else {
		flashvars = { video: "/wp-content/themes/opium/videos/opium_newreel.f4v", autoplay:false, loop:false, image:"/wp-content/themes/opium/img/videopic/opium_newreel.jpg"};
	}
	params    = { wmode: "transparent", allowFullScreen:true };
	swfobject.embedSWF("/wp-content/themes/opium/flash/OpiumVideoPlayer.swf", "videobox", "460", "333", "10", "/wp-content/themes/opium/flash/flash/expressInstall.swf",flashvars,params);
	// Wanted Teaser
	randomNumber = Math.floor(Math.random()*4) + 1;
	flashvars = { video: "/wp-content/themes/opium/videos/JoinUs0"+randomNumber+".flv", videowidth:"338", videoheight: "226", videoy:"-20", autostart:"true", loop:"true", link:"http://www.opiumeffect.de/en/news/#wanted"};
	params    = { bgcolor: '#000000', allowFullScreen:true, allowscriptaccess: 'always' };		
	swfobject.embedSWF("/wp-content/themes/opium/flash/bytebox_SimplePlayer.swf", "wantedflash", "338", "190", "10", "/wp-content/themes/opium/flash/flash/expressInstall.swf",flashvars,params,{name:"wantedVideo"});	
	// Studioanimation
	flashvars = { video: "/wp-content/themes/opium/videos/studio.flv", autostart:"true", loop:"true", videowidth:"380", videoheight:"254", videoy:"-20" };
	params    = { bgcolor: '#000000', allowFullScreen:true, allowscriptaccess: 'always' };		
	swfobject.embedSWF("/wp-content/themes/opium/flash/bytebox_SimplePlayer.swf", "studioanimationflash", "380", "214", "10", "/wp-content/themes/opium/flash/flash/expressInstall.swf",flashvars,params,{name:"studioVideo"});
	// Jessplayer
	$('.jessplayer').each(function() {
		var id = $(this).attr('playerid');
		var videofile = $(this).attr('video');
		var imagefile = $(this).attr('image');
		flashvars = { video: "/wp-content/uploads/"+videofile, autostart:"false", videowidth:"338", videoheight: "191", imagepreview: "/wp-content/uploads/"+imagefile, playimage: "/wp-content/themes/opium/img/icon/play.png", pauseimage: "/wp-content/themes/opium/img/icon/pause.png" };
		params    = { allowFullScreen:true, allowscriptaccess: 'always' };		
		swfobject.embedSWF("/wp-content/themes/opium/flash/bytebox_SimplePlayer.swf", id, "338", "190", "10", "/wp-content/themes/opium/flash/flash/expressInstall.swf",flashvars,params,{name:"swf_"+videofile});
	});
}

function setWorldmap() {
	// Worldmap
	flashvars = { };
	params    = { wmode: "transparent" };
	swfobject.embedSWF("/wp-content/themes/opium/flash/Weltkarte.swf", "worldmap", "400", "300", "10", "/wp-content/themes/opium/flash/flash/expressInstall.swf",flashvars,params);
}

function setBoxHeights() {
	// Halbboxen auf Höhe anpassen
	$('.twohalfs').each(function() {
		var height1 = $('.box:first',this).height();
		var height2 = $('.box:last',this).height();
		if (height1 > height2) {
			$('.box:last',this).css('height',height1+'px');
		} else if (height1 < height2) {
			$('.box:first',this).css('height',height2+'px');
		}
	});	
}

function initContactForm() {
	$('#sendbtn').click(function() {
		var status = checkContactInput();
		if (status) {
			$('.contactform').hide('slow');
			$('#feedback').html('<p><strong>Thank you for your message!</strong></p><p>We will get in touch with you as soon as possible.</p>');
			var linkToFile = '/wp-content/themes/opium/ph'; linkToFile += 'p/__for'; linkToFile += 'm.php?opiu'; linkToFile += 'm=effect';
			$.post(linkToFile, { nam: $('#form_name').val(),ema: $('#form_email').val(),msg: $('#form_msg').val() } );			
		} else {
			alert("Please enter name, email and message. Thank you!");	
		}
	});
}

function checkContactInput() {
	var myReturn = true;
	if ($('#form_name').val().length < 4) {	myReturn = false; }
	if ($('#form_email').val().length < 4) {	myReturn = false; }
	if ($('#form_msg').val().length < 10) {	myReturn = false; }
	return myReturn;
}

function initSubHeadlineZufall() {
	$('.box h5').each(function() {
		var zufall = Math.round(Math.random()*2);
		$(this).css('background-position','0 -'+zufall+'00px');
	});
}

$(document).ready(function() {
		
	// Divider mit Klasse versehen
	$('.box').each(function() { $('.divider:first',this).addClass('first'); });
	
	// Content trimmen
	$('#content p').each(function() {
		var myP = $(this);
		// leere p's entfernen
		if ($(this).html() == '') { $(this).remove(); }
		// enhält einen Anker, p entfernen
		$('a',this).each(function() { if (!$(this).attr('href')) { myP.css({margin:'0',height:'1px',lineHeight:'0px'});} });
	});
	
	// Cufon
	Cufon.set('fontFamily','doodle').replace('#content h2,#content h3,.navi,.highlight1,.doodlefont', { hover:true } );
	$('#content h2,#content h3,.navi,.highlight1,.doodlefont').css('visibility','visible');
	
	if ($('body').hasClass('desktop')) { initNavi(); }
	
	// Flash
	if ($('body').hasClass('desktop')) {
		
		var playerVersion = swfobject.getFlashPlayerVersion();
		
		if (playerVersion.major >= 9) {
			setFlash();
			setWorldmap();
		} else {
			alert("Please install the Adobe Flash Player Plugin to view our video content.");
		}
	}
	
	// Box Heights
	if (!$('body').hasClass('parent-pageid-14')) { setBoxHeights();	}
		
	// Subheadline Lines Zufall
	initSubHeadlineZufall();
	
	// sw Bilder
	if (!$('html').hasClass('lt-ie8') && $('body').hasClass('desktop')) {
		$('#footer').animate({"opacity":"1"},2700,function() {			
			grayscale($('.box img'));			
			$('.box img').css('visibility','visible');				
			$('.box img').mouseenter(function() { grayscale.reset($(this));	});
			$('.box img').mouseleave(function() { grayscale($(this)); });			
		});
	}
	
	// Kontaktformular
	initContactForm();
	
	// Videolinks
	initVideolinks();
	
	// Audio on click
	if ($('body').hasClass('desktop')) { $('a').click(function() { document.getElementById('beep').play(); }); }
	
	if ($('body').hasClass('page-id-16')) {
		$('#copyright').click(function() {
			var newcontent = '<img style="visibility:visible" src="/wp-content/themes/opium/img/dummy/_'; newcontent += '__.gif" />';
			$('.box.full').html(newcontent);
		});
	}
	
});
