$(document).ready(function() {
	moveAds();
	$(document).ajaxComplete(function() { moveAds(); });
	$(document.body).change(function() { setTimeout("moveAds();", 10); });
});

Tools.addEvent(window, "onload", moveAds, 0);

function moveAds(timeout, justMove) {
	if (typeof lazyadids != "undefined" && !Tools.isEmpty(lazyadids) && Tools.isArray(lazyadids)) {
		for (var key in lazyadids) {
		    if (justMove === true) {
		        justMoveAd(lazyadids[key]);
		    }
		    else {
		        moveAd(lazyadids[key], timeout);
		    }
		}
	}
}

function moveAd(adid, timeout) {
    
    var ad = HuutoAd.getAd(adid);
    
    // if there is no banner or if banner already in its place don't do anything
    if (ad.isEmpty() || ad.isInPlace()) { return; }
    
    // if banner still loading wait 500 ms 
    if (ad.banner.height() <= 0) {
    	if (timeout < 10000) {
	    	timeout += 500;

	    	setTimeout("moveAd('"+ad.adid+"', "+timeout+");", 500);
    	}
    	return;
    }

    justMoveAd(ad.adid);
    moveAds(0, true);
}

function justMoveAd(adid) {
    
    var ad = HuutoAd.getAd(adid);
    
    // if there is no banner or if banner already in its place don't do anything
    if (ad.isEmpty() || ad.isInPlace()) { return; }
    
    // special tweak for ban1
    if (ad.adid.indexOf("ban1") > -1 && ad.banner.height() > 1) {
        if (ad.banner.height() < 20) { return; }
        
        var ban1div = $("#ban1div");
        if (Tools.isEmpty(ban1div.get(0))) { return; }
        
        ban1div.addClass("showlist_resultset showlist_td_1 showlist_results");
        ban1div.css({
            width: "675px",
            padding: "3px",
            display: "table-cell"
        });
    }
    
    // special tweak for jb1
    if (ad.adid.indexOf("jb1") > -1 && ad.banner.height() <= 120) {
        ad.plh.css({ width: ad.banner.width() + "px", height: "120px" });
    }
    else {
        ad.plh.css({ width: ad.banner.width() + "px", height: ad.banner.height() + "px" });
    }
    
    ad.banner.css({
        visibility: "visible",
        display: "block",
        position: "absolute",
        top: ad.plh.offset().top + "px",
        left: ad.plh.offset().left + "px"
    });
}



function HuutoAd(adid) {
    if (Tools.isEmpty(adid) || !Tools.isString(adid)) {
        this.isEmpty = function() { return true; };
        return null;
    }

    this.adid = adid;
    this.plhID = adid + "_plh";
    
    this.banner = $("#" + this.adid);
    this.plh = $("#" + this.plhID);
    
    this.isInPlace = function() {
        if (this.plh.offset().top == this.banner.offset().top &&
            this.plh.offset().left == this.banner.offset().left &&
            this.plh.height() > 0 &&
            this.banner.height() > 0 &&
            this.plh.width() == this.banner.width() &&
            (this.plh.height() == this.banner.height() || (this.adid.indexOf('jb1') >= 0 && this.plh.height() == 120 && this.banner.height() > 0 && this.banner.height() <= 120)) &&
            this.banner.css("visibility") == "visible" &&
            this.banner.css("display") != "none") {
            return true;
        }
        
        return false;
    };
    
    this.isEmpty = function() {
        if (Tools.isEmpty(this.adid) || !Tools.isString(this.adid) ||
            Tools.isEmpty(this.plh.get(0)) || Tools.isEmpty(this.banner.get(0))) {
            return true;
        }
        
        return false;
    };
    
    if (!this.isEmpty() && this.banner.height() == 1) {
        this.banner.css("height", "0");
    }
}
HuutoAd.ads = {};
HuutoAd.getAd = function(adid) {
    if (!(adid in HuutoAd.ads)) {
        if (Tools.isEmpty(adid) || !Tools.isString(adid)) {
            return new HuutoAd(null);
        }
        var ad = new HuutoAd(adid);
        HuutoAd.ads[adid] = ad;
        return ad;
    }
    
    return HuutoAd.ads[adid];
};

