// JavaScript Document

Cufon.replace("h2", { fontFamily: 'rockwell' });
Cufon.replace("h1", { fontFamily: 'rockwell' });
Cufon.replace("h3", { fontFamily: 'rockwell' });
Cufon.replace("#top_nav_inner a", { fontFamily: 'rockwell' });
Cufon.replace("#footer_colA a", { fontFamily: 'rockwell' });
Cufon.replace(".rockwell", { fontFamily: 'rockwell' });
Cufon.replace(".sitemap #sitemap_listing li a", { fontFamily: 'rockwell' });

var navOriginalHeight = 0;
var topNav = null;
var navButton = null;
var sideContent = null;
var sideOriginalWidth = 0;
var ieVersion = 0;
var performingMorph = false;

window.addEvent("domready",function(){

	// prevent ugly border on click of -ive text indent links
	$$("a").each(function(elm){
		elm.addEvent("click",function(linkElm){
			this.blur();
		});
	});
	
	//browser check
	var appVersion = navigator.appVersion;
	var MSIEindex =  appVersion.indexOf('MSIE');
	if(MSIEindex != -1){
		ieVersion = (appVersion.charAt(MSIEindex + 5));
	}
	
	//set the side contents starting properties
	setSideContent();
	
	//set the menu starting properties
	setMenu();

});


function setMenu(){
	//Store var for navigation
	topNav = $("top_nav_inner");
	
	if(Browser.Engine.trident){
		topNav.setStyle('opacity',0.8);
		topNav.setStyle('backgroundColor', '000');
	}
	
	navButton = $("nav_button");
	navOriginalHeight = topNav.getStyle('height').toInt();
	
	menuAnimations();
	
	var morphObject = navButton.retrieve('morph');
	
	var menuTimer = function(){
		performMenuAnimation('expand', morphObject,0);		
	};
	menuTimer.delay(2500);	
	
	var contentTimer = function(){
		performContentAnimation('expand');
	};
	contentTimer.delay(1000);
}

// Animates the slide of the main naviagtion 
function menuAnimations(){
	topNav.setStyle('height',0);
	
	var morphElement = topNav;
	var morphObject = new Fx.Morph(morphElement, {
		duration: 800,
		transition: 'expo:out'
	});
	
	navButton.store('morph',morphObject);
		
	navButton.addEvent('click', function(event){
		event.stop();						
		var morphObject = navButton.retrieve('morph');		
		var currentMenuHeight = topNav.getStyle('height').toInt();
				
		//If menu is expanded
		if(currentMenuHeight == navOriginalHeight){
			performMenuAnimation('shrink', morphObject, currentMenuHeight);
		}else{
			performMenuAnimation('expand', morphObject, currentMenuHeight);
		}
	});
}

function performMenuAnimation(animationType, morphObject, currentMenuHeight){	
	
	morphObject.set({
		'height': currentMenuHeight
	});

	if(animationType == 'expand'){
		morphObject.start({
			'height': navOriginalHeight,
			onComplete : function(){				
			}
		});
	}else if(animationType == 'shrink'){
		morphObject.start({
			'height': 0
		});
	}	
}

function setSideContent(){
	sideContent = $("right_column_standard");
	if(sideContent == null){
		sideContent = $("right_column_gallery");
	}

	if(sideContent != null){
		var button = $("close_tag");
		
		if(ieVersion >= 8 || ieVersion == 0){
			Cufon.replace("#right_title", { fontFamily: 'rockwell' });
		}else{
			var standard_title = $("standard_title");
			if(standard_title != null){
				standard_title.setStyle("opacity","0.0");
			}
		}
		
		//set up morph object
		var morphObject = new Fx.Morph(sideContent, {
			duration: 1500,
			transition: "expo:out",
			onStart: function(){
				performingMorph = true;
			},
			onComplete: function(){			
				performingMorph = false; 
				if(ieVersion < 8 && ieVersion > 0){
					if(standard_title != null && $("right_title") != null){
						Cufon.replace("#right_title", { fontFamily: "rockwell" });
						$("standard_title").fade("in");
					}
				}
				
				if(button != null){
					button.addEvent("click", function(event){
						event.stop();						
						if(sideContent.getStyle("width").toInt() == 0){
							performContentAnimation("expand");
						}else{
							performContentAnimation("shrink");
						}
					});
				}
			}
		});
		sideContent.store("morph", morphObject);
		
		//hide title so cufon can be added before its shown		
		sideOriginalWidth = sideContent.getStyle("width").toInt();
		sideContent.setStyle("width", 0);
		
		//if ie set opacity as background transparent png doesn't display correctly - SIGH
		if(Browser.Engine.trident){
			sideContent.setStyle("backgroundColor", "000");
			sideContent.setStyle("opacity","0.8");
		}
	}
}

function performContentAnimation(animationType){
	var button = $("close_tag");

	if(sideContent != null && performingMorph == false){
		var morphObject = sideContent.retrieve("morph");	
		if(animationType == "expand"){
			if(button != null){
				button.innerHTML = 'HIDE';			
				button.setStyle('background-image','url(/../style/images/hide.png)');
				button.setStyle('marginLeft','0px');
				morphObject.set({
					"width": 0
				});
			
				morphObject.start({
					"width": sideOriginalWidth
				});
			}else{
				sideContent.setStyle("borderLeft","1px solid #4c4c4c");
				if(ieVersion < 8 && ieVersion > 0){
					morphObject.start({
						"width": 280,
						"marginLeft": -282
					});
				}else{
					morphObject.start({
						"width": 280,
						"marginLeft": 697
					});
				}
				morphObject.start('div.display');
			}
		}else if(animationType == "shrink"){
			if(button != null){
				button.innerHTML = 'SHOW';				
				button.setStyle('background-image','url(/../style/images/show.png)');
				button.setStyle('marginLeft','-7px');
			}
		
			morphObject.set({
				"width": sideOriginalWidth
			});
		
			morphObject.start({
				"width": 0
			});
		}
	}
}

