
$(document).ready(function() {
	
	
	createPlayer();
	$('#GalleryModal').center();
	
	$('.button').hover(function() {
		$(this).addClass('button_on');
	}, function() {
		$(this).removeClass('button_on');
	});
	
	$('.thumb').live('click', function(e) {
		e.preventDefault();
		currIdx = getGalleryIndex($(this).attr('href'));
		$('#GalleryModal').css({opacity:0.01});
		$('#GalleryModal').css({width:300, height:80, marginTop:-40, marginLeft:-150});
		$('#GalleryModal').jqmShow();
		setGalleryPhoto();		
	});
	
	getGallery();
	
	$('#GalleryModal').jqm();
	
	$('#GalleryLeft').click(function() {
		currIdx = (currIdx - 1 + images.length) % images.length;
		setGalleryPhoto();
	});
	
	$('#GalleryRight').click(function() {
		currIdx = (currIdx + 1) % images.length;
		setGalleryPhoto();
	});
	
	$('.media-button').hover(function() {
		$(this).find('.button-arrow').show();
	}, function() {
		$(this).find('.button-arrow').hide();
	});


	
});

var flashplayer;

var cache = [];
var images = [];
var videos = [];
var currIdx = 0;
var currVideoIdx = 0;

var player = null;

function setGalleryPhoto(){

	$('#Loading').show();
	$('#GalleryCurrent').html('<strong>PHOTO ' + (currIdx + 1) + ' OF ' + images.length + '</strong>');
	$('#ImageContainer').css({opacity:0.01});
	$('#ImageContainer').css({width:null, height:null});
	$('#ImageContainer').html('<img src="' + images[currIdx] + '" />');
	$(document).everyTime(100, function() {
	    // alert($('#ImageContainer img').width());
		if($('#ImageContainer img').width() > 0) {
			$('#GalleryModal').css({opacity:1});
			$(document).stopTime();
			$('#ImageContainer').hide();
			$('#ImageContainer').css({opacity:1});
			var w = $('#ImageContainer').width() > 300 ? $('#ImageContainer').width() : 300;
			var h = $('#ImageContainer').height() + 33;
			var ml =  w / 2 * -1;
			var mt =  ((h / 2) * -1) - 15;
			$('#GalleryModal').animate({width:w, marginLeft:ml}, function() {
				$('#GalleryModal').animate({height:h, marginTop: mt}, function() {
					$('#Loading').hide();
					$('#GalleryModal').center();
					$('#ImageContainer').fadeIn();
				});
			});
		}
	});
}

function getGalleryIndex(src) {
	var idx = 0;
	for(var i = 0; i < images.length; i++) {
		if (src.indexOf(images[i]) > -1) {
			idx = i;
			break;
		}
	}
	return idx;
}

function loadGallery(xml) {
	
	features = $(xml).find('image');
	
	var gallery = $('#GalleryContainer');
	
	var idx = 0;
	var HTML = '';
	
	features.each(function() {
		images[images.length] = $(this).attr('src');
		HTML += '<a href="' + $(this).attr('src') + '" class="thumb"><img src="' + $(this).attr('thumb') + '" alt="" /></a>';
	});
	
	gallery.html(HTML);
	
}

function getGallery() {
	var link = $('#GalleryData').attr('href');
	$('#GalleryData').hide();
	if (!cache[link]) {
		$.ajax({
	    	type: "GET",
	        url: link,
	        dataType: "xml",
	        success: function(xml) {
				cache[link] = xml;
				loadGallery(xml);
			 }
		});
	} else {
		loadGallery(cache[link]);
	}
}

function playVideo(idx){
	player.sendEvent('ITEM', idx)
}


function loadMedia(xml) {
	
	features = $(xml).find('video');
	
	var idx = 0;
	var count = 0;
	
	var HTML = '<ul>';
	features.each(function() {
		videos[videos.length] = {
			file: $(this).attr('src'),
			runtime: $(this).attr('runtime'),
			title: $(this).attr('title'),
			link: $(this).attr('link')
		};
		HTML += '<li><a href="javascript:playVideo(' + count + ');" class="video-button" id="Video' + count + '"> </a></li>';
		count++;
	});
	HTML += '</ul>';
	$('#MediaNavMenu').html(HTML);
	
	if (count > 0) {

		player.sendEvent('LOAD', videos);
		setVideoIndex(0);
	}
	
}

function getMedia() {
	var link = $('#MediaData').attr('href');
	$('#MediaData').hide();
	if (!cache[link]) {
		$.ajax({
	    	type: "GET",
	        url: link,
	        dataType: "xml",
	        success: function(xml) {
				cache[link] = xml;
				loadMedia(xml);
			 }
		});
	} else {
		loadMedia(cache[link]);
	}
}

function createPlayer() {
    var flashvars = { }
    var params = {
            allowfullscreen:"true", 
            allowscriptaccess:"always",
			wmode:'transparent'
    }
    var attributes = {
            id:"player1",  
            name:"player1"
    }
    swfobject.embedSWF("js/player.swf", "PlayerContainer", "320", "266", "9.0.115", false, flashvars, params, attributes);
}
function playlistUpdate(obj) {
	currVideoIdx = obj.index;
	setVideoIndex(obj.index);
}

function setVideoIndex(idx) {
	$('#MediaTitle').html(videos[idx].title);
	$('#RunTime').html('<strong>RUNTIME ' + videos[idx].runtime + '</strong>');
	$('#MediaLink').attr('href', videos[idx].link);
	$('.video-button').removeClass('on');
	$('#Video' + idx).addClass('on');
}

function playerReady(obj) {
	player = document.getElementById(obj['id']);
	player.addControllerListener('ITEM', "playlistUpdate");
	getMedia();
};
