$(document).ready(function() {
	jQuery.easing.def = 'easeOutQuint';
	if ($('a.tip').length > 0) {
		$('a.tip').tooltip({ 
		    track: true, 
		    delay: 200, 
		    showURL: false, 
		    showBody: true, 
		    fade: 500 
		});
	}
	if ($('.lightbox').length > 0) {
		$('a.lightbox').fancybox({
			overlayColor:'#FDFBEB',
			overlayOpacity:.8,
			padding:0
		});
	}
	if ($('.lightbox_a').length > 0) {
		/*
		$('.lightbox_a').fancybox({
			overlayColor:'#FDFBEB',
			overlayOpacity:.8,
			padding:0,
			ajax : { type:"GET" }
		});
		*/
		$('.lightbox_a').click(function() { return false; });
	}
	if ($('.page_about_almanac').length > 0) about_setup();
	if ($('#email_signup_form').length > 0) email_signup();
	if ($('.page_home').length > 0) home_setup();
	if ($('#news_header').length > 0) news_setup();
	if ($('.gallery_page').length > 0) gallery_setup();
	if ($('#switch_projects').length > 0) switch_pagination();
	if ($('a#b_view_slideshow').length > 0) button_placement();
});
function button_placement() {
	function readjust() {
		if ($(window).width() < 1030) {
			$('a#b_view_slideshow').css({'margin-left':'570px'});
		} else {
			var p = $('#content').offset();
			var x = 570+p.left;
			$('a#b_view_slideshow').css({'margin-left':x+'px'});
		}
		//$('a#b_view_slideshow')
	}
	readjust();
	$(window).resize(function() { readjust(); });
}
function switch_pagination() {
	var curr = 0;
	var w = 352;
	var total = $('ul.list').length;
	$('#prev_page,#next_page').click(function() {
		var dir = $(this).attr('href').substr(1);
		if (!$(this).hasClass('disabled')) {
			slide_page(dir);
		}
		return false;
	});
	function slide_page(dir) {
		if (dir == 'next') {
			curr++;
		} else {
			curr--;
		}
		set_buttons();
		var n = -(curr*w);
		$('#width').animate({'margin-left':n+'px'},500);
	}
	function set_buttons() {
		if (curr <= 0) {
			$('#prev_page').addClass('disabled');
			$('#next_page').removeClass('disabled');
		} else if (curr > 0 && curr < (total-1)) {
			$('#prev_page').removeClass('disabled');
			$('#next_page').removeClass('disabled');
		} else if (curr >= (total-1)) {
			$('#prev_page').removeClass('disabled');
			$('#next_page').addClass('disabled');
		}
	}
}
function gallery_setup() {
	var curr = 0;
	var t = $('#bg img').length;
	$('#b_project_info,#b_switch').click(function() {
		$(this).toggleClass('active');
		$('~ .dd',this).toggleClass('hide');
		return false;
	});
	
	$('.image_caption').fadeTo(0,0);
	$('.image_caption').eq(0).fadeTo(0,1);
	
	var scaler=Scaler('bg'),els=$('#bg').children(),width=100/els.length;
	var float=$('<span />').css({position:'absolute',color:'#fff',cursor:'pointer',zIndex:2});
	var div=$('<div/>').css({position:'absolute',left:0,bottom:0,marginRight:-20,height:'100%'});
	div.width(els.css({float:'left',width:width+'%'}).length+'00%').append(els);

	var swap=function(e){
			var key=e.type=='click'?(e.clientX>(document.body.clientWidth/2)?40:37):e.keyCode;
			//if(key>36&&key<41)div.animate({left:(scaler(key>38?'+1':'-1').to*-1)+'00%'},600);	//swap with animation
			div.animate({left:(scaler(curr).to*-1)+'00%'},{duration: 700, easing: 'easeOutQuint'});
	}
	
	function fade_captions(a) {
		$('.image_caption').fadeTo(300,0,function() {
			$('.image_caption').eq(a).fadeTo(300,1);
		});
	}
	
	$('#b_main_next').bind('click',function(e) {
		if (curr+1 >= t) {
			curr = 0;
		} else {
			curr++;
		}
		fade_captions(curr);
		set_buttons(curr);
		swap(e);
		return false;
	});
	$('#b_main_prev').bind('click',function(e) {
		if (curr <= 0) {
			curr = t-1;
		} else {
			curr--;
		}
		fade_captions(curr);
		set_buttons(curr);
		swap(e);
		return false;
	});
	$(window).bind('keypress',function(e) {
		if (e.keyCode == 39) {
			// forward
			if (curr+1 >= t) {
				curr = 0;
			} else {
				curr++;
			}
			fade_captions(curr);
			set_buttons(curr);
			swap(e);
			return false;
		}
		if (e.keyCode == 37) {
			// back
			if (curr <= 0) {
				curr = t-1;
			} else {
				curr--;
			}
			fade_captions(curr);
			set_buttons(curr);
			swap(e);
			return false;
		}
		
	});

	//$(window).bind('keydown',swap);
	$('#bg').bind('click',swap).append(div,float);
	
	$('#image_links a').bind('click',function(e) {
		curr = $('#image_links a').index(this);
		var n = Number($(this).html())-1;
		fade_captions(n);
		set_buttons(n);
		swap(e);
		return false;
	});
	function set_buttons(i) {
		$('#image_links a').removeClass('active');
		$('#image_links a').eq(i).addClass('active');
	}
}
function home_setup() {
	$('span.ring').fadeTo(0,0);
	$('a.glow').mouseover(function() { $('> span.ring',this).fadeTo(300,1); });
	$('a.glow').mouseout(function() { $('> span.ring',this).fadeTo(300,0); });
	
	function resize_slide_imgs() {
		// 1440 x 1024
		$('.slide_in').each(function() {
			var w = $(this).width();
			var h = $(this).height();
			
			// find proportional image for height.
			var proposed_h = (1024*h)/1440;
			var proposed_w = (1440*w)/1024;
			//alert('proposed_h: '+proposed_h+'\rproposed_w: '+proposed_w);
			
			if (w/h > 1440/1024) {
				var nh = (1024*w)/1440;
				var t = (nh-h)/2;
				$('img',this).css({'width':w+'px','height':nh+'px','margin-top':-t+'px','margin-left':'auto'});
			} else {
				var nw = (1440*h)/1024;
				var l = (nw-w)/2;
				$('img',this).css({'width':nw+'px','height':h+'px','margin-left':-l+'px','margin-top':'auto'});
			}
		});
	}

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

	
	var s = 1;
	function reset_animation() {
		//$('.slide_in').css({'margin-left':'1000px'}).fadeTo(0,1);
		$('.items').each(function() {
			$('> a.tip',this).each(function() {
				if (!$(this).hasClass('active')) {
					$(this).fadeTo(0,0,function() {
						$(this).css({'display':'none'});
					});
				} else {
					$(this).fadeTo(0,1,function() {
						$(this).css({'display':'block'});
					});
				}
			});
		});
		
		var n = Math.round(Math.random());
		var d = (n == 1) ? 'case_box' : 'work_box';
		setTimeout(function() { animate_a(d) },7000);
		s = 1;
	}
	
	// set this to be "random" for either work or case
	reset_animation();	
	
	function animate_a(box) {
		var fade_bw = 5000;
		var fade_tm = 1000;
		var opp = (box == 'work_box') ? 'case_box' : 'work_box';
		$('#'+box+' a.glow span.ring').fadeTo(500,1,function() {
			$(this).fadeTo(2000,0,function() {
				$('#'+box+' a.glow').animate({'margin-left':'-1000px'},2500);
				if (box == 'work_box') { var nn = 1; } else { var nn = 0; }
				$('#'+box+' .slide_in').animate({'margin-left':nn+'px'},2500,function() {
					if (box == 'work_box') {
						$('a#b_social_facebook,a#b_social_twitter').css({'display':'none'});
					}
					
					$('#'+box+' .slide_in a.tip').eq(0).delay(fade_bw).fadeTo(fade_tm,0,function() {
						$(this).css({'display':'none'});
					});
					$('#'+box+' .slide_in a.tip').eq(1).css({'display':'block'});
					$('#'+box+' .slide_in a.tip').eq(1).delay(fade_bw).fadeTo(fade_tm,1,function() {
						$(this).delay(fade_bw).fadeTo(fade_tm,0,function() {
							$(this).css({'display':'none'});
						});
						$('#'+box+' .slide_in a.tip').eq(2).css({'display':'block'});
						$('#'+box+' .slide_in a.tip').eq(2).delay(fade_bw).fadeTo(fade_tm,1,function() {
							if (box == 'work_box') {
								$('a#b_social_facebook,a#b_social_twitter').css({'display':'block'});
							}
							$('#'+box+' a.glow').css({'margin-left':'-72px'}).fadeTo(0,0);
							$('#'+box+' .slide_in').delay(fade_bw).fadeTo(1500,0,function() {
								$('#'+box+' a.glow img').fadeTo(0,1).css({'display':'block'});
								$('#'+box+' a.glow').fadeTo(1000,1);
								$(this).css({'display':'none'});
								s++;
								if (s <= 2) {
									setTimeout(function() { animate_a(opp) },5000);
								} else {
									$('#news_box a.glow').delay(5000).fadeTo(1000,0,function() {
										$('#news_box .fade_in').fadeTo(0,0,function() {
											$(this).css({'display':'block'});
											$(this).fadeTo(1000,1,function() {
												$(this).delay(6000).fadeTo(1500,0,function() {
													$(this).css({'display':'none'});
													$('#news_box a.glow img').fadeTo(0,1).css({'display':'block'});
													$('#news_box a.glow').fadeTo(1000,1);
													// reset and delay
													reset_animation();
												});
											});
										});
									});
								}
							});
						});	
					});
				});
			});
		});
	}
}
function news_setup() {
	$('#search_form input.text').focus(function() { $(this).css({'background-image':'none'}); });
	$('#search_form input.text').blur(function() { if ($(this).attr('value') == '') $(this).css({'background-image':'url(/library/images/text-search-entries.gif)'}); });
	
	$('#b_cats_tags,h3#t_cats_tags').click(function() {
		$('#dd_cats').toggleClass('hide');
		return false;
	});
	
	$('.list_wrap a.button').click(function() {
		$('.list_wrap a.button, ul.list').each(function() { $(this).removeClass('active'); });
		$(this).addClass('active');
		var name = $(this).attr('id').substr(2);
		$('#list_'+name).addClass('active');
		return false;
	});
}
function email_signup() {
	$('a#b_signup,a#b_email_signup').click(function() {
		if (!$(this).hasClass('complete')) {
			$('#email_signup_form').removeClass('hide');
		}
		return false;
	});
	$('#email').focus(function() { $(this).css({'background-image':'none'}); });
	$('#email').blur(function() { if ($(this).attr('value') == '') $(this).css({'background-image':'url(/library/images/text-your-email-address.gif)'}); });
	//$('#email_signup_form,#b_submit').focusout(function() { $('#email_signup_form').addClass('hide'); });
	$('#b_submit').click(function() {
		var email = $('#email').val();
		$.ajax({
			type: 'POST',
			url:'/library/ajax/email-signup.php',
			data:'email='+email,
			success:function(msg) {
				if (msg == 'Success') {
					$('#email_signup_form').css({'background-color':'#000000'}).html('<p>Ok, we\'ve got you down!</p>').delay(1000).fadeTo(500,0,function() {
						$(this).hide();
						$('a#b_signup,a#b_email_signup').css({
							'background-position':'bottom left',
							'cursor':'default'
						}).addClass('complete');
					});
				} else {
					$('#email').attr('value',msg);
				}
			}
		});
		return false;
	});
}

function about_setup() {
	$('.pseudo_lightbox').fadeTo(0,0,function() { $(this).css('display','none'); });
	$('a.b_more').click(function() {
		var name = $(this).closest('div').attr('id')+'_box';
		//$('#'+name).removeClass('hide');
		$('#'+name).css('display','block');
		$('#'+name).fadeTo(300,1);
		return false;
	});
	$('.pseudo_lightbox a.b_close').click(function() {
		//$(this).closest('.pseudo_lightbox').addClass('hide');
		$(this).closest('.pseudo_lightbox').fadeTo(300,0,function() { $(this).css('display','none'); });
		return false;
	});
}
