// JavaScript Document


//dojo requires
dojo.require("dojo.fx");
dojo.require("dijit.layout.AccordionContainer");
dojo.require("dijit.layout.AccordionPane");
dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.Tooltip");


//globals in "blayne" namespace
var blayne = {
	dotsInteriors: 17,
	dotsDresses: 12,
	dotsWonderland: 19,
	zones: ["homeblock","aboutblock","galleryblock","pressblock","contactblock"],
	currentNav: "nav1",
	fadeTime: "500",
	isBusy: false,
	currentImg: picConcat("interiors","int",0,false),
	currentImgId: "int0",
	isLoaded: true
};

blayne.currentZone = blayne.zones[0];




//start functions
dojo.addOnLoad(function(){
						
 	//accordion menu load
	var container = new dijit.layout.AccordionContainer({style:"height: 200px; width: 160px"}, "gallerymenu");
 	
	container.addChild(new dijit.layout.ContentPane({
		title: "Interiors",
		content: dotLoader("interiors","int",17),
		selected: true
	}));
		
	container.addChild(new dijit.layout.ContentPane({
		title: "Dresses",
		content: dotLoader("dresses","drs",12),
		selected: false
	}));	
	
	container.addChild(new dijit.layout.ContentPane({
		title: "Wonderland",
		content: dotLoader("wonderland","wnd",19),
		selected: false
	}));

	container.startup();
	
	document.getElementById("int0").style.color = "#a5c0aa";
	
	//home screen preload
	var i = 0;
	
	imageObj = new Image();
	images = new Array();
    images[0]="imgs/med/bbi-splash.png";
    images[1]="imgs/med/fp0.jpg";
    images[2]="imgs/med/fp1.jpg";
    images[3]="imgs/med/fp2.jpg";
	
	for(i=0; i<=3; i++) {imageObj.src=images[i];}
	
	document.getElementById(blayne.currentNav).className = "navbutton currentnav";
	
	//fade animation variables
	var coverOut = dojo.fadeOut({	
		node: "coverblock",
		duration: "600",
		delay: "1600",
		beforeBegin: function(){
			document.getElementById('aboutblock').style.display = 'none';
			document.getElementById('galleryblock').style.display = 'none';
			document.getElementById('pressblock').style.display = 'none';
			document.getElementById('contactblock').style.display = 'none';
		},
		onEnd: function(){document.getElementById('coverblock').style.display = 'none';}
	});
	
	var fpr0Out = dojo.fadeOut({node:"fpright0", duration:"3000", delay:"0" });
	var fprIn = dojo.fadeIn({ node:"fpright", duration:"3500", delay:"0" });
	
	var bbiOut = dojo.fadeOut({ node:"bbisplash", duration:"2400", delay:"500" });
	var fplIn = dojo.fadeIn({ node:"fpleft", duration:"1700", delay:"1300" });
	
	var menuPop = dojo.animateProperty({
			node:"nav1",
			properties: {color: '#47664D'}
	});
	
	var faders = dojo.fx.combine([fpr0Out, fprIn, bbiOut, fplIn]);
	
	//currently not popping Home menu
	dojo.fx.chain([coverOut, faders/*, menuPop*/]).play();

});


function dotLoader(category,cat,topfile){
		//populates a field of dots, and gives them image link code
		var j=1;
		
		var htmlString = new String;
		htmlString = "<div class='blip' id='"+cat+0+"' onclick='picPop(&#39;"+category+"&#39;,&#39;"+cat+"&#39;,"+0+")'>&#8226;</div> <div dojoType='dijit.Tooltip' showDelay='0' connectId='"+cat+0+"' position='above'><img src='"+picConcat(category,cat,0,true)+"'/></div>";
		
		for(j=1; j<=topfile; j++){
			htmlString = htmlString+"<div class='blip' id='"+cat+j+"' onclick='picPop(&#39;"+category+"&#39;,&#39;"+cat+"&#39;,"+j+")'>&#8226;</div> <div dojoType='dijit.Tooltip' showDelay='0' connectId='"+cat+j+"' position='above'><img src='"+picConcat(category,cat,j,true)+"'/></div>";
		
		}
		
		return htmlString;
}

function picConcat(category,cat,file,thumb){
	var fileString = new String;
	if (thumb == true){
		fileString = "imgs/med/"+category+"/thumb/"+cat+file+".jpg";
		return fileString;
	} else {
		fileString = "imgs/med/"+category+"/"+cat+file+".jpg";
		return fileString;
	}
}


function picPop(category,cat,file){
	//the funciton that displays and fades out images in the gallery
	var fileString = new String;
	fileString = "imgs/med/"+category+"/"+cat+file+".jpg";
	if (fileString == blayne.currentImg){
		return;
	} else if (blayne.isLoaded == false) {
		return;	
	}
	
	blayne.isLoaded = false;
	
	var myFadeIn = dojo.fadeIn({
				node: "imgtater",
				duration: blayne.fadeTime,
				onEnd: function(){blayne.isLoaded = true;}
	})
	
	var newImg = new Image();
	
	newImg.onload = function(){
		myFadeIn.play();
	}
	
	//alert(document.images.tater.src);
	
	var myFadeOut = dojo.fadeOut({
		node: "imgtater",
		duration: blayne.fadeTime,
		onEnd: function(){
			newImg.src = fileString;
			document.getElementById(blayne.currentImgId).style.color = "#000000";
			blayne.currentImgId = cat+file;
			document.getElementById(blayne.currentImgId).style.color = "#a5c0aa";
			document.images.tater.src = fileString;
			blayne.currentImg = fileString;
		}
	}).play();
	
	//dojo.fx.chain([myFadeOut, myFadeIn]).play();
}


function arrowd(direction){
	//takes in a direction L/R, and moves the image train one to the left or right
	var category = [];
	category = blayne.currentImg.split("/",3);
	var cat = blayne.currentImgId.substr(0,3);
	var file = blayne.currentImgId.substr(3);
	
	//alert(file);
	
	if (direction.id == "leftarrow"){
	
		if (file == 0) {
			if (cat == "int"){file = blayne.dotsInteriors;}
			else if (cat == "drs"){file = blayne.dotsDresses;}
			else if (cat == "wnd"){file = blayne.dotsWonderland;}
		} else {
			file--;
		}
		
		//alert(blayne.currentImgId);
		//alert(category[2]+" is the category and "+cat+" is the cat and "+file+" is the file");
		picPop(category[2],cat,file);
		return;
	
	} else if (direction.id == "rightarrow"){
		
		if ((cat == "int")&&(file >= blayne.dotsInteriors)){file = 0;}
		else if ((cat == "drs")&&(file >= blayne.dotsDresses)){file = 0;}
		else if ((cat == "wnd")&&(file >= blayne.dotsWonderland)){file = 0;}
		else {
			file++;	
		}
		
		picPop(category[2],cat,file);
		return;
	}
}


function myFadeOut(zoneNumber){
	//general content fade outs
	myFade = dojo.fadeOut({
		node: blayne.zones[zoneNumber],
		duration: blayne.fadeTime,
		onEnd: function(){document.getElementById(blayne.zones[zoneNumber]).style.display = 'none';}
	});
	
	return myFade;
}

function myFadeIn(zoneTitle){
	//general content fade outs
	myFade = dojo.fadeIn({
		node: zoneTitle,
		duration: blayne.fadeTime,
		beforeBegin: function(){blayne.isBusy = true;},
		onEnd: function(){blayne.isBusy = false;}
	});
	
	return myFade;
}

function goMenu(thisIn){
	//action for menu buttons, returns if in current zone, fades out others if moving to a new zone
	thisTitle = thisIn.getAttribute("title").toLowerCase() + "block";
	
	if (thisTitle == blayne.currentZone) {
		return;
	
	} else if (blayne.isBusy) {
		return;
		
	} else {
		document.getElementById(thisTitle).style.display = "block";
		
		var fades = ["","","",""];
		
		var i = 0;
		var j = 0;
		for (i=0; i<=4; i++){
			if (thisTitle != blayne.zones[i]){
				fades[j] = i;
				j++;
			}	
		}
	
	document.getElementById(blayne.currentNav).className = "navbutton";
	blayne.currentNav = thisIn.id;
	thisIn.className = "navbutton currentnav";
	blayne.currentZone = thisTitle;
	dojo.fx.combine([myFadeOut(fades[0]), myFadeOut(fades[1]), myFadeOut(fades[2]), myFadeOut(fades[3]), myFadeIn(thisTitle)]).play();
		
	}
}

