$(document).ready(function(){

	// This take care of having a transparent bg at the top of the page, but solid white when you scoll down
	$(document).scroll(function(){ 
		 if($(window).scrollTop() > 500){
			 $("#wrapper").css("background-color","white");
		}else{
			$("#wrapper").css("background-color","transparent");
		}
	}); 

	// If need be this is here to record outbound links for Google Anayltics
	function recordOutboundLink(link, category, action) {
	  try {
	    var myTracker=_gat._getTrackerByName();
	    _gaq.push(['myTracker._trackEvent', ' + category + ', ' + action + ']);
	    setTimeout('document.location = "' + link.href + '"', 100)
	  }catch(err){}
	}
	
	// Open external links in new tabs
	 $("a[href^='http']").attr('target','_blank');
		
	
	/* ------------------------------------------------------
		For hi-lighting the nav links
	--------------------------------------------------------- */
	 
	// Init the variables
	var current_section;
	var current_subpage;
	
	// Split the URL
	path_array = window.location.pathname.split( '/' );
	
	// Get the section and subpage
	current_section = path_array[1]; 
	current_subpage = path_array[2]; 
	
	// If there is a section name, (about, schedule, etc.) set the corresponding #nav a class to active
	if(current_section){
		$('#nav a[href*="'+current_section+'"]').addClass('active');
	}
	
	// If there is a sub-page name, (about/about-noah, etc.) set the corresponding #left a's class to active
	if(current_subpage){
		$('#left a[href$="'+current_subpage+'/"]').addClass('active');
		
		// Add to the HTML title
		
		// Remove the dashes from the subpage and add to the title
		sub_title = $(document).attr('title') + " - " + current_subpage.replace(/\-/g,' ');
		
		// Capitalize the first letter after a space
		sub_title = sub_title.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );
		
		// Set the title
		$(document).attr('title', sub_title);
	}
	
	/* ------------------------------------------------------
		Schedule page
	--------------------------------------------------------- */
		
	// Hiding table rows on Schedule page
	switch(current_subpage){
		case 'workshops':
		type = 'WS';
		break;
		
		case 'teacher-trainings':
		type = 'TT';
		break;
		
		case 'immersions':
		type = 'IM';
		break;
		
		case 'retreats':
		type = 'RT';
		break;
	
		//case 'la-classes':
		//type = 'CL';
		//break;
				
		default:
		type = 'all';
		break;
	}	
	
	if(type != 'all'){
		
		$("#scheduletable").find('tbody tr').each(function() {
	
	      if ($('td', this).filter(':nth-child(4)').text() == type) {
	
	        $(this).show();
	
	      }
	
	      else if ($('th',this).length == 0){
	
	        $(this).hide();
	
	      }
	
	    });
		
	}
	
	
	// Hide / Reveal the Site Index
	$('#site-index span').hide();
	
	$('a.site-index').click(function(){
		$('#site-index span').toggle('slow');
	});
	
	
		

	//var s = 'July 5, 2011';
	
	//r = s.replace(/ - \d+/,'');
		
	//window.alert(r);
	
	// Sort states by , 
	
	/*
	
	var s = 'Flagstaff, Canada';
	s = s.substring(100, s.lastIndexOf(',') +1 );
	
	window.alert(test);
	
	headers: { 
                1: { 
                    sorter:'CommaText' 
                } 
            } 

	
	*/
	


	
	// add parser through the tablesorter addParser method 
    $.tablesorter.addParser({ 
        // set a unique id 
        id: 'CommaText', 
        is: function(s) { 
            // return false so this parser is not auto detected 
            return false; 
        }, 
        format: function(s) { 
            // format your data for normalization 
            return s.substring(100, s.lastIndexOf(',') +1 );
        }, 
        // set type, either numeric or text 
        type: 'text' 
    }); 
    
    // add parser through the tablesorter addParser method 
    $.tablesorter.addParser({ 
        // set a unique id 
        id: 'DashDate', 
        is: function(s) { 
            // return false so this parser is not auto detected 
            return false; 
        }, 
        format: function(s) { 
            // format your data for normalization 
            
            // Get just the first date of the event
            
            	//d = "November 11 - 13, 2011";
				//d = "March 30 - April 2, 2011";
				//d = "July 6, 2010";
				
				s_array = s.split(',');
				s_date = s_array[0].replace(/ - .+/,''); 
				s_year = s_array[1];
				s_new = s_date +', '+ s_year;
				
				//window.alert(s_new);
            
            // Format date as number, taken from source usLongDate
            return $.tablesorter.formatFloat(new Date(s_new).getTime());

        }, 
        // set type, either numeric or text 
        type: 'numeric' 
    }); 
    
    
	
	// Sort the Schedule table
	$("#scheduletable").tablesorter({
		sortList: [[0,0]],
		widgets: ['zebra'],
		headers: { 
		   	0: { sorter:'DashDate' },
            1: { sorter:'CommaText' }
        } 

	}); 
	
	// Hide the past schedule rows
	if(type != "all") { $(".sort-info span").hide(); }
	
	$("tr.past").hide();
	
	$(".sort-info span").click(function(){
		if($("tr.past").css("display")=='none'){
			$(".sort-info span").html("Hide past dates");
		}else{
			$(".sort-info span").html("Show past dates");
		}	
		$("tr.past").toggle();
	
		setMainHeight();	
	});	
	
	
	/* ------------------------------------------------------
		jPlayer music player on home page
	--------------------------------------------------------- */
	  
  	$("#jpId").jPlayer({
		ready: function () {
			$(this).jPlayer("setMedia", {
        		mp3: "/audio/btl_edit_02.mp3", 
	        	ogg: "/audio/btl_edit_02.ogg" 
			})//.jPlayer("play");
		},		
		solution: "html, flash",
		swfPath: "http://noahmaze.com/js/",
		supplied: "mp3, ogg",
		volume: .2
	})
	.bind($.jPlayer.event.playing, function() { // Using a jPlayer event to avoid both jPlayers playing together.
			//console.log("now playing");
	});
		
		
	// Mouseover on header text (Noah Maze Yoga) image	
	headimg= new Image(); headimg.src="/i/global/header_Noah_Maze_Yoga-over.png";
	
	$("#main-header img").mouseover(function(){
		$(this).attr("src","/i/global/header_Noah_Maze_Yoga-over.png");
	});
	
	$("#main-header img").mouseout(function(){
		$(this).attr("src","/i/global/header_Noah_Maze_Yoga.png");
	});
	
	
	// Random quote at bottom of pages
	var quotes = new Array();
	quotes[0] = '<p class="loading">"We must become the change we want to see." <span>Mahatma Gandhi</span></p>';
	quotes[1] = '<p class="loading">"It is through your body that you realize you are a spark of divinity." <span>B.K.S. Iyengar</span></p>';
	quotes[2] = '<p class="loading">"The simplest pulse within us is the embodiment of the Supreme..." <span>John Friend</span></p>';	
	quotes[3] = '<p class="loading">"I only went out for a walk, and finally concluded to stay out till sundown,<br />for going out, I found, was really going in." <span>John Muir</span></p>';
	quotes[4] = '<p class="loading">"Adopt the pace of nature: her secret is patience." <span>Ralph Waldo Emerson</span></p>'
	quotes[5] = '<p class="loading">"When you inhale, you are taking the strength from God.<br />When you exhale, it represents the service you are giving to the world." <span>B.K.S. Iyengar</span></p>';
	quotes[6] = '<p class="loading">"In your vulnerable moments you are held by the current, floating.<br />In your empowered moments you swim with the current. In your ultimate moments you and the current are one....That&rsquo;s Anusara." <span>Dr. Douglas Brooks</span></p>'; 
	quotes[7] = '<p class="loading">"Freedom is our nature and the dignity and value of real diversity is the highest expression of yoga." <span>Dr. Douglas Brooks</span></p>'; 
	
	randno = Math.floor ( Math.random() * quotes.length ); 
	
	$("#quote").html(quotes[randno]);


	/* ------------------------------------------------------
		Main div height
	--------------------------------------------------------- */
	
	// For moving the main div's bottom shadown up and down - this is called on ready, load and resize
	
	function setMainHeight(){
		
		// Reset the div to it's "natural" height, if it's smaller than the window.		
		$('#main').css('height', "auto");	 
		
		// Get the win height minus the footer minus 10 so main is behind the footer		
		var win_h = $(window).height() - 240;
		
		var main_h = $('#main').height();

		// If the main is smaller than the window, set it to win height
		if(main_h < win_h) {
			$('#main').css('height', win_h);
		}
		
		if(main_h > win_h) {	
			$('#main').css('height', main_h);
		}
		
		delete main_h;
		delete body_h;
		
	}
	

	// Run on ready
	setMainHeight();

	// And on resize
	$(window).resize(function() {
		setMainHeight();
	});
	
	// And on load
	$(window).load(function(){
		setMainHeight();
	});


	/* ------------------------------------------------------
		This helps deal w/ FOUT on FF
		
		There is a hidden div w/ a sample of the webfonts
		Once these are loaded the nav links (in the same 
		font) are faded in.
	--------------------------------------------------------- */
	
	// Get the time for caching		
	var now = new Date().getTime();

	// Set the size attr of the iframe, it can't just be hidden
	$("#webfonts").attr({ src: '/pages/webfonts.php?'+now, width: '1', height: '1' });	
	
	//console.log($("#webfonts").attr("src"));
			
	if($.browser.mozilla){
		
		// Hide the navs
		$("#nav").addClass("loading");
		$("#left ul").addClass("loading");
	
		// When the iframe is loaded, show the navs
		$("#webfonts").load(function() {
			$("#nav").removeClass("loading",250);
			$("#left ul").removeClass("loading",250);
			$("p").removeClass("loading",250);	
		});
	}else{
		$("#nav").removeClass("loading");
		$("#left ul").removeClass("loading");
		$("p").removeClass("loading");	
	}		
	
		
});





	










