var ParakweetWidget_JsonResults = null;
var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
var IE7 = false /*@cc_on || @_jscript_version == 5.7 @*/;
var gteIE7 = false /*@cc_on || @_jscript_version >= 5.7 @*/;
var isMSIE = /*@cc_on!@*/false;
function ParakweetWidget(category,frp,displayObject){
    this.newDealsSlide = new Slide();
    this.timeArray = new Array();
    this.category = category;
    this.frp = frp;
    this.bottomLink = "";
    this.displayObject = displayObject;
    this.initCSS();
    this.haveResults = false;
    this.self = this;
    this.title = "Latest fashion deals on Twitter";
    this.statIframe = document.createElement("iframe");
    this.statIframe.style.display = "none";
    try{document.body.appendChild(this.statIframe);}catch(e){}
    window._ParakweetWidget = this;
}
ParakweetWidget.prototype.stat = function(url){
    try{this.statIframe.setAttribute("src","http://stat.dweeter.com/stat.txt?r=" + Math.random() + "t=widget&category=" + this.category + "&frp=" + this.frp + "&destUrl=" + this._ue(url));}catch(e){}
};
ParakweetWidget.prototype.run = function(){
    this.displayObject.innerHTML = "";
    var t = this.findOffsetHeight(this.displayObject);
    var text1 = document.createElement("div");
    if (this.frp != "1029"){
	    text1.innerHTML = this.title;
		text1.style.cssText = "font-size:16px;text-align:center;font-weight:bold;margin:6px;font-style:italic";
	}
	else{
	    text1.innerHTML = "<img src=\"http://wdg.parakweet.com/images/widget/R29_1.png\" style=\"position:relative;top:0;left:-2px\" />";
		text1.style.cssText = "height:65px;margin-bottom:6px";
	}
    this.displayObject.appendChild(text1);
    var text2 = document.createElement("div");
    if (!this.bottomLink || this.bottomLink.indexOf("parakweet.com") == -1)this.bottomLink = "http://www.parakweet.com/";
    if (this.frp != "1029"){
		text2.innerHTML = "<a href=\"" + this.bottomLink + "\" target=\"_blank\" style=\"color:#1875BD;background:url(http://www.parakweet.com/images/blue-right-arrow.gif) left center no-repeat;padding:0 0 0 18px\">Search fashion deals on Parakweet</a>";
		text2.style.cssText = "text-align:center;margin:6px;padding:0";
	}
	else{
		var translucentBar = "";
		if (IE6 || IE7) {}
		else { translucentBar = "<img src=\"http://wdg.parakweet.com/images/widget/R29_2.png\" style=\"position:relative;top:-79px;left:-6px\" />";}
		text2.innerHTML = "<a href=\"" + this.bottomLink + "\" target=\"_blank\"><img src=\"http://wdg.parakweet.com/images/widget/R29_3.png\" border=\"0\" style=\"position:relative;top:-3px;left:-3px\" /></a>" + translucentBar;
		text2.style.cssText = "background:url(http://wdg.parakweet.com/images/widget/R29_3.png) -1px 0 no-repeat;height:38px";
	}
    this.displayObject.appendChild(text2);
    var textHeight = text1.offsetHeight + text2.offsetHeight;
    var widget = document.createElement("div");//widget content
    if (t != null){
       var totalHeight = t.offsetHeight;
       with(widget.style){
            height = (totalHeight - textHeight) +"px";
            width = "100%";
            backgroundColor = this.bgColor;
            if (this.frp != "1029")borderTop = "1px #CCC solid";
            borderBottom = "1px #CCC solid";
            overflow = "auto";
        }
    }
    this.displayObject.insertBefore(widget,text2);
    this.displayObject = widget;
    this.refetch(this)();
};
ParakweetWidget.prototype.findOffsetHeight = function(t){
    while (t != null && t != "undefined"){
        if (t.offsetHeight && t.offsetHeight > 200)return t;
        t = t.parentNode;//parentNode parentElement offsetParent
    }
    return 450;
};
ParakweetWidget.prototype.initCSS = function(){
    this.containerWidth = "300";
    this.bgColor = "#FFF";
    this.showProductImage = true;
    this.linkFontSize = "14px";
    this.linkFontColor = "#0000FF";
    this.linkBgColor = "#FFF";
    this.domainFontSize = "12px";
    this.domainFontColor = "#008000";
    this.domainBgColor = "#FFF";
    this.timeFontSize = "12px";
    this.timeFontColor = "#888";
    this.timeBgColor = "#FFF";
    this.textFontSize = "12px";
    this.textFontColor = "#000";
    this.textBgColor = "#FFF";
};
ParakweetWidget.prototype.refetch = function(_this){
    return function(){
        var id = "ParakweetWidgetScript4Jsonp";
        var h = document.getElementsByTagName("head");
        if (h && h.length > 0)
            h = h[0];
        else
            h = document.body;
        var t = document.getElementById(id);
        if (t != null)
            h.removeChild(t);
        t = document.createElement("script");
        t.setAttribute("type","text/javascript");
        t.id = id;
        h.appendChild(t);
        t.setAttribute("src","http://wdg.parakweet.com/widget.jsp?category=" + _this._ue(_this.category) + "&frp=" + _this._ue(_this.frp) + "&startTime=" + _this._ue(_this.getStartTime()) + "&_rand=" + Math.random());
//        t.setAttribute("src","http://localhost:2790/widget.jsp?category=" + _this._ue(_this.category) + "&frp=" + _this._ue(_this.frp) + "&startTime=" + _this._ue(_this.getStartTime()) + "&_rand=" + Math.random());
        if (t.readyState){//IE && Opera
            var readyStateCount = 0;
            t.onreadystatechange = function(){
                if (t.readyState == "loaded" || t.readyState == "complete"){
                    readyStateCount++;
                    if (window.opera || readyStateCount == 2){
                        _this.handleResponseText(ParakweetWidget_JsonResults);
                        t.onload = t.onreadystatechange = null;
                    }
                }
            }
        }
        else{
            t.onload = function(){
                _this.handleResponseText(ParakweetWidget_JsonResults);
            }
        }
    }
};
ParakweetWidget.prototype.handleResponseText = function(responseText){
    var json = eval(responseText);
    this.refreshTimeArray(json.timeArray);
    this.showResults(json.dealResults);
};
ParakweetWidget.prototype.showResults = function(dealResults){
    //todo can ParakweetWidget.prototype.showResults to overwrite this function
    var h = "",md5 = "";
    var dr,shown;
    this.haveResults = false;
    var hStr = "";
    var price = 0,percentDiscount = 0;
    if (dealResults && dealResults.length > 0){
        for (var i = 0;i < dealResults.length;i++){
            dr = dealResults[i];
            md5 = "_pw_" + hex_md5(dr.longUrl);
            shown = document.getElementById(md5);if (shown){var sp = shown.parentNode;if (sp){sp.removeChild(shown);}}
			if (this.frp != "1029")
				h += "<div id=\"" + md5 + "\" style=\"border-bottom:dotted 1px #000;margin-bottom:10px;padding-bottom:10px;overflow:hidden\">";
			else
				h += "<div id=\"" + md5 + "\" style=\"padding-bottom:16px;overflow:hidden\">";
            if (this.frp != "1029"){
                h += "<div style=\"float:left;width:30px;height:30px;padding-left:8px\"><a target=\"_blank\" href=\"http://www.twitter.com/" + dr.publisherScreenname + "\"><img style=\"width:24px;height:24px;border:solid 1px #aaa;margin-top:3px\" src=\""+ dr.publisherImg + "\" border=\"0\" /></a></div>";
                h += "<div style=\"line-height:1.3em;margin-left:40px\">";
                h += "<a href=\"javascript:vglnk.click('" + dr.longUrl + "','_blank');\" style=\"font-size:" + this.linkFontSize + ";color:" + this.linkFontColor + ";background-color:" + this.linkBgColor + "\" onclick=\"_ParakweetWidget.stat('" + dr.longUrl + "');\">" + this.removeLink(dr.text) + "</a>";
                h += "<br/><span style=\"font-size:" + this.domainFontSize + ";color:" + this.domainFontColor + ";background-color:" + this.domainBgColor + "\">" + dr.domain + "</span> - ";
                h += "<span style=\"font-size:" + this.timeFontSize + ";color:" + this.timeFontColor + ";background-color:" + this.timeBgColor + "\">" + dr.date + "</span></div>";
                if (dr.picUrl&&dr.picUrl != ""){
                    h += "<div style=\"margin-left:40px\">";
                    if (IE6 || IE7)
                        hStr = " height=\"100\"";
                    h += "<a href=\"javascript:vglnk.click('" + dr.longUrl + "','_blank');\"><img " + hStr + " src=\"" + dr.picUrl + "\" border=\"0\" style=\"max-height:110px;max-width:110px;overflow:hidden\" onclick=\"_ParakweetWidget.stat('" + dr.longUrl + "');\"/></a>";
                    h += "</div>";
                }
            }
            else{
                if (dr.picUrl&&dr.picUrl != ""){
                    h += "<div style=\"float:left;width:100px;padding-left:8px;text-align:center;overflow:hidden\">";
                    if (IE6 || IE7)
                        hStr = " height=\"100\"";
                    h += "<a href=\"javascript:vglnk.click('" + dr.longUrl + "','_blank');\"><img " + hStr + " src=\"" + dr.picUrl + "\" border=\"0\" style=\"max-height:110px;max-width:110px;overflow:hidden\" onclick=\"_ParakweetWidget.stat('" + dr.longUrl + "');\"/></a>";
                    h += "</div>";
                }
                h += "<div style=\"line-height:1.3em;margin-left:110px\">";
                h += "<a href=\"javascript:vglnk.click('" + dr.longUrl + "','_blank');\" style=\"font-size:" + this.linkFontSize + ";color:" + this.linkFontColor + ";background-color:" + this.linkBgColor + "\" onclick=\"_ParakweetWidget.stat('" + dr.longUrl + "');\">" + this.removeLink(dr.text) + "</a>";
                h += "<br/><span style=\"font-size:" + this.domainFontSize + ";color:" + this.domainFontColor + ";background-color:" + this.domainBgColor + "\">" + dr.domain + "</span> - ";
                h += "<span style=\"font-size:" + this.timeFontSize + ";color:" + this.timeFontColor + ";background-color:" + this.timeBgColor + "\">" + dr.date + "</span><br/>";
                price = 0,percentDiscount = 0;
                if (dr.price && dr.price != "null")
                    price = parseInt(dr.price);
                if (dr.percentDiscount && dr.percentDiscount != "null")
                    percentDiscount = parseInt(dr.percentDiscount);
                if (price > 0 && percentDiscount > 0)
                    h += "<span style=\"text-decoration:line-through;color:gray\">$" + parseInt(price * 100 / (100 - percentDiscount)) + "</span> ";
                h += "<span style=\"color:red\">";
                if (price > 0)
                    h += "$" + price;
                if (percentDiscount > 0){
                    if (price > 0)
                        h += " (" + percentDiscount + "% off)";
                    else
                        h += percentDiscount + "% off";
                }
                h += "</span></div>";
            }
            h += "<div style=\"clear:both\"></div>";
            h += "</div>";
        }
        this.haveResults = h != "";
        var newId = "_" + (new Date().getTime());
        var d = document.createElement("div");
        d.style.width = this.containerWidth + "px";
        d.innerHTML = h;
        var nd = d.cloneNode(true);
        d.id = newId;
        this.displayObject.insertBefore(d,this.displayObject.firstChild);
        this.newDealsSlide.afterShow = function(){d.parentNode.replaceChild(nd,d);};
        this.newDealsSlide.show(newId);
    }
    window.setTimeout(this.refetch(this), 60000);//1 min.
};
ParakweetWidget.prototype.refreshTimeArray = function(timeArray){
    this.timeArray = new Array();
    for (var i = 0;i < timeArray.length;i++){
        this.timeArray.push(timeArray[i]);
    }
};
ParakweetWidget.prototype.getStartTime = function(){
    if (!this.timeArray||this.timeArray.length == 0)return "";
    var _timeArray = new Array();
    for (var i in this.timeArray){
        this.timeArray[i] = this.timeArray[i].replace("T"," ");
        this.timeArray[i] = this.timeArray[i].replace("Z","");
        this.timeArray[i] = this.timeArray[i].replace(/\-/g,"/");
        _timeArray.push(Date.parse(this.timeArray[i]));
    }
    _timeArray.sort(function c(a,b){return b - a;});
    var startTime = _timeArray[0];
    var d = new Date();
    d.setTime(this.haveResults?(startTime + 1000):startTime);
    var m = d.getMonth() + 1;if (m < 10)m = "0" + m;//getUTCMonth
    var _d = d.getDate();if (_d < 10)_d = "0" + _d;
    var h = d.getHours();if (h < 10)h = "0" + h;
    var _m = d.getMinutes();if (_m < 10)_m = "0" + _m;
    var s = d.getSeconds();if (s < 10)s = "0" + s;
    return d.getFullYear() + "-" + m + "-" + _d + "T" + h + ":" + _m + ":" + s + "Z";
};
ParakweetWidget.prototype._ue = function(s){
    return encodeURIComponent(s);
};
ParakweetWidget.prototype.removeLink = function(s){
    return s.replace(/(ftp|http|https):\/\/(\w+:?\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-/]))?/,"");
};