// Init menu
var QueensClass = new Class({

	initialize: function(){
		
		window.addEvent("domready", function() {  
			this.searchBoxInit();
			this.menuInit();
			this.header_message();
			this.GalleriesInit();
			this.NumbersOnly();
			this.SearchTabsInit();
			this.galleryBoxedInit();
		}.bind(this));
		
		window.wysiwygFix=true;
		window.wysiwygFixFunc=(function() {
		
			Queens.galleryBoxedInit();
		
		});
		
	},
	
	galleryBoxedInit : function() {
		
		this.GetDimensions();
		
		var windowBoxedLinks = $$('.gallery_boxed');
		
		if (windowBoxedLinks) {
			
			windowBoxedLinks.each(function(Link) {
				Link.setProperty('target', '');
				
				var galleryImageHref=Link.href;
								
				if(Link.getFirst()) {
					
					Link.getFirst().addEvent('click', function(ev) {
					
						//alert(galleryImageHref);
					
						var eva = new Event(ev);
						eva.stop();
						
						// Get json for gallery image
						var jsonRequest = new Request.JSON({url: "/ajax/galleryitem", onComplete: function(media){
												
							var galleryHTML = ''+
								'<img src="'+galleryImageHref+'" width="'+media.gallerymedia.width+'" height="'+media.gallerymedia.height+'" />';

							var winHeight = media.gallerymedia.height;
							var winWidth = media.gallerymedia.width;
							
							if (media.gallerymedia.longdescription!=''){
								var galleryDescHTML = '<div class="gallery_description" style="height : 200px;"><div class="inner">'+media.gallerymedia.longdescription+'</div></div>';
								winHeight = winHeight + 200;
								
								if (winHeight+200 > this.WindowHeight) {
									winHeight = media.gallerymedia.height;
									winWidth = winWidth + 300;
									
									galleryDescHTML = '<div class="gallery_description" style="height : '+media.gallerymedia.height+'px; width : 300px; float : right;"><div class="inner">'+media.gallerymedia.longdescription+'</div></div>';
									
									galleryHTML = galleryDescHTML + galleryHTML;
								} else {
									
									galleryHTML = galleryHTML + galleryDescHTML;
								}
							}
														
							this.ShowShadowOverlay(true, true, 1000, true);
							this.ShowOverlayWindow('', true, galleryHTML, winWidth, winHeight, 1500);
						
						}.bind(this)}).get({'gallerymedia': Link.id.replace('gallery_item', '')});

						
						
					}.bind(this));		
				}
				
				//Link.fireEvent('click');
			}.bind(this));
		}
	},
	
	ShowShadowOverlay : function(blnShowHide, blnInjectTop, 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' : 'shadow_overlay'
			});
			
			if (Zindex != null) { objShadowOverlay.setStyle('z-index', Zindex); }
			
			// Inject element
			if (blnInjectTop) {
				objShadowOverlay.injectTop($$('body')[0]);
			} else {
				objShadowOverlay.injectInside($$('body')[0]);
			}
						
			if (ClickClose) {
				objShadowOverlay.addEvent('click', function() {
					objShadowOverlay.dispose();
					var objWindowOverlayExisting = $$('.overlay_window')[0];
					if (objWindowOverlayExisting) {
						objWindowOverlayExisting.dispose();
					}
					document.body.style.overflowY='scroll';		
				});			
			}			
	
		} else {
		
			// Hides and removes the overlay
			var objShadowOverlayExisting = $$('.shadow_overlay')[0];
			if (objShadowOverlayExisting) {
				objShadowOverlayExisting.dispose();
				document.body.style.overflowY='scroll';		
			}
		
			
		}
	
	},
	
	ShowOverlayWindow : function(WindowID, blnShowHide, strHTML, iWidth, iHeight, zIndex) {
	
		if (blnShowHide) {
	
			// Create the elements
			var objOverlayWindow = new Element('div', {
				'styles' : {	
					'position' : 'absolute',
					'display' : 'block',
					'width' : iWidth,
					'height' : iHeight,
					'opacity' : 1,
					'top' : (((this.WindowHeight / 2) - (iHeight.toInt() / 2)) + this.ScrollAmountY),
					'left' : ((this.WindowWidth / 2) - (iWidth.toInt() / 2))
				},
				'class' : 'overlay_window',
				'id' : WindowID
			});
			
			var objOverlayWindowInsider = new Element('div', { 'class' : 'Inner' });

			objOverlayWindowInsider.injectInside(objOverlayWindow);
			objOverlayWindow.injectInside($$('body')[0]);
			objOverlayWindowInsider.innerHTML = strHTML;
			if (zIndex != null) { objOverlayWindow.setStyle('z-index', zIndex); }
	
		} else {
		
			// Hides and removes the overlay window
			var objWindowOverlayExisting = $$('.OverlayWindow')[0];
			
			if (objWindowOverlayExisting) {
				
				this.ShowShadowOverlay(false, null, null);
				$j(objWindowOverlayExisting).fadeTo("fast", 0, function() {
					objWindowOverlayExisting.dispose();
				});
				
				
			}
		
		}
	
	},
	
	SearchTabsInit : function() {
	
		var searchPageTabs = $$('.search_pages li');
	
		if(searchPageTabs) {
			searchPageTabs.each(function(pageTab) {
				pageTab.addEvent('click', function() {
					window.location=pageTab.getElement('a').href;
				});	
			});
		}
	},
	
	NumbersOnly : 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();
					}
				}
			});
		});
	},
	
	header_message : function() {
	
		window.addEvent("domready", function() {
			var header_messageDv = $('header_message');
			var header_messageDv2 = $('header_message2');
			
			if (header_messageDv) {
				
				var CloseDiv = new Element('div', {
				    'styles': {
						'position': 'absolute',
						'top':0,
						'display':'none',
						'left':0,
						'cursor': 'pointer',
				        'width': '19px',
						'height': '18px',
						'background-image' : 'url(/mi-content/img/popup-close.gif)'
				    }
				});
				
				CloseDiv.inject(document.body, 'top');
				
				(function() {
					var FXMessage = new Fx.Tween(header_messageDv, {duration: 'long'});
					FXMessage.start('margin-top', 147, 147 - header_messageDv.getStyle('height').toInt());
				}).delay(1000);
				
				// Add a hide button to message if rolled over
				header_messageDv.addEvents({
					'mouseenter' : function() {
						CloseDiv.setStyles({
							'top': header_messageDv.getCoordinates().top,
							'left': header_messageDv.getCoordinates().left + header_messageDv.getCoordinates().width - 19,
							'display': 'block'
						});
					},
					'mousemove' : function() {
						CloseDiv.setStyles({
							'top': header_messageDv.getCoordinates().top
						});
					}
				});
				
				CloseDiv.addEvents({
					'click' : function() {
						CloseDiv.removeEvents();
						header_messageDv.removeEvents();
						CloseDiv.setStyles({ 'display': 'none' });
						var FXMessage = new Fx.Tween(header_messageDv, {duration: 'long'});
						FXMessage.start('margin-top', 147 - header_messageDv.getStyle('height').toInt(), 147);
					},
					'mouseenter' : function() {
						CloseDiv.setStyles({ 'display': 'block'	});
					},
					'mouseleave': function() {
						CloseDiv.setStyles({ 'display': 'none'	});
					}
				});
				
				header_messageDv.addEvent('mouseleave', function() {
					CloseDiv.setStyles({
						'display': 'none'	
					});
				});
			}

			if (header_messageDv2) {
				(function() {
					var FXMessage = new Fx.Tween(header_messageDv2, {duration: 'long'});
					FXMessage.start('margin-top', 147, 147 - header_messageDv2.getStyle('height').toInt());
				}).delay(1000);			
			}
		});
		
	},
	
	GalleriesInit : function() {
	
		var Galleries = $$('.GalleryList');
		var Galleryimages;
		var Gallerylink;
		
		Galleries.each(function(Gallery) {
			Galleryimages = Gallery.getElements('li');
			
			Galleryimages.each(function(Galleryimage) {
				
				Gallerylink = Galleryimage.getElements('a');
				Gallerylink.addEvent('click', function(e) {
					
					new Event(e).stop();
					
					this.ShowShadowOverlay(true, false, 2);
					
					var iHeight;
					var iWidth;
					
					// Create the gallery element
					var objGalleryWindow = new Element('div', {
						'styles' : {
							'position' : 'absolute',
							'display' : 'block',
							'z-index' : 3
						},
						'class' : 'GalleryWindow',
						'id' : 'GalleryWindow'
					});
					
					var objGalleryimage = new Element('img', {
						'src' : Gallerylink.getProperty('href')
					});
					
					objGalleryimage.injectInside(objGalleryWindow);
					objGalleryWindow.injectInside(document.body);
					
					iHeight = 400;
					iWidth = 500;

					objGalleryWindow.setStyle('height', iHeight);
					objGalleryWindow.setStyle('top', (((this.WindowHeight / 2) - (iHeight.toInt() / 2)) + this.ScrollAmountY));
					
					objGalleryWindow.setStyle('width', iWidth);
					objGalleryWindow.setStyle('left', ((this.WindowWidth / 2) - (iWidth.toInt() / 2)));
			
					$('ShadowOverlay').addEvent('click', function() {
						this.ShowShadowOverlay(false, null, null);	
						objGalleryWindow.dispose();
					}.bind(this));
					
					objGalleryimage.addEvent('click', function() {
						this.ShowShadowOverlay(false, null, null);	
						objGalleryWindow.dispose();
					}.bind(this));
				
				}.bind(this));
	
			}.bind(this));	
			
		}.bind(this));	
	
	},
	
	searchBoxInit : function() {
		
		var searchQueryBox = $('searchQuery');
		var InitValue = searchQueryBox.value;
	
		var searchbutton = $$('#search .button')[0];
		searchbutton = searchbutton.getElement('input[type=image]');
		
		searchQueryBox.addEvent('click', function() {
			if (searchQueryBox.value==InitValue) {
				searchQueryBox.value ='';
			}
		});
		
		searchbutton.addEvent('click', function() {
			if (searchQueryBox.value==InitValue) {
				searchQueryBox.value ='';
			}
		});
		
	},	
	
	menuInit : function() {
	
		var menucolors = new Hash({
			menuitemcolor1 : '#33863E',
			menuitemcolor2 : '#858F4C',
			menuitemcolor3 : '#A04C23',
			menuitemcolor4 : '#7BB041',
			menuitemcolor5 : '#8B211F',
			menuitemcolor6 : '#A4852E',
			menuitemOff : '#FFFFFF'
		});
				
		var menuitems = $$('.menu li');
		
		menuitems.each(function(menuitem, i) {

			if(menuitem.getProperty('class') != 'current') {
			
				menuitem.addEvents({
					'click' : function(){ 
						window.location = menuitem.getElement("a").getProperty('href');
					},
					'mouseover' : function(){ 
						var Newcolor = menucolors.get('menuitemcolor'+(i+1));
						menuitem.setStyle('background-color', Newcolor);
						menuitem.getElement("span").setStyle('color', '#ffffff');
					},
					'mouseout' : function(){ 
						var Newcolor = menucolors.get('menuitemOff');
						menuitem.setStyle('background-color', Newcolor);
						menuitem.getElement("span").setStyle('color', '#888A8E');
					}
				});

			}
		
		});
	
	
	},
	
	// **************************************************************************
	// 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;
	
	}

});

var Queens = new QueensClass();

