// ************ main javascript file ******************

// *** Globals ***
_active_button_play = "";
_active_button_dl = "";
_active_loop = 0;
_loop_playing = false;
_cart_preview_playing = false;
_cur_cart_playbtn = "";

forumplay = false; // default, reset inside forums
volactive = false; // for preview volume control
wd_playbacktime = (20 * 1000); // 20 seconds
wd_playback = "";
previewloadstr = "";
curPlayerVol = 80;

// *** Navigation ***
function Go_Home() { top.location.href = "/index.php"; }
function Go_Story() { top.location.href = "/story.php"; }
function Go_Game() { top.location.href = "/gameinfo.php"; }
function Go_Media() { top.location.href = "/media.php"; }
function Go_Files() { top.location.href = "/downloads.php"; }
function Go_Jobs() { top.location.href = "/jobs.php"; }
function Go_About() { top.location.href = "/about.php"; }
function Go_Resources() { top.location.href = "/resources.php"; }
function Go_Forums() { top.location.href = "/forums/"; }

// *** Loop Player ***
function readyToPlay() { PlayLoop(); } // callback
function donePlaying() { if(!_cart_preview_playing){StopLoop();} else {stopcartpreview();} } // callback
function startPlayTimer() { window.clearTimeout(wd_playback); wd_playback = window.setTimeout("stopTimedPlay();", wd_playbacktime); }
function stopTimedPlay() { if( _loop_playing ) {
  if(_active_button_play) { _active_button_play.src = "/images/skin/skin01/Play.png"; }
  if(_active_button_dl) { _active_button_dl.src = "/images/skin/skin01/download_s.png"; }
  StopLoop(); window.clearTimeout(wd_playback); _active_loop = 0;
} }
function PlayLoop() { soundObj.stop(); ApplyVol(); if(!_cart_preview_playing){startPlayTimer();soundObj.start(0.05118,6);} else {soundObj.start(0.05118,1);} _loop_playing = true;}
function StopLoop() { soundObj.stop(); _loop_playing = false; }
function PlayerStop() { stopTimedPlay(); }
function ApplyVol() { soundObj.setVolume(curPlayerVol); }
function setLoopPlayButton(audioid)
{
 if(!forumplay) {
 if( (_loop_playing) && (_active_loop == audioid) )
 { // same loop, which means stop
  if(_active_button_play) { _active_button_play.src = "/images/skin/skin01/Play.png"; }
  if(_active_button_dl) { _active_button_dl.src = "/images/skin/skin01/download_s.png"; }
  window.clearTimeout(wd_playback); _active_loop = 0;
  return false;
 }
 else
 if(_loop_playing)
 { // new loop clicked, previous loop is playing and not fresh page ... button still exists
  if(_active_button_play) { _active_button_play.src = "/images/skin/skin01/Play.png"; }
  if(_active_button_dl) { _active_button_dl.src = "/images/skin/skin01/download_s.png"; }
  _active_button_play = document.getElementById(audioid+"_PSbtn");
  _active_button_play.src = "/images/skin/skin01/Stop.png";
  _active_button_dl =  document.getElementById(audioid+"_DLbtn");
  _active_button_dl.src = "/images/skin/skin01/download.png";
  window.clearTimeout(wd_playback); _active_loop = audioid;
  return true;
 }
 else
 { // no loop playing
  _active_button_play = document.getElementById(audioid+"_PSbtn");
  _active_button_play.src = "/images/skin/skin01/Stop.png";
  _active_button_dl =  document.getElementById(audioid+"_DLbtn");
  _active_button_dl.src = "/images/skin/skin01/download.png";
  window.clearTimeout(wd_playback); _active_loop = audioid;
  return true;
 }
 } // end normal play (not forum or shopping cart
 else
 { // playing inside forum with preview player...
  // just play it
  return true;
 }
}
function LoadPreview(audioid, displayname, artist, tempo, bars, category, subcategory, genre, samplerate, samplebits) {
 previewloadstr = "http://www.loopartists.com/nexus1.5/system/preview_a.mp3?c=genesispreview&ai="+audioid;
 if(forumplay)
 {
  itemstr = "<IMG SRC=\"http://statics.loopartists.com/webskins/skin01/download_es.png\" ";
  itemstr += "ONCLICK=\"showdownload(" + audioid + ", '" + displayname + "');\" ";
//  itemstr += "ONCLICK=\"setdownloadloop(" + audioid + ", '" + displayname + "', '" + artist + "', " + bars + ", " + samplerate + ", " + samplebits + "); setupdownload();event.cancelBubble=true;\" ";
  itemstr += "ALT=\"download\" TITLE=\"Download this loop\">";
// +" &nbsp; "+itemstr;
//  document.getElementById("playerinfobox").innerHTML=" "+displayname+" by "+artist+" - "+category+":"+subcategory+" - "+genre+" - "+tempo+" BMP";
  document.getElementById("playerinfobox").innerHTML=" "+displayname+" by "+artist+" - "+category+":"+subcategory+" - "+genre+" - "+tempo+" BMP"+" &nbsp; "+itemstr;
 }
 if(setLoopPlayButton(audioid)) { StopLoop(); soundObj.loadSound(previewloadstr, false); } else  { StopLoop(); _active_loop = 0; }
}

function stopcartpreview()
{
 lastplaybtn = _cur_cart_playbtn;
 if(_cart_preview_playing)
 {
  lastplaybtn = document.getElementById(_cur_cart_playbtn);
  lastplaybtn.src = "/images/skin/skin01/Play.png";
  lastplaybtn.status = "play";
  StopLoop();
  _cart_preview_playing = false;
 }
}

function playcartpreview(playbtn, audiolink)
{
 lastplaybtn = _cur_cart_playbtn;
 stoponly = false;
 if(_cart_preview_playing)
 {
  curplaybtn = document.getElementById(_cur_cart_playbtn);
  curplaybtn.src = "/images/skin/skin01/Play.png";
  curplaybtn.status = "play";
  StopLoop();
  _cart_preview_playing = false;
  if(lastplaybtn == playbtn) { stoponly = true; }
 }
 if(!stoponly)
 {
  _cur_cart_playbtn = playbtn;
  curplaybtn = document.getElementById(_cur_cart_playbtn);
  curplaybtn.src = "/images/skin/skin01/Stop.png";
  _cart_preview_playing = true;
  soundObj.loadSound(audiolink, false);
  curplaybtn.status = "stop";
 }
}
// *** requirements check ***//
function CookieCheck() { if(!readCookie("SID")) {alert("Session cookies are required for full function of this site. Without cookies enabled, you will not be able to preview or download our loops."); } }
function FlashCheck() { if(!DetectFlashVer(8, 0, 0)) {alert("Adobe Flash Player 8.0+ is required for full function of this site. Without Flash Player enabled, you will not be able to preview our loops."); } }

function setupsearchlists()
{
 var searchbar = "searchbar";
 var resultsbtnstr = 'Show Results:<BR><span ID="totalloops" STYLE="font-weight:bold;">0</span>';
 
// catdrop = new cDropList("catdl", searchbar, 20, 14, "10px", "200px", 100, "updatedroplists(1, catdrop.value);");
// document.getElementById(catdrop.htmlID).style.left = "10px";
// genredrop = new cDropList("genredl", searchbar, 20, 14, "10px", "170px", 80, "updatedroplists(3, genredrop.value);");
// document.getElementById(genredrop.htmlID).style.left = "220px";
// tempodrop = new cDropList("tempodl", searchbar, 20, 14, "10px", "150px", 70, "updatedroplists(4, tempodrop.value);");
// document.getElementById(tempodrop.htmlID).style.left = "400px";

 catdrop = new cDropList("catdl", searchbar, 20, 14, "8px", "200px", 100, "updatedroplists(1, catdrop.value);");
 subcatdrop = new cDropList("subcatdl", searchbar, 20, 14, "8px", "190px", 90, "updatedroplists(2, subcatdrop.value);");
 genredrop = new cDropList("genredl", searchbar, 20, 14, "8px", "170px", 80, "updatedroplists(3, genredrop.value);");
 tempodrop = new cDropList("tempodl", searchbar, 20, 14, "8px", "150px", 70, "updatedroplists(4, tempodrop.value);");

 document.getElementById(catdrop.htmlID).style.left = "20px";
 document.getElementById(subcatdrop.htmlID).style.left = "230px";
 document.getElementById(genredrop.htmlID).style.left = "430px";
 document.getElementById(tempodrop.htmlID).style.left = "610px";

 var btninsertstr = "<BUTTON ID='searchresultsbtn' STYLE='position:absolute;top:0px;left:770px;width:130px;' ONCLICK='displayresults(1);'>"+resultsbtnstr+"</BUTTON>";
 btninsertstr += "<SPAN STYLE='position:absolute;top:8px;left:910px;width:70px;'><A href='' ONCLICK='resetcatlists();return false;'>Reset</A></SPAN>";
 document.getElementById(searchbar).insertAdjacentHTML("beforeEnd", btninsertstr);
 window.setTimeout("resetcatlists();", 100);
}

function showdownload(aid, loopname)
{
 if(uid==""){ window.open("http://www.loopartists.com/download.php?aid="+aid+"&name="+loopname, "download_"+aid, "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=510,height=480,left=300,top=200"); }
 else { window.location = "http://www.loopartists.com/nexus1.5/system/proc_a.php?r=alpha&c=freeloop&ai="+aid; }
}

function LoadLegal(index)
{
 switch(index)
 {
  case 1: window.open("http://www.loopartists.com/nexus1.5/system/proc_a.php?r=alpha&c=consumerterms", "consumerterms", "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=610,height=480,left=300,top=200"); break;
  case 2: window.open("http://www.loopartists.com/nexus1.5/system/proc_a.php?r=alpha&c=copyrightpolicy", "copyrightpolicy", "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=610,height=480,left=300,top=200"); break;
  case 3: window.open("http://www.loopartists.com/nexus1.5/system/proc_a.php?r=alpha&c=privacypolicy", "privacypolicy", "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=610,height=480,left=300,top=200"); break;
  case 4: window.open("http://www.loopartists.com/nexus1.5/system/proc_a.php?r=alpha&c=trademarks", "trademarks", "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=610,height=480,left=300,top=200"); break;
 }
}

function GoTo_Forums(f_url) { top.location.href = f_url; }

/***********************************************************************************************
 * !! The following copyright only applies to the code below !!
 *
 * Copyright (C) 2008 Backplane Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /**
        @fileoverview
        For browsers such as Firefox, that do not support IE's rather handy bonus functions
                insertAdjacentHTML, and insertAdjacentElement.
 */

/**
        Inserts an element into the DOM at a given location.  This is an addon applied to Elements
                in the target environment.  Nodes must be of compatible types in the target DOM, i.e.
                this will not be able to insert an XML Element into an HTML tree unless the underlying
                DOMs allow it.  To mix incompatible DOMs, serialise the inserted node, and use insertAdjacentHTML
        @param {String} where One of beforeBegin, afterBegin, beforeEnd, or afterEnd
        @param {Node} parsedNode a node to insert into this element
        @addon
*/
        function insertAdjacentElement(where,parsedNode)
        {
                switch (where)
                {
                        case 'beforeBegin':
                                this.parentNode.insertBefore(parsedNode,this);
                                break;
                        case 'afterBegin':
                                this.insertBefore(parsedNode,this.firstChild);
                                break;
                        case 'beforeEnd':
                                this.appendChild(parsedNode);
                                break;
                        case 'afterEnd':
                                if (this.nextSibling)
                                {
                                        this.parentNode.insertBefore(parsedNode,this.nextSibling);
                                }
                                else
                                {
                                        this.parentNode.appendChild(parsedNode);
                                }
                                break;
                }
        }

/**
        Inserts some markup into the DOM at a given location.  This is an addon applied to Elements
                in the target environment.
        @param {String} where One of beforeBegin, afterBegin, beforeEnd, or afterEnd
        @param {String} htmlStr markup to insert into this element
        @addon
*/
        function insertAdjacentHTML(where,htmlStr)
        {
                var r = this.ownerDocument.createRange();
                r.setStartBefore(this);
                var parsedHTML = r.createContextualFragment(htmlStr);
                this.insertAdjacentElement(where,parsedHTML);
        }


/**
        Inserts some text into the DOM at a given location, ignoring markup.
                This is an addon applied to Elements in the target environment.
                If parseable (or poorly-formed) markup is present in txtStr it will
                be escaped and inserted into the target element as text.
        @param {String} where One of beforeBegin, afterBegin, beforeEnd, or afterEnd
        @param {String} txtStr text to insert into this element
        @addon
*/
        function insertAdjacentText(where,txtStr)
        {
                var parsedText = document.createTextNode(txtStr);
                this.insertAdjacentElement(where,parsedText);
        }


//Add the functions to the HTMLElement prototype, if absent
if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement)
{
        HTMLElement.prototype.insertAdjacentElement = insertAdjacentElement;
        HTMLElement.prototype.insertAdjacentText = insertAdjacentText;
        HTMLElement.prototype.insertAdjacentHTML =insertAdjacentHTML;
}

//Add the functions to the Element prototype, if absent
if(typeof Element!="undefined" && !Element.prototype.insertAdjacentElement)
{
        Element.prototype.insertAdjacentElement = insertAdjacentElement;
        Element.prototype.insertAdjacentText = insertAdjacentText;
        Element.prototype.insertAdjacentHTML =insertAdjacentHTML;
}

