//To hook in on page load
$(document).ready( function () {
						
	searchClear();
	directoryToggle();
	olReplace();
	
	if ($('.tabs').length != 0)
		$('.tabs').tabs();
		
	if ($('#carousel').length != 0)
		imageCarousel();
	
	$('#header').append('<div id="mwButton">'+
				'<a href="/"><span>Visit<br />the media wall<br />to see our students\' work</span><img src="/images/UCA_Recruitment/button_view.gif" alt="View"></a>'+
				'<img src="/images/UCA_Recruitment/mw_hover.png" class="hoverImg" />'+
			'</div>');
	
	
	if ($('#naviBar').length != 0 ) {
		mediaButtonOffset = $('#naviBar').outerHeight() + 195;
		$('#mwButton').css('top', mediaButtonOffset+'px');
		$('#logoHolder .subTitle').hide();
	} else
		mediaButtonOffset = false;
	
	/* Media wall setup */
	$(window).load(function() {
		//randomly positions images with different offsets for different movements each time
		$('div#mediaWall div.mwBox').each(function() {
			randomX = 840 + Math.floor(Math.random()*201);
			randomY = 1000 + Math.floor(Math.random()*101);
			$(this).width(randomX);
			$(this).height(randomY);
			
			//set heights and width of the images themselves
			
			//check the height and width of the images
			currentImg = $(this).find('img');
			currentImgDimensions = new Array(currentImg.width(), currentImg.height());
									
			indexImg = $('div#mediaWall div.mwBox').index($(this));
			
			if ((indexImg%2) == 0 || indexImg > 22)
				randomiser = (Math.floor(Math.random()*50)) + 140;
			else
				randomiser = (Math.floor(Math.random()*50)) + 190;
						
			if (currentImgDimensions[0] > currentImgDimensions[1]) {
				percentageDiff = (randomiser/currentImgDimensions[0]) * 100;
				newDimensions = new Array(randomiser, Math.floor((currentImgDimensions[1]/100)*percentageDiff));
			} else {
				percentageDiff = (randomiser/currentImgDimensions[1]) * 100;
				newDimensions = new Array(Math.floor((currentImgDimensions[0]/100)*percentageDiff), randomiser);
			}
			
			currentImg.width(newDimensions[0]);
			currentImg.height(newDimensions[1]);
			
			//$(this).find('a').append('<img src="images/UCA_Recruitment/mediaWall/mw_bgBlack.gif" class="bg" />').end().find('img.bg').height($(this).find('img').height()).width($(this).find('img').width()).css('opacity', 0.4);
		});
		
		$('div#mwOverlay').css('opacity', '0.4');
		$('#mwButton .hoverImg').css('opacity', '0');
		$('div#main').css('opacity', '1');
			
		//hover tooltip
		$('div#mediaWall div.mwBox a').hover(
		function() {
			if (parallaxOn)
				$(this).children('img.bg').stop().animate({'opacity':'0'}, 'fast');
		},
		function() {
			if (parallaxOn)
				$(this).children('img.bg').stop().animate({'opacity':'0.4'}, 'fast');
		});
		
		jQuery('#mediaWall').jparallax({mouseActiveOutside: true, mouseport: jQuery('body')}); // initiates media wall
		
		//On button click - checks if media wall is on or off
		//controls the image movement and opacity setting of the screen
		$('#mwButton a').click(function(event) {
			if (parallaxOn == true) {
				parallaxOn = false;
				$('div#main, div#naviBar').show();
				$('div#mwOverlay').stop().css('display','block').animate({ 'opacity': '0.4' });
				$('div#main, div#naviBar').stop().animate({ 'opacity': '1' });
				$(this).html('<span>Visit<br />the media wall<br />to see our students\' work</span><img src="/images/UCA_Recruitment/button_view.gif" alt="View">');
				
				if (mediaButtonOffset) {
					$('#logoHolder .subTitle').fadeOut();
					$(this).parent().animate({'top': mediaButtonOffset+'px'}, 'slow');
				}
			} else {
				parallaxOn = true;
				
				$('div#mwOverlay').stop().animate({ 'opacity': '0' }, function() { $(this).css('display','none' )});
				$('div#main, div#naviBar').stop().animate({ 'opacity': '0' }, function() { 
					$('div#main, div#naviBar').hide();
				} );
				$(this).html('<span>Back to<br />main site</span><img src="/images/UCA_Recruitment/button_back.gif" alt="View">');
				$(this).siblings('.hoverImg').stop().animate({ 'opacity': '0' }, function() { $(this).hide(); } );
				if (mediaButtonOffset) {
					$('#logoHolder .subTitle').fadeIn();
					$(this).parent().animate({'top': '254px'}, 'slow');
				}
			}
			event.preventDefault();
	  	});
		
		$('#mwButton a').hover(function() {
			if (parallaxOn == false)
				$(this).siblings('.hoverImg').stop().show().animate({ 'opacity': '1' });
		},
		function() {
			$(this).siblings('.hoverImg').stop().animate({ 'opacity': '0' }, function() { $(this).hide(); } );
		});
		
		parallaxOn = false;
	});
});


// Clears search text onFocus, and adds back in if empty onBlur
function searchClear() {
	$('#searchText').focus(function() {
		if ($(this).attr('value').toLowerCase() == 'search this site')
			$(this).attr('value', '');
	});
	$('#searchText').blur(function() {
		if ($(this).attr('value') == '')
			$(this).attr('value', 'Search this site');
	});
}

//Directory box functionality
function directoryToggle() {
	$('#directoryPanel h3 a').click(function(event){
		event.preventDefault();
		$(this).parent().siblings('div').slideToggle();
		$(this).parent().parent('div').toggleClass('closed')
	});
}
//Image replaces ol numbers with images functionality
function olReplace() {
	oLists = $('ol');
	
	for (i=0; i<oLists.length; i++) {
		currentList = oLists.eq(i);
		startId = currentList.attr('start');
		if (startId == '-1')
			startId = 0;
		else
			startId--;
		
		currentList.css({'list-style': 'none outside'}).children('li').css('padding-left', '22px');
		
		currentList.find('li').each(function() {
			listItem = $(currentList.find('li')).index($(this));
			$(this).prepend('<div class="listItem'+(listItem+startId+1)+'"></div>');
		});
	}
}

//Image carousel on course page
function imageCarousel() {
	var currentAd = 0;
	
	function fadeBanner() {
		if (currentAd < $('#carousel div.bannerSlide').length - 1) {
			currentAd++;
		} else {
			currentAd = 0;
		}
		goToBanner(currentAd);
	}
	
	function goToBanner(bannerNum) {
		$currentBanner = $('#carousel div.bannerSlide:visible');
		$currentBanner.css('z-index', '1');
		
		if(jQuery.browser.msie) {
			$('#carousel div.bannerSlide').eq(bannerNum).css('z-index', '2').show();
			$currentBanner.hide();
		} else {
			$('#carousel div.bannerSlide').eq(bannerNum).css('z-index', '2').fadeIn('slow', function () {
				$currentBanner.fadeOut('fast');
			});
		}
	}
	
	function animating(element) {
		if ( element.parent('li').hasClass('active') || $('#carousel div.bannerSlide:animated').length > 0 )
			return true;
		else
			return false
	}
	
	$('div.hide').removeClass('hide').fadeOut('fast');
	
	$('#carousel .bannerLeft').click(function(event) {
		if ( !animating( $(this) ) ) {	
			if ((currentAd-1) < 0) {
				currentAd = $('#carousel div').length - 1;
			} else {
				currentAd--;
			}
			goToBanner(currentAd);
		}
		event.preventDefault();
	});
	$('#carousel .bannerRight').click(function(event) {
		if ( !animating( $(this) ) ) {
			fadeBanner();
		}
		event.preventDefault();
	});

}

/* 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');
    }
  }
}

/* Gets URL params and returns as assoc array */
function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
