//railsUI
//Prototype based UI framework
//http://www.prototypejs.org/
//uses Scriptaculous for effects
//---------------------
//Author: Richard Testani
//3.rdRail.net


//rails UI class
//provide some basic tools for UI parts
var railsUI = Class.create();
railsUI.prototype = {
	
	allLinks: document.getElementsByTagName('a'),
	listItems: document.getElementsByTagName('li'),
	
	initialize: function() {
		//none
	},
	
	setAllElements: function(el, messaging) {
	
		var allEls = document.getElementsByTagName(el);
		return allEls;
	
	},
	
	
	getElAttribute: function(el, attrib, showMsg) {
		var el = this.elIsNull(el);

		var attName = attrib;
		if(el == null) {

			return null;
		} else if(el.readAttribute(attName) == null) {
			var message = new messaging("The attribute ["+attName+"] specified is not avialable for this element", showMsg);
			return null;
		} else {
			return el.readAttribute(attName);
		}

	}

};



//Rails Messaging object - display alerts on your page
var railsMessaging = Class.create();
railsMessaging.prototype = Object.extend(new railsUI(), {

	initialize: function(message, showMsg) {
		if(!showMsg) {
			this.showMsg = showMsg;
		} else {
			this.showMsg = true;		
		}
		this.message = message;
		this.closeMessage = "<a href=\"javascript:void(null);\"><span id=\"dismiss\"></a></span>";
		this.divWrapper = "<div id=\"messaging\">"+this.message+this.closeMessage+"</div>";
		
		if(this.showMsg) {
			if(!$('messaging')) {
				new Insertion.Top('wrapper', this.divWrapper);
				$('messaging').hide();
				new Effect.Appear('messaging', {
				beforeStart: this.styleMessage,
				duration: 0.5
				})
				$('messaging').onclick = this.dismissMessage;
			}
		}
	},
	
	
	styleMessage: function() {
		this.height = $('messaging').getHeight();
		this.width = $('messaging').getWidth();
		this.halfWidth = new Number((this.width / 2));
		var marginLeft = "-"+this.halfWidth+"px";
		$('messaging').setStyle({
			top: "50%",
			marginLeft: marginLeft,
			left: "50%",
			color: 'red'
		});
	
	},
	
	dismissMessage: function() {
		$('messaging').remove();
		return true;
	}
	

});


//thumbnail image gallery class
var railsThumbGallery = Class.create();
railsThumbGallery.prototype = Object.extend(new railsUI(), {

	initialize: function(targetID, imgClass) {
	
		this.targetID = targetID;
		this.imgClass = imgClass;
		
		this.allIMGS = document.getElementsByTagName('img');
		imgNum = $(this.allIMGS).length;
		
		for(i=0; i<imgNum; i++) {
		
			if($(this.allIMGS[i]).hasClassName(this.imgClass)) {
			
				Event.observe($(this.allIMGS[i]), 'click', this.changeImage.bindAsEventListener(this));
			
			}
		
		}
	
	},
	
	
	changeImage: function(e) {
		imageItem = $(e.target);
		$(this.targetID).src = imageItem.src;
	
	},
	
	highlightCurrent: function() {
	
	}

});



//sliding layer class
var slideDrawer = Class.create();
slideDrawer.prototype = {

	initialize: function(drawerId, view) {
		this.drawer = $(drawerId);
		this.handle = $('drawer_tab');
		this.drawerView = view;

		$(this.handle).observe('click', this.toggleSlide.bindAsEventListener(this));
		
		if($('viewProperties')) {
		
			$('viewProperties').observe('click', this.toggleSlide.bindAsEventListener(this));
			
		}
	
		
		if(!this.drawerView) {
			this.slideOut();
		}
		
		
	},
	
	toggleSlide: function(e) {
		
		if(this.drawerView) {
		
			this.slideOut();
			this.drawerView = false;
			
		
		} else {

			this.slideIn();
			this.drawerView = true;
		
		}
	
	},
	
	slideIn: function() {

		new Effect.Move (this.drawer,{ x: 0, y: 0, mode: 'absolute'});

		
	},
	
	slideOut: function() {
		
		new Effect.Move (this.drawer,{ x: -240, y: 0, mode: 'absolute'});

	}

}

var formChecker = Class.create();
formChecker.prototype = Object.extend(new railsUI(), {

	initialize: function(formName) {
	
		this.formName = formName;
		this.formValues = $(this.formName).getInputs();
		//$(this.formName).observe('submit', this.checkRequired.bindAsEventListener(this));
		$(this.formName).focusFirstElement();
	},
	
	checkRequired: function() {
		this.returnError = new Array()
		for(i=0; i < this.formValues.length; i++){
		
			if($(this.formValues[i]).hasClassName('required') && $(this.formValues[i]).value == "") {
				errorid = $(this.formValues[i]).id + "_error";
				this.displayError(errorid);
				this.returnError[i] = errorid;
				
			} else {
				errorid = $(this.formValues[i]).id + "_error";
				
				if($(errorid)) {
					$(errorid).removeClassName('error');
					$(errorid).innerHTML = "";
				
				}
			}
		
		}

		if(this.returnError.length > 0) {
		
			return this.sendForm(false);
		
		} else {
		
			return this.sendForm(true);
		
		}

	
	},
	
	displayError: function(field) {
		this.errorField = field;
		$(this.errorField).addClassName('error');
		//$(this.errorField).innerHTML = "Required empty value";
		return this.sendForm(false);
	},
	
	validateEmail: function() {
	
	
	},
	
	sendForm: function(send) {
	
		return send;
	
	}

});

$('before').observe('mouseover', enlarge);
$('before').observe('mouseout', shrink);
$('after').observe('mouseover', enlarge);
$('after').observe('mouseout', shrink);
function enlarge(e) {
	var el	=	e.target;
	var id	=	el.id;
	
	new Effect.Scale($(id), 110, {scaleFromCenter:true, duration: 0.1});
} 

function shrink(e) {
	var el	=	e.target;
	var id	=	el.id;
	
	new Effect.Scale($(id), 110, {scaleFromCenter:true, duration: 0.1});
}

