/*document.write('<style type="text/css">#intro {visibility:hidden} #all-container {height:0px; overflow:hidden;}</style>');*/
document.write('<style type="text/css">#people .carousel {display:none}</style>');
function resizeImg(imgObj){
	var maxHeight = 431;
	var maxWidth = 525;
	var getWidth = imgObj.width;
	var getHeight = imgObj.height;
	var aspect = getHeight/getWidth;
	if(getWidth > getHeight){
		
		// horizontal images
		if(getWidth > maxWidth){
			imgObj.width = maxWidth;
			imgObj.height = imgObj.width * aspect;
		} 	
							
	} else {
		
		//vertical or square images
		if (getHeight > maxHeight){
			imgObj.height = maxHeight;
			imgObj.width = maxHeight / aspect;
		}
		
	}
	return [Math.round(imgObj.height), Math.round(imgObj.width)];
	
}

// start jGallery extesion ________________________________________________________________*/
(function($){
		
    $.fn.imgSwap = function(options){
	
		var defaults = {};
		
		var options = $.extend(defaults, options);
        
		return this.each(function(){
			
			$(this).hover(function(){
				var img = $(this).find('img')
				var src = img.attr('src');
				var newSrc = src.replace('_off', '_on');
				img.attr('src', newSrc);
			}, function(){
				var img = $(this).find('img')
				var src = img.attr('src');
				var newSrc = src.replace('_on', '_off');
				img.attr('src', newSrc);
			})
		});
		
     };
})(jQuery);
// end jGallery extension________________________________________________________________*/

// start jGallery extesion ________________________________________________________________*/
(function($){
		
    $.fn.jGallery = function(options){
	
		var defaults = {};
		
		var options = $.extend(defaults, options);
        
		return this.each(function(){
			
			// maintain main jQuery object
			var $this = $(this);
			

			var iTitle = $(this).find('a:first').find('img').attr('title');
			$(this)
			.wrap('<div class="jGallery-container"></div>')
			.before('<div class="jGallery-image"><img src=""/><div class="item-title">'+ iTitle +'</div></div>');
			$(this).parent('li').addClass('selected');
			
			$(this).find('a').click(function(e){
				$this.find('li').removeClass('selected');
				$(this).parents('li').eq(0).addClass('selected');
				
				var jImg = $(this).parents('.jGallery-container').find('.jGallery-image img');
				var jTitle = $(this).parents('.jGallery-container').find('.item-title');
				
				var newImg = new Image();
				$('.jGallery-image').addClass('loading');

				newImg.onload = function(){
					$('.jGallery-image').removeClass('loading');
					var dims = resizeImg(newImg);

					var genImg = $('<img>')
						.attr('src', newImg.src)
						.attr('height', dims[0])
						.attr('width', dims[1])

					jImg.replaceWith(genImg);
					genImg.hide().fadeIn(function(){
						jTitle.css({width: dims[1] - 48 + 'px'}).slideDown('slow');
					});

					return;
				}
				newImg.src = $(this).attr('href');
				jImg.hide();
				jTitle.hide();
				jTitle.html($(this).find('img').attr('title'));

			
				
				e.preventDefault();
			});
			$(this).find('a:first').click();
			
		});
		
     };
})(jQuery);
// end jGallery extension________________________________________________________________*/

// start jLightbox extesion ________________________________________________________________*/
(function($){
		
    $.fn.jLightbox = function(options){
	
	
		var defaults = {};
		
		var options = $.extend(defaults, options);
        
		$('body').append('<div id="jLightbox-overlay-bg"></div>');
		$('body').append('\
			<div id="jLightbox">\
				<a id="jLightbox-close" href="#"><img src="/images/core/close_btn.png" /></a>\
				<div id="jLightbox-inner">\
					<div id="jLightbox-content">\
					</div>\
				</div>\
			</div>\
		');
		
		$('#jLightbox-overlay-bg').hide();
		$('#jLightbox').hide();
		
		return this.each(function(){
			
			// this is a hack need to fix if resused should use .hide() instead of css
			$($(this).attr('href')).css({
				position:'absolute',
				left:'-9999px'
			});
			
			$(this).click(function(e){
				
				var showDiv = $($(this).attr('href'));
				var jLightbox = $('#jLightbox');
				
				// add data to lightbox div
				
				/*
				jQuery.data(document, 'jLightbox', {
						showDiv: showDiv
					}
				);
				*/
				$('.playerElement').hide();
				
				$('#jLightbox-content').append(showDiv);
				
				// position relative is temporary hack need to fix if reused
				showDiv.find('.playerElement').show();
				showDiv.css({position:'relative', left: '0px'}).show()
				
				$('#jLightbox-overlay-bg').fadeIn(function(){
					jLightbox.fadeIn();
					
				});
				
				e.preventDefault();
				
			});
			
			function closeLightbox(){
				
				var showDiv = $('#jLightbox-content div:first');
				
				$('#jLightbox').fadeOut(function(){
					showDiv.hide();
					$("body").append(showDiv);
					$('#jLightbox-content').empty();
					$('#jLightbox-overlay-bg').fadeOut();
					$('.playerElement').show();
					//jQuery.data(document, "jLightbox", null);
				});
			}
			
			$('#jLightbox-close').click(closeLightbox);
			$('#jLightbox-overlay-bg').click(closeLightbox);
			$(window).keydown(function(e){
				if(e.keyCode == 27){
					closeLightbox();
				}
			})
			
		});
		
     };
})(jQuery);
// end jLightbox extension________________________________________________________________*/

// start preloadImages extension________________________________________________________________*/

(function($){
	$.preloadImages = function()
	{
	  for(var i = 0; i<arguments.length; i++)
	  {
	    $("<img>").attr("src", arguments[i]);
	  }
	}
})(jQuery);
// end preloadImages extension________________________________________________________________*/

(function($){
	
	$(window).load(function(){
		$('#people .carousel').show();
		$('#people .carousel ul').jcarousel({
			visible: 7,
			scroll: 7
		});
	});
	
	// page load animation
	/*
	$(window).load(function(){
		
		var height = parseInt($('#intro').height());
		var top = parseInt($('#intro').css('top'));
		$('#loading').fadeOut(function(){
			$('#all-container').hide().css({height:"936px"}).fadeIn(function(){
				$('#intro-inner').hide();
				$('#intro').css({
					visibility: 'visible',
					height:'0px',
					top: height + top - 13  + "px"
				})
				.animate({
					height: height,
					top: top
				}, 1000, function(){
					$('#intro-inner').fadeIn();
				});
			})
		})
	});
	*/
	
	
	$(document).ready(function(){
		//main nav and view list mouseover
		$("#main-nav a, #view-list").imgSwap();
		
		// FEATURED VIDEO BEHAVIOR
		$('#fea-vids a').click(function(e){
			$('#featured-video-player').load($(this).attr('href'));
			$('#fea-vids li').removeClass('selected');
			$(this).parents('li').addClass('selected');
			e.preventDefault();
		});
		
		//VIDEO LIGHTBOX BEHAVIOR
		
		$('#video-list a').click(function(e){
			$('#video-list li').removeClass('selected');
			$(this).parents('li').addClass('selected');
			$('#video-container').load($(this).attr('href'));
			e.preventDefault();
		})
		
		//IMAGE LIGHTBOX BEHAVIOR
		
		//initialize
		$('#galleries li:first').addClass('selected');
		$(window).load(function(){
			$('#gallery-container').load($('#galleries li a:first').attr('href'), function(){
				$('#gallery-thumbs').jGallery();
			});
		})
		
		$('#galleries a').click(function(e){
			$('#gallery-container').load($(this).attr('href'), function(){
				$('#gallery-thumbs').jGallery();
			});
			$('#galleries li').removeClass('selected');
			$(this).parents('li').addClass('selected');
			e.preventDefault();
		})
		
		//ARTICLE LIGHTBOX
		
		//initialize
		$(window).load(function(){
			$('#article-container').load($('#article-list a:first').attr('href'));
		});
		$('#article-list li:first').addClass('selected');
		
		// featured-article-nav
		
		$('#featured-article-nav a').click(function(e){
			var rel = $(this).attr('rel');
			var cId = rel.substring(rel.lastIndexOf('-') + 1);
			$('#article-container').empty().addClass('loading');
			$('#article-container').load('/static/article/' + cId, function(){
				$('#article-container').removeClass('loading');
			});
			$('#article-list li').removeClass('selected');
			$('#article-link-'+ cId).addClass('selected');
			e.preventDefault();
		}).imgSwap();
		
		$('#article-list a').click(function(e){
			$('#article-container').empty().addClass('loading');
			$('#article-container').load($(this).attr('href'), function(){
				$('#article-container').removeClass('loading');
			});
			$('#article-list li').removeClass('selected');
			$(this).parents('li').addClass('selected');
			e.preventDefault();
		})
		
		//PEOPLE LIGHTBOX FUNCTIONALITY
		$('.carousel li:first').addClass('selected');
		$('#person').load($('.carousel li:first a').attr('href'));
		
		/*
		$('#gallery .carousel ul').jcarousel({
			visible: 7,
			scroll: 7
		});
		*/

		$('.lightbox').jLightbox();
		
		// handle ajax page load and selected states
		$('#people .carousel li a').click(function(e){
			$('#person').load($(this).attr('href'), function(){
				// duplicate code abstract at some point
				$('.jGallery-image').addClass('loading');
				var img = $("#person .jGallery-image img");
				/*
				var jTitle = $('#person .jGallery-container').find('.item-title');
				jTitle.hide()
				*/
				img.hide();
				var newImg = new Image(); 
				newImg.src = img.attr('src');
				newImg.onload = function(){
					$('.jGallery-image').removeClass('loading');
					var dims = resizeImg(newImg);

					var genImg = $('<img>')
						.attr('src', newImg.src)
						.attr('height', dims[0])
						.attr('width', dims[1]);

					img.replaceWith(genImg);
					genImg.hide().fadeIn(function(){
						//jTitle.slideDown('slow').css({width: dims[1] - 48 + 'px'});
					});
					return;
				}
			});
			$('.carousel li').removeClass('selected');
			$(this).parents('li').addClass('selected');
			e.preventDefault();
		});
		
	});
	
	
	// preload images
	$.preloadImages(
					/*
					"/images/core/logo.png", 
					"/images/core/loading_anim.gif",
					"/images/core/bg.jpg", 
					"/images/core/screen.png", 
					"/images/core/loading_bg.gif",
					*/
					"/images/core/vid_btn_on.jpg",
					"/images/core/photo_btn_on.jpg",
					"/images/core/contest_btn_on.jpg",
					"/images/core/article_1_off.png",
					"/images/core/article_2_off.png",
					"/images/core/article_3_off.png");
})(jQuery);
