var scomix = {};	
scomix.gallery = {};

scomix.gallery.init = function() {
	scomix.gallery.viewer = new PhotoViewer;
	if (dojo.byId("filmstrip")) {
		scomix.gallery.imgs = dojo.byId("filmstrip").getElementsByTagName("img");
		scomix.gallery.left = 0;
		scomix.gallery.right = (scomix.gallery.imgs.length <= 4) ? scomix.gallery.imgs.length - 1 : 3;
		scomix.gallery.locked = false;
		for (var i = 0; i < scomix.gallery.imgs.length; i++) {
			var img = scomix.gallery.imgs[i];
			scomix.gallery.viewer.add(img.src,img.getAttribute("author"),img.getAttribute("date"));
		}		
	}
}


scomix.gallery.scrollLeft = function() {
	if (scomix.gallery.imgs && (scomix.gallery.left > 0) && !scomix.gallery.locked) {
		scomix.gallery.locked = true;
		dojo.lfx.html.slideBy(dojo.byId("filmstrip"),{top:0,left:135},400,dojo.lfx.easeInOut,function() {
			scomix.gallery.left--;
			scomix.gallery.right--;
			scomix.gallery.locked = false;
		}).play();
	} /* else {
		if (!scomix.gallery.imgs) {
			alert("not scrolling because scomix.gallery.imgs = undefined");
		} else if(scomix.gallery.left <= 0) {
			alert("not scrolling because scomix.gallery.left <= 0");
		} else if (scomix.gallery.locked) {
			alert("not scrolling because scomix.callery.locked = true");
		} else {
			alert("ERROR: not scrolling for unknown reason");
		}
	} */
}

scomix.gallery.scrollRight = function() {
	if (scomix.gallery.imgs && (scomix.gallery.right != scomix.gallery.imgs.length - 1) && !scomix.gallery.locked) {
		scomix.gallery.locked = true;
		dojo.lfx.html.slideBy(dojo.byId("filmstrip"),{top:0,left:-135},400,dojo.lfx.easeInOut,function() {
			scomix.gallery.left++;
			scomix.gallery.right++;
			scomix.gallery.locked = false;
		}).play();	
	} /* else {
		if (!scomix.gallery.imgs) {
			alert("not scrolling because scomix.gallery.imgs = undefined");
		} else if(scomix.gallery.right == scomix.gallery.imgs.length-1) {
			alert("not scrolling because scomix.gallery.right = scomix.gallery.imgs.length-1");
		} else if (scomix.gallery.locked) {
			alert("not scrolling because scomix.callery.locked = true");
		} else {
			alert("ERROR: not scrolling for unknown reason");
		}
	} */
}

scomix.news = {};
scomix.news.toggle = function(id) {
	var tmp = dojo.byId(id);
	scomix.news.locked = true;
	if (tmp.showing) {
		dojo.lfx.propertyAnimation(dojo.byId(id),{"height":{start:dojo.byId(id).offsetHeight, end:36}},400,dojo.lfx.easeInOut,function() { scomix.news.locked = false; }).play();
		dojo.byId(id).showing = false;
	} else {
		dojo.lfx.propertyAnimation(dojo.byId(id),{"height":{start:36, end:dojo.byId(id+"inner").offsetHeight}},400,dojo.lfx.easeInOut,function() { scomix.news.locked = false; }).play();
		dojo.byId(id).showing = true;
	}
}

scomix.search = {};
scomix.search.execute = function() {
	var query = dojo.byId("search").value;
	var container = document.createElement("div");
	
	// Create a search control
	searchControl = new google.search.SearchControl();
	
	// Add in a full set of searchers
	var siteSearch = new google.search.WebSearch();
	siteSearch.setUserDefinedLabel("scuolacomix.com");
	siteSearch.setUserDefinedClassSuffix("siteSearch");
	siteSearch.setSiteRestriction("scuolacomix.com");
	siteSearch.setResultSetSize(google.search.Search.LARGE_RESULTSET);
	searchControl.addSearcher(siteSearch);
	
	searchControl.setSearchCompleteCallback(this,function() {
		var inputs = container.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++) {
			if ((inputs[i].type) && (inputs[i].type.toLowerCase() == "submit") && (inputs[i].value) && (inputs[i].value.toLowerCase() == "search")) {
				inputs[i].value = "Cerca";
			}
		}
		dojo.byId("content").innerHTML = "";
		dojo.byId("content").appendChild(container);
	});
	
	// Tell the searcher to draw itself and tell it where to attach
	searchControl.draw(container);	
	
	searchControl.execute(query);
};
scomix.search.check = function(evt) {
	if (evt.keyCode == 13) {
		scomix.search.execute();
	}
}
