$(document).ready(function(){
	
	///////////////////////////
	/// FUNCTIONS / METHODS ///
	///////////////////////////
	if($(".gallery-item-thumb").size()){
		$(".gallery-item-thumb").lazyload({effect : "slideDown", placeholder : "/img/loader.gif"});
	}
	
	jQuery.linkFade = function(){
		$("#content").fadeIn(500);
		$("a:not(a[href^='http'], a[href^='mailto'], #work-filter, a.resize-image)").live("click", function (event) {
			if (event.button != 0) return true;
			else{
				var toLoad = $(this).attr('href');
				$(this).parents('.nav').find('.active').removeClass('active');
				$(this).addClass('active');
				$("#content").fadeOut(500, function(){
					window.location = toLoad;
				});
				return false;
			}
		});
	};
	
	jQuery.subNavAnimate = function(){
		$("#sub-nav ul").hide();
		$("#sub-nav ul").slideDown(1000);
	};
	
	jQuery.switchImage = function(){
		var old_image = $('.work-gallery-active-image');
		if (old_image.next('.work-gallery-item-full').size()) {
			var new_image = old_image.next('.work-gallery-item-full');
		}
		else{
			var new_image = $('.work-gallery-item-full:first');
		}
		$('#gallery-gallery-full').height(new_image.height());
		old_image.removeClass('work-gallery-active-image').fadeOut(duration, function(){
			new_image.fadeIn(duration).fadeTo(0, 1).addClass('work-gallery-active-image');
		});
		work_gallery_timer = setTimeout("$.switchImage();", switch_image_duration);
	};
	
	jQuery.reachedBottom = function(){
		if($("#news-items-pager:in-viewport").size()){
			setTimeout("$('#news-items-hidden').slideDown('slow'); $('#news-items-pager').slideUp('slow');", 1000);
		}
		else{
			setTimeout("$.reachedBottom();", 100);
		}
	};
	
	jQuery.switchLiveImage = function(){
		var speed = 700;
		var fade_speed = 200;
		if($("#live-image-rotator").size()){
			var active_image = $("#live-image-rotator .active-image");
			var next_image = $("#live-image-rotator .next-image");
			var preload_image1 = $("#live-image-rotator .preload-image1");
			var preload_image2 = $("#live-image-rotator .preload-image2");
			var preload_image3 = $("#live-image-rotator .preload-image3");
			next_image.fadeIn(fade_speed, function(){
				active_image.fadeOut(0).html(next_image.html()).fadeIn(0, function(){
					next_image.fadeOut(0).html(preload_image1.html());
					var new_image_number = 1 + parseInt($("#live-image-rotator .preload-image3 img").attr('src').replace('/livecam/station', '').replace('.jpg', ''), 10);
					if(new_image_number < 10){
						new_image_number = '0000'+new_image_number;
					}
					else if(new_image_number == 100){
						new_image_number = '00001';
					}
					else{
						new_image_number = '000'+new_image_number;
					}
					preload_image1.html(preload_image2.html());
					preload_image2.html(preload_image3.html());
					$("#live-image-rotator .preload-image3 img").attr('src', '/livecam/station'+new_image_number+'.jpg');
					setTimeout("$.switchLiveImage();", speed);
				});
			});
		}
	};
	$.linkFade();
	setTimeout("$.switchLiveImage();", 1000);
	var duration = 500;
	var switch_image_duration = 5000;
	if(!jQuery.support.opacity){
		$('html, input, .normal-font-size').css('font-size', '15px');
		$('.gallery-item-overlay, .gallery-item-overlay-wrapper').css('display', 'none');
		$('.gallery-item-overlay-wrapper').css('border', '1px solid #ccc');
		$('.gallery-item-overlay').css('background-color', '#fff');
		$('.gallery-item').hover( function(){
			$(this).find('.gallery-item-overlay').css('display', 'block');
			$(this).find('.gallery-item-overlay-wrapper').css('display', 'block');
		}, 	function(){
				$(this).find('.gallery-item-overlay').css('display', 'none');
				$(this).find('.gallery-item-overlay-wrapper').css('display', 'none');
			});
	}
	else{
		$('.gallery-item-overlay, .gallery-item-overlay-wrapper').fadeTo(0,0).css('display', 'block');
		$('.gallery-item-overlay').css('background-color', '#fff');
		$('.gallery-item').mouseover( function(){
	        $(this).find('.gallery-item-overlay').animate({ opacity: .7 }, { duration: duration, queue: false } );
	        $(this).find('.gallery-item-overlay-wrapper').animate({ opacity: 1 }, { duration: duration, queue: false } );
		});
		$('.gallery-item').mouseout( function(){
	        $(this).find('.gallery-item-overlay').animate({ opacity: 0 }, { duration: duration, queue: false } );
	        $(this).find('.gallery-item-overlay-wrapper').animate({ opacity: 0 }, { duration: duration, queue: false } );
		});
		$('.gallery-item-overlay, .gallery-item-overlay-wrapper').mouseover( function(){
	        $(this).find('.gallery-item-overlay').animate({ opacity: .7 }, { duration: duration, queue: false } );
	        $(this).find('.gallery-item-overlay-wrapper').animate({ opacity: 1 }, { duration: duration, queue: false } );
		});
		$('.gallery-item-overlay, .gallery-item-overlay-wrapper').mouseout( function(){
	        $(this).find('.gallery-item-overlay').animate({ opacity: 0 }, { duration: duration, queue: false } );
	        $(this).find('.gallery-item-overlay-wrapper').animate({ opacity: 0 }, { duration: duration, queue: false } );
		});
	}
	$('#work-filter.inactive').click( function(){
		$(this).toggleClass('inactive');
		var keyword = $('#work-filter-keyword').text() == 'None' ? '' : $('#work-filter-keyword').text();
		$('#work-filter-keyword').replaceWith('<input name="work-livesearch" id="work-livesearch" type="text" value="'+keyword+'"/>');
		$('#work-livesearch').focus();
		$("#work-livesearch").autocomplete('livesearch.php', {autoFill: true, multiple: true});
		return false;
	});
	
	$('#content #work-gallery').fadeTo(0,0);
	$('.work-gallery-item-full:not(.work-gallery-active-image)').fadeOut(0);
	$('.work-gallery-active-image').fadeIn(0);
	$('.gallery-detail-tab:not(.gallery-detail-tab-active)').fadeOut(0);
	$('#news-items-hidden').hide();
	
	$('#gallery-detail-nav ul li:not(.active)').live("click", function(){
		var new_tab = '#gallery-detail-tab-'+$(this).text();
		$('#gallery-detail-nav ul li.active').removeClass('active');
		$(this).addClass('active');
		$('#gallery-detail-tabs-wrapper').height($(new_tab).height());
		$('.gallery-detail-tab-active').removeClass('gallery-detail-tab-active').fadeOut(duration, function(){
			$(new_tab).fadeIn(duration).addClass('gallery-detail-tab-active');
		});
	});
	var work_gallery_timer = setTimeout("$.switchImage();", switch_image_duration);
	$('#content #work-gallery-thumb .work-gallery-item-thumb img').click(function(){
		var new_image = $(this).attr('src').replace('_thumb', '_big');
		$('.work-gallery-active-image').removeClass('work-gallery-active-image').css('display', 'none')
		$('.work-gallery-item-full img[src*='+new_image+']').parent().css('display', 'block').addClass('work-gallery-active-image');
		return false;
	});
	var duration = 500;
	$('#content #gallery-gallery-full').live("mouseout", function(){
		var opacity = 1;
		var opacity_gallery = 0;
		$('.work-gallery-item-full').animate({ opacity: opacity }, { duration: duration, queue: false } );
		$('#content #work-gallery-thumb').toggle().animate({ opacity: opacity_gallery }, { duration: duration, queue: false } );
		clearTimeout(work_gallery_timer);
		work_gallery_timer = setTimeout("$.switchImage();", switch_image_duration);
	});
	
	$('#content #gallery-gallery-full').live("mouseover", function(){
		var opacity = .3;
		var opacity_gallery = 1;
		$('.work-gallery-item-full').animate({ opacity: opacity }, { duration: duration, queue: false } );
		$('#content #work-gallery-thumb').toggle().animate({ opacity: opacity_gallery }, { duration: duration, queue: false } );
		clearTimeout(work_gallery_timer);
	});
	
	$('#content #work-gallery-thumb').live("mouseout", function(){
		var opacity = 1;
		var opacity_gallery = 0;
		$('.work-gallery-item-full').animate({ opacity: opacity }, { duration: duration, queue: false } );
		$('#content #work-gallery-thumb').toggle().animate({ opacity: opacity_gallery }, { duration: duration, queue: false } );
		clearTimeout(work_gallery_timer);
		work_gallery_timer = setTimeout("$.switchImage();", switch_image_duration);
	});
	$('#content #work-gallery-thumb').live("mouseover", function(){
		var opacity = .3;
		var opacity_gallery = 1;
		$('.work-gallery-item-full').animate({ opacity: opacity }, { duration: duration, queue: false } );
		$('#content #work-gallery-thumb').toggle().animate({ opacity: opacity_gallery }, { duration: duration, queue: false } );
		clearTimeout(work_gallery_timer);
	});
	if($("#news-items-pager").size()){
		$.reachedBottom();
	}
	if($("#live-clock").size()){
		$("#live-clock").jclock({ utc: true, utcOffset: 1, format: '%I:%M %p' });
	}
	$('.job-item').fadeOut(0);
	$('.job-link').bind('click', function(){
		var new_item = $('#job-item-'+$(this).attr('id'));
		$('.job-link.active').removeClass('active');
		$(this).addClass('active');
		$('#jobs-wrapper').height(new_item.height());
		if($(".job-item-active").size()){
			$('.job-item-active').removeClass('job-item-active').fadeOut(duration, function(){
				new_item.fadeIn(duration).addClass('job-item-active');
			});
		}
		else{
			new_item.fadeIn(duration).addClass('job-item-active');
		}
	});
	$(document).pngFix();
	$('.resize-image').live("click", function (event) {
		if (event.button != 0) return false;
		if($('.resize-image-full').size()){
    		$('.resize-image-full, .resize-image-small').stop().animate({ width: '578px', height: '325px' }, 500, '', function(){
	      		$(this).remove();
				$('#left_col, #right_col p, #gallery-detail').fadeIn(200);
			});   
		}
		else{
			var fullimage = $('.resize-image-thumb').attr('src').replace('thumb', 'full');
			var smallimage = $('.resize-image-thumb').attr('src');
			var alt = $('.resize-image-thumb').attr('alt').replace('thumb', 'full');
			$('.resize-image').append('<img src="" alt="'+alt+'" height="325" width="578" class="resize-image-full">');
			$('.resize-image-full').attr('src', fullimage).load(function() {
		        $('#left_col, #right_col p, #gallery-detail').fadeOut(200, function(){
		        	$('.resize-image-full').show();
		        	$('.resize-image-full, .resize-image-small').stop().animate({ width: '980px', height: '550px' }, 500);
					pageTracker._trackEvent('Pictures', 'click', window.location.pathname + "big-picture", "big_picture");
		        });
			});
		}
		return false;
	});
});