if (!BOOK_THE_SEQUEL) {
	var BOOK_THE_SEQUEL = {};
}

BOOK_THE_SEQUEL.bannerSelection = 1;
BOOK_THE_SEQUEL.bannerBehavior = "overlay";

BOOK_THE_SEQUEL.XHRContentLoader = function(url, containerId) {
	this.url = url;
	this.containerId = containerId;
}

BOOK_THE_SEQUEL.XHRContentLoader.prototype = {
	loadContent: function() {
		var options = {
			method : "get",
			onSuccess: this._success.bind(this),
			onException: this._exception.bind(this)
		};
		new Ajax.Request(this.url, options);
	},
	
	_success : function(req) {
		var data = req.responseText;
		$(this.containerId).insert(data);
	},
	
	/*
	* Function makes exception/error info explicit
	*	@param {string} req - the Ajax request
	*	@param {array} ex - an array containing exception keys and meanings
	*/
	_exception : function(req, ex) {
		var errorString = "";
		var template = new Template("#{key}: #{error} <br\/>");
		for(var key in ex) {
			errorString += template.evaluate({key: key, error: ex[key]});
		}
		//alert(errorString);
	}
}

BOOK_THE_SEQUEL.getRandomSequel = function(containerId) {
	var newSequel = new BOOK_THE_SEQUEL.XHRContentLoader("random_sequels.php?ret=1&rand=" + Math.random() + "&rnd=" + Math.random(), containerId);
	newSequel.loadContent({ fadeIn : true });
}

BOOK_THE_SEQUEL.replaceSequel = function(sequel, index) {
	var newSequel = new Element('blockquote');
	newSequel.update(sequel.innerHTML);
	sequel.fade({ duration: 3.0, from: 1, to: 0 });
	var next = index + 1;
	setTimeout(function(){
		BOOK_THE_SEQUEL.refreshSequel(next);
	}, 15000);
	setTimeout(function(){
		BOOK_THE_SEQUEL.getRandomSequel('sideBar');
	}, 18000);
}

BOOK_THE_SEQUEL.refreshSequel = function(index) {
	var sequel = $$('#sideBar blockquote')[index];
	BOOK_THE_SEQUEL.replaceSequel(sequel, index);
}

BOOK_THE_SEQUEL.loadFunctions = function() {
	
	var preloader = new Element('img');
	preloader.src = "http:\/\/www.bookthesequel.com\/images\/modal.png";
	
	iphonePattern = new RegExp("iphone", "i");
	
	var clearingDiv = new Element('div');
	clearingDiv.style.clear = "both";
	clearingDiv.style.height = "1px";
	clearingDiv.style.overflow = "hidden";
	
	var windowHeight = document.viewport.getHeight();
	
	if ($$('a.bts_link').length > 0 && !iphonePattern.match(navigator.userAgent)) {
		var modalWindow = new Element('div');
		modalWindow.style.margin = 0;
		modalWindow.style.padding = 0;
		modalWindow.style.position = "fixed";
		modalWindow.style.zIndex = 9000;
		modalWindow.style.width = "100%";
		modalWindow.style.height = "100%";
		modalWindow.style.overflow = "auto";
		modalWindow.style.left = 0;
		modalWindow.style.top = 0;
		modalWindow.style.textAlign = "center";
		modalWindow.style.backgroundImage = "url(http:\/\/www.bookthesequel.com\/images\/modal.png)";
		modalWindow.style.backgroundRepeat = "repeat";
		modalWindow.style.backgroundAttachment = "scroll";
		modalWindow.hide();
		
		var entryBox = new Element('div');
		entryBox.style.width = "700px";
		entryBox.style.margin = "20px auto 0 auto";
		entryBox.style.backgroundColor = "#EEEEEE";
		entryBox.style.border = "2px solid #CCCCCC";
		entryBox.style.borderRightColor = "#666666";
		entryBox.style.borderBottomColor = "#666666";
		entryBox.style.padding = 0;
		
		var boxHeader = new Element('div');
		boxHeader.style.padding = "3px 3px 3px 5px";
		boxHeader.style.margin = "0 0 8px 0";
		boxHeader.style.lineHeight = "15px";
		boxHeader.style.fontSize = "12px";
		boxHeader.style.fontFamily = "'Trebuchet MS', Verdana, Arial, Helvetica, sans-serif";
		boxHeader.style.fontWeight = "bold";
		boxHeader.style.textAlign = "left";
		boxHeader.style.backgroundColor = "#EEEEEE";
		boxHeader.style.backgroundImage = "url(http:\/\/www.bookthesequel.com\/images\/box_head_bkgrnd.jpg)";
		boxHeader.style.backgroundRepeat = "repeat-x";
		boxHeader.style.backgroundPosition = "left bottom";
		
		var headLink = new Element('span');
		headLink.update("&raquo; <em>Book: The Sequel</em>");
		headLink.style.color = "#666666";
		
		var closeBox = new Element('div');
		closeBox.style.cssFloat = "right";
		closeBox.style.styleFloat = "right";
		
		var closeLink = new Element('a');
		closeLink.title = "close";
		closeLink.update("close");
		closeLink.style.display = "block";
		closeLink.style.width = "25px";
		closeLink.style.height = "15px";
		closeLink.style.overflow = "hidden";
		closeLink.style.fontSize = "1px";
		closeLink.style.textAlign = "left";
		closeLink.style.textIndent = "-5000px";
		closeLink.style.backgroundImage = "url(http:\/\/www.bookthesequel.com\/images\/close.gif)";
		closeLink.style.backgroundRepeat = "no-repeat";
		closeLink.style.backgroundPosition = "left top";
		closeLink.onmouseover = closeLink.onfocus = function(){
			closeLink.style.backgroundPosition = "left bottom";
		}
		closeLink.onmouseout = closeLink.onblur = function(){
			closeLink.style.backgroundPosition = "left top";
		}
		closeLink.onclick = function(){
			modalWindow.hide();
		}
		
		var frameHeight = 880;
		if (parseInt(windowHeight) && parseInt(windowHeight) > 150) {
			var requiredHeight = frameHeight + 70;
			frameHeight = windowHeight < requiredHeight ? windowHeight - 70 : frameHeight;
		}
		
		var entryBoxSub = new Element('div');
		
		closeBox.insert(closeLink);
		boxHeader.insert(closeBox);
		boxHeader.insert(headLink);
		boxHeader.insert(clearingDiv);
		entryBox.update(boxHeader);
		entryBox.insert(entryBoxSub);
		modalWindow.insert(entryBox);
		
		$(document.body).insert({
			top : modalWindow
		});
		
		$$('a.bts_link').each(function(i){
			i.target = "_self";
			i.href = "#";
			i.onclick = function(){
				entryBoxSub.update('<iframe src="http:\/\/www.bookthesequel.com\/submission_form.php?hd=n&rnd=' + Math.random() + '" frameborder="0" style="border: none; width: 100%; height: ' + frameHeight + 'px; overflow: auto;"><\/iframe>');
				modalWindow.show();
			}.bind(this);
		}.bind(this));
		
	} // end if
	
	
	if ($('email_page_link') && !iphonePattern.match(navigator.userAgent)) {
		
		$('email_page_link').target = "_self";
		$('email_page_link').href = "#";
	
		var modalWindow2 = new Element('div');
		modalWindow2.style.margin = 0;
		modalWindow2.style.padding = 0;
		modalWindow2.style.position = "fixed";
		modalWindow2.style.zIndex = 9500;
		modalWindow2.style.width = "100%";
		modalWindow2.style.height = "100%";
		modalWindow2.style.overflow = "auto";
		modalWindow2.style.left = 0;
		modalWindow2.style.top = 0;
		modalWindow2.style.textAlign = "center";
		modalWindow2.style.backgroundImage = "url(http:\/\/www.bookthesequel.com\/images\/modal.png)";
		modalWindow2.style.backgroundRepeat = "repeat";
		modalWindow2.style.backgroundAttachment = "scroll";
		modalWindow2.hide();
		
		var entryBox2 = new Element('div');
		entryBox2.style.width = "700px";
		entryBox2.style.margin = "20px auto 0 auto";
		entryBox2.style.backgroundColor = "#EEEEEE";
		entryBox2.style.border = "2px solid #CCCCCC";
		entryBox2.style.borderRightColor = "#666666";
		entryBox2.style.borderBottomColor = "#666666";
		entryBox2.style.padding = 0;
		
		var boxHeader2 = new Element('div');
		boxHeader2.style.padding = "3px 3px 3px 5px";
		boxHeader2.style.margin = "0 0 8px 0";
		boxHeader2.style.lineHeight = "15px";
		boxHeader2.style.fontSize = "12px";
		boxHeader2.style.fontFamily = "'Trebuchet MS', Verdana, Arial, Helvetica, sans-serif";
		boxHeader2.style.fontWeight = "bold";
		boxHeader2.style.textAlign = "left";
		boxHeader2.style.backgroundColor = "#EEEEEE";
		boxHeader2.style.backgroundImage = "url(http:\/\/www.bookthesequel.com\/images\/box_head_bkgrnd.jpg)";
		boxHeader2.style.backgroundRepeat = "repeat-x";
		boxHeader2.style.backgroundPosition = "left bottom";
		
		var closeBox2 = new Element('div');
		closeBox2.style.width = "25px";
		closeBox2.style.cssFloat = "right";
		closeBox2.style.styleFloat = "right";
		
		var closeLink2 = new Element('a');
		closeLink2.title = "close";
		closeLink2.update("close");
		closeLink2.style.display = "block";
		closeLink2.style.width = "25px";
		closeLink2.style.height = "15px";
		closeLink2.style.overflow = "hidden";
		closeLink2.style.fontSize = "1px";
		closeLink2.style.textAlign = "left";
		closeLink2.style.textIndent = "-5000px";
		closeLink2.style.backgroundImage = "url(http:\/\/www.bookthesequel.com\/images\/close.gif)";
		closeLink2.style.backgroundRepeat = "no-repeat";
		closeLink2.style.backgroundPosition = "left top";
		closeLink2.onmouseover = closeLink2.onfocus = function(){
			closeLink2.style.backgroundPosition = "left bottom";
		}
		closeLink2.onmouseout = closeLink2.onblur = function(){
			closeLink2.style.backgroundPosition = "left top";
		}
		closeLink2.onclick = function(){
			modalWindow2.hide();
		}
		
		var frameHeight2 = 700;
		if (parseInt(windowHeight) && parseInt(windowHeight) > 150) {
			var requiredHeight = frameHeight2 + 70;
			frameHeight2 = windowHeight < requiredHeight ? windowHeight - 70 : frameHeight2;
		}
		
		var entryBoxSub2 = new Element('div');
		
		closeBox2.insert(closeLink2);
		boxHeader2.insert(closeBox2);
		boxHeader2.insert("Spread the Word!");
		boxHeader2.insert(clearingDiv);
		entryBox2.insert(boxHeader2);
		entryBox2.insert(entryBoxSub2);
		modalWindow2.insert(entryBox2);
		
		$(document.body).insert({
			top : modalWindow2
		});
		
		$('email_page_link').onclick = function(){
			entryBoxSub2.update('<iframe src="http:\/\/www.bookthesequel.com\/email_form.php?rnd=' + Math.random() + '" frameborder="0" style="border: none; width: 100%; height: ' + frameHeight2 + 'px; overflow: auto;"><\/iframe>');
			modalWindow2.show();
		}
		
	} // end if email_page_link exists
	
	if (($('homePage') || ($('bookexpoPage'))) && $('sideBar')) {
		setTimeout(function(){
			BOOK_THE_SEQUEL.refreshSequel(0);
		}, 15000);
		setTimeout(function(){
			BOOK_THE_SEQUEL.getRandomSequel('sideBar');
		}, 15000);
	}
	
	if ($('participantsPage') && $('sideBar')) {
		var firstAd = new Element('div', {
			id : 'firstVid',
			title : 'Click to Play (Requires Flash Player)'
		});
		firstAd.style.width = '225px';
		firstAd.style.margin = '20px auto';
		firstAd.style.textAlign = 'center';
		var secondAd = new Element('div', {
			id : 'secondVid',
			title : 'Click to Play (Requires Flash Player)'
		});
		secondAd.style.width = '225px';
		secondAd.style.margin = '20px auto';
		secondAd.style.textAlign = 'center';
		var playButton1 = new Element('img', {
			src : 'images/bts_a.jpg',
			alt : 'Click to Play (Requires Flash Player)',
			width : 225,
			height : 188
		});
		playButton1.style.cursor = 'pointer';
		var playButton2 = new Element('img', {
			src : 'images/bts_b.jpg',
			alt : 'Click to Play (Requires Flash Player)',
			width : 225,
			height : 188
		});
		playButton2.style.cursor = 'pointer';
		firstAd.update(playButton1);
		secondAd.update(playButton2);
		$('sideBar').insert(firstAd);
		$('sideBar').insert('<p style="text-align: center; font-size: 75%; font-weight: bold;">Ads courtesy of <a href="http://www.versoadvertising.com" target="_blank" style="white-space: nowrap;">Verso Reader Channels</p>');
		$('sideBar').insert(secondAd);
		playButton1.onclick = function() {
			var so = new SWFObject('images/bts_a.swf', 'VersoFlash1', 225, 188, '8', '#FFFFFF');
			so.addParam('scale', 'showall');
			so.addParam('wmode', 'transparent');
			so.addParam('loop', 'true');
			so.write('firstVid');
			secondAd.update(playButton2);
		}
		playButton1.onclick();
		playButton2.onclick = function() {
			var so = new SWFObject('images/bts_b.swf', 'VersoFlash1', 225, 188, '8', '#FFFFFF');
			so.addParam('scale', 'showall');
			so.addParam('wmode', 'transparent');
			so.addParam('loop', 'true');
			so.write('secondVid');
			firstAd.update(playButton1);
		}
	}
	
	if ($('widgetsindexPage')) {
		$$('textarea').each(function(i){
			i.onfocus = function() {
				this.select();	
			}
		});
		$$('div.bannerBox').each(function(i, index){
			var bannerId = index + 1;
			i.onclick = function(){
				BOOK_THE_SEQUEL.pickBanner(bannerId);
			}.bind(this);
			i.onmouseover = i.onfocus = function(){
				i.style.backgroundColor = "#FFFF99";	
			}.bind(this);
			i.onmouseout = i.onblur = function(){
				i.style.backgroundColor = "transparent";	
			}.bind(this);
		});
		$('behaviorOverlay').onclick = BOOK_THE_SEQUEL.setWidgetBehavior;
		$('behaviorLink').onclick = BOOK_THE_SEQUEL.setWidgetBehavior;
	}
	
	/*if ($('homePage')) {
		var audioClip = new Element('div', { id : 'Audio' });
		audioClip.style.textAlign = 'center';
		audioClip.style.fontSize = '75%';
		audioClip.style.fontStyle = 'italic';
		audioClip.style.color = '#333333';
		audioClip.style.margin = '2em auto';
		$('mainCol').insert({
			top : audioClip
		});
		var qt = new QTObject('WNYC-52609-BookSequel.mp3', 'Audio', 300, 16);
		qt.addParam('autostart', 'false');
		qt.write('Audio');
		var audioCounter = 0;
		$$('#Audio embed').each(function(i){
			i.style.margin = "0 auto";
			audioCounter++;
		});
		$$('#Audio object').each(function(i){
			i.style.margin = "0 auto";
			audioCounter++;
		});
		if (audioCounter > 0) {
			audioClip.insert('<span style="display: block;">Listen to Clive Priddle, editor of<br />BOOK: The Sequel, on WNYC Radio</span>');
		}
	}*/
	
} // end loadFunctions

BOOK_THE_SEQUEL.pickBanner = function(bannerId) {
	var inputName = "bannerSelex" + bannerId;
	$(inputName).click();
	BOOK_THE_SEQUEL.bannerSelection = bannerId;
	BOOK_THE_SEQUEL.buildWidget();
}

BOOK_THE_SEQUEL.setWidgetBehavior = function() {
	BOOK_THE_SEQUEL.bannerBehavior = $('behaviorOverlay').checked == true ? "overlay" : "link";
	BOOK_THE_SEQUEL.buildWidget();
}

BOOK_THE_SEQUEL.buildWidget = function() {
	var codeString = "<a href=\"http:\/\/www.bookthesequel.com\" target=\"_blank\" id=\"book_the_sequel_link\">";
	if (BOOK_THE_SEQUEL.bannerSelection == 1) {
		codeString += "TEXT OF YOUR LINK HERE";	
	} else {
		var imgId = "bannerImg" + BOOK_THE_SEQUEL.bannerSelection;
		var bannerImg = $(imgId);
		codeString += "<img src=\"";
		codeString += bannerImg.src;
		codeString += "\" border=\"0\" width=\"";
		codeString += bannerImg.width;
		codeString += "\" height=\"";
		codeString += bannerImg.height;
		codeString += "\" alt=\"Book: The Sequel - Be a part of it; Enter today!\" \/>";
	}
	codeString += "<\/a>";
	if (BOOK_THE_SEQUEL.bannerBehavior == "overlay") {
		codeString += "<script type=\"text\/javascript\" src=\"http:\/\/www.bookthesequel.com\/scripts\/book_the_sequel.js\"></script>";	
	}
	$('codeBox1').value = codeString;
}

Event.observe(window, "load", BOOK_THE_SEQUEL.loadFunctions);

