// ThePremoPlayer.js - v1.1 - http://www.thepremoplayer.com
// audiobridge.js - v1.1 - http://www.thepremoplayer.com
//
// Copyright (c) 2010 LeBraun Premo / ZIPBOX Media LLC
// All rights reserved.
// 
//     Requires: Coming Soon
// Instructions: Coming Soon
// 
// Redistribution and use of this code in source form, with or without modification,
// are not permitted at this time.
// 
// * USE OF SOURCE ON COMMERCIAL (FOR-PROFIT) WEBSITE REQUIRES ONE-TIME LICENSE FEE PER DOMAIN.
//   Coming Soon! Visit www.thepremoplayer.com for licensing instructions. Thanks!
//
// * Non-commercial (personal) website use is not permitted at this time without license/payment!
//
// * Redistribution of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
//
// * Redistribution of source code and derived works cannot be sold without specific
//   written prior permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//     
//     ####        #
//       #  # #### #### #### #  #
//      #   # #  # #  # #  #  ##
//     #### # #### #### #### #  #
//            #
//

var files = new Array();
var durations = new Array();
var access = new Array();

var steps = 60;
var imgWidth = 30;
var progress = -1;

var aid = null;
var sid = null;
var cid = null;
var platform = null;

var timerID = null;

function populateArrays(id, url, duration) {
	files[id] = url;
	durations[id] = duration;
}

function populateStats(aidx, sidx, cidx, platformx) {
	aid = aidx;
	sid = sidx;
	cid = cidx;
	platform = platformx;
}

function addEventListenersTo(id) {
	var e = document.getElementById(id);
		if (e.addEventListener) {
			e.addEventListener('mouseover',MouseOver, false);
			e.addEventListener('mouseout',MouseOut, false);
			e.addEventListener('mousedown',MouseDown, false);
			e.addEventListener('mouseup',MouseUp, false);
		} else {
			e.attachEvent('onmouseover',MouseOver);
			e.attachEvent('onmouseout',MouseOut);
			e.attachEvent('onmousedown',MouseDown);
			e.attachEvent('onmouseup',MouseUp);
		}	
}

var pressed = false;
var currentID = null;

function MouseOver(evt) {
	MouseEvent(evt, -26);
}
function MouseOut(evt) {
	MouseEvent(evt, 0);
}
function MouseDown(evt) {
	MouseEvent(evt, -52);
}

function MouseEvent(evt, xv) {
	evt = evt||window.event;
	var trgSrc = evt.target||evt.srcElement;
	xpos = xv;
	if (pressed && currentID == trgSrc.id && durations[trgSrc.id.substring(1, trgSrc.id.length)] != null) {
		xpos -= 78;
	}
	trgSrc.style.backgroundPosition = xpos+'px 0px';
}

function MouseUp(evt) {
	evt=evt||window.event;
	var trgSrc = evt.target||evt.srcElement;
	var bool = durations[trgSrc.id.substring(1, trgSrc.id.length)];

	if (trgSrc.id == currentID) {
		if (pressed) {
			pressed = false;
			//pause track
			if (bool != null) {
				PlaySong();
			 } else {
			 	PauseSong();
			 }
		} else {
			pressed = true;
			//play track
			if (bool != null) {
				PlaySong();
			 } else {
			 	PauseSong();
			 }
		}
	} else {
		if (currentID != null) { // reset
			document.getElementById(currentID).style.backgroundPosition = '0px 0px';
			document.getElementById('a'+currentID.substring(1, currentID.length)).style.backgroundPosition = '0px 0px';
		}
		pressed = true;
		currentID = trgSrc.id;
		//load new track
			if (bool != null) {
				PlaySong();
			 } else {
			 	PauseSong();
			 }
			 

			 var id =trgSrc.id.substring(1, trgSrc.id.length);
			 ajax('ajax/prod_preview_stat.php?pid='+id+'&aid='+aid+'&sid='+sid+'&cid='+cid+'&platform='+platform);
		
	}	
	
	MouseEvent(evt, -26);
}

/* function to tell flash to play a song */
    function thisMovie(movieName) {
         if (navigator.appName.indexOf("Microsoft") != -1) {
             return window[movieName];
         } else {
             return document[movieName];
         }
     }
     function GetReadyToSend(value, v) {
         thisMovie("Bridge").sendToActionScript(value, v);
     }
     
function PlaySong() {
	var id = currentID.substring(1, currentID.length); 
	GetReadyToSend(files[id], durations[id]);
	progress = -1;
	if (timerID) {
		clearTimeout(timerID);
	}
	count = 30;
	timerID = setTimeout ( "waitinger()", 25 );
}
function PauseSong() {
	GetReadyToSend("", -1);
}

function setProgress(percentage) {
	percentage = parseInt(percentage)*imgWidth;
	document.getElementById('a'+currentID.substring(1, currentID.length)).style.backgroundPosition = (-percentage)+'px 0px';
	progress = percentage;
}

var count = 30;

function waitinger() {
	count-=30;
	if (count < -570) {
		count = 0;
	}
	//setBP(elmnt('audio_progress'), count);
	if (pressed && progress < 0) {
		document.getElementById('a'+currentID.substring(1, currentID.length)).style.backgroundPosition = count+'px -30px';
		timerID = setTimeout ( "waitinger()", 25 );
	}
}

function sendToJavaScript(value) {
	value = value/100 * 60;
     setProgress(value);
}

function songComplete() {
	if (currentID != null) { // reset
		document.getElementById(currentID).style.backgroundPosition = '0px 0px';
		document.getElementById('a'+currentID.substring(1, currentID.length)).style.backgroundPosition = '0px 0px';
	}
	pressed = false;
	currentID = null;
}

