$(document).ready(function(){
	if (jQuery.browser.mobile) {
		window.location = "/m"
	}
	
	
	// animate logo
	$("#logo").delay(600).animate({"top": "+=113px", opacity: 1 }, 800);
	
	// scrollspy
	$('.section').each(function(i) {
		var position = $(this).position();
		$(this).scrollspy({
			min: position.top,
			max: position.top + $(this).height() - 1 ,
			onEnter: function(element, position) {
				$("#logo").addClass(element.id);
				$("#nav-" + element.id + " a").addClass("active");
				//window.location.hash = element.id;	
			},
			onLeave: function(element, position) {
				$("#logo").removeClass(element.id);
				$("#nav-" + element.id + " a").removeClass("active");
			}
		});
	});
				
	function carouselCallback(carousel) {
				
		$('#carousel-control a').bind('click', function() {

	        carousel.scroll($.jcarousel.intval($(this).attr("slideindex")));
			$(this).addClass("active").siblings().removeClass("active");
	        return false;

	    });
		
		
	};
 
	function highlight(carousel, obejctli, liindex, listate){
	    $('#carousel-control a:nth-child('+ (liindex) +')').attr("class","active");
	};
	
	// it will remove last selected slide from <a>
	// <a href="#" class="selected">1</a> to <a href="#">1</a>
	function removehighlight(carousel, obejctli, liindex,listate){
	    $('#carousel-control a:nth-child('+ (liindex) +')').removeAttr("class","active");
	};
	
	$('#viewport').jcarousel({
			//auto: 7,
			scroll:1,
	        wrap: 'both',
			initCallback: carouselCallback,
			itemFallbackDimension: 980,
			itemVisibleInCallback:  highlight,
			itemVisibleOutCallback: removehighlight,
			visible: 1,
			vertical: false
	});

	setInterval(function() {
		// message us button
	    $('#message-us').animate({opacity: 0.3}, 800).delay(300).animate({opacity: 1}, 800);
	
	}, 3000);

	// animate footer
	
	// setInterval(function() {
	// 
	// 	// left light
	// 	$('#left-light-beam')
	// 		.delay(0)
	// 		.animate({opacity: 0.05}, 800)
	// 		.animate({opacity: 0.15}, 800)
	// 		.delay(1000)
	// 		.animate({opacity: 0.05}, 800)
	// 		.animate({opacity: 0.15}, 800);
	// 
	// 	// right light
	// 	$('#right-light-beam')
	// 		.delay(3000)
	// 		.animate({opacity: 0.05}, 800)
	// 		.animate({opacity: 0.15}, 100)
	// 		.delay(3000)
	// 		.animate({opacity: 0.05}, 2000)
	// 		.animate({opacity: 0.15}, 800);
	// 
	// }, 10000);
	
	
	//console.log("browser: " + $.browser.safari);
	
	// smooth scroll
	$("#menu a").click(function(event){	
		
		event.preventDefault();
		var hash = $(this).attr("href");
		
		if($.browser.mozilla){
			var currentScroll = $('html,body').scrollTop();
			window.location.hash = hash ;
			$('html,body').scrollTop(currentScroll)
				.animate({ scrollTop: $(hash).offset().top + 5 }, 800);
		} else {
			$('html,body').animate({scrollTop:$(hash).offset().top + 5 }, 800, function() {
			 			window.location.hash = hash ;
						// if($.browser.msie){
						// 		 	$('html,body').animate({scrollTop:$(hash).offset().top + 5 });
						// 		}
			});
		}

		
		// scroll to bottom of page if you click on for hire
		
		// if(hash == "#for-hire"){
		// 	
		// 	$('html,body').animate({scrollTop:$('html,body').height() - $(window).height() }, 800, function() {
		// 			window.location.hash = hash ;
		// 	});
		// 	
		// }else{
			
		// $('html,body').animate({scrollTop:$(this.hash).offset().top}, 800, function() {
		// 		window.location.hash = hash ;
		// });
			
		// }

	});
	

	$('#ajax-overlay').hide();
	var showOverlay = function(event){
		if (event) event.preventDefault();
		
		$('#main-content').fadeOut(1000);
		$('#for-hire').fadeOut(1000);
		$('#ajax-overlay').fadeIn(1000);
		
		// Get the page content
		var href = $(this).attr('href');

		$.get(href, function(data) {
			$("#overlay-content").html(data);

			$('#product-cycle').cycle({ 
			    fx:     'fade', 
			    speed:  300,            			
			    timeout: 3000
			});
		});
		
		javascript:scroll(0,0);
		$('#menu').fadeOut(1000);
		$('#logo').animate({left:'800px'}, 1000);
		$('#main-home-link').removeClass('hidden');
		$('#main-home-link').fadeOut(0);
		$('#main-home-link').fadeIn(1000);
		
	}
	
	$('#main-home-link').click(function(event){
		if (event) event.preventDefault();
		
		$('#menu').fadeIn(1000);
		$('#logo').animate({left:'-11px'}, 1000);
		$('#main-home-link').fadeOut(1000);
		
		// Page content
		$('#main-content').fadeIn(1000);
		$('#for-hire').fadeIn(1000);
		$('#ajax-overlay').fadeOut(1000);
	});
	
	$('.ajax-link').click(showOverlay);

	// /* TEST CODE */
	// 
	// 	$('#main-content').fadeOut(1000);
	// 	$('#for-hire').fadeOut(1000);
	// 	$('#ajax-overlay').fadeIn(1000);
	// 	
	// 	// Get the page content
	// 	
	// 	$.get('/clients/dreamworks', function(data) {
	// 		$("#overlay-content").html(data);
	// 		
	// 		$('#product-cycle').cycle({ 
	// 		    fx:     'fade', 
	// 		    speed:  300,  
	// 		    timeout: 3000
	// 		});
	// 	});
	
	
});

