﻿
//this variable is cleared in the SampleHolder
//whenever a new batch of samples are rendered
var currentAudio = "";

function playHTML5MP3Audio(ref)
{
	
	//first stop the current sample from playing
	
	if (currentAudio != "")
	{
		document.getElementById(currentAudio).pause();
	
		$("#"+currentAudio).next().find("img").attr("src", "common/images/sample_out.png");
	}
	//now play the new one
	if (currentAudio != ref)
	{
		document.getElementById(ref).play();
	
		$("#"+ref).next().find("img").attr("src", "common/images/sample_over.png");
	}
	
	currentAudio = ref;
	
	/*
	for(var i=0; i < audios.length; i++)
	{
		audios[i].pause();
	}
	
	$("#"+ref).parent().find(".sample_img").src = "common/images/sample_over.png";
	
	document.getElementById(ref).play();
	*/
}


function WriteObject(strToWrite)
{

	this.value = strToWrite;

}

WriteObject.prototype.write = function(idToWriteTo)
{
	document.getElementById(idToWriteTo).innerHTML = this.value;
}

function Sample(id, name, file, selected)
{
    this.ID = id;
    this.TalentName = name;
    this.File = file;
    this.Select = selected;
    this.SO = null;
}

Sample.prototype.writeSample = function()
{
    if (this.SO != null)
    {
        this.SO.write("sampleHolder_"+this.ID);
    }
}

Sample.prototype.render = function(index)
{

    
    var css = "vtSampleOdd";
    if (index % 2 == 0)
    {
        css = "vtSampleEven";
    }
    
    var s = create("div", css);
    
        var cellName = create("div", "vtCellName");
            var cellNameSpan = create("span");
            cellNameSpan.innerHTML = this.TalentName;
        cellName.appendChild(cellNameSpan);
    s.appendChild(cellName);
        
        var cellSample = create("div", "vtCellSample");
            var sampleHolder = create("div");
            sampleHolder.id = "sampleHolder_"+this.ID;
        cellSample.appendChild(sampleHolder);
    s.appendChild(cellSample);
    
    if (hasHTML5MP3Audio())
	{
		var renderLink = false;
		
		if (isAppleMobile() && getSystemVersion() < 4)
		{
			renderLink = true;
		}
		
		
		
		if (renderLink)
		{
			this.SO = new WriteObject("<a href=\""+this.File+"\"><img src=\"common/images/sample_out.png\"></a>");
		}
		else 
		{
			//render the sample as html5
			this.SO = new WriteObject("<audio class=\"audio_sample\" id=\"audio_"+this.ID+"\" src=\""+this.File+"\" preload=\"none\"></audio><a href=\"javascript:playHTML5MP3Audio('audio_"+this.ID+"');\"><img class=\"sample_img\" src=\"common/images/sample_out.png\" /></a>");
		}
		
	}
	else if (isMobile())
	{

		this.SO = new WriteObject("<a href=\""+this.File+"\"><img src=\"common/images/sample_out.png\"></a>");
    }
    else 
    {
		//if all else fails, use flash
		this.SO = new SWFObject("common/media/soundPlayer.swf", "sample_"+this.ID, "15", "15", "9", "#ffffff", "high", "", "Snap.aspx?page=Flash");
		this.SO.addParam("salign", "TL");
		this.SO.addParam("scale", "noscale");
		this.SO.addParam("wmode", "transparent");
		this.SO.addVariable("soundFile", this.File);
	}
    //so.write("sample_"+this.ID);

    
        var selected = "";
        if (this.Select)
        {
            selected = "checked";
        }
        var cellSelect = create("div", "vtCellSelect");
        cellSelect.innerHTML = "<input type=\"radio\" name=\"voicetalent\" "+selected+" /><input type=\"hidden\" value=\""+this.ID+"\" /><input type=\"hidden\" value=\""+this.TalentName+"\" />";
        
    s.appendChild(cellSelect);
        
    return s;
    
}


