$(document).ready(function(){

	//  Carousel
	if ( $("#Carousel").length>0 ) {
		$("#Carousel").carousel({
			displayItems : 1,
			btnNext		 : '#Carousel .carouselNext',
			btnPrev		 : '#Carousel .carouselPrevious', 
			listMargin	 : 0
		})
	}

	
	
	// slider 
	if ( $("#slider").length>0 ) {
		$("#slider").slider();
	}
	
	// auto adjust sub-menu nav
	if ( $(".sub-menu").length>0 ) {
		$(".sub-menu").each(function(){
			var l = $(this).width();			

			if ( $(this).parent().width() + 40 > l ) {
				l = $(this).parent().width() + 40;
			}
			
			$(this).children('li:first').css('border-top', '10px solid #fff');
			$(this).children('li:last').css('border-bottom', '5px solid #fff');
			
			$(this).css({
				display: 'block',
				opacity: 0
			});						
			
			$(this).children('li').each(function(){
				if ( $(this).width()>l ) {
					l = $(this).width();
				}
			});
			$(this).css({
				display: 'none',
				opacity: 1				
			});
			
			$(this).children('li').width(l);
			
			// center sub navigation
			if ( $(this).parent().width()+40 < l+10 ){
				var lf = '-' + parseInt( ((l+10) - ($(this).parent().width()+40))/2 ) + 'px';
				$(this).css({
					marginLeft: lf
				});
			}

			
		});
	}
	
	// sub navigation
	$(".main-nav").hover(
		function(){
			var $sub = $(this).children('.sub-menu');
			var $length = $sub.children('li').length;
			$sub.show();
			$sub
				.css('height', 0)
				.stop()
				.show()
				.animate({ height: ($length*31)+($length+10) }, 300 );
		},
		function(){
			var $sub = $(this).children('.sub-menu');
			$sub
				.stop()
				.animate({ height: 0 }, 300, function(){ $(this).hide(); } );
		}
	);
	
	
	// Featured Project
	if ( $(".portfolio-thumb").length>0 ) {
		var ctr = 0;
		$(".portfolio-thumb").each(function(i) {
											
			if ( i%4==0 ) {
				ctr++;
			}
			
			$(this).addClass('portfolio-thumb-col-'+ (i%4));
			$(this).addClass('portfolio-thumb-row-'+ctr);
				
		});
		
		$("#project-thumb").height(ctr*230);
		
		ctr = 0;
		$(".thumb-link").each(function(i) {
											
			if ( i%4==0 ) {
				ctr++;
			}
			
			$(this).addClass('portfolio-thumb-col-'+ (i%4));
			$(this).addClass('portfolio-thumb-row-'+ctr);
				
		});
		
		$(".thumb-link").css('opacity', 0.01);
		
	}


	// Grid Box Hover
	$(".thumb-link").hover(
		function() {
			var idx = $(this).attr('id').split('-')[2];
			var off_left = 0;
			var off_top = 0;
			
			// box width and position
			if ( $(this).hasClass('portfolio-thumb-col-2') || $(this).hasClass('portfolio-thumb-col-3') ) {
				off_left = parseInt($(this).css('left')) - 230;
			} else {
				off_left = parseInt($(this).css('left'));
			}
			
			if ( $(this).hasClass('portfolio-thumb-row-1')==false ) {
				off_top = parseInt($(this).css('top')) - 230;
			}
			
			$("#portfolio-thumb-"+idx)
				.css({
					 height: 455,
					 top: off_top
				})
				.stop()
				.animate({
					left: off_left,
					width: '455px'
				}, 600 )
				.css('z-index', 100);				


			// image position
			if ( $(this).hasClass('portfolio-thumb-row-1')==false ) {
				$("#portfolio-thumb-"+idx+" .imgWrap").stop().animate({
						bottom: 0,
						height: '330px',
						width: '455px'
					}, 600 );
			} else {
				$("#portfolio-thumb-"+idx+" .imgWrap").stop().animate({
						height: '330px',
						width: '455px'
					}, 600 );
				
			}
			
			$("#portfolio-thumb-"+idx+" img").stop().animate({
					left: 0,
					top: 0
				}, 600 );			


			// description box
			$("#portfolio-thumb-"+idx+" .desc")
				.stop()
				.animate({
					height: '126px', 
					width: '455px'
				}, 600);


			// other image opacity
			$("#portfolio-thumb-"+idx+" img").css('opacity', 1);
			$(".portfolio-thumb img").not("#portfolio-thumb-"+idx+" img")
				.stop()
				.animate({
					opacity: 0.3
				}, 600);			
			
		},
		function() {
			var idx = $(this).attr('id').split('-')[2];
			var off_left = off_left = parseInt($(this).css('left'));
			var off_top = 0;
			
			// box width and position
			if ( $(this).hasClass('portfolio-thumb-row-1')==false ) {
				off_top = parseInt($(this).css('top'));
			}			
			
			$("#portfolio-thumb-"+idx)
				.stop()
				.animate({
					left: off_left,
					width: '225px'
				}, 600, function(){
					$(this).css({
						height: 225, 
						top: off_top
					});
					
				} )
				.css('z-index', 1);
			
			
			// image position
			$("#portfolio-thumb-"+idx+" .imgWrap")				
				.stop()
				.animate({
					height: '225px',
					width: '225px'
				}, 600 );
				
			$("#portfolio-thumb-"+idx+" img")
				.stop()
				.animate({
					left: '-120px',
					top: '-85px'
				}, 600, function(){
					$(".portfolio-thumb img")
						.stop()
						.animate({opacity: 1}, 600);							
			} );			

			// description box
			$("#portfolio-thumb-"+idx+" .desc")
				.stop()
				.animate({
					height: 0, 
					width: '226px'
				}, 600);
				
		}
	);
	
	// project tab
	$(".tabs a").click(function(){
		var rel = $(this).attr('href').split('#')[1];
		
		$(".tabs li").removeClass('current_page_item');
		$(this).parent().addClass('current_page_item');
		
		if (rel=='all') {
			$(".tabItems").slideDown('fast');
		} else {
			$(".tabItems").each(function(){
				if ( $(this).hasClass(rel) ) {
					$(this).slideDown('fast');		
				} else {
					$(this).slideUp('fast');
				}
			});
				
		}
		return false;
	});
	
	$(".tabItems img").css({
		height: '42px', 
		width: '65px'
	});
	
	/*
	$("#interior-right img").css({
		height: '109px', 
		width: '170px'
	});
	*/
	
	// project secondary drop down
	if ( $("#title").html()=='Project Gallery' || $("#title").html()=='Portfolio' ) {
		var ul = $(".current_page_ancestor ul").clone();
		ul.removeClass();
		$("#content-wrap").append('<div id="ddNav"><div class="ddNavLeft"><div class="ddNavRight"><a href="#" class="ddNavLink">View Another Straub Portfolio</a></div></div></div>');
		$("#ddNav .ddNavRight").append(ul);
		$("#ddNav ul li:even").addClass('alt');
	}
	
	$(".ddNavLink").live('click', function() {
		return false;		
	});
	
	$("#ddNav").live('mouseover', function(){		
		$("#ddNav ul").show();
	});	
	$("#ddNav").live('mouseout', function(){		
		$("#ddNav ul").hide();
	});	
	
	
	// Featured Project testimonials
	/*
	$('#project-testimonials').totemticker({
		speed       :   600,
		interval    :   6000,
		max_items   :   2,
		row_height	:	'160px',
		mousestop   :   true										   
	});
	*/
	
	// ie fix
	if ( $.browser.mozilla ) {
		$("#nav .sub-menu").css('top', '93px');
		$("#content").css('top', '93px');
	}
	
	
	// adjust gallery ul width
	if ($('.gallery li').length>0) {
		var gal_li_w = $('.gallery li:first').width();
		var li_clone_first = $('.gallery li:first').clone();
		var li_clone_last = $('.gallery li:last').clone();
		
		li_clone_first.addClass('fakelast');
		li_clone_last.addClass('fakefirst');
		
		$('.gallery ul').prepend(li_clone_last);
		$('.gallery ul').append(li_clone_first);
		
		$('.gallery ul').width( gal_li_w * ($('.gallery ul').children().length+1) );
		
		// set initial view and active state
		$(".gallery").scrollTo($('.gallery li:eq(1)'), 0);		
		$('.galleryPagination a').eq(0).addClass('active');
		$('.gallery li').eq(1).addClass('active');

		// move arrow keys		
		$('.arrowLeft').css('left', ($('.gallery li.active').width()) + 29 + 'px' );
		$('.arrowRight').css('left', ( 2 * $('.gallery li.active').width())  - 78 + 'px' );

	}
	
	
	// 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');
			clearTimeout(x);
			x = setTimeout("autoMoveGallery();",6000);
		}
	);
	
	
	// moving gallery image via pagination
	$('.galleryPagination a').click(function(){
		moveGallery($(this).index()+1);		
		$('.gallery li.active .description').hide();
		clearTimeout(x);
		return false;
	});
	
	$('.arrowLeft').click(function(){
		var index = $('.gallery li.active').index() - 1;
		moveGallery(index);		
		$('.arrowRight').show();
		$('.gallery li.active .description').show();
		return false;							   
	});

	$('.arrowRight').click(function(){
		var index = $('.gallery li.active').index() + 1;
		moveGallery(index);
		$('.arrowLeft').show();
		$('.gallery li.active .description').show();
		return false;							   
	});

	
	moveGallery = function(index) {
		
		$(".gallery").scrollTo($('.gallery li:eq('+index+')'), 600);
		
		if ( $('.gallery li:eq('+index+')').hasClass('fakefirst') ) {
			$(".gallery").scrollTo($('.gallery li.fakelast').prev(), 0);
			index = $('.gallery li.fakelast').prev().index();
		}
		
		if ( $('.gallery li:eq('+index+')').hasClass('fakelast') ) {
			$(".gallery").scrollTo($('.gallery li:eq(1)'), 0);
			index = 1;
		}
			
		
		// set active state for navigation
		$('.galleryPagination a.active').removeClass('active');
		$('.galleryPagination a').eq(index-1).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;
			moveGallery(index);	
			clearTimeout(x);
			x = setTimeout("autoMoveGallery();",6000);
			
		}
	}
	
	if ($(".gallery").length>0) {
		var img_count = $('.gallery li').length;
		var x = setTimeout("autoMoveGallery();",6000);		
	}	
	
	// fix active nav state when a project details is shown
	var currentUrl 	= document.location.toString();
	var anchors		= currentUrl.split('/');	
	if (anchors[3]=='projects') {
		$(".current_page_parent").removeClass('current_page_parent');	
		$("li.main-nav:contains('Portfolio')").addClass('current_page_parent');
	}
	
});
