﻿// This function will find a html item by his name
function $(pcn) {
    var pcx, pcd = document;
    if (!(pcx = pcd[pcn]) && pcd.all) pcx = pcd.all[pcn];
    if (!pcx && pcd.getElementById) pcx = pcd.getElementById(pcn);
    return pcx;
}

// Funtion add to favorites
function favoris(urlAddress,pageName) {
    if (window.sidebar) {
        window.sidebar.addPanel(pageName, urlAddress, "");
    } else if (window.external) {
    window.external.AddFavorite(urlAddress, pageName);
    }
    else if (window.opera && window.print) {
        return true;
    }
}

// This function will add any function the page load event
function addLL(functionToAdd) {
    if (typeof window.addEventListener != 'undefined') {
        window.addEventListener('load', functionToAdd, false);
    }
    else if (typeof window.document.addEventListener != 'undefined') {
        window.document.addEventListener('load', functionToAdd, false);
    }
    else if (typeof window.attachEvent != 'undefined') {
        window.attachEvent('onload', functionToAdd);
    }
    else {
        if (typeof window.onload != 'function') {
            window.onload = functionToAdd;
        }
        else {
            var oldFunction = window.onload;

            window.onload = function() {
                oldFunction();
                functionToAdd();
            }
        }
    }
}

// This will create and save a Cookie
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

// Read a Cookie
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

// For ajax requests
function GetXmlHttp() {
    var oXmlHttp = false;
    if (!oXmlHttp && typeof XMLHttpRequest != 'undefined') {
        oXmlHttp = new XMLHttpRequest();
    }
    return oXmlHttp;
}

// will lightup stars depending on the rating.
// Active (1,2)
function GET_StarsTXT(Active, rating, nbr_users) {
    Rtg = Math.round(rating)+1;
    for (ii = 1; ii < Rtg; ii = ii + 1) 
    {
        $("el" + ii).src = "/img/star" + Active + ".gif";
    }
    for (ii = Rtg; ii <= 5; ii = ii + 1) {
        $("el" + ii).src = "/img/star0.gif";
    }

    if (nbr_users != undefined) { $("el6").innerHTML = "By <b>" + nbr_users + "</b> users."; }
}

// Will rate the lyrics and send the ajax request to save the new rating and read the response and show the new rating average.
function RateIT(type, id, rate) {
    var sUrl = "/mods/rateit.aspx?type=" + type + "&id=" + id + "&rate=" + rate;
    var oXmlHttp = GetXmlHttp();
    oXmlHttp.open("GET", sUrl, true);
    oXmlHttp.onreadystatechange =
	function() {
        if (oXmlHttp.readyState == 4) {
            var resp = oXmlHttp.responseText;
            if (resp.indexOf(";") > -1) {
                var oss = resp.split(';');
                o0 = 0;
                o1 = 0;
                for (var i = 0; i < oss.length; i++) {
                    o0 = oss[0];
                    o1 = oss[1];
                }

                $("el1").onmouseout = function() { GET_StarsTXT(2, o0); }
                $("el2").onmouseout = function() { GET_StarsTXT(2, o0); }
                $("el3").onmouseout = function() { GET_StarsTXT(2, o0); }
                $("el4").onmouseout = function() { GET_StarsTXT(2, o0); }
                $("el5").onmouseout = function() { GET_StarsTXT(2, o0); }
                GET_StarsTXT(2, o0, o1);
                alert("Done, Thank YOU");
            }
            else {
                alert(resp);
            }
        }
    }
    oXmlHttp.send(null);
}

// This will create the stars on the html page.
// EID  :  Element ID
// Type : (A:albums, L:lyrics)
// ID   : (album or Lyric ID)
// IV   : (Initial Value)
// IU   : (Initial Users)
function SetStars(type, id, iv, iu) {
    if (!$("starsdiv")) {
        document.write("<table id=\"starsdiv\"><tr><td class=\"padw5px titre3\">Rating :</td><td><img id=\"el1\" src=\"/img/star0.gif\" style=\"cursor:pointer;\" /><img id=\"el2\" src=\"/img/star0.gif\" style=\"cursor:pointer;\" /><img id=\"el3\" src=\"/img/star0.gif\" style=\"cursor:pointer;\" /><img id=\"el4\" src=\"/img/star0.gif\" style=\"cursor:pointer;\" /><img id=\"el5\" src=\"/img/star0.gif\" style=\"cursor:pointer;\" /><td id=\"el6\" class=\"padw5px notee\"></td></tr></table>");
    }

    GET_StarsTXT(1, iv, iu);
        
    $("el1").onmouseover = function() { GET_StarsTXT(2, 1); }
    $("el2").onmouseover = function() { GET_StarsTXT(2, 2); }
    $("el3").onmouseover = function() { GET_StarsTXT(2, 3); }
    $("el4").onmouseover = function() { GET_StarsTXT(2, 4); }
    $("el5").onmouseover = function() { GET_StarsTXT(2, 5); }

    $("el1").onmouseout = function() { GET_StarsTXT(1, iv); }
    $("el2").onmouseout = function() { GET_StarsTXT(1, iv); }
    $("el3").onmouseout = function() { GET_StarsTXT(1, iv); }
    $("el4").onmouseout = function() { GET_StarsTXT(1, iv); }
    $("el5").onmouseout = function() { GET_StarsTXT(1, iv); }

    $("el1").onclick = function() { RateIT(type, id, 1); }
    $("el2").onclick = function() { RateIT(type, id, 2); }
    $("el3").onclick = function() { RateIT(type, id, 3); }
    $("el4").onclick = function() { RateIT(type, id, 4); }
    $("el5").onclick = function() { RateIT(type, id, 5); }
}

// Reviews Ratings (will send the Rating to the server)
function UpdateReview(id, ud, type) {
    var sUrl = "/mods/ratereview.aspx?type=" + type + "&id=" + id + "&ud=" + ud;
    var oXmlHttp = GetXmlHttp();
    oXmlHttp.open("GET", sUrl, true);
    oXmlHttp.onreadystatechange =
	function() {
        if (oXmlHttp.readyState == 4) {
            var resp = oXmlHttp.responseText;
            if (resp.indexOf(";") > -1) {
                NRR = parseInt(resp);
                Nr = NRR;
                if (NRR > 0) {
                    Nr = "+" + NRR;
                }
                $(id + "r").innerHTML = Nr;
            }
            else {
                alert(resp);
            }
        }
    }
    oXmlHttp.send(null);
}

// Add the Reviews Rating Buttons to the html page
// Type L:lyric B:album A:artist,
function ShowRB(Rid, Rvalue,type) {
    Uid = Rid + "u";
    Did = Rid + "d";
    if (parseInt(Rvalue) > 0) {
        Rvalue = "+" + Rvalue;
    }
    document.write("<table><tr><td id=\"" + Rid + "r\" class=\"padw5px\">" + Rvalue + "</td><td><img id=\"" + Did + "\" src=\"/img/rated.gif\" style=\"cursor:pointer;\" /></td><td><img id=\"" + Uid + "\" src=\"/img/rateu.gif\" style=\"cursor:pointer;padding-left:2px;padding-right:5px;\" /></td></tr></table> ");
    $(Uid).onclick = function() { UpdateReview(Rid, "u", type); }
    $(Did).onclick = function() { UpdateReview(Rid, "d", type); }
}

// Add Buy Album Section
// not used currently
function Buyalbum(chrtxt) {
    //document.write('<a href="http://www.amazon.com/gp/search?ie=UTF8&keywords=' + chrtxt + '&tag=6lyrics-20&index=music&linkCode=ur2&camp=1789&creative=9325" target="_blank" rel="nofollow"><img src="/img/buy.gif" style="border:0px;" width="209px" height="22px" /></a><img src="http://www.assoc-amazon.com/e/ir?t=6lyrics-20&amp;l=ur2&amp;o=1" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />');
    //document.write('<a href="http://www.cduniverse.com/sresult.asp?HT_Search=ARTIST&HT_Search_Info=' + chrtxt + '&style=music&frm=lk_6lyrics&altsearch=yes" target="_blank"><img src="/img/buy_cdu.gif" style="border:0px;" width="117px" height="21px" /></a>');
}

// Will save the selected FontName and Size in a cookie, so user will have his settings saved for other pages or future visits
function Save_Cur() {
    var slly = $("slly");
    createCookie("6lyrics_fdname", (slly.style.fontFamily) ? slly.style.fontFamily : "courier new, courier, monospace", 365);
    createCookie("6lyrics_fdsize", (slly.style.fontSize) ? slly.style.fontSize.replace("px", "") : "12", 365); 
}

// Change the Font name and size from reading the cookies
function setup_params() {
    var slly = $("slly");
    var fsel = $("fontsel");
    

    // Setting Fond Name
    if (readCookie("6lyrics_fdname") == null) {
        fsel.value = "courier new, courier, monospace";
        slly.style.fontFamily = "courier new, courier, monospace";
    }
    else {
        rcoc = readCookie("6lyrics_fdname").toLowerCase().replace(/, /g, ",").replace(/,/g, ", ");
        fsel.value = rcoc;
        slly.style.fontFamily = rcoc;
    }
    // Setting Font Size
    if (readCookie("6lyrics_fdsize") == null) {
        slly.style.fontSize = "12px";
    }
    else {
        slly.style.fontSize = readCookie("6lyrics_fdsize") + "px";
    }
}

// Change font size when user click the buttons.
function change_fontsize(ind) {
    if (ind=="1"){inc=2;}else{inc=-2;}
    var slly = $("slly"); 
    fcur = (slly.style.fontSize) ? parseInt(slly.style.fontSize.replace("px", "")) : 12;
    fcur += inc;
    if (fcur < 72 && fcur > 2) { slly.style.fontSize = fcur + "px";}

}

// Add the Bar buttons in the top of the lyrics in the html page
function Add_lyrics_bar(art,title) {
    var iurl = "http://www.6lyrics.com/music/" + art + "/lyrics/" + title + ".aspx";
    var itxt = "Add this lyric to ";
    document.write('<table><tr><td class="lt_l"></td>');
    document.write('<td class="lt_m"><img src="/img/a_min.gif" alt="Decrease font size" style="cursor:pointer;" onclick="change_fontsize(\'0\'); Save_Cur()" /></td>');
    document.write('<td class="lt_m"><img src="/img/a_plus.gif" alt="Increase font size" style="cursor:pointer;" onclick="change_fontsize(\'1\'); Save_Cur()" /></td>');
    document.write("<td class=\"lt_m\"><select id=\"fontsel\" class=\"lt_msel\" onchange=\"$('slly').style.fontFamily = this.value; Save_Cur()\"><option value=\"times new roman, times, serif\">Times</option><option value=\"arial, helvetica, sans-serif\">Arial</option><option value=\"courier new, courier, monospace\">Courier New</option><option value=\"tahoma, geneva, sans-serif\">Tahoma</option><option value=\"verdana, geneva, sans-serif\">Verdana</option><option value=\"garamond, serif\">Garamond</option><option value=\"georgia, serif\">Georgia</option><option value=\"palatino linotype, book antiqua, palatino, serif\">Palatino</option></select></td>");
    document.write('<td class="lt_m">Save/Share : </td>');
    document.write('<td class="lt_m"><a target="_blank" href="http://del.icio.us/post?url=' + iurl + '" alt="' + itxt + 'delicious" rel="nofollow"><img alt="' + itxt + 'delicious" src="/img/sites/delicious.gif" width="14" height="14"/></a></td>');
    document.write('<td class="lt_m"><a target="_blank" href="http://digg.com/submit?phase=2&amp;url=' + iurl + '" alt="' + itxt + 'digg" rel="nofollow"><img alt="' + itxt + 'digg" src="/img/sites/digg.gif" width="14" height="14" /></a></td>');
    document.write('<td class="lt_m"><a target="_blank" href="http://reddit.com/submit?url=' + iurl + '" alt="' + itxt + 'reddit" rel="nofollow"><img alt="' + itxt + 'reddit" src="/img/sites/reddit.gif" width="14" height="14" /></a></td>');
    document.write('<td class="lt_m"><a target="_blank" href="http://www.stumbleupon.com/submit?url=' + iurl + '&=Music" alt="' + itxt + 'Stumbleupon" rel="nofollow"><img alt="' + itxt + 'Stumbleupon" src="/img/sites/stumble.gif" width="14" height="14" /></a></td>');
    document.write('<td class="lt_m"><a target="_blank" href="http://cgi.fark.com/cgi/fark/edit.pl?new_url=' + iurl + '&amp;linktype=Misc" alt="' + itxt + 'fark" rel="nofollow"><img alt="' + itxt + 'fark" src="/img/sites/fark.gif" width="14" height="14" /></a></td>');
    document.write('<td class="lt_r"></td></tr></table>');
}

// Add the links in the buttom of the page
function Add_Links() {
    document.write('<table width="700px"><tr><td align="left"><a href="/advertising.aspx" title="Advertise on 6Lyrics">Advertise</a> - <a href="/widgets.aspx" title="Our widgets">Widgets</a> - <a href="/lyrics_submission.aspx" title="Submit lyrics">Submit Lyrics</a> - <a href="/mp3downloads.aspx" title="Download free mp3 songs & albums">Download Mp3 Music</a> - <a href="/partners.aspx" title="Partners & Links">Links</a></td><td align="right" class="minilink"><a href="/contact_us.aspx" title="Contact-Us">Contact-Us</a> | <a href="/info/terms.aspx" title="Terms of use">Terms of use</a> | <a href="/info/privacy_policy.aspx" title="Privacy Policy">Privacy Policy</a> | <a href="/info/dmca.aspx" title="DMCA">DMCA</a></td></tr></table>');
}