
$(document).ready(function() {
    setupOurclients();
    setupHeaderSwitcher();
	setupLinks();
	setupHeader();
	setupMovie();
	setupEmoCompass();         
	setupPeopleVisitenkarten();
	setupMarquee();
	
	if ($.browser.msie && $.browser.version == 6) {
        $(document).pngFix();
        
        $('.menu-item-type-custom').mouseenter(function() {
            $(this).css({
                background: '#015092',
                color: '#F5DEB3'
            });
            $(this).children('ul').attr('style', 'display: block !important;');
            $(this).find('a:first').css('color', '#F5DEB3');
        }).mouseleave(function(elem) {
            $(this).css( {
                background: 'none',
                color: '#015092'
            });
            $(this).children('.sub-menu:first').attr('style', 'display: none;');
            $(this).find('a:first').css('color', '#015092');
        });
        
        $('.sub-menu *').mouseenter(function() {
            $(this).css('color', '#F5DEB3');
        }).mouseleave(function(elem) {
            $(this).css('color', 'white');
        });
        
        var ie6_fixedMovie = function() {
            $('#movie.zoomed').css('top', $(this).scrollTop() + "px");
        }  
        $(window).scroll(ie6_fixedMovie).resize(ie6_fixedMovie);
        
        $('#youtubeBlock').css({
            right: '5px',
            bottom: '5px'
        }); 
	}
});


function switchHeaders() {
    var firstHeader = $("#header > .first");
    var secondHeader = $("#header > .second");
    var newHeader;
    var oldHeader;
    if (firstHeader.is(":visible")) {
        newHeader = secondHeader;
        oldHeader = firstHeader;
    } else {
        newHeader = firstHeader;
        oldHeader = secondHeader;
    }
    oldHeader.fadeOut(500, function() {
        newHeader.fadeIn(500);
    }); 
    // switch again in three seconds
    setTimeout(switchHeaders, 5000);
}

function setupOurclients() {
    $('.slideshow').cycle({
        fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
        random: 1,
        timeout: 4000
    });
}



function setupHeaderSwitcher() {
    $('#header').cycle({
        fx:'fade',
        timeout: 8000
    });
}



function setupLinks() {
	$("#logo").click(function(event) {     
	 	window.location.href = $(event.target).attr('href');
	});      
	$("a[href='http://www.globalemotionsforum.com/']").attr('target', '_blank');
	$(".autolink").click(function(event) {         
		window.location.href = $(this).attr("href");
	   	event.preventDefault();
	})
	.css("cursor", "pointer");
}  

function setupHeader() {
	var currentMenuItem = $(".current-menu-item:not(.current-menu-parent)");    
	if (currentMenuItem.length == 0) {
		return;
	}
	var parentMenuItemOffset = currentMenuItem.parents(".current-menu-parent").offset();
	var breadcrump = $("#breadcrump");
	breadcrump.text(currentMenuItem.text());
	parentMenuItemOffset.top = parentMenuItemOffset.top + 40;
	parentMenuItemOffset.left = parentMenuItemOffset.left + 10;
	breadcrump.offset(parentMenuItemOffset); 
}          

function updateMoviePosition(movie) { 
	if (!movie || movie.length == 0) {
		return;
	}
	
	if (!($.browser.msie && $.browser.version == 6)) {
    	var leftPos = $(window).width() / 2 - movie.width() / 2;
    	movie.offset({left: leftPos})          
    	// first time it doesn't work
    	if (movie.offset().left != leftPos) {
    		movie.offset({left: leftPos})
    	}
	}
}       

function setupMovie() {
	
	$("#moviecontrol").hide();
	
	$("#togglezoommovie").click(function(event) {
		event.preventDefault();
		var link = $(this);                  
		var movie = $("#movie");
		if (moviePlayer) {
			if (movie.hasClass("zoomed")) {        
				movie.removeClass("zoomed");
				link.removeClass("on");
				moviePlayer.setSize(364, 200);
				$('#youtubeBlock').css({
                    width: 388,
                    height: 223
                });
			} else { 
				movie.addClass("zoomed");
				link.addClass("on");
				moviePlayer.setSize(640, 480);
				updateMoviePosition(movie);

			    $('#youtubeBlock').css({
			        width: 640,
			        height: 355
			    });
				
				
				if ($.browser == 'msie' && $.browser.version == 6) {
				    $('#movie.zoomed').css('top', $(this).scrollTop() + "px");
				}
			}
		}
	});
	
	$("#tooglemutemovie").click(function(event) {
		event.preventDefault();
		var link = $(this);
		if (moviePlayer) {    
			if (moviePlayer.isMuted()) {
				moviePlayer.unMute();
				link.addClass("on");
			} else {
				moviePlayer.mute();
				link.removeClass("on");
			}
		}
	}).addClass("on");
	
	$(window).resize(function(event) {
		updateMoviePosition($("#movie.zoomed"));
	});
	
	loadMovie();   
	
}

function loadMovie() {
	var params = { allowScriptAccess: "always", wmode: "transparent" };
	var atts = { id: "ytPlayer" };
	// All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
	swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=player1", 
	                   "movieholder", "356", "200", "8", null, {'wmode': 'transparent'}, params, atts);        
}                  

function onYouTubePlayerReady(playerId) {
  	moviePlayer = document.getElementById("ytPlayer");
  	// This causes the updatePlayerInfo function to be called every 250ms to
  	// get fresh data from the player 
	var movieYoutubeID = $('#movie').attr('youtube_id');
  	moviePlayer.loadVideoById(movieYoutubeID);
	$("#moviecontrol").show();      
	moviePlayer.addEventListener("onStateChange", "onYouTubePlayerStateChange");
	moviePlayer.playVideo();   
}        

function onYouTubePlayerStateChange(newState) {
	if (newState == 0) { // player ended 
		moviePlayer.seekTo(0, true); // rewind to start
		moviePlayer.playVideo();   
	}
}

currentPopup = null;

function setupEmoCompass() {
	$(".emopopup").hide(); 
	$(".emopopup").click(function(event) {
		event.preventDefault();
		$(this).fadeOut(function(event) {
			var thePopup = $(this);   
			thePopup.attr("style", "display:none;");
		});
		window.location.href = $(this).find("a").attr("href");
	}); 
	$(".emostep").mouseenter(function(event) {
		var step = $(this);
		
		// step.addClass("highlight");
		var theId = step.attr("id");      
		var popupId = theId + "popup"
		var popup = $("#" + popupId);
		if (currentPopup) {                
			if (popupId != currentPopup.attr("id")) {
			    
			    var afterFadeOut = function(event) {
                    var thePopup = $(this);   
                    thePopup.attr("style", "display:none;");
                }
			    if ($.browser.msie && $.browser.version <= 8) {
		            currentPopup.show(afterFadeOut);
		        } else {
		            currentPopup.fadeOut(afterFadeOut);
		        }
			}
		}
        currentPopup = popup;
        var left = 0;
		if ($.browser.msie && $.browser.version == 6) {
	        $(document).pngFix();
	        left = -408;
	        if (popupId == 'emostrategypopup') {
                left = -574;
            } else if (popupId == 'emodesignpopup') {
                left = -740;
            }
		} else if ($.browser.msie && ($.browser.version == 7 || $.browser.version == 8)) {
		    left = 0;
		    if (popupId == 'emostrategypopup') {
		        left = -166;
		    } else if (popupId == 'emodesignpopup') {
                left = -332;
            }
		} else {
		    left = 168;
		}
		currentPopup.css({ 
            top: 0,
            left: step.position().left + left
        });
		
		if ($.browser.msie && $.browser.version <= 8) {
		    currentPopup.show();
		} else {
		    currentPopup.fadeIn();
		}
	});            
	
	$("#emoholder").mouseleave(function(event) {   
		if (currentPopup) {
		    var afterFadeOut = function(event) {
                var thePopup = $(this);   
                thePopup.attr("style", "display:none;");
            }
            if ($.browser.msie && $.browser.version <= 8) {
                currentPopup.show(afterFadeOut);
            } else {
                currentPopup.fadeOut(afterFadeOut);
            }
		}      
		currentPopup = null;
	});
}       

function setupPeopleVisitenkarten() {
	var images = $(".visitenkarte_click");
	images.click(function(event) {
		var self = $(this);      
		var visitenkarte_id = self.attr("visitenkarte_id");
		var highslide = $("#" + visitenkarte_id + " a.highslide");
		highslide.click();
	}).css("cursor", "pointer");
}    

function setupMarquee() {
	$('marquee').marquee();
}



