$(document).ready(function(){
  var time = 300;
  var slide_offset = 900;
  $('#nav a').click(function(){
    var num = $(this).attr('id');
   	//window.location.hash = "/" + num*1 + "";
		set_proper_hash(num*1);
    $('#track').animate({marginLeft:-1*num*slide_offset}, time);
    strip_active();
    $(this).addClass('active');
    return false;
  });
  $('#low_nav a').click(function(){
    var num = $(this).attr('id');
    $('#track').animate({marginLeft:-1*num*slide_offset}, time);
    strip_active();
    if(num + "" == num*1 + ""){ // top links
      $('#low_nav #0' + num).addClass('active');
    }else{ // bottom links
      $('#nav #' + num*1).addClass('active');
      //window.location.hash = "/" + num*1;
			set_proper_hash(num*1);
    }
    return false;
  });
  
	if(window.location.hash){
  	time = 0;
		var position = get_page_from_hash(window.location.hash);
  	$('#nav #' + String(position)).trigger('click');
  	time = 300;
	}
  
  var about_nav_loc = 1;
  var about_nav_offset = 860;
  $('#about_nav a').click(function(){
    about_nav_loc = $(this).attr('id');
    move_about_track(about_nav_loc, about_nav_offset);
    select_about_active(about_nav_loc);
    return false;
  });
  
  $('#about_arrow_left').click(function(){
    about_nav_loc--;
    if(about_nav_loc == 0) about_nav_loc = 8;
    move_about_track(about_nav_loc, about_nav_offset);
    select_about_active("00" + about_nav_loc);
    return false;
  });
  $('#about_arrow_right').click(function(){
    about_nav_loc++;
    if(about_nav_loc == 9) about_nav_loc = 1;
    move_about_track(about_nav_loc, about_nav_offset);
    select_about_active("00" + about_nav_loc);
    return false;
  });

	$('#store_music_link').click(function(){
		$('#store_subtrack').animate({marginLeft:0}, 300);
		$(this).parent().addClass('active');
		$('#store_apparel_link').parent().removeClass('active');
		return false;
	});
	$('#store_apparel_link').click(function(){
		$('#store_subtrack').animate({marginLeft:-500}, 300);
		$(this).parent().addClass('active');
		$('#store_music_link').parent().removeClass('active');
		return false;
	});
	
	$('#email').focus(function(){
		if($(this).val() == "EMAIL") $(this).val('');
	});
	$('#email').blur(function(){
		if($(this).val() == "") $(this).val('EMAIL');
	});
	
	if($.browser.msie){
		$('#slide_pete .desc p').css({'overflow':'auto'});
	}else{
		$('#slide_pete .desc p').jScrollPane();
	}
	
	$('img#sb-content').click(function(){
		alert('hi');
		Shadowbox.next();
	});
	
	// gallery sliders
	var images_gallery_pos = 0;
	var gallery_time = 300;
	var gallery_offset = 800;
	var num_image_sets = $('#images_arrow_right').attr('rel');
	$('#image_track').css({width:(num_image_sets+1)*gallery_offset});
	$('#images_arrow_right').click(function(){
		images_gallery_pos++;
		if (images_gallery_pos > num_image_sets) images_gallery_pos = 0;
		$('#image_track').animate({marginLeft:-1*gallery_offset*images_gallery_pos}, gallery_time);
		return false;
	});
	$('#images_arrow_left').click(function(){
		images_gallery_pos--;
		if (images_gallery_pos < 0) images_gallery_pos = num_image_sets;
		$('#image_track').animate({marginLeft:-1*gallery_offset*images_gallery_pos}, gallery_time);
		return false;
	});
	
	var videos_gallery_pos = 0;
	var num_video_sets = $('#videos_arrow_right').attr('rel');
	$('#video_track').css({width:(num_video_sets+1)*gallery_offset});
	if(num_video_sets == 0){
		$('#videos_arrow_right').css({visibility:'hidden'});
		$('#videos_arrow_left').css({visibility:'hidden'});
	}
	$('#videos_arrow_right').click(function(){
		videos_gallery_pos++;
		if (videos_gallery_pos > num_video_sets) videos_gallery_pos = 0;
		$('#video_track').animate({marginLeft:-1*gallery_offset*videos_gallery_pos}, gallery_time);
		return false;
	});
	$('#videos_arrow_left').click(function(){
		videos_gallery_pos--;
		if (videos_gallery_pos < 0) videos_gallery_pos = num_video_sets;
		$('#video_track').animate({marginLeft:-1*gallery_offset*videos_gallery_pos}, gallery_time);
		return false;
	});
	
	$('#mailer_form').submit(function(){
    //$.post("http://www.reverbnation.com/fan_reach/add_email_direct_internal/artist_436135", {email: $('#email').val()});
		$.post("./fan_email.php", {email: $('#email').val()});
    alert('Thanks!');
    return false;
  });
});

function strip_active(){
  $('#nav a, #low_nav a').removeClass('active');
}
function move_about_track(a, b){
  a = a-1;
  $('#about_subtrack').animate({marginLeft:-1*a*b}, 300);
}
function select_about_active(a) {
  $('#about_nav li').removeClass('active');
  $('#' + a).parent().addClass('active');
}
function set_proper_hash(num){
	var new_hash;
	switch(num){
		case 0: new_hash = ""; break;
		case 1: new_hash = "lessons"; break;
		case 2: new_hash = "coaches"; break;
		case 3: new_hash = "hotels"; break;
		case 4: new_hash = "courses"; break;
		case 5: new_hash = "contact"; break;
//		case 6: new_hash = "contact"; break; 
	}
	window.location.hash = new_hash;
}
function get_page_from_hash(string){
	switch(string){
		case "": return 0;
		case "#lessons": return 1;
		case "#coaches": return 2;
		case "#hotels": return 3;
		case "#courses": return 4;
		case "#contact": return 5;
//		case "#contact": return 6;
	}
	window.location.hash = "";
	return 0;
}
