$(document).ready(function(){
	jQuery.easing.def = 'easeInQuad';

	function cleanImage(n){
		n = n.replace(/ /g,'');
		n = n.toLowerCase();
		return n;
	}

function imageReplace(n, p){ //n=image p=imageheight
		$img = cleanImage(n.text());
		$last = $img.lastIndexOf("/");
		h = p
		// setup rollover.  CSS must be set to overview hidden. 
		n.text("").html("<img src='/images/menu_"+ $img  +".gif' />");		
		if(n.hasClass('active')){
			n.css({display: "block", overflow: "hidden", height: ""+ h + "px"});
			n.children("img").css({marginTop:"0px"});
		}else{
			n.children("img").css({marginTop:-h+"px"});
			n.css({display: "block", overflow: "hidden", height: ""+ h + "px"});
			n.hover(function(){
				n.children("img").css({marginTop:"0px"});
				},function(){
				n.children("img").css({marginTop:"-"+ p + "px"});
			});
		}
	}

	// fire image replace on the menu items
	$(".mainmenu a").each(function(){
		imageReplace($(this), 36);
	});
	/** Transition Peice **/
	var $url = location.href;
	var $xpos = 0;
	var $page = 1;
	var $maxwidth = 900;
	var $lang = "en";
	/** functions **/
	function getPage(n){
		$last = n.lastIndexOf("/");
		n = n.substring(0, $last);
		n = n+"not";
		return n;
	}
	function moveForward(){
			$(".spacer").animate({
				marginLeft: -$windowwidth+"px"		
			}, 800, 'easeInBack', function(){$page=0});			

	}
	function moveBack(){
			$(".spacer").animate({
				marginLeft: "0px"		
			}, 800, 'easeInBack', function(){$page=1});	

	}
	
	function switchStyle(){
		//first detect french or english.
		$n = location.href;
		$side = "not";
		if($funtest > 1){
			$side = "";
		}		
		$n = $n.lastIndexOf("/fr/");
		
		
		
		if($n < 0){
			// its english
			$("#switch").html("<img src='/images/"+$side+"funside_en.gif' />");
			$lang = "en";
		}
		if($n > 1){
			$("#switch").html("<img src='/images/"+$side+"funside_fr.gif' />");
			$lang = "fr";
		}
		$("#switch").css({
			backgroundImage: "url('/images/"+$side+"funside_back.gif')"
		});
		
	};
	function collapseSwitch(){
		$img = null;
		$bg = null;
		if($page == 1){
			$img = "funside_" + $lang + ".gif";
			$bg = "funside_back.gif";
		}else{
			$img = "notfunside_" + $lang + ".gif";
			$bg = "notfunside_back.gif";
		}
		$("#switch").animate({
			width: "0px",
			marginLeft: "-20px"
		}, 10, function(){
			
			$(this).children("img").attr("src", "/images/" + $img);
			$(this).css({
				backgroundImage: "url('/images/"+$bg+"')"

			});
			if($page == 1){
				$(this).removeClass("right");
				$(this).addClass("left");
				$("#switch").css({
					marginLeft:"-16px",
					width: "50px"
				});
			}else{
				$(this).removeClass("left");
				$(this).addClass("right");
				$("#switch").css({
					marginLeft: "0px",
					width: "34px"
				});
			}
			
		});	
	}


	function dimension(n, p){
		n = parseInt(n);
		x = parseInt(p);
		if(x <= 590){
			x = 590;
		}

		$(".bg").css({width: n,
			overflow: "hidden",
			height: x
			});

			$pad = ((n - 700)/2);

		$(".container").css({
			width: n-$pad+"px",
			paddingLeft: $pad+"px", 
			height: p+35+"px"
		});
		$("#other").css({
			width: n-$pad+"px",
			paddingLeft: $pad+"px",
			height:p+35+"px",
			marginLeft: n+"px"
		});
		if($page == 0){
			$(".spacer").css({marginLeft: -n+"px"});
		}
		
	}
	
	/** init **/	
	//check to make sure that the 
	
	$url = $url;
	$url = getPage($url);
	$browserfail = null;
	$(".container, .containernot").css({position: "absolute"});
	$(".spacer").append("<div id='other'></div>");
	
	var $windowwidth = $(window).width();
	var $windowheight = $(window).height();
	
	dimension($windowwidth, $windowheight);
	//check on resize
	$(window).resize(function(){
		$windowwidth = $(window).width();
		$windowheight = $(window).height();
		dimension($windowwidth,$windowheight);		
	});
	
//	if ( $.browser.msie ){
//	  if( $.browser.version < 7){
//		$browserfail = true;
//		}
//	}
	// if the user is using ie6 lets gracefully degrade the page as to not show the transition script.  
	//
	if($browserfail == null){
	$("#other").load($url + " .containernot>*", mainInit);
	$("#other").css({marginLeft: $windowwidth+"px"});
	}
	var $funtest = location.href;
	var $funtest = $funtest.lastIndexOf("#not");
	
	if($funtest > 1){
		$page = 0; // set to page 0;
		$(".spacer").css({marginLeft: -$windowwidth+"px"});
		$("body").css({
			backgroundImage: "none",
			backgroundColor: "#e6e6e6"
		});

	}
	$(".bg").css({display:"block"});
	
	
	function mainInit(){
		$("body").append("<div id='switch'></div>");
		if($page == 0){
			$("#switch").addClass("left");
		}else{
			$("#switch").addClass("right");
		}
		//create button to fire animation//
		//style switch button
		$("#other .item2 a").text("Bingo");
		
		switchStyle();	
		$("#switch").click(function(){			
			if($page == 1){
			collapseSwitch();
			moveForward();
			}
			if($page == 0){
			collapseSwitch();	
			moveBack();
			}
		});
		$("#switch").hover(function(){
			if($page == 0){
				$(this).stop().animate({marginLeft: "0px"}, {queue: false,  duraction :300});
			}else{
				$(this).stop().animate({width: "50px"}, {queue: false,  duraction :300});
			}
		},function(){
			if($page == 0){
				$(this).stop().animate({marginLeft: "-16px"}, {queue: false,  duraction :300});
			}else{
				$(this).stop().animate({width: "34px"}, {queue: false,  duraction :300});
			}
		});
		
	

		// press other links to #not hash
		$("#other .mainmenu li a").each(function(){
			$linkex = $(this).attr('href');
			$linkex = $linkex + "#not";
			$(this).attr('href', $linkex);
		});
		$("#other .languageswitch a").each(function(){
			$thislang = $(this).children("img").attr("alt");
			if($thislang == "francais"){
				$(this).text("Français");
			}else{
				$(this).text("English");
			}
		});
		$("#other .languageswitch a").css({
			color: "#373737",
			padding: "3px",
			marginTop: "0px",
			borderTop: "none"
		});
	}
});
