// Init menu
var IYAClass = new Class({

	initialize: function(){
		
		window.addEvent("domready",function(){
			this.passwordclear();
			this.cleartext();
			this.integerOnly();
			this.NumbersOnly();
			this.NoPaste();
			this.Buttons();
			this.windowbox();
			this.GalleryInit();
			this.disabled();
			this.SearchActivate();
			this.RowMarkCheckbox();
			this.searchnavigationInit();
		}.bind(this));
		
		//alert(this.DetectPlugins('Acrobat'));
		
	},
	
	searchnavigationInit : function(){
	
		var searchnavigationTabs = $$('.searchnavigation li');
		if(searchnavigationTabs) {
			searchnavigationTabs.each(function(tab) {
				tab.addEvent('click', function() {				
					window.location = tab.getElement('a').href;
				});
			});
		}
	
	},
	
	RowMarkCheckbox : function() {
		
		// If checkbox ticked marks the row as well
		var RowMarkCheckboxes = $$('.RowMarkCheckbox');
		RowMarkCheckboxes.each(function(Checkbox) {
		
			var CheckboxRow = Checkbox.getParent('tr');

			if(Checkbox.checked) {
				CheckboxRow.getElements('td').setStyle('background-color', '#E1E1E1');
			}
			
			Checkbox.addEvent('click', function() {
				if(Checkbox.checked) {
					CheckboxRow.getElements('td').setStyle('background-color', '#E1E1E1');
				} else {
					CheckboxRow.getElements('td').setStyle('background-color', '#FFFFFF');
				}
			});	
		});
	
	},
	
	SearchActivate : function() {
	
		/*IYA.GeoSelectImplement($('SEARCH_Country'), 
			'SEARCH_CountyID', 
			'SEARCH_County', 
			'SEARCH_County',
			true,
			(function() {
				if (($('SEARCH_Country').value=='2635167') && ($('SearchDistancecontainer'))) {
					$('SearchDistancecontainer').setStyle('display', 'block');
				}
			}),
			'All ');	*/
		
		/*var SearchCountry = $('SEARCH_Country');
		var uksearchbits = $('uksearchbits');
		var irelandsearchbits = $('irelandsearchbits');
		
		if(SearchCountry) {
			SearchCountry.addEvent('change', function() {
				if(SearchCountry.value=='2635167'){
					uksearchbits.setStyle('display', 'block');
				} else {
					uksearchbits.setStyle('display', 'none');			
				}
				if(SearchCountry.value=='2963597'){
					irelandsearchbits.setStyle('display', 'block');
				} else {
					irelandsearchbits.setStyle('display', 'none');			
				}
			});
		}
		*/
		
		var SearchCountry = $('SEARCH_Country');
		
		var searchSurname = $('search_surname');
		var searchPostcode = $('search_postcode');
		var searchIrishcounty = $('search_irishcounty');
		
		if(SearchCountry) {
			SearchCountry.addEvent('change', function() {
				if(SearchCountry.value=='2635167'){ // UK
					searchSurname.setStyle('display', 'block');
					searchPostcode.setStyle('display', 'block');
					searchIrishcounty.setStyle('display', 'none');
				} else if(SearchCountry.value=='2963597'){ // Irish
					searchSurname.setStyle('display', 'block');
					searchPostcode.setStyle('display', 'none');
					searchIrishcounty.setStyle('display', 'block');				
				} else {
					searchSurname.setStyle('display', 'block');
					searchPostcode.setStyle('display', 'none');
					searchIrishcounty.setStyle('display', 'none');
				}
			});
		}


	},
	
	disabled : function() {
	
		var disableditems = $$('.disabled');
		disableditems.each(function(item) {
			
			item.fade(0.3);
			item.setStyle('cursor', 'default');
			
			if (Browser.Engine.trident) {
				item.getElements('input').dispose();
				item.getElements('div[class=ButtonShadContain]').dispose();
			} else {
				item.getElements('input').addEvent('click', function(e) { this.blur(); }).setStyle('cursor', 'default');
			}
			item.getElements('select').addEvent('click', function(e) { this.blur(); }).setStyle('cursor', 'default');
			item.getElements('textarea').addEvent('click', function(e) { this.blur(); }).setStyle('cursor', 'default');
			item.getElements('a').addEvent('click', function(e) { e = new Event(e); e.stop();}).setStyle('cursor', 'default');
		
		}.bind(this));	
	
	},
	
	GalleryInit : function() {
	
		var GalleryLinks = $$('.gallerylist li a');
		
		GalleryLinks.each(function(Link) {
	
			Link.addEvent('click', function(e) {
			
				var Eventer = new Event(e);
				Eventer.stop();
				
				this.Showoverlaywindow(true, '<div style="width : 500px; height : 375px;"></div>', 500, 375, true);
			
			}.bind(this));
	
		}.bind(this));
	
	},
	
	windowbox : function() {
	
		// Opens links marked as windowbox into a lightbox type of frame
		var Links = $$('a.windowbox, a.windowboxlarge');

		Links.each(function(Link) {

			Link.addEvent('click', function(e) {

				var Eventer = new Event(e);
				Eventer.stop();

				//this.ShowShadowOverlay(true, false, 10, true);
				if(Link.getProperty('class') == 'windowbox') {
					this.Showoverlaywindow(true, '<iframe src="'+Link.href+'"></iframe>', 800, 500, true);
				} else if(Link.getProperty('class') == 'windowboxlarge') {
					this.Showoverlaywindow(true, '<iframe src="'+Link.href+'"></iframe>', 800, 500, true);
				} else {
					this.Showoverlaywindow(true, '<iframe src="'+Link.href+'"></iframe>', 800, 500, true);
				}
			
			}.bind(this));
		
		}.bind(this));
			
	},
	
	Showoverlaywindow : function(blnShowHide, strHTML, iWidth, iHeight, ClickCloseWin) {
	
		if (blnShowHide) {
	
			// Shwo the shadow overlay
			this.ShowShadowOverlay(true, false, ClickCloseWin);
			
			// Create the elements
			var objoverlaywindow = new Element('div', {
				'styles' : {	
					'position' : 'absolute',
					'display' : 'block',
					'width' : iWidth,
					'height' : iHeight,
					'top' : (((this.WindowHeight / 2) - (iHeight.toInt() / 2)) + this.ScrollAmountY),
					'left' : ((this.WindowWidth / 2) - (iWidth.toInt() / 2))
				},
				'class' : 'overlaywindow'
			});
			
			var objoverlaywindowclosetab = new Element('div', {
				'styles' : {	
					'position' : 'absolute',
					'display' : 'block',
					'top' : (((this.WindowHeight / 2) - (iHeight.toInt() / 2)) + this.ScrollAmountY + (iHeight + 16)),
					'left' : ((this.WindowWidth / 2) - (iWidth.toInt() / 2) + (iWidth - 78))
				},
				'class' : 'overlaywindowclosetab'
			});
			
			var objoverlaywindowInsider = new Element('div', { 'class' : 'inner' });
	
			objoverlaywindowInsider.injectInside(objoverlaywindow);
			objoverlaywindow.injectInside($$('body')[0]);
			objoverlaywindowclosetab.injectInside($$('body')[0]);
			
			objoverlaywindowclosetab.addEvent('click', function() {
				this.ShowShadowOverlay(false, null, null);
			}.bind(this));
						
			objoverlaywindowInsider.innerHTML = strHTML;
			
		} else {
		
			// Hides and removes the overlay window
			this.ShowShadowOverlay(false, null, null);
	
		}
	
	},
	
	ShowShadowOverlay : function(blnShowHide, Zindex, ClickClose) {
	
		if (blnShowHide) {
		
			this.GetDimensions();
		
			document.body.style.overflow='hidden';
			
			// Create element
			var objShadowOverlay = new Element('div', {
				'styles' : {	
					'background' : '#464646',
					'position' : 'absolute',
					'display' : 'block',
					'opacity' : 0.5,
					'width' : this.WindowWidth + 'px',
					'height' : this.PageHeight + 'px',
					'top' : this.ScrollAmountY,
					'left' : 0
				},
				'class' : 'ShadowOverlay'
			});
			
			objShadowOverlay.injectInside($$('body')[0]);

			if(ClickClose) {

				objShadowOverlay.addEvent('click', function() {
				
					var objShadowOverlayExisting = $$('.ShadowOverlay')[0];
					var objWindowOverlayExisting = $$('.overlaywindow')[0];
					var objWindowOverlayTabExisting = $$('.overlaywindowclosetab')[0];
					
					if (objShadowOverlayExisting) {
						objShadowOverlayExisting.dispose();
						document.body.style.overflowY='scroll';		
					}
					
					if (objWindowOverlayExisting) {
						objWindowOverlayExisting.dispose();
					}
					
					if (objWindowOverlayTabExisting) {
						objWindowOverlayTabExisting.dispose();
					}
					
				}.bind(this))
			}
	
		} else {
		
			// Hides and removes the overlay
			var objShadowOverlayExisting = $$('.ShadowOverlay')[0];
			var objWindowOverlayExisting = $$('.overlaywindow')[0];
			var objWindowOverlayTabExisting = $$('.overlaywindowclosetab')[0];
			
			if (objShadowOverlayExisting) {
				objShadowOverlayExisting.dispose();
				document.body.style.overflowY='scroll';		
			}

			if (objWindowOverlayExisting) {
				objWindowOverlayExisting.dispose();
			}
			
			if (objWindowOverlayTabExisting) {
				objWindowOverlayTabExisting.dispose();
			}
			
		}
	
	},
	
	passwordclear : function() {
		
		var passwordclears = $$('input.passwordclear');
		
		passwordclears.each(function(field) {
		
			var PasswordOverlay = new Element('div', {
				'styles' : {
					'height' : 16,
					'top' : field.getCoordinates().top + 4,
					'left' : field.getCoordinates().left + 5,
					'position' : 'absolute'
				}
			});
			PasswordOverlay.innerHTML ='Password';
			PasswordOverlay.injectInside($$('body')[0]);
			
			PasswordOverlay.addEvent('click', function() {
				PasswordOverlay.dispose();
				field.focus();
			});
			
			field.addEvent('focus', function() {
				if(PasswordOverlay) {
					PasswordOverlay.dispose();
				}
			});
			
			window.addEvent('resize', function() {
				PasswordOverlay.setStyle('top', (field.getCoordinates().top + 4));
				PasswordOverlay.setStyle('left', (field.getCoordinates().left + 5));
			});
			
			(function() {
				PasswordOverlay.setStyle('top', (field.getCoordinates().top + 4));
				PasswordOverlay.setStyle('left', (field.getCoordinates().left + 5));
			}).periodical(1000);
		
		});	
	
	
	},
	
	cleartext : function() {
	
		var cleartextfields = $$('.cleartext');
	
		cleartextfields.each(function(field) {
			
			var ParentForm = field.getParent("form");			
			var InitValue = field.value;
			field.addEvent('focus', function() {
				if (field.value==InitValue) {
					field.value='';
				}
			});	
			if (ParentForm) {
				ParentForm.addEvent('focus', function() {
					if (field.value==InitValue) {
						field.value='';
					}
				});	
			}
		
		
		});	
	
	},
	
	Buttons : function() {
	
		if ((!Browser.Engine.presto) && (!Browser.Engine.trident)) {
		
			var ShadowDistance = 3;
			
			var AjaxLoader = new Element('div', { 'styles' : {
				'background' : 'transparent url(img/ajax-loader.gif) no-repeat',
				'top' : 0,
				'left' : 0,
				'height' : 16,
				'width' : 16,
				'position' : 'absolute'
			}});
			
			var Buttons = $$('.Button, .butt');
			Buttons.each(function(Button) {
			
				var ButtonWidth = Button.getCoordinates().width;
				var ButtonHeight = Button.getCoordinates().height;
			
				// Create shadow and insert for image
				var ButtondefaultShadowContain = new Element('div', {
					'class' : 'ButtonShadContain', 
					'styles' : {
						'width' : ButtonWidth+ShadowDistance,
						'height' : ButtonHeight+ShadowDistance
					}
				});
				var ButtondefaultShadow = new Element('div', {
					'class' : 'ButtonShadContain', 
					'styles' : {
						'opacity' : 0.1,
						'width' : ButtonWidth,
						'height' : ButtonHeight,
						'backgroundcolor' : '#000000',
						'position' : 'absolute'
					}
				});
				
				ButtondefaultShadow.clone().setStyles({'margin-top' : ShadowDistance, 'margin-left' : ShadowDistance}).injectInside(ButtondefaultShadowContain);
				ButtondefaultShadow.clone().setStyles({'width' : ButtonWidth-2, 'height' : ButtonHeight-2, 'margin-top' : 1+ShadowDistance, 'margin-left' : 1+ShadowDistance }).injectInside(ButtondefaultShadowContain);
				ButtondefaultShadow.clone().setStyles({'width' : ButtonWidth-4, 'height' : ButtonHeight-4, 'margin-top' : 2+ShadowDistance, 'margin-left' : 2+ShadowDistance }).injectInside(ButtondefaultShadowContain);
				ButtondefaultShadow.clone().setStyles({'width' : ButtonWidth-6, 'height' : ButtonHeight-6, 'margin-top' : 3+ShadowDistance, 'margin-left' : 3+ShadowDistance }).injectInside(ButtondefaultShadowContain);
				ButtondefaultShadow.clone().setStyles({'width' : ButtonWidth-8, 'height' : ButtonHeight-8, 'margin-top' : 4+ShadowDistance, 'margin-left' : 4+ShadowDistance }).injectInside(ButtondefaultShadowContain);
				Button.clone().setStyle('position', 'absolute').injectInside(ButtondefaultShadowContain);
				ButtondefaultShadowContain.replaces(Button);
				
				var ClonedButton = ButtondefaultShadowContain.getElement('input');
	
				if (ClonedButton) {
							
					ClonedButton.addEvents({
						'click' : function(e) {
							
							//var Eve = new Event(e);
							//Eve.stop();
							
							//AjaxLoader.injectInside($$('body')[0]);
							//AjaxLoader.setStyle('top', ButtondefaultShadowContain.getCoordinates().top);
							//AjaxLoader.setStyle('left', ButtondefaultShadowContain.getCoordinates().left+ButtonWidth+10);
							
						
						},
						'mousedown' : function() {
						
							ClonedButton.setStyle('margin-top', ShadowDistance);
							ClonedButton.setStyle('margin-left', ShadowDistance);
						
						},
						'mouseup' : function() {
						
							ClonedButton.setStyle('margin-top', ShadowDistance-1);
							ClonedButton.setStyle('margin-left', ShadowDistance-1);
						
						},
						'mouseenter' : function() {
						
							ClonedButton.setStyle('margin-top', ShadowDistance-1);
							ClonedButton.setStyle('margin-left', ShadowDistance-1);
						
						},
						'mouseleave' : function() {
							
							ClonedButton.setStyle('margin-top', 0);
							ClonedButton.setStyle('margin-left', 0);				
						
						}
					});	
						
				} else {
				
					ClonedButton = ButtondefaultShadowContain.getElement('div');
				
					ClonedButton.addEvents({
						'click' : function(e) {
							
							
						
						},
						'mousedown' : function() {
						
							ClonedButton.setStyle('margin-top', ShadowDistance);
							ClonedButton.setStyle('margin-left', ShadowDistance);
						
						},
						'mouseup' : function() {
						
							ClonedButton.setStyle('margin-top', ShadowDistance-1);
							ClonedButton.setStyle('margin-left', ShadowDistance-1);
						
						},
						'mouseenter' : function() {
						
							ClonedButton.setStyle('margin-top', ShadowDistance-1);
							ClonedButton.setStyle('margin-left', ShadowDistance-1);
						
						},
						'mouseleave' : function() {
							
							ClonedButton.setStyle('margin-top', 0);
							ClonedButton.setStyle('margin-left', 0);				
						
						}
					});		
							
				}	
				
				if(Browser.Engine.webkit) {
					ClonedButton.setStyle('margin-top', ShadowDistance-1);
					ClonedButton.setStyle('margin-left', ShadowDistance-1);		
					ClonedButton.setStyle('margin-top', 0);
					ClonedButton.setStyle('margin-left', 0);	
				}	
			
			});	
			
		}
	
	},
	
	NoPaste : function() {
	
		var NoPastefields = $$('input.NoPaste');
		var lastCode = 0;
		
		NoPastefields.each(function(field){		
			field.addEvent("keydown", function(event) {  
				event = new Event(event);
				
				if ((lastCode==17) && (event.code == 86)) {
					event.stop();
				}
				
				if(event.code==17) {
					lastCode = 17;
				} else {
					lastCode = 0;
				}
			
			});
			
		});
	
	},
	
	integerOnly : function() {
	
		var NumberOnlyfields = $$('input.integer');
		NumberOnlyfields.each(function(field){		
			field.addEvent("keydown", function(event) {  
				event = new Event(event);
				if ((!(event.code>=48 && event.code <=57)) && (!(event.code>=96 && event.code <=105))) {
					if ((event.code!=8) && (event.code!=46) && (event.code!=37) && (event.code!=39) && (event.code!=9)) {
						event.stop();
					}
				}
				if ((event.code == 17) || (event.code == 86)) {
					event.stop();
				}
			});
			field.addEvent("keyup", function(event) {  
				if(field.value=='') { field.value=0; }
			});
			field.addEvent("blur", function(event) {  
				if(field.value=='') { field.value=0; }
			});
	
		});
	},
	
	NumbersOnly : function() {
	
		var NumberOnlyfields = $$('input.Numbers');
		NumberOnlyfields.each(function(field){		
			field.addEvent("keydown", function(event) {  
				event = new Event(event);
				if ((!(event.code>=48 && event.code <=57)) && (!(event.code>=96 && event.code <=105))) {
					if ((event.code!=8) && (event.code!=46) && (event.code!=37) && (event.code!=39) && (event.code!=9)) {
						event.stop();
					}
				}
				if ((event.code == 17) || (event.code == 86)) {
					event.stop();
				}
			});
	
		});
	},
		
	// **************************************************************************
	// Get page dimensions
	// **************************************************************************
	GetDimensions : function() {
	
		var xScroll, yScroll;
	
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		// all but Explorer Mac
		} else if (document.body.scrollHeight > document.body.offsetHeight){
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		// Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		} else {
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		// all except Explorer
		if (self.innerHeight) {
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		// Explorer 6 Strict Mode
		} else if (document.documentElement && document.documentElement.clientHeight) {
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		// other Explorers
		} else if (document.body) {
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
	
		this.WindowWidth = windowWidth;
		this.WindowHeight = windowHeight;
		this.PageHeight = pageHeight;
		this.PageWidth = pageWidth;
		
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {
			yScroll = document.body.scrollTop;
		}
	
		this.ScrollAmountY = yScroll;
	
	},
	
	DetectPlugins : function(PluginName) {
	
		//Acrobat
		//QuickTime
		//DivX
		//Joost
		//RealJukebox
		//RealPlayer
		//Flash
		//Google Update
		//iTunes
		//Google Earth
		//Lively
		//Silverlight
		//Java
	
		var PluginFound = false;
		
		//alert(navigator.plugins);
		
		if (navigator.plugins && navigator.plugins.length > 0) {
			for (pluginsArrayCounter=0; pluginsArrayCounter < navigator.plugins.length; pluginsArrayCounter++ ) {
				if(navigator.plugins[pluginsArrayCounter].name.contains(PluginName) >0) {
					PluginFound = true;
					break;				
				}			
			}
		}
		
		return PluginFound;
	
	}
	
});

var IYA = new IYAClass();
