
var galleryLoop = true;
var colors;
var isIE = (navigator.appName == "Microsoft Internet Explorer");
var hasFocus = true;
var active_element;


// Browser focus




// isChildOf

(function($) {
    $.fn.extend({
        isChildOf: function( filter_string ) {
          
          var parents = $(this).parents().get();
         
          for ( j = 0; j < parents.length; j++ ) {
           if ( $(parents[j]).is(filter_string) ) {
      return true;
           }
          }
          
          return false;
        }
    });
})(jQuery);

function decorateGalleryBoxes(){
	$.each($("#gallery .box"), function(index, item){
		switch (index){
			case 0:
				$(item).css("background-color", "#ed1c24");
			break;
			case 1:
				$(item).css("background-color", "#00b0d8");
			break;
			case 2:
				$(item).css("background-color", "#faa627");
			break;
			case 3:
				$(item).css("background-color", "#8a73b4");
			break;
		}
	});
}






function decorateSidebarHeadings(){
	$(".widget h3").prepend('<span class="bullet"></span>');
	$(".menu li").prepend('<span class="bullet"></span>');
}

function decorateSearchbar(){
	$("#s").val("Type here to search");
}

function handleSearchbarFocusIn(event){
	if ($(event.target).val() == "Type here to search"){
		$(event.target).val("");
	}
}

function handleSearchbarFocusOut(event){
	if ($(event.target).val() == ""){
		$(event.target).val("Type here to search");
	}
}

function startGallery(){
	colors = new Array();	
	$.each($("#gallery .box"), function(index, item){
		colors.push($(item).css("background-color"));
	})	
	$("#gallery .box:nth-child(1)").css("background-color", "black");
	var iterations = 1;
	var slideCount = 4;
	var func = setInterval(function(){
	
	if ((galleryLoop == true) && (hasFocus == true)){
	
		if (iterations < slideCount){		
			$('#slide-hole').animate({
    			marginLeft: '-=655'
   		}, 700);  		
   		iterations++;
   		$("#gallery .box:nth-child(" + iterations + ")").css("background-color", "black");
   		$("#gallery .box:nth-child(" + (iterations - 1) + ")").css("background-color", colors[(iterations - 2)]);
   	} else {  	
   		$('#slide-hole').animate({
    		marginLeft: '0'
   		}, 700);   		
   		iterations = 1;
   		decorateGalleryBoxes();
   		$("#gallery .box:nth-child(1)").css("background-color", "black");
   	}
   	//console.log(hasFocus);
   }
   
	}, 4000);
	
}

function addGalleryEvents(){
/*	$(".vignette").live({
	mouseenter: function(event){
		
		},
	mousemove: function(event){
		
		},
	mouseout: function(event){
	
		}
	});*/
	$("#viewer").bind("mouseenter", function(event){
		/*$(".vignette").fadeOut();*/
		$('#viewer h2').css("color", "#04c8f4");
		
		//console.log($(event.target).attr("src"))
	})
	$("#viewer").bind("mouseleave", function(event){
		/*$(".vignette").fadeIn();*/
		$('#viewer h2').css("color", "white");
		
		//console.log("I'M LEAVING YOU.")
	})
	
	$(".box").click(function(event){
		var index = $(event.target).index();
		//console.log(index);
		galleryLoop = false;
		$.each($("#gallery .box"), function(colorIndex, item){
			$(item).css("background-color", colors[colorIndex]);
		})
		//$("#gallery .box:nth-child(" + (index) + ")").css("background-color", colors[(index - 1)]);
		$(event.target).css("background-color", "black");
   	
		$('#slide-hole').animate({
    			marginLeft: -index * 655
   		}, 700); 
	})
	
}

function colorBullets(type){
	$.each($(type + " .bullet"), function(index, item){
		var headingTitle = $(item).next().html();
		
		switch (headingTitle){
		
			case "Who We Are":
				$(item).addClass("purple");
			break;
			
			case "Management":
				$(item).addClass("red");
			break;
			
			case "Nerve Center":
				$(item).addClass("yellow");
			break;
			
			case "VMX":
				$(item).addClass("blue");
			break;
			
			case "Our Approach":
				$(item).addClass("purple");
			break;
			
			case "Where We Are":
				$(item).addClass("yellow");
			break;
			
			case "Careers":
				$(item).addClass("red");
			break;
			
			case "Client Work":
				$(item).addClass("purple");
			break;
			
			case "Reflecteur":
				$(item).addClass("yellow");
			break;
			
			case "Research":
				$(item).addClass("red");
			break;
			
			case "Leadership":
				$(item).addClass("blue");
			break;
			
			case "Contact":
				$(item).addClass("red");
			break;
			
			case "The Pool":
				$(item).addClass("purple");
			break;
			
			case "AOD":
				$(item).addClass("red");
			break;
			
			case "Philanthropy":
				$(item).addClass("purple");
			break;
		}
	})
}

function tidyUp(){
	decorateSidebarHeadings();
	decorateSearchbar();
	addGalleryEvents();
	colorBullets(".page .post");
	colorBullets(".menu");
	
	
}

function setUpManagement(){
	$("body").append('<div id="overlay"><div id="managementViewer"><div class="margin"><div class="left-column"><div class="headshot"></div><div class="main-info"><p class="viewer-name">Viewer Name</p><p class="viewer-title">Viewer Title</p><p class="viewer-company">Viewer Company</p></div></div><div class="viewer-bio"></div></div></div></div>');
	setTimeout(function(){
		//$("#overlay").slideDown(500);
	}, 300)
	
	$("#overlay").click(function(event){
		if ($(event.target).isChildOf("#managementViewer")){
				//console.log("Should not close");
				
		} else if ($(event.target).attr("id") == "managementViewer"){
			//console.log("Should not close");
			
					//console.log("Still should not close");
			
		} else {
			//console.log("Should close");
			$("#overlay").slideUp(500);
		}
	})
	
	$(".director").live("click", function(event){
		$("#overlay").slideDown(500);
		
		var headshot = $(this).find("img").attr("src");
		var name = $(this).find(".name").html();
		var title = $(this).find(".position").html();
		var company = $(this).find(".company").html();
		var bio = $(this).find(".bio").html();
		//console.log(headshot);
		$("#managementViewer .headshot").css("background-image", "url(" + headshot + ")");
		$("#managementViewer .viewer-name").html(name);
		$("#managementViewer .viewer-title").html(title);
		$("#managementViewer .viewer-company").html(company);
		$("#managementViewer .viewer-bio").html(bio);
	})

}

function init(event){
	//console.log("Loaded...");
	decorateGalleryBoxes();
	setTimeout(startGallery, 500);
	tidyUp();
	
	$("#s").focus(handleSearchbarFocusIn);
	$("#s").focusout(handleSearchbarFocusOut);
	$("#searchsubmit").val("");
	
	setUpManagement();
	
	$(window).focus(function() {
    hasFocus = true;
    //console.log("Focused");
});

window.addEventListener('blur', function() {
    hasFocus = false;
    //console.log("Blurred");
},false);
	
	//window.focus();
	
}


$(document).ready(init);
