var UtilInit = {
	newElement: function(tagName, style) {
	  var doc = document;
	  var newElement = UtilInit.createElement(doc,tagName);
	  if (style) {
	      for (key in style) {
	        if (key == 'cssFloat' && UtilInit.isIE()) {
	          newElement.style['styleFloat'] = style[key];
	        }
	        else {
	          newElement.style[key] = style[key];
	        }
	      }
	  }
	  return newElement;
	},
	
	createElement: function(doc,tagName) {
	  var newElt = doc.createElement(tagName);
	  if (typeof(newElt) != 'object') {newElt = doc.standardCreateElement(tagName);}
	  return newElt;
	},
	
	isIE: function() {
	  return (navigator.appName == "Microsoft Internet Explorer");
	},
	
	isIE6: function() {
	  return (navigator.appVersion.indexOf("MSIE 6")!=-1);
	},
	
	canUseFixedPosition: function() {
		return (UtilInit.isIE6() || (UtilInit.isIE() && UtilInit.detectDoctype() == null))?false:true;
	},
	
	getWindowWidth: function(){
		var x = 0;
      	if (self.innerHeight){
      		x = self.innerWidth;
      	} else if (document.documentElement && document.documentElement.clientHeight){
      		x = document.documentElement.clientWidth;
      	} else if (document.body){
      		x = document.body.clientWidth;
      	}
      	return x;
	},
      
	getWindowHeight: function(){
      	var x = 0;
      	if (self.innerHeight){
      		x = self.innerHeight;
      	} else if (document.documentElement && document.documentElement.clientHeight){
      		x = document.documentElement.clientHeight;
      	} else if (document.body){
      		x = document.body.clientHeight;
      	}
      	return x;
	},
	
	detectDoctype: function(){
		var re=/\s+(X?HTML)\s+([\d\.]+)\s*([^\/]+)*\//gi;
		var res=false;
		if(typeof document.namespaces != "undefined")
			res=document.all[0].nodeType==8 ? re.test(document.all[0].nodeValue) : false;
		else
			res=document.doctype != null ? re.test(document.doctype.publicId) : false;
		if(res){
			res=new Object();
			res['xhtml']=RegExp.$1;
			res['version']=RegExp.$2;
			res['importance']=RegExp.$3;
			if(res['xhtml'].toLowerCase() != 'xhtml')
				return null;
			return res;
		}else{
			return null;
		}
	},

	getElementsByClassName: function(searchClass,node) {
		if (node == null) node = document;
	    if (node.getElementsByClassName) 
			return node.getElementsByClassName(searchClass);
			
		var classElements = new Array();
		var els = node.getElementsByTagName("*");
		var elsLen = els.length;
		var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
		for (i = 0, j = 0; i < elsLen; i++) {
			if (pattern.test(els[i].className)) {
			  classElements[j] = els[i];
			  j++;
			}
		}
		return classElements;
	},
	
	getElementsByName: function(search,node) {
		if (node == null) node = document;
		var elements = new Array();
		var els = node.getElementsByTagName("*");
		var elsLen = els.length;
		var pattern = new RegExp("(^|\\s)"+search+"(\\s|$)");
		for (i = 0, j = 0; i < elsLen; i++) {
			if (pattern.test(els[i].name)) {
				elements[j] = els[i];
			  j++;
			}
		}
		return elements;
	},
	
	 htmlDecode: function(input){
		var e = document.createElement('div');
		e.innerHTML = input;
		var result = "";
		for(var i=0;i<e.childNodes.length; i++) {
			result += e.childNodes[i].nodeValue;
		};
		return result;
	},
	
	contains: function(a, obj) {
	    for (var i = 0; i < a.length; i++) {
	        if (a[i] === obj) {
	            return true;
	        }
	    }
	    return false;
	}

};




var WidgetInit = new WidgetClass();
function  WidgetClass() {		
	var self = this;
	this.WidgetHtml = null;
	this.WidgetParams = null;
	this.WidgetLocalizationProps = { day: 'd', hour: 'h', min: 'm', sec: 's' };
	this.scroll = true;
	this.seconds = new Array();
	this.shortedUrlParams = new Array('wId','affId','site','utm_medium','utm_campaign','host');

	this.gebi = function(elId) {
		var wid = this.WidgetParams.id;
		var result = document.getElementById(elId+wid);
		if(null == result) {
			result = document.getElementById(elId)
		}
		return result;
	};

	this.getWidgetLeft= function(){
		var w = UtilInit.getWindowWidth();
		var btn = this.gebi("nimbleBuyWidget");
		if(!btn) return w;
		if(btn.clientWidth) {
			return w - btn.clientWidth;
		}
		else {
			return w - btn.offsetWidth;
		}
	};
	
	this.getWidgetHeight= function(){
		var btn = this.gebi("nimbleBuyWidget");
		if (btn) {
			if (btn.clientHeight) {
				return btn.clientHeight;
			}
			else {
				return btn.offsetHeight;
			}
		}
		return 0;
	};
	
	this.getParams = function(){
		this.params = {};
		
		var s = null;
		s = this.gebi("nimbleBuyWidgetScript");
		
		if (s) {
			var src = s.getAttribute("src");
			this.params.root = src.substring(0,src.indexOf("/widget.action"));
			var c = src.indexOf("?");
			if (c == -1) 
				return;
			
			var ps = src.substr(c + 1);
			var userPassedUrlParams = "";
			if (ps.length > 3) {
				var pp = ps.split("&");
				for (var i = 0; i < pp.length; i++) {
					var pa = pp[i].split("=");
					this.params[pa[0]] = pa[1];
					if(!UtilInit.contains(this.shortedUrlParams, pa[0])) {
						userPassedUrlParams += "&" + pa[0] + "=" + pa[1];
					}
				}
				if(userPassedUrlParams != "") {
					this.WidgetParams.urlSuffixForIms = userPassedUrlParams;
				}
			}
		}
	};
	
	this.insertWidget = function(){ 
		var nimbleBuyWidget = this.gebi("nimbleBuyWidget");
		if(nimbleBuyWidget){
			if(this.WidgetParams.widgetWidth != 0 && this.WidgetParams.widgetHeight !=0) {
			    var widgetWidth = this.WidgetParams.widgetWidth;
			    if (widgetWidth.indexOf("px") == -1){
			    	widgetWidth += "px";
			    }
			    nimbleBuyWidget.style.width = widgetWidth;
			    
			    var widgetHeight = this.WidgetParams.widgetHeight;
			    if (widgetHeight.indexOf("px") == -1){
			    	widgetHeight += "px";
			    }
			    nimbleBuyWidget.style.height = widgetHeight;
			}
		    nimbleBuyWidget.style.position = "relative";
		    nimbleBuyWidget.style.overflow = "hidden";
		    nimbleBuyWidget.innerHTML = this.WidgetHtml.getNimbleBuyWidgetHTML();
		    if(this.WidgetHtml.getNimbleBuyWidgetHTML() == "") {
		    	nimbleBuyWidget.style.display = "none";
		    }
		    this._outerDiv = nimbleBuyWidget;
		}
		else {
	        var prms = {
		        	zIndex: 20001,
		            scrolling: 'no',
		            overflow: 'hidden',
		            position: 'absolute',
					width: this.WidgetParams.widgetWidth+'px',
					height: this.WidgetParams.widgetHeight+'px'
		        };
		    var bd = document.body;
		    if(!bd) return;
		    this._outerDiv = UtilInit.newElement("div",prms);
	        this._outerDiv.id = "nimbleBuyWidget"+this.WidgetParams.id;
	        this._outerDiv.innerHTML = this.WidgetHtml.getNimbleBuyWidgetHTML();
			bd.insertBefore(this._outerDiv, bd.firstChild);
			this.setWidgetPosition();
		}
		
		this.processWidgetLinks();
    };
    
    this.isImshopping= function() {
    	var url = document.location.href;
    	if ('' != this.WidgetParams.imsAppUrl && url.indexOf(this.WidgetParams.imsAppUrl) > -1) {
    		return true;
    	} else {
    		return false;
    	}
    };
    
    this.processWidgetLinks= function() {
    	var isIms = this.isImshopping();
    	var nimbleBuyWidget = this.gebi("nimbleBuyWidget");
    	var links = UtilInit.getElementsByName("widgetNimbleBuyProductLink",nimbleBuyWidget);
    	if (links.length > 0) {
	    	for (var i = 0; i < links.length; i++) {
	    		var link = links[i];
    			var suffix = this.WidgetParams.urlSuffixForIms;
    			if(suffix.length > 0){
					if (link.href.indexOf("?") > -1) {
						link.href += suffix;
					} else {
						link.href += "?" + suffix;
					}
    			}
	    	}
	    }
    };
    
    this.startScroll = function(){
		this.scroll = true;
	};
	
	this.stopScroll = function(){
		this.scroll = false;
	};
	
	var children = typeof document
	.createElement('div')
	.children != 'undefined';
	
	this.childArray = children ? function(node) {
		return node.children;
	} : function(node) {
		var list = node.childNodes,
		length = list.length,
		i = -1,
		array = [];
		while(++i < length)
			if(list[i].nodeType == 1)
				array.push(list[i]);
		return array;
	}
	
	this.scrollOffer = function(){
    	var offers = this.gebi("nimbleBuyOffers");
    	if(!offers) return;
		if (this.scroll) {
			var l = parseInt(offers.style.left, 10);
			var w = offers.offsetWidth/this.childArray(offers).length;
			if (l > -(this.WidgetParams.widgetOffersSize * w - w)) {
				offers.style.left = l - w + "px";
			}
			else {
				offers.style.left = 0;
			}
		}
		self.processImage();
   		setTimeout(function(){self.scrollOffer();}, this.WidgetParams.rotateTimeInterval);
    };

    this.calcWidgetPosition = function(){
    	var widgetHeight = 400;
    	var widgetWidth = 450;
        var winHeight = UtilInit.getWindowHeight();
        var winWidth = UtilInit.getWindowWidth();
    	var scrollTop = 0;
    	var top = Math.round((winHeight/2) - (widgetHeight/2) +  scrollTop);
		if(top<(10+scrollTop)) top = 10+scrollTop;
    	var widgetPosH = top + 'px';
    	var widgetPosW = Math.round((winWidth/2) - (widgetWidth/2)) + 'px';
		return {top:widgetPosH, left:widgetPosW};
	};
    
	this.setWidgetPosition = function(){
		var btn = this.gebi("nimbleBuyWidget");
		if(!btn) return;
		
        var winHeight = UtilInit.getWindowHeight();
    	var WidgetHeight = this.getWidgetHeight();
    	var btnTop = Math.round((winHeight/2) - (WidgetHeight/2));
		btn.style.top = btnTop + "px";
		btn.style.left = 0;
    };
    
    this.showLoading = function(){
		if(!this.gebi("imsLoading")){
			var w = UtilInit.getWindowWidth();
			var h = UtilInit.getWindowHeight();
	    	var l = Math.round(w/2-100) + 'px';
	    	var t = Math.round(h/2-50) + 'px';
	        var prms = {
		        	zIndex: 20001,
		            display: 'block',
		            position: 'absolute',
		            left: l,
		            top: t,
		            textAlign: 'center',
					padding: '20px',
					border: '10px solid #acacac',
					backgroundColor: '#fff',
					width: '200px',
					fontSize: '18px',
					fontWeight: 'bold',
					fontFamily: 'Arial'
				};
		    this.loading = UtilInit.newElement("div",prms);
	        this.loading.id = "imsLoading"+this.WidgetParams.id;		
	        this.loading.innerHTML = "Loading...";
			document.body.insertBefore(this.loading, document.body.firstChild);
		}
		else {
			this.gebi("imsLoading").innerHTML = "Loading...";
			this.gebi("imsLoading").style.display = "block";
		}
		
	};
	
	this.refreshDealTimeLeftArea = function() {
		var times = UtilInit.getElementsByClassName("offerTimeLeft",this.gebi("nimbleBuyOffers"));
		for(var i=0; i<times.length; i++){
			if (this.seconds.length < i+1) {
				this.seconds[i] = 0;
			}
			var timerText = times[i].innerHTML.toString();
			if(timerText == "") { return; }
			if(null != timerText) {
				timerText = timerText.replace(/(<([^>]+)>)/ig,"");
			} else {timerText = "";}
			if (timerText == "Expired" || timerText == "00:00:00") {
				times[i].innerHTML = "00" + this.WidgetLocalizationProps.hour + ":00" + this.WidgetLocalizationProps.min + ":00" + this.WidgetLocalizationProps.sec;
			} else {
				var timestrs = timerText.split(":");
				var inDays = (timestrs[0].indexOf(this.WidgetLocalizationProps.day)!=-1) ? true : false;
				
				var daysStr = inDays == true ? timestrs[0].substr(0, timestrs[0].length-this.WidgetLocalizationProps.day.length) : null;
				var hoursStr = inDays == true ? timestrs[1].substr(0, timestrs[1].length-this.WidgetLocalizationProps.hour.length) : timestrs[0].substr(0,  timestrs[0].length-this.WidgetLocalizationProps.hour.length);
				var minStr = inDays == true ? timestrs[2].substr(0, timestrs[2].length-this.WidgetLocalizationProps.hour.length) : timestrs[1].substr(0, timestrs[1].length-this.WidgetLocalizationProps.min.length);
				var secStr = inDays == true ? null : timestrs[2].substr(0, timestrs[2].length-this.WidgetLocalizationProps.sec.length);
				
				var seconds = inDays == true ? parseInt(daysStr, 10)*86400 + parseInt(hoursStr, 10)*3600 + parseInt(minStr, 10)*60 : parseInt(hoursStr, 10)*3600 + parseInt(minStr, 10)*60 + parseInt(secStr, 10) - 1;
				if (this.seconds[i] == 0) {
					this.seconds[i] = seconds;
				}
				this.seconds[i]--;
				seconds = this.seconds[i];
				
				if (inDays == true) {
					var days = Math.floor(seconds / 86400);
					seconds -= days*86400;
				}
				var hours = Math.floor(seconds / 3600);
				seconds -= hours*3600;
				var minutes = Math.floor(seconds / 60);
				seconds -= minutes*60;
				if (inDays == false) {
					times[i].innerHTML = hours + this.WidgetLocalizationProps.hour + ":" + ((minutes<10)?("0"+minutes):minutes) + this.WidgetLocalizationProps.min + ":" + ((seconds<10)?("0"+seconds):seconds)+this.WidgetLocalizationProps.sec; 
				} else {
					minutes++;
					times[i].innerHTML = days + this.WidgetLocalizationProps.day + ":" + ((hours<10)?("0"+hours):hours) + this.WidgetLocalizationProps.hour + ":" + ((minutes<10)?("0"+minutes):minutes)+this.WidgetLocalizationProps.min;
				}
			}
		}
		setTimeout(function() {self.refreshDealTimeLeftArea();}, 1000);
	};
	
	this.unInstall = function(){
		var	s = this.gebi("nimbleBuyWidgetScript");
		if (s) {
			s.parentNode.removeChild(s);
			var nimbleBuyWidget = this.gebi("nimbleBuyWidget");
			if (nimbleBuyWidget)
				nimbleBuyWidget.innerHTML = "";
		}
	};
	
	this.processImage = function() {
		var nimbleBuyWidget = this.gebi("nimbleBuyWidget");
    	var imgs = UtilInit.getElementsByName("widgetMainImage",nimbleBuyWidget);
    	if(null != imgs && imgs.length > 0) {
    		if(null != imgs[0].title && imgs[0].title != "") {
    			imgs[0].src=imgs[0].title; imgs[0].title="";
    		} imgs[0].name="";
    	}
	};
	
	this.install = function(WidgetHtml,  WidgetParams, WidgetLocalizationProps){
		this.WidgetHtml = WidgetHtml;
		this.WidgetParams = WidgetParams;
		if (WidgetLocalizationProps){
			this.WidgetLocalizationProps = WidgetLocalizationProps;
		}
		this.getParams();
		this.insertWidget();
		if(this.WidgetParams.widgetOffersSize>1){
			setTimeout(function(){ self.scrollOffer(); }, this.WidgetParams.rotateTimeInterval);
		}
		self.processImage();
		setTimeout(function() {self.refreshDealTimeLeftArea();}, 1000);
	};
	
	this.installOnload = function(WidgetHtml,  WidgetParams){
		if (window.addEventListener) {
			window.addEventListener("load", function(){
				self.install(WidgetHtml,  WidgetParams);
			}, false);
		} 
		else if(window.attachEvent) {
			window.attachEvent("onload", function(){
				self.install(WidgetHtml,  WidgetParams);
			}, false);
		} 
	};
	
};


var WidgetHtml13412 = {	getNimbleBuyWidgetHTML: function(){		var s = '';		s = "&lt;div id=&quot;nimbleBuyOffers13412&quot; style=&quot;width:300px; height:125px; position:relative;left:0;top:0;&quot;&gt; &lt;div style=&quot;float:left;width:300px;height:125px;&quot;&gt; &lt;table cellpadding=&quot;2&quot; cellspacing=&quot;2&quot; style=&quot;padding: 5px 0 0 5px; width: 300px; height: 100px; background:#000; border: none;&quot;&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td width=&quot;110&quot; style=&quot;vertical-align:top;&quot;&gt;&lt;img src=&quot;http://s3.amazonaws.com/product-images.imshopping.com/nimblebuy/wild-rice-sushi-grill-feb17-7121622-small_lv.jpg&quot; alt=&quot;Daily Deal thumbnail&quot; style=&quot;height: 95px; width: 95px; border: 0px solid #3366cc; margin-top:10px; padding:10px;&quot;&gt;&lt;/td&gt; &lt;td style=&quot;vertical-align:top; text-align:center;&quot; valign=&quot;top&quot; width=&quot;190&quot;&gt; &lt;img src=&quot;http://www.omaha.com/app/deal/daily-deal-logo.png&quot; width=&quot;175&quot; height=&quot;24&quot; alt=&quot;&quot;&gt;&lt;br&gt; &lt;span style=&quot;color:#ff9933; font-size:12px; font-weight:bold; font-family: Arial; line-height: 14px;&quot;&gt;Wild Rice Sushi &amp; Grill&lt;/span&gt;&lt;br&gt; &lt;span style=&quot;color:#fff; font-size:11px; font-family: Arial; line-height: 14px;&quot;&gt;$10 for $20 Worth of Delicious Sushi, Hibachi Grill or Thai Food&lt;/span&gt; &lt;div style=&quot;height:10px;&quot;&gt;&lt;/div&gt; &lt;div id=&quot;DDBuyNow&quot; style=&quot;text-align:center;&quot;&gt;&lt;a href=&quot;http://w.nmb.ly/1t3pk8&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; alt=&quot;Buy Now&quot; src=&quot;http://www.omaha.com/app/deal/view3.png&quot;&gt;&lt;/a&gt;&lt;/div&gt; &lt;div style=&quot;height:5px;&quot;&gt;&lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/div&gt;";		return UtilInit.htmlDecode(s);	}};var WidgetParams13412 = {	id: '13412',		rootUrl: 'http://widget.imshopping.com',	imsAppUrl: 'http://www.imshopping.com',	widgetWidth: '300',	widgetHeight: '125',	widgetOffersSize: 1,	rotateTimeInterval: 10000, 	urlSuffixForIms: ''};var WidgetLocalizationProps13412 = {	day: 'd',	hour: 'h',	min: 'm',	sec: 's'};WidgetInit.install(WidgetHtml13412,  WidgetParams13412, WidgetLocalizationProps13412);