/** 
 * Error handling and debugging
 */
var defaultErrorMessage="Onbekende fout. Probeer het (later) nog eens.";
function __toDebugWindow(message)
{
	if ( __isDebug )
	{
		console.debug(message);
	}
}
function __toErrorWindow(e, fileName, functionName, showAlert)
{
	if ( __isDebug && showAlert)
	{
		alert("Fout");
		console.debug(fileName + " - " + functionName);
		console.debug(e);
	}
}

/**
 * Flash-communication
 */
// Hebben we nodig om een flashobject op te roepen
function getPlayer(gid) {
	if(navigator.appName.indexOf('Microsoft') != -1) {
		return window[gid];
	} else {
		return document[gid];
	}
}
// Zoek-functie
function search(term)
{
	if ( main )
	{
		main.requestPage('/search/'+term, null);
	}
}
function searchFromHTMLForm()
{
	__toDebugWindow('searchFromHTMLForm()');
	
	var searchTerm = dojo.byId('searchInputField').value;
	
	if ( searchTerm && searchTerm != "" && searchTerm.length > 1 )
	{
		search(searchTerm);
	}
	else
	{
		alert("Let op: je zoekopdracht moet uit minstens 2 tekens bestaan!");
	}
	
	return false;
}
function showSearch(visible)
{
	if ( visible )
	{
		dojo.byId('searchWrapper').style.display = 'block';
	}
	else
	{
		dojo.byId('searchWrapper').style.display = 'none';
	}
}
// Flash roept deze functie op om een lijst op te halen
function showEpisodeTop30(episodeID)
{
	if ( main && main.timemachine.isInit == true )
	{
		main.requestPage('/chart-list/'+episodeID, null);
	}
	else if ( main && main.timemachine.isInit == false )
	{
		main.timemachine.isInit = true;
	}
}

/**
 * dispatchers
 */
// Dispatch een dojo-event
function dispatchCatEvent(name, parameters)
{
	__toDebugWindow("dispatchCatEvent("+name+","+parameters+")");
	try
	{
		dojo.publish(name, parameters);
	}
	catch(e){__toErrorWindow(e);}
	
	return false; // disable href
}
// Op een state te dispatchen vanuit html/of met een timeout!
function dispatchState(state)
{
	__toDebugWindow("dispatchState(state:"+state+")");
	
	// Als we een nieuwe pagina aan het inladen zijn, dispatchen we naar deze nieuwe pagina
	if ( main && main.nextPage )
	{
		main.nextPage.setState(state);
	}
	else if ( main && main.currentPage )
	{
		main.currentPage.setState(state);
	}
	return false; // Zodat we geen bookmark instellen
}
function dispatchUserState(state)
{
	__toDebugWindow("dispatchUserState(state:"+state+")");
	
	// 
	if ( userController && userController.p )
	{
		userController.p.setState(state);
	}
	return false; // Zodat we geen bookmark instellen
}

/**
 * radio2.top30.utils.AudioPlayer-functies
 * Deze functies worden door de JWPlayer aangeroepen en mislukken in IE 6+7 als we die in de file plaatsen met de AudioPlayer-klasse
 */
// Aangeroepen door JWPlayer
function playerReady(obj) {
	try
	{
		dojo.publish("event_AudioPlayer_onSWFInitialized", [ obj ] );
	}
	catch(e){__toErrorWindow(e);}
}
// Opvangen van event uit JWPlayer
function event_AudioPlayer_onChangeState(obj)
{
	try
	{
		dojo.publish("event_AudioPlayer_onChangeState", [ obj ] );
	}
	catch(e){__toErrorWindow(e);}
}

/**
 * Globale functies
 */
// Start de radioplayer met de meest recente top 30 uitzending
function herbeluisterMostRecent()
{
	var url = 'http://internetradio.vrt.be/player_detection.html?qsbrand=21&qsODfile=/internetradio_master/productiesysteem2/programma_od/21_21to30.xml';
	var u = new Date();
	window.open(url, "vrtwindow"+u.getTime(),'resizable=0,toolbar=0,scrollbars=0,location=0,status=0,menubar=0,width=787,height=433,left=301,top=232');
	return false;
}
// Converteer een database-formaat-datum naar dag/maand/jaar (DD/MM/YYYY)
function getBelgiumDateFromDB(input)
{
	dojo.require("dojo.date");
	dojo.require("dojo.date.locale");
	dojo.require("dojo.date.stamp");

	var myDate = dojo.date.stamp.fromISOString(input);
	return dojo.date.locale.format(myDate, {selector:'date', datePattern:'dd/MM/yyyy'}).toLowerCase();
}

/**
 * Mouseover-out-functies
 */
function opMouseOver(id)
{
	var eep = dojo.query("#"+id);
	eep.addClass("hover");
}
function opMouseOut(id)
{
	var eep = dojo.query("#"+id);
	eep.removeClass("hover");
}

/**
 * Mouse-over functies van de custom-top-30
 */
function onCTop30Over(id)
{
	var showOver = true;
	if ( main.currentPage.handler.dragNdropWidget && main.currentPage.handler.dragNdropWidget.isDragging == true )
	{
		showOver = false;
	}
	if ( showOver )
	{
		var eep = dojo.query("#"+id);
		eep.addClass("hover");
	}
}
function onCTop30Out(id)
{
	var eep = dojo.query("#"+id);
	eep.removeClass("hover");
}

/**
 * UTF-8 encoderings-klasse
 */
var Utf8 = {

    // public method for url encoding
    encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // public method for url decoding
    decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}


/**
 * Voorlopig nog niet nodig gehad
 */
// String hulpfuncties
function addslashes(str) {
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\0/g,'\\0');
	return str;
}
function stripslashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	return str;
}
/*function addslashes(str)
{
  // http://www.atlwebsite.com
 // By Sean Gallagher
 // Example: addslashes('what "ya\'ll" doing?')
 // Returns: what \"ya\'ll\" doing?
 str = str.replace(/'/g,"\\'");
 return str.replace(/"/g,'\\"');
}*/