var vp;
function playerReady(obj){
	vp = document.getElementById(obj['id']);
}

function calculateHeightWidth(theObject) {
	if(theObject.width == null || "undefined" == typeof(theObject.width))
		theObject.width = 640;
	if(theObject.height == null || "undefined" == typeof(theObject.height))
		theObject.height = 360;
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var aspectRatio = theObject.height / theObject.width;
	if(parseInt(theObject.width) > parseInt(windowWidth)) {
			 theObject.width = parseInt(windowWidth) - 24;
			 theObject.height = parseInt(theObject.width) * aspectRatio;
	} else if(parseInt(theObject.height) > parseInt(windowHeight)) {
			 theObject.height = parseInt(windowHeight) - 24;
			 theObject.width = parseInt(theObject.height) / aspectRatio;
	}
	return theObject;
}

function playVideo(playerEvent, theObject) {
	openPlayer(playerEvent, theObject);
	openPlayer("PLAY", "true");
}

function videoPlayer(playerEvent, theObject){
	if (typeof(theObject) == "object") {
				theObject = calculateHeightWidth(theObject);
				
        if ((vp == null || typeof(vp) == "undefined") || (navigator.userAgent.indexOf('Firefox') > -1 && playerParams.popup)) {
            createPlayer(theObject);
            if (playerParams.popup) 
                $(playerParams.modalWindow).css('width',(parseInt(theObject.width) + 24)).jqmShow();
        }
        else {
            if (playerParams.popup) 
                $(playerParams.modalWindow).css('width',(parseInt(theObject.width) + 24)).jqmShow();
            openPlayer(playerEvent, theObject);
        }
    }
    else {
        createPlaylistPlayer(theObject);
    }
}

function createPlaylistPlayer(theObject){
    var flashvars = {
        file: theObject,
        playlist: 'bottom',
		playlistsize: 200,
        backcolor: 'ffffff',
        logo: '/images/vdc_logo_sm.png',
        id: playerParams.placeholder,
        controlbar: 'bottom'
    }
    var params = {
        allowfullscreen: "true",
        allowscriptaccess: "always"
    }
    var attributes = {
        id: playerParams.placeholder,
        name: playerParams.placeholder
    }
    swfobject.embedSWF(playerParams.url, playerParams.placeholder, theObject.width, theObject.height, playerParams.version, false, flashvars, params, attributes);
}


function createPlayer(theObject){
	var flashvars = {
        streamer: theObject.streamer,
        file: theObject.file,
        id: playerParams.placeholder,
        subscribe: true,
        type: theObject.type,
        autostart: theObject.autostart,
        image: theObject.image,
        controlbar: 'bottom',
				displaytitle: true,
				icons: true
    }
    var params = {
        allowfullscreen: "true",
        allowscriptaccess: "always",
		wmode: "transparent"
    }
    var attributes = {
        id: playerParams.placeholder,
        name: playerParams.placeholder
    }
    swfobject.embedSWF(playerParams.url, playerParams.placeholder, theObject.width, theObject.height, playerParams.version, false, flashvars, params, attributes);
}

function createMultiPlayer(theObject, placeholder){
    var flashvars = {
        streamer: theObject.streamer,
        file: theObject.file,
        id: playerParams.placeholder,
        subscribe: true,
        type: theObject.type,
        autostart: theObject.autostart,
        image: theObject.image,
        controlbar: 'bottom',
		displaytitle: true
    }
    var params = {
        allowfullscreen: "true",
        allowscriptaccess: "always"
    }
    var attributes = {
        id: placeholder,
        name: placeholder
    }
    swfobject.embedSWF(playerParams.url, placeholder, theObject.width, theObject.height, playerParams.version, false, flashvars, params, attributes);
}

function stopPlayer(){
    vp.sendEvent('STOP');
}

function openPlayer(playerEvent, theObject){
	$(vp).css('width', parseInt(theObject.width));
	$(vp).css('height', parseInt(theObject.height));
	vp.sendEvent(playerEvent, theObject);
}

