var Site = {

	styleSheets: Array("text-decrease","text-standard", "text-increase"),
	styleSheetIndex: 1,
	styleSheetCookie: null,
	cookieDomain: "rme.local",
	/*documentIds: [],*/
	

	start: function() {
		if ($('search')) Site.addSearchEventListeners();
		if ($('kwick')) Site.parseKwicks();
		
		
	
		// Launch-in-new-window links automagically created
		var extLinks = $$('a.external');
		if ( extLinks.length ) {
			extLinks.each(function(elem, idx) { 
				elem.setProperty('target', '_blank');
			});
		}
		
		//Print automagic
		$$('.api-page #printer-friendly a').addEvent('click',function(e) {
			e.stop();
			window.print();
			return false;
		});
		
		//Breadcrumbles Automagic
		if ($('breadcrumbs')) {
			$('breadcrumbs').breadcrumbles();
		}
		
		//automagic mootable2. Give your table container (a div or whatever) a class of mootable2
		$$('.mootable2').each(function(tableContainer) {
			new mooTable2(
				tableContainer, 
				{
					'replaces':			tableContainer.getFirst('table'),
					'updateType':		'replacement',
					'rowsPerPage':		30,
					'funcRowCreate':	function(row) {
						
						//Set up hover effect
						row.fx = new Fx.Morph(row,	{	
								'duration': 50,
								'wait':	false,
								'transition': Fx.Transitions.Quad.easeOut
						});
						
						//Try and get the link from the first anchor inside the first td. Applies link to whole row. 
						firstTd = row.getFirst('td');
						firstLink = firstTd.getFirst('a');
						
						if (firstLink) {
							row.set('href',firstLink.get('href'));
							row.addEvent('click', function() {	
								window.location.href = row.get('href');
							});
						}
						
					},
					'funcRowOver':		function(row) {
						this.fx.start('.mooTable tr.rowActive');
					},
					'funcRowOff':		function(row) {
						this.fx.start('.mooTable tr.rowInactive');
					}
				}
			);
		});
		
		
		if ($('gallery') ){
			
			var even = $$('#gallery li:nth-child(4n+1)');
			var odd = $$('#gallery li:nth-child(4n)');
			odd.each(function (elem, idx) {
				elem.addClass('omega');
			});
			even.each(function (elem, idx) {
				elem.addClass('alpha');
			});
		}
		
		if($('panels') ){
		
			var third = $$('#panels li:nth-child(3n)');
			
			third.each(function(elem, idx) {
				elem.addClass('third-child');
			});

		}
		

		//TEXT RESIZE
		Site.styleSheetCookie = Cookie.read("styleSheet");
		Site.styleSheetIndex = Site.styleSheetCookie ? Site.styleSheetCookie : Site.getPreferredStyleSheet();
		
		Site.setActiveStyleSheet(0);
		
		if ($('text-increase') && $('text-decrease')) Site.addToolsEventListeners();
	
	
		//CLIENT LOGIN AREA
		if ($('change-password-mask') && $('change-password-fields')) {
			$('change-password-fields').hide();
			$('change-password-mask').addEvent('click',function() {
				$('change-password-fields').show();
				$('old-password').focus();
				$('change-password-mask').hide();
			});
		}
		
		
		//GENERIC FIELD MASKS
		$$('.field-mask').each(function(el) {
			var field = el.getNext();
			if (field) {
				$(field).hide();
				el.addEvent('click',function() {
					field.show();	
					this.hide();
					return false;
				});
			}
		});

		// Form validation automagic
		var valForms = $$('form.validate-form');
		if ( valForms.length && FormValidator) {
			valForms.each(function(elem, idx) { 
				new FormValidator.Inline(elem, {
					'onFormValidate': Site.formHandler,
					'errorPrefix': '',
					'useTitles': true
				});
			});
		}
		
		//ACCORDION
		if($("client-documents-listing")) {
			
			$$('.nested-element').hide();
			
			$$('.toggler').addEvent('click', function(e){
				e.preventDefault();
				this.getNext().toggle();
				
			});
			
			$$('.nested-toggler').addEvent('click', function(e){
				e.preventDefault();
				this.getNext().toggle();
				
			});
	
		}

		if ($('show-all')) {

			$('show-all').addEvent('click', function(e){
				$$('.element').show();
			});

			$('hide-all').addEvent('click', function(e){
				$$('.element').hide();
			});
		}

		
		$$('input[id^=file-parent-]').each(function(fileEl){
			fileEl.addEvent("change", function() {
				if(this.get("checked")) {
					$$('.child-' + this.get('value')).set("checked",true);
				} else {
					$$('.child-' + this.get('value')).set("checked", false);
				}	
			});
		});
		
		
		if($('select-all-documents')) {
			$('select-all-documents').addEvent("click", function() {
				$$('input').set("checked", true);
				return false;
			});
		}
		
		if($('unselect-all-documents')) {
			$('unselect-all-documents').addEvent("click", function() {
				$$('input').set("checked", false);
				return false;
			});
		}

	},
	
	
	//SEARCH
	addSearchEventListeners: function () {
		var label = $('search').getElement('label');
		var query = $('search').getElement('#query');
		query.addEvents({
			'focus' : function () {
				label.setStyle('left', '-9999px');
			},
			'blur' : function () {
				if (this.value == '')
					label.setStyle('left', '0');
			}
		});
	},
	//END SEARCH
	
	//KWICKS
	parseKwicks: function() {
	
		var squeeze_to = 100;
		var max_width = 299;
	
		//get original widths
		var start_widths = new Array();
		var kwicks = $$('#kwick .kwick');
		//var fx = new Fx.Elements(kwicks, {wait: false, duration: 250, transition: Fx.Transitions.quadOut});
		var fx = new Fx.Elements(kwicks, {wait: false, duration: 250});
		kwicks.each(function(kwick, i){
		
			start_widths[i] = kwick.getStyle('width').toInt();
		
			kwick.addEvent('mouseenter', function(e){
			
				var obj = {};
				obj[i] = {
					'width': [kwick.getStyle('width').toInt(), max_width]
				};
		
				kwicks.each(function(other, j){
					if (other != kwick){
						var w = other.getStyle('width').toInt();
						if (w != squeeze_to) obj[j] = {'width': [w,squeeze_to] };
					}
				});
				fx.start(obj);
			}
			);
		});
	
		$('kwick').addEvent('mouseleave', function(e){
			var obj = {};
			kwicks.each(function(other, j){
				obj[j] = {'width': [other.getStyle('width').toInt(), start_widths[j]]};
			});
			fx.start(obj);
		});
	},
	//END KWICKS
	
	
	//INCREASE, DECREASE & DEFAULT TEXT SIZES
	
	addToolsEventListeners : function() {
		
		$('text-increase').addEvent('click', function(e) {
			e.stop();
			Site.setActiveStyleSheet(1);											  
		});
		$('text-decrease').addEvent('click', function(e) {
			e.stop();
			Site.setActiveStyleSheet(-1);											  
		});
		
	},
	
	getActiveStyleSheet: function() { return Site.styleSheetIndex; },
	
	getPreferredStyleSheet: function() { return 1; },
	
	getCookie: function(name) { return Cookie.read(name); },
	
	setActiveStyleSheet: function(index) {

		Site.styleSheetIndex = parseInt(Site.styleSheetIndex) + index;
		if(Site.styleSheetIndex < 0){
			Site.styleSheetIndex = 0;
		} else if(Site.styleSheetIndex >= Site.styleSheets.length) {
			Site.styleSheetIndex = Site.styleSheets.length - 1;
		}
		
		Site.setCookie("styleSheet", Site.styleSheetIndex, 1);
		
		var title = Site.styleSheets[Site.styleSheetIndex];
		
		var i, a, main;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && a.getAttribute("title").indexOf("text") > -1) {
				a.disabled = true;
				if(a.getAttribute("title") == title) a.disabled = false;
			}
		}
		
		try {
			Cufon.refresh();
		} catch (e) {
			//nothing
		}
		
		return null;
		
	},
	
	setCookie: function(name,value,days) {
		Cookie.write(name, value, {domain : Site.cookieDomain, duration: days ? days : 0});
	},
	
	//END INCREASE, DECREASE & DEFAULT TEXT SIZES
	
	
	'doUserContext': function (event, elem, id, status) {
		
		if ( checkRightClick(event) ) {
			switch ( status ) {
				
				case "Disabled": 
					var menus = {	0: {	'title':		'Enable User',
											'url':			'/admin/f/users/status/'+id+'/active',
											'icon':			'/images/icon_add.gif'
										},
									1: {	'title':		'Delete User',
											'url':			'/admin/f/users/delete/'+id,
											'icon':			'/images/icon_delete.gif'
										}	
								}
					break;
				
				default: 
					var menus = {	0: {	'title':		'Disable User',
											'url':			'/admin/f/users/status/'+id+'/disabled',
											'icon':			'/images/icon_remove.gif'
										}
								}
					break;
			}
			
			var params = {	'optionTransition': Fx.Transitions.Quad.easeOut };
			
			ContextMenu.doContext(event, elem, menus, params)
		}
		
		return false;
	}
	
	
	

};

window.addEvent('domready', Site.start);
