/* 	
	JV Player - Simple jQuery Video Player
	Copyright ©2009 Russell McVeigh
*/

// Configurable Settings

// Div where video is to be placed - default = "jv_player"
var div = "jv_player";
// Width of player - default = "320px"
var width = "320px";
// Height of player - default = "240px"
var height = "240px";
// Background colour of player div - default = "#EEEEEE"
var bgcolour = "#EEEEEE";
// Border colour of player div - default = "#333333"
var bordercolour = "#CCCCCC";
// Border width of player div - default = "1px" (use 0 for off)
var borderwidth = "1px";
// Placeholder image for player div - default = "thumbs/bg.gif"
var bgimage = "thumbs/bg.gif";


// NO NEED TO EDIT BEYOND THIS POINT!

// Location of the Flash player - the video links must be *relative* to this location!
// default = "jv.swf"
var swf = "jv_player.swf";

// jQuery ready function
$(document).ready(function () {
							
// Initialise the player
initPlayer();

// Click handlers for our load video function
$(".jv_loader").click(function () {
	var url = $(this).attr('href');
	var image = $(this).attr('rel');
	// If it has autoplay class
	var autoplay = "false";
	if ($(this).hasClass('autoplay')){
		autoplay = "true";
	}
	loadVideo(div, url, image, autoplay, swf, width, height);
	return false;
});

// Click handlers to unload the video (and re-init)
$(".jv_unloader").click(function () {
	var what = $(this).attr('rel');
	$("#"+what).fadeOut("slow", function(){ 
	if (isObject(what)) {
        replaceSwfWithEmptyDiv(what);
		initPlayer();
    }
	return false;
	});
});

});


function initPlayer(){
	// Add the wrapper div
	$("#"+div).wrap("<div id='jv_player_wrapper'></div>");
	// Size the wrapper so we don't get a flickering DOM
	$("#jv_player_wrapper")
	.css("width",width)
	.css("height",height)
	.css("background-color",bgcolour)
	.css("border","solid "+borderwidth+" "+bordercolour)
	.css("background-image","url("+bgimage+")")
	// Size/style the placeholder
	$("#"+div)
	.css("width",width)
	.css("height",height);
}

// Function to load the video
function loadVideo(targetID, url, image, autoplay, swf, width, height) {
    //Check for existing SWF
    if (isObject(targetID)) {
        //replace object/element with a new div
    	replaceSwfWithEmptyDiv(targetID);     
    }
    // vars
    var flashvars = {
        videoLink: url,
        autoplay: autoplay,
        defaultVideo: "1",
        image: image
    };
    // This method *creates* the object and allows runtime switching - it's more verbose as it uses the obj2query function
    var att = {
        data: swf,
        width: width,
        height: height,
		allowFullScreen: "true",
        menu: "false",
		wmode: "transparent"
    };
	// obj2query here
    var par = {
        flashvars: obj2query(flashvars)
    };
    var myObject = swfobject.createSWF(att, par, targetID);
	$("#"+targetID).fadeOut(0, function(){
	$(this).fadeIn("slow");
	});
    //
}

// Function to decide whether the element is an object or an embed
function isObject(targetID) {
    var isFound = false;
    var el = document.getElementById(targetID);
    if (el && (el.nodeName === "OBJECT" || el.nodeName === "EMBED")) {
        isFound = true;
    }
    return isFound;
}

// Function to empty and replace the flash div
function replaceSwfWithEmptyDiv(targetID) {
    var el = document.getElementById(targetID);
    if (el) {
        var div = document.createElement("div");
        el.parentNode.insertBefore(div, el);
        // Remove the SWF
        swfobject.removeSWF(targetID);
        // Give the new DIV the old element's ID
        div.setAttribute("id", targetID);
    }
}

// Function to convert flashvars to the right format
function obj2query(obj, forPHP, parentObject) {
    if (typeof obj != 'object') return '';

    if (arguments.length == 1) forPHP = /\.php$/.test(document.location.href);

    var rv = '';
    for (var prop in obj) if (obj.hasOwnProperty(prop)) {

        var qname = parentObject ? parentObject + '.' + prop : prop;

        // Expand Arrays
        if (obj[prop] instanceof Array) for (var i = 0; i < obj[prop].length; i++) if (typeof obj[prop][i] == 'object') rv += '&' + obj2query(obj[prop][i], forPHP, qname);
        else rv += '&' + encodeURIComponent(qname) + (forPHP ? '[]' : '') + '=' + encodeURIComponent(obj[prop][i]);

        // Expand Dates
        else if (obj[prop] instanceof Date) rv += '&' + encodeURIComponent(qname) + '=' + obj[prop].getTime();

        // Expand Objects
        else if (obj[prop] instanceof Object)
        // If they're String() or Number() etc
        if (obj.toString && obj.toString !== Object.prototype.toString) rv += '&' + encodeURIComponent(qname) + '=' + encodeURIComponent(obj[prop].toString());
        // Otherwise, we want the raw properties
        else rv += '&' + obj2query(obj[prop], forPHP, qname);

        // Output non-object
        else rv += '&' + encodeURIComponent(qname) + '=' + encodeURIComponent(obj[prop]);

    }
    return rv.replace(/^&/, '');
}

