// JavaScript Document

var windowloc = window.location.protocol + "//" + window.location.hostname + window.location.pathname;
metaCollection = document.getElementsByTagName('meta');
sitekeywords = "nok";
for (i = 0; i < metaCollection.length; i++) {
    nameAttribute = metaCollection[i].name.search(/keywords/);
    if (nameAttribute != -1) {
        sitekeywords = metaCollection[i].content;
        break;
    } else sitekeywords = "nok";
}
var param = windowloc + "," + sitekeywords;



chrome.extension.sendRequest({
    greeting: param + "loadpopupss"
}, function (response) {

});
var elem = document.getElementsByClassName("mab_zlxtqbdmrhs")
var count = 0;
var count2 = 0;
var data = [];
var data2 = [];
var data3 = [];
var scroll_array = [];
for (var i = 0; i < elem.length; i++) {
    count2++;
    var anchors = elem[i].getElementsByTagName('a');
    if (anchors.length == 1) {
        count++;
        data.push(anchors[0].getAttribute("href"));
        data2.push(1);
        data3.push(anchors[0].getAttribute("href"));

    }
    else {
        data2.push(0);
        data3.push(0);
    }
    
    scroll_array.push($(".mab_zlxtqbdmrhs:eq(" + i + ")").position().top)

}



if (count2 > 0) {



    chrome.runtime.sendMessage({ greeting: count, data: data, data2: data2, data3: data3 }, function (response) {
        var c_status = 0;
        var time_number = response.split(",");
        var time_delay = (parseInt(time_number[1]) - 1) / (scroll_array.length + 1);
        var loop = -1;
        var id = setInterval(function () {
            loop++;

            if (loop === (scroll_array.length + 1)) {
                clearInterval(id);
                //time_number[0] = 2;
                window.scrollTo(0, scroll_array[time_number[0]]);
                setTimeout(function () {
                    if ($(".mab_zlxtqbdmrhs:eq(" + time_number[0] + ") a ").size() > 0) {
                        $(".mab_zlxtqbdmrhs:eq(" + time_number[0] + ") a ").mouseover();
                        setTimeout(function () {
                            if (time_number[2] == 0) {
                               
                                //$(".mab_zlxtqbdmrhs:eq(" + time_number[0] + ") a ").get(0).click();
                                chrome.runtime.sendMessage({ greeting: "-1", data: data, data2: data2, data3: data3, addnumber: time_number[0] }, function (response) {

                                });
                            }
                        }, 800);
                    }
                    else if ($(".mab_zlxtqbdmrhs:eq(" + time_number[0] + ") iframe ").size() > 0) {
                        $(".mab_zlxtqbdmrhs:eq(" + time_number[0] + ") iframe ").mouseover();
                        setTimeout(function () {

                        }, 800);
                    }
                    else {
                        $(".mab_zlxtqbdmrhs:eq(" + time_number[0] + ")").mouseover();
                        setTimeout(function () {

                        }, 800);
                    }

                   
                }, 200);
            }
            else {
                
                window.scrollTo(0, scroll_array[loop]);
            }

        }, time_delay);
    });
}




  