var xmlHTTP, ACCOUNT, PROJECT, g_dataIndex, g_dataArtist, GETDATA

function getIndex(p_urlIndex) {
	xmlHTTP = new XMLHttpRequest()
	xmlHTTP.open("GET", p_urlIndex + "?"+ Math.random(), false)
	
	try {
		xmlHTTP.send("")
	} catch (e) {
		alert ("Error reading data: "+ e.message)
		return false
	}
	
	if (xmlHTTP.status != 200) {
		alert ("couldn't read index")
		return false
	}
	
	try {
		g_dataIndex = new Function("return "+ xmlHTTP.responseText)()
	} catch (e) {
		alert ("Couldn't read index JSON")
		return false
	}
	
	// set globals
	ACCOUNT = g_dataIndex.account
	PROJECT = g_dataIndex.project
	
	return true
}


function loadArtist(p_idArtist) {
	
	xmlHTTP = new XMLHttpRequest()
	xmlHTTP.open("GET", 'http://'+ document.domain +'/'+ g_dataIndex.data[p_idArtist].data +"?"+ Math.random(), false)
	
	try {
		xmlHTTP.send("")
	} catch (e) {
		alert ("Error reading data: "+ e.message)
		return false
	}
	
	if (xmlHTTP.status != 200) {
		alert ("couldn't read artist data")
		return false
	}
	
	try {
		g_dataArtist = new Function("return "+ xmlHTTP.responseText)()
	} catch (e) {
		alert ("Couldn't read JSON")
		return false
	}
	
	return true
}


// process request data

GETDATA = new Array()

// AM: UPDATE: Redirect from Wordpress.com with Safari doesn't unescape the URL - need to do it ourselves.
var sGet = unescape(window.location.search)

if (sGet) {
    sGet = sGet.substr(1)
    var sNVPairs = sGet.split("&")
    for (var i = 0; i < sNVPairs.length; i++) {
        var sNV = sNVPairs[i].split("=")
        GETDATA[sNV[0]] = sNV[1]
    }
}



function hasProperties(p_obj) {
	for (var prop in p_obj)
		if (p_obj.hasOwnProperty(prop))
			return true
	return false
}

function numProperties(p_obj) {
	var count = 0
	for (var prop in p_obj)
		if (p_obj.hasOwnProperty(prop))
			count++
	return count
}

