﻿var getNext = true;
var getNextSearch = false;
var getNextPriceMover = false;

$(document).ready(function() {
    $("div.switchpaging a#difficulty").colorbox({
        innerWidth: 241,
        innerHeight: 144,
        inline: true,
        href: ".difficulty-text:first"
    });
});

$(window).scroll(function () {
    if (($(window).scrollTop() + 2000) >= ($(document).height() - $(window).height())) {
        if (getNext) {
            getNext = false;
            if (getNextSearch)
                getNextSearchItem();
            else if (getNextPriceMover)
                getNextPriceMoverItem();
            else
                getNextAdvert();
        }
    }
});

function getNextAdvert() {
    $(".ajaxloader").show();
    $.ajax({
        type: "POST",
        url: "/service/hq.asmx/getNextAdvertisment",
        data: "{'lastAdvert':'" + $(".ph:last").attr("id") + "', 'url':'" + window.location + "', 'formAction':'" + document.aspnetForm.action + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
	error: function(){$(".ajaxloader").hide();},
        success: function (data) {
            $(".ajaxloader").hide();
            if (data.d != "") {
                $(".ph:last").after(data.d);

                getNext = true;

                $(".ph:last .stable-lnk").colorbox({
                    innerWidth: 241,
                    innerHeight: 150,
                    iframe: true,
                    scrolling: false,
                    onComplete: function() { $(this).colorbox.resize(); },
                    onOpen: function() { $(this).colorbox({ href: "/addToStable.aspx?adId=" + $(this).attr("id") }) }
                });

                $(".ph:last .stableemail-lnk").colorbox({
                    innerWidth: 300,
                    innerHeight: 300,
                    iframe: true,
                    scrolling: false,
                    //onComplete: function () { $(this).colorbox.resize(); },
                    onOpen: function () { $(this).colorbox({ href: "/submitenquiry.aspx?aid=" + $(this).attr("id") }) }
                });
            }
        }
    });
};

function getNextSearchItem() {
    $(".ajaxloader").show();
    $.ajax({
        type: "POST",
        url: "/service/hq.asmx/getNextSearchItem",
        data: "{'lastAdvert':'" + $(".ph:last").attr("id") + "', 'url':'" + window.location + "', 'formAction':'" + document.aspnetForm.action + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            $(".ajaxloader").hide();
            if (data.d != "") {
                $(".ph:last").after(data.d);

                getNext = true; 

                $(".ph:last .stable-lnk").colorbox({
                    innerWidth: 241,
                    innerHeight: 150,
                    iframe: true,
                    scrolling: false,
                    onComplete: function() { $(this).colorbox.resize(); },
                    onOpen: function() { $(this).colorbox({ href: "/addToStable.aspx?adId=" + $(this).attr("id") }) }
                });

                $(".ph:last .stableemail-lnk").colorbox({
                    innerWidth: 300,
                    innerHeight: 300,
                    iframe: true,
                    scrolling: false,
                    //onComplete: function () { $(this).colorbox.resize(); },
                    onOpen: function () { $(this).colorbox({ href: "/submitenquiry.aspx?aid=" + $(this).attr("id") }) }
                });
            }
        }
    });
};

function getNextPriceMoverItem() {
    $(".ajaxloader").show();
    $.ajax({
        type: "POST",
        url: "/service/hq.asmx/getNextPriceMover",
        data: "{'lastAdvert':'" + $(".ph:last").attr("id") + "', 'url':'" + window.location + "', 'formAction':'" + document.aspnetForm.action + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            $(".ajaxloader").hide();
            if (data.d != "") {
                $(".ph:last").after(data.d);

                getNext = true;
                $(".ajaxloader").hide();

                $(".ph:last .stable-lnk").colorbox({
                    innerWidth: 241,
                    innerHeight: 150,
                    iframe: true,
                    scrolling: false,
                    onComplete: function () { $(this).colorbox.resize(); },
                    onOpen: function () { $(this).colorbox({ href: "/addToStable.aspx?adId=" + $(this).attr("id") }) }
                });

                $(".ph:last .stableemail-lnk").colorbox({
                    innerWidth: 300,
                    innerHeight: 300,
                    iframe: true,
                    scrolling: false,
                    //onComplete: function () { $(this).colorbox.resize(); },
                    onOpen: function () { $(this).colorbox({ href: "/submitenquiry.aspx?aid=" + $(this).attr("id") }) }
                });
            }
        }
    });
};
