/* ----------------------------------------------------------------------
   Javascript scripts for artlinksgalore.com -- Copyright 2007-2009 Pictolink.com
   ---------------------------------------------------------------------- */

// Scripts for use by main window

function pl_initialize()
{
	pl_params = unescape(location.search.substr(1)); // get parameters less leading "?" character
	//var pl_test = "";
	var pl_param_hash = new Array();
	if (pl_params) {
		var tempParamsArray = pl_params.split("&"); // get individual param pairs
		var tempArray = new Array();
		for (var i = 0; i < tempParamsArray.length; i++) {
			tempArray = tempParamsArray[i].split("=");
			pl_param_hash[tempArray[0]] = tempArray[1];
			//pl_test += (tempArray[0] + " = " + tempArray[1] + ", ");
		}
	}

	pl_referrer_value = pl_param_hash["pl_referrer"];
	if (pl_referrer_value && pl_referrer_value.length>0) {
		msgDiv = document.getElementById("referrer-div");
		msgDiv.innerHTML = "You have been redirected from a currently inactive web site:<br><i>" + pl_referrer_value + "</i><br>Enjoy the Pictolinks&trade; below.";
		msgDiv.style.display="block"; // display the div

		// log the referrer for this access
		writeEventLogRecord("ref", null, "ref:"+pl_referrer_value);
	}
	else initBannerAds();

	// pl_display_buttons(); -- Does not work in Safari
	// pl_display_loading_message();
}

/*
// Does not work in Safari -- Buttons to be normally inline loaded
function pl_display_buttons()
{

	var buttonContentContainerElem = document.getElementById("button-content-container");
	var buttonContentDivElem = document.getElementById("button-content-div");
	var buttonTargetDivElem = document.getElementById("button-target-div");
	var tempContent = buttonContentContainerElem.removeChild(buttonContentDivElem);
	buttonTargetDivElem.appendChild(tempContent);
	tempContent.style.display = ""; // unhide by removing the "display: none" style
}
*/

/*
function pl_display_loading_message() {
	var mosaicDivElem = document.getElementById("pl_mosaic_div");
	mosaicDivElem.innerHTML = '<div style="font-family: Comic Sans MS, Brush Script MT, cursive; font-size:12pt; color:darkblue"><div style="font-size:28pt; color:rgb(209, 209, 238); padding-top:140px; padding-left:10px">Downloading!</div></div>';
}
*/

self.infoWindow = null; // information window object

// Opens or sets focus to information window
function openInfoWindow(anchor, infoWindowURL)
{
	// Internet Explorer will NOT accept a window name that includes hyphens
	infoWindowName = "alg_info_window";
	if (!infoWindowURL) {
		infoWindowURL = "alg-info.htm"
	}
	if (anchor) {
		infoWindowURL = infoWindowURL + "#" + anchor;
	}

	if (self.infoWindow) {
		self.infoWindow.close(); // Get rid of the old one because can not give focus for some reason
	}
	self.infoWindow = window.open(infoWindowURL, infoWindowName);
	self.infoWindow.focus();
}

function closeInfoWindow()
{

	// may not work
	if (history.length>1) {
		history.back(-1);
	}

	// The first window -- attempt to go back to main window opener
	iWindow = self.infoWindow;
	if (!iWindow && self.opener) {
		iWindow = self.opener.infoWindow;
	}
	if (iWindow) {
		iWindow.close();
	}
	else {
		history.back(-1);
	}
}


// Ad Presentation Scripts

/////////////////////// BANNER AD DATA ////////////////////

var adBanners = new Array();
var nAds = 0;
var selectedBannerAd = null;

function BannerAdData(bannerImagePath, targetURL, adName) {
	this.bannerImagePath = bannerImagePath; // banner image displayed at top of Kaleidopost web page
	this.targetURL = targetURL; // target url
	this.adName = adName; // name of banner ad
}

function initBannerAds() {
	// load ad data
	var bannerAdDir = "images/banner-ads/";
	adBanners[nAds++] = new BannerAdData(bannerAdDir + "grsites-ad.jpg", "http://www.grsites.com/archive/textures/", "grsites-ad");
	adBanners[nAds++] = new BannerAdData(bannerAdDir + "cardscan-ad.png", "http://www.cardscan.com/products/core_contact_mgmt/executive/index.asp", "cardscan-ad");
	adBanners[nAds++] = new BannerAdData(bannerAdDir + "comfortinn-ad.png", "http://www.choicehotels.com/shtml/ComfortInn/", "comfortinn-ad");
	adBanners[nAds++] = new BannerAdData(bannerAdDir + "capitalone-ad.png", "http://www.capitalone.com/creditcards/compare/banners/newoffers.php", "capitalone-ad");
	adBanners[nAds++] = new BannerAdData(bannerAdDir + "verizon-ad.png", "http://www22.verizon.com/content/ConsumerFios", "verizon-ad");
	adBanners[nAds++] = new BannerAdData(bannerAdDir + "newyorktimes-ad.png", "https://www.nytimesathome.com/offer.php", "newyorktimes-ad");
	adBanners[nAds++] = new BannerAdData(bannerAdDir + "delta-ad.gif", "http://www.delta.com/home/index.jsp", "delta-ad");
	adBanners[nAds++] = new BannerAdData(bannerAdDir + "artdeadlineslist-ad.png", "http://artdeadlineslist.com/", "artdeadlineslist-ad");
	adBanners[nAds++] = new BannerAdData(bannerAdDir + "artdeadlineslist-ad.png", "http://artdeadlineslist.com/", "artdeadlineslist-ad"); // 2nd instance to double the probability of display

	// select a random ad
//	var selectedAdIdx = new Date().getTime() % (nAds-1); // Convert the current time into a mostly random number between 0 & the index of the last ad
	// 10/21/07 -- Always load ArtDeadlinesList.com ad
	var selectedAdIdx = 8;
	selectedBannerAd = adBanners[selectedAdIdx];

	loadBannerAdImage();
}

// Move this into pl_mosaic_code.js
var pl_event_image = null;
function writeEventLogRecord(eventType, duration, comment)
{
	// load small image - used as mechanism to transmit parameters back to server
	pl_event_image = new Image();
	eType = eventType ? eventType : "unspecified_event";
	parameters = "presenter_id=" + plSiteId + "&instance_id=" +  pl_instanceId + "&event_type=" +  eType + "&version=" // +  pl_code_version;
	if (duration) {
		parameters += "&duration=" +  duration;
	}
	if (comment) {
		parameters += "&comment=" +  comment;
	}

	pl_event_image.src = plRegisterEventPathPrefix + "register_event.rb?" + parameters;
}

function openAdTarget()
{
	adWindowName = "alg_ad_window"; // IE 7 requires underscore, not dash, in window name
	adWindowURL = selectedBannerAd.targetURL;

	self.adWindow = window.open(adWindowURL, adWindowName);
	self.adWindow.focus();

	// log the opening of this ad web site
	writeEventLogRecord("bnr", null, "bnr:"+selectedBannerAd.adName);
}

function loadBannerAdImage()
{
	bannerImage = document.getElementById("top-banner-ad-image");
	adDiv = document.getElementById("ad-space");
	if (bannerImage!=null && adDiv!=null) {
		selectedAdPath = selectedBannerAd.bannerImagePath;
		bannerImage.src = selectedAdPath;
		adDiv.style.display="block"; // display the ad
	}
}

/*
// Scripts for use by info window
function goBackToOpenerWindow()
{
	if (self.window.opener) {
//		moveWindowOffscreen(); // simulate a minimize by moving the info window far offscreen
		self.window.opener.focus();
	}
}
*/

// Not placed into pl_mosaic_code_xxx.js because of critical race loading issues
function isSafari()
{
	return (navigator.userAgent.indexOf("Safari") != -1);
}

isArtsScriptLoaded = true;