/*
	javascript functions for ben beirs wordpress theme
*/

benbeirs(jQuery);

function benbeirs ($) {
	$(document).ready(init);
	
	function init() {
		// newsletter dropdown
		$('#head a#newsletter').click(function() {
			$('#newsletter-signup').stop().show().animate({'top':'0px'},450);
			return false;
		});
		$('#newsletter-signup a.close').click(function() {
			$('#newsletter-signup').stop().animate({'top':'-270px'},450,
			function() {
				$(this).hide();
			});
			return false;
		});
		
		$('#headerIMG').hover(
			function() {
				console.log('over');
				$(this).find('#img1').animate({'opacity': 0},300,function() {$(this).hide();});
			},
			function() {
				$(this).find('#img1').show().animate({'opacity': 1},300);
			}
		);
		
		
		// set size of horizontal scroller widgets
		$(window).resize(function() {
			$('.scroller').each(function() {
				if ($(window).width()>930) {
					$(this).width($(window).width());
				} else {
					$(this).width(930);
				}
			});
		});
		
		// blog header popouts
		/*
		$('#bloghead').each(function() {
			$('.tab').each(function() {
				$(this).click(function() {
					var popwindow = $('#bloghead').find('#'+$(this).attr('rel'));
					$(popwindow).show();
				});
			});
		});
		*/
		
		// scroller window on home and photo page
		$('.scroller').each(function() {
			var current = null;
			var scroller = $(this);
			var panes = $(this).find('.panes');
			$(this).width($(window).width());
			$(this).addClass('js');
			$('.pageheader').append('<div class="scrollnav"><a href="#" class="next imgreplace">next</a><a href="#" class="prev imgreplace">previous</a></div>');
			$(panes).width(100);
			var nav = $('.scrollnav:last');
			$(this).find('.panes .pane').each(function(i) {
				var div = $(this);
				if ($(this).find('img').length==0) {
					$(this).width(300);
					$(panes).width($(panes).width()+310);
				} else {
					var img = $(this).find('img:first');
					if ($(img).width()==0) {
						$(img).load(function() {
							div.width($(this).width());
							$(panes).width($(panes).width()+$(this).width()+20);
						});
					} else {
						div.width($(img).width());
						$(panes).width($(panes).width()+$(img).width()+20);
					}
				}
				if (i==0) {
					current = $(this);
					$(current).addClass('current');
				}
				$(this).append('<div class="hovericon"></div>');
				$(this).hover(
					function() {
						$(this).addClass('hover');
					},
					function() {
						$(this).removeClass('hover');
					}
				);
				$(this).click(function() {
					var l = $(this).position().left-parseInt($(panes).css('padding-left'));
					$(panes).animate({
						'left': -l
					},300);
					$(current).removeClass('current');
					current = $(this);
					$(this).addClass('current');
					$(this).prevAll('.pane').addClass('past');
					$(this).nextAll('.pane').removeClass('past');
					return false;
				});
			});
			$(nav).find('.next').click(function() {
				scroll_right();
				return false;
			});
			$(nav).find('.prev').click(function() {
				scroll_left();
				return false;
			});
			
			$(document).keydown(function(e){
				if (e.keyCode==37) {
					// left
					scroll_left();
				} else
				if (e.keyCode==39) {
					// right
					scroll_right();
				}
			});
			
			function scroll_right() {
				if ($(current).next('.pane').length!=0) {
					$(current).removeClass('current');
					current = $(current).next();
					$(current).addClass('current');
					$(current).prevAll('.pane').addClass('past');
					$(current).nextAll('.pane').removeClass('past');
					var l = $(current).position().left-parseInt($(panes).css('padding-left'));
					$(panes).stop().animate({
						'left': -l
					},300);
				}
			}
			
			function scroll_left()  {
				if ($(current).prev('.pane').length!=0) {
					$(current).removeClass('current');
					current = $(current).prev();
					$(current).addClass('current').removeClass('past');
					$(current).prevAll('.pane').addClass('past');
					$(current).nextAll('.pane').removeClass('past');
					var l = $(current).position().left-parseInt($(panes).css('padding-left'));
					$(panes).stop().animate({
						'left': -l
					},300);
				}
			}
		});
	}
}
