var shopsquad_advice_popup = {
	create: function(opts) {
		document.write(
			'<iframe id="ssqapopup" allowtransparency="true" frameborder="0" scrolling="no" '+(opts.id ? 'id="'+opts.id+'" name="'+opts.id+'"' : '')+' '+
				  'src="'+(opts.server ? opts.server : window.location.protocol + "//" + window.location.host)+'/partners/advice_popup?'+
					'xid='+opts.partner_api+'&'+
					'popup_html='+encodeURIComponent(opts.popup_html)+'&'+
					'url='+encodeURIComponent(window.location.href)+'&'+
					'topic='+(opts.topic ? escape('I am looking for advice on ')+encodeURIComponent(opts.topic) : '')+'&'+
					'delay='+(opts.delay ? opts.delay : 0)+
					'" width="'+opts.iframe_width+'" height="'+opts.iframe_height+'" '+
					'style="z-index:9999;'+(opts.hidden || opts.delay ? 'display:none;' : '')+'" '+(opts.hidden || opts.delay ? 'onload="this.style.display=\'block\'"' : '')+'>'+
					'</iframe>');
	},
	show_chat_link: function() {
		var sideiframe = document.getElementById('shopsquad_side_iframe');
		if (sideiframe) sideiframe.style.display = 'none';
		
		var theiframe = document.getElementById('shopsquad_chat_link_iframe');
		if (theiframe) theiframe.style.display = 'block';
	},
	create_popup: function(partner_api, popup_html, iwidth, iheight) {
		// this create is different thatn the other create. this one creates a true popup. the other one just creates something inline.
		var theiframe = document.getElementById('shopsquad_chat_link_iframe');
		if (!theiframe) {
			this.create({
				id: 'shopsquad_chat_link_iframe',
				partner_api: partner_api,
				popup_html: popup_html,
				topic: (typeof shopsquad_topic == "undefined" ? '' : shopsquad_topic),
				iframe_width: iwidth,
				iframe_height: iheight,
				hidden: true
			});
			theiframe = document.getElementById('shopsquad_chat_link_iframe');
			
			// center the iframe
			var windowHeight = 0;
			var windowWidth = 0;
			if (typeof window.innerWidth == "number") {
				// modern browsers
				windowWidth = window.innerWidth;
				windowHeight = window.innerHeight;
				theiframe.style.position = "fixed";
			} else if (document.documentElement && document.documentElement.clientWidth) {
				// dealing with IE-newer
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
				theiframe.style.position = "absolute";
			} else {
				// dealing with IE-old-and-quirky
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
				theiframe.style.position = "absolute";
			}
			theiframe.style.top = Math.ceil((windowHeight - iheight) / 2)+"px";
			theiframe.style.left = Math.ceil((windowWidth - iwidth) / 2)+"px";
			
			// close the popup later -- look in partner_iframe.html 
			setInterval(function() {
			  var f = window.frames["shopsquad_chat_link_iframe"];
			  if (f) {
			  	// omg firefox! i have to do this crazy time sync because you keep the reference to a deleted dom node.
			  	var datetime = new Date();
			  	var timestamp1 = parseInt(datetime.getTime() / 100);
			  	var timestamp2 = timestamp1 - 1;
			  	var timestamp3 = timestamp2 - 1;
			  	var timestamp4 = timestamp3 - 1;
			  	var timestamp5 = timestamp4 - 1;
			
			    if (f.frames["closeme"+timestamp1] || f.frames["closeme"+timestamp2] || f.frames["closeme"+timestamp3] || f.frames["closeme"+timestamp4] || f.frames["closeme"+timestamp5]) {
						var theiframe = document.getElementById('shopsquad_chat_link_iframe');
						if (theiframe) {
							theiframe.style.display = 'none';
							var sideiframe = document.getElementById('shopsquad_side_iframe');
							if (sideiframe) sideiframe.style.display = 'block';
						}
			    } 
			  }
			}, 200);
		}
	}
};
var _qevents = _qevents || [];
(function() {
  if (!_qevents.ssqp) {
     _qevents.ssqp = true;
    var elem = document.createElement('script');
    elem.src = (document.location.protocol == "https:" ? "https://secure" : "http://edge") + ".quantserve.com/quant.js";
    elem.async = true;
    elem.type = "text/javascript";
    var scpt = document.getElementsByTagName('script')[0];
    scpt.parentNode.insertBefore(elem, scpt);
    _qevents.push( {qacct:"p-58v4Z4eYgQCjg"} );
  }
})();

