var iebody = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;

jQuery(function( $ ){
	
	if(typeof($.scrollTo) != "undefined"){
		$.scrollTo.defaults.axis = 'xy';
		$.scrollTo( 0 );//reset the screen to (0,0)
	}
	
	$('#thenav a').click(function(){
		
		if(this.id == "download"){
			theid = '7';
		}else{	
			theid = this.hash.replace("#section", "");
		}
		
		mark_visited(theid);
		
		if(this.id == "download"){
			return true;
		
		}else{
			
			$.scrollTo( this.hash, 500, { easing:'easeOutQuad' });
		
			// Was the movie playing? pause it if it was
			pauseInactive(theid);
			
		}
	});
	
	
	if(typeof($("#thenav_maxs")) != "undefined"){
		$("#thenav_maxs").bind("mouseenter",function(){
		
			if($("#thenav_maxs").height() == 26){
				$("#thenav_maxs h2").attr({style: "background-position:bottom left"});
				$("#thenav_maxs").animate({height:"92px"}, { "duration": 120, "easing": "easeOutQuad" } );
			}
			
		}).bind("mouseleave",function(){
				$("#thenav_maxs h2").attr({style: "background-position:top left"});
				$("#thenav_maxs").animate({height:"26px"}, { "duration": 120, "easing": "easeOutQuad" } );
		});
	}
	
	if(typeof($("#thenav")) != "undefined"){
		$("#thenav").bind("mouseenter",function(){
			if($("#thenav").height() == 26){
				$("#thenav").animate({height:"144px"}, { "duration": 150, "easing": "easeOutQuad" } );
				$("#thenav h2").attr({ style: "background-position:bottom left" });
			}
		}).bind("mouseleave",function(){
			
				$("#thenav").animate({height:"26px"}, { "duration": 150, "easing": "easeOutQuad" } );
				$("#thenav h2").attr({ style: "background-position:top left" });
			
		});
		}
	
	if(typeof($("#thenav_site")) != "undefined"){
		$("#thenav_site").bind("mouseenter",function(){
		
			if($("#thenav_site").height() == 26){
				$("#thenav_site").animate({height:"125px"}, { "duration": 120, "easing": "easeOutQuad" } );
				$("#thenav_site h2").attr({ style: "background-position:bottom left" });
			}
			
		}).bind("mouseleave",function(){
		
				$("#thenav_site").animate({height:"26px"}, { "duration": 120, "easing": "easeOutQuad" } );
				$("#thenav_site h2").attr({ style: "background-position:top left" });
			
		});
	}
	
	$('.next').click(function(){
		
		theid = this.hash.replace("#section", "");
		
		pauseInactive(theid);
		mark_visited(theid);
		
		$.scrollTo( this.hash, 500, { easing:'easeOutQuad' });
		return false;
	});
	

	function show_caption1(){
		$('#caption1').html(this.alt);
	}
	function show_caption2(){
		$('#caption2').html(this.alt);
	}
	function show_caption3(){
		$('#caption3').html(this.alt);
	}
	function show_caption4(){
		$('#caption4').html(this.alt);
	}	
	function show_caption5(){
			$('#caption5').html(this.alt);
	}
	function show_caption6(){
			$('#caption6').html(this.alt);
	}


		
function set_slideshows(){

		$('#s1').cycle({ 
			fx:'scrollSmart',
			timeout: 0, 
			next:'#next1', 
			prev:'#prev1',
			ease: 'easeInOutQuad',
			speedIn: '300',
			speedOut: '700',
			after: show_caption1
		});
		
		
		$('#s2').cycle({ 
			fx:'scrollSmart',
			timeout: 0, 
			next:'#next2', 
			prev:'#prev2',
			ease: 'easeInOutQuad',
			speedIn: '300',
			speedOut: '700',
			after: show_caption2
		});
		
		
		$('#s3').cycle({ 
			fx:'scrollSmart',
			timeout: 0, 
			next:'#next3', 
			prev:'#prev3',
			ease: 'easeInOutQuad',
			speedIn: '300',
			speedOut: '700',
			after: show_caption3
		});
		
		
		$('#s4').cycle({ 
			fx:'scrollSmart',
			timeout: 0, 
			next:'#next4', 
			prev:'#prev4',
			ease: 'easeInOutQuad',
			speedIn: '300',
			speedOut: '700',
			after: show_caption4
		});
		
		
		$('#s5').cycle({ 
			fx:'scrollSmart',
			timeout: 0, 
			next:'#next5', 
			prev:'#prev5',
			ease: 'easeInOutQuad',
			speedIn: '300',
			speedOut: '700',
			after: show_caption5
		});
		
		$('#s6').cycle({ 
			fx:'scrollSmart',
			timeout: 0, 
			next:'#next6', 
			prev:'#prev6',
			ease: 'easeInOutQuad',
			speedIn: '300',
			speedOut: '700',
			after: show_caption6
		});
	}
	
	if(typeof(slideshow) != "undefined"){
	
		if(slideshow.length > 0){
			set_slideshows();
		}
	}
	
	function mark_visited(id){
		
		$('#thenav .i' + id + ' a').addClass("visited");
	}
	
 /****************
	*
	* Begin map
	*
	***************/
	
	// Settings and Definitions
	var largediv = "div.shim";
	var mapdiv = "#map";
	var mapcontainer = "#map";
	var markerdiv = "#mapPos";

	var updateMarker = true;
	
	$(document).ready(function(){
		
		if(typeof($.scrollTo) == "undefined" || $("#nav_map").length == 0){
			return false;
		}else{
			$.scrollTo.defaults.axis = 'xy';
		}
		
		$("#nav_map").css({"display" : "block"});
		
		$("#nav_map h2").bind("mouseenter",function(){
			
			if($('#nav_map').height() == 26){
				show_map();
			}
			
		});
		
		$("#nav_map h2").bind("mousedown",function(){
			hide_map();
		});

		
	
		
		// "Static" variables
		/*var mapW = $(mapdiv).width();
		var mapH = $(mapdiv).height();
		var totalx = $(largediv).width();
		var totaly = $(largediv).height();
		
		TODO: doesn't work in Safari??
		
		*/
		var mapW = 178;
		var mapH = 253;
		var totalx = 2386;
		var totaly = 3398;
		var xratio = mapW / totalx;
		var yratio = mapH / totaly;
		
		// Dynamic vars
		
		// Window Viewport dimensions
		var winW = $(window).width();
		var winH = $(window).height();
		
		// Current position
		var thisy;
		var thisx;
		
		var allowdrag = true;
		
		/************
		 * More Mac Safari fun: Drag constrains vertically
		 * Remove draggable altogether
		 * http://borgar.undraland.com/programs/sniffer/sniffer.js
		 ************/
		
		$(markerdiv).draggable({containment: $(mapcontainer), stop: getcoords});
		
		/* On scroll */
		$(window).scroll(function(){
			moveMarker();
		});
		
		$(window).resize(function(){
  		winW = $(window).width();
			winH = $(window).height();
			
			resizeMarker();
			
		});
	
		/* On stop drag */
		function getcoords(){
			
			clearInterval(mapTimer);
		
			mapX =  Number($(markerdiv).css("left").replace("px", ""));
			mapY =  Number($(markerdiv).css("top").replace("px", ""));
		
			var newx = mapX/xratio;
			var newy = mapY/xratio;
			
			updateMarker = false;
			$(markerdiv).draggable("disable");
			
			$('html, body').scrollTo( { top: Number(newy), left: Number(newx)} , 500, { easing:'easeOutQuad', onAfter: function(){ setUpdate(); } });
			
		}
		
		/* called after scrolling is finished - make marker draggable again */
		function setUpdate(){
			updateMarker = true;
			$(markerdiv).draggable("enable");
		}
		
		/* Resize marker to be the correct proportions */
		function resizeMarker(){
			
			$(markerdiv).css("width", ((winW * xratio) - Number($(markerdiv).css("border-left-width").replace("px", "")) - Number($(markerdiv).css("border-right-width").replace("px", ""))) + "px");
			$(markerdiv).css("height", ((winH * yratio) - Number($(markerdiv).css("border-top-width").replace("px", "")) - Number($(markerdiv).css("border-bottom-width").replace("px", ""))) + "px");
		}
		
		/* Move marker */
		function moveMarker(){
		
			if(updateMarker == false){
				return false;
			}
			
			thisy = document.all? iebody.scrollTop : pageYOffset;
			thisx = document.all? iebody.scrollLeft : pageXOffset;
			
			var newx = thisx * xratio;
			var newy = thisy * yratio;
			
			$(markerdiv).css("left", newx + "px");
			$(markerdiv).css("top", newy + "px");
		}
		
		resizeMarker();
		moveMarker();
		
		show_map();
		
		mapTimer = setTimeout("mapTimeout();", 5000);
	
	});
	
});

var mapTimer;
function mapTimeout(){
		clearInterval(mapTimer);
		hide_map();
}

function show_map(){
	$('#nav_map h2').attr({style: "background-position:bottom left"});
	$('#nav_map').css({ "padding-bottom": "14px" });
	$('#nav_map').animate({height:"279px", width: "178px"}, { "duration": 120, "easing": "easeOutQuad" } );
}

function hide_map(){
		$('#nav_map h2').attr({style: "background-position:top left"});
		$('#nav_map').css({ "padding-bottom": "0px" });
		$('#nav_map').animate({height:"26px", width: "55px"}, { "duration": 120, "easing": "easeOutQuad" } );
}


/*****************
 *
 * For interfacing with Flash
 *
 ******************/

// pause all the flash movies that are inactive
function pauseInactive(theid){
	theid = parseInt(theid);
	
	for(var i = 0; i < media.length; i++){
		if(theid != media[i]){
			var themedia = document.getElementById('media_player' + media[i]);
			themedia.pausePlayer();
		}
	}
}




/*************
 * preload nav images...
 *
 *************/
function preload_nav_images(poster){
	var preload = Array("images/bg.jpg",
	"images/map.jpg",
	"images/nav_1.png",
	"images/nav_2.png",
	"images/nav_3.png",
	"images/nav_4.png",
	"images/nav_5.png",
	"images/nav_6.png",
	"images/nav_7.png",
	"images/nav_1-2.png",
	"images/nav_2-2.png",
	"images/nav_3-2.png",
	"images/nav_4-2.png",
	"images/nav_5-2.png",
	"images/nav_6-2.png",
	"images/nav_7-2.png",
	"images/nav_this_bg.png",
	"images/nav_this_poster.gif",
	"../images/nav_get_media_kit.png",
	"../images/nav_maxs_on_myspace.png",
	"../images/nav_contact_us.png",
	"../images/nav_other_bg.png",
	"../images/nav_view_all.png",
	"../images/nav_maxs_kansas_city.png",
	"../images/nav_aerosmith.png",
	"../images/nav_andy_warhol.png",
	"../images/nav_david_bowie.png",
	"../images/nav_punk.png",
	"../images/nav_chickpea_conspiracy.png",
	"../images/nav_other_posters.gif",
	"../images/nav_maxs_bg.png"
	);
	var preloaded = Array();
	
	for(var i = 0; i < preload.length; i++){
		preloaded[i] = document.createElement('img');
		preloaded[i].setAttribute('src', preload[i]);
	}
}


function mailme(userid,domain)
{
		var email = domain +'@' + userid;
		var TempBefore = '';
		var TempAfter = '';
		var NewString = '';
		var Start = 0;
		
		var WhereHash = email.indexOf ('@');
		for (Count = 1; Count <= WhereHash; Count ++)
		{
			TempBefore += email.substring (Start, Count);
			Start++
		}
		Start = WhereHash;
		Start ++
		WhereHash +=2;
		for (Count = WhereHash; Count <=email.length; Count++)
		{
			TempAfter +=email.substring (Start, Count)
			Start++
		}
		NewString = TempAfter +'@' + TempBefore;
		parent.location = 'mailto:'+NewString+'?subject=From maxskansascity.com';
}