var TINY={};function T$(i){return document.getElementById(i)}TINY.box=function(){var p,m,b,fn,ic,iu,iw,ih,ia,f=0;return{show:function(c,u,w,h,a,t){if(!f){p=document.createElement('div');p.id='tinybox';m=document.createElement('div');m.id='tinymask';b=document.createElement('div');b.id='tinycontent';document.body.appendChild(m);document.body.appendChild(p);p.appendChild(b);m.onclick=TINY.box.hide;window.onresize=TINY.box.resize;f=1}if(!a&&!u){/*start DC edit*/p.style.zIndex=999999;/*end DC edit*/p.style.width=w?w+'px':'auto';p.style.height=h?h+'px':'auto';p.style.backgroundImage='none';b.innerHTML=c}else{b.style.display='none';p.style.width=p.style.height='100px'}this.mask();ic=c;iu=u;iw=w;ih=h;ia=a;this.alpha(m,1,80,3);if(t){setTimeout(function(){TINY.box.hide()},1000*t)}},fill:function(c,u,w,h,a){if(u){p.style.backgroundImage='';var x=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');x.onreadystatechange=function(){if(x.readyState==4&&x.status==200){TINY.box.psh(x.responseText,w,h,a)}};x.open('GET',c,1);x.send(null)}else{this.psh(c,w,h,a)}},psh:function(c,w,h,a){if(a){if(!w||!h){var x=p.style.width,y=p.style.height;b.innerHTML=c;p.style.width=w?w+'px':'';p.style.height=h?h+'px':'';b.style.display='';w=parseInt(b.offsetWidth);h=parseInt(b.offsetHeight);b.style.display='none';p.style.width=x;p.style.height=y}else{b.innerHTML=c}this.size(p,w,h)}else{p.style.backgroundImage='none'}},hide:function(){TINY.box.alpha(p,-1,0,3)},resize:function(){TINY.box.pos();TINY.box.mask()},mask:function(){m.style.height=TINY.page.total(1)+'px';m.style.width='';m.style.width=TINY.page.total(0)+'px'},pos:function(){var t=(TINY.page.height()/2)-(p.offsetHeight/2);t=t<10?10:t;p.style.top=(t+TINY.page.top())+'px';p.style.left=(TINY.page.width()/2)-(p.offsetWidth/2)+'px'},alpha:function(e,d,a){clearInterval(e.ai);if(d==1){e.style.opacity=0;e.style.filter='alpha(opacity=0)';e.style.display='block';this.pos()}e.ai=setInterval(function(){TINY.box.ta(e,a,d)},20)},ta:function(e,a,d){var o=Math.round(e.style.opacity*100);if(o==a){clearInterval(e.ai);if(d==-1){e.style.display='none';e==p?TINY.box.alpha(m,-1,0,2):b.innerHTML=p.style.backgroundImage=''}else{e==m?this.alpha(p,1,100):TINY.box.fill(ic,iu,iw,ih,ia)}}else{var n=Math.ceil((o+((a-o)*.5)));n=n==1?0:n;e.style.opacity=n/100;e.style.filter='alpha(opacity='+n+')'}},size:function(e,w,h){e=typeof e=='object'?e:T$(e);clearInterval(e.si);var ow=e.offsetWidth,oh=e.offsetHeight,wo=ow-parseInt(e.style.width),ho=oh-parseInt(e.style.height);var wd=ow-wo>w?0:1,hd=(oh-ho>h)?0:1;e.si=setInterval(function(){TINY.box.ts(e,w,wo,wd,h,ho,hd)},20)},ts:function(e,w,wo,wd,h,ho,hd){var ow=e.offsetWidth-wo,oh=e.offsetHeight-ho;if(ow==w&&oh==h){clearInterval(e.si);p.style.backgroundImage='none';b.style.display='block'}else{if(ow!=w){var n=ow+((w-ow)*.5);e.style.width=wd?Math.ceil(n)+'px':Math.floor(n)+'px'}if(oh!=h){var n=oh+((h-oh)*.5);e.style.height=hd?Math.ceil(n)+'px':Math.floor(n)+'px'}this.pos()}}}}();TINY.page=function(){return{top:function(){return document.documentElement.scrollTop||document.body.scrollTop},width:function(){return self.innerWidth||document.documentElement.clientWidth||document.body.clientWidth},height:function(){return self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight},total:function(d){var b=document.body,e=document.documentElement;return d?Math.max(Math.max(b.scrollHeight,e.scrollHeight),Math.max(b.clientHeight,e.clientHeight)):Math.max(Math.max(b.scrollWidth,e.scrollWidth),Math.max(b.clientWidth,e.clientWidth))}}}();
// only proceed if publisher parameters have been defined
if (typeof(F4P) !== 'undefined') {

    // only define FollowThis once
    if (typeof(FollowThis) === 'undefined') {

        var FollowThis = function() {
            // constants
            this.DOMAIN_URL = 'http://publisher.fliptop.com';
            this.CSS_URL = this.DOMAIN_URL + '/stylesheets/adcss.css';
            this.CLOSE_ICON_URL = this.DOMAIN_URL + '/images/wgtimages/trans_icon_big1.gif';
            this.WIDGET_ICON_URL = this.DOMAIN_URL + '/images/wgtimages/wedget_icon.png';
            this.WIDGET_PAGE_URL = this.DOMAIN_URL + '/home/show/1?wdata=';
            this.WIDGET_INIT_URL = this.DOMAIN_URL + '/home/widget?wdata=';
            this.IFRAME_BG_IMG_URL = this.DOMAIN_URL + '/images/wgtimages/blank.gif';
            this.TEST_IMG_URL = "/images/wgtimages/wedget_icon.png";
            this.CORRECT_WIDTH = 16;
            this.CORRECT_HEIGHT = 17;

            // state variables
            this._qtyButton = 0;
            this._waitCycles = 0;
        };

        FollowThis.prototype = {
            initialize: function() {
                this._addCss();
            },

            addButton: function(buttonId) {
                var widgetHTML = this._createLightbox();
                var button = this._createButton(widgetHTML);
                var buttonSpan = document.getElementById(buttonId);
                if (buttonSpan) {
                    buttonSpan.appendChild(button);
                }
            },

            _createButton: function(widgetHTML) {
                var img = document.createElement('img');
                img.src = (typeof(F4P.wIcn) === undefined || F4P.wIcn === '' || F4P.wIcn === null || F4P.wIcn === undefined) ? this.WIDGET_ICON_URL : unescape(F4P.wIcn);
                img.alt = F4P.wbtn;
                img.className = 'f4pFollowIcon';
                img.setAttribute('align', 'absmiddle');

                var wrapperSpan = document.createElement('span');
                wrapperSpan.appendChild(img);

                if (F4P.wbtn !== undefined && F4P.wbtn !== '') {
                    var followTextSpan = document.createElement('span');
                    followTextSpan.className = 'f4pFollowText';
                    followTextSpan.innerHTML = F4P.wbtn;
                    wrapperSpan.appendChild(followTextSpan);
                }

                var anchor = document.createElement('a');
                anchor.id = F4P.wdata;
                anchor.className = 'f4pFollowAnchor';
                anchor.onclick = function() {
                    TINY.box.show(widgetHTML, 0, 0, 0, 1);
                };
                anchor.appendChild(wrapperSpan);
                this._qtyButton++;
                return anchor;
            },

            _addCss: function() {
                var cssOpts = {href:this.CSS_URL,rel:'stylesheet',type:'text/css',media:'screen'};
                var cssLink = document.createElement('link');
                for (var opt in cssOpts) {
                    cssLink.setAttribute(opt, cssOpts[opt]);
                }

                try {
                    document.getElementsByTagName('head')[0].appendChild(cssLink);

                } catch(e) {
                    document.body.appendChild(cssLink);
                }
            },

            _createLightbox: function() {
                var bgImgUrl = 'background-image:url(' + this.IFRAME_BG_IMG_URL + ');';
                var lbOpts = {allowTransparency:true,className:'f4pDialogFrame',frameBorder:0,scrolling:'no',width:410,height:350,backgroundColor:'transparent',visibility:'hidden',style: bgImgUrl};
                var lbFrame = document.createElement('iframe');
                for (var opt in lbOpts) {
                    lbFrame.setAttribute(opt, lbOpts[opt]);
                }
                lbFrame.src = this.WIDGET_INIT_URL + F4P.wdata;
                var widgetInitIframe = document.createElement("IFRAME");
                widgetInitIframe.src = this.WIDGET_INIT_URL + F4P.wdata;
                widgetInitIframe.setAttribute("border", 'no');
                if (this._detectIEUserAgent() == 'true') {
                    widgetInitIframe.setAttribute("id", 'fliptopInitIframe');
                }
                else {
                    widgetInitIframe.setAttribute("name", 'fliptopInitIframe');
                }
                widgetInitIframe.setAttribute("frameBorder", 'no');
                widgetInitIframe.setAttribute("scrolling", 'no');
                widgetInitIframe.setAttribute("width", 1);
                widgetInitIframe.setAttribute("height", 1);
                widgetInitIframe.setAttribute("visibility", 'hidden');
                widgetInitIframe.setAttribute("display", 'none');

                var closeButtonDiv = document.createElement('div');
                if (this._detectIEUserAgent() == 'true') {
                    closeButtonDiv.className = "f4pcloseIE7";
                } else {
                    closeButtonDiv.className = "f4pclose";
                }
                closeButtonDiv.innerHTML = '<a onclick="TINY.box.hide();return false;" style="cursor:pointer;"><img src="' + this.CLOSE_ICON_URL + '"/></a>';

                var wrapperDiv = document.createElement('div');
                //wrapperDiv.appendChild(widgetInitIframe);
                wrapperDiv.appendChild(closeButtonDiv);
                wrapperDiv.appendChild(lbFrame);

                return wrapperDiv.innerHTML;
            },

            _detectIEUserAgent: function() {
                var isIE7 = 'false';
                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 <= 7)
                        isIE7 = 'true';
                }
                return isIE7;
            },
            
            _waitfor: function(button, buttonId) {
                if (this._waitCycles < 10) {
                    if (!button.complete) {
                        this._imgwait = setTimeout(function() {
                            FOLLOWTHIS_INSTANCE._waitfor(button, buttonId)
                        }, 500);

                        this._waitCycles = this._waitCycles + 1;

                    } else {
                        if (button.width == this.CORRECT_WIDTH && button.height == this.CORRECT_HEIGHT) {
                            clearTimeout(this._imgwait);

                            this._waitCycles = 0;
                            this.addButton(buttonId);
                        }
                    }
                }
            }
        };
    }

    // only instantiate FollowThis once
    if (typeof(FOLLOWTHIS_INSTANCE) === 'undefined') {
        var FOLLOWTHIS_INSTANCE = new FollowThis();
        FOLLOWTHIS_INSTANCE.initialize();
    }

    var buttonId = 'f4pWidgetButton' + F4P.wdata + '-' + FOLLOWTHIS_INSTANCE._qtyButton;
    try {
        document.write('<span id="' + buttonId + '"></span>');

//        var button = new Image();
//        button.src = FOLLOWTHIS_INSTANCE.DOMAIN_URL + FOLLOWTHIS_INSTANCE.TEST_IMG_URL + '?r=' + Math.random();
//
//        FOLLOWTHIS_INSTANCE._waitfor(button, buttonId);

        FOLLOWTHIS_INSTANCE.addButton(buttonId);

    } catch(e) {}
}
