﻿//load main page contents 

// initialization of vars 
var totalPrice = 0;
var noDataFoundMsg = '<span class="frmerror">No Data Found</span>';
var noTrackFound = 'No record found.'
var currSection = 'genres';
var GridAltColor1= '#E6E6E6';  // This is for repeating background color 
var GridAltColor2= '#FFFFFF';  // This is for repeating background color


$(document).ready(function() {
    loadDefaultVars(); // loading default variable values 
    loadHomePage();
    loadCart();    // loading intial cart 
    loadLeftImage();    // loading default temperory image 

    });


function loadDefaultVars() {

}
function loadLeftImage() {
    $("#divLeftLinks").html('<img src="images/concert_gratuit_du_dj_aqueel.jpg" width=100%>');
}
function loadHomePage(LoadResults) {
    // sequence 
    // load template -> load individual modules inside 

    // loading template 

    $.ajax({ type: "GET", url: "templates/main.htm", data: "{}", contentType: "application/json; charset=utf-8", // dataType: "json",
        success: function(msg) {
            $("#ContentArea").html(msg); // load the page template 
          
            //load top order advertisements 
            loadMiddleSlider(); 
           
                        
            loadSlider();
            
            
            //load  Featured Charts
            $.ajax({ type: "GET", url: "common/FeaturedCharts.aspx?recordsPerPage=5", contentType: "application/json; charset=utf-8", // dataType: "json",
                success: function(msg) { $("#divFeaturedCharts").html(msg); FetrChartsmakeTable(); }
            });
            //load  Top Downloads
            $.ajax({ type: "GET", url: "common/TopDownloads.aspx?type=recent", contentType: "application/json; charset=utf-8", // dataType: "json",
                success: function(msg) { $("#divTopDownload").html(msg); LoadTopDownLoads(); }
            });
        }
    });
    $("#message_box").hide();
    currSection = 'home';
}


// Common function to load tracks within main area
function loadTracks(type, id) {
    $.ajax({ type: "GET", url: "common/ListTracks.aspx?type=" + type + "&id=" + id, data: "{}", contentType: "application/json; charset=utf-8", // dataType: "json",
        success: function(msg)
        { $("#middleContent").html(msg); listTracks(); }
    });
}  

function loadFeatureCharts() {
    $.ajax({ type: "GET", url: "common/FeaturedCharts.aspx?recordsPerPage=5", contentType: "application/json; charset=utf-8", // dataType: "json",
        success: function(msg) { $("#divFeaturedCharts").html(msg); FetrChartsmakeTable(); }
    });

    $("#tdFetChrts").attr("class", "onLink");
    $("#tdFetRls").attr("class", "offLink");
}

function loadFeatureReleases() {
    $.ajax({ type: "GET", url: "common/FeaturedReleases.aspx?recordsPerPage=5", contentType: "application/json; charset=utf-8", // dataType: "json",
        success: function(msg) { $("#divFeaturedCharts").html(msg); fetchRmakeTable(); }
    });
    $("#tdFetRls").attr("class", "onLink");
    $("#tdFetChrts").attr("class", "offLink");
}


// -----------------------------  load for genres  ----------------------------- //

function loadGenresDefaultRightPage() {
    /* load left side content with 
    1. Categories related to Genres
    // load right hand side content with 
    1. All the categories with latest 3 songs related to particular category 
    */

    // load all controls to page if section is not genres
    if (currSection != 'genres') {
        loadHomePage();
    }


    $.ajax({ type: "GET", url: "Leftlinks/lGenres.aspx", contentType: "application/json; charset=utf-8", // dataType: "json",
        success: function(msg) { $("#divLeftLinks").html(msg); }
    });
    // change the breadcrum navigation
    $("#divNavgn1").html(" Genres"); $("#divNavgn2").html(""); $("#divNavgn3").html("");
    $("#imgHdr").attr('src', 'images/genresHdr.jpg');
    currSection = 'genres';

}
function loadSingleGenres(id, cat) {
    // load the subcategories 
    $.ajax({ type: "GET", url: "Leftlinks/lGenres.aspx?id=" + id, contentType: "application/json; charset=utf-8", // dataType: "json",
        success: function(msg) { $("#divLeftLinks").html(msg); $("#GenresCatText").html(cat); }
    });
    // change the breadcrum navigation
    $("#divNavgn2").html(" >> " + cat); $("#divNavgn3").html("");

}

function loadSingleSubGenres(id, cat) {

    // check if center div is present otherwise load the entire page
    if (document.getElementById("divTopDownload") == null)
    { loadHomePage(LoadResults(id, cat)); }
    else
    {LoadResults(id, cat);}
   
}

function LoadResults(id, cat)
{
 
    // load subcategory releases to right hand side page 
    $.ajax({ type: "GET", url: "templates/genres.htm", data: "{}", contentType: "application/json; charset=utf-8", // dataType: "json",
        success: function(msg) {
            $.ajax({ type: "GET", url: "Genres/gRelease.aspx?id=" + id, contentType: "application/json; charset=utf-8", // dataType: "json",
                success: function(msg) { $("#middleContent").html(msg); gnrMmakeTable(); }
            });
        }
    });
    // change the breadcrum navigation
    $("#divNavgn3").html(" >> " + cat);
}

// -----------------------------  load for Charts  ----------------------------- //

function loadChartsDefaultRightPage() {
    /* load left side content with 
    1. Categories related to charts 
    // load right hand side content with 
    1. All the categories with latest 3 songs related to particular category 
    */

    // load left links page
    $.ajax({ type: "GET", url: "Leftlinks/lCharts.aspx", contentType: "application/json; charset=utf-8", // dataType: "json",
        success: function(msg) { $("#divLeftLinks").html(msg); }
    });
    $.ajax({ type: "GET", url: "Charts/Default.aspx", contentType: "application/json; charset=utf-8", // dataType: "json",
        success: function(msg) {
            $("#ContentArea").html(msg); ChrtsmakeTable(); formPager();

            $.ajax({ type: "GET", url: "common/FeaturedCharts.aspx?recordsPerPage=7", contentType: "application/json; charset=utf-8", // dataType: "json",
                success: function(msg) { $("#divFtrCharts").html(msg); FetrChartsmakeTable(); loadFtrChrtHdr(); }
            });

        }
    });
    $("#divNavgn1").html(" Charts"); $("#divNavgn2").html(""); $("#divNavgn3").html("");
    $("#imgHdr").attr('src', 'images/chartsHdr.jpg');

    currSection = 'charts';
}


function loadSingleCharts(letter) {

 $.ajax({ type: "GET", url: "Charts/Default.aspx", contentType: "application/json; charset=utf-8", // dataType: "json",
        success: function(msg) {
            $("#ContentArea").html(msg); ChrtsmakeTable(); formPager();

            $.ajax({ type: "GET", url: "common/FeaturedCharts.aspx?recordsPerPage=7", contentType: "application/json; charset=utf-8", // dataType: "json",
                success: function(msg) { $("#divFtrCharts").html(msg); FetrChartsmakeTable(); loadFtrChrtHdr(); }
            });

        }
    });
    
    // load the subcategories 
    $.ajax({
        type: "GET",
        url: "Charts/default.aspx?letter=" + letter,
        //data: "{'letter':'" + letter + "'}",
        contentType: "application/json; charset=utf-8",
        success: function(msg) {
            $("#ContentArea").html(msg); ChrtsmakeTable(); formPager();
        }
    });

    $("#divNavgn2").html(" >> " + letter.toUpperCase()); $("#divNavgn3").html("");

}

function loadChartsDetails(ID) 
{
    $.ajax({
        type: "GET",
        url: "Charts/ChartTracks.aspx?id=" + ID,
        //data: "{'letter':'" + letter + "'}",
        contentType: "application/json; charset=utf-8",
        success: function(msg) {
            $("#ContentArea").html(msg); loadChartTracks();
        }
    });
    
    // check if left panel related to chart is loaded
    if (currSection != 'charts') {
        // load left links page
        $.ajax({ type: "GET", url: "Leftlinks/lCharts.aspx", contentType: "application/json; charset=utf-8", // dataType: "json",
            success: function(msg) { $("#divLeftLinks").html(msg); }
        });
    }
    
    $("#divNavgn1").html(" Charts"); $("#divNavgn2").html(""); $("#divNavgn3").html("");
}

// -----------------------------  load for Artists  ----------------------------- //


function loadArtistsDefaultPage() {
    $.ajax({ type: "GET", url: "Leftlinks/lArtists.aspx", contentType: "application/json; charset=utf-8", // dataType: "json",
        success: function(msg) { $("#divLeftLinks").html(msg); }
    });
      $("#imgHdr").attr('src', 'images/artistsHdr.jpg');
}
function loadArtistsDefaultRightPage() {
    /* load left side content with 
    1. Categories related to charts 
    // load right hand side content with 
    1. All the categories with latest 3 songs related to particular category 
    */

    // load left links page
    $.ajax({ type: "GET", url: "Leftlinks/lArtists.aspx", contentType: "application/json; charset=utf-8", // dataType: "json",
        success: function(msg) {
            $("#divLeftLinks").html(msg);

            $.ajax({ type: "GET", url: "Artists/Default.aspx", contentType: "application/json; charset=utf-8", // dataType: "json",
                success: function(msg) {
                    $("#ContentArea").html(msg); ArtsmakeTable(); formPager();

                    $.ajax({ type: "GET", url: "common/FeaturedArtists.aspx?recordsPerPage=7", contentType: "application/json; charset=utf-8", // dataType: "json",
                        success: function(msg) { $("#divFtrArtistss").html(msg); FetrArtistsmakeTable(); loadFtrArtsHdr(); }
                    });

                }
            });
        }
    });
    $("#imgHdr").attr('src', 'images/artistsHdr.jpg');
    currSection = 'artists';
    $("#divNavgn1").html(" Artists"); $("#divNavgn2").html(""); $("#divNavgn3").html("");
}

function loadSingleArtists(letter) {

$.ajax({ type: "GET", url: "Artists/Default.aspx", contentType: "application/json; charset=utf-8", // dataType: "json",
                success: function(msg) {
                    $("#ContentArea").html(msg); ArtsmakeTable(); formPager();
                    
                     $.ajax({ type: "GET", url: "common/FeaturedArtists.aspx?recordsPerPage=7", contentType: "application/json; charset=utf-8", // dataType: "json",
                        success: function(msg) { $("#divFtrArtistss").html(msg); FetrArtistsmakeTable(); loadFtrArtsHdr(); }
                    });
    }
 });

    // load the subcategories
    $.ajax({
        type: "GET",
        url: "Artists/default.aspx?itemOnly=1&letter=" + letter,
        //data: "{'letter':'" + letter + "'}",
        contentType: "application/json; charset=utf-8",
        success: function(msg) {
            $("#divData").html(msg);
            ArtsmakeTable(); formPager(); PgrArts(1);
        }
    });
}
function loadArtistsDetails(id) {

    // load left links page if user is in other section than artists
    if (currSection != 'artists') {
        loadArtistsDefaultPage();
    }

    // load the artists details 
    $.ajax({
        type: "GET",
        url: "Artists/ArtistsDetails.aspx?id=" + id,
        //data: "{'letter':'" + letter + "'}",
        contentType: "application/json; charset=utf-8",
        success: function(msg) {
            $("#ContentArea").html(msg);
            loadArtistsTracks();
        }
    });
}

// -----------------------------  load for  Labels  ----------------------------- //

function loadLablesDefaultRightPage() {
    /* load left side content with 
    1. Categories related to charts 
    // load right hand side content with 
    1. All the categories with latest 3 songs related to particular category 
    */

    // load left links page
    $.ajax({ type: "GET", url: "Leftlinks/lLables.aspx", contentType: "application/json; charset=utf-8", // dataType: "json",
        success: function(msg) {
            $("#divLeftLinks").html(msg);

            $.ajax({ type: "GET", url: "labels/Default.aspx?recordsPerPage=7", contentType: "application/json; charset=utf-8", // dataType: "json",
                success: function(msg) {
                    $("#ContentArea").html(msg); LablsmakeTable(); formPager();

                    $.ajax({ type: "GET", url: "common/FeaturedReleases.aspx?recordsPerPage=7", contentType: "application/json; charset=utf-8", // dataType: "json",
                        success: function(msg) { $("#divFtrCharts").html(msg); fetchRmakeTable(); loadFtrChrtHdr(); }
                    });
                }
            });

        }
    });
    $("#imgHdr").attr('src', 'images/labelsHdr.jpg');
    currSection = 'labels';
}

function loadSingleLabel(letter) {
    // load the subcategories 
    $.ajax({
        type: "GET",
        url: "Labels/default.aspx?letter=" + letter,
        //data: "{'letter':'" + letter + "'}",
        contentType: "application/json; charset=utf-8",
        success: function(msg) {
            $("#middleContent").html(msg); LabelsmakeTable();
        }
    });
}

// -----------------------------  load for  Tracks  ----------------------------- //

function TrackDetails(id) {
    $.ajax({ type: "GET", url: "tracks/Default.aspx?id=" + id, contentType: "application/json; charset=utf-8", // dataType: "json",
        success: function(msg) { $("#ContentArea").html(msg); TrcksmakeTable(); }
    });
    // load left side with the tracks related to label of track selected.
    if (currSection != 'tracks') {
        $.ajax({ type: "GET", url: "Leftlinks/lTracks.aspx", contentType: "application/json; charset=utf-8", // dataType: "json",
            success: function(msg) { $("#divLeftLinks").html(msg); TrksmakeTable(); }
        });
    }
    currSection = 'tracks';
}


// -----------------------------  CART  functions   ----------------------------- //


function AddTrackToCart(id, price) {
    if ($.cookie('cartTracks') != null) {
        var cookItems = new Array();
        cookItems = $.cookie('cartTracks').split(",");
        for (i = 0; i < cookItems.length; i++) {
            if (id == cookItems[i]) {
                alert("The item is already in your cart");
                return;
            }
        }
    }

    var cartId = $.cookie('cartTracks') + "," + id
    $.cookie('cartTracks', cartId);
    if ($.cookie('cartCounter') == null) {
        $.cookie('cartCounter', 1);
    }
    else {
        var cook = parseInt($.cookie('cartCounter')) + 1;
        $.cookie('cartCounter', cook);
    }
    if ($.cookie('totalPrice') != null) {
        $.cookie('totalPrice', (parseFloat($.cookie('totalPrice')) + parseFloat(price)).toFixed(2));
    }
    else {
        $.cookie('totalPrice', parseFloat(price));
    }



    // updating user about added
    loadCart(); // update    
    $("#message_box").html("Item has beed added to cart.");
    $("#message_box").show();
    $("#message_box").fadeOut(5000);


}

function RemoveFromCart(id, Objprice, type) {
    if (type == "tracks") {
        $("#Trackrow" + id).remove(); // removing from front end
        var cartId = $.cookie('cartTracks').split(",");
        var finalStrCookiesValue = "";

        for (i = 0; i < cartId.length; i++) {
            if (id != cartId[i])
                finalStrCookiesValue = finalStrCookiesValue + "," + cartId[i];
        }

        $.cookie('cartTracks', finalStrCookiesValue);
       
    }

    if (type == "charts") {
        var cartId = $.cookie('cartCharts').split(",");
        var finalStrCookiesValue = "";

        for (i = 0; i < cartId.length; i++) {
            if (id != cartId[i])
                finalStrCookiesValue = finalStrCookiesValue + "," + cartId[i];
        }

        $.cookie('cartCharts', finalStrCookiesValue);
        $("#Chartrow" + id).remove(); // removing from front end
    }
    if (type == "lebels") {
        var cartId = $.cookie('cartLabels').split(",");
        var finalStrCookiesValue = "";

        for (i = 0; i < cartId.length; i++) {
            if (id != cartId[i])
                finalStrCookiesValue = finalStrCookiesValue + "," + cartId[i];
        }

        $.cookie('cartLabels', finalStrCookiesValue);
        $("#Labelsrow" + id).remove(); // removing from front end
    }

    $.cookie('cartCounter', parseInt(parseInt($.cookie('cartCounter')) - 1));
    $.cookie('totalPrice', (parseFloat($.cookie('totalPrice')) - parseFloat(Objprice)).toFixed(2));


    // updating user about added
    loadCart(); // update users cart
    $("#message_box").html("Item has beed removed from your  cart.");
    $("#cartTotal").html($.cookie('totalPrice')); // to update from cart 
    $("#message_box").show();
    $("#message_box").fadeOut(5000);
}

function loadCart() {
    if ($.cookie('cartCounter') != null) {
        var strLnk1 = '<a class="thickbox" href="pages/pViewCart.aspx?height=450&width=650&modal=true"><span class="description">';
        var strLnk2 = '</span></a>';
        $("#cartInfo").html($.cookie('cartCounter') + " items [$" + $.cookie('totalPrice') + "]" );
    }
}
function showCart() {
    // load cart template 
    /* $.ajax({ type: "GET",url: "templates/cart.htm",data: "{}",contentType: "application/json; charset=utf-8",// dataType: "json",
    success: function(msg) 
    {
    $("#middleContent").html(msg); // load the page template ContentArea
    $.ajax({ type: "GET",url: "pages/pViewCart.aspx",contentType: "application/json; charset=utf-8",// dataType: "json",
    success: function(msg) { $("#cartAreaContent").html(msg); strTracks(); } //
    });
    }
    });     */
    $.ajax({ type: "GET", url: "pages/pViewCart.aspx", contentType: "application/json; charset=utf-8", // dataType: "json",
        success: function(msg) { $("#middleContent").html(msg); strTracks(); } //
    });
}

function AddChartToCart(id, price) {
    if ($.cookie('cartCharts') != null) {
        var cookItems = new Array();
        cookItems = $.cookie('cartCharts').split(",");
        for (i = 0; i < cookItems.length; i++) {
            if (id == cookItems[i]) {
                alert("Item is already added");
                return;
            }
        }
    }

    var cartId = $.cookie('cartCharts') + "," + id
    $.cookie('cartCharts', cartId);
    if ($.cookie('cartCounter') == null) {
        $.cookie('cartCounter', 1);
    }
    else {
        var cook = parseInt($.cookie('cartCounter')) + 1;
        $.cookie('cartCounter', cook);
    }
    if ($.cookie('totalPrice') != null) {
        $.cookie('totalPrice', (parseFloat($.cookie('totalPrice')) + parseFloat(price)).toFixed(2));
    }
    else {
        $.cookie('totalPrice', parseFloat(price));
    }



    // updating user about added
    loadCart(); // update    
    $("#message_box").html("Item has beed added to cart.");
    $("#message_box").show();
    $("#message_box").fadeOut(5000);

}

function AddLabelToCart(id, price) {
    if ($.cookie('cartLabels') != null) {
        var cookItems = new Array();
        cookItems = $.cookie('cartLabels').split(",");
        for (i = 0; i < cookItems.length; i++) {
            if (id == cookItems[i]) {
                alert("Item is already added");
                return;
            }
        }
    }

    var cartId = $.cookie('cartLabels') + "," + id
    $.cookie('cartLabels', cartId);
    if ($.cookie('cartCounter') == null) {
        $.cookie('cartCounter', 1);
    }
    else {
        var cook = parseInt($.cookie('cartCounter')) + 1;
        $.cookie('cartCounter', cook);
    }
    if ($.cookie('totalPrice') != null) {
        $.cookie('totalPrice', (parseFloat($.cookie('totalPrice')) + parseFloat(price)).toFixed(2));
    }
    else {
        $.cookie('totalPrice', parseFloat(price));
    }



    // updating user about added
    loadCart(); // update    
    $("#message_box").html("Item has beed added to cart.");
    $("#message_box").show();
    $("#message_box").fadeOut(5000);

}


/*function Center()
{
var width = document.documentElement.clientWidth + document.documentElement.scrollLeft;
var height = document.documentElement.clientHeight + document.documentElement.scrollTop;
            
var top = ((height + document.documentElement.scrollTop) / 2) - ($('#centerdiv').height() / 2) +"px";
            
var left = (width / 2) - ($('#centerdiv').width() / 2) +"px";
$('#message_box').css({position:"absolute",top:top,left:left});
}*/




// -----------------------------  Top Downloads  functions   ----------------------------- //
function loadViewallTopDownloads() {
    $.ajax({ type: "GET", url: "common/TopDownloads.aspx?type=recent", contentType: "application/json; charset=utf-8", // dataType: "json",
        success: function(msg) { $("#divTopDownload").html(msg); LoadTopDownLoads(); }
    });
    $("#tdViewAll").attr("class", "onLink");
    $("#tdClsc").attr("class", "offLink");
}


function loadClassicsTopDownloads() {
    $.ajax({ type: "GET", url: "common/TopDownloads.aspx?type=classics", contentType: "application/json; charset=utf-8", // dataType: "json",
        success: function(msg) { $("#divTopDownload").html(msg); LoadTopDownLoads(); }
    });
    $("#tdViewAll").attr("class", "offLink");
    $("#tdClsc").attr("class", "onLink");
}


// -----------------------------  Common  functions   ----------------------------- //

function toggleOut(me) {
    $(me).attr("class", "offMLink");
}

function toggleOver(me) {
    $(me).attr("class", "onMLink");
}

function validateEmail(src) 
        {
            
            var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
            return regex.test(src);
        }




// -----------------------------   SEARCH  ----------------------------- //

function SearchMain() {
    /* load left side content with 
    1. Categories related to charts 
    // load right hand side content with 
    1. All the categories with latest 3 songs related to particular category 
    */

    // load left links page
    var leftPageToLoad,sectionType,searchText;
    
    sectionType= $("#drpSection").val();
    searchText= $("#txtSearch").val();
    
   /* if (sectionType=="genres") leftPageToLoad="Leftlinks/lGenres.aspx";
    if (sectionType=="charts") leftPageToLoad="Leftlinks/lCharts.aspx";
    if (sectionType=="labels") leftPageToLoad="Leftlinks/lLables.aspx";
    if (sectionType=="artists") leftPageToLoad="Leftlinks/lArtists.aspx";
    
    
    
    $.ajax({ type: "GET", url: leftPageToLoad , contentType: "application/json; charset=utf-8",  
        success: function(msg) { $("#divLeftLinks").html(msg); }
    });*/
    if(sectionType=="genres" || sectionType=="tracks")
        {
            $.ajax({ type: "GET", url: "search/Default.aspx?type="+sectionType+"&searchText="+ searchText, contentType: "application/json; charset=utf-8",  
                success: function(msg) {
                    $("#ContentArea").html(msg); showResult();//ChrtsmakeTable(); formPager();
                }
         
            });
            /*$("#imgHdr").attr('src', 'images/genresHdr.jpg');
            $.ajax({ type: "GET", url: "Leftlinks/lGenres.aspx", contentType: "application/json; charset=utf-8", // dataType: "json",
                    success: function(msg) { $("#divLeftLinks").html(msg); }
                });*/
        }
    if(sectionType=="charts")
        {
            $.ajax({ type: "GET", url: "Charts/Default.aspx?type="+sectionType+"&searchText="+ searchText, contentType: "application/json; charset=utf-8", // dataType: "json",
                success: function(msg) {
                    $("#ContentArea").html(msg); ChrtsmakeTable(); formPager();

                    $.ajax({ type: "GET", url: "common/FeaturedCharts.aspx?recordsPerPage=7", contentType: "application/json; charset=utf-8", // dataType: "json",
                        success: function(msg) { $("#divFtrCharts").html(msg); FetrChartsmakeTable(); loadFtrChrtHdr(); }
                    });
                }
            });
            //$("#imgHdr").attr('src', 'images/chartsHdr.jpg');
        }
        
    if(sectionType=="artists")
        {
            
            $.ajax({ type: "GET", url: "Artists/Default.aspx?type="+sectionType+"&searchText="+ searchText, contentType: "application/json; charset=utf-8", // dataType: "json",
                success: function(msg) {
                    $("#ContentArea").html(msg); ArtsmakeTable(); formPager();

                    $.ajax({ type: "GET", url: "common/FeaturedArtists.aspx?recordsPerPage=7", contentType: "application/json; charset=utf-8", // dataType: "json",
                        success: function(msg) { $("#divFtrArtistss").html(msg); FetrArtistsmakeTable(); loadFtrArtsHdr(); }
                    });

                }
            });
           // $("#imgHdr").attr('src', 'images/artistsHdr.jpg');
        }
    $("#divNavgn1").html(" Search"); $("#divNavgn2").html(""); $("#divNavgn3").html("");


    $("#divLeftLinks").html('');

    //currSection = sectionType;

    currSection = 'search';
}




    
    