var clicked_obj;

$(document).ready(init);
$(window).resize(onResize);

function init(){
	initParallax();
	initLocalScrolling();
	initHashChange();
	initSubnavs();
	initRollovers();
	initCallouts();
}

function onResize(){
	try{
		Processing.getInstanceById('sketch').onResize();
	}catch(e){}
}

function initParallax(){
	$('#wrapper').scrollParallax({
		'speed': -0.2,
		'axis' : 'x'
	});
	
	$('#bg').scrollParallax({
		'speed': -0.25,
		'axis' : 'x'
	});
}

function initLocalScrolling(){
	$('#mainnav').localScroll({
		onBefore: beforeScroll,
		onAfter: afterScroll,
		axis: 'x',
		duration: 1200,
		hash:true,
		easing:'easeOutQuad'
	});
	
	
	if(window.location.hash) {
			var hash = window.location.hash;
			 $("#mainnav li" ).each(function (i) {
						$(this).removeClass("active");												
			});
			$('a[href$="'+hash+'"]').parent().addClass("active");
			moveCanvas($(hash));
		}
	
}

function initHashChange(){
	 $(window).hashchange( function(){
    var hash = location.hash;
   	if (hash == ''){
   		hash = "#home";
   	}
    
  	 $("#mainnav li" ).each(function (i) {
					$(this).removeClass("active");												
		});
		$('a[href$="'+hash+'"]').parent().addClass("active");
		try{
			Processing.getInstanceById('sketch').reset();
		}catch(e){};
		moveCanvas($(hash));
		if($('#slider').css('display')=="block"){
			sliderDown();
		}
		$.colorbox.close();
  });
}

function initCallouts(){
	$("#featured").click(function() {
		sliderUp($('#home'),'pages/featured.html');
	});	
}

function initSubnavs(){
	$(".subnav li" ).each(function (i) {
					//$(this).removeClass("active");
			$(this).click(function(){
				//console.log(this.id)
				if(this.id!="form"){
					sliderUp($(this).parents('section'), 'pages/'+this.id+'.html')
				}else{
					sliderUp($(this).parents('section'), 'pages/'+this.id+'.cfm')
				}
				try{
					Processing.getInstanceById('sketch').updateControlPoints();
				}catch(e){}
			});									
		});
		
		$('#close').click(function(){
			sliderDown();
			
		})
	
}

function initRollovers(){
	

	 $(".subnav li" ).each(function (i) {
        $(this).hover( function () {
						if ( $(this).data("clicked") ) { return; }
            var img_url = $(this).find("img:last").attr("src");
            $(this).find("img:last").attr("src", img_url.substring( 0, img_url.indexOf( ".jpg") ) + "_over.jpg" );
        },
        function () {
						if ( $(this).data("clicked") ) { return; }
            var img_url = $(this).find("img:last").attr("src");
            $(this).find("img:last").attr("src", img_url.substring( 0, img_url.indexOf( "_over.jpg") ) + ".jpg" );
				});
				
				 $(this).click( function () {
					 if ( $(this).data("clicked") ) { return; }
					if ( clicked_obj ) {
        		$(clicked_obj).removeData("clicked").mouseout();
    			}
    				clicked_obj = this;
    				$(this).data("clicked", true);										
					});

    });
    
    $(".rollover" ).each(function (i) {
			  $(this).hover( function () {
            var img_url = $(this).attr("src");
            $(this).attr("src", img_url.substring( 0, img_url.indexOf( ".jpg") ) + "_over.jpg" );
        },
        function () {
            var img_url = $(this).attr("src");
            $(this).attr("src", img_url.substring( 0, img_url.indexOf( "_over.jpg") ) + ".jpg" );
				});
		 });
		

}


function beforeScroll(e, elm, selm){
	
	/*
	 $("#mainnav li" ).each(function (i) {
						$(this).removeClass("active");												
			});
		
		$(e.target).parent().addClass("active");
		//if(elm.id != 'home'){
			sliderDown();
			//$.colorbox.close();
		//}*/
	
		
}

function afterScroll(e, elm, selm){

	if(e.id == 'home'){
		//sliderUp($('#home'),'pages/featured.html');
		//$('#close').hide();
	}else{
		//$('#close').show();
	}

	try{
		Processing.getInstanceById('sketch').reset();
		//moveCanvas(e);
	}catch(e){};

	
}


function moveCanvas(where){
	//$('canvas').fadeOut(0)
	$('canvas').css('left', ($(where).offset().left)+650);
	//$('canvas').fadeIn(1500);
}

function sliderUp(where, what){
	if(where)
		$('#slider').css('left', $(where).offset().left);
		
	if(what)
		$('#pagesframe').attr('src', what);
		
	$('#slider').slideDown('fast', function() {
    // Animation complete.
  });
}

function sliderDown(){
	$('#slider').slideUp('fast', function() {
    // Animation complete.
   // $('#pagesframe').attr('src', 'about:blank');
   $('#pagesframe').contents().empty();
    
    $(".subnav li" ).each(function (i) {
			
        		 if ( $(this).data("clicked") ){
        				$(this).removeData("clicked").mouseout();
        				clicked_obj = null;
        			}
    			
				
			});
	});
}


