jQuery(document).ready(function($){
	// Main navigation
	if ($("ul.main-nav").length) {
		$("ul.main-nav").superfish({
			hoverClass: "sfHover",
			pathClass: "",
			delay: 800,
			animation: {
				opacity: "show"
			},
			speed: "fast"
		}).find(">li:has(ul)")
		.mouseover(function(){
			$("ul", this).bgIframe({opacity:false});
		})
		.find("a")
			.focus(function(){
				$("ul", $(".nav>li:has(ul)")).bgIframe({opacity:false});
			});
	}
	
	/* Main search box default 'Search' value show/hide */	
	if ($('#main-searchbox')) {
		$('#main-searchbox input#SearchForm_SearchForm_Search').bind('click', function(e){
			if (e.target.value == 'Search') 
				e.target.value = '';
		}).bind('blur', function(e){
			if (e.target.value == '') 
				e.target.value = 'Search';
		});
		;
	}
	
	 /* Stock feed */
	if ($('#stock-feed').length) {
		$.ajax({
			type: 'GET',
			url: 'home/GetStockFeed',
			dataType: 'xml',
			cache: false,
			success: function(xml){
				// (fields in the feed: exchange, symbol, price,
				// change, date, time, lastclose)
				
				// Create a string of interesting fields
				var output = '<span id="feed-intro">Current share price:</span>' +
				'<br/>' +
				'<span id="feed-price"> ' +
				$('price', xml).text() +
				'p</span>' +
				', ' +
				'<span id="feed-change">change: ' +
				$('change', xml).text() +
				'</span>';
				// Insert the string into html
				$('#stock-feed').empty().append(output);
			},
			error: function(xml){
				$('#stock-feed').empty().append('Share info not available');
			}
		});
	}
	   
	 /* Modal disclaimer */

	if ($('#modal-disclaimer').length) {
		$('#modal-disclaimer').jqm({
			modal: true, /* FORCE FOCUS */
			overlay: 80, /* 0-100 (int) : 0 is off/transparent, 100 is opaque */
			onShow: function(hash){
				// 'Yes', I acknowledge clicked
				$('#modal-disclaimer #disclaimer-yes').click(function(e){
					e.preventDefault();
					$('#modal-disclaimer').addClass('mouse-busy');
					$.ajax({
						type: 'GET',
						url: 'home/AcceptDisclaimer?sid=' + $.cookie('PHPSESSID'),
						cache: false,
						success: function(){
							$('#modal-disclaimer').removeClass('mouse-busy');
							// Hide the modal window
							$('#modal-disclaimer').jqmHide()
						},
						error: function(){
							$('#modal-disclaimer').removeClass('mouse-busy');
							alert('Couldn\'t contact the server, please try again.');
						}
					});
				});
				// 'No', I decline clicked
				$('#modal-disclaimer #disclaimer-no').click(function(e){
					e.preventDefault();
					$.ajax({
						type: 'GET',
						url: 'home/DeclineDisclaimer?sid=' + $.cookie('PHPSESSID'),
						cache: false,
						success: function(){
							$('#modal-disclaimer').removeClass('mouse-busy');
							$('#modal-disclaimer input').attr('disabled', true);
							// Redirect to the previous page
							alert('This page requires that you acknowledge the disclaimer.\nYou will be redirected back.');
							window.location.href = (document.referrer != '') ? document.referrer : 'home';
						},
						error: function(){
							$('#modal-disclaimer').removeClass('mouse-busy');
							alert('Couldn\'t contact the server, please try again.');
						}
					});
					
				});
				// This prevents main window from scrolling
				// while scrolling content in the disclaimer
				$('html').addClass('overflow-disabled');
				$('body').addClass('overflow-disabled'); // needed for ie6
				// Show the modal window
				hash.w.show();
			},
			onHide: function(hash) {
				// This brings back normal scrollbars
				$('html').removeClass('overflow-disabled');
				$('body').removeClass('overflow-disabled'); // needed for ie6
				hash.w.hide();
				hash.o.remove();
			}
		});
		
		// Show the window ass soon as possible
		$('#modal-disclaimer').jqmShow();
	}

});