function getHTTPObject() {
    var xmlhttp = false;
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e) {
            try {
                xmlhttp = new XMLHttpRequest();
            }
            catch (e) {
                alert(e);
            }
        }
    }
    return xmlhttp;
}

var http = getHTTPObject(); //instantiate the object
var objJson;
var $mp3;

function ParseJson() {
    if (http != null) {
        try {
            http.open("GET", 'Songs.html', false);
            http.setRequestHeader("Content-Type", "application/json; charset=utf-8");

            var parameters = null;

            http.send(parameters);

            if (http.readyState == 4) {
                objJson = eval("(" + http.responseText + ")");
            }
        }
        catch (e) {
            alert(e);
        }

    }

    var html = "";

    if (objJson.songdetail.length > 0) {
        html = "<div class=\"playlist-container\"><ul>";
        var li = "";

        for (var i = 0; i < objJson.songdetail.length; ++i) {
            li += "<li> <a title=\"Play " + objJson.songdetail[i].name + "\"   onclick=\"playR('" + objJson.songdetail[i].filepath + "', '" + objJson.songdetail[i].image + "', '" + objJson.songdetail[i].name + "', '" + objJson.songdetail[i].id + "', '" + objJson.songdetail[i].artist + "')\">" + objJson.songdetail[i].name + "</a></li>";
        }
        html = html + li + "</ul></div>";

        var first = objJson.songdetail[1].filepath + "|" + objJson.songdetail[1].image + "|" + objJson.songdetail[1].name + "|" + objJson.songdetail[1].id + "|" + objJson.songdetail[1].artist;
        jQuery("#first").val(first);
    }

    if (html != null && html.length > 0) {
        jQuery("#playlistr").html(html);
    }
}

function SetCurrentSong(index) {

    jQuery('#first').val(objJson.songdetail[index].filepath + "|" + objJson.songdetail[index].image + "|" + objJson.songdetail[index].name + "|" + objJson.songdetail[index].id + "|" + objJson.songdetail[index].artist);
    bindnexpreviousR(objJson.songdetail[index].id);
}

function bindnexpreviousR(id) {
    var intKeyLength = objJson.songdetail.length;

    var strAttribute = "";

    if (intKeyLength > 0) {

        for (i = 0; i < intKeyLength; i++) {

            strAttribute = objJson.songdetail[i].id;
            if (strAttribute == id) {
                jQuery('#first').val(objJson.songdetail[i].filepath + "|" + objJson.songdetail[i].image + "|" + objJson.songdetail[i].name + "|" + objJson.songdetail[i].id + "|" + objJson.songdetail[i].artist);
                if (i > 0) {
                    jQuery('#previous').val(objJson.songdetail[i - 1].filepath + "|" + objJson.songdetail[i - 1].image + "|" + objJson.songdetail[i - 1].name + "|" + objJson.songdetail[i - 1].id + "|" + objJson.songdetail[i - 1].artist);
                }
                if (i + 1 < intKeyLength) {
                    jQuery('#next').val(objJson.songdetail[i + 1].filepath + "|" + objJson.songdetail[i + 1].image + "|" + objJson.songdetail[i + 1].name + "|" + objJson.songdetail[i + 1].id + "|" + objJson.songdetail[i + 1].artist);
                }
                break;
            }
        }
    }
}

//show & hide the playlist
function toggle() {

    var obj = jQuery(".control").offset();
    if (obj.top < jQuery("#playlistr").height()) {
        obj.top = obj.top + jQuery(".control").height();
    }
    else {
        obj.top = obj.top - jQuery("#playlistr").height();
    }

    jQuery("#playlistr").attr("style", "top:" + obj.top + "px;left:" + obj.left + "px");


    if (jQuery("#seeAll").attr("title") == "show") {
        //jQuery("#listimg").attr("src", "images/arrow-down.png");
        jQuery("#seeAll").attr("title", "hide");
        jQuery("#playlistr").fadeIn("slow");
    }
    else {
        //jQuery("#listimg").attr("src", "images/arrow-up.png");
        jQuery("#seeAll").attr("title", "show");
        jQuery("#playlistr").fadeOut("slow");
    }
}


function play(file, image, name, id, artist, autoplays, autopauses) {
    $mp3 = jQuery("#mysong").jmp3({
        filepath: file,
        backcolor: "767779",
        forecolor: "FFFFFF",
        width: 256,
        showdownload: "false",
        autoplay: autoplays.toString(),
        autopause: autopauses.toString(),
        repeat: "yes"
    });
}

function playR(file, image, name, id, artist) {
    if (jQuery("#mysong").find("object") != null) {
        jQuery("#mysong").html("");
        jQuery(".song-img").find("img").fadeOut("fast");
        jQuery("#dvSongName").html("");
    }

    bindnexpreviousR(id);

    play(file, image, name, id, artist, "true", "false");

    jQuery(".song-img").fadeIn("slow");
    jQuery(".song-img").find("img").fadeIn("slow");
    jQuery(".song-img").find("img").attr("src", image);
    jQuery(".song-img").find("img").attr("alt", name);
    jQuery(".songname").text((name.length > 43) ? name.substring(0, 40) + '...' : name);
    //var artist = (artist.toString().length > 43) ? (artist.substring(0, 40) + '...') : artist;
    //jQuery(".artistname").text("Artist: " + artist.toString());

    jQuery("#listimg").attr("src", "images/arrow-up.png");
    jQuery("#listimg").attr("alt", "up");
    jQuery("#playlistr").fadeOut("slow");


}

function playRepeating(file, image, name, id, artist) {
    if (jQuery("#mysong").find("object") != null) {
        jQuery("#mysong").html("");
        jQuery(".song-img").find("img").fadeOut("fast");
    }

    $mp3 = jQuery("#mysong").jmp3({
        filepath: file,
        backcolor: "767779",
        forecolor: "FFFFFF",
        width: 256,
        showdownload: "false"
    });

    jQuery(".song-img").fadeIn("slow");
    jQuery(".song-img").find("img").fadeIn("slow");
    jQuery(".song-img").find("img").attr("src", image);
    jQuery(".song-img").find("img").attr("alt", name);

    jQuery(".songname").text((name.length > 43) ? name.substring(0, 40) + '...' : name);

    //var artist = (artist.toString().length > 43) ? (artist.substring(0, 40) + '...') : artist;
    //jQuery(".artistname").text("Artist: " + artist.toString());
}

//This function calls up when first song play at load
function playSingle(file, image, name, id, artist) {

    if (jQuery("#mysong").find("object") != null) {
        jQuery("#mysong").html("");
        jQuery(".song-img").find("img").fadeOut("fast");
    }

    $mp3 = jQuery("#mysong").jmp3({
        filepath: file,
        backcolor: "767779",
        forecolor: "FFFFFF",
        width: 256,
        showdownload: "false"
    });

    jQuery(".song-img").fadeIn("slow");
    jQuery(".song-img").find("img").fadeIn("slow");
    jQuery(".song-img").find("img").attr("src", image);
    jQuery(".song-img").find("img").attr("alt", name);

    jQuery(".songname").text((name.length > 43) ? name.substring(0, 40) + '...' : name);

    //var artist = (artist.toString().length > 43) ? (artist.substring(0, 40) + '...') : artist;
    //jQuery(".artistname").text("Artist: " + artist.toString());

    //        jQuery("#imgnext").click(function() {
    //            var next = jQuery("#next").val();
    //            if (next != null && next.toString().length > 0) {
    //                var ar = next.toString().split('|');
    //                if (ar.length > 2) {
    //                    jQuery('#imgplay').attr("src","images/pause-button.png");
    //                    playR(ar[0],ar[1],ar[2],ar[3],ar[4]);   
    //                }
    //            }
    //        });

    //         
    //        jQuery("#imgprevious").click(function() {
    //            var previous = jQuery("#previous").val();
    //            if (previous != null && previous.toString().length > 0) {
    //                var ar = previous.toString().split('|');
    //                if (ar.length > 2) {
    //                    jQuery('#imgplay').attr("src","images/pause-button.png");
    //                    playR(ar[0],ar[1],ar[2],ar[3],ar[4]);   
    //                }
    //            }
    //        });
}

//on load
jQuery(document).ready(function() {
    jQuery('#imgprevious').hover(function() {
        $(this).attr("src", "images/rewindbutton-hover.png");
    }, function() {
        $(this).attr("src", "images/rewindbutton.png");
    });

    jQuery('#imgnext').hover(function() {
        $(this).attr("src", "images/fwd-button-hover.png");
    }, function() {
        $(this).attr("src", "images/fwd-button.png");
    });

    jQuery('#imgplay').hover(function() {
        if ($(this).attr("src") == "images/playButton.png") {
            $(this).attr("src", "images/playButtonOver.png");
        }
        if ($(this).attr("src") == "images/pause-button.png") {
            $(this).attr("src", "images/pause-button-hover.png");
        }

    }, function() {
        if ($(this).attr("src") == "images/playButtonOver.png") {
            $(this).attr("src", "images/playButton.png");
        }
        if ($(this).attr("src") == "images/pause-button-hover.png") {
            $(this).attr("src", "images/pause-button.png");
        }
    });


    ParseJson();

    jQuery("#imgnext").click(function() {
        var next = jQuery("#next").val();
        if (next != null && next.toString().length > 0) {
            var ar = next.toString().split('|');
            if (ar.length > 2) {
                jQuery('#imgplay').attr("src", "images/pause-button.png");
                playR(ar[0], ar[1], ar[2], ar[3], ar[4]);
            }
        }
    });


    jQuery("#imgprevious").click(function() {
        var previous = jQuery("#previous").val();
        if (previous != null && previous.toString().length > 0) {
            var ar = previous.toString().split('|');
            if (ar.length > 2) {
                jQuery('#imgplay').attr("src", "images/pause-button.png");
                playR(ar[0], ar[1], ar[2], ar[3], ar[4]);
            }
        }
    });

    if (jQuery.browser.msie == true) {
        PlayFirstSong();
    }
    else {
        //function for first time play
        window.setTimeout('checkIframeLoading();', 100);

    }

    jQuery('#imgplay').click(function() {
        if ($(this).attr("src") == "images/pause-button-hover.png" || $(this).attr("src") == "images/pause-button.png") {
            $(this).attr("src", "images/playButton.png");
            var first = jQuery("#first").val();
            if (first != null && first.toString().length > 0) {
                var ar = first.toString().split('|');
                if (ar.length > 2) {
                    playSingle(ar[0], ar[1], ar[2], ar[3], ar[4]);
                }
            }
        }
        else {
            $(this).attr("src", "images/pause-button.png");
            var first = jQuery("#first").val();
            if (first != null && first.toString().length > 0) {
                var ar = first.toString().split('|');
                if (ar.length > 2) {

                    playR(ar[0], ar[1], ar[2], ar[3], ar[4]);
                }
            }
        }

    });

    jQuery(".btn-buy").find("a").each(function() {

        this.href = "shop.html";
    });

});

function checkIframeLoading() {
    var iframe = document.getElementById('homeContent');
    var icontent;
    if (jQuery.browser.msie == true) {
        icontent = iframe.document;
        alert(iframe);
    }
    else {
        icontent = iframe.contentDocument.getElementById('isLoaded');
    }
    if (icontent != null) {
        if (icontent.value == 'true') {
            PlayFirstSong();
        }
    }
    else {
        window.setTimeout('checkIframeLoading();', 100);
    }
    return;
}
function PlayFirstSong() {
    var val = jQuery("#first").val();
    if (val != null && val.toString().length > 0) {
        var ar = val.toString().split('|');
        if (ar.length > 2) {
            bindnexpreviousR(ar[3]);
            jQuery('#imgplay').attr("src", "images/pause-button.png");
            playR(ar[0], ar[1], ar[2], ar[3], ar[4]);
        }
    }
}
//function opensharepopup()
//{
//    var ofset = jQuery("#imgshare").parent().parent().offset();
//    
//    if(ofset.top < jQuery(".sharepopup").height()){
//        ofset.top = ofset.top + jQuery("#imgshare").height();
//    }
//    else
//    {
//        ofset.top = ofset.top - jQuery(".sharepopup").height();
//    }
//    ofset.left = ofset.left -23;
//    
//    jQuery(".sharepopup").attr("style","top:" + ofset.top +"px;left:"+ofset.left+"px");
//    jQuery(".sharepopup").fadeIn("slow");
//}

//function OpenShareMailPopup()
//{
//    var ofset = jQuery("#outer").offset();
//    jQuery(".sharepopup").fadeOut("slow");
//    ofset.left = ofset.left+180;

//    jQuery(".sharesongpopup").attr("style","top:" + ofset.top +"px;left:"+ofset.left+"px");
//    jQuery(".sharesongpopup").fadeIn("slow");
//}

function opensharepopup() {
    var ofset = jQuery("#imgshare").parent().parent().offset();

    if (ofset.top < jQuery(".sharesongpopup").height()) {
        ofset.top = ofset.top + jQuery("#imgshare").height();
    }
    else {
        ofset.top = ofset.top - jQuery(".sharesongpopup").height();
    }
    ofset.left = ofset.left - 130;
    ofset.top = ofset.top - 6;

    jQuery(".sharesongpopup").attr("style", "top:" + ofset.top + "px;left:" + ofset.left + "px");
    jQuery(".sharesongpopup").fadeIn("slow");
}
