// JS hide style
document.write('<style type="text/css">');
document.write('.js-hide { display:none }');
document.write('</style>');

var fadeInItems;

$(document).ready(function() {
		// randomise main background image on homepage
		var hpImages = new Array('home_img1.jpg',
														 'home_img2.jpg',
														 'home_img3.jpg');
		var rand = Math.floor(Math.random()*hpImages.length);
														 
		$('body.home #wrap-content #content').attr('style', 'background:url(/images/'+hpImages[rand]+') no-repeat 50% 50%');
		
		// fading in items on home page
		fadeInItems = $('.module-home-main .module-content li');
		
		if (fadeInItems[0]) {
			fadeInItems[fadeInItems.length] = $('.module-home-main .module-content .rel-links');
			setTimeout('itemFadeIn(0)', 3000);
		}
		
		//set lightbox links 
		$('a.thickbox').each(function(){
			var aHref = $(this).attr('href');
			if (aHref.indexOf('?')==0) {
				aHref = '#TB_inline'+aHref.substr(aHref.indexOf('?'), aHref.indexOf('#'));
				$(this).attr('href', 'http://'+window.location.hostname+window.location.pathname+aHref);
			}
		});
		
    //accordion
    $('.accordion h2').remove();
    
    $('.accordion').accordion({
        autoHeight: false,
        active: false,
        active: 'h3.show-first',
        selectedClass: 'accordion-on',
        collapsible: true
    });
    
    $('.accordion-change').each(function() {
    	var aHref = $(this).attr('href');
    	aHref = aHref.substr((aHref.indexOf('#')+1));
    	$(this).click(function() {
				$('.accordion h3 a').each(function(i) {
					var parent = this.parentNode;
					if (aHref==$(this).attr('id')) {
						if ($(parent).hasClass('ui-state-active')) {
							return false;
						}
						$( '.accordion' ).accordion('activate', i );
						
					}
				});
			});
    });
    
    var aLink = location.hash;
    aLink = aLink.substr((aLink.indexOf('#')+1));
    
    $('.accordion h3 a').each(function(i) {
			var parent = this.parentNode;
			if (aLink && aLink==$(this).attr('id')) {
				if ($(parent).hasClass('ui-state-active')) {
					return false;
				}
				$( '.accordion' ).accordion('activate', i );
			}
		});
		
		// basic search box validation 
		$('#nav-top .search').submit(function() {
			
			var textVal = ($(this).find('.field-text').attr('value'));
			if (!textVal) {
				alert('Please enter a value in the search field');
				return false;
			}
		})
		
    //open in a new window
    $('a.new-win').attr('target','_blank');

});
		
	function itemFadeIn(i) {
		$(fadeInItems[i]).fadeIn(500);
		
		if ($(fadeInItems[i+1])[0]) {
			setTimeout('itemFadeIn('+(i+1)+')', 1000);
		}			
	}



	
	
	
	
	
