window.addEvent('domready', function() 
{
	 var myFx = new Fx.Style('rightColumn', 'opacity',{duration: 1000}).start(0,1);

	$("rightColumn").setStyle("background-image", "url(/images/mainEmma.jpg)");

	var myChain = new Chain();
	$$('#rotator img').each(function(element) 
	{
		myChain.chain( function()
		{ 
			var navFx = new Fx.Style('rotator', 'opacity').set(0);
			$('rotator').setStyle('background-image', 'url(' + element.getProperty('src') + ')');
			
			var myFx = new Fx.Style(
				'rotator', 
				'opacity', 
				{
					duration:1000,
					onComplete: function() 
					{
						$('rightColumn').setStyle('background-image', 'url(' + element.getProperty('src') + ')');	
					}
				}).start(0, 1);
			})
		});
	(function(){myChain.callChain();}).periodical(3000);

	var content = $('containerIndexes');
	var contentslide = new Element('div',{
		'id': 'contentslide'
	});

	var currentpageel = content.clone().setProperties({
		'id': 'contentX',
		'class': 'pseudopage'
	});

	contentslide.injectInside(content.empty());

	//var navlinks = $$('#leftColumn a');
	
	//navlinks.extend( $$('.bagNav a') );
	var sPath = window.location.pathname;
	var currentpage = sPath.substring(sPath.lastIndexOf('/') + 1);
	var currentel;

	if(window.webkit || window.webkit419 || window.webkit420?'mac':''){
		$('rightColumn').setProperty('class','mac');
	}

	var navlinks = $$('.mainNav');
	navlinks.each(function (el,index)
	{
		//dynamically bring in pages.
		// get href property of link
		var page = el.getProperty('href');
		
		if(page != currentpage)
		{
			var tempholder = new Element('div',{
				'class': 'pseudopage',
				'id': 'content'+index
			});
			//alert ('loading pages/' +page+'?1');
			new Ajax(page+'?1', {
				method: 'get',
				evalScripts:'true',
				update: tempholder
			}).request();
			tempholder.injectInside(contentslide);
		}
		else
		{
			currentel = index;
			currentpageel.setProperty('id','content'+index);
			currentpageel.injectInside(contentslide);
		}

	
		el.addEvent('click', function(){
			if (index==5){
				thisIndex=3;
			} else {
				thisIndex=index;
			}
			el.setProperty('href','#'+(true?thisIndex:page));

			$('nav').setProperty('class','active'+thisIndex);
			/*
			if (thisIndex==0){
				$('bagNavSmall').setProperty('class','bagNavActive');
			}
			if (thisIndex==1){
				$('bagNavMedium').setProperty('class','bagNavActive');
			}
			if (thisIndex==2){
				$('bagNavLarge').setProperty('class','bagNavActive');
			}
			*/
			//el.setProperty('class','selected');
			//updatePage('content','pages/emma_bag.php');
			slideto(thisIndex);

		});

	});
	

		
	$$('.sectionNav').each(function (el,index)
	{
		var page = el.getProperty('href');
		
		el.addEvent('click', function(){
			
			el.setProperty('href','#'+(true?index:page));
			$('nav').setProperty('class','active'+index);
			//el.setProperty('class','selected');
			//updatePage('content','pages/emma_bag.php');
			slideto(index);
		});
	});
	
	
	
	var gotleft = currentpageel.getLeft()-content.getLeft()+(window.ie6?0:1);
	contentslide.setStyle('left',-gotleft);

	var sfx = new Fx.Style(contentslide, 'left', {
		duration: 1000,
		transition: Fx.Transitions.Quint.easeOut,
		onStart: function(){
			contentslide.effect('opacity').start(0,.65);
			$('rightColumn').setProperty('class','');
		},
		onComplete: function(){
			$('rightColumn').setProperty('class','active');
			contentslide.effect('opacity').start(.94);
		}
	});

	function slideto(index){
		if ($('containerProducts'))
		{
			$('containerIndexes').setStyle('display','block');
			$('containerProducts').setStyle('display','none');
		}
		
		if(index != currentel){
			var tox = $('content'+index).getLeft()-contentslide.getLeft();
			if(true){
				sfx.stop();
				sfx.start(-tox);
			}else{
				contentslide.setStyle('left',-tox);
			}
			currentel = index;
		}
		
		
		$$('.slide .link').each(function (el,index)
		{
			//alert (el.getProperty('href'));
			var linkinpage=el.getProperty('href');
			el.setProperty('href','#');
			if (linkinpage != "#"){
				el.addEvent('click', function()
				{
					updatePage('containerProducts',linkinpage);
				});
			}
		});
	}
});
	
function updatePage(div,url){
	
	new Ajax(url, {
	method: 'get',
	evalscript:'true',
	update: $(div),
	onRequest:  function(){
		$('indicator').setStyle('display','block');
	},
	onSuccess: function(){
		$('containerIndexes').setStyle('display','none');
		$('containerProducts').setStyle('display','block');
	},
	onComplete: function() {
		window.addEvent('domready', Lightbox.init.bind(Lightbox));
		
		window.addEvent('domready', function() 
		{
			$('indicator').setStyle('display','none');
			
			$('submitForm').setProperty('disabled','true');
			$('submitForm').setStyle('cursor','not-allowed');
			
			$$('.mini_swatch').each(function (el,index)
			{
				var thisDiv = el.getProperty('id');
				
				// add click action to swatch //
				$(thisDiv).addEvent('click', function(){
					$('fabricChoice').value=$(thisDiv).getText();
				
					// enable the form now that they selected //
					$('submitForm').disabled = false;
					$('submitForm').setStyle('cursor','default');
				});
				
				// add rollover image using tooltips :) //
				var thisDiv = el.getProperty('id');
				var thisClassName = 'swatch_over'+index;
				new Tips($(thisDiv), {
				className: thisClassName,
				fixed:true,
				offsets: {
				x: -129,
				y: -165
				
				}
				});
				if (thisDiv=="swatch_mosaic"){
					var background = 'swatch-med-mosaic.jpg'
				}
				if (thisDiv=="swatch_amber"){
					var background = 'swatch-med-amber.jpg'
				}
				if (thisDiv=="swatch_olives"){
					var background = 'swatch-med-marble.jpg'
				}
				if (thisDiv=="swatch_granite"){
					var background = 'swatch-med-granite.jpg'
				}
				if (thisDiv=="swatch_peridot"){
					var background = 'swatch-med-peridot.jpg'
				}
				if (thisDiv=="swatch_daisy"){
					var background = 'swatch-med-daisy.jpg'
				}
				if (thisDiv=="swatch_voodoo"){
					var background = 'swatch-med-voodoo.jpg'
				}
				if (thisDiv=="swatch_tamale"){
					var background = 'swatch-med-tamale.jpg'
				}
				if (thisDiv=="swatch_meadow"){
					var background = 'swatch-med-meadow.jpg'
				}
				$$('.'+thisClassName+'-tip').setStyle('background','url(/images/thumbnails/'+background+') no-repeat');
			});
		});
	}
	}).request();
	
	function submitForm(){ 
			var getCategories = 
			new ajax('mailer.php', 
			{
				postBody: $('mailform').toQueryString, 
				update: $('mailStatus') 
			}).request(); 
	}
}
