﻿if (!window.GW) {
    GW = window.GW || {};
    var Z = function() { var b = navigator.userAgent; return { ie: b.match(/MSIE\s([^;]*)/)} } ();
    var G = function(b) { if (typeof b == "string") { return document.getElementById(b) } return b };

    var F = function() {
        if (document.defaultView && document.defaultView.getComputedStyle) {
            return function(c, f) {
                var e = null;
                var d = document.defaultView.getComputedStyle(c, "");
                if (d) { e = d[f] }
                var b = c.style[f] || e;
                return b
            }
        } else {
            if (document.documentElement.currentStyle && Z.ie) {
                return function(b, d) {
                    var c = b.currentStyle ? b.currentStyle[d] : null;
                    return (b.style[d] || c)
                }
            }
        }
    } ();

    var V = function(c) { try { c.parentNode.removeChild(c) } catch (b) { } };

    N = {};
    N.loadStyleSheet = function(url, c) {
        if (!GW.SW.loadingStyleSheet) {
            GW.SW.loadingStyleSheet = true;
            var b = document.createElement("link");
            b.href = url;
            b.rel = "stylesheet";
            b.type = "text/css";
            document.getElementsByTagName("head")[0].appendChild(b);
            GW.SW.hasLoadedStyleSheet = true;
        }
    };
    (function() {
        var b = false;
        N.css = function(e) {
            var d = document.createElement("style"); d.type = "text/css";
            if (Z.ie) {
                d.styleSheet.cssText = e
            } else {
                var f = document.createDocumentFragment();
                f.appendChild(document.createTextNode(e));
                d.appendChild(f)
            }
            function c() {
                document.getElementsByTagName("head")[0].appendChild(d)
            }
            if (!Z.ie || b) {
                c()
            } else {
                window.attachEvent("onload", function() {
                    b = true;
                    c()
                })
            }
            return d;
        }
    })();


    GW.SW = function(B) { return this.init(B); }

    GW.SW.loadingStyleSheet = false;
    GW.SW.hasLoadedStyleSheet = false;
    GW.SW.W_NUM = 0;

    GW.SW.jsonC = function(url, d) {
        var b = document.createElement("script");
        b.type = "text/javascript";
        b.src = url;
        document.getElementsByTagName("head")[0].appendChild(b);
        d(b);
        return b;
    };

    GW.SW.prototype = function() {
        var baseUrl = "http://www.gaucho.com/handler/stamp_widget_handler.ashx";
        return {
            init: function(opt) {
                var h = this;
                h.sinceId = 0;
                this._widgetNumber = ++GW.SW.W_NUM;
                GW.SW["cjosn_callback" + this._widgetNumber] = function(j) {
                    var ulHtml = "";
                    for (var i = 0; i < j.stamp.length; i++) {
                        var stampObj = j.stamp[i];
                        if (parseFloat(stampObj.cacheid) > h.sinceId) {
                            h.sinceId = parseInt(stampObj.cacheid);
                        }
                        var sTarget = h.new_window ? 'target="_blank"' : '';
                        var sDate = h.show_timestamp ? stampObj.added_date_display : '';
                        ulHtml += '<div class="gaucho_widgetrow">';
                        if (h.show_avatar) {
                            ulHtml += '<a class="gaucho_avatar" ' + sTarget + 'href="' + stampObj.user_profile + '"><img width=\"40\" height=\"40\" alt="' + stampObj.username + '" src="' + stampObj.user_avatar + '"></a>';
                        }
                        if (stampObj.stamp_type == "stamp_salefind" || stampObj.stamp_type == "stamp_grocery") {                           
                            ulHtml += '<div class="gaucho_cont"><div class="gaucho_conth4"><a ' + sTarget + ' href="' + stampObj.permalink + '">' + stampObj.title + '</a></div><div class="gaucho_contby">stamped ' + sDate + ' by <a ' + sTarget + ' href="' + stampObj.user_profile + '">' + stampObj.username + '</a></div><div class="gaucho_contxt">' + stampObj.store_desc + '</div></div></div>';
                        } else if (stampObj.stamp_type == "stamp_coupon" || stampObj.stamp_type == "stamp_personalsale" || stampObj.stamp_type == "stamp_freestuff" || stampObj.stamp_type == "stamp_fsdeal") {
                            ulHtml += '<div class="gaucho_cont"><div class="gaucho_conth4"><a ' + sTarget + ' href="' + stampObj.permalink + '">' + stampObj.title + '</a></div><div class="gaucho_contby">stamped ' + sDate + ' by <a ' + sTarget + ' href="' + stampObj.user_profile + '">' + stampObj.username + '</a></div><div class="gaucho_contxt">' + stampObj.desc +  '</div></div></div>';
				        } else if (stampObj.stamp_type == "stamp_savingtip") {
                            ulHtml += '<div class="gaucho_cont"><div class="gaucho_conth4"><a ' + sTarget + ' href="' + stampObj.permalink + '">' + stampObj.title + '</a></div><div class="gaucho_contby">stamped ' + sDate + ' by <a ' + sTarget + ' href="' + stampObj.user_profile + '">' + stampObj.username + '</a></div><div class="gaucho_contxt">' + stampObj.tips +  '</div></div></div>';
				        }
                    }
                    if (ulHtml.length > 0) {
                        h.widgetEl.innerHTML = ulHtml;
                    }
                    V(h.scriptElement);
                };
                this._cb = "GW.SW.cjosn_callback" + this._widgetNumber;
                this.opts = opt;
                this._base = baseUrl;
                this.sw_user = opt.username || "";

                this.shellcolor = opt.shellcolor || "#25b5ca";
                this.shelltextcolor = opt.shelltextcolor || "#ffffff";
                this.linkhovercolor = opt.linkhovercolor || "#c32228";
                this.linkcolor = opt.linkcolor || "#25b5ca";
                this.textcolor = opt.textcolor || "#7b7a7a";
                this.bgcolor = opt.bgcolor || "#ffffff";
                this.show_avatar = opt.show_avatar || false;
                this.show_timestamp = opt.show_timestamp || false;
                this.new_window = opt.new_window || false;

                this.number = opt.number || 10;
                if (opt.version >= 1 && !GW.SW.hasLoadedStyleSheet) {
                    N.loadStyleSheet("http://www.gaucho.com/css/gauchostampwidget.css", this.widgetEl)
                }

                this.sw_interval = opt.interval || 10000;
                this.sw_width = opt.width || "100%";
                this.sw_height = (opt.height || 400) - 100;
                if (this.sw_height < 50) {
                    this.sw_height = 50;
                }

                this.sw_title = opt.title || "Gaucho Save Widget";
                this.pid = opt.pid;
                this.id = "gs-sw-" + this._widgetNumber;

                //hard code css style now
                var cssText = '#' + this.id + '{background-color:' + this.shellcolor + ';}\r\n';
                cssText += '#' + this.id + ' .gaucho_widgeth2{color:' + this.shelltextcolor + ';}\r\n';
                cssText += '#' + this.id + ' .gaucho_widgetboxc,#' + this.id + ' .gaucho_widgetboxf{background-color:' + this.bgcolor + ';}\r\n';
                cssText += '#' + this.id + ' .gaucho_widgetboxc{color:' + this.textcolor + ';}\r\n';
                cssText += '#' + this.id + ' a{color:' + this.linkcolor + ';}\r\n';
                cssText += '#' + this.id + ' a:hover{color:' + this.linkhovercolor + ';}\r\n';
                this.styleEl = N.css(cssText);

                var avatarClass = this.show_avatar ? " gaucho_widgethasavatar" : " gaucho_widgetnoavatar";
                if (!opt.pid) {
                    //widget style string
                    document.write('<div class="gaucho_widgetbox" id="' + this.id + '" style="width:' + this.sw_width + 'px;">\
			       <div class="gaucho_widgeth2">' + this.sw_title + '</div>\
			       <div class="gaucho_widgetboxc ' + avatarClass + '" style="overflow:auto;height:' + this.sw_height + 'px;" id="' + this.id + '_div">\
			       </div>\
			       <div class="gaucho_widgetboxf">\
			         <a class="gaucho_logofoot" href="http://www.gaucho.com"><img alt="Gaucho" src="http://www.gaucho.com/images/logo_gaucho.png"></a>\
				     <a class="gaucho_savelink" href="http://www.gaucho.com">Start Savings</a>\
			       </div>\
			     </div>');
                } else {
                G(this.pid).innerHTML = '<div class="gaucho_widgetbox" id="' + this.id + '" style="width:' + this.sw_width + 'px;">\
			       <div class="gaucho_widgeth2">' + this.sw_title + '</div>\
			       <div class="gaucho_widgetboxc ' + avatarClass + '" style="overflow:auto;height:' + this.sw_height + 'px;" id="' + this.id + '_div">\
			       </div>\
			       <div class="gaucho_widgetboxf">\
			         <a class="gaucho_logofoot" href="http://www.gaucho.com"><img alt="Gaucho" src="http://www.gaucho.com/images/logo_gaucho.png"></a>\
				     <a class="gaucho_savelink" href="http://www.gaucho.com">Start Savings</a>\
			       </div>\
			     </div>'
                }
                this.widgetEl = G(this.id + "_div");
                h.url = baseUrl + "?jsoncallback=" + h._cb + "&sinceId=" + h.sinceId + "&username=" + h.sw_user + "&size=" + h.number;
                GW.SW.jsonC(h.url, function(i) { h.scriptElement = i })

                this.refreshTimer = setInterval(function() {
                    V(h.scriptElement);
                    h.url = baseUrl + "?jsoncallback=" + h._cb + "&sinceId=" + h.sinceId + "&username=" + h.sw_user + "&size=" + h.number;
                    GW.SW.jsonC(h.url, function(i) { h.scriptElement = i })
                }, h.sw_interval);
            },
            stop: function() {
                V(this.styleEl);
                clearInterval(this.refreshTimer);
            }
        }
    } ();

}


