/*
* Javascript implementations for Novel website
* (c) Papercut Media. No reproduction without permission. Feel free to ask.
* Author: Patrick Pittman
* Date: October 2007
* Version: 1.0
* Requires: Prototype.js framework, ufo.js for flash loading, Dreamgirl on the other end for AJAXy JSON communication
*/
var baseHref = "/";

function Novel() {
	this.loaderlock = false;
	this.baseHref = baseHref;
	this.ajaxBase = this.baseHref+"js/ajax/";
	this.ajaxProgress = new Image();
	this.ajaxProgress.src = this.baseHref+"images/icons/ajax-loader.gif";
	this.anchors = $H({});
	
	
	this.init = function(e) {
		Element.extend(document.body);
		fixIEPNG();
		this.setMenuRolls();	
		this.setCartLinks();	
	};
	
	this.setMenuRolls = function() {
		if ($('FirstEdNav')) { 
			$log('checking it');
			['FirstEd','MyShelf','About','Contact'].each(function(item){
						var thisItem = item;
						$log("looking at "+item);
						$(thisItem+'Nav').observe('mouseover',function(e) {
							if (this.down('img').src) {
								this.down('img').src = baseHref+"images/nav/"+thisItem+"-over.jpg";
							}
							
							// alert(this.down('img').src);
						});
						$(thisItem+'Nav').observe('mouseout',function(e) {
								if (this.down('img').src) {
									this.down('img').src = baseHref+"images/nav/"+thisItem+".jpg";
								}
						});			
			},this);
		} else {
			$log('could not find nav');
		}
	}
	
	this.setCartLinks = function() {
			$$('.addToCart').each(function(element) {
				// linkObj = element.up('a');
				element.observe('click',function(e) {
						prodID = /\[additem\]\=(\d+)\&/i.exec(decodeURIComponent(this.href));
						if (prodID) {
							e.stop();
//							alert(prodID.inspect());
							this.update("Adding...");
							novelBadge.addToCart(prodID[1]);		
						}
					});
				});
	}
	
	this.addToCart = function (prodid) {
	//	alert("adding "+prodid);
		url = this.ajaxBase+'addToCart/'+prodid;
		new Ajax.Updater('cart',url, {
		  onComplete: function(transport, json) {
			$$('.addToCart').each(function(element) {
				element.update("Add to Shelf");
			});
			new Effect.Highlight('miniCart', { duration: 2.0, startcolor: '#dfd8c7', endcolor:'#f9f7ec'})

			if (!$('successNote')) {

				var success = Builder.node('div',{
					id: 'successNote',
					display: 'none'
				},[Builder.node('h2',{
					className: 'cartSuccess'
				},"Thank you for your selection.  You can view purchases or checkout at the top right of the screen.")]);

				$('productDetailHolder').appendChild(success);
				new Effect.Appear('successNote',{duration: 1.0});
				new Effect.Fade('successNote',{duration: 6.0});
		//		setTimeout('killProgress()',2000);
			}

		  }
		});
	}	
}




function cleanupFP9IELeaks() {
  __flash_unloadHandler = function() {};
  __flash_savedUnloadHandler = function() {};
}

function fixIEPNG() {
	var version = parseFloat(navigator.appVersion.split('MSIE')[1]);

	if ((version >= 5.5) && (document.body.filters)) 
	{
	   for(var i=0; i<document.images.length; i++)
	   {
	      var img = document.images[i]
	      var imgName = img.src.toUpperCase()
	      if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
	      {
	         var imgID = (img.id) ? "id='" + img.id + "' " : ""
	         var imgClass = (img.className) ? "class='" + img.className + "' " : ""
	         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
	         var imgStyle = "display:inline-block;" + img.style.cssText 
	         if (img.align == "left") imgStyle = "float:left;" + imgStyle
	         if (img.align == "right") imgStyle = "float:right;" + imgStyle
	         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
	         var strNewHTML = "<span " + imgID + imgClass + imgTitle
	         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
	         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
	         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
	         img.outerHTML = strNewHTML
	         i = i-1
	      }
	   }
	}
}


var novelBadge = new Novel();

initNovel = function() {
	novelBadge.init();
}

document.observe("dom:loaded", initNovel);
