﻿function playMe(file, title, expert, descrip, excerpt, full, download, attachment, purchase, oneClick) {

    if (!file || file == undefined || file == null || file == '')
        return;

    //hidePlayer('divClose');
    // Determine file type

    var ext = file.substring(file.length - 4).toLowerCase();
    var playImg = '/Images/Player/Audio.png';
    var swf = '';
    var lov = '';
    switch (ext) {
        case '.flv':
            swf = '/media/playflv.swf';
            playImg = '/Images/Player/Video.png';
            lov = 'View';
            break;
        case '.mp3':
            swf = '/media/PlayMP3.swf'; // This is the image that shows up when playing audio files
            playImg = '/Images/Player/Audio.png';
            lov = 'Listen to';
            break;
    }

    if (title == undefined) {
        title = 'CopingUniversity.com Media';
    }

    // Create background div
    var divWash = document.getElementById('MPlayerDivBack');
    if (divWash == undefined || divWash == null) {
        divWash = document.createElement('div');
        divWash.id = 'MPlayerDivBack';
        divWash.style.position = 'absolute';
        divWash.style.top = 0;
        divWash.style.height = Screen.getDocumentHeight() + 20;
        document.body.appendChild(divWash);
    }

    // Create "floating" div
    var divFloat = document.getElementById('MPlayerDiv');
    if (divFloat == undefined || divFloat == null) {
        divFloat = document.createElement('div');
        divFloat.style.marginTop = (getScrollY() - 240).toString() + 'px';
        //divFloat.style.margin-top += getScrollY();
        divFloat.id = 'MPlayerDiv';
        document.body.appendChild(divFloat);
    }

    // Create Close div
    var divClose = document.getElementById('MPlayerDivClose');
    if (divClose == undefined || divClose == null) {
        divClose = document.createElement('div');
        divClose.id = 'MPlayerDivClose';
        divClose.onclick = hidePlayer;
        divClose.innerHTML = 'CLOSE PLAYER';
        divFloat.appendChild(divClose);
    }

    // Create player div
    var divPlayer = document.getElementById('MPlayerDivPlayer');
    if (divPlayer == undefined || divPlayer == null) {
        divPlayer = document.createElement('div');
        divPlayer.id = 'MPlayerDivPlayer';
        divFloat.appendChild(divPlayer);
    }

    // Create info div
    var divInfo = document.getElementById('MPlayerDivInfo');
    if (divInfo == undefined || divInfo == null) {
        divInfo = document.createElement('div');
        divInfo.id = 'MPlayerDivInfo';
        divInfo.innerHTML = '<font style="font-size: 1.25em; font-weight: bold;">' + title + ' with ' + expert + '</font><br/>' + descrip;
        var links = '';
        if (excerpt != undefined && excerpt != null && excerpt != '')
            links += buildPlayerLink('<img src="/Images/Player/Preview.png" alt="'+lov+' Excerpt" border="0" />', excerpt, title, expert, descrip, excerpt, full, download, attachment, purchase, oneClick);
        if (full != undefined && full != null && full != '')
            links += buildPlayerLink('<img src="' + playImg + '" alt="' + lov + ' Full Program" border="0" />', full, title, expert, descrip, excerpt, full, download, attachment, purchase, oneClick);
        if (download != undefined && download != null && download!='')
            links += buildDownloadLink(download);
        if (attachment != undefined && attachment != null && attachment!='')
            links += buildAttachmentLink(attachment);
        if (purchase != undefined && purchase != null && purchase != '')
            links += buildPurchaseLink(purchase);
        if (oneClick != undefined && oneClick != null && oneClick != '')
            links += buildOneClickLink(oneClick);
        if (links != '')
        	//divInfo.innerHTML += '<div class="LinksDiv" style="position: relative; top: 60px;">' + links + '</div>';
			divInfo.innerHTML += '<div class="LinksDiv" style="position: relative; top: 60px;"></div>';
        divFloat.appendChild(divInfo);
    }

    var flashvars = {};
    switch (ext) {
        case '.flv':
            flashvars.flvUrl = file;
            flashvars.flvTitle = title;
            break;
        case '.mp3':
            flashvars.mp3Url = file;
            flashvars.mp3Title = title;
            break;
    }
    var params = { 'allowfullscreen': 'true', 'allowscriptaccess': 'always', 'allownetworking': 'all' };
    var attributes = { 'id': 'MPlayerDivPlayer' };
    swfobject.embedSWF(swf, "MPlayerDivPlayer", "480", "360", "9.0.0", false, flashvars, params, attributes);



}

function buildPlayerLink(linktext, file, title, expert, descrip, excerpt, full, download, attachment, purchase, oneClick) {
    var link = "&nbsp;<a href=\"javascript:playMe(\'" + file + "\',\'" + title + "\',\'" + expert + "\',\'" + descrip + "\',\'" + excerpt + "\',\'" + full + "\',\'" + download + "\',\'" + attachment + "\',\'" + purchase + "\',\'" + oneClick + "\')\">" + linktext + "</a>";
    return link;
}

function buildDownloadLink(download) {
    //var link = "&nbsp;<a href=\"" + download + "\" onclick=\"alert('Right-click and choose Save as...');return false;\"><img src='/Images/Player/Download.png' alt='Download this program' /></a>";
    return ''; //link;
}

function buildAttachmentLink(attachment) {
    return "&nbsp;<a href=\"" + attachment + "\" target=\"_blank\"><img border=0 alt='Get Handouts' src='/Images/Player/Handouts.png'/ ></a>";
}

function buildPurchaseLink(purchase) {
    return "&nbsp;<a href=\"" + purchase + "\" target=\"_blank\"><img border=0 alt='Add to cart' src='/Images/Player/Cart.png' /></a>";
}

function buildOneClickLink(oneClick) {
    return "&nbsp;<a href=\"" + oneClick + "\" target=\"_blank\"><img border=0 alt='Purchase now' src='/Images/Player/Purchase.png' /></a>";
}

function hidePlayer() {
    var currentwindow = document.location.href ;
    // Ok, I know this stupid, BUT, can't get the getElementId in Firefox/Opera, had to do an ugly
    // workaround to backup the out of this JS/Flash window, then go back (forward) to the window that was just backed out of!
    if (document.all) {
        var div = document.getElementById('MPlayerDiv');
        if (div != undefined || div != null) {
            swfobject.embedSWF('/media/quiet.swf', 'MPlayerDivPlayer', '480', '360', '9.0.0', false, null, null, null);
            div.outerHTML = '';
        }
        div = document.getElementById('MPlayerDivBack');
        if (div != undefined || div != null) {
            div.outerHTML = '';
        }
    } else {
        //var x = document.getElementById('divFloat');
        //alert('Here');
        history.go(-1);
        window.location = currentwindow;
        //document.close();
    }
}

function getScrollY() {
    if (self.pageYOffset) // all except IE
        return self.pageYOffset;
    if (document.documentElement && document.documentElement.scrollTop)
        return document.documentElement.scrollTop;
    if (document.body)
        return document.body.scrollTop;
}

function forceRightClick(element) {
    alert('a');
    var evt = document.createEvent('MouseEvents');
    var RIGHT_CLICK_BUTTON_CODE = 2; // Right click event
    evt.initMouseEvent('contextmenu', true, true, element.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, RIGHT_CLICK_BUTTON_CODE, null);
    if (document.createEventObject) { // IE
        return element.fireEvent('onclick', evt);
    }
    else {
        return !element.dispatchEvent(evt);
    }
}
