var extrasWidgetInit = 0; // flag
var consolidatedData;
var currencies = [];
function generateHomepage(){
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds();
    var time = h + ":" + m + ":" + s;
    $.ajax({type: "GET",
        url: data_url + "pages/home_1_json" + timestamp + data_ext,
        dataType: 'json',
        success: function(data) {

            consolidatedData = data;

            //headline
            if(is_live || is_awsdev){
                generateHeadlineData(consolidatedData.headlines_home, 'banner'); 
            }else{
                getHeadline('banner');
            }
            
            //just_in
            if(is_live || is_awsdev){
                generateJustIn(data.story_lists_just_in);
            }else{
                getJustIn();
            }
            if(is_live || is_awsdev){
                //row_thumbnail
                ROW_THUMBNAIL.generate_content(consolidatedData.row_thumbnails_top_picks, 'landscape');
            }else{
                ROW_THUMBNAIL.init('landscape');
            }     

            //featured content
            if(is_live || is_awsdev){
                generateFeaturedContent(consolidatedData.featured_lists_featured_content);
            }else{
                getFeaturedContent(); 
            }
           
            $.ajax({type: "GET",
                url: data_url + "pages/home_2_json" + timestamp + data_ext,
                dataType: 'json',
                success: function(data) {
                    
                    consolidatedData = data;

                    //at_a_glance
                    var at_a_glance_params = {section: "home", for_crosssell: false};
                    atAGlanceGenerateContent(consolidatedData.at_a_glance_lists_home, at_a_glance_params);

                    //tracking
                    var tracking_param = {trackingType: "tracking", type: "banner"};
                    trackingWidgetGenerate(consolidatedData.tracker_lists_home, tracking_param);

                    //carousel
                    var carousel_params = {carouselType: "latest_videos", type: "video", section: "0", subsection: "0", show: "0", isCarousel: "1", title: "LATEST VIDEOS", for_crosssell: false, container: "carousel_container"};
                    carouselGenerate(consolidatedData.carousel_lists_latest_videos, carousel_params);

                    //currency cal
                    var currency_calc_params = {type: "currcalc", item: 0, start: 0, title: "CURRENCY CALCULATOR"};
                    currCalculatorGenerate(data.extras_lists_currcal, currency_calc_params);

                    //trending
                    var trending_params = {trendingType: "trending", type: "all", limit: "9", title: "TRENDING", container: "trending_home"};
                    TRENDING.initConsolidate(data.trending_lists_trending, trending_params);

                    if(is_live || is_awsdev){
                        generateLivestream(data.livestream_home);
                    }else{
                        getLivestream();
                    }
                    var window_width = $(window).width();
                    $(window).resize(function() {
                        if (window_width >= 768 && extrasWidgetInit == 0) {
                            callExtrasWidget(consolidatedData);
                            extrasWidgetInit = 1;
                        }
                    }).resize();
                },
                // code to run if the request fails; the raw request and
                // status codes are passed to the function
                error: function(xhr, status, errorThrown) {
                    console.log('Consolidated Home : ' + errorThrown);
                    console.log("Status: " + status);
                    console.dir(xhr);
                }
            });
        },
        // code to run if the request fails; the raw request and
        // status codes are passed to the function
        error: function(xhr, status, errorThrown) {
            console.log('Consolidated Home : ' + errorThrown);
            console.log("Status: " + status);
            console.dir(xhr);
        }
    });
    if(onRefreshHomepage){
        /*google analytics send*/
        var params = {page_overriden: '/news/', page_title: document.title};
        CUSTOM_ANALYTICS.ga_send_event(params);
    }
//    setTimeout(generateHomepage, 900000);
}
$(document).ready(function() {
    
//    var at_a_glance_params = {section: "home", for_crosssell: false};
//    atAGlanceCall(at_a_glance_params);

//    var tracking_param = {trackingType: "tracking",type: "banner"};
//    trackingWidgetCall(tracking_param);



//    var trending_params = {trendingType: "trending", type: "all", limit: "9", title: "TRENDING", container: "trending_home"};
//    TRENDING.init(trending_params);

    /*var connectWithUsEvent = function() {
        if($(window).scrollTop() + $(window).height() == $(document).height()) {
            $('div.row.connectWithUs').slideDown('fast');
        } else if($(window).scrollTop() == 0){
            $('div.row.connectWithUs').slideDown('fast');

        } else{
            $('div.row.connectWithUs').slideUp('fast');
        }
    };

    $(document).scroll(connectWithUsEvent);*/

    $(".back_to_top_button").click(function(){
        $('html, body').animate({
            scrollTop: 0
        }, 300);
    });
    
    
    generateHomepage();
    
});
var onRefreshHomepage = false;

//function callExtrasWidget(){
//    var exam_results_params = {type: "exam_result", item: 6, start: 0, title: "EXAM RESULTS"};
//    examResultsCall(exam_results_params);
//
//    var forex_results_params = {type: "forex", item: 4, start: 0, title: "FOREX"};
//    forexCall(forex_results_params);
//
//    var currency_calc_params = {type: "currcalc", item: 0, start: 0, title: "CURRENCY CALCULATOR"};
//    currCalculatorCall(currency_calc_params);
//
//    var lotto_results_params = {type: "lotto_result", item: 4, start: 0, title: "LOTTO RESULTS"};
//    lottoResultsCall(lotto_results_params);
//}

function callExtrasWidget(consolidatedData){
    var exam_results_params = {type: "exam_result", item: 8, start: 0, title: "EXAM RESULTS"};
    examResultsGenerate(consolidatedData.extras_lists_exams, exam_results_params);
    //examResultsCall(exam_results_params);

    var forex_results_params = {type: "forex", item: 4, start: 0, title: "FOREX"};
    forexGenerate(consolidatedData.extras_lists_forex, forex_results_params);

    //currency cal
    var currency_calc_params = {type: "currcalc", item: 0, start: 0, title: "CURRENCY CALCULATOR"};
    currCalculatorGenerate(consolidatedData.extras_lists_currcal, currency_calc_params);

    var lotto_results_params = {type: "lotto_result", item: 6, start: 0, title: "LOTTO RESULTS"};
    lottoResultsGenerate(consolidatedData.extras_lists_lotto, lotto_results_params);
    //lottoResultsCall(lotto_results_params);
}
