$(document).ready(function(){
	
	// Cufon text replacement
	Cufon.replace('#sidebar h2, .pageTitle');
	
	// preload images
	$.preloadCssImages();
	
	// nav pull down 
	$("#nav li").hover(
		function(){
			var subnav = $(this).children().eq(1);			
			if (subnav.length>0) {		
				$(this).children().eq(0).addClass('active');
				subnav.after('<div id="shadow"></div>');
				var h=subnav.height(), w=subnav.width();

				subnav
					.show()
					.css('opacity',0);
					
				$("#shadow")
					.css({
						height: 0,
						opacity: 0, 
						width: w
					})
					.animate({
						height: h,
						opacity: 0.8
					}, 'fast');
									
				subnav.animate({
						opacity: 1
					}, 300);
			}
		},
		function() {
			var subnav = $(this).children().eq(1);			
			if (subnav.length>0) {
				subnav.hide();	
				$("#shadow").remove();			
				$(this).children().eq(0).removeClass('active');
			}
		}
	);
	
	// ie8 nav fix
	if ($.browser.msie && $.browser.version>7) {
		$("#nav li li a").hover(
			function(){
				$(this).addClass('hover');
			},
			function(){
				$(this).removeClass('hover');
			}
		);
	}
	
	
	// adjust gallery ul width
	if ($('.gallery').length>0) {
		var gal_li_w = $('.gallery li:first').width();
		$('.gallery ul').width(gal_li_w*$('.gallery ul').children().length);
	}
	
	
	// gallery hover
	$('.gallery').hover(
		function(){
			var index = $('.gallery li.active').index();

			// arrows
			if ( index > 0 ) $('.arrowLeft').show();
			if ( index < ($('.gallery li').length-1) ) $('.arrowRight').show();
			
			// description
			$('.gallery li.active .description').slideDown('fast');
			clearTimeout(x);
		},
		function(){
			$('.arrowLeft').hide();		
			$('.arrowRight').hide();
			$('.gallery li.active .description').slideUp('fast');
			x = setTimeout("autoMoveGallery();",5000);
		}
	);
	
	
	// moving gallery image via pagination
	$('.galleryPagination a').click(function(){
		moveGallery($(this).index());		
		$('.gallery li.active .description').hide();
		clearTimeout(x);
		return false;
	});
	
	$('.arrowLeft').click(function(){
		var index = $('.gallery li.active').index() - 1;
		moveGallery(index);
		if (index==0) $('.arrowLeft').hide();
		$('.arrowRight').show();
		$('.gallery li.active .description').show();
		return false;							   
	});

	$('.arrowRight').click(function(){
		var index = $('.gallery li.active').index() + 1;
		moveGallery(index);
		if (index>=$('.gallery li').length-1) $('.arrowRight').hide();		
		$('.arrowLeft').show();
		$('.gallery li.active .description').show();
		return false;							   
	});

	
	moveGallery = function(index, remove) {
		
		if (remove) {
			$(".gallery").scrollTo($('.gallery li:eq('+index+')'), 1, {onAfter:function(){ 
				$(".gallery li").css('visibility','visible');
				$(".gallery li:last").remove();
			}});	
		} else {
			$(".gallery").scrollTo($('.gallery li:eq('+index+')'), 300);	
		}
		
		// set active state for navigation
		$('.galleryPagination a.active').removeClass('active');
		if (index==img_count) {
			$('.galleryPagination a').eq(0).addClass('active');
		} else {
			$('.galleryPagination a').eq(index).addClass('active');
		}
		
		
		// set active state for picture
		$('.gallery li.active').removeClass('active');
		$('.gallery li').eq(index).addClass('active');
		
		// move arrow keys		
		$('.arrowLeft').css('left', (index*$('.gallery li.active').width()) + 29 + 'px' );
		$('.arrowRight').css('left', ((index+1)*$('.gallery li.active').width())  - 78 + 'px' );		
	}
	
	
	// auto move carousel
	autoMoveGallery = function(){
		if ( $(".gallery").hasClass('static')==false ) {
			var index = $('.gallery li.active').index() + 1;
			
			if ( index==img_count ) {				
				$(".gallery ul").append($(".gallery li:first").clone());
				var gal_li_w = $('.gallery li:first').width();
				$('.gallery ul').width(gal_li_w*$('.gallery ul').children().length);				
			} 
			if (index>img_count) {
				$(".gallery li").css('visibility','hidden');
				moveGallery(0, true);	
				autoMoveGallery();
			} else {
				moveGallery(index);	
				x = setTimeout("autoMoveGallery();",5000);
			}
			
			
		}
	}
	if ($(".gallery").length>0) {
		var img_count = $('.gallery li').length;
		var x = setTimeout("autoMoveGallery();",5000);		
	}
	
	
	// thumbs effects
	if ( $(".thumbs").length>0 ) {
		$(".thumbs")
			.css({
				background: '#fff',
				opacity: .6
			})
			.hover(
				function(){
					$(this).stop().animate({opacity: 1},'fast');	
				},
				function(){
					$(this).stop().animate({opacity: .6},'fast');	
				}
			);				
	}
	
	
	// thumb show
	$("#studentLife.studentPortfolioList .content a.thumbs").hover(
		function(){
			$(this).next().slideDown('fast');
		},
		function(){
			$(this).next().slideUp('fast');
		}
	);
	
	// scrollbars
	if ($('.scroll-pane').length>0) {
		$('.scroll-pane').jScrollPane({scrollbarWidth:20, scrollbarMargin:10});
		$('.jScrollPaneTrack').height($('.scroll-pane').parent().height());
	}
	
	
	// hover tooltip 
	$(".post .category a").hover(
		function(){
			if ($(this).children().hasClass('hide')) {
				if ($(this).next().hasClass('tooltip')==false) {			
					$(this).after('<div class="tooltip">'+$(this).children().html()+'</div>');
				}
				$(this).next()
					.css({
						left: $(this).offset().left - $(".post .category").offset().left - 30 + 'px',
						opacity: 0
					})
					.animate({
						opacity: 1,
						top: -50
					}, 'fast');
			}
		},
		function(){
			if ($(this).children().hasClass('hide')) {
				$(this).next()
					.stop()
					.animate({
						opacity: 0,
						top: 0
					}, 'fast');
			}
			
		}
	);
	
	
	/**
	 * Modal Functionalities 
	 **/
	positionModal = function(){
		 var wWidth = window.innerWidth;
		 var wHeight = window.innerHeight;
	
		 if (wWidth==undefined) {
			 wWidth = document.documentElement.clientWidth;
			 wHeight = document.documentElement.clientHeight;
		 }
	
		 var boxLeft = parseInt((wWidth / 2) - ( $("#modal").width() / 2 ));
		 var boxTop = parseInt((wHeight / 2) - ( $("#modal").height() / 2 ));
	
		 // position modal
		 $("#modal").css('margin', boxTop + 'px 0 0 ' + boxLeft + 'px');		
	}

	loadModal = function(url){
		 $("body").append('<div id="modalBackground"></div>');
		 $("#modalBackground").css("opacity", 0).fadeTo("slow", "0.6");
		 $("body").append('<div id="modalWrapper"></div>');
		 $("#modalWrapper").append('<div id="modal"></div>');	

		$.ajax({
			 url: url,
			 cache: false, 
			 success: function(html) {		
				 $("#modal").html(html);	
				if ($('#modal .gallery').length>0) {
					var gal_li_w = $('#modal .gallery li:first').width()+20;
					 $('#modal .gallery ul').width(gal_li_w*$('#modal .gallery ul').children().length)
				}	
				$("#modal").append('<a href="#" class="closeModal"><span class="hide">Close</span></a>');
				 positionModal();
				 
				 // ie fix for select fields that still shows on top of modal windows
				 if ($.browser.msie) {
					 $("select").hide();
					 $("#modal select").show();
				 }
				 
			 }, 
			 error: function() {
				alert("Error loading page");
				closeModal();
			 }
		 });
		
		return false;
	}
	
	
	closeModal = function(){
		 $("#modalWrapper").remove();
		 $("#modalBackground").remove();
		 
		 // ie fix for select fields that still shows on top of modal windows
		 if ($.browser.msie) {
			 $("select").show();
		 }
	}
	

	// close modal trigger
	$(".closeModal").live('click', function(){
		 closeModal();
		 x = setTimeout("autoMoveGallery();",5000);
	});	
		
		
		
	// open modal
	$('.modal').live('click', function(){
		var url = $(this).attr('href');
		loadModal(url);
		return false;
	});
	
	
	moveModalGallery = function(index) {
		$("#modal .gallery").scrollTo($('#modal .gallery li:eq('+index+')'), 300);
		
		// set active state
		$('#modal .gallery li.active').removeClass('active');
		$('#modal .gallery li').eq(index).addClass('active');
	}


	$('#modal .arrowLeft').live('click', function(){
		var index = $('#modal .gallery li.active').index() - 1;
		moveModalGallery(index);
		return false;							   
	});

	$('#modal .arrowRight').live('click', function(){
		var index = $('#modal .gallery li.active').index() + 1;
		moveModalGallery(index);
		return false;							   
	});	
	
	
	
	if ($.browser.msie && $(".studentPortfolioList").length>0) {
		var l = $(".roundedList li").length;
		$(".roundedList li").each(function(i){
			$(this).css('z-index', l-i);	
		});
	}		
});