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

// Validator
// Copyright 2008 x-rails ltd.
var Validator = {
	
	validate: function(argsHash) {
		// initialize
		var element = argsHash.get('element');
		var funcArray = argsHash.get('funcArray');
		var errors = [];
		
		funcArray.each( function(name, index) {
			// we are in the context of errors
			// which is an array... so we can use push
			this.push(Validator.evaluate(name, index, argsHash));
		}, errors);
		
		// remove blank array elements
		errors = errors.without('');
		
		// get the first errormsg
		var msg = errors.length == 0 
							? "ok"
							: errors.first();
		
		var field = $(element).ancestors()[1];
		
		// updates the hint element
		$(element + "_hint").removeClassName("error");
		field.removeClassName('error-on-field');
		if (errors.length > 0) {
			$(element + "_hint").addClassName("error");
			field.addClassName('error-on-field');
		}											 
		
		$(element + "_hint").update(msg);
	},
	
	/**
	 * params:
	 * @index			-> position of the needed 
	 *							 validation parameters in the hash arrays
	 * @argsHash	-> holds the parameters (JSON structure!)
	 */
	validates_presence_of: function (index, argsHash) {
		// hash has to be initialized because
		// we have a JSON structure in argsHash
		argsHash = new Hash(argsHash);
		var element = argsHash.get('element');
		var errormsg = argsHash.get('errormsgs')[index];
		
		return $F(element).length == 0 
								? errormsg 
								: "";
	},
	
	validates_length_of: function (index, argsHash) {
		argsHash = new Hash(argsHash);
		var element = argsHash.get('element');
		var errormsg = argsHash.get('errormsgs')[index];
		var length = argsHash.get('params')[index];
		
		return $F(element).length < length 
							? errormsg + ' ' + length
							: "";
	},
	
	validates_format_of: function (index, argsHash) {
		argsHash = new Hash(argsHash);
		var element = argsHash.get('element');
		var errormsg = argsHash.get('errormsgs')[index];
											
		return eval(argsHash.get('params')[index]).exec($F(element)) == null 
							? errormsg
							: "";
	},
	
	/**
	 * Calls the needed functions with specific parameters.
	 */
	evaluate: function (name, index, argsHash) {
		return eval('Validator.' + name + '(' + index + ',' + argsHash.toJSON() + ')');
	}
	
}

var Helper = {
	
	max: function(x, y) {
		return x > y ? x : y;
	},
	
	initailizeHighlightEvents: function(element){
		$(element).select('.field input').each(function (element) {
			element.observe('focus', function() {
				element.ancestors()[1].toggleClassName("highlight");
			});
			
			element.observe('blur', function() {
				element.ancestors()[1].toggleClassName("highlight");
			});
		});
		
		$(element).select('.field textarea').each(function (element) {
			element.observe('focus', function() {
				element.ancestors()[1].toggleClassName("highlight");
			});
			
			element.observe('blur', function() {
				element.ancestors()[1].toggleClassName("highlight");
			});
		});
	},
	
	changeGroupImage: function(oThumb, sImageID, sLargeImagePath, sImageText) {
		oImages = $("Images").getElementsBySelector("a");
	
		for (iCnt = 0; iCnt < oImages.length; iCnt++)
		{
			if (oImages[iCnt].hasClassName("Current"))
				oImages[iCnt].removeClassName("Current");
		}
	
		oThumb.addClassName("Current");
		$(sImageID).src = sLargeImagePath;
		$(sImageID).alt = sImageText;
		$(sImageID).title = sImageText;
		$("ImageText").innerHTML = sImageText;
	},
	
	changeHistoryImage: function(sYear, oThumb, sImageID, sLargeImagePath, sImageText) {
		oImages = $("HistoryImages").getElementsBySelector("a");

		for (iCnt = 0; iCnt < oImages.length; iCnt++) {
			if (oImages[iCnt].hasClassName("Current"))
			oImages[iCnt].removeClassName("Current");
		}

		oThumb.addClassName("Current");

		iTop = ($(sYear).positionedOffset($("Content"))[1] - $("Top").getHeight() + 23);

		bFade = (iTop + "px" == $("ImageRight").style.top);

		$("ImageRight").style.top = iTop + "px";

		$(sImageID).src = sLargeImagePath;
		$(sImageID).alt = sImageText;
		$(sImageID).title = sImageText;

		if ($("ImageText") != null) {
			$("ImageText").innerHTML = sImageText;
		}

		if (bFade) {
			$("BigImage").setOpacity(0);
			Helper.showHistoryImage(1);
		}
	},
	
	showHistoryImage: function(iOpacity) {
		$("BigImage").setOpacity(iOpacity / 100);

		if (iOpacity < 100)	{
			iOpacity *= 1.7;

			if (iOpacity > 100)
				iOpacity = 100;

			setTimeout("Helper.showHistoryImage(" + iOpacity + ")", 30);
		}
	},

  openFileBrowser: function(link, sReturnUrl, sType, iType) {
		window.open(link + '?module=' + sType + 'mId='+ iType +'&returnurl=' + escape(sReturnUrl), 'browser', 'width=655,height=480,top=150,left=100,screenY=150,screenX=100, scrollbars');
	},
	
	toggleToolTip: function(caller) {
		var pos = Position.positionedOffset(caller);
		$('tooltip').setStyle({left: (pos[0] + 10) + "px", top: (pos[1] + 30) + "px"});
		$('tooltip').toggle();
		$('tooltip').innerHTML = caller.next().innerHTML;
	}
	
};


var DropDown = {
	
	over: function(element) {
		$(element).setStyle({zIndex: '3000'});
		$(element).select('ul').last().show();
		$(element).select('ul li a').first().toggleClassName('over');
	},
	
	out: function(element) {
		$(element).setStyle({zIndex: '1'});
		$(element).select('ul').last().hide();
		$(element).select('ul li a').first().toggleClassName('over');
	}
	
};

/**
 * Initialize some event listeners...
 */
Event.observe(window, 'load', function() {
	Helper.initailizeHighlightEvents(document.body);
});
