jQuery(function($){
	$('a[class^=img]').click(function(){
		return false;
	});
    $('.abstract').before('<a href="#" class="abstractlink">Read abstract&hellip;</a>').hide();
    $('.abstractlink').click(function(){
    	var link = this;
		if ($(this).next(':visible').length) {
			$(this).next().slideUp('slow', function(){
				$(link).text('Read abstract');
			});
		} else {
			$(this).next().slideDown('slow', function(){
				$(link).text('Hide abstract');
			});
		}
		return false;
	});
	if ($("a[href$='mp3']").length) {
		$.getScript("http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js", function(){
			$("a[href$='.mp3']").each(function(i){
		        var mp3_file = $(this).attr("href");
		        var player_ID = "mp3_player"+i;
		        $(this).parent('p').before('<div id="'+player_ID+'"></div>');
				var flashParams = {wmode:"opaque"};
		    	var flashVars = {playerID:player_ID,soundFile:mp3_file};
				var flashAttributes = {name:player_ID,style:"outline: none"};
				var playerURL = "/assets/flash/player.swf";
                swfobject.embedSWF(playerURL, player_ID, "100%", "24", "7", false, flashVars, flashParams, flashAttributes);
			});
		});
	}
	if ($('.cgd-gallery-video').length) {
		$.getScript("http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js", function(){
		    $('.cgd-gallery-video a').click(function(){
			    var imgsrc = $('img', this).attr('src');
			    var vidKey = imgsrc.substr((imgsrc.lastIndexOf("/")+1));
			    embedVideo(vidKey);
		    });
		    var firstImgSrc = $('.cgd-gallery-video a:first img').attr('src');
		    var vidKey = firstImgSrc.substr((firstImgSrc.lastIndexOf("/")+1));
		    embedVideo(vidKey);	
		});
	}
	function embedVideo(key)
	{
	    var flashVars = {};
		var flashParams = {allowFullScreen:true,allowScriptAccess:'sameDomain'};
		var flashAttributes = {};
		var playerURL = "http://lutube.leeds.ac.uk/video/"+key;
	    swfobject.embedSWF(playerURL, "video_player", "368", "246", "7", false, flashVars, flashParams, flashAttributes);
	}
    if ($('.slideshow').length) {
    	$('.slideshow').each(function(idx){
        	/* container for slides */
        	var $c = $(this);
        	var cid = $c.attr("id");
    	    /* check to see if there is more than one slide */
    	    if (slides[cid] && slides[cid].length > 1) {
    		    /* time between slides */
    		    var interval = 10000;
    		    /* duration of transition */
    		    var transition = 500;
    		    /* set some data on the container */
    		    $.data($c[0], 'slideData', {'currentSlide':0});
    		    /* set width and height of container */
    		    var img = $('img:first', $c);
    		    $c.css({'width':img.attr("width")+'px','height':img.attr("height")+'px','float':'none','clear':'left'});
    		    /* position image */
    		    img.css({'position':'absolute','left':0,'top':0});
    		    /* run slideshow */
    		    setTimeout(function(){go(cid, interval, transition);}, interval);
    	    }
    	});
	}
    function go(cid, interval, transition) {
    	$c = $("#"+cid);
    	var s = slides[cid];
    	var cur = $.data($c[0], 'slideData').currentSlide;
    	var nxt = ((cur + 1) >= s.length)? 0: (cur + 1);
    	var img = new Image();
    	/* load next image and add to the slideshow */
    	img.onload = function() {
    		$('<img src="'+s[nxt].imgsrc+'" />').css({'opacity':0,'position':'absolute','left':0,'top':0}).appendTo($c);
    		$('img:last', $c).animate({'opacity':1}, transition, function(){
    			$('img:first', $c).remove();
    			if ($.browser.msie) {
    				this.style.removeAttribute('filter');
    			}
    			$.data($c[0], 'slideData', {'currentSlide':nxt});
    		    setTimeout(function(){go(cid, interval, transition);}, interval);
    		});
    		$('.caption', $c).fadeOut((transition / 2), function(){
    			$('p', this).html(s[nxt].caption);
    			$(this).fadeIn((transition / 2));
    		});
    	}
    	img.src = s[nxt].imgsrc;
    }
	if ($('.lutube-video-wrap').length) {
		var vid = document.createElement('video');
		if (vid) {
			/* browser supports video media element */
		    $('video').each(function(){
		    	/* get the video mime types */
			    var sourceTypes = [];
			    if ($('source', this).length) {
			        $('source', this).each(function(){
			    	    sourceTypes.push($(this).attr("type"));
			        });
			    } else {
			    	/* single sources - h.264 baseline or extended */
			    	if ($(this).attr("src").indexOf(".m4v") != -1) {
			    		/* baseline profile */
			    		sourceTypes.push('video/mp4; codecs="avc1.42E01E, mp4a.40.2"');
			    	} else {
			    		/*extended profile */
			    		sourceTypes.push('video/mp4; codecs="avc1.64001E, mp4a.40.2"');
			    	}
			    }
		        var canPlay = false;
			    for (i = 0; i < sourceTypes.length; i++) {
			    	if (vid.canPlayType(sourceTypes[i]) === "probably") {
			    		canPlay = true;
			    	}
			    }
			    if (!canPlay) {
			    	var flashObject = $('object', this).clone();
			    	$(this).replaceWith(flashObject);
			    }
		    });
		} else {
			/* browser doesn't support video media element - should fall back to flash/image */
			
		}
	}
});

