var _hoverConfig = _hoverConfig || {};
_hoverConfig.googleAccount = 'UA-709906-10';
_hoverConfig.className = 'overlay_two';

var relatedArticles=function(){var e={};return{get:function(o,r){try{if(typeof e[o]!="undefined"){r(e[o]);return}}catch(t){}if(typeof r==="function"){if(typeof o=="object"){limit=o.l;o=o.t}else limit=3;jQuery.getJSON("/api/json/2/search/",{q:o,l:limit},function(l){if(!(l.status!="ok"&&l.result.length==0)){try{for(var m=[],k=0,x=0;x<l.result.length;x++)if(l.result[x].url!=window.location.pathname){m[k]=l.result[x];k++}l.result=m;e[o]=l}catch(u){}r(l)}})}}}}();

//
// 
//
(function(){

	//
	// Get the referrer query
	//
	if (!document.referrer)
    {
		return false;
	}

    var referrer =  document.referrer;
    // referrer = "http://www.google.com/#q=Why+did+1/3+of+the+angels+become+evil&hl=en&prmd=ivns&ei=vblfTseMAtGlsAKA0eU2&start=10&sa=N&bav=on.2,or.r_gc.r_pw.&fp=756c45c4808f223d&biw=1330&bih=863";


	//
	// Check if you came from a search engine
	//
	if((referrer.indexOf("google.com") == -1) && 
	   (referrer.indexOf("yahoo.com") == -1) && 
	   (referrer.indexOf("bing.com") == -1) ) {
		return false;
	}

	//
	// Parse out the query parameter
	//
	var querySearchString = (referrer.indexOf("yahoo.com") != -1) ? "p=" : "q=";
	var qstart = referrer.indexOf(querySearchString);
	var qend = referrer.indexOf("&", qstart);
	if(qend == -1) {
		qend = referrer.length;
	}
	query_text = referrer.substr((qstart+2), (qend-qstart-2));
    try {
        query_text = decodeURIComponent(query_text.replace(/\+/g, " "));
    } catch(e) {
        query_text = query_text.replace(/\+/g, " ");
        query_text = query_text.replace(/\%20/g, " ");
        query_text = query_text.replace(/\%3F/g, "");
    }


	var shown = false;

	var config = {
		width: 225,
		height: 100,
		googleAccount: _hoverConfig.googleAccount || '',
		className: _hoverConfig.className || '',
		show: _hoverConfig.show || undefined
	}

	//
	// Create the container 
	//
	document.write('<div id="popupContainer"></div>');
	var popupContainer = document.getElementById("popupContainer");
	popupContainer.className = config.className;

	/*
	else {
		query_text = 'Dinosaurs';
	}
	*/

	//
	// Catch a user moving to the back button
	//
	var body = document.getElementsByTagName("body")[0];
	document.onmousemove = function(e) {
		e = e || window.event;
		if(e.clientX < config.width && e.clientY < config.height) {
			showPopup();
		}
	}

	//
	// When a user loses focus of the page.
	//
	if (typeof window.addEventListener == 'function') {
		window.addEventListener('blur', function() {
			showPopup();
		}, false);
	}

	//
	// Popup Function
	//
	function showPopup() {
        if (document.getElementById('template_search_suggestion') == null)
        {
            return;
        }

		if(shown == true) {
			return;
		}

		// Make sure this doesn't annoy anyone by only showing it once
		shown = true;

		// ============================================================
		// =============== Site Specific Code STARTS here =============
		// ============================================================

		//
		// Dependencies :(
		//   * jquery.js 
		//   * template.js 
		//   * relatedArticles.js 
		//

		// Get the related articles
		relatedArticles.get(document.title.replace(" | Answerbag", ""), function(data){

			if (data.result.length == 0) {
				return;
			}

			//
			// Put the results into HTML
			//
			data.query_text = query_text;
			var html = jQuery.template("template_search_suggestion", data);
			popupContainer.innerHTML = html;

			// 
			// Show the results in a popup
			//
			jQuery(popupContainer).overlay({

				top: '8%',

				mask: {
					color: '#000',
					loadSpeed: 200,
					opacity: 0.5
				},

				onLoad: function(){
					// Hide ads when popup shows because of Flash
					jQuery(".Column2 .SuperButtonAd").css("display", "none");
					
					var searchSuggestionsInput = document.getElementById("searchSuggestionsInput");
					searchSuggestionsInput.focus();
				},

				onClose: function(){
					jQuery(".Column2 .SuperButtonAd").css("display", "block");
				},

				load: true

			});

		});

		// ============================================================
		// =============== Site Specific Code ENDS here =============== 
		// ============================================================

		//
		// Track analytics when the popup shows
		//
		if(typeof _gaq === "object" && typeof config.googleAccount === 'string' && config.googleAccount.length > 0) {
			_gaq.push(['_setAccount', config.googleAccount]);
			_gaq.push(['_trackPageview']);
		}

		if(typeof config.show === "function") { 
			config.show();
		}

	}

})();

