﻿// Global Variables
var affil_scheme = "http:";
var affil_hostname = "www.foundry42.com";
var affil_path = "partnertest";
var affil_format = "json";
var affil_maxcount = "5";
var affil_page = "";
var affil_src = "";
var affil_lmsg = "";
var affil_display_lmsg = "yes";
var affil_debugjs = "no";
var affil_feedcalled = "no";
var affil_z = "";
var cur_url = window.location.href;
var add_params = "";
var necessary_keys = new Array("keywords", "referrer", "keyword", "camp", "group", "creativeid", "traffictype");
var docPage = false;
var f42_defaultkeywords = 'business plan';

var f42_host = "docstoc.foundry42.com";
var f42_path = "tsavo/docstoc";
var f42_maxcount = "9"

function f42_showAds() {
	if (f42_checkParams()) {
		f42_callFeed();
	}
}
function f42_callFeed() {
	if (affil_feedcalled == 'no') {
		var headID = document.getElementsByTagName("head")[0];
		var newScript = document.createElement('script');
		newScript.type = 'text/javascript';
		newScript.src = affil_src;
		headID.appendChild(newScript);
		//affil_feedcalled = "yes";
	}
}
function closeWindow(){
	$('blackbg').setStyle('display', 'none');
	$('doc-search-win').setStyle('display', 'none');
}
function submitSearch(){
	doSearch('cat', 'txtSearchDocsPopup');
	
}

function renderWindow() {
	// Call function to show Ads
	f42_showAds();

}
var ds_ppcSearchTerm ='';
// Renders the feed from returned JSON object
function jsonPPCFeed(feed) {
	if (feed.items.length == 0 && docPage) {
		f42_defaultkeywords = '';

		f42_checkParams();
		f42_callFeed();
	}

	ds_ppcSearchTerm = feed.ppckeywords.replace(/'/g, '\\\'');
	trackEvent('Docstoc', 'Displayed Tsavo Ad with (' + feed.items.length + ') results', "'" + ds_ppcSearchTerm + "'", 1);

	if (feed.items.length == 0) {return;}
	//make screen go blank
	DisableScreen('blackbg', '#000000', '.7');
	
	var u = f42_getUrlAndPath(false);

	var itemsHTML = RenderBlock(u, feed, 'ad1', 0, 4);
	

	
	var div = document.createElement('div');
	div.id = 'doc-search-win';
	div.style.top = ($(document.body).getScrollTop() + 70) + 'px';
	div.innerHTML = '<div class="header-top">' +
		'<a href="javascript:closeWindow();"></a>' +
		'<div><h1>"' + feed.ppckeywords + '"<b>?</b></h1></div>' +
	'</div>' +
	'<div class="header-mid">' +itemsHTML +	'</div>' +
	'<div class="header-bot">' +
		'<input type="text" id="txtSearchDocsPopup" onfocus="clearSearch(this)" value="search all documents" onkeypress="return handleSearchEnter(event,\'cat\')" />' +
		'<h6>Advertisement</h6>' +
		'<a href="javascript:submitSearch()"></a>' +
	'</div>';


	document.body.appendChild(div);
	
	//window.scrollTo(0, 100)
	//window.scrollTop
}

function dc_RenderAd(u, obj) {
	var t = '';
	var clickEvent = ' onclick = "trackEvent(\'Docstoc\',\'Clicked Tsavo Ad\',\'' + ds_ppcSearchTerm + '\',1)" ';
	t += '<div class="item" onmouseover="this.style.backgroundColor=\'#ebebeb\'"  onmouseout="this.style.backgroundColor=\'#fff\'">' +
			'<h1><a href="' + u + obj.clickurl + '" ' + clickEvent + ' target="_blank">' + obj.ppctitle + '</a></h1>' +
			'<h2><a href="' + u + obj.clickurl + '" ' + clickEvent + ' target="_blank">' + obj.ppcdescription + '</a></h2>' +
			'<a href="' + u + obj.clickurl + '" ' + clickEvent + ' target="_blank">' + obj.ppchost + '</a>' +
		'</div>';
	return t;
}

function RenderBlock(u, feed, obj, startCount, endCount) {
	var t = '';
	if (startCount > feed.items.length) { return; }
	if (endCount > feed.items.length) { endCount = feed.items.length; }
	var u = f42_getUrlAndPath(false);
	for (x = startCount; x < endCount; x++) {
		var clkurl = feed.items[x].clickurl;

		if (clkurl.substring(4, 0) == 'http') { u = ''; }
		t += dc_RenderAd(u, feed.items[x])
	}
	return t;
}
function f42_getUrlAndPath(withpath) {
	var returl = "";
	var cur_scheme = document.location.protocol;
	if (cur_scheme == 'https:')
		affil_scheme = cur_scheme;
	try {  //attempt to get js var f42_host
		if (f42_host != null && f42_host != "")
			affil_hostname = f42_host;
	} catch (e) { }
	try {  //attempt to get js var f42_path
		if (f42_path != null && f42_path != "")
			affil_path = f42_path;
	} catch (e) { }

	returl = affil_scheme + '//';
	returl += affil_hostname;
	if (withpath) returl += '/' + affil_path;
	return returl;

}

function affilwtpopup() {
	wtpopupwin = window.open('http://media.foundry42.com/partner/inc/wt.html', 'wtpopup', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=250');
}
function f42_checkParams() {
	if (affil_feedcalled == "no") {
		var url_params = new Object();
		var url_params2 = new Array();
		try {
			for (var i = 0; i < necessary_keys.length; i++) {
				if (url_params[necessary_keys[i]] != undefined) url_params2.push(necessary_keys[i] + '=' + url_params[necessary_keys[i]]);
				else if (necessary_keys[i] == "keywords" && url_params[necessary_keys[i]] == undefined) {
					if (f42_defaultkeywords == '') return false;
					url_params2.push(necessary_keys[i] + '=' + encodeURIComponent(f42_defaultkeywords));
				}
			}
			url_params2.push('format=' + affil_format);

			try {  //attempt to get js var f42_maxcount
				if (f42_maxcount != '')
					affil_maxcount = f42_maxcount;
			} catch (e) { }
			url_params2.push('maxcount=' + affil_maxcount);


			try {  //attempt to get js var f42_css
				if (f42_css != '')
					url_params2.push('css=' + encodeURIComponent(f42_css));
			} catch (e) { } // carry on without CSS

			if (url_params["serveUrl"] != undefined) {
				cur_url = url_params["serveUrl"];
			}
			var serve_url = cur_url;
			if (cur_url.indexOf('?') > 0) {
				serve_url = cur_url.substr(0, cur_url.indexOf('?'));
			}
			url_params2.push('serveUrl=' + encodeURIComponent(serve_url));

			if (url_params2.length != undefined && url_params2.length > 0) add_params = url_params2.join("&");
			if (add_params != "") {

				affil_src = f42_getUrlAndPath(true) + '/';
				affil_src += affil_page + '?';
				affil_src += add_params;

			}
		} catch (e) { return false; }
	} // end if havefeed = no
	return true;
}

function ctWinOpen(url) {
	var url = url;
	window.open(url);
}

function launchPopup() {
	var firstTime = getCookie("first_time");
	var pages_visited = getCookie("pages_visited");
	var searchTerm = GetGoogleSearchTerm();
	
	if (firstTime==1 && pages_visited==1 && (searchTerm!=undefined && searchTerm.length>0)) {
		//alert("is first time: " + firstTime + "\r\n Pages Visited: " + pages_visited + "\r\n Ref Keyword: " + searchTerm);
		f42_defaultkeywords = searchTerm;
		renderWindow();
	}
	
}

setTimeout(launchPopup, 3500);

