(function($){
	$(document).ready(function(){
		initFocusLabelShow();
		initAutocomplete();
						
		// target all browsers unless IE lt 7
		if( !jQuery.browser.msie || parseInt(jQuery.browser.version) >= 7 ){
			
			if($("#main").find(".side-map-holder")[0]){
				var $sideBarCon = $("#main").find(".side-map-holder");
				pinBlock($sideBarCon, 'side-map-holder-pinned');
			}
			
			if($("#main").find(".ad-2")[0]){
				var $sideBarCon = $("#main").find(".ad-2");
				pinBlock($sideBarCon, 'ad2-pinned');
			}						
		}				
				
		initPopupFunc();
		initLightBox();				
		
	});
	
	function pinBlock(pinElem, pinnedClass){					
		var $sideBar = pinElem;
		var sideBarOffset = $sideBar.offset();
		var sideBarOffsetTop = sideBarOffset.top;		
		
		var windowWidth = $(window).width();
		var mainWidth = $("#main").width();		
		
		// TMH: 24-05-2011: Only apply sticky banner if window size is greater than the content area.		
		if(windowWidth > mainWidth){						
			setInterval(function(){																	
				var scrollOffset = $(window).scrollTop();
				if(scrollOffset > sideBarOffsetTop){
					if(!$sideBar.hasClass(pinnedClass)){
						$sideBar.addClass(pinnedClass);
					}
				}else{
					if($sideBar.hasClass(pinnedClass)){
						$sideBar.removeClass(pinnedClass);
					}
				}			
			}, 150);					
		}
	}
	
	/*--- LightBox ---*/
	function initLightBox(){
		if($('#fader').length == 0) $('body').append('<div id="fader"></div>');
		var _fader = $('#fader');
		_fader.hide();
		var _popup = -1;
		
		$('a.with-popup').each(function(){
			var _el = this;
			if(_el.hash && _el.hash.length > 1){
				_el._popup = $(_el.hash);
				if(_el._popup.length){
					_el.onclick = function(){
						if(_el._popup){
							_el._popup.hide();
							_popup = _el._popup;
							showPopup();
						}
						return false;
					}
					_el._popup.find('.btn-close').click(function(){
						if($.browser.msie){
							_popup.hide();
							_fader.fadeOut(200);
							_popup = -1;
							if($.browser.version < 7) $('#wrapper select:not(".outtaHere")').css('visibility', 'visible');
						}
						else{
							_popup.fadeOut(400, function(){
								_fader.fadeOut(200);
								 _popup = -1;
							});
						}
						return false;
					});
				}
			}
		});
		_fader.click(function(){
			if(_popup != -1){
				if($.browser.msie){
					_popup.hide();
					_fader.fadeOut(200);
					_popup = -1;
					if($.browser.version < 7) $('#wrapper select:not(".outtaHere")').css('visibility', 'visible');
				}
				else{
					_popup.fadeOut(400, function(){
						_fader.fadeOut(200);
						_popup = -1;
					});
				}
			}
			return false;
		});
		$(document).keydown(function(e){
			if(!e)evt = window.event;
			if(e.keyCode == 27 && _popup != -1){
				if($.browser.msie){
					_popup.hide();
					_fader.fadeOut(200);
					_popup = -1;
					if($.browser.version < 7) $('#wrapper select:not(".outtaHere")').css('visibility', 'visible');
				}
				else{
					_popup.fadeOut(400, function(){
						_fader.fadeOut(200);
						_popup = -1;
					});
				}
			}
		});
		function showPopup(){
			_fader.css({
				opacity: 0,
				height: initH(),
				display:'block'
			});
			_popup.css({top: $(window).scrollTop()});
			if($.browser.msie && $.browser.version < 7) if($.browser.version < 7) $('#wrapper select:not(".outtaHere")').css('visibility', 'hidden');
			_fader.fadeTo(400, 0.5, function(){
				if(_popup != -1){
					if($.browser.msie) _popup.show();
					else _popup.fadeIn(200, function(){
						if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
					});
					_popup.css({top: $(window).scrollTop()+ ($(window).height() - _popup.outerHeight())/2});
					if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
				}
			});
		}
		function initH(){
			var _h = $('#wrapper').outerHeight();
			if(_h < $(window).height()) _h = $(window).height();
			if(_h < $('body').height()) _h = $('body').height();
			return _h;
		}
	}
	
	/* PopupFunc */
	function initPopupFunc(){
		var open_class = 'open';
		var popup_opener_link = $('.popup-opener-link');
		var popup_open = $(popup_opener_link.attr("rel"));
		if(!popup_open.hasClass(open_class)){
			popup_open.hide();
		}else{
			popup_open.show();
		}
		popup_opener_link.click(function(){
			popup_open.show();
			return false;
		});
		var popup_close_link = $('.popup-close-link');
		var popup_close = $(popup_close_link.attr("rel"));
		popup_close_link.click(function(){
			popup_close.hide();
			return false;
		});
		$(document).bind('mousedown', function(e){
			if(popup_open&& popup_open.is(':visible')){
				e = e || event;
				var t = e.target || e.srcElement;
				t = $(t);
				if(t.parents(popup_opener_link.attr("rel")).length == 0){
					popup_open.hide();
				}
			}
		});
	}
	
	/* FocusLabelShow(); */
	function initFocusLabelShow(){
		var holders = $('.search-form .cell');
		holders.each(function(){
			var holder = $(this);
			var input = holder.find('input:text:eq(0)');
			var label = holder.find('label:eq(0)');
			/*label.hide();*/
			input.focus(function(){
				label.show();
			}).blur(function(){
				/*label.hide();*/
			});
		});
	}
	
	/* Autocomplete(); */
	function initAutocomplete(){
		if(typeof($.fn.autocomplete)=='function'){
			var autocomplete_inputs = $('input.autocomlete');
			autocomplete_inputs.each(function(){
				var this_input = $(this);
				var this_inc = this_input.attr('title');
				this_input.removeAttr('title');
				if(this_inc.length){
					$.ajax({
						url: this_inc,
						dataType: "xml",
						success: function(xmlResponse) {
							var data = $("geoname", xmlResponse).map(function() {
								return {
									value: $("name", this).text() + ", " + ($.trim($("countryName", this).text()) || "(unknown country)"),
									id: $("geonameId", this).text()
								};
							}).get();
							this_input.autocomplete({
								source: data
							});
						}
					});
				}
			});
		}
	}
	
	/*
	var _init = $.ui.dialog.prototype._init;
		
	// Custom Dialog Init
	$.ui.dialog.prototype._init = function() {
		var self = this;
		_init.apply(this, arguments);		
		
		// Instantiate my custom stickyDialog() function
		var dialogOffset = 0;
		if(self.options.dialogOffset){
			dialogOffset = self.options.dialogOffset;
		}
		
		//self.stickyDialog(dialogOffset);				
	};
	
	// stickyDialog function
	$.extend($.ui.dialog.prototype, {				
		stickyDialog: function(offset){						
			// Cache the length in pixel from the scrollbar to the top
			var topScroll = $(window).scrollTop();
			
			// Cache dialog
			var dialogBox = this.uiDialog;
					
			// The initial dialog offset is set
			// if topScroll is greater than 0 add that value to the offset value and append 'px'.
			// if not, simply set css top property to the offset value.
			if(topScroll > 0){
				dialogBox.css('top', (offset+topScroll)+'px');	
			}
			else{
				dialogBox.css('top', (offset)+'px');
			}
		
			// Set didScroll to false
			var didScroll = false;
			$(window).scroll(function() {
				// Every time a scroll is done i set didScroll to true				
				didScroll = true;
			});

			setInterval(function() {
				if ( didScroll ) {
					// Set didScroll to false
					didScroll = false;
					// Set topScroll
					topScroll = $(window).scrollTop();
					// Set new top scroll
					topScroll = offset + topScroll;
					// Append 'px' to topScroll
					topScroll = topScroll + 'px';
					
					// Move the dialogBox to the new position
					dialogBox.animate({
						top: topScroll
						}, 300, 'easeInOutCirc'
					);					
				}
			}, 300);
		}
	});*/
	
	
})(jQuery);

