// JavaScript Document
$(document).ready(
	function(){
		headerSpace();
		navHovers();
		features();
		caseStudies();
		videoReady();
	}
);


function checkScroll(){
	var hash = window.location.hash;
	window.location.hash = hash;
}
function headerSpace(){
	
	var subnavheight = $("#contentHeader").height();
	$("#headingSpace").css({'height':subnavheight});
	var top = -181;
	top -= subnavheight-8;
	
	$("a.jumpanchor").css({'top':top});
	
	
	jQuery.each(jQuery.browser, function(i) {
   if($.browser.msie){
      $("span.jumpBox").css("top",top+8);
	  checkScroll();
   }
 });
}

function navHovers(){
	var Fade = false;
	$("#navList li").mouseenter(
		function(){
			var id = $(this).attr('id');
			mouseEnter = id;
			$(this).addClass('hover');
		}
	).mouseleave(
		function(){
			mouseEnter = 0;
			var id = $(this).attr('id');
			$("#"+id+" ul").hide();
			$("#"+id+" a:not(.cur)").animate({'opacity':'0'}, 400, function(){
				if(mouseEnter != id){
					$("#"+id).removeClass('hover'); 
				}
				$("#"+id+" a").animate({'opacity':'1'}, 1);
				$("#"+id+" > ul").show();
			});
		}
	);
}



function features(){
	$("#helpButton").click(
		function(){
			help();
			return false;
		}
	);
	$("#emailButton").click(
		function(){
			email();
			return false;
		}
	);
	
}

function help(){
	var html = "<div id='helpBox'></div><div id='overlay'></div>";
	$('body').append(html);
	$("#helpBox").css({'position':'absolute','left':'50%', 'top':'20%', 'background':'white', 'z-index':'20000', 'width':'600px', 'margin-left':'-300px'});
	$.post("features/help.php", function(data){ 
		$("#helpBox").html(data);
		$("#helpBox div.close").click(function(){
			closeHelp();
		});
		$("#overlay").click(function(){
			closeHelp();
		
		});
	});
	
}
function email(){
	var html = "<div id='emailBox'></div><div id='overlay'></div>";
	$('body').append(html);
	$("#emailBox").css({'position':'absolute','left':'50%', 'top':'20%', 'background':'white', 'z-index':'20000', 'width':'600px', 'margin-left':'-300px'});
	var href = $("#emailButton").attr('href');
	$.post(href, function(data){ 
		$("#emailBox").html(data);
		emailLoaded(href);
	});
	
}

function closeHelp(){
	$("#helpBox").fadeOut('fast', function(){ $("#helpBox").remove(); });
	$("#overlay").fadeOut('fast', function(){ $("#overlay").remove(); });
}

function closeEmail(){
	$("#emailBox").fadeOut('fast', function(){ $("#emailBox").remove(); });
	$("#overlay").fadeOut('fast', function(){ $("#overlay").remove(); });
}

function emailLoaded(href){
	var height = $("#emailBox").height()/-5;
	$("#emailBox").css({"margin-top":height});
	$("#emailBox div.close").click(function(){
		closeEmail();
	});
	$("#overlay").click(function(){
		closeEmail();
	
	});
	
	$(".email-button").click(function(e){
		var to = $("#to").val();
		var from = $("#from-name").val();
		var from_email = $("#from-email").val();
		var subject = $("#subject").val();
		var message = $("#message").val();
		errors = new Array();
		whitespace = /(\n)*(\s)*/;
		if(message=="" | message.replace(whitespace, "") == ""){
			errors.push("#message");
		}
		if(to == '' | to.replace(whitespace, "") == ""){ errors.push("#to"); }
		if(errors.length > 0){
		for(var x in errors){
			$(errors[x]).css({"border":"solid 1px red", "background":"#ffdddd"});
			}
			alert("Please fill in required fields");
			return false;
		}
		$.post(href, {"to":to, "from-name":from, "from-email":from_email, "subject":subject, "message":message, "Submit":"Send"}, function(data){ $("#emailBox").html(data); 
		emailLoaded(href); });
		return false;
	});
}


function caseStudies(){
	
	var n = $("#caseStudiesContent > div").length;
	
	$("#caseStudiesContent > div:first").css({'display':'block'});
	var current = $("#caseStudiesContent > div:first").attr('id');
	$("#caseStudiesContent").children().each(function(){
		html = this.nodeName;
		id = $(this).attr('id');
		if(id != 'caseStudy1'){
		$(".editButton"+id+"content").hide();
		}
		if(n>1){
			if(html=="DIV"){
				id = $(this).attr('id');
				title = $("#"+id+" h2").html();
				$("#caseSelect").append("<option value='"+id+"'>"+title+"</option>");
			}
		} else { $("#caseSelect").remove(); }
	});
	$("#caseSelect").change(function(){ selectCase(current); });
	
		
}

function selectCase(current){
	
	$("#caseStudiesContent > div").css({'display':'none'});
	var sc = $("#caseSelect").val();
	$("#caseStudiesContent #"+sc).css({'display':'block'});
		var left = $(".editButton"+current+"content").css('left');
		var top = $(".editButton"+current+"content").css('top');
		$(".editButton"+current+"content").hide();
		$(".editButton"+sc+"content").show().css({'top':top, 'left':left});
	
	
}
	
function videoReady(){
	$("body").append("<div id='videoOverlay'></div><div id='video_pop'></div>");
	$("#video_pop").html(
"<div id='videoBar'><strong>Videos@CCA</strong><span id='videoClose'>Close X</span></div><div id='videoPad'><div id='video_player'><a href='http://www.adobe.com/go/getflashplayer'><img src='http://wwwimages.adobe.com/www.adobe.com/shockwave/download/images/flashplayer_100x100.jpg' /><br> This content requires Adobe Flash<br> Click here to get Adobe Flash</a></div></div>");
	$("#videoOverlay").css({
		'background':'#000',
		'position':'absolute',
		'left':'0',
		'top':'0',
		'width':'100%',
		'height':'100%',
		'z-index':'10000',
		'display':'none',
		'opacity':'0.6',
		'filter':'alpha(opacity=60)'
						   }).click(function(){ closeVideo(); });
	$("#video_pop").css({
		'background':'#fff',
		'position':'absolute',
		'left':'50%',
		'top':'20%',
		'margin-left':'-200px',
		'margin-top':'-100px',
		'width':'420px',
		'height':'auto',
		'z-index':'10001',
		'overflow':'hidden',
		'display':'none'
						   });
	
	$('#videoBar').css({
		'height':'auto',
		'overflow':'auto',
		'width':'100%',
		'background':'#40bad0',
		'color':'white',
		'padding':'5px 10px',
		'font-size':'15px'
					   });
	$("#videoClose").css({
		'display':'block',
		'background':'#666',
		'border':'solid 1px white',
		'position':'absolute',
		'right':'5px',
		'top':'5px',
		'font-size':'10px',
		'color':'#fff',
		'padding':'1px 3px',
		'cursor':'pointer'
						 }).click(function(){ closeVideo(); });
	$("#videoPad").css("padding", "10px");
	$("#video_player").css({ 'padding':'50px'});
	
}

function closeVideo(){
	$("#video_pop").hide();
	$("#videoOverlay").hide();
}
var initVideo = 0;
function showVideo(num){
		var params = {};
		params.allowfullscreen=true;
		params.allowscriptaccess=true;
		var flashvars = {};
		flashvars.file="playlist.asx";
		flashvars.playlist="bottom";
		flashvars.item=num;
		swfobject.embedSWF("player.swf", "video_player", "399", "424", "8.0.0",'js/expressInstall.swf',flashvars,params);
		
		width = $("#video_pop").width();
		left = width/2;
		
		
		$("#videoOverlay").fadeIn('fast', 
			function(){ 
				$("#video_pop").show();
				height = $("#video_pop").height();
				if(initVideo == 0){
					height -= '60';
					initVideo++;
				}
				$("#video_pop").css({
				'display': 'block',
				'margin-left':-left+'px',
				'margin-top':-height/5+'px',
				'height':height+'px',
				'overflow':'hidden'
			}); 
		});
														 

}
