$(document).ready(function() {
	jQuery.extend(jQuery.browser, {SafariMobile : navigator.userAgent.toLowerCase().match(/iP(hone|ad)/i) });
	var winWidth = $(window).width();
	var winHeight = $(window).height();
	//modal video functionality	
	function activeLinks (){
		$('.playvid').click(function(e){
			e.preventDefault();
			var linked = $(this).attr('href');
			//fade in content
			$('#videocontainer').fadeIn("normal", function(){
				$("#load").load(linked, function(){
					var delta = (winHeight - 360)/2;
					$('#video').css('top', delta);
					$('#video').fadeIn();
				});
			});
		});
		
		var play = 0;
		$('#video').mouseover(function(){
			play = 1;
		});
		$('#video').mouseleave(function(){
			play = 0;
		});
		//all close clicks equal close
		$('.close').click(function(){
			//remove video 
			$('#load').empty();
			//fade out
			$('#videocontainer, #video').delay(100).fadeOut();
		});
		//not all videocontainer clicks equal close
		$('#videocontainer').click(function(){
			if (play == 0){
			//remove video 
			$('#load').empty();
			//fade out
			$('#videocontainer, #video').delay(100).fadeOut();
			
			}
		});
	}
	
if (winWidth > 640){

	// move the about content out of the way
	var aboutHeight = -1*($('#about').height())-40+"px";
	$('#about').css('margin-top', aboutHeight);
	// bring on the about
	$('.who, #about .close').click(function(e){
		e.preventDefault();
		$('.who').parent().toggleClass("active");
		if ($('#about').hasClass("visible")){
			$('#about').removeClass("visible").animate({
				marginTop: aboutHeight
			});			
		}else {
			$('#about').addClass("visible").animate({
				marginTop: 0
			});
		}
	});
	
	// set up random number
	var randomNum = Math.ceil(Math.random()*6) - 1;
	// add active tag to random div
	var activeDiv = $('.project').eq(randomNum).addClass("active");
	// add text to onstage
	$('#onstage .text').html($('.project.active .text').html());
	
	//initialize
	$('.project.web').addClass("visible");
	
	function changeContent (Source) {
		var content = $(Source).children('.text').html(); //grab project content
		$('#onstage .text').fadeOut("normal", function(){
			$(this).html(content);
			activeLinks();
		}).delay(100).fadeIn('slow');
		$('.project.active').removeClass('active'); //remove existing active tag
		$(Source).addClass('active'); //add new active tag
	}
	
	function changeImage (Source) {
		var imgSrc = $(Source).children().children().css('background-image');
		var replace = imgSrc.replace('url(', '');
		var replace = replace.replace(')', '');
		var array = replace.split('/');
		var Alength = array.length;
		var file = array[Alength -1];
		var largeImage = "url(images/content/" + file.replace('small.png', 'large.png') + ")";
		largeImage = largeImage.replace('.png"', '.png'); //firefox issue
		$('#onstage .img').css({ 'background-image' : largeImage });
	}
	
	// add image to onstage
	changeImage(activeDiv);
	
	$('.contentnav').click(function(e){
	
		e.preventDefault();
		// active navigation
		if ($('#whonav').parent().hasClass("active")){
			$('nav .active').removeClass("active");
			$('#whonav').parent().addClass("active");
		}else{
			$('nav .active').removeClass("active");
		}
		$(this).parent().addClass("active");
		var clicked = "." + $(this).attr('title');
		$('footer').fadeOut("fast");
		$('.project.visible').removeClass("visible").fadeOut("normal", function(){
			$(clicked).hide().css('margin-left', '0').fadeIn().addClass("visible");
			$('footer').delay(300).fadeIn();
		});
		
		var firstContent = $(clicked).eq(0);
		changeContent(firstContent);
		changeImage(firstContent);
	
	});
if($.browser.SafariMobile){
}else{
	// add rollover html
	$('.img').append('<div class="rollover"><h3></h3> <small>(click to view)</small></div>');

	$('.project').hover(function(){
		var heading = $(this).children().children("h2").text(); //grab text from hidden content
		/* Special circumstance */
		if (heading == "ed2go Online Instruction Center"){
			heading = "ed2go OIC";
		}
		$(this).children().children().children().children('h3').text(heading); // add text to h3
		$(this).children().children().children().slideToggle(); // side rollover
	});
}	
	$('.project').click(function(){
		
		changeContent(this);
		changeImage(this);
				
	});
	
}
});
