/* ($Id: corefunctionsRevised.js.build=603177 4 2011-04-19 22:49:30Z hale $) */
/* (Id: corefunctionsRevised.js 495 2011-03-23 12:21:46Z nixonm ) */
var $j = jQuery.noConflict();

stopAnim = false;
stopRotate = false;
processingFeatureChange = false;
sliderAnim = false;
sliderWidth = 710;
timer = 0;
expanded = false;

function featureNext() {

	if (!processingFeatureChange){
		
		processingFeatureChange = true;
	
		$j('#features ul li.last a').unbind('click',featureNext);


		//Only rotate headers if there's more than one:
		if ($jfeaturesHeader.length > 1){
			$jcurrentHeader = $jfeaturesHeader.eq(0);
			$jnextHeader = $jcurrentHeader.next();	
			$jcurrentHeader.hide();
			
			$jnextHeader.fadeIn('slow', function(){
				$jcurrentHeader.insertAfter($jfeaturesHeader.eq($jfeaturesHeader.length-1));
				$jfeaturesHeader = $j('#features h2');
			})
		}
		
		if ($jfeatures.length >1){
			$jcurrent = $jfeatures.eq(0);
			$jnext = $jcurrent.next();
			
			$jnext.fadeIn('slow',function(){
				$jcurrent.insertAfter($jfeatures.eq($jfeatures.length-1));
				$jcurrent.hide();
				$jfeatures = $j('#features span.sys_featureImage');
				$j('#features ul li.last a').bind('click',featureNext);
				processingFeatureChange = false;
			});
		}

	}
	return false;
}

function featurePrev() {

	if (!processingFeatureChange){
	
		processingFeatureChange = true;
		
		$j('#features ul li.first a').unbind('click',featurePrev);
		
		//Only rotate headers if there's more than one:
		if ($jfeaturesHeader.length > 1){
			$jcurrentHeader = $jfeaturesHeader.eq(0);
			$jnextHeader = $jfeaturesHeader.eq($jfeaturesHeader.length-1);	
			$jnextHeader.insertBefore($jcurrentHeader);
			$jcurrentHeader.hide();
			$jnextHeader.fadeIn('slow', function(){
				
				$jfeaturesHeader = $j('#features h2');
			})
		}
		
		if ($jfeatures.length > 1){
		
		    $jcurrent = $jfeatures.eq(0);
			$jnext = $jfeatures.eq($jfeatures.length-1);
			$jnext.fadeIn('slow',function(){
				$jnext.insertBefore($jcurrent);
				$jcurrent.hide();
				$jfeatures = $j('#features span.sys_featureImage');
				$j('#features ul li.first a').bind('click',featurePrev);
				processingFeatureChange = false;

			});
			
		}

	}

	return false;
}

/* Thanks to http://www.hardcode.nl/ for providing the following script */
function drawSocialLinks(oContainers){
    	var l,i,socialList = [], socialHtm='';
    	var t = $j('h1').eq(1).text();   /*  adjust this to select the title of your article */
    	var u = window.location.href;  /*  this selects the link to your article */
		var iconDirectory = '/Images/website/icons/'; /* this is the director containing your icons */
 
		var socialMedia = [
			{linkText: 'Del-icio-us', icon:'delicious32.png',href:'http://del.icio.us/post?url='+u+'&title='+t},
			{linkText: 'Stumbleupon', icon:'stumbleupon32.png',href:'http://www.stumbleupon.com/submit?url='+u},
			{linkText: 'Facebook', icon:'facebook32.png',href:'http://www.facebook.com/share.php?u='+u}, 
			{linkText: 'Digg', icon:'digg32.png',href:'http://digg.com/submit?phase=2&url='+u+'&title='+t},
			{linkText: 'Twitter', icon:'twitter32.png',href:'http://twitter.com/home?status='+u}, 
			{linkText: 'Google', icon:'google32.png',href:'http://www.google.com/bookmarks/mark?op=edit&bkmk='+u}
		];
 
		l = socialMedia.length;
		for (i=0; i<l;i++){
			socialList.push('<li><a href="'+socialMedia[i].href+'" title="'+socialMedia[i].linkText+'"><img width="21" height="21" src="'+iconDirectory+socialMedia[i].icon+'" alt="'+socialMedia[i].linkText+'" /></a></li>');
		}
		socialHtm = '<ul><li class="sys_first">Bookmark this</li>'+socialList.join("\n")+'</ul>';
		oContainers.append('<div class="socialMediaContainer">'+socialHtm+'</div>'); 
    	}


function toggleAll(){
	if ($j('div.sys_accordion div:hidden').length > 0) {
		$j('div.sys_accordion h2.sys_summary span').html('Open all sections');
	} else {
		$j('div.sys_accordion h2.sys_summary span').html('Close all sections');
	};
}


$j(document).ready(function(){

	// JPW Added functions start here //

    	drawSocialLinks($j('#bookmarks'));
    	
	//Contensis advert fade-away
    	$j('.sys_fadeAway a').animate({ opacity: 1.0 }, 3000).fadeOut('slow', function() {
    	    $j(this).remove()
    	});
    	
    	// JPW Added functions end here //
	
	//Set up the search input box
	$j('#search .sys_textbox').focus(function(){
		if(this.value == 'Search...')
			this.value = '';
	});
	$j('#search .sys_textbox').blur(function(){
		if(this.value == '')
			this.value = 'Search...';
	});
	
	$jfeaturesDiv = $j('#features');
	
	//Add navigation buttons with jQuery as they're no use without javascript
	//but only if there's more than one image
	if ($j("#features>span").length > 1){
		$j('#features').append(
			'<ul> 			<li class="sys_first"><a href="javascript:void(0)">Left</a><li> 			<li class="sys_last"><a 				href="javascript:void(0)">Right</a></li> 		</ul>');

		//Setup feature rotation
		$jfeaturesDiv.find('ul li.sys_last a').bind('click',featureNext);
		$jfeaturesDiv.find('ul li.sys_first a').bind('click',featurePrev);
		$jfeaturesDiv.mouseenter(function(){ //stop rotating images when mouse over feature area
			stopRotate=true;
		});
		$jfeaturesDiv.mouseleave(function(){
			stopRotate=false;
		});
		$jfeatures = $j('#features span.sys_featureImage');
		$jfeaturesHeader = $j('#features h2');
		
		//Setup the timer for feature rotation
		setTimeout('rotateFeature()', 8000);
	}
	
	$jglobalNav = $j('ul#globalNav');
	$jglobalNav.find('> li > div div').remove();
	$j('div#navWrap').prepend('<div id="overlay"></div>');
	$jglobalNav.find('> li').append('<div class="sys_overlay2"></div>');
	$jglobalNav.find('> li').append('<div class="sys_showPicture"></div>');
	$jshowPicture = $j('div.sys_showPicture');
	$joverlay = $j('#overlay');
	$joverlay2 = $j('div.sys_overlay2');
	$joverlay.hide();
	$joverlay2.hide();
	$jcontent = $j('#content');

	$jglobalNav.find('.sys_player > a, .player > div').each(function(){
		//loadVideo(this.id);
	});
	$jcontent.find('.sys_player > a, .player > div').each(function(){
		//loadVideo(this.id);
	});
	
	$jglobalNav.find('li').bind('click',function(){
		$joverlay.height($jcontent.outerHeight({margin:true})+$jfeaturesDiv.outerHeight({margin:true}));
		$joverlay.fadeIn('fast');
	});

	$jglobalNav.find('> li > ul').hide();
	
    	$jglobalNav.find('> li').bind('click', navOver);
    	
    	
    	
    	//jpw added to fix probs with no jscript menu
    	
	$jglobalNav.find('> li > ul').addClass('sys_origPos');
	
	/*
	$jglobalNav.find('ul.sys_grid > li').click(function(){
    	
    		return false;
    	});
    	*/
    	
    	$jglobalNav.find('ul > li').click(function(){
    	
    		return false;
    	});
    	
    	$jglobalNav.find('ul > li a').click(function(){ 
    	
    		if(!$j(this).hasClass('sys_expand')) { 
    			window.location = $j(this).attr("href"); 
    			return true;
		}
    	});
    	
			
	
    $jglobalNav.find('> li > ul').mouseenter(navIn);
    $jglobalNav.find('> li > ul').mouseleave(navOut);
    $jglobalNav.find('a.sys_expand').click(function() {
        expanded = true;
        $jelem = $j(this);
        $jnext = $jelem.next();
        width = $jnext.find('img').width();

        $jelem.parents('li').eq(1).find('div.sys_overlay2').show();
        $jshowPic = $jelem.parents('li').eq(1).find('div.sys_showPicture');
        $jshowPic.html($jnext.html());
        $jshowPic.css({ 'left': (945 / 2) - (width / 2) }); //centre the image

        $jshowPic.show('normal', function() {
            $jshowPic.find('p').css('width', ($j(this).width() - 22) + 'px');
        });
        $jshowPic.find('a.sys_collapse').click(collapse);
       
        return false;
    })

    $jglobalNav.find('a.sys_collapse').click(collapse);
    //Slider tabs
    $jsliderWrapper = $j('div#sliderWrapper');

    $j('div.sys_sliderContent').mouseleave(function() {
        $jelem = $j(this).parent();

        $jelem.stop().css({ 'z-index': 500 });
        sliderAnim = true;
        $jsliderWrapper.animate({ 'width': 45 });
        $jelem.animate({ 'right': 0 - sliderWidth }, function() {
            $jelem.css({ 'z-index': $jelem.data('zindex') });
            sliderAnim = false;
        });
        //$jsliderWrapper.animate({ 'width': 45 });
    });

    $j('a.sys_sliderButton').click(function() {
        if (sliderAnim == false) {
            $jelem = $j(this).parent();
            $jelem.data('zindex', $jelem.css('z-index'));
            $jelem.css({ 'z-index': 500 });
            sliderAnim = true;
            $jsliderWrapper.animate({ 'width': sliderWidth + 90 });
            $jelem.animate({ 'right': 0 }, function() {
                sliderAnim = false;
            });
            //$jsliderWrapper.animate({ 'width': 45 });
        }
     });
 
    
	
	//for accordions
	$j('div.sys_accordion h2').each(function() {
	var $jh2 = $j(this);

		if ($jh2.hasClass('sys_summary')) {
			$jh2.append('<span>Open all sections</span>').bind('click',function() {
				//for courses
				if ($j('div#details div.sys_accordion div.sys_squeezeBox:hidden').length > 0) {
					$j('div#details div.sys_accordion div.sys_squeezeBox:hidden').slideDown('fast');
					$j(this).find('span').html('Close all sections');
					$j('div#details div.sys_accordion h2:not(.sys_summary)').addClass('sys_collapse').removeClass('sys_expand');
					    
				} 
				//for staff
				else if ($j('div#sys_staff div.sys_accordion div.sys_squeezeBox:hidden').length > 0) {
					$j('div#sys_staff div.sys_accordion div.sys_squeezeBox:hidden').slideDown('fast');
					$j(this).find('span').html('Close all sections');
					$j('div#sys_staff div.sys_accordion h2:not(.sys_summary)').addClass('sys_collapse').removeClass('sys_expand');
					    
				}
				else {
					$j('div.sys_accordion div.sys_squeezeBox').slideUp('fast');
					$j(this).find('span').html('Open all sections');
					$j('div#details div.sys_accordion h2:not(.sys_summary)').removeClass('sys_collapse').addClass('sys_expand');
					$j('div#sys_staff div.sys_accordion h2:not(.sys_summary)').removeClass('sys_collapse').addClass('sys_expand');
				};
			});
		} else {
			$jh2.next('div').hide();
			$jh2.addClass('sys_expand').bind('click',function() {
				if ($j(this).hasClass('sys_expand')) {
					$j(this).addClass('sys_collapse').removeClass('sys_expand').next('div').slideDown('fast',toggleAll);
				} else {
					$j(this).removeClass('sys_collapse').addClass('sys_expand').next('div').slideUp('fast',toggleAll);
				};
			});
		};
	});

			
 	/* New tabs behaviour making full use of JQuery UI, scrollto and address plugins */

	/* moved to corefunctionsTabs */

});

function rotateFeature(){
	if(!stopRotate)
		featureNext();
	setTimeout('rotateFeature()',8000);
}

function trans1($jelem) {
	$jelem.fadeIn('fast',function(){
		$j(this).next().length && !stopAnim && trans1($j(this).next()); 
	});
}

function navOver() {
    $jglobalNav.find('li.sys_iehover').removeClass('sys_iehover');
    $jglobalNav.find('> li > ul').hide();
    stopAnim = false;
    //$jglobalNav.find('> li').unbind('mouseenter');
    $jelem = $j(this);
    $jelem.addClass('sys_iehover');
    $jelem.find('> ul').eq(1).show();
    $jelem.find('> ul').eq(1).find('> li').hide();
    $jelem.find('> ul').eq(0).fadeIn('fast', function() {
        if (!stopAnim) {
            $jitems = $jelem.find('> ul:last > li');
            trans1($jitems.eq(0));
        }
    });
}

function navIn() {
    clearTimeout(timer);
}

function navOut() {
    if (expanded == false) {
        $jelem = $j(this).parent();
        timer = setTimeout(function() {
            navHide($jelem);
        }, 500);
    }
}

function navHide($jelem) {
    stopAnim = true;
    // $globalNav.find('> li').bind('mouseenter', navOver);
    $juls = $jelem.find('> ul');
    $juls.fadeOut('slow', function() {
        $jelem.removeClass('sys_iehover');
    });
    $joverlay.fadeOut('slow');
    $joverlay2.fadeOut('slow');
    $jshowPicture.fadeOut('slow');
}

function collapse() {
    expanded = false;
    $joverlay2.hide();
    $jshowPicture.hide('normal');
    return false;
}


/* used to get rid of _blank so your code validates w3c stylee */
function setExternalLinks() {
  var el_list = document.getElementsByTagName('A');
  for (i=0; i<el_list.length; i++) {
    if (el_list[i].getAttribute('rel') == 'external') {
      el_list[i].setAttribute('target', '_blank');
    }
  }
}

/* Check's users version of Flash */
function checkFlash (version) {
	return DetectFlashVer(version, 0, 0);
}
