// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var progBarId = "progressbar";

// behavior rules
var appRules = {
	a : function(element){
		element.onfocus = function(){
			this.blur();
		}
	}
}
Behaviour.register(appRules);

// used for observing the pagination links on recent work
document.observe("dom:loaded", function() {
	// the element in which we will observe all clicks and capture
	// ones originating from pagination links
	var container = $("thumbcontainer");
	if (container) {
		container.observe('click', function(e) {
			var el = e.element();
			if (el.match('.pagination a')) {
				new Ajax.Request(el.href, { 
					method: 'get',
					onLoading: function(transport) {
						new Effect.Fade("thumbcontainer", {duration: 0.2, afterFinish: function() {
							$("thumbcontainer").innerHTML = "";
							Element.show("slideloading");
							Element.show("thumbcontainer");
						}});
					},
					onComplete: function(transport){
						Element.hide("slideloading");
					}
				});
				e.stop();
			}
		})
	}
})


function clientInit() {
	console.log("init");

	Ajax.Base.prototype.initialize = Ajax.Base.prototype.initialize.wrap(
	   function(p, options){
	     p(options);
	     this.options.parameters = this.options.parameters || {};
	     this.options.parameters.authenticity_token = authenticityToken || '';
	   }
	);	

	Ajax.Responders.register({
		onComplete: function(){
			hideLoading();
		},
		onLoading: function(){
			showLoading();
		}
	});
}


//
// RECENT WORK
//
function startSwap() {
	Element.hide("info");
	Element.hide("thumbs");
	Element.hide('Photo');
	Element.show('slideloading');
	$("Container").style.backgroundColor = "#efefef";
}

function swap(url) {
    //getCurrentSize();
	imgPreloader = new Image();
	imgPreloader.onload = function() {
		var p = $("Photo");
        if (imgPreloader.width < 720) {
			$("Container").style.backgroundColor = "#ffffff";
		}
		
		//resizePhotoBox();
       	p.src = url;
		Element.hide("slideloading");
		//new Effect.Fade("slideloading", {duration: 0.5, queue: 'end', afterFinish: function(){Element.hide('slideloading')}});
		//new Effect.Fade("slideloading", {duration: 0.5, afterFinish: function(){Element.hide('slideloading')}});
        new Effect.Appear("Photo", {duration: 0.8, queue: 'end'}); //, afterFinish: function(){fadeLoader();}});
	}
	imgPreloader.src = url;
}

function toggleInfo() {
	if ($("info").visible()) { new Effect.Fade("info", {duration: 0.3 }); }
	else { new Effect.Appear("info", {duration: 0.2 }); }
}


// 
// CLIENT AREA
//
function addSelectedToLightbox() {
	console.log("addSelectedToLightbox");
	
	var selected = getSelectedPhotos();
	if (selected.length == 0) {
		alert("No images are selected.");
	}
	else {
		new Ajax.Request("/lightboxes/addphotos", { 
			asynchronous: true,
			parameters: {
				authenticity_token: authenticityToken, 
				"ids[]": selected 
			},
			onSuccess: function() {
				var s = (selected.length == 1 ? "1 image was " : selected.length + " images were ");
				s += "added to the lightbox";
				alert(s);
			}
		});
	}
}

function removeSelectedFromLightbox() {
	var selected = getSelectedPhotos();
	if (selected.length == 0) {
		alert("No images are selected.");
	}
	else {
		new Ajax.Updater("clientMain", "/lightboxes/removephotos", { 
			asynchronous: true,
			parameters: {
				authenticity_token: authenticityToken, 
				"ids[]": selected 
			}
		});
	}	
}

function getSelectedPhotos() {
	var vals = [];
	var cbs = dojo.byId("photos_form").photos_selected;
	if (cbs[0]) {  // is it an array?
		for (var i = 0; i < cbs.length; i++) {
			var e = cbs[i];
			if (e.checked) vals.push(e.value);
		}		
	}
	else {
		vals.push(cbs.value);
	}
	return vals;
}

function photoSelected(id) {
	console.log("photoSelected: ", id);
	
	var d = "photo_" + id;
	if (dojo.byId("photo_cb_" + id).checked) {
		dojo.addClass(d, "slideselected");
	}
	else {
		dojo.removeClass(d, "slideselected");
	}
}

function selectAllPhotos(all) {
	var cbs = dojo.byId("photos_form").photos_selected;
	for (var i = 0; i < cbs.length; i++) {
		var e = cbs[i];
		e.checked = all;
		all == true ? dojo.addClass("photo_" + e.value, "slideselected") : dojo.removeClass("photo_" + e.value, "slideselected");
	}
}



function toggleThumbs() {
	if ($("thumbs").visible()) { hideThumbs(); }
	else { showThumbs(); }
}

function showThumbs() {
	new Effect.Appear("thumbs", {duration: 0.2 });
}

function hideThumbs() {
	new Effect.Fade("thumbs", {duration: 0.3 });
}


//
// PROGRESS BAR
//
function showLoading() {
	console.log("showLoading");
	var e = dojo.byId(progBarId);
	if (e != null) e.style.display = "";
}

function hideLoading() {
	console.log("hideLoading");
	var e = dojo.byId(progBarId);
	if (e != null) e.style.display = "none";
}