/* sad.js 03.10.07 robb@3st.com */

var curr_bH = 0; // current background (gradient) height
var curr_wH = 0; // rounded body height 
var curr_hash = ''; // for tracking active hash
var curr_subHash = '';
var currm = '';

var fullTitles = {
	"#home"			: "Welcome | Sir Andrew Davis",
	"#biography"	: "Biography | Sir Andrew Davis",
	"#discography"	: "Discography | Sir Andrew Davis",
	"#resources"	: "Resources | Sir Andrew Davis",
	"#schedule"		: "Schedule | Sir Andrew Davis",
	"#contact"		: "Contact | Sir Andrew Davis"
}

var aHomeWidths = { // heights for homepage sad image
	"600"	: "226", 
	"700"	: "264", 
	"800"	: "302", 
	"900"	: "339", 
	"1000"	: "377", 
	"1100"	: "415", 
	"1200"	: "452", 
	"1300"	: "490", 
	"1400"	: "528", 
	"1500"	: "565", 
	"1600"	: "603"
};

var aBioWidths = { // heights for bio sad image 
	"600"	: "207", 
	"700"	: "242", 
	"800"	: "276", 
	"900"	: "311", 
	"1000"	: "345", 
	"1100"	: "380", 
	"1200"	: "414", 
	"1300"	: "449", 
	"1400"	: "483", 
	"1500"	: "518", 
	"1600"	: "552"
};

var loggedIn = true;

function login() {
	var u = $('#username').val();
	var p = $('#password').val();
	var params = {'ajax':true, 'username': $('#username').val(), 'password': $('#password').val()};

  	$.post("/schedule/login", params, function(s){ 
//	alert(s);
		eval(s);
//		alert(ret.result);
		$('#login-output').html(ret.message);
		
		$('.login').hide();
		$('.logout').show();
		
		//'<br/><br/><a href="/schedule/printable/">Printable schedule</a>');
		if (ret.result == 1) {
			setTimeout("window.location.hash = 'schedule';", 500);
		}
//		alert(s);
    });
}

function subscribe() {
	var e = $('#subscribe_email').val();
	var params = {'ajax':true, 'email': e};

  	$.post("/contact/subscribe", params, function(s){ 
		$('#subscribe_message').html(s);
	});
}

function setGradient() {
	if (curr_hash == '#home') 
		cleanup(false, curr_hash);
	
	$('.portrait').hide();
  
    var wH = $(window).height();
    var bH = 600; // default

    if (wH > 550) // for all...it's better to go over than under
		bH = 1024;
    if (wH > 974) 
		bH = 1200;
    if (wH > 1150) 
		bH = 1600;
    if (wH > 1550) 
		bH = 2000;
    if (wH > 1950) 
		bH = 2400; 
       
    var r_wH = Math.ceil(wH/100)*100; // slightly less options for the images                
    if (r_wH < 600) r_wH = 600;    
    if (r_wH > 1600) r_wH = 1600;
                   
    if (bH != curr_bH)
        $('body').addClass('bg' + bH).removeClass('bg' + curr_bH);
   
    $('.home-content').css('left', (parseInt(aHomeWidths[r_wH]) + -40)+'px');
    $('.copyright').css('left', (parseInt(aHomeWidths[r_wH]) -0)+'px');
   
    if (r_wH >= 650) // blow up any type on the page 
    {
        $('.home-content').addClass('big');
        $('.copyright').addClass('bigcopy');
    }
    else {
        $('.home-content').removeClass('big');
        $('.copyright').removeClass('bigcopy');
    }
    
 	setTimeout("show('"+curr_hash+"')", 500);

    curr_bH = bH;
	curr_wH = r_wH;
}

function showPortrait() {
    $('.portrait').show().animate({opacity: 'show', width: 'show'}, 'slow');
}

function hidePortrait() {
    $('.portrait').animate({opacity: 'hide', width: 1}, 'slow');
	setTimeout("$('.portrait').hide()", 500);
}

function cleanup(h, c) {	

    if (c == '#home') {
        hidePortrait();
		setTimeout("$('.portrait').html('')", 500);
		if (h != '#home') {
        	$('.home-content').animate({opacity: 'hide'}, 'slow');
        	$('.copyright').animate({opacity: 'hide'}, 'slow');
		}
    }

	if (c == '#biography' && curr_subHash != 'timeline') {
        hidePortrait();
		$('#biography-scroller').hide();
		$('#background-scroller').animate({opacity: 'hide'}, 'slow');
		$('#biography-up').animate({opacity: 'hide'}, 'slow');
		$('#biography-down').animate({opacity: 'hide'}, 'slow');
		setTimeout("$('.portrait').html('')", 500);
	}
	
	if (c == '#biography' && curr_subHash == 'timeline') {
        hidePortrait();
		$('#timeline-scroller').hide();
		$('#background-scroller').animate({opacity: 'hide'}, 'slow');
		$('#timeline-up').animate({opacity: 'hide'}, 'slow');
		$('#timeline-down').animate({opacity: 'hide'}, 'slow');		
		setTimeout("$('.portrait').html('')", 500);
	}
	
	if (c == '#discography') {
	    $('#discography-letters').hide();
        hidePortrait();
		$('#discography-scroller').hide();
		$('#background-scroller').animate({opacity: 'hide'}, 'slow');
		$('#discography-up').animate({opacity: 'hide'}, 'slow');
		$('#discography-down').animate({opacity: 'hide'}, 'slow');
	}

	if (c == '#resources') {
        hidePortrait();
		$('#resources-scroller').hide();
		$('#background-scroller').animate({opacity: 'hide'}, 'slow');
		$('#resources-up').animate({opacity: 'hide'}, 'slow');
		$('#resources-down').animate({opacity: 'hide'}, 'slow');
	}

	if (c == '#schedule') {
	    $('#schedule-months').hide();
        hidePortrait();
		$('#schedule-scroller').hide();
		$('#background-scroller').animate({opacity: 'hide'}, 'slow');
		if (curr_subHash != 'login') {
			$('#schedule-up').animate({opacity: 'hide'}, 'slow');
			$('#schedule-down').animate({opacity: 'hide'}, 'slow');
		}
	}

	if (c == '#contact') {
        hidePortrait();
		$('#contact-scroller').hide();
		$('#background-scroller').animate({opacity: 'hide'}, 'slow');
		$('#contact-up').animate({opacity: 'hide'}, 'slow');
		$('#contact-down').animate({opacity: 'hide'}, 'slow');
	}
    
	if (h)
    	setTimeout("show('"+h+"');", 1000);
}

function show(h) {	
	$('.portrait').hide();
	
	if (fullTitles[h])
		document.title = fullTitles[h];
		
	if (h == '#home') {		
	    $('.portrait').html('<img src="/assets/site/images/home/sad_' + curr_wH + '.png" onload="showPortrait();" width="' + aHomeWidths[curr_wH] + '" height="' + curr_wH + '" alt="" title="" />');
        $('.home-content').animate({opacity: 'show'}, 'slow');
        $('.copyright').animate({opacity: 'show'}, 'slow');
    }

	/* biography -----------------------------*/

	if (h == '#biography' && curr_subHash != 'timeline') {
	    var params = {'ajax':true};

		$('#biography-scroller').html('<img src="/assets/site/images/biography_loading.gif" width="32" height="32" alt="loading&hellip;" title="loading&hellip;" />');
	
	  	$.post("/biography", params, function(s){ 
			$('#biography-scroller').html(s);

			var scrollerLeft = (Math.floor(aBioWidths[curr_wH]/40))*40;

			var wH = $(window).height();
			var wW = $(window).width();

			var nL = scrollerLeft + 40;
			var nW = wW - nL - 446; 
			var nH = wH - 80;

			if (nW < 175) {
				nW += nL-40;
				nL = 40;
				$('.portrait').html('');
			} else {
				$('.portrait').html('<img src="/assets/site/images/bio/bio2_' + curr_wH + '.png" onload="showPortrait();" width="' + aBioWidths[curr_wH] + '" height="' + curr_wH + '" alt="" title="" />');
			}

			$('#background-scroller').css('background', '#9eca5b').animate({opacity: 'show'}, 'slow').css('left', (nL-40)+'px').css('width', (nW+60)+'px');
			$('#biography-scroller').css('height', nH+'px').css('width', nW+'px').css('left', nL+'px').show();

			$('#biography-up').css('left', (nL-40)+'px').css('top', '0px').show();
			$('#biography-down').css('left', (nL-40)+'px').css('top', (wH-20)+'px').show();
		});  			
	}
	
	/* biography/timeline -----------------------------*/

	if (h == '#biography' && curr_subHash == 'timeline') {
		
		var params = {'ajax':true};
	 //	$('#timeline-scroller').html('');
		$('#timeline-scroller').html('<img src="/assets/site/images/biography_loading.gif" width="32" height="32" alt="loading&hellip;" title="loading&hellip;" />');
	
	
	  	$.post("/biography/timeline", params, function(s){ 

		 	$('#timeline-scroller').html(s);
			
			var scrollerLeft = (Math.floor(aBioWidths[curr_wH]/40))*40;
		
			var wH = $(window).height();
			var wW = $(window).width();
		
			var nL = scrollerLeft + 40;
			var nW = wW - nL - 446; 
			var nH = wH - 80;
		
			// nW += nL-40;
			// nL = 40;
			
			if (nW < 175) {
				nW += nL-40;
				nL = 40;
				$('.portrait').html('');
			} else {
				$('.portrait').html('<img src="/assets/site/images/bio/bio1_' + curr_wH + '.png" onload="showPortrait();" width="' + aBioWidths[curr_wH] + '" height="' + curr_wH + '" alt="" title="" />');
			}
			
			$('#background-scroller').css('background', '#9eca5b').animate({opacity: 'show'}, 'slow', $('#timeline-scroller').css('height', nH+'px').css('width', nW+'px').css('left', nL+'px')).css('left', (nL-40)+'px').css('width', (nW+60)+'px').show();
			
			$('#timeline-scroller').css('height', nH+'px').css('width', nW+'px').css('left', nL+'px').show();
			
			$('#timeline-up').css('left', (nL-40)+'px').css('top', '0px').show();
			$('#timeline-down').css('left', (nL-40)+'px').css('top', (wH-20)+'px').show();
		});
	}
	
	/* discography -----------------------------*/
	
	if (h == '#discography') {
		var params = {'ajax':true};
	
		var subPage = curr_subHash;
		if (subPage == undefined)
			subPage = 'newreleases';
	
		$('#discography-scroller').html('<img src="/assets/site/images/discography_loading.gif" width="32" height="32" alt="loading&hellip;" title="loading&hellip;" />');
		
		var scrollerLeft = (Math.floor(aBioWidths[curr_wH]/40))*40;
	
		var wH = $(window).height();
		var wW = $(window).width();
	
		var nL = scrollerLeft + 40;
		var nW = wW - nL - 446; 
		var nH = wH - 80;

		nW += nL-40;
		nL = 40;
	

		$('#background-scroller').css('background', '#84b6b9').animate({opacity: 'show'}, 'slow', $('#discography-scroller').css('height', nH+'px').css('width', nW+'px').css('left', nL+'px').show()).css('left', (nL-40)+'px').css('width', (nW+60)+'px');
		$('#discography-up').css('left', (nL-40)+'px').css('top', '0px').show();
		$('#discography-down').css('left', (nL-40)+'px').css('top', (wH-20)+'px').show();
		
	  	$.post("/discography/"+subPage, params, function(s){ 
		 	$('#discography-scroller').html(s);
//		 	$('#discography-letters').fadeIn('slow');
		 	$('#discography-letters').html($('#discography-letters-contents').html()).fadeIn('slow');
		 	$('#discography-letters-contents').html(' ');
		});
	}
	
	/* resources -----------------------------*/

	if (h == '#resources') {
		var params = {'ajax':true};
		
		var scrollerLeft = (Math.floor(aBioWidths[curr_wH]/40))*40;
		
		var wH = $(window).height();
		var wW = $(window).width();
		
		var nL = scrollerLeft + 40;
		var nW = wW - nL - 446; 
		var nH = wH - 80;
		
		if (nW < 175) {
			nW += nL-40;
			nL = 40;
			$('.portrait').html('');
		} else {
			$('.portrait').html('<img src="/assets/site/images/bio/bio3_' + curr_wH + '.png" onload="showPortrait();" width="' + aBioWidths[curr_wH] + '" height="' + curr_wH + '" alt="" title="" />');
		}
	
		$('#background-scroller').css('background', '#616ba6').animate({opacity: 'show'}, 'slow', $('#resources-scroller').css('height', nH+'px').css('width', nW+'px').css('left', nL+'px').show()).css('left', (nL-40)+'px').css('width', (nW+60)+'px');
		$('#resources-up').css('left', (nL-40)+'px').css('top', '0px').show();
		$('#resources-down').css('left', (nL-40)+'px').css('top', (wH-20)+'px').show();
		
		$('#resources-scroller').html('<img src="/assets/site/images/resources_loading.gif" width="32" height="32" alt="loading&hellip;" title="loading&hellip;" />');
		
		$.post("/resources", params, function(s){ 
		 	$('#resources-scroller').html(s);
		});
	  	
	}
	
	/* schedule -----------------------------*/
	
	if (h == '#schedule') {

		var params = {'ajax':true};
	
		var subPage = curr_subHash;
		if (subPage == undefined)
			subPage = '2009';
	
		var scrollerLeft = (Math.floor(aBioWidths[curr_wH]/40))*40;
				
		var wH = $(window).height();
		var wW = $(window).width();
				
		var nL = scrollerLeft + 40;
		var nW = wW - nL - 446; 
		var nH = wH - 80;
			
		nW += nL-40;
		nL = 40;
				
		$('.portrait').html('');
			
		$('#background-scroller').css('background', '#b13057').animate({opacity: 'show'}, 'slow', $('#schedule-scroller').css('height', nH+'px').css('width', nW+'px').css('left', nL+'px').show()).css('left', (nL-40)+'px').css('width', (nW+60)+'px');
		
		if (subPage == 'login' || subPage == 'logout') {
			$('#schedule-up').hide();
			$('#schedule-down').hide();
        	$('#schedule-months').hide();
		}
		else {
			$('#schedule-up').css('left', (nL+42)+'px').css('top', '0px').show();
			$('#schedule-down').css('left', (nL+42)+'px').css('top', (wH-20)+'px').show();
        	$('#schedule-months').fadeIn('slow');
		}
		
		//$('#schedule-scroller').html('');
		
		if (curr_subHash == 'logout' && loggedIn) {
		    loggedIn = false;

		   	$.post("/schedule/logout", params, function(s){ 
        		$('.login').show();
        		$('.logout').hide();
        		setTimeout("window.location.hash = 'schedule';", 500);
    			
            });
		}
		
		$('#schedule-scroller').html('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="/assets/site/images/schedule_loading.gif" width="32" height="32" alt="loading&hellip;" title="loading&hellip;" />');
		
	 	
	  	$.post("/schedule/"+subPage, params, function(s){ 
		 	$('#schedule-scroller').html(s);
		 	
        	$('.month_active').each(function(){
        		currm = this.id.split('_')[1];
        	});
        	
        	
        	$('#month_nav_'+currm).addClass('active');
	
		});
	}
	
	/* contact -----------------------------*/
	
	if (h == '#contact') {
		var params = {'ajax':true};
		var scrollerLeft = (Math.floor(aBioWidths[curr_wH]/40))*40;
		
		var wH = $(window).height();
		var wW = $(window).width();
		
		var nL = scrollerLeft + 40;
		var nW = wW - nL - 446; 
		var nH = wH - 80;

		if (nW < 175) {
			nW += nL-40;
			nL = 40;
			$('.portrait').html('');
		} else {
			$('.portrait').html('<img src="/assets/site/images/bio/bio4_' + curr_wH + '.png" onload="showPortrait();" width="' + aBioWidths[curr_wH] + '" height="' + curr_wH + '" alt="" title="" />');
		}

		$('#background-scroller').css('background', '#c45415').animate({opacity: 'show'}, 'slow', $('#contact-scroller').css('height', nH+'px').css('width', nW+'px').css('left', nL+'px').show()).css('left', (nL-40)+'px').css('width', (nW+60)+'px');
		$('#contact-up').css('left', (nL-40)+'px').css('top', '0px').show();
		$('#contact-down').css('left', (nL-40)+'px').css('top', (wH-20)+'px').show();
		
		$('#contact-scroller').html('<img src="/assets/site/images/contact_loading.gif" width="32" height="32" alt="loading&hellip;" title="loading&hellip;" />');
		
		$.post("/contact", params, function(s){ 
		 	$('#contact-scroller').html(s);
		});		
	}
}

function showMonth(m) {
    if (currm == '') 
    	$('.month_active').each(function(){
    		currm = this.id.split('_')[1];
    	});
	
	if (currm != m) {
		$('#month_'+currm).fadeOut('slow').removeClass('month_active').addClass('month_inactive');
		$('#month_nav_'+currm).removeClass('active');
	}

	$('.month_inactive').hide();
	$('#schedule-scroller').get(0).scrollTop = 0;
    
    currm = m;
	
	$('#month_'+m).fadeIn('slow').removeClass('month_inactive').addClass('month_inactive');
	$('#month_nav_'+m).addClass('active');
}

function drawMenu(h) {	
	var tempH = h.split('/');
	var sH;
	
	h = tempH[0];
	if (tempH[1])
		sH = tempH[1];
	else
		sH = '';
		
	if (h == '#discography' && sH == '') {
		sH = 'newreleases';
	}

	if (h == '#schedule' && sH == '') {
		sH = '2009';
	}
	
	$('a.menu-item').each(function() {
		if (h == '#home') {
			var hr = this.href.split('/');
			var id = hr[hr.length-1].substring(1);
			var mo = $('#'+id+"-image").get(0).src='/assets/site/images/nav/'+id+'_1.png';
			$(this).parent().find('.submenu-item').hide();
		} else {
			var hr = this.href.split('/');
			var id = hr[hr.length-1].substring(1);			
			if (h != ('#'+id)) {
				var mo = $('#'+id+"-image").get(0).src='/assets/site/images/nav/'+id+'_0.png';		
				$(this).parent().find('.submenu-item').hide();
			} else
			{
				var mo = $('#'+id+"-image").get(0).src='/assets/site/images/nav/'+id+'_2.png';
				setTimeout("$('#"+id+"-image').get(0).src='/assets/site/images/nav/"+id+"_2.png'",100);

				$(this).parent().find('.submenu-item').each(function() {
					$(this).show();
					
					var hr = this.href.split('/');
					var id = hr[hr.length-1];
					if (id == sH) {
						$(this).find('img').each(function() {
							$(this).get(0).src = '/assets/site/images/nav/'+id+'_1.png';
						});
					} else {
						$(this).find('img').each(function() {
							$(this).get(0).src = '/assets/site/images/nav/'+id+'_0.png';
						});
					}
				});
			}
		}
	})
}

function hashListener() {
    var h = window.location.hash;
	var sH;
	
	var tempH = h.split('/');
	h = tempH[0];
	
	if (!tempH[1])
		sH = '';
	else
		sH = tempH[1];

	if (h == '')
		h = '#home';
	
    if (h != curr_hash || tempH[1] != curr_subHash) {
        cleanup(h, curr_hash);
        curr_hash = h;
		curr_subHash = tempH[1];
    }
    setTimeout('hashListener();', 500);
}

function overButton(action, scroller) {
	
}

function outButton(action, scroller) {
	
}

var downAction;
var downScroller;

function downButton(action, scroller) {
	downAction = action;
	downScroller = scroller;
	setTimeout('buttonListener()', 50);
	return false;
}

function buttonListener() {
	if (downAction && downScroller) {
		if (downAction == 'down')
			$('#'+downScroller+'-scroller').get(0).scrollTop += 10;
		if (downAction == 'up')
			$('#'+downScroller+'-scroller').get(0).scrollTop -= 10;		
		setTimeout('buttonListener()', 50);
	}
}

function upButton(action, scroller) {
	downAction = false;
	downScroller = false;
}

$(document).ready(function() {
    $('.nav').hide();
    $('.home-content').hide();
    $('.copyright').hide();
    
	/* preload over and active states */
	var menus = ['biography', 'discography', 'resources', 'schedule', 'contact'];
	var imagePreload = [];

	for (var menu in menus) {
		for (var i=0; i<3; i++) {
			newImg = new Image();
			newImg.src = '/assets/site/images/nav/'+menus[menu]+'_'+i+'.png';
			imagePreload.push(newImg);
		}
	}
	
	/* ditch silly borders */	
	$('a.logo-item').each(function() {
	    this.href = '#home';
	    $(this).click(function() { this.blur(); });
	}).click(function() {
		drawMenu('#home');		
	});

	$('a.menu-item').click(function() { 
		var hr = this.href.split('/');
		var id = hr[hr.length-1].substring(1);
		drawMenu('#'+id);		
		this.blur(); 
	});

	$('a.submenu-item').click(function() { 
		var hr = this.href.split('/');
		var id = hr[hr.length-2]+'/'+hr[hr.length-1];
		drawMenu(id);
		this.blur(); 
	});
		
	/* mouseover */
	$('a.menu-item').mouseover(function() {
		if (curr_hash == '#home') {
			var hr = this.href.split('/');
			var id = hr[hr.length-1].substring(1);
			var mo = $('#'+id+"-image").get(0).src='/assets/site/images/nav/'+id+'_2.png';
		} else {
			var hr = this.href.split('/');
			var id = hr[hr.length-1].substring(1);
			if (curr_hash != ('#'+id)) {
				var mo = $('#'+id+"-image").get(0).src='/assets/site/images/nav/'+id+'_2.png';				
			}
		}
	});
	
	/* mouseout */
	$('a.menu-item').mouseout(function() {
		if (curr_hash == '#home') {
			var hr = this.href.split('/');
			var id = hr[hr.length-1].substring(1);
			var mo = $('#'+id+"-image").get(0).src='/assets/site/images/nav/'+id+'_1.png';
		} else {
			var hr = this.href.split('/');
			var id = hr[hr.length-1].substring(1);
			if (curr_hash != ('#'+id)) {
				var mo = $('#'+id+"-image").get(0).src='/assets/site/images/nav/'+id+'_0.png';				
			}
		}
	})
	
	/* rewrite paths to hashes */
	$('a.menu-item').each(function() {
	    var href = this.href.split('/');
	    href = href[href.length-2];
	    this.href = '#'+href;
	})
	  
	$('a.submenu-item').each(function() {
	    var href = this.href.split('/');
	    href = href[href.length-3]+'/'+href[href.length-2];
	    this.href = '#'+href;
	})  	
});

$(window).load(function() {	
    setGradient();
    $('.home-content').hide();
    $('.copyright').hide();
	var h = window.location.hash;
	if (h == '')
		h = '#home';
	
	drawMenu(h);
    $('.nav').fadeIn('slow');
	hashListener();
})

$(window).resize(function() {
    setGradient();
})

function showLetter(l) {
    if (l == 'all') {
        $('.letterlink').removeClass('active');    
        $('#letterlink_'+l).addClass('active').css('border', '#f0f');
        $('.letterview').show();
    } else {
        $('.letterlink').removeClass('active');    
        $('#letterlink_'+l).addClass('active').css('border', '#f0f');
        $('.letterview').hide();
        $('#letter_'+l).show();
    }
}