// JavaScript Document

function loadFlash(divContainer, Source, VideoWidth, VideoHeight, AutoPlay, Volume, ShowCloseButton, redirectURL, previewMode, StillImage, BufferTime, Position, PositionDistance, PlayAgain) {
    //First Draw the HTML
    drawHTML(divContainer, VideoWidth, VideoHeight, Position, PositionDistance);

    //Set and check for cookie if Play Again Enabled
    if (PlayAgain == "true") {
        if (getCookie(divContainer) == "1") {
            AutoPlay = "false";
        }
        setCookie(divContainer, "1", "30");
    }
	
	//this are variables manipulated by the the swf file
	var flashvars = {};											//IMPORTANT NOTE: values are case sensitive
	flashvars.swfSource = Source;								//path of the flv file
	flashvars.swfAutoPlay = AutoPlay;							//disable/enable autoplay possible values: true, false
	flashvars.swfAlign = "bottomRight";							//alignment of the movie file with respect to the size of swf file
	flashvars.swfIsLive = "false";								//if video if live							
	flashvars.swfScaleMode = "maintainAspectRatio";							//possible values: noScale, maintainAspectRatio, exactFit
	flashvars.swfVolume = Volume;																
	flashvars.swfSkin = "none";									//skin: replace text with 'none' to remove skin
	flashvars.swfSkinAutoHide = "false";				    	//possible values: true, false
	flashvars.swfSkinBackgroundAlpha = "1";		                //possible range of values(decimal fraction): 0.0 - 1.0
	flashvars.swfSkinBackgroundColor = "0x000000";		        //hexadecimal values		
	//additional variables
	flashvars.swfShowCloseButton = ShowCloseButton;
	flashvars.redirectURL = redirectURL; 					    // this will not be invoked is empty
	flashvars.swfPreviewMode = previewMode;                     // enable or disable preview starting method values: true, false
	flashvars.swfStillImage = "/portals/0/MayITalk-2.png";                       // full path to still image for replay and may i talk
	flashvars.swfBufferTime = BufferTime;                       // float value for amount of video to buffer
	
	//parameters for the swf file
	var params = {};
	params.menu = "false";
	params.quality = "high";
	params.wmode = "transparent"; 						// remove it if skinBackgroundColor is enabled
	params.allowScriptAccess = "always";                // this allows scripts to execute
	
	//html attribute of the swf file
	var attributes = {};
	attributes.id = "myDynamicFlash";                   //ID of the flash installed, must stay 'myDynamicFlash' for close button
	attributes.name = "myDynamicFlash";
	
	//SWF content information
	var swfFile = "http://hairmax.com/portals/0/flvplayer.swf";                      //Path to the swf file to play videos
	var div = divContainer;
	var swfWidth = VideoWidth;
	var swfHeight = VideoHeight;
	var flashPlayerVersion = "9.0.0";
	
	swfobject.embedSWF(swfFile, div, swfWidth, swfHeight, flashPlayerVersion,"http://hairmax.com/portals/0/expressInstall.swf", flashvars, params, attributes);
}

// This is the function for close button
function removeFlash( swfFile ) {
	swfobject.removeSWF( swfFile );
}

// This block is to display errors during testing
function displayFlashMessage( msg ) {
	alert( msg );	
}

// This block is just for testing
function setAndLoadFlash( frm ){
	divContainer		=frm.swfDivContainer.value;
	Source				=frm.swfSource.value;
	AutoPlay			=frm.swfAutoPlay.value;
	Volume				=frm.swfVolume.value;
	ShowCloseButton		=frm.swfShowCloseButton.value;
	redirectURL = frm.redirectURL.value;
	previewMode = frm.previewMode.value;
	StillImage = frm.stillImage.value;
	VideoWidth = frm.swfWidth.value;
	VideoHeight = frm.swfHeight.value;
	BufferTime = frm.swfBuffer.value;
				
	loadFlash(divContainer, Source, VideoWidth, VideoHeight, AutoPlay, Volume, ShowCloseButton, redirectURL, previewMode, StillImage, BufferTime);
}

// This function draws corret HTML on page for alignment
function drawHTML(divContainer, VideoWidth, VideoHeight, Position, PositionDistance) {
    var myDivTag = "No Function Fired";

    if (Position == "BottomLeft") {
        myDivTag = "<div style=\"position: fixed; bottom: 0; left: " + PositionDistance + "px; \">" +
                                 "<div id=\"" + divContainer + "\">" +
                                 "</div>" +
                             "</div>";
    }

    if (Position == "BottomRight") {  
        myDivTag = "<div style=\"position: fixed; float: right; height: 350px; right:0px; bottom:" + PositionDistance + "px; \">" +
                                 "<div id=\"" + divContainer + "\">" +
                                 "</div>" +
                             "</div>";
    }

    if (Position == "Floating") {
        myDivTag = "<div>" +
                        "<div id=\"" + divContainer + "\">" +
                         "</div>" +
                        "</div>";
    }

    document.write(myDivTag);

}

// Sets cookie for play again
function setCookie(cookieName, cookieValue, nDays) {
    var today = new Date();
    var expire = new Date();
    if (nDays == null || nDays == 0) nDays = 1;
    expire.setTime(today.getTime() + 3600000 * 24 * nDays);
    document.cookie = cookieName + "=" + escape(cookieValue)
                 + ";expires=" + expire.toGMTString();
}

// Gets cookie to check for play again
function getCookie(Name) {
    var search = Name + "="
    if (document.cookie.length > 0) { // if there are any cookies
        offset = document.cookie.indexOf(search)
        if (offset != -1) { // if cookie exists 
            offset += search.length
            // set index of beginning of value
            end = document.cookie.indexOf(";", offset)
            // set index of end of cookie value
            if (end == -1)
                end = document.cookie.length
            return unescape(document.cookie.substring(offset, end))
        }
    }
}




