$(document).ready(function(){
	
	// Cufon text replacement
	Cufon.replace('#sidebar h2, .pageTitle');
	
	// preload images
	$.preloadCssImages();

	var modal_img_count = 0;
	var modal_x = '';		

	// 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() + 30;
		$('.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-top', boxTop + '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 .modal_gallery').length>0) {
					var gal_li_w = $('#modal .modal_gallery li:first').width()+30;
					 $('#modal .modal_gallery ul').width(gal_li_w*$('#modal .modal_gallery ul').children().length);
					 
					modal_img_count = $('.modal_gallery li').length;
					modal_x = setTimeout("modalAutoMoveGallery();",5000);							 
				}	
				
				if ($('#modal .scroll-pane').length>0) {
					$('#modal .scroll-pane').jScrollPane({scrollbarWidth:20, scrollbarMargin:10});
					$('#modal .jScrollPaneTrack').height($('#modal .scroll-pane').parent().height());
				}
				
				if ($("#modal .tableSort").length>0) {
					 $("#modal .tableSort").tablesorter(); 	
				}
				
				$("#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();
		 return false;
	});	
		
		
		
	// open modal
	$('.modal').live('click', function(){
		var url = $(this).attr('href');
		loadModal(url);
		return false;
	});
	
	


	/* Modal Gallery */	
	$('.modal_galleryPagination a').live('click', function(){
		modalMoveGallery($(this).index());		
		$('.modal_gallery li.active .description').hide();
		clearTimeout(modal_x);
		return false;
	});
	
	
	modalMoveGallery = function(index, remove) {
		
		if (remove) {
			$(".modal_gallery").scrollTo($('.modal_gallery li:eq('+index+')'), 1, {onAfter:function(){ 
				$(".modal_gallery li").css('visibility','visible');
				$(".modal_gallery li:last").remove();
			}});	
		} else {
			$(".modal_gallery").scrollTo($('.modal_gallery li:eq('+index+')'), 300);	
		}
		
		// set active state for navigation
		$('.modal_galleryPagination a.active').removeClass('active');
		if (index==modal_img_count) {
			$('.modal_galleryPagination a').eq(0).addClass('active');
		} else {
			$('.modal_galleryPagination a').eq(index).addClass('active');
		}
		
		
		
		// set active state for picture
		$('.modal_gallery li.active').removeClass('active');
		$('.modal_gallery li').eq(index).addClass('active');
		
		// move arrow keys		
		$('.modal_gallery .modal_arrowLeft').css('left', (index*$('.modal_gallery li.active').width()) + 29 + 'px' );
		$('.modal_gallery .modal_arrowRight').css('left', ((index+1)*$('.modal_gallery li.active').width())  - 78 + 'px' );		
	}
	
	
	// auto move carousel
	modalAutoMoveGallery = function(){
		if ( $(".modal_gallery").hasClass('static')==false ) {
			var index = $('.modal_gallery li.active').index() + 1;
			
			if ( index==modal_img_count ) {				
				$(".modal_gallery ul").append($(".modal_gallery li:first").clone());
				var gal_li_w = $('.modal_gallery li:first').width();
				$('.modal_gallery ul').width(gal_li_w*$('.modal_gallery ul').children().length);				
			} 
			if (index>modal_img_count) {
				$(".modal_gallery li").css('visibility','hidden');
				modalMoveGallery(0, true);	
				modalAutoMoveGallery();
			} else {
				modalMoveGallery(index);	
				modal_x = setTimeout("modalAutoMoveGallery();",5000);
			}
			
			
		}
	}


	$('.modal_gallery').live('mouseover mouseout', function(event){
		
		if (event.type=='mouseover') {
			var index = $('.modal_gallery li.active').index();

			// arrows
			if ( index > 0 ) $('.modal_gallery .modal_arrowLeft').show();
			if ( index < ($('.modal_gallery li').length-1) ) $('.modal_gallery .modal_arrowRight').show();
			
			
			// description
			$('.modal_gallery li.active .description').css('height', 0).stop().animate({height: '40px'}, 'fast');
			clearTimeout(modal_x);
			
		} else {
			$('.modal_gallery .modal_arrowLeft').hide();		
			$('.modal_gallery .modal_arrowRight').hide();
			$('.modal_gallery li.active .description').stop().animate({height: '0'}, 'fast', function(){ $(this).hide(); });
			modal_x = setTimeout("modalAutoMoveGallery();",5000);
		}
		
	});	
	
	
	$('.modal_gallery .modal_arrowLeft').live('click', function(){
		var index = $('#modal .modal_gallery li.active').index() - 1;
		modalMoveGallery(index);
		return false;							   
	});

	$('.modal_gallery .modal_arrowRight').live('click', function(){
		var index = $('#modal .modal_gallery li.active').index() + 1;
		modalMoveGallery(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);	
		});
	}		
	
	
	// subscriber form
	if ( $(".inField").length>0 ) {
		$(".inField").each(function(){
			$(this).addClass('inputHint');							
		});
	}
	
	$(".inField").focus(function(){
		var attrTitle = $(this).attr('title');
		var attrValue = $(this).val();
		
		if (attrTitle==attrValue) {
			$(this).removeClass('inputHint').val('');
		}
	});

	$(".inField").blur(function(){
		var attrTitle = $(this).attr('title');
		var attrValue = $(this).val();
		
		if (attrValue=='') {
			$(this).addClass('inputHint').val(attrTitle);
		}
	});
	
	if ($.browser.msie && $.browser.version<8) {
		$("#subscriberForm .fullText, #subscriberForm .halfText, #subscriberForm .quart2Text, #subscriberForm .quart1Text").css({
			top: '-1px',
			left: '2px',
			padding: '8px 0 7px'
		});
	}

});
