/*
if (window['loadFirebugConsole']) {
    window.loadFirebugConsole();
} else if (!window['console']) {
    window.console = {};
    window.console.info =
    window.console.log =
    window.console.warn =
    window.console.error = function(){}
} */
	function initIntro(){
		var callback_once = false;
		$('body.intro #logo-hide div').animate({
			width:"1%"
		},2000,function(){
			if(!callback_once){
				callback_once = true;
			//animate words
			$('body.intro #logo-hide').css("visibility","hidden");
			//$('body.intro #logo-content').fadeOut();
			$.sound.play("/images/intro-assets/mainsong.mp3", {timeout:100000});
            setTimeout(function(){
			    $.sound.play("/images/intro-assets/mainsong.mp3", {timeout:100000});
            },15000);
			$('body.intro #logo-content').fadeOut(
                function(){
		    	$($('body.intro')[0]).animate({
	    			backgroundColor:"#495977"
    			},1000,function(){
			    	initMenu();
		    		$('li.menu-button .drop').hide();
	    			$('li.menu-button .drop li').hide();
    				$('body.intro #footer').fadeIn(500);
			    	$('#doyouhaveit-content').show();
		    		var all_doyouhaveits = $('#doyouhaveit img');
	    			var counter=0;	
    				$(all_doyouhaveits[counter]).fadeIn(500,function(){
					    counter++;
				    	if(counter < all_doyouhaveits.length){
			    		$(all_doyouhaveits[counter]).fadeIn(500,arguments.callee);
		    			}
	    				else{
    						$('body.intro #doyouhaveit-content').fadeOut(1000, function(){
					    		$('body.intro #youknow-content').show();
				    			$('body.intro #youknow-hide').animate({height:333},8000, "linear");
			    			});
		    			}
	    			});
    			});
                }
            );
			}
			return;
		});
	
	}
	function initPseudoPages(elementselector, check_active, no_animate){
		$('.button-list a').click(function(){
            if($('body.why-mofo').length != 0){
			    $.sound.play("/images/why-mofo/ding2.wav", {timeout:100000});
            }
			if(check_active){
				var parent = $(this).parent();
				//$('.number-table td').removeClass("active");
				$(elementselector).removeClass("active");
				parent.addClass("active");
			}
			//var contentSelector = $(this).attr("rel");
			var contentSelector = $(this).attr("href");
            if(contentSelector == '#' || contentSelector == '' || 
                typeof contentSelector == "undefined"){return false; }
			if($('#content-holder:visible').length == 0){
				$('.content-box').hide();
				$(contentSelector).show();
                if(no_animate){
    				$('#content-holder').show();

                }
                else{
    				$('#content-holder').fadeIn(350);
                }
			}
			else{
                if(no_animate){
			    	$('#content-holder').hide();
		    			$('.content-box').hide();
	    				$(contentSelector).show();
			    	$('#content-holder').show();
                    
                }
                else{
			    	$('#content-holder').fadeOut(350, function(){
		    			$('.content-box').hide();
	    				$(contentSelector).show();
    				}).fadeIn(350);
                }
			}
            return false;
		});
	}
	function colorLighten(color, amt){	
		var parts = new Array();
		if(color.match("rgb")){
			parts = color.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
			parts[1] = Math.round( Math.min(255, parseInt(parts[1])+ (255*amt)) );
			parts[2] = Math.round( Math.min(255, parseInt(parts[2])+ (255*amt)) );
			parts[3] = Math.round( Math.min(255, parseInt(parts[3])+ (255*amt)) );
			
			return("rgb("+parts[1]+", "+parts[2]+", "+parts[3]+")");
		}
		else{ //hex values
			color = color.substring(1);
			parts[0] = color.substr(0,2);
			parts[1] = color.substr(2,2);
			parts[2] = color.substr(4,2);
			parts[0] = Math.round( Math.min(255, parseInt(parts[0],16)+ (255*amt)) );
			parts[1] = Math.round( Math.min(255, parseInt(parts[1],16)+ (255*amt)) );
			parts[2] = Math.round( Math.min(255, parseInt(parts[2],16)+ (255*amt)) );
			parts[0] = ""+parts[0].toString(16);
			parts[1] = ""+parts[1].toString(16);
			parts[2] = ""+parts[2].toString(16);
			color = "#"+parts[0]+parts[1]+parts[2];
			
		}
		return color;
	}

    function menuMouseLeave(){

			$(this).find('.drop').removeClass("drop-active");
			var all_menu_el = $(this).find('ul.drop li');
			$(all_menu_el[0]).fadeOut(30,function(){
				$(this).next('li').fadeOut(30,arguments.callee);
				if(($(this).next('li')).length == 0){
					$(this).parent().hide();
                    menu_visible = false;
                    //$('li.menu-button').unbind("mouseleave");
				}
			});
    }
	function initMenu(){	
		var background_color = $('body').css('backgroundColor');
		var background_lighter_color = background_color;
		background_color = colorLighten(background_color, 0.08);
		background_lighter_color = colorLighten(background_lighter_color, 0.16);

		$('.nav-box li:not(.active) a:not(.top-link)').css("background-color",background_color);
		$('.nav-box li:not(.active) a.top-link').css("background-color",background_lighter_color);
        var menu_visible = false;
		$('li.menu-button').mouseenter(function(event){
            if(menu_visible){return;}
			$(this).find('.drop').addClass("drop-active");
			var all_menu_el = $(this).find('ul.drop li');

			$(this).find('.drop').show();
			$(all_menu_el[all_menu_el.length-1]).fadeIn(30,function(){
                
				if($(this).prev('li').length == 0){
                        menu_visible = true;
		                //$('li.menu-button').bind("mouseleave", menuMouseLeave);
                }
				$(this).prev('li').fadeIn(30,arguments.callee);
				//maybe check if this is the only one left, then add mouseleave
			});
		});
		
		$('li.menu-button').mouseleave(function(event){
            if(!menu_visible){return;}
			$(this).find('.drop').removeClass("drop-active");
			var all_menu_el = $(this).find('ul.drop li');
			$(all_menu_el[0]).fadeOut(30,function(){
				$(this).next('li').fadeOut(30,arguments.callee);
				if(($(this).next('li')).length == 0){
					$(this).parent().hide();
                    menu_visible = false;
				}
			});
		});
        
		$('.drop li a').mouseover(function(){
			$(this).css({color:"#fff"},100);
			$(this).css({backgroundColor:"#000"},100);
			
		});
		$('.drop li a:not(.top-link)').mouseleave(function(){
			$(this).css({backgroundColor:background_color},100);
			$(this).css({color:"#000"},100);
		});
		$('.drop li a.top-link').mouseleave(function(){
			$(this).css({backgroundColor:background_lighter_color},100);
			$(this).css({color:"#000"},100);
		});
		var all_menu_buttons = $('li.menu-button');
		var last_menu = $(all_menu_buttons[all_menu_buttons.length -1]);
		
	    //THESE TWO METHODS SHOW MENU
		//$(all_menu_buttons).find('.drop').show();
		//$(all_menu_buttons).find('.drop li').show();
        var mouseX;
        var mouseY;
        $(all_menu_buttons).each(function(i, e){
            $(e).find('.drop').show();
            $(e).find('.drop li').show();
            $(e).find('.drop').css('visibility','hidden');
            
            var drop_position = $(e).find('.drop').offset();
            var drop_width = $(e).find('.drop').width();
            var drop_height = $(e).find('.drop').height();
            var drop_x_1 = drop_position.left;
            var drop_y_1 = drop_position.top;
            var drop_x_2 = drop_position.left+drop_width;
            var drop_y_2 = drop_position.top+drop_height;
            var sibling = $(e).siblings(".active");
            $(document).bind('mouseover.initialMenu',function(me){
                mouseX = me.pageX;
                mouseY = me.pageY;
                if(!( mouseX<drop_x_1 || 
                    mouseX >drop_x_1 && mouseX < drop_x_2 && mouseY > drop_y_1 && mouseY < drop_y_2
                ) ){
                    /*
                    NOTE: WEBKIT and IE actually triggers mousemove 
                    before user physically moves mouse
                    This initializes the location of the cursor
                    Of course, this only happens if the cursor is inside document's boundaries
                    */
                    if(! ($(sibling).hasClass('active-1') && $(sibling).hasClass('active-hidden')) ){
                        $(e).find('.drop').hide();
                        $(e).find('.drop li').hide();
                    }

                }
                $(e).find('.drop').css('visibility','visible');
                $(document).unbind('mouseover.initialMenu');
            });

            setTimeout(function(){
                $(e).find('.drop').css('visibility','visible');
            },100);
            
        });
		
	} 
    
    function initCaseInPoint(){
        $('.case-list-box').each(function(i, e){
            var yellow_box = $(e).find(".case-list-info");
            var link = $(e).find('a').attr("href");
            $(e).mouseover(function(){
                yellow_box.css("visibility","visible");
            });
            $(e).mouseleave(function(){
                yellow_box.css("visibility","hidden");
            });
            yellow_box.click(function(){
               window.location = link; 
            });
        });
    }
function initImageCarousel(){
    var all_images = $('div.image-carousel img');
    if(all_images.length <= 1){return;}


    var counter = 0;
    var carousel_length = all_images.length;
    var carousel_delay = 5000;
    $(all_images[counter]).delay(carousel_delay).fadeOut(500, function(){
        counter++;
        if(counter == carousel_length){counter=0;}
        $(all_images[counter]).fadeIn(500).delay(carousel_delay).fadeOut(500, arguments.callee);
    });
}
function initLateralVoices(){
        if($('body.voices').length == 0){return;}
        var lateralvoices_firstclick = false;
        initPseudoPages('.button-list li', true);
        $('.button-list a').click(function(){
            if(!lateralvoices_firstclick){
                $('.img-list-block').animate({
                    marginLeft:0
                },200,'swing',function(){
                    $('.intro-content').fadeOut(200);
                });
                lateralvoices_firstclick = true;

            }
                for(var i =1 ; i < 7; i++){
                    if($("#mofo_video_"+i).length > 0){
                        jwplayer("mofo_video_"+i).stop();
                    }
                }
                /*retrieve id  of the video in the shown panel*/
                var autoplayvid = $('.button-list .active a').attr('href');
                autoplayvid = $(autoplayvid).find('.video-block *');
                autoplayvid = $(autoplayvid[0]).attr('id');
                jwplayer(autoplayvid).play(); //autoplay that video
            
            return false; 
        });
}
function initProbono(){
        if($('body.probono').length == 0){return;}
        $('.awards-block a').click(function(){
            $(".main-block").removeClass("hide");
            
            $('.middle-block .jScrollPaneContainer').animate({
                marginLeft:-26
            }, 200);
            return false;
        });
        $('.close-block a').click(function(){
            $(".main-block").addClass("hide");
            $('.middle-block .jScrollPaneContainer').animate({
                marginLeft:340
            }, 200);
            return false;
        });
         $('.img-list li').each(function(i, e){
            var yellow_box = $(e).find(".probono-info");
            var link = $(e).find('a').attr("href");
            $(e).mouseover(function(){
                //yellow_box.css("display","inline");
                yellow_box.css("visibility","visible");
            });
            $(e).mouseleave(function(){
                yellow_box.css("visibility","hidden");
            });
            
            yellow_box.click(function(){
               window.location = link; 
            });
        });   
    }
function initFlashClick(delaytime,callback){
    $('.fullflash-shim').click(function(){
        $('.fullflash-block').delay(delaytime).fadeOut(function(){
            
            $('.fullflash-shim').hide();
            callback();
            });

    });
}
function initOffices(){
    if($('body.offices').length == 0){return;}
    $('.main-block').css('filter', 'alpha(opacity=100)'); //TODO
    $('.jScrollPaneContainer, .scrollify').css('filter', 'alpha(opacity=100)');
        //the problem with ie is that it needs to have this filter set
        //the other problem is that it doesn't like changing opacity for floated objects. The
        //background for floated objects is the body color, not it's parent
    $('.button-list li a').css('filter', 'alpha(opacity=100)');
    $('.button-list li a').css('opacity',0);
        //Anything setting filter and opacity css is an ie hack because ie seomtimes doesn't hide the elements in time. 
    $('.main-block').css('opacity',0);
    initFlashClick(200,function(){
        $('.main-block').animate({
            opacity:1
        });
        $('.intro-box').show(); 
        $('.intro-box').css("opacity","1");//reverting ie hack
        $('.button-list li a').animate({
            opacity:1
        });
        $('#wrapper').css('z-index','1');
        $('#footer').css('z-index','2');
         $(".offices .content-box").css("position","relative"); //IE hack to remove absolute positioning on divs

    });
}
function initLGBT(){
    if($('body.lgbt').length == 0){return;}
    
    $('.center-content').css("visibility","hidden");
    setTimeout(function(){
        $('.center-content').css("visibility","visible");
    },2000);
    setTimeout(function(){ //need to setTimeout for ie
        $('#wrapper').css('z-index','1');
        $('#footer').css('z-index','2');
        $('.fullflash-block').fadeOut();
    },6000);
    
    initFlashClick(1500,function(){
        $('#wrapper').css('z-index','1');
        $('#footer').css('z-index','2');
    });
    
}
function flashCallbackScroll(fe){
    if($('.main-block').height() > 0){
        $('.main-block').jScrollPane({
            showArrows : true,
            scrollbarWidth : 6,
            scrollbarMargin : 20
        });
    }
   
}
function flashCallbackHeader(fe){
    if(!fe.success){
        $('.heading').addClass('reset-header');
        $('#main').addClass('reset-main');
    }

}

var flash_success = false;
function flashCallback(fe){
    flash_success = fe.success;
    //if($(".fullflash-shim").hasClass("shim-click")){
    if($("body.offices").length > 0 || $("body.lgbt").length>0){
        //alert("hasclass");
        if(!fe.success){
            //$(".fullflash-shim").trigger("click");
            if($("body.offices").length > 0){
                $(".fullflash-shim").hide();
        
                $('.main-block').animate({
                    opacity:1
                });
                $('.intro-box').show();
                $('.button-list li a').animate({
                    opacity:1
                });
                $('#wrapper').css('z-index','1');
                $('#footer').css('z-index','2');
            }
            else{
                $('.center-content').css("visibility","visible");
                $('#wrapper').css('z-index','1');
                $('#footer').css('z-index','2');
                $('.fullflash-block').hide();
            }
        }
        return;
    }
    
    if($('.scrollify').length > 0){
        flashCallbackScroll(fe);
    }
    var alt_content = $('.alt-block');
     $('.alt-original').append(alt_content);
    if(!fe.success){
        //move/show alternative content
        //if moving, append it to center-content
        //also remove center-content-padding if exist
        $('.alt-insert').append(alt_content);
        $('.alt-remove').remove();
        $('#wrapper').css('z-index','1');
        $('#footer').css('z-index','2');
        $('.alt-block .main-block').css('opacity',1);
    }
    else{
        $('.alt-block').hide();
    }
    flashCallbackHeader(fe);
}
(function ($) {
	$( document ).ready( function () {
		initMenu();
        initImageCarousel();
        initAllVideos();
        initCaseInPoint();
        initLateralVoices();
        initProbono();
        initOffices();
        initLGBT();
        //$('body #wrapper').css('filter', 'alpha(opacity=100)');
        //$('body #wrapper').css('opacity','0');
        //$('body #wrapper').css('visibility','visible');
        
	});
    /*
    $(window).load(function(){
        $('body #wrapper').delay(200).animate({opacity:1});
        
    });*/
})(jQuery);

function initAllVideos() {
    if($('#mofo_video').length>0){initVideoPlayer('mofo_video');}
    for(var i=1; i<10; i++){
        if($('#mofo_video_'+i).length > 0){
            initVideoPlayer('mofo_video_'+i);
        }
    }
}

function initVideoPlayer( ref ) {
    var playerOptions = {
	flashplayer: '/images/flash/player.swf',
	skin: '/images/flash/five/five.xml',
    showicons: false,
	autostart: false//,
	//width: 304,
	//height: 166,
	//image: '/images/video/Vilcek_Commercial-home.png'
    };
    
    /* override player height for MSIE */
    var v = document.createElement( 'video' );
    if( v && !v.canPlayType ) { /* then this is MSIE 6/7/8 */
        if($('body.probono').length != 0){
            playerOptions.height = 220;
        }
	   else {
	       playerOptions.height = 189;
	   }
    }

    jwplayer(ref).setup(playerOptions);
    
} // initVideoPlayer


