﻿var PW_THO = {
    articlePanel: null,
    smallAdPanel: null,
    bannerPanel: null,
    propertyAdPanel: null,
    propertyAds: new Array()//,
    //advertPanelHeight: 0
};

PW_THO.registerConversion = function(campaignId, campaignItemId) {
    var url = "ajax/registerconversion.aspx?id=" + campaignId + "&cid=" + campaignItemId;
    var loader = new net.ContentLoader(url);
};

/*
PW_THO.article = function(id, name, html, campaignId) {
var div = document.createElement("div");
PW_THO.articlePanel.appendChild(div);
div.innerHTML = html;
var links = div.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
links[i].onclick = function() {
PW_THO.registerConversion(campaignId, id);
}
}
};
*/

var articleCount = 0;

PW_THO.article = function(id, name, html, image, campaignId) {
    if (html.length > 0) {
        var additionalStyle = "";
        if (articleCount == 0) {
            additionalStyle = "first";
        }

        var div = document.createElement("div");

        div.className = "rounded_content " + additionalStyle;

        var section = document.createElement("div");
        div.appendChild(section);
        section.className = "box " + additionalStyle;

        if (image != "") {
            section = document.createElement("div");
            section.className = "article_image";
            div.appendChild(section);
            section.innerHTML = "<img src=\"uploads/" + image + "\"></img>";
        }

        section = document.createElement("div");
        div.appendChild(section);
        section.innerHTML = "<p>" + html + "</p>";

        var links = div.getElementsByTagName("a");
        for (var i = 0; i < links.length; i++) {
            links[i].onclick = function() {
                PW_THO.registerConversion(campaignId, id);
            }
        }

        PW_THO.articlePanel.appendChild(div);
        articleCount++;
    }
};

/*PW_THO.smallAd = function(id, name, text, target, popup, campaignId, index) {

var a = document.createElement("a");
PW_THO.smallAdPanel.appendChild(a);
a.className = "THO_smallAd_link THO_smallAd_link" + index;
a.href = target;
if (popup) a.target = "_blank";
a.title = name;
a.onclick = function() {
PW_THO.registerConversion(campaignId, id);
};

var div = document.createElement("div");
a.appendChild(div)
div.className = "THO_smallAd THO_smallAd" + index;

var section = document.createElement("div");
div.appendChild(section);
section.className = "THO_smallAd_title THO_smallAd_title" + index;
section.innerText = name;

section = document.createElement("div");
div.appendChild(section);
section.className = "THO_smallAd_text THO_smallAd_text" + index;
section.innerText = text;
};*/
PW_THO.smallAd = function (id, name, text, target, popup, campaignId, index, icon) {

    var a = document.createElement("a");
    PW_THO.smallAdPanel.appendChild(a);
    //a.className = "THO_smallAd_link THO_smallAd_link" + index;
    a.href = target;
    if (popup) a.target = "_blank";
    a.title = name;
    a.onclick = function () {
        PW_THO.registerConversion(campaignId, id);
    };

    var div = document.createElement("div");
    a.appendChild(div)
    div.className = "small_ad";

    var section = document.createElement("div");
    div.appendChild(section);
    section.className = "rounded_top_blue";
    section.innerHTML = "<span></span>";

    section = document.createElement("div");
    div.appendChild(section);
    if (icon != "") {
        section.innerHTML = "<h1><img class='icon' src='uploads/" + icon + "' />" + name.toUpperCase() + "</h1>";

        //KN 24 Feb 2011 There is an IE7 bug that the a href does not work if the user clicks on the image so I have added the onlick event for IE7 only 
        if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
            var ieversion = new Number(RegExp.$1) // capture x.x portion and store as a number
            if ((ieversion < 8) && (ieversion >= 7)) {
                if (popup) {
                    section.innerHTML = "<h1><img class='icon' src='uploads/" + icon + "' onclick='window.open(\"" + target + "\", \"_blank\")' />" + name.toUpperCase() + "</h1>";
                } else {
                    section.innerHTML = "<h1><img class='icon' src='uploads/" + icon + "' onclick='document.location.href=\"" + target + "\"' />" + name.toUpperCase() + "</h1>";
                }
            }
        }
        
    } else {
        section.innerHTML = "<h1>" + name.toUpperCase() + "</h1>";
    }

    section = document.createElement("div");
    div.appendChild(section);
    section.className = "rounded_content box";
    section.innerHTML = "<p>" + text + "</p>";

    section = document.createElement("div");
    div.appendChild(section);
    section.className = "rounded_bottom";
    section.innerHTML = "<span></span>";
};

PW_THO.smallAdWithBackgroundImage = function (id, name, text, target, popup, campaignId, index, backgroundImage) {

    var a = document.createElement("a");
    PW_THO.smallAdPanel.appendChild(a);
    a.href = target;
    if (popup) a.target = "_blank";
    a.title = name;
    a.onclick = function () {
        PW_THO.registerConversion(campaignId, id);
    };

    var div = document.createElement("div");
    a.appendChild(div)
    div.className = "small_ad";
    if (backgroundImage != "") {
        div.style.backgroundImage = "url(uploads/" + backgroundImage + ")";
    }

    section = document.createElement("div");
    if (backgroundImage != "") {
        div.style.backgroundImage = "url(uploads/" + backgroundImage + ")";
        //There is an IE7 bug that the a href does not work if the user clicks on the image so I have added the onlick event for IE7 only
        if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
            var ieversion = new Number(RegExp.$1) // capture x.x portion and store as a number
            if ((ieversion < 8) && (ieversion >= 7)) {
                if (popup) {
                    section.onclick = "window.open(\"" + target + "\", \"_blank\")'";
                } else {
                    section.onclick = "'document.location.href=\"" + target + "\"";
                }
            }
        }
    }
    div.appendChild(section);

    section.innerHTML = "<h2>" + name.toUpperCase() + "</h2>";
    section.innerHTML = section.innerHTML + "<p>" + text + "</p>";
};

PW_THO.propertyAd = function(id, name, text, target, popup, icon, campaignId) {
    PW_THO.propertyAds[PW_THO.propertyAds.length] = PW_THO.propertyAd;

    var a = document.createElement("a");
    a.setAttribute("id", "propertyAdvert_" + id);
    a.href = target;
    if (popup) a.target = "_blank";
    a.title = name;
    a.onclick = function() {
        PW_THO.registerConversion(campaignId, id);
    };

    var div = document.createElement("div");

    a.appendChild(div);

    div.className = "rounded_content";
    var section = document.createElement("div");
    div.appendChild(section);
    section.className = "box";

    if (icon != "") {
        section = document.createElement("div");
        section.className = "article_image";
        div.appendChild(section);
        section.innerHTML = "<img src=\"uploads/" + icon + "\"></img>";
    }

    section = document.createElement("div");
    div.appendChild(section);
    section.innerHTML = "<p><h2>" + name + "</h2></p><p>" + text + "</p>";

    PW_THO.propertyAdPanel.appendChild(a);

    /*



    var a = document.createElement("a");
    a.setAttribute("style", "display:none; border: solid 1px black; margin-bottom:10px;");
    a.setAttribute("id", "propertyAdvert_" + id);
    PW_THO.propertyAdPanel.appendChild(a);
    //a.className = "THO_smallAd_link THO_smallAd_link" + index;
    a.href = target;
    if (popup) a.target = "_blank";
    a.title = name;
    a.onclick = function() {
    PW_THO.registerConversion(campaignId, id);
    };

    var div = document.createElement("div");
    a.appendChild(div);

    var section = document.createElement("div");
    div.appendChild(section);
    section.innerHTML = "<img class='icon' src='uploads/" + icon + "' /> " + name.toUpperCase() + "<br/>" + text;
    */

    //PW_THO.advertPanelHeight = (parseInt(PW_THO.advertPanelHeight) + 100);
};

PW_THO.banner = function (id, name, imageUrl, target, popup, campaignId) {

    if (PW_THO.bannerPanel != null) {
        var a = document.createElement("a");
        PW_THO.bannerPanel.appendChild(a);
        if (campaignId != 0) {
            a.href = target;
            if (popup) a.target = "_blank";
            a.title = name;

            a.onclick = function () {
                PW_THO.registerConversion(campaignId, id);
            };
        }
        var img = document.createElement("img");
        a.appendChild(img)
        img.src = "uploads/" + imageUrl;
        img.alt = name;
    }
};
