//Product Selection Tool
function prodSelector(){
	var categoryInput = $('category');
	var categoryDisplay = $('categoryDisplay');
	var selectorContainer = $('productSelector');
	if(!selectorContainer) return false;
	var statusContainer = $('selectorStatus');
	var categoryContainer = $('productCategories');
	var submitButtons = $$('p.submit');
	var resetButtons = $$('label.reset');
	var selectBoxes = $$('#hasIE6 #main select');
	
	//Manage all click related events on the categories and subcategories.
	var categoryMasterList = categoryContainer.down('ul');
	
	normalizeListHeight(categoryMasterList);
	var categoryMasterHTML = categoryMasterList.innerHTML;
	
				
	var chosenCategories = new Element('ul');
	chosenCategories.addClassName('chosenCategories');
	
	statusContainer.down('p.submit').insert({before: chosenCategories});
	
	categoryInput.value = 'Select Category >'; //Reset the value on start.
	//Observe the click on the input and bring up the selection window. Create a darkened lightbox
	//back-drop to focus the user's attention.  Hide panel and destroy background on click out.
	
	//Function - Normalize List Height
	//Parameter: listContainer - Containing Unordered List Element
	//Go into the Categories panel and fix the heights of the LIs by
	//grabbing the height of the third item and matching the previous LI heights to it
	function normalizeListHeight(listContainer){
		var listElements = listContainer.childElements('li');
		//console.log(listElements);
		
		listElements.eachSlice(3, function(listSlice){
			var maxHeight = listSlice.max(function(listElement){
				return listElement.getHeight();
			});
			
			listSlice.each(function(listElement){
				listElement.setStyle({
					height: maxHeight + 'px'
				});
			});
		});
	}
	
	categoryDisplay.observe('click', function(event){
		selectorContainer.addClassName('shown');
		selectorContainer.hide();
		
		categoryMasterList.innerHTML = categoryMasterHTML;
		//console.log(categoryMasterHTML);
		var categoryLinks = categoryMasterList.select('li');
		
		categoryLinks.each(function(categoryLink, index) {
			categoryLink.observe('click', function(event){
		
				statusContainer.appear();
				var subCategory = categoryLink.down('ul');
				var previousList = categoryLink.up('ul');
				
				if(subCategory){
					var categoryName = subCategory.up('li').down('a').innerHTML.unescapeHTML();
					
					categoryDisplay.update(categoryDisplay.innerHTML + '<span>' + categoryName + '</span>');
					
					categoryInput.value = categoryName;
					
					previousList.remove();
					
					categoryContainer.down('h2').insert({after: subCategory});
					
					categoryItem = new Element('li');				
					categoryItem.update(categoryName);
					
					chosenCategories.insert({bottom: categoryItem});
					
					normalizeListHeight(categoryContainer.down('ul'));
					
				} 
				else{
					var categoryName = this.down('a').innerHTML.unescapeHTML();
					categoryInput.value = categoryName;
					categoryDisplay.update(categoryDisplay.innerHTML + '<span>' + categoryName + '</span>');
					blackout.fire('parker:closeSelector');
				}
			});
			
		});
		
		new Effect.Appear(selectorContainer, {duration: .5});		
		
		var viewport = document.viewport.getDimensions();
		var viewportWidth = viewport.width; // Usable window width
		var viewportHeight = viewport.height; // Usable window height
		var pageBody = $('main');
		var pageBodyOffset = pageBody.cumulativeOffset();
		
		//Reset list contents to the Master Category List
		categoryContainer.down('ul').remove();
		categoryContainer.insert({
			bottom: categoryMasterList
		});
		
		if(chosenCategories.down('li')){
			chosenCategories.innerHTML = "";
		}
		
		//To stop IE6 select box zindex issue, we hide them while the tools in use.
		selectBoxes.each(function(selectBox){
			selectBox.hide();
		});
		
		//Create page blackout 
		blackout = new Element('div');
		blackout.addClassName('blackout');
		blackout.setOpacity(.5);
		blackout.setStyle({
			width: viewportWidth + 'px',
			height: viewportHeight + 'px'
		});
		
		//reposition the tool to the top center of the page.
		selectorContainer.setStyle({
			left: pageBodyOffset.left + 'px',
			top: pageBodyOffset.top + 'px'
			
		});
		
		pageBody.insert({
			bottom: blackout
		});
		
		//Create Close Button
		closeButton = new Element('p');
		closeButton.addClassName('close');
		closeButton.update('Close Selector');
		
		categoryContainer.insert({
			top: closeButton
		});
		
		//Handle the submission events.
		blackout.observe('click', function(event){
			blackout.fire('parker:closeSelector');
		});
	
		closeButton.observe('click', function(event){
			blackout.fire('parker:closeSelector');
		});
	});	
		
	//Handle the final submission event.
	submitButtons.each(function(submitButton){
		submitButton.observe('click', function(event){
			blackout.fire('parker:closeSelector');		
		});
	});
	
	resetButtons.each(function(resetButton){
		resetButton.observe('click', function(event){
			window.location.reload();
		});
	});
	
	statusContainer.setStyle({
		opacity: 0
	});
	
	document.observe('parker:closeSelector', function(event){		
		blackout.remove();
		closeButton.remove();
		
		new Effect.Fade(selectorContainer, {duration: .2});
		
		selectBoxes.each(function(selectBox){
			selectBox.show();
		});
	});
	
}

//Resize blackout on window resize.
function resizeBlackout(){
	var blackouts = $$('div.blackout');			
	
	var selectorContainer = $('productSelector');
	var viewport = document.viewport.getDimensions();
	var viewportWidth = viewport.width; // Usable window width
	var viewportHeight = viewport.height; // Usable window height
	var pageBody = $('main');
	var pageBodyOffset = pageBody.cumulativeOffset();
	
	if(selectorContainer.hasClassName('shown')){
		
		blackouts.each(function(blackout){
			blackout.setStyle({
				width: viewportWidth + 'px',
				height: viewportHeight + 'px'
			});
		});	
			
		selectorContainer.setStyle({
			left: pageBodyOffset.left + 'px',
			top: pageBodyOffset.top + 'px'
			
		});
	
	}
}


//Country Selection reveal City/State
function countryTool(){
	var cityStateFields = $$('label.optional');
	var countryOptions = $$('select.countrySelect option');
	var countrySelects = $$('select.countrySelect');
		
		countrySelects.each(function(countrySelect) {
			countrySelect.observe('change', function(event){
				
				var selectOptions = countrySelect.select('option');
				
				selectOptions.each(function(selectOption){
					if(selectOption.selected == true){
						if(selectOption.hasClassName('hasCityState')){
							cityStateFields.each(function(cityStateField){
								cityStateField.setStyle({
									display: 'block'
								});
							});
						} else {
							cityStateFields.each(function(cityStateField){
								cityStateField.setStyle({
									display: 'none'
								});
							});
						}
					}	
				});
			});
		});	
}

//First and Last LI Selector
//Note: Prototype Driven
function liFirstLast() {
	var firstLIs =	$$('ul > li:first-child');
	var lastLIs = $$('ul > li:last-child');
	
	firstLIs.each(function(liFirst) {
		liFirst.addClassName('first');
		});
		
	lastLIs.each(function(liLast) {
		liLast.addClassName('last');
	});
}

//External Link Helper
//Updates Links with External Relation to
//use target="_blank"
function externalLinks(){
	var links = $$('a[rel=external]');
	
	links.each(function(externalLink){
		externalLink.writeAttribute('target', '_blank');
	});
}

//Input Clear
//Clears text inputs on a page on focus
//Note: Prototype driven
function inputClear() {
	var textInputs = $$('input[type="text"]');
	
	textInputs.each(function(textInput){
		textInput.initialValue = textInput.value;
		textInput.observe('focus', function(event) {
			if(textInput.value == textInput.initialValue){
				textInput.clear();
			}
		});
		textInput.observe('blur', function(event){
			if(textInput.value.blank() == true) {
				textInput.value = textInput.initialValue;
			}
		});
	});
}

// Cookie Functions
// Set the cookie 
function setCookie(name, value, expires, path, domain, secure) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

// Read the cookie 
function readCookie(name) { 
	var needle = name + "="; 
	var cookieArray = document.cookie.split(';'); 
	for(var i=0;i < cookieArray.length;i++) { 
		var pair = cookieArray[i]; 
		while (pair.charAt(0)==' ') { 
			pair = pair.substring(1, pair.length); 
		} 
		if (pair.indexOf(needle) == 0) { 
			return pair.substring(needle.length, pair.length); 
		} 
	} 
	return null; 
}

//Controls main navigation behavior.
//Prototype and scripty 2 related.
function navMainFlyout() {
	var navMain = $('navMain');
	var navigationBlind = $('shutter');
	
	if(!navMain) return false;
	//create out button
	var navControl = new Element('p').writeAttribute('id', 'navControl').update('View More');
	//insert out button, following the nav.
	navMain.insert({
		after: navControl
	});
	
	var navigationItems = navMain.select('li'); //grab all the main nav li's
	var navWidth = 0; //create the initial overall width variable
	
	//fill out the initial width, by grabbing the width of all li's and adding them together.
	navigationItems.each(function(navigationItem){
		var	itemWidth = navigationItem.getWidth();
		navWidth = itemWidth + navWidth;
	});
	
	//get the width of the first item and the blind to it.
	var homeButtonWidth = navigationItems[0].getWidth();
	
	navigationBlind.setStyle({
		width: '' + homeButtonWidth + 'px'
	});
	
	navControl.observe('click', function(event){
		if(navControl.hasClassName('open')){			
			new Effect.Tween(navigationBlind, navWidth, homeButtonWidth, {
				duration: .75,
				transition: Effect.Transitions.sinoidal,
				afterFinish: function(){
					navControl.removeClassName('open');
				}}, 
				function(value){
					this.setStyle({
						width: ''+ value +'px' 					
					});
			});
		} else {
		/*	navigationBlind.morph('width:' + navWidth + 'px', {
				transition: 'easeOutBack', 
				duration: .5,
				after: function(){
					navControl.addClassName('open');
				}
			});*/
			//console.log('clicked to open');
			
			new Effect.Tween(navigationBlind, homeButtonWidth, navWidth, {
				duration: .75, 
				transition: Effect.Transitions.sinoidal,
				afterFinish: function(){
					navControl.addClassName('open');
				}}, 
				function(value){
					this.setStyle({
						width: ''+ value +'px' 					
					});
			});
		}
	});
	
	navControl.observe('mouseenter', function(event){
		navControl.addClassName('hover');
	});
	
	navControl.observe('mouseleave', function(event){
		navControl.removeClassName('hover');
	});
}

function secondaryNavHelper() {
	var secondaryNavs = $$('div.navSecondary>ul');
	
	//console.log(secondaryNavs);
	if(!secondaryNavs[0]) return false;
	
	secondaryNavs.each(function(secondaryNav){
		var subNavigation = secondaryNav.select('ul');
		if(!subNavigation[0]){
			return false;
		} else {
			secondaryNav.addClassName('collapsible');
		}	
		
		
	});
	
}

//Footer Padding Corrector
//Adjust the footer padding for the browser size
function positionElements() {
	var docFooter = $('footer');
	
	var viewportDimensions = document.viewport.getDimensions();
	
	docFooter.setStyle({
		padding: '26px ' + (viewportDimensions.width - 932)/2 + 'px',
		width: viewportDimensions.width - (viewportDimensions.width - 932) + 'px'
	});

}

//Carousel functionality. Goes through unordered list and makes controls and layout for slide show.
function carousel() {
	var highlights = $$('ul#featureCarousel li');
	var carouselContainer = $('breakout');
	var carouselList = $('featureCarousel');
	var speed = 20 //Carousel Pause Speed
	//console.log('highlights:', highlights);
	//console.log('carouselContainer:', carouselContainer);
	
	if(!highlights[0]) return false; //lets be sure they exist, before we break anything
	
	var queue = Effect.Queues.get('highlightQueue'); // a queu's necessary for the overall animation.
	
	var navItems = []; //Setting up our variable for the array of navigation items that make up the menu.
	
	highlights.each(function(highlight, index) { //Lets strip out any accidental, empty LIs.
		if(highlight.empty()){
			highlight.remove();
		}
	});
	
	var highlights = $$('ul#featureCarousel li');  //we've removed empties, now lets redefine the variable
		
	var currentHighlight = 0; //Right out of the gate, slide number 1 is the first. We'll keep track with this variable.
	var previousHighlight = highlights.size(); //The previous highlight at the start is the maximum number of slides, ie. 5 slides, slide #5 is the one before #1.
	
	//Check to see if is more than one - if not don't put the controls - it's unnecessary 
	highlightNavigation = new Element('ul').writeAttribute('id','carouselControls');//Making the controls for the carousel.
	
	carouselList.insert({
		after: highlightNavigation
	});
	
	highlights.each(function(highlight, index) {//For every carousel item, create a navigation control item.
		if(index > 0){
				highlight.setOpacity(0);
			} else {
				highlight.setOpacity(1);
				highlight.setStyle({
					zIndex: 1000
				});
			
			}
		
		navItems[index] = new Element('li').update((index + 1));
		
		if(index == 0){
			navItems[index].addClassName('currentPanel'); //Lets assign a class to keep track of the selected navItem
		}
		
		navItems[index].observe('click', function(event){
			if(queue.effects.size() == 0) {	
				var panelMove = currentHighlight - index;
				navItems[index].addClassName('currentPanel');
				carousel.stop();
				navItems[index].fire('parker:carouselUpdate', { indexMove: panelMove });	
				event.stop();
			}
		});
						
			highlightNavigation.insert({
				bottom: navItems[index]
			});		
	});
		
	nextHighlightButton = new Element('li');
	nextHighlightButton.addClassName('next');
	nextHighlightAnchor = new Element('a', { href: "#"}).update("Next");
		
	nextHighlightButton.insert({ bottom: nextHighlightAnchor });
	highlightNavigation.insert({ bottom: nextHighlightButton });
	
	
	previousHighlightButton = new Element('li');
	previousHighlightButton.addClassName('previous');
	previousHighlightAnchor = new Element('a', { href: "#"}).update("Previous");
	
	previousHighlightButton.insert({ bottom: previousHighlightAnchor });
	highlightNavigation.insert({ top: previousHighlightButton });
	
	previousHighlightButton.observe('click', function(event){
		document.fire('parker:carouselUpdate', { indexMove: 1 });	
	});
	
	nextHighlightButton.observe('click', function(event){
		document.fire('parker:carouselUpdate', { indexMove: -1 });		
	});
	
		
	if(highlights.size() < 1) {
		highlightNavigation.hide();
	}
	
	document.observe('parker:carouselUpdate', function(event){
		if(queue.effects.size() == 0) {
			var highlightMove = event.memo.indexMove;
			
			previousHighlight = currentHighlight;

			currentHighlight = (highlights.size() + ((currentHighlight - highlightMove) % highlights.size())) % highlights.size();
			
			var internalPreviousHighlight = previousHighlight;
			var internalCurrentHighlight = currentHighlight;
			
			navItems[internalPreviousHighlight].removeClassName('currentPanel');
			navItems[internalCurrentHighlight].addClassName('currentPanel');
			
			new Effect.Parallel([
				new Effect.Tween(highlights[previousHighlight], 1, 0, {sync: true}, function(value){
					this.setOpacity(value);
					this.setStyle({
						zIndex: 0 					
					});
				}),
				new Effect.Tween(highlights[currentHighlight], 0, 1, {sync: true}, function(value){
					this.setOpacity(value);
					this.setStyle({
						zIndex: 1000 					
					});
				})], {
				queue: {
					position: 'end',
					scope: 'highlightQueue',
					limit: 1
				}
			});	
		}
	});	
	
	carousel = new PeriodicalExecuter(function(){
		carouselContainer.fire('parker:carouselUpdate', { indexMove: -1 });
	}, speed);	
	
}
//Height Balance
//Normalizes the height of the elements passed in,
//based on the number of columns desired
function heightBalance(balanceElements, columns){
	balanceElements.eachSlice(columns, function(balanceElementSlice){
		var sliceHeight = balanceElementSlice.max(function(sliceElement){
			return sliceElement.getHeight();
		});
		
		balanceElementSlice.each(function(balanceElement){
			balanceElement.setStyle({
				height: sliceHeight + 'px'
			});
		});
	});
}

function adjustHeights () {
	var items = $$('div.productListing ul li');
	heightBalance(items, 5);
	
	var navDivisionItems = $$('div#navDivision ul li');
	var numberofDivItems = navDivisionItems.length;
	
	heightBalance(navDivisionItems, numberofDivItems);
}

function navigationHovers () {
	var navItems = $$('div#navDivision ul li');
	
	navItems.each(function(navItem){
		navItem.observe('mouseenter', function(event){
			navItem.addClassName('hover');
		});
		navItem.observe('mouseleave', function(event){
			navItem.removeClassName('hover');
		});
	});
	
}

//Cart View
function viewCart() {
	if(!$('cartExpanded')){
		return false;
	}
	
	var cartExpanded = $('cartExpanded');
	var cartButtons = $$('h2.cartButton');
	var closeButtons = $$('p.closeButton');
	var viewCart = $('viewCart');
	
	viewCart.hide('viewCart');
	
	cartButtons.each(function(cartButton, buttonIndex){
		cartButton.observe('click', function(event){
			cartExpanded.toggleClassName('open');

		});
	});	
	
	closeButtons.each(function(closeButton, closebuttonIndex){
		closeButton.observe('click', function(event){
			cartExpanded.toggleClassName('open');

		});
	});	
}

//Result List Expansion 
//This function allows for a hide and close on results under the ul tree 'resultExpander'
function resultExpander() {
	
	if(!$$('ul.resultExpander')){
		return false;
	}
	
	var resultExpanders = $$('ul.resultExpander > li');
	
	
	resultExpanders.each(function(resultExpander, expanderIndex){ 
		var resultParent = resultExpander.down('ul');
		
		resultExpanders[expanderIndex].observe('click', function(event){
			resultParent.toggleClassName('open');
			resultExpanders[expanderIndex].toggleClassName('open');
		});
		
	});
}

function supportAltNavigation() {
	var supportLists = $$('ul.support');
	
	if(!supportLists[0]){ return false};
	
	supportLists.each(function(supportList){
		var supportCategories = supportList.select('>li>h2>a');
		//console.log(supportCategories);
		
		supportCategories.each(function(category, index){
			var categoryChildren = category.up('h2').up('li').select('li>a');				
			//console.log(categoryChildren);
			category.observe('click', function(event){
				//console.log('true');
				category.up('h2').up('li').toggleClassName('open');
				event.stop();
			});
			
			categoryChildren.each(function(categoryChild){
				categoryChild.observe('click', function(event){
					//console.log('clicked child');
					if(categoryChild.up('li').down('ul')){
						categoryChild.up('li').toggleClassName('open');
						event.stop();
					}
					
				});
			});
			
		});
	});
	
}

function searchHandler() {
	var headerSearchFieldsets = $$('#header fieldset');
	//console.log(headerSearchFieldsets);
	
	if(!headerSearchFieldsets[1]){
		return false;
	}
	
	currentItem = 0;
	
	previousItem = 1;
	
	headerSearchFieldsets[previousItem].addClassName('closed');
	
	var fieldsetSwitchControls = $$('#header p.fieldSetSwitch');
	
	fieldsetSwitchControls.each(function(switchControl){
		switchControl.observe('click', function(event){
			headerSearchFieldsets.each(function(headerFieldset){
				headerFieldset.toggleClassName('closed');
			});
			if(headerSearchFieldsets[0].hasClassName('closed')){
				switchControl.down('a').update('Click to Search Division');
			} else {
				switchControl.down('a').update('Click to Search Parker.com by Keyword or Part #');

			}
			event.stop();
		});
	});
}



//Replacement for Window Onload - Loads before images, cross-browser
document.observe("dom:loaded", function() {
    //liFirstLast(); // Adds classes 'first' and 'last' to respective LIs
	/*positionElements();
	navigationHovers();
    Event.observe(document.onresize ? document : window, "resize", function() {
        positionElements();
    });
	countryTool();
	prodSelector();
	if($('productSelector')){
		Event.observe(document.onresize ? document : window, "resize", function() {
		resizeBlackout();
	});
	}
	
	inputClear(); //Clears input on focus
	navMainFlyout(); //Handles the hidden slide-out main navigation.
	carousel();
	adjustHeights();
	secondaryNavHelper();
	viewCart();
	resultExpander();
	supportAltNavigation();
	searchHandler();*/

	adjustHeights();
	//resultExpander();
	detailToggle();
	productOrdering();
	productTabHovers();
	configuratorLightbox();
});

// usage: log('inside coolFunc',this,arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
	log.history = log.history || [];   // store logs to an array for reference
	log.history.push(arguments);
	if(this.console){
		console.log( Array.prototype.slice.call(arguments) );
	}
};

// catch all document.write() calls
//(function(doc){
	//var write = doc.write;
	//doc.write = function(q){
		//log('document.write(): ',arguments);
		//if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments); 
	//};
//})(document);

function detailToggle() {
	var descriptions = $$('div.description');
	
	if(!descriptions[0]) return false;
	
	descriptions.each(function(description){
		var summary = description.down('div.summary');
		var detail = description.down('div.detail');
		var readMore = description.down('p.readMore');
		var readLess = description.down('p.readLess');
		
		readMore.observe('mouseenter', function(event){
			readMore.addClassName('hover');
		});
		
		readMore.observe('mouseleave', function(event){
			readMore.removeClassName('hover');
		});
		
		readLess.observe('mouseenter', function(event){
			readLess.addClassName('hover');
		});
		
		readLess.observe('mouseleave', function(event){
			readLess.removeClassName('hover');
		});
		
		readMore.observe('click', function(event){
					summary.hide();
					detail.show();
		});
		
		readLess.observe('click', function(event){
					summary.show();
					detail.hide();
		});
	});
		
}

//Fourth Product Balancer
//Adds a class of 'fourth' to the last item of a 
//row of products on the products section of the site.
//Note: Prototype Driven
function productOrdering() {
	var firstProducts =	$$('ul.productList > li:first-child');
	var fifthProducts =	$$('ul.productList > li:nth-child(4n+1)');
	var fourthProducts =	$$('ul.productList > li:nth-child(4n)');
	var products =	$$('ul.productList > li');
	
	firstProducts.each(function(firstProduct) {
		firstProduct.addClassName('first');
	});
	fourthProducts.each(function(fourthProduct) {
		fourthProduct.addClassName('fourth');
	});
	fifthProducts.each(function(fifthProduct) {
		fifthProduct.addClassName('fifth');
	});
	
	products.each(function(product){
		product.observe('mouseenter', function(event){
			product.addClassName('hover');
		});
		product.observe('mouseleave', function(event){
			product.removeClassName('hover');
		});
	
	});
}

function productTabHovers() {
	var productTabs = $$('#landingNavigation ul li');
	
	productTabs.each(function(productTab){
	
		productTab.observe('mouseenter', function(event){
			productTab.addClassName('hover');
		});
		
		productTab.observe('mouseleave', function(event){
			productTab.removeClassName('hover');
		});
	});
}

function configuratorLightbox(){
	var configuratorLinks = $$('a.configLightbox');
	var pageTop = $('subpage');
	var pageHeight = pageTop.getHeight();
	
	window.log(configuratorLinks);
	
	configuratorLinks.each(function(configLink){
		var linkSource = configLink.readAttribute('href');
		
		var closeButton = new Element('p').addClassName('close').update('close window');
		var caption = new Element('h2').addClassName('close').update('Configurator');
		var overlay = new Element('div').writeAttribute('id', 'configOverlay');
		overlay.setStyle({
			height: pageHeight + 24 + 'px'
		});
		var lightbox = new Element('div').writeAttribute('id', 'configLightbox');
		lightbox.setStyle({
			height: (window.innerHeight - 140) + 'px'
		});
		
		var newIframe = new Element('iframe').writeAttribute('src', linkSource);
		
		configLink.observe('click', function(event){
			
			linkSource = configLink.writeAttribute('href', '#');
			
			pageTop.insert({
				top: overlay
			});
			
			overlay.insert({
				top: lightbox
			});
			
			lightbox.insert({
				top: newIframe
			});
			
			lightbox.insert({
				top: closeButton
			});
			
			lightbox.insert({
				top: caption
			});
			
			event.stop();
			return false;
		
		});
		
		overlay.observe('click', function(event){
			overlay.remove();
		});
		
		closeButton.observe('click', function(event){
			overlay.remove();
		});
		
		lightbox.observe('click', function(event){
			event.stop();
		});
	});
}
