var contentEdit;

function toggleContentEditor(idArray){
	if(!contentEdit){
		$("#contentSpacerContainer").animate({height:"25px"},500);
		contentEdit = new nicEditor();
	    contentEdit.setPanel('contentSpacer');
	
	for(var id in idArray)//add all editable instances
	    contentEdit.addInstance(idArray[id]);
		
	}else{
		$("#contentSpacer").html("");
		//contentEdit.saveContent();
		//remove all editable instances
		for(var id in idArray)
		contentEdit.removeInstance(idArray[id]);
		
		contentEdit = null;
		$("#contentSpacerContainer").animate({height:"5px"},500);
	}
}

var shade = false;

function shadeCallback(){
	if($("#shade").css("opacity") == "0"){
		shade = false;
		$("#shade").css("visibility", "hidden");
	}
}

function toggleShade(){
	if(!shade){
		$("#shade").css("top", topScroll + "px");
		$("#shade").css("visibility", "visible");
		$("#shade").fadeTo("slow", 0.75);
		shade = true;
	}
	else{
		$("#shade").fadeTo("slow", 0.0,shadeCallback);
	}
}

var mailer = false;
var mailForm = "Please complete all the following forms.<br /><br />Buisness Name:<br /><input name=\"bName\" type=\"text\" class=\"input\"><br />Contact Name:<br /><input name=\"cName\" type=\"text\" class=\"input\"><br />Telephone Number:<br /><input name=\"tNumber\" type=\"text\" class=\"input\"><br />E-mail:<br /><input name=\"email\" type=\"text\" class=\"input\"><br />Message:<br /><textarea cols=\"50\" rows=\"18\"></textarea><br /><br /><button onClick=\"sendMail();\">Send</button><button onClick=\"toggleMailer();\">Cancel</button>";

function mailerCallback(){
	if($("#mailer").css("opacity") == "0"){
		mailer = false;
		$("#mailerContainer").css("visibility", "hidden");
	}else if($("#mailer").css("opacity") == "1"){
		$("#mailer").html(mailForm);
	}
}

function toggleMailer(){
	if(!mailer){
		toggleShade();
		$("#mailerContainer").css("top", (topScroll - 3) + "px");
		$("#mailerContainer").css("visibility", "visible");
		$("#mailer").animate({height:"550px", width:"450px", opacity:"1"},1000,"",mailerCallback);
		mailer = true;
	}
	else{
		$("#mailer").html("");
		$("#mailer").animate({height:"0px", width:"0px", opacity:"0.0"},1000,"",mailerCallback);
		
		toggleShade();
	}
}

function sendMail(){
	toggleMailer();
}

var gallery = false;
var galleryContent;

function galleryCallback(){
	if($("#gallery").css("opacity") == "0"){
		gallery = false;
		$("#galleryContainer").css("visibility", "hidden");
	}else if($("#gallery").css("opacity") == "1"){
		$("#gallery").html(galleryContent);
	}
}

function toggleGallery(content){
	if(!gallery){
		galleryContent = content;
		toggleShade();
		$("#galleryContainer").css("top", (topScroll - 3) + "px");
		$("#galleryContainer").css("visibility", "visible");
		$("#gallery").animate({ width:"90%",opacity:"1"},1000,"",galleryCallback);
		gallery = true;
	}
	else{
		$("#gallery").html("");
		$("#gallery").animate({ width:"0px",opacity:"0"},1000,"",galleryCallback);
		toggleShade();
	}
}

var lastNav;
var loadingScreen = "";
var homeSwf = "<object type=\"application/x-shockwave-flash\" data=\"pages/home/title.swf\" width=\"805\" height=\"380\"><param name=\"wmode\" value=\"opaque\" /><param name=\"movie\" value=\"pages/home/title.swf\" /></object>";

function loadPage(page){
	//$("body").css("overflow","hidden");
	
	if(page == "contact")
		$("#contentText").css("background","#E5F9FF");
	else
		$("#contentText").css("background","#fff");
	
	window.location.hash=page;
//	$("#contentText").html(loadingScreen);
//	$("#contentText").animate({opacity:"0"}, 500);
	$("#contentText").load("pages/"+page+"/data.html","hi",function(){
		$("#plan").css("background-image", "url(pages/"+lastNav+"/plan.jpg)");
	});
	if(page == "home" || page == "reuse" || page == "contact")
		$("#contentBar").load("pages/home/sideBar.html");
	else
	$("#contentBar").load("backend.php?folder=pages/"+page+"/thumbs/&build=thumbs","hi",function(){
		$(".sideThumbnailImg").click(function(){
			var imageName = $(this).css("background-image").replace(/thumb/,"full");
			toggleGallery("<img src=\""+imageName+"\" width=\"98%\" onclick=\"toggleGallery('');\"/>");
		});
		
	});
	$("#n"+lastNav).attr("class","navItem");
	$("#n"+page).attr("class","navItemS");
	lastNav = page;
	
	var newContentHeight;
	if(page == "home"){
		//if($("#titleContainer").height() == 356)
		//	newContentHeight = $("#contentContainer").height();
		//$("#contentContainer").animate({height:newContentHeight+"px"},1000,"",function(){$("body").css("overflow-y","scroll");});
		$("#titleContainer").animate({height:"380px"},1000,"",resizeContent);
		$("#navBarPushD").animate({height:"13%"},1000);
		$("#titleSWF").html(homeSwf);
		$("#titlePush").css("visibility","visible");
		$("#titlePush").animate({opacity:"1.0", height:"380px", top:"107px"},1000);
		$("#titleCover").animate({opacity:"0.0", height:"380px", top:"107px"},1000,"",function(){$("#titleCover").css("visibility","hidden");
		});
		
		
	}else{
		//if($("#titleContainer").height() == 380)
		//	newContentHeight = $("#contentContainer").height() + 148 ;
	//	$("#contentContainer").animate({height:newContentHeight+"px"},1000	,"",function(){$("body").css("overflow-y","scroll");});
		$("#titleContainer").animate({height:"356px"},1000,"",resizeContent);
		$("#navBarPushD").animate({height:"13%"},1000);
	//	$("#titleSWF").html("<img src=\"pages/"+page+"/title.jpg\" height=\"100%\" width=\"805\" alt=\""+page+"\"/>");
		$("#titleSWF").html("");
		$("#titleSWF").css("background-image", "url(pages/"+page+"/title.jpg)");
		$("#titlePush").animate({opacity:"0.0", height:"356px", top:"107px"},1000,"",function(){$("#titlePush").css("visibility","hidden");
		});
		$("#titleCover").css("visibility","visible");
		$("#titleCover").css("left",($("body").width()-980)/2);
		$("#titleCover").animate({opacity:"0.25", height:"356px", top:"107px"},1000);
	}
	
	if(page == "login"){
		
	}
	
	resizeContent();
}

function preLoadImages(){
	/*$(".navItem").each(function(i){
		var thisName = $(this).attr("id").slice(1);
		var img = new Image();
		img.src = "pages/"+thisName+"/title.jpg";
		
		if(thisName != "contact" && thisName != "home" && thisName != "login" ){
			var img2 = new Image();
			img2.src = "pages/"+thisName+"/plan.jpg";
			for(var i = 1; i <= 6; i++){
				var img = new Image();
				img.src = "pages/"+thisName+"/thumb"+i+".jpg";
			}
		}
	});*/
}

function growCallback(){
	if($("#headerContainer").height() == 100){
		resizeContent();
		$("body").css("overflow-y","scroll");
		preLoadImages();
	}
}

function grow(){
	$("#topBorder").animate({height:"5px"},1000);
	$("#headerContainer").animate({height:"100px"},1000,"",growCallback);
//	$("#titleContainer").animate({height:"380px"},1000);
	$("#titlePush").css("left",($("body").width()-1252)/2);
	//$("#titlePush").animate({opacity:"1.0", height:"380px", top:"107px"},1000);
	$("#contentSpacerContainer").animate({height:"5px"},1000);
	
}

function resizeContent(){
	var contentHeight = $("body").height() - $("#topSite").height() - $("#footerContainer").height();
	if(contentHeight < 75)
		contentHeight = 75;
	if(browser == "Microsoft Internet Explorer")
		contentHeight = contentHeight + 16;
		
		contentHeight = contentHeight + topScroll;
		
//	$("#contentContainer").height(contentHeight);
}

window.onresize = function(e){
	resizeContent();
	$("#titlePush").css("left",($("body").width()-1252)/2);
	var coverx = ($("body").width()-980)/2;
	if(coverx < 0)
	coverx = 0;
	$("#titleCover").css("left",coverx);
}

var topScroll = 0;



window.onscroll = function (e) {
	if(window.pageYOffset == undefined){
		if(document.documentElement.scrollTop != undefined && document.documentElement.scrollTop != 0){
			topScroll = document.documentElement.scrollTop;
			return;
		}
		else if(document.body.scrollTop != undefined && document.body.scrollTop != 0){
			topScroll = document.body.scrollTop;
			return;
		}
		else
			topScroll = 0;
	}
	
//	$("#footerContainer").css("bottom",(-topScroll)+"px");
	$("#titleCover").css("left",($("body").width()-980)/2);
//	$("#shade").css("top",topScroll+"px");
//	$("#shade").css("left",window.pageXOffset+"px");
	$("#galleryContainer").css("top",topScroll+"px");
	resizeContent();
}

window.onload = function(e) {
	
	$(".navItem").mouseover(function(){$(this).animate({color:'#cbdb2a'}, 250);});
	$(".navItem").mouseout(function(){$(this).animate({color:'#ffffff'}, 250);});
	
//	$("#contentContainer").height($("body").height()/2);
	
	if(window.location.hash)
		lastNav = window.location.hash.slice(1);
	else
		lastNav = "home";
		
	loadPage(lastNav);
//	toggleShade();
	grow();
	
	if(browser == "Microsoft Internet Explorer"){
		$("#contentText").animate({width:"804px"},500);
		$("#headerEmail").animate({left:"360px", top:"60px"},500);
	}else
		$("#headerEmail").animate({left:"806px", top:"60px"},500);
		
}

var openHome = function(){};

function toggleAboutHome(a){
	if(a == true){
		$('#aboutHome').css('visibility', 'visible');
		$('#aboutHome').animate({height:'80px', opacity:'1'},500);
		if(openHome != toggleAboutHome){
			openHome(false);
			openHome = toggleAboutHome;
		}
	}
	else{
		$('#aboutHome').animate({height:'0px', opacity:'0'},500, '', function(){$('#aboutHome').css('visibility', 'hidden');});
	}
	
}


function toggleSpecializedHome(a){
	if(a == true){
		$('#specializeHome').css('visibility', 'visible');
		$('#specializeHome').animate({height:'410px', opacity:'1'},500);
		if(openHome != toggleSpecializedHome){
			openHome(false);
			openHome = toggleSpecializedHome;
		}
	}
	else{
		$('#specializeHome').animate({height:'0px', opacity:'0'},500, '', function(){$('#specializeHome').css('visibility', 'hidden');});
	}
	
}


function toggleProgressHome(a){
	if(a == true){
		$('#stepsHome').css('visibility', 'visible');
		$('#stepsHome').animate({height:'400px', opacity:'1'},500);
		if(openHome != toggleProgressHome){
			openHome(false);
			openHome = toggleProgressHome;
		}
	}
	else{
		$('#stepsHome').animate({height:'0px', opacity:'0'},500, '', function(){$('#stepsHome').css('visibility', 'hidden');});
	}
	
}
