// taf ads functions
var STTAFADSFUNC = {

    // attach the taf button (btImg) to the ad (adImg)
    getAdAttachPos: function(pos, adImg, btImg) {
        var adSPos = pos.split("-");
        var buttonOrientation = adSPos[0];
        var posHorizontal = adSPos[1];
        var posVertical = adSPos[2];
        var posInOrOut = adSPos[3];

        var offLeft = $(adImg).offset().left;
        var offTop = $(adImg).offset().top;
        var bannerWidth = $(adImg).width();
        var bannerHeight = $(adImg).height();
        var buttonHeight = $(btImg).height();
        var buttonWidth = $(btImg).width();

        var finTop = 0;
        var finLeft = 0;

        if (posHorizontal == "left") {
            finLeft = offLeft; // left inside
        } else if (posHorizontal == "right") {
            finLeft = (offLeft + bannerWidth) - buttonWidth; // right inside
        }

        if (posVertical == "top") {
            finTop = offTop; // top inside
        } else if (posVertical == "bottom") {
            finTop = (offTop + bannerHeight) - buttonHeight; // top inside
        }

        if (posInOrOut == "outside") {
            if (buttonOrientation == "vertical") {
                if (posHorizontal == "right") {
                    finLeft = finLeft + buttonWidth;
                } else {
                    finLeft = finLeft - buttonWidth;
                }
            } else {
                if (posHorizontal == "right") {
                    if (posVertical == "top") {
                        finTop = finTop - buttonHeight;
                    } else {
                        finTop = finTop + buttonHeight;
                    }
                } else if (posHorizontal == "left") {
                    if (posVertical == "top") {
                        finTop = finTop - buttonHeight;
                    } else {
                        finTop = finTop + buttonHeight;
                    }
                }
            }
        }

        return [finTop, finLeft, posInOrOut == "inside"];
    },

    attachTafAds: function(id) {

        $(window).resize(function() {
            STTAFADSFUNC.attachTafAds(id);
        });

        $(STTAFADS.ads).each(function(i, n) {
            var adPos = n[0];
            var adType = n[1];
            var adWidth = n[2].split("x")[0];
            var adHeight = n[2].split("x")[1];
            var adClass = n[3];
            var btImg = n[4];

            var btmg = new Image();
            btmg.src = btImg;

            btmg.onload = function() {
                $("." + adClass).each(function(i, n) {
                    var thisObj = $(this);
                    var clName = "st-ad-taf-" + i + adClass;
                    $(this).after("<img src='" + btImg + "' class='" + clName + "' style='display: none;'/>");

                    $("." + clName).mouseout(function() {
                        $(this).attr("src", btImg);
                        STTAFFUNC.hideHoverMap(this);
                    });

                    $("." + clName).mouseover(function() {
                        $(this).attr("src", btImg.replace(".png", "-hover.png"));
                        STTAFFUNC.showHoverMap(
                                this,
                                id,
                                window.location,
                                document.title,
                        {
                            "_adLink" : $(this).parent("a").attr("href"),
                            "_adImage" : $(this).prev().attr("src")
                        }
                                );
                    });

                    $("." + clName).click(function() {
                        $(this).parent("a").click(function() {
                            return false;
                        });
                        STTAFFUNC.cw(this, {
                            id: id,
                            link: window.location,
                            title: document.title,
                            custom: {
                                "_adLink" : $(this).parent("a").attr("href"),
                                "_adImage" : $(this).prev().attr("src")
                            }
                        });
                        $(this).parent("a").unbind("click");
                        return false;
                    });

                    thisObj.css({
                        border: "2px solid red"
                    });

                    var posXY = STTAFADSFUNC.getAdAttachPos(adPos, thisObj, $("." + clName));
                    var topPos = posXY[0];
                    var leftPos = posXY[1];
                    var borderFactor = posXY[2];
                    $("." + clName).css({
                        display: "inline",
                        position: "absolute",
                        top: topPos + (borderFactor ? 2 : 0),
                        left: leftPos + (borderFactor ? 2 : 2)
                    });
                });
            };

        });
    }
};