var refCallbackFunction;
var timeoutID;
var callbackTimeoutID;
var stackArray;
var launchCounter = 0;
var checkCounter = 0;

function trace( msg ){
	if( typeof( jsTrace ) != 'undefined' ){
		jsTrace.send( msg );
	} else if (window.console)  {
		window.console.log(msg);
	}
}

function showCDTrack(cdList,trackList) {
        refCallbackFunction = function() {
		if(window.refIndexSWF!=undefined) {
 			window.refIndexSWF.searchCDTrackInfo(cdList,trackList);
		} 
		
        }
        launchMyAPM();
}

function showCategory(catID) {
        refCallbackFunction = function() {
 		if(window.refIndexSWF!=undefined) {
			window.refIndexSWF.searchCategory(catID);
		} 
	}
        launchMyAPM();
}

function showMinisearch(searchtext) {
        refCallbackFunction = function() {
		if(window.refIndexSWF!=undefined) {
			window.refIndexSWF.searchMini(searchtext);
		} 
        }
        launchMyAPM();
}

function showTrackList(folderID) {
        refCallbackFunction = function() {
		if(window.refIndexSWF!=undefined) {
			window.refIndexSWF.searchProjectList(folderID);		
		} 
	}
	launchMyAPM();
}

function showComposer(composer) {
	refCallbackFunction = function() {
		//try {
		if(window.refIndexSWF!=undefined) {
			window.refIndexSWF.searchComposer(composer);
		}
		//} catch(e) {
		//} 
	}
	launchMyAPM();
}

function showLibLine(lib,libLine) {
	refCallbackFunction = function() {
		if(window.refIndexSWF!=undefined) {
			// This is reversed because when I changed the searchLibLine, I didn't want to break existing libline calls
			trace("showLibLine:"+typeof window.refIndexSWF.searchLibLine);
			if(!window.winMyAPM.closed && typeof window.refIndexSWF.searchLibLine != "unknown") {
				window.refIndexSWF.searchLibLine(libLine,lib);
			}
		}
	}
	launchMyAPM();
}

function doNothing() {
	// Callback for static launch MyAPM
}

function checkLoadMyAPM2() {
	trace("checkLoadMyAPM2:"+checkCounter);
	stackArray.push({type:'CHK',lcount:launchCounter,checkCount:checkCounter,toID:timeoutID,cbID:callbackTimeoutID,winRef:window.winMyAPM,indexRef:refIndexSWF});
	checkCounter++;
	// All of these IFs and TRYs are for IE that, if it errors out, sometimes cuts off all access to the specified domain until
	// you close and re-open the browser
	var success = false;
	//window.console.log(output goes here)
	//try {
		// Check the IE property of .closed in case the window was closed (even reading the .document property if the window is closed throws an error)
		if(window.winMyAPM && !window.winMyAPM.closed ) {
			//trace("cma window.winMyAPM:"+window.winMyAPM);
			// Check if document object exists
			if(window.winMyAPM.document != null) {
				//trace("cma document:"+window.winMyAPM.document);
				refIndexSWF = undefined;
				// If IE, use the object in the <object> tag
				//trace("cma indexswf_ie:"+window.winMyAPM.document.getElementById('indexswf_ie'));
				if (window.winMyAPM.document.getElementById('myAPM')!=null )   {
                                        window.refIndexSWF = window.winMyAPM.document.getElementById('myAPM');
                                        // Is the MyAPM application active?
					trace("cma typeof:"+typeof refIndexSWF.searchCategory);
                                        if(typeof window.refIndexSWF.searchCategory=="function" || typeof refIndexSWF.searchLibLine=="object") {
                                                success = true;
                                        }
				} 
				if(success == true) {
					trace("cma success cbf:"+refCallbackFunction);
					if(callbackTimeoutID != undefined) {
						//try {
							clearTimeout(callbackTimeoutID);
							callbackTimeoutID = undefined;
						//} catch(e) {
						//}
					}
					if(refCallbackFunction != undefined) {
						//setTimeout('refCallbackFunction();', 5000);
					}
				}
			}
		}
		
		if(success == false && !window.winMyAPM.closed) {
			timeoutID = setTimeout('checkLoadMyAPM2();', 1000);
		} else if (!window.winMyAPM.closed) {
			window.winMyAPM.focus();
			if(refCallbackFunction != undefined) {
				refCallbackFunction();
			}
		}
	//} catch(e) {
	//}
}

// This should be called directly by a routine with a callBackFunction
function launchMyAPM() {
	trace("launchMyAPM:"+launchCounter);
	if(stackArray==undefined) {
		stackArray = new Array();
	}
	//stackArray.push({type:'LMA',lcount:launchCounter,checkCount:checkCounter,toID:timeoutID,cbID:callbackTimeoutID,winRef:window.winMyAPM,indexRef:refIndexSWF});
	launchCounter++;
	// Check if there is already a timeout for checking MyAPM routine -- and clear it
	if(timeoutID != undefined) {
		//alert("Timeoutid:"+timeoutID);
		clearTimeout(timeoutID);
		timeoutID = undefined;
	}
	// Check if there is already a timeout for the MyAPM callback -- and clear it
	if(callbackTimeoutID != undefined) {
		//try {
			clearTimeout(callbackTimeoutID);
			callbackTimeoutID = undefined;
		//} catch(e) {
		//}
	}
	if (location.href.indexOf("dms") != -1) {
	var url = "/mydms/main.php";
	} else {
	var url = "/myapm/main.php";
	}
	var firstLoad = true;
	//alert("WINMA:"+typeof window.winMyAPM);
	if(window.winMyAPM == undefined) {
		firstLoad = true;
	} else if(window.winMyAPM.closed == true) {
		firstLoad = true;
	} else if(window.winMyAPM.document==null) {
		firstLoad = true;
	} else {
		//alert("not first load");
		firstLoad = false;
	}
	if(firstLoad) {
		refIndexSWF = undefined;
		window.winMyAPM = undefined;
		//alert("First load");
		window.winMyAPM = window.open(url); //,'_myapm'); //,'menubar=yes,toolbar=yes,location=yes,directories=yes,fullscreen=no,titlebar=yes,hotkeys=yes,status=yes,scrollbars=yes,resizable=yes');
	}
	checkLoadMyAPM2();

	return false;
}

// This should be called by a routine WITHOUT a callBackFunction
function launchMyAPM2() {
	window.showWelcome = 11;
	refCallbackFunction = undefined;
	launchMyAPM();
}

function checkEnter(e,inObj) {
        var charCode;
        if(e&& e.which) {
                charCode = e.which;
        } else {
                e = event;
                charCode = e.keyCode;
        }
        if(charCode==13) {
		if(inObj.name=="searchword") {
			//alert("Hello:"+inObj.name);
			showMinisearch(document.getElementById('mod_myapmsearch_searchword').value);
		}
                //document.forms[0].submit();
                return false;
        } else {
                return true;
        }
}


function openLibraries() {
		//var parent = getItem111();
		//var libPop = parent.getElementsByTagName('ul')[0].style; //first ul under item111
		var libPop = document.getElementById('library-popup').style; //find library popup articleinclude div
		libPop.display='block';
		//libPop.position='relative';
		libPop.position='absolute';
}
function closeLibraries() {
		//var parent = getItem111();
		//var libPop = parent.getElementsByTagName('ul')[0].style; 
		var libPop = document.getElementById('library-popup').style; //find library popup articleinclude
		libPop.display='none';
		//libPop.position='absolute'; //this for ie6
}

function toggleAudioPlayer(selID,toggleType) {
    //var aplayerList = $$('a.aplayer');
    var aplayerList = jQuery('a.aplayer');
    
    jQuery.each(aplayerList, function(index, val) {
        thisOne = false;
        toggle = true;
        var item = aplayerList[index];
        // window.console.log("index:"+index+" ID:"+item);
        // Check if this is the one to toggle
        if(item.id == selID) {
            thisOne = true;
        }
        // Check if icon is the _stop icon
        if(item.childNodes[0].src.contains("_stop")) {
            // Yes, then turn off IF this is the one and TT is -1 (toggle) or 0 (turn off)
            //              OR    IF this isn't the one
            if( (thisOne && (toggleType==-1 || toggleType == 0)) || (!thisOne) ) { 
                item.childNodes[0].src = item.childNodes[0].src.replace('_stop', '');
                //trace("Turn on ID:"+item.id);
            }
        } else {
            // Icon is off, turn on IF this is the one and TT is -1 (toggle) or 1 (turn on). If not the one, don't turn on.
            if( thisOne && (toggleType==-1 || toggleType == 1)  ) { 
                var tempStr = item.childNodes[0].src;
                tempStr = tempStr.replace('.gif', '_stop.gif')
                tempStr = tempStr.replace('.png', '_stop.png')
                tempStr = tempStr.replace('.jpg', '_stop.jpg')
                item.childNodes[0].src = tempStr;
            }
        }
    } );
}


