//Create a new data store
dojo.addOnLoad(function() {
picsStore = new dojo.data.ItemFileReadStore({url: "includes/pics_json.dot"}, {urlPreventCache: "true"});
getData(queryPics);
txt = new Array();
});
//This function writes the retrieved data to a DIV, and is attached
//to the 'onComplete' event in the request object
var gotPics = function(items, request){
picsItems = items;
var randomnumber=Math.floor(Math.random()*picsItems.length)
currentIndex = randomnumber;
cap = picsItems[randomnumber].cap;
drawCufonCaption(cap);
getNextPic();
}

//This query finds all items that have a 'type' attribute set to 'continent'
var queryPics = {collection:"cba"};

var getNextPic = function(){
	currentIndex = ((picsItems.length - 1) < currentIndex) ?  1 : (currentIndex + 1);
	var animArgsEven = {
		node: "pictures_even",
		duration: 1000, // ms to run animation
		delay: 250 // ms to stall before playing
	    };
	
		var animArgsOdd = {
			node: "pictures_odd",
			duration: 1000, // ms to run animation
			delay: 250 // ms to stall before playing
		    };
		
	//currentIndex = currentIndex + 1;
	if(currentIndex % 2 == 0){
	//console.log(currentIndex);
	currentpic = picsItems[currentIndex -1];
	dojo.byId("pictures_even").innerHTML = '<img src=\"' + currentpic.file + '\"' + 'alt=\"' + currentpic.cap + '\" />';
	dojo.fadeOut(animArgsOdd).play();
	dojo.fadeIn(animArgsEven).play();
	}
		else {
			//console.log(currentIndex);
			currentpic = picsItems[currentIndex -1];
			dojo.byId("pictures_odd").innerHTML = '<img src=\"' + currentpic.file + '\"' + 'alt=\"' + currentpic.cap + '\" />';
			dojo.fadeOut(animArgsEven).play();
			dojo.fadeIn(animArgsOdd).play();
		}
		doCaption(currentpic.cap);
	}

//Fetch the data.
var getData = function(query){
picsStore.fetch({query: query, onComplete: gotPics});
};

var fadeOutCaption = function() {
	var animArgs = {
		node: ["caption", "caption_shadow"],
		duration: 1000, // ms to run animation
		delay: 0 // ms to stall before playing
	    };
	dojo.fadeOut(animArgs).play();
}

var replaceCaption = function(cap){
	dojo.byId("white_caption").innerHTML = cap;
	Cufon.replace(dojo.byId("caption"));
}

var fadeInCaption = function() {
	var animArgs = {
		node: ["caption", "caption_shadow"],
		duration: 1000, // ms to run animation
		delay: 0 // ms to stall before playing
	    };
	dojo.fadeIn(animArgs).play();
}

var doCaption = function(cap) {
	var node = dojo.byId("caption");
	var white_caption = dojo.byId("white_caption");
	var caption_shadow = dojo.byId("caption_shadow")
	dojo.fadeOut({
	        node: node,
	        onEnd: function(){
	                caption_shadow.innerHTML = cap,
					Cufon.replace(caption_shadow),
					white_caption.innerHTML = cap,
					Cufon.replace(white_caption)
	                dojo.fadeIn({
	                        node: node
	                }).play()
	        }
	}).play();
}

var drawRaphCaption = function(cap){
	var attr1 = {"font": '15px "Verdana"', opacity: 1, "color": "#ccc", "font-weight": "bold", "text-anchor": "end"};
	var attr2 = {"font": '15px "Verdana"', opacity: 0.5, "font-weight": "bold", "text-anchor": "end"};
	txt[0][0] = paper.text(612, 11.5, cap).attr(attr2).attr({"fill": "#000"});
	txt[1][0] = paper.text(610, 10, cap).attr(attr1).attr({"fill": "#eee"});
};

var drawCufonCaption = function(cap){
	dojo.byId("white_caption").innerHTML = cap;
	Cufon.replace(dojo.byId("white_caption"));
	dojo.byId("caption_shadow").innerHTML = cap;
	Cufon.replace(dojo.byId("caption_shadow"))
	//amin2.play();
};