var basepath = 'http://ywamsunshinecoast.com/wp-content/themes/ywam/';

// Stuff for end of video.
var player    =  null;
var playlist  =  null;

function playerReady(obj) {
	player = gid(obj.id);
    addListeners();
}

function addListeners() {
	playlist = player.getPlaylist();
	if(playlist.length > 0) {
    	player.addModelListener('STATE', 'stateHandler');
    }
    else {
    	setTimeout("addListeners();", 100);
    }
}

// Check state
function stateHandler(obj) {
	if(obj.newstate == 'COMPLETED') {
		closeVideo();
    }
};

function gid(name) {
	return document.getElementById(name);
};

// playing the video.
function playVideo() {
	var s1 = new SWFObject(basepath + "player.swf","ply","400","300","9");
	s1.addParam("allowscriptaccess","always");
	s1.addParam("allowfullscreen","true");
	s1.addParam('wmode', 'transparent','true');
	s1.addParam("flashvars","file=" + basepath + "flv/part" + videoID + ".flv&controlbar=false&icons=false&screenalpha=0&stretching=exactfit&bufferlength=3&autostart=true");
	s1.write("main-video-inner");
}

function closeVideo() {
	$('#main-video').fadeOut(500, function() {
		$(this).remove();
	});
}

function videoSetup() {
	$('#main-video').fadeIn(300,function() {
		playVideo();
	})
}

$(document).ready(function() {
	if($.cookie('ignoreVideo' + videoID) != videoID) {
		var pv = setTimeout("videoSetup()", 1000);
	}
	
	$('a#video-close').click(function() {
    	$.cookie('ignoreVideo' + videoID, videoID, { expires: 10, path: '/' });
		closeVideo(videoID);	
		return false;
	});
});
