function toggleStar(el) {
    if (el.getAttribute('src') == '/img/star_inactive.png') {
    	el.setAttribute('src', '/img/star_active.png');
    } else {
        el.setAttribute('src', '/img/star_inactive.png');
    }
}

function doArtistBlur(elId, oTitleDS_XHR, oRemixDS_XHR, titleElId) {
    doCheckArtistExists(elId);
    oTitleDS_XHR.scriptQueryAppend = 'artist=' + escape(document.getElementById(elId).value);
    oRemixDS_XHR.scriptQueryAppend =
        'artist=' + escape(document.getElementById(elId).value) +
        '&title=' + escape(document.getElementById(titleElId).value);
}

function doTitleBlur(elId, artistElId, oRemixDS_XHR) {
    doCheckTitleExists(elId, artistElId);
    oRemixDS_XHR.scriptQueryAppend =
        'artist=' + escape(document.getElementById(artistElId).value) +
        '&title=' + escape(document.getElementById(elId).value);
}

function doRemixBlur(elId, artistElId, titleElId) {
    doCheckRemixExists(elId, artistElId, titleElId);
}

function doAddArtist(elId) {
    var el = document.getElementById(elId);
    
    var sUrl = '/artists/ajaxadd';
    
    var sPostData = 'name=' + escape(el.value);
    
    var addArtistCallback = {
        success: callbackAddArtist,
        argument: { elId: elId }
    }

    var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, addArtistCallback, sPostData);
}

function callbackAddArtist(o) {
    if (o.responseText !== undefined) {
        var elId = o.argument.elId;
        
        if (o.responseText > 0) {
            doCheckArtistExists(elId);
        } else {
            alert('Seem like we failed to add artist but please search for it before you try to submit it again.');
        }
    }
}

/***** autocomplete *****/

var fnCustomFormatter = function(oResultItem, sQuery) {
    var aMarkup = ['<div class="sample-result">',
        oResultItem[0],
        '</div>'];
    return (aMarkup.join(""));
};

function initAutocomplete(name, url) {
    var oDS_XHR = new YAHOO.widget.DS_XHR(url, ["\n", "\t"]);
    oDS_XHR.scriptQueryParam = 'key';
    oDS_XHR.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;
    oDS_XHR.maxCacheEntries = 60;
    oDS_XHR.queryMatchSubset = true;

	var elInput = document.getElementById(name);
	var elContainer = document.getElementById(name + '_ysearchcontainer');

	var oAutoComplete = new YAHOO.widget.AutoComplete(elInput, elContainer, oDS_XHR);
	oAutoComplete.queryDelay = 0.5;
	oAutoComplete.maxResultsDisplayed = 25;
	oAutoComplete.formatResult = fnCustomFormatter;
    
    return oDS_XHR;
}

/***** artist onBlur check *****/

var tCheckArtistExists = null;
var sCheckArtistExists_elId = null;

function doRemoveArtistCheck(elId) {
	window.clearTimeout(tCheckArtistExists);
    document.getElementById(elId + '_actioncontainer').innerHTML = '';
	YAHOO.util.Dom.setStyle(elId, 'background-color', 'white');
}

function doCheckArtistExists(elId) {
    document.getElementById(elId + '_actioncontainer').innerHTML = '<img src="/img/loading.gif">';
	sCheckArtistExists_elId = elId;
    tCheckArtistExists = window.setTimeout(doCheckArtistExistsDelayed, 150);
}

function doCheckArtistExistsDelayed() {
    var el = document.getElementById(sCheckArtistExists_elId);
    
    var sUrl = '/artists/getid?name=' + escape(el.value);
    
    var commentCallback = {
        success: callbackCheckArtistExists,
        argument: { elId: sCheckArtistExists_elId }
    }

    var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, commentCallback);
}

function callbackCheckArtistExists(o) {
    if (o.responseText !== undefined) {
        var elId = o.argument.elId;
        var el = document.getElementById(elId);
        
        if (o.responseText > 0) {
        	YAHOO.util.Dom.setStyle(elId, 'background-color', 'lime');
            
            var s = '<a href="/artists/details?id=' + o.responseText + '" target="_blank">';
            s += '<img src="/img/arrow_right.gif">';
            s += '</a>';
            document.getElementById(elId + '_actioncontainer').innerHTML = s;
        } else {
            YAHOO.util.Dom.setStyle(elId, 'background-color', 'red');
            
            var s = '<a href="javascript:void(0)" onclick="';
            s += 'if(confirm(\'Artist does not exist. Do you want to add \\\'' + el.value + '\\\' to the artist database?\'))doAddArtist(\'' + elId + '\')';
            s += '">';
            s += '<img src="/img/icon_alert.gif">';
            s += '</a>';
            document.getElementById(elId + '_actioncontainer').innerHTML = s;
        }
    }
}

/***** title onBlur check *****/

var tCheckTitleExists = null;
var sCheckTitleExists_elId = null;
var sCheckTitleExists_artistElId = null;

function doRemoveTitleCheck(elId) {
    window.clearTimeout(tCheckTitleExists);
    document.getElementById(elId + '_actioncontainer').innerHTML = '';
    YAHOO.util.Dom.setStyle(elId, 'background-color', 'white');
}

function doCheckTitleExists(elId,artistElId) {
    document.getElementById(elId + '_actioncontainer').innerHTML = '<img src="/img/loading.gif">';
    sCheckTitleExists_elId = elId;
    sCheckTitleExists_artistElId = artistElId;
    tCheckTitleExists = window.setTimeout(doCheckTitleExistsDelayed, 150);
}

function doCheckTitleExistsDelayed() {
    var el = document.getElementById(sCheckTitleExists_elId);
    var artistEl = document.getElementById(sCheckTitleExists_artistElId);
    
    var sUrl = '/songs/getid?title=' + escape(el.value) + '&artist=' + escape(artistEl.value);
    
    var commentCallback = {
        success: callbackCheckTitleExists,
        argument: { elId: sCheckTitleExists_elId }
    }

    var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, commentCallback);
}

function callbackCheckTitleExists(o) {
    if (o.responseText !== undefined) {
        var elId = o.argument.elId;
        
        if (o.responseText > 0) {
            YAHOO.util.Dom.setStyle(elId, 'background-color', 'lime');

            var s = '<a href="/songs/details?id=' + o.responseText + '" target="_blank">';
            s += '<img src="/img/arrow_right.gif">';
            s += '</a>';
            document.getElementById(elId + '_actioncontainer').innerHTML = s;
        } else {
            YAHOO.util.Dom.setStyle(elId, 'background-color', 'red');
 
            var s = '<a href="javascript:void(0)" onclick="';
            s += 'if(confirm(\'Song does not exist. Do you want to create it?\'))alert(\'Ok, soon you can do it but not yet, sorry\')';
            s += '">';
            s += '<img src="/img/icon_alert.gif">';
            s += '</a>';
            document.getElementById(elId + '_actioncontainer').innerHTML = s;
        }
    }
}

/***** remix onBlur check *****/

var tCheckRemixExists = null;
var sCheckRemixExists_elId = null;
var sCheckRemixExists_artistElId = null;
var sCheckRemixExists_titleElId = null;

function doRemoveRemixCheck(elId) {
    window.clearTimeout(tCheckRemixExists);
    document.getElementById(elId + '_actioncontainer').innerHTML = '';
    YAHOO.util.Dom.setStyle(elId, 'background-color', 'white');
}

function doCheckRemixExists(elId,artistElId,titleElId) {
    document.getElementById(elId + '_actioncontainer').innerHTML = '<img src="/img/loading.gif">';
    sCheckRemixExists_elId = elId;
    sCheckRemixExists_artistElId = artistElId;
    sCheckRemixExists_titleElId = titleElId;
    tCheckRemixExists = window.setTimeout(doCheckRemixExistsDelayed, 150);
}

function doCheckRemixExistsDelayed() {
    var el = document.getElementById(sCheckRemixExists_elId);
    var artistEl = document.getElementById(sCheckRemixExists_artistElId);
    var titleEl = document.getElementById(sCheckRemixExists_titleElId);
    
    var sUrl = '/remixes/getid?remix=' + escape(el.value) + '&title=' + escape(titleEl.value)+ '&artist=' + escape(artistEl.value);
    
    var callback = {
        success: callbackCheckRemixExists,
        argument: { elId: sCheckRemixExists_elId }
    }

    var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
}

function callbackCheckRemixExists(o) {
    if (o.responseText !== undefined) {
        var elId = o.argument.elId;
        
        if (o.responseText > 0) {
            YAHOO.util.Dom.setStyle(elId, 'background-color', 'lime');

            var s = '<a href="/remixes/details?id=' + o.responseText + '" target="_blank">';
            s += '<img src="/img/arrow_right.gif">';
            s += '</a>';
            document.getElementById(elId + '_actioncontainer').innerHTML = s;
        } else {
            YAHOO.util.Dom.setStyle(elId, 'background-color', 'red');
 
            var s = '<a href="javascript:void(0)" onclick="';
            s += 'if(confirm(\'Remix does not exist. Do you want to create it?\'))alert(\'Ok, soon you can do it but not yet, sorry\')';
            s += '">';
            s += '<img src="/img/icon_alert.gif">';
            s += '</a>';
            document.getElementById(elId + '_actioncontainer').innerHTML = s;
        }
    }
}
