/* 
REQUIRED ON PAGE USING THIS CODE:
<iframe id="soundSource" name="soundSource" width="5" height="5" frameborder="0">hidden frame</iframe>
 */

function SoundClip( uri, trid, color ){
  this.uri = uri;
  this.trid = trid;
  this.color = color;
}

SoundClip.prototype.play = function(){
	document.frames["soundSource"].location = this.uri;
	var invalid = ( !this.uri || !this.trid || !this.color );
	if ( invalid ) return;
	var img = document.getElementById(this.trid + '_img');
	var buttonState = this.color + "_pause.gif";
	img.src = "https://fun.samsungmobileusa.com/image/"+ buttonState;
	tmrPreviewButton = setTimeout("resetPreviewButton('"+this.trid+"','"+this.color+"');", 20000);
};

SoundClip.prototype.stop = function(){
	document.frames["soundSource"].location = "/blankpage.html";
	var invalid = ( !this.uri || !this.trid || !this.color );
	if ( invalid ) return;
	var img = document.getElementById(this.trid + '_img');
	var buttonState = this.color + "_preview.gif";
	img.src = "https://fun.samsungmobileusa.com/image/"+ buttonState;
};

SoundClip.prototype.nullify = function(){
	this.uri = null;
	this.trid = null;
	this.color = null;
};

SoundClip.prototype.hasURI = function( URI ){
    return ( this.uri == URI );
};

SoundClip.prototype.alertString = function(){
	return ( "CLIP[uri=" + this.uri + ",\ntrid=" + this.trid + ",\ncolor=" + this.color + "]" );
};

///////////////////////////////////////////////////////////////////////////
//                       GLOBALS AND FUNCTIONS
///////////////////////////////////////////////////////////////////////////


var currentClip = null;
var tmrPreviewButton;

function previewUri( uri, trid, color ) {
	var cleanUri = truncateUriExt( uri );
	if ( currentClip ){ 
//		alert( "STOPPING: " + currentClip.alertString() );
		var stoppingSameSound = currentClip.hasURI( cleanUri );
		currentClip.stop();
		currentClip.nullify();
		if ( stoppingSameSound ) return;
	}
		
	
	currentClip = new SoundClip( cleanUri, trid, color );
//	alert( "STARTING: " + currentClip.alertString() );
	currentClip.play();	
}

function truncateUriExt( uri ){
    var lastIndex = uri.lastIndexOf( "." );
    return ( uri.substring( 0, lastIndex ) );
}

function resetPreviewButton(trid, color) {
    if (trid == null)
        return null; //do nothing
    var a = document.getElementById(trid + '_a');
    var img = document.getElementById(trid + '_img');
    img.src = "https://fun.samsungmobileusa.com/image/" + color + "_preview.gif";
}


/*
document.write( 
  '<iframe id="soundSource" name="soundSource" width="5" height="5" frameborder="0">hidden frame</iframe>'
 );
*/
