﻿//create a unique id
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };
var isPlaying = null;

var isLoopedRead = false;

var playingIcon = new Image();
playingIcon.src = 'images/playlist/speakerOn.gif';
var notPlayingIcon = new Image();
notPlayingIcon.src = 'images/playlist/speakerOff.gif';

//
//Flash / JS Communication
//
function jsReadComplete()
{
    stopRead();
    if (isPlaying != null)
    {
        var nextRead = null;
        
        // Find next item
        if (isLoopedRead)
        {
            nextRead = isPlaying;
            var tempCell = nextRead.parentNode.rowIndex + 1;

            while (tempCell < document.getElementById('itemsList').rows.length)
            {
                nextRead = document.getElementById('itemsList').rows[tempCell].cells[1];
                if (nextRead.getAttribute('p') == '1')
                    break;
                tempCell = nextRead.parentNode.rowIndex + 1;
                nextRead = null;
            }
            
            if (nextRead == isPlaying)
                nextRead = null;
        }
        
        isPlaying.getElementsByTagName('img').item(0).src = 'images/playlist/speakerOff.gif';
        isPlaying = null;
        
        if (isLoopedRead && nextRead != null)
            playFlash(nextRead);
    }
}

function toggleLooping()
{
    isLoopedRead = !isLoopedRead;
    
    if (isLoopedRead)
        document.getElementById('dpLoop').getElementsByTagName('img').item(0).src = 'images/loopOn.png';
    else
        document.getElementById('dpLoop').getElementsByTagName('img').item(0).src = 'images/loopOff.png';
}

var isPlaying = null;
var savedItem = null;

function playFlash(item)
{
    if (item == null)
    {
        item = savedItem;
        isPlaying = null;
    }
    
    // Change the old icon if necessary
    if (isPlaying != null)
    {
        isPlaying.getElementsByTagName('img').item(0).src = 'images/playlist/speakerOff.gif';
        stopRead();
        
        if (isPlaying == item)
        {
            isPlaying = null;
            return;
        }
        else
        {
            isPlaying = null;
            savedItem = item;
            window.setTimeout('playFlash()', 0);
            return;
        }
    }

    // Start the player
    isPlaying = item;
    positionFlash();
    var theURL = item.getAttribute('url');
    if (theURL == null || theURL == '')
        theURL = 'Audition/PlayAuditionItem.ashx?a=' + document.getElementById('auditionId').value + '&k=' + document.getElementById('auditionKey').value + '&i=' + item.getAttribute('aid');
    playRead(theURL);
    isPlaying.getElementsByTagName('img').item(0).src = 'images/playlist/speakerOn.gif';

    // Create a Zamtistic
    SendZamEvent(ZamtisticsEvent.PLAYED, item, '');
}

function positionFlash()
{
    if (isPlaying != null)
    {
        var flash = document.getElementById('flashPlayer');

        flash.style.top = (isPlaying.offsetTop + (isPlaying.offsetHeight - 26)/2) + 'px';
        flash.style.left = (isPlaying.offsetLeft + isPlaying.clientWidth) + 'px';
    }
}

function down(itemSeq, item) {

    var fileName = '';

    if (document.getElementById('infoTable') == null) {
        fileName = 'Audition/PlayAuditionItem.ashx?h=' + document.getElementById('auditionId').value;
    }
    else {
        if (itemSeq == null)
            fileName = 'Audition/PlayAuditionItem.ashx?a=' + document.getElementById('auditionId').value + '&k=' + document.getElementById('auditionKey').value;
        else
            fileName = 'Audition/PlayAuditionItem.ashx?a=' + document.getElementById('auditionId').value + '&k=' + document.getElementById('auditionKey').value + '&i=' + itemSeq;
    }

	// Create a Zamtistic if this is not a full playlist download
	if (item != null && itemSeq != null)
		SendZamEvent(ZamtisticsEvent.DOWNLOADED, item, '');

	document.getElementById('DownloadFrame').src = fileName;
}

function getURL(url)
{
	document.getElementById('DownloadFrame').src = url;
}

function del(itemSeq)
{
    document.getElementsByName('deleteItem')[0].value = itemSeq;
    // Call the server to delete item
    RefreshPage();
}

function ServerDone(result, context) {}
function ServerError(result, context) {}

function onRowSwitch(beginRow, endRow)
{
    if (endRow - beginRow == 1)
        return;

    document.getElementsByName('resequence')[0].value = beginRow + '-' + endRow;
    RefreshPage();
}

function linkItemList()
{
    var titles = document.getElementById('itemsList').getElementsByTagName('span');
    var count;
    
    for(count = 0; count < titles.length; count++)
        titles[count].onclick = new Function('showTalentDemo(this)');
}

function showTalentDemo(item) {
    // Stop the player before displaying the talent's demos
    if (isPlaying != null)
        playFlash(isPlaying);

    if (document.getElementById('infoTable') == null) {
        TALENT_CONTACT = false;
        setPortal('Search Hold List');
        setOwner(item.id);
    }
    else {
        setPortal('Playlist: ' + projectName);
        setOwner(ownerId);
        logOwnerEventOnly(true);
    }

    if (item.getAttribute('owner') != null)
        setOwner(item.getAttribute('owner'));

    portalPlayer(item.id, item.getAttribute('mt'));
}

function showSafHelp()
{
    window.openBrWindow('public/content/PlaylistIssue.html','SAFARIWARNING',400,280,false,true);
}

function showSafariWarning()
{
    if (navigator.userAgent.indexOf('Safari/') != -1)
    {
        // Detect version of Safari
        var version = parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf('Safari/') + 7));

        if (version < 400)
            document.write('<div class="safariWarning"> Unsupported browser - <a href="javascript:showSafHelp()" style="color:blue;">Click here</a> to learn more... </div>');
    }
}

function loadPicture(item)
{
	// Get the URL
	var theURL = item.getAttribute('url');
	if (theURL == null || theURL == '')
	    theURL = 'ShowPicture.aspx?a=' + document.getElementById('auditionId').value +
			'&i=' + item.getAttribute('aid') + '&k=' + escape(document.getElementById('auditionKey').value);

	// Open a window and load the picture
	SendZamEvent(ZamtisticsEvent.PLAYED, item);
	openBrWindow('ShowPicture.aspx?url=' + escape(theURL), 'URL', 400, 400, 1);
}

function SendZamEvent(zamType, readItem) {
    // Create an event for the player if this is not a preview
    if (ownerId != null) {
        // Get the pre-pend for the Zamtistic
        var prepend = '';
        if (readItem != null && readItem.parentNode != null)
            prepend = readItem.parentNode.getAttribute('prepend');

        if (prepend == null)
            prepend = '';

        // Find the name of the read without HTML markup
        var logString = '';
        if (readItem != null) {
            if (readItem.parentNode.cells[2].textContent)
                logString = prepend + readItem.parentNode.cells[2].textContent;
            else
                logString = prepend + readItem.parentNode.cells[2].innerText;
        }

        // Create an event for the player if this is not a preview
        var informationItem = document.getElementById('infoTable');
        if (informationItem != null) {
            zamEvent = new PlaylistEvent(zamType + (readItem != null ? ZamtisticsEvent.READ : ZamtisticsEvent.DEMO));

            zamEvent.CreateEvent(projectName, logString);

            zamEvent.AddEventTag('PLI', document.getElementById('auditionId').value);
            var prjInfo = document.getElementById('projectContact');
            if (prjInfo != null)
                zamEvent.AddEventTag('CNT', (prjInfo.textContent ? prjInfo.textContent : prjInfo.innerText));
            prjInfo = document.getElementById('projectClient');
            if (prjInfo != null)
                zamEvent.AddEventTag('CLT', (prjInfo.textContent ? prjInfo.textContent : prjInfo.innerText));
            zamEvent.AddOwner(ownerId);
            zamEvent.Send();
        }
    }
}
