var videoPlaying = false;
var galleryImageContainerHeight = 311;

// Using jquery get all a tags with class of termsConditionsLink and add an on click to popup window
function initGallery() {
    
    $('.eventGalleryList li a').click(function(e) {
        e.preventDefault();
        if(videoPlaying == true) {
            $('#eventVideo').remove();
            $('.eventImage').css({'display':'inline'});
            $('.eventGalleryPlayButton').animate({opacity: 1}, 300, "swing", function() {
                $('.eventGalleryPlayButton').css({'display':'inline'});    
            });
        }
        $('.eventImage').stop(true, true);
        element = $(this);
        $('.eventImage').animate({opacity: 0}, 300, "swing", function() {
            $('.eventImage img').attr('src', element.attr('href'))
            .load(function() {
               galleryImageHeight = $(this).height();
               if(galleryImageHeight < galleryImageContainerHeight) {
                   offset = (galleryImageContainerHeight - galleryImageHeight) / 2
                   $('.eventImageController').css({
                       'padding-top': offset+'px',
                       'padding-bottom': offset+'px'
                   });
                   
               } else {
                   $('.eventImageController').css({'padding': '0px'});
               }
               $('.eventImage').animate({opacity: 1}, 300, "swing"); 
            });
        });
    });
    
    $('.eventGalleryPlayButton img').click(function(e) {
        e.preventDefault();
        videoFile = $('.eventVideoFile').text();
        $('.eventImageContainer').append('<div id="sponsorVideo"></div>');
        playVideo(videoFile);
    });
    
    $('.sponsorGalleryPlayButton img').click(function(e) {
        e.preventDefault();
        videoId = $('.sponsorVideoId').html();
        //$('.sponsorImageContainer').append('<div id="sponsorVideo"></div>');
        playYoutubeVideo(videoId);
    });
}

function playVideo(videoUrl) {
    $('.eventGalleryPlayButton').animate({opacity: 0}, 300, "swing", function() {
        $('.eventGalleryPlayButton').css({'display':'none'});    
    });
    $('.eventImage').animate({opacity: 0}, 300, "swing", function() {
        $('.eventImage').css({'display':'none'});
        $('#sponsorVideo').css({
            'width': '470px',
            'height': '311px'
        });
        showVideo(videoUrl);
    });
    videoPlaying = true;
}

function showVideo(videoUrl) {
    splashImageUrl = $('.eventVideoSplashImage').html();
	if(youtubePlayer == true) {
    	var so = new SWFObject("swf/youtubePlayer.swf", "rotate", "470", "311", "8", "#FFFFFF");
    	so.addVariable("youTubeId", videoUrl);
    } else {
        var so = new SWFObject("swf/videoPlayer.swf", "rotate", "470", "311", "8", "#FFFFFF");
    	so.addVariable("videoUrl", '../uploads/'+videoUrl);
    }
    so.addVariable("splashImage", 'http://malcolm.flintinteractive.com.au/~anthonym/builds/flintinteractive-mfw-website_cms/'+'uploads/'+splashImageUrl);
	so.addVariable("width","470");
	so.addVariable("height","311");
	so.write("sponsorVideo");
}

function playYoutubeVideo(id) {
    $('.sponsorGalleryPlayButton').animate({opacity: 0}, 300, "swing", function() {
        $('.sponsorGalleryPlayButton').css({'display':'none'});    
    });
    $('.sponsorImage').animate({opacity: 0}, 300, "swing", function() {
        $('.sponsorImage').css({'display':'none'});
        $('#sponsorVideo').css({
            'width': '470px',
            'height': '250px'
        });
        $('#sponsorVideo').html('<object width="470" height="250"><param name="movie" value="http://www.youtube.com/v/'+id+'?fs=1&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+id+'?fs=1&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="470" height="250"></embed></object>');
        videoPlaying = true;
    });
}
