/*
	NHS, Specialised Services
	Created @ rehabstudio.com
*/

// jQuery example plugin
(function(A){A.fn.example=function(E,C){var D=A.isFunction(E);var B=A.extend({},C,{example:E});return this.each(function(){var F=A(this);if(A.metadata){var G=A.extend({},A.fn.example.defaults,F.metadata(),B)}else{var G=A.extend({},A.fn.example.defaults,B)}if(!A.fn.example.boundClassNames[G.className]){A(window).unload(function(){A("."+G.className).val("")});A("form").submit(function(){A(this).find("."+G.className).val("")});A.fn.example.boundClassNames[G.className]=true}if(A.browser.msie&&!F.attr("defaultValue")&&(D||F.val()==G.example)){F.val("")}if(F.val()==""&&this!=document.activeElement){F.addClass(G.className);F.val(D?G.example.call(this):G.example)}F.focus(function(){if(A(this).is("."+G.className)){A(this).val("");A(this).removeClass(G.className)}});F.blur(function(){if(A(this).val()==""){A(this).addClass(G.className);A(this).val(D?G.example.call(this):G.example)}})})};A.fn.example.defaults={className:"example"};A.fn.example.boundClassNames=[]})(jQuery);

// jQuery onReady functions
$(document).ready(function(){
	
	// Scroller
	if($('div.scroll').length > 0) {
		$('.scroll').jScrollPane();
		$('div.jScrollPaneDrag').mousedown(function() { $(this).addClass('click'); });
		$('html').mouseup(function() { $('div.jScrollPaneDrag').removeClass('click'); });
	}
	
	// Top Menu
	$('ul.menudrop').each(function(index) {
		var element	= $(this).attr('id');
		var top		= $('ul#menu li.' + element.substr(5)).width();
		var item	= $(this).width();
		
		if(top > item) {
			$('ul#' + element +'.menudrop li a').css('width', top);
		} else {
			$('ul#' + element +'.menudrop li a').css('width', item);
		}
	});
	
	$("#menu li").hover(
		function () {
			$('ul.menudrop').hide();
			var el = $(this).attr('class');
			$('#menu-' + el).show();
		},
		function () {
			var el = $(this).attr('class');
			$('#menu-' + el + '.menudrop').hide();
		}
	);
	
	$("ul.menudrop").hover(
		function () {
			var el = $(this).attr('id');
			$('ul#' + el).show();
		},
		function () {
			var el = $(this).attr('id');
			$('ul#' + el + '.menudrop').hide();
		}
	);
	
	// Dropdown Items
	$('p#dropdown a').bind('click', function(){
		var element = $(this).attr('class');
		
		if(element == 'opened')
		{
			$('ul#dropdownlist').hide();
			$(this).removeClass('opened');
		} else {
			$(this).addClass('opened');
			$('ul#dropdownlist').show();
		}
	});
	
	$('ul#dropdownlist li a').bind('click', function(){
		var element = $(this).attr('rel');
		var el = $(this).html();
		
		$('form input#documentCategory').attr('value', element);
		$('div#dropdownmenu ul').hide();
		$('p#dropdown a').removeClass('opened').html(el);
		$('#DocumentIndexForm').submit();
	});
	
	// Image Gallery Show Image
	if($('div.carouselimage img').length > 0) {
		$('div.carousel img').bind('click', function(){
			var image	= $(this).attr('rel');
			var id		= $(this).attr('id');
			var item	= $(this).attr('class');
			
			// Remove Any No Download Class preset
			$('img#carouselimage').removeClass('nodownload');
			
			// Change Image
			$('img#carouselimage').attr('src', 'images/gallery/medium/' + image);
			
			// Download Link
			if(item == 'nodownload') {
				$('img#carouselimage').addClass('nodownload');
			} else {
				$('p#downloadlink a').attr('href', 'images/gallery/original/' + image);
			}
			
			window.location.hash = 'i' + id;
		});
	}
	
	// Video Gallery Show Video
	if($('div.carouselimage iframe').length > 0) {
		$('div.carousel img').bind('click', function(){
			var image = $(this).attr('rel');
			var id = $(this).attr('id');
			$('iframe#carouselplayer').attr('src', image);
			window.location.hash = 'vid' + id;
		});
	}
	
	// Image Gallery Controls
	if($('div.carousel').length > 0) {
		$('div.carousel').cycle({
			fx:      'scrollHorz',
			timeout:  0,
			pager:  '#carouseldots', 
			
			pagerAnchorBuilder: function(idx, slide) {
				return '#carouseldots a:eq(' + (idx) + ')';
			}
		});
		
		$('p#carouseldots a').bind('click', function(){
			var id	= $(this).attr('id');
			var foo	= parseInt(id.substr(4))
			
			$('div.carousel ul.current').removeClass('current');
			$('div.carousel ul#slide' + foo).addClass('current');
			$('p#carouseldots a.current').removeClass('current');
			$(this).addClass('current');
		});
		
		$('a.carouselcontrol').bind('click', function(){	
			var type	= $(this).attr('id');
			var id		= $('div.carousel ul.current').attr('id');
			
			if(type == 'goback') {
				var goto	= parseInt(id.substr(5)) - 1;
			} else {
				var goto	= parseInt(id.substr(5)) + 1;
			}
			
			$('p#carouseldots a#goto' + goto).trigger('click');
		});
	}
	
	// If Image is Downloadable
	if($('p#downloadlink').length > 0) {
		$('div.carouselimage').hover(
			function() {
				var status	= $('img#carouselimage').attr('class');
				if(status !== 'nodownload') {
					$('p#downloadlink').fadeIn();
				}
			},
			function() {
				$('p#downloadlink').fadeOut();
			}
		);
	}
	
	// Pre Load Images
	if($('div.carouselimage img').length > 0) {
		if(window.location.hash) {
  			var hash	= window.location.hash.substr(2,50);
			$('li img#' + hash).trigger('click');
  		}
  	}
  	
  	if($('div.carouselimage iframe').length > 0) {
		if(window.location.hash) {
  			var hash	= window.location.hash.substr(4,50);
			$('li img#' + hash).trigger('click');
  		}
  	}
	
	// Home Page Slideshow
	if($('div#slideshow').length > 0) {
		$('ul#menu').addClass('ie6bugfix'); // Fix simple IE6 Margin bug fix class
		$('div#slideshow').cycle({
			speed:		2000,
			timeout:	8000
		});
	}
	
	// Map Links
	if($('ul#mapLinks').length > 0) {
		$('ul#mapLinks li').hover(
			function() {
				var mapID	= $(this).attr('id');
				$('#' + mapID + ' span.mapback').fadeIn(100);
				$('#' + mapID + ' a.map').fadeIn(100);
			},
			function() {
				var mapID	= $(this).attr('id');
				$('#' + mapID + ' span.mapback').fadeOut(100);
				$('#' + mapID + ' a.map').fadeOut(100);
			}
		);
	}
	
	// Renewal
	if($('div#renewal').length > 0) {
		
		$('div#renewal div.questionblock').click(
			function() {
				var element	= $(this).attr('id');				
				var status	= $('div#' + element + ' div.hidden').attr('rel');				
				
				$('div.hidden').slideUp();
				$('div.hidden').attr('rel', 'closed');
				
				if(status !== 'opened') {
					
					$('div#' + element + ' div.hidden').slideDown();
					$('div#' + element + ' div.hidden').attr('rel', 'opened');
					
				}
			}
		);
	}
	
	// Example Calls
	if($('div#example-plugin-off').length == 0) {
		$('input#mainQuery').example('Enter your query here');
		$('input#serviceQuery').example('Enter service name here');
		$('input#newsQuery').example('Enter search query here');
		$("input#documentQuery").example('Enter key search word here');
		$('input#contactFirstName').example('Enter your first name here');
		$('input#contactLastName').example('Enter your last name here');
		$('input#contactEmail').example('Enter your email address here');
		$('input#contactOrganisation').example('Enter NHS organisation name here');
	}
	
	// Temporarily remove the Press Releases link from News & Media subnavigation
	//$('#menu-media a[href$=press_releases]').hide();

});

