/* Author: 

*/

  document.createElement('header');
  document.createElement('nav');
  document.createElement('section');
  document.createElement('article');
  document.createElement('aside');
  document.createElement('footer');


var EASING = 'easeOutExpo';


function switch_main_image(src) {
	var img = $('#banner img');
	img.animate({opacity:0},200,function() {
		
		img.load(function() {
			img.animate({opacity:1},200);
		});
		img.attr('src',src);
	});


}


$(document).ready(function(){
	
	if ($('html').hasClass('ie6'))
	 return;
	
	
	var $back = $('#back');
	var right = $back.css('right');
	
	
	var $image_sliders = $('#image_sliders');
	var image_sliders_right = $image_sliders.css('right');
	
	
	
	$back.css('right',-300);
	$image_sliders.css('right',-300);
	
	
	$('nav#Main_navbar  a[title="Home"]').hide();

	$('#thumbs a').click(function() {
		switch_main_image($(this).attr('href'));
		return false;
	
	});
		
	
	
	
	
	$('#banner').cycle({slideResize:   0,timeout:5000,speed:500});
//	$('#banner a, #banner img').css('width','100%');



	
	$('.clickable').hover(function() {
		$(this).find('a').css('color','#00ACEF');
	},function() {
		$(this).find('a').css('color','#444');
	})





	var single_project, project_width;
	
	$('.slider_contents').each(function() {
		
		var t = $(this);
		var projects = t.children();
//		if (!projects.length);
//			return;
		single_project = projects.first();
		if (!single_project.length)
			return;
		
		single_project.css('display','block');
		
		project_width = single_project.width() + parseInt(single_project.css('margin-left').replace('px','')) + parseInt(single_project.css('margin-right').replace('px','')) + parseInt(single_project.css('padding-right').replace('px','')) + parseInt(single_project.css('padding-left').replace('px',''));
		var total_width = project_width * projects.size();
	//	t.find('.slider_contents').project_width = project_width;
	//	t.find('.slider_contents').total_width = project_width;
		t.width(total_width);
	});
	
	
	
	
	
	
	var SLIDE_TIME = 600;
	
	var contents = $('.slider_contents');
	contents.css('position','relative').height('-=35').css('overflow','hidden');
	
	contents.find('.project').css('opacity',1).hover(function() {
		$(this).find('img').stop().animate({opacity:0.8});
	},function() {
		$(this).find('img').stop().animate({opacity:1});
	}).find('img').css('background','#000');
	
	contents.parent().css('overflow-x','hidden');
	contents.parent().parent().prepend('<a href="#" class="slider_left"></a><a href="#" class="slider_right"></a>');
	
	$('.slider_right').click(function() {
		
	
		var $t = $(this);
		if ($t.hasClass('standalone'))
			return false;
			
			
		var contents = $t.parent().find('.slider_contents');
	
		var left = contents.position().left;
		

		
		if (left >  0-(parseInt(contents.width())-parseInt(project_width*2)))
			contents.animate({left:'-='+(project_width*2)},SLIDE_TIME,function() {
				var left = contents.position().left;
				
				if (left <= 0-(parseInt(contents.width())-parseInt(project_width*2)))
					$t.addClass('disabled');
				else
					$t.removeClass('disabled');
				
				if (left >= 0) 
					$t.parent().find('.slider_left').addClass('disabled');
				else 
					$t.parent().find('.slider_left').removeClass('disabled');
			})
		
		return false;
	});
	$('.slider_left').click(function() {
		var $t = $(this);
		
		if ($t.hasClass('standalone'))
			return false;
		
		var contents = $t.parent().find('.slider_contents');
		var left = contents.position().left;
		
		
		if (left < 0)
			contents.animate({left:'+='+(project_width*2)},SLIDE_TIME,function() {
				var left = contents.position().left;
			
				if (left >= 0) 
					$t.addClass('disabled');
				else 
					$t.removeClass('disabled');
					
				if (left <= 0-(parseInt(contents.width())-parseInt(project_width*2)))
					$t.parent().find('.slider_right').addClass('disabled');
				else
					$t.parent().find('.slider_right').removeClass('disabled');
			})
		
		
		return false;
	});
	
	
	
	$('.slider_contents').each(function() {
		var $t = $(this);
	
		
	
		var par = $t.parent().parent();
		
		var left = $t.position().left;

		if (left >= 0)
			par.find('.slider_left').addClass('disabled');
		else 
			par.find('.slider_left').removeClass('disabled');
			
		if (left <= 0-(parseInt($t.width())-parseInt(project_width*2)))
			par.find('.slider_right').parent().find('.slider_right').addClass('disabled');
		else
			par.find('.slider_right').removeClass('disabled');
			
		var moving = false;
		
		$t.touchwipe({
   			wipeRight: function() {
	   			if (moving)
	   			 	return;
	   			moving = true;
	   			 
	   			var left = $t.position().left;
	   		
			 	if (left < 0) {
			 	    par.find('.slider_left').removeClass('disabled');
			 		$t.animate({left:'+='+project_width},SLIDE_TIME,function() {moving = false});
				}
				else
					moving = false;
   			},
    		wipeLeft: function() {
    			if (moving)
    				return;
	   			 moving = true;
	   			 
	   			 var left = $t.position().left;
    			if (left > 0-(parseInt(contents.width())-parseInt(project_width*2))) {
   			 		$t.animate({left:'-='+project_width},SLIDE_TIME,function() {moving = false});
					par.find('.slider_left').removeClass('disabled');

   				}else
   					moving = false;
    		}
		});
		
		$t.bind('mousewheel', function(event, delta, deltaX, deltaY) {
   			 if (moving)
   			 	return;
   			 moving = true;
   			 
   			 var left = $t.position().left;
   			 
   			 
   			 
   			 
   			 if (deltaX > 0.5) {
   			 	if (left < 0) {
   			 	    par.find('.slider_left').removeClass('disabled');
   			 		$t.animate({left:'+='+(project_width*2)},SLIDE_TIME,function() {moving = false});
   				}
   				else
   					moving = false;
   			 } else if (deltaX < -0.5) {
   			 	if (left > 0-(parseInt(contents.width())-parseInt(project_width*2))) {
   			 		$t.animate({left:'-='+(project_width*2)},SLIDE_TIME,function() {moving = false});
					par.find('.slider_left').removeClass('disabled');

   				}else
   					moving = false;
   			 }
   			 else 
   			 	moving = false;
   			 
		});	
	});
	
	$('.projects').hide();
	
	var pathname = window.location.pathname;
	pathname = pathname.split('/');
	
	
	if (pathname[pathname.length - 2 ] == "projects" || pathname[pathname.length - 1 ] == "projects") {
		if (pathname[pathname.length -1 ] == "projects") {
			show_projects('featured');
		}
		else {
			show_projects(pathname[pathname.length -1 ]);
		}
	}
	
	
	$('#project_nav a').click(function() {
		var $t = $(this);
	
		show_projects($t.attr('rel'));
		return false;
	});
   
   function show_projects(key) {
   	$('.projects.active').stop().slideUp(1000,EASING);
   	$('.projects[rel="'+key+'"]').stop().slideDown(1000,EASING).addClass('active');
   	$('#project_nav li.active').removeClass('active');
   	$('#project_nav a[rel="'+key+'"]').parent().addClass('active');
   }



	var hide_show = $('<a href="#" id="hide_btn"></a>');
	
	
	
	hide_show.html('hide text');
	var content = $('#content');
//	hide_show.css('opacity',0);

	var h = content.height();
	var w = content.width();
	var inner = content.find('.inner');
	clicked = false;
	hide_show.css('opacity',0).animate({opacity:1},1000,EASING);
	hide_show.click(function() {
		if (clicked) {
			hide_show.html('hide text');
			content.animate({width:w},1000,EASING,function() {
				content.css('overflow','auto');
				inner.css('width','auto');
			});	
		
		}
		else {
			hide_show.html('show text');
			inner.width(inner.width());
			content.css('overflow','hidden')
				.animate({width:0},1000,EASING);
		}
		
		clicked = !clicked;
		return false;
	},function() {
		
	});
	
	
	var overflow = $('#main').css('overflow-x');
	$('#main').css('overflow-x','hidden');
	$('footer').css('bottom','-200px').show().animate({bottom:0},2000,EASING);


	$('header').css('top','-200px').show().animate({top:0},2000,EASING,function() {
		$back.animate({right:right},2000,EASING);
		
		$image_sliders.animate({right:image_sliders_right},2000,EASING)
		
	
		content.css('right','-900px').show().animate({right:0},2000,EASING,function() {
			hide_show.css('opacity',0);
			content.append(hide_show);
			hide_show.animate({opacity:1},200,EASING);
			$('#main').css('overflow-x',overflow);
	
		});
	});
	
	

	window.onbeforeunload = function() {
		return;
		$('header').slideUp(100);
		content.show().animate({right:-900},100,EASING);
		$('footer').animate({bottom:-100},2000,EASING);
	}
	
	
	
	var main_images = $('#thumbs a');
	
	if (main_images.length <= 1) {
		$('#image_sliders').hide();
	}
	
	var current_main_image = -1;
	$('#image_sliders a').click(function() {
		var $t = $(this);
		if ($t.hasClass('disabled'))
			return false;
		
		if ($t.hasClass('slider_right')) {
			current_main_image++;
			var img = $(main_images[current_main_image]);
			var src = img.attr('href');
			switch_main_image(src);
			
			
			
		}
		else {
			current_main_image--;
			var img = $(main_images[current_main_image]);
			var src = img.attr('href');
			switch_main_image(src);
		}
		
		if (main_images[current_main_image+1]) {
			$('#image_sliders .slider_right').removeClass('disabled');
		}
		else {
			$('#image_sliders .slider_right').addClass('disabled');
		}
		
		if (current_main_image > 0) {
			$('#image_sliders .slider_left').removeClass('disabled');
		}
		else {
			$('#image_sliders .slider_left').addClass('disabled');
		}
		
		
		return false;
	});
	
});




















// usage: log('inside coolFunc', this, arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
function log(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  arguments.callee = arguments.callee.caller;  
  if(this.console) console.log( Array.prototype.slice.call(arguments) );
};
// make it safe to use console.log always
(function(b){function c(){}for(var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();)b[a]=b[a]||c})(window.console=window.console||{});



