//Cufon.replace('a, h1', { fontFamily: 'AlphaHeadlinePro-Regular' });
var moving_bg = 0;
var global_clicked = 0;

$(window).load(function(){
	$("#main_loader").remove();
	$(window).hashchange( function(){
		
	if (global_clicked == 0){
			
	    var hash = location.hash;
	    
	    if (hash == "#/" || !hash){
	    	
	    	
		var height = $(window).height();
		btm = height - 14;
		$("#background").animate({
			top: btm,
		}, 500, function(){
			$("#handle").css("cursor", "n-resize");
		});		
		
		window.location.hash = '';

		$('#info').hide();
		$("#promenu").hide();
		$("#info").hide(); 
		$("#works").hide(); 
		$("#submenu").hide();	
		$(".menu").animate({
			marginTop:"214px"
		}, 500, function(){
			$('#viewport').fadeIn(300);
		});				

	    	
	    	
	    	
	    	
	    	
	    }else if (hash == "#/projects"){
	    
			var height = $(window).height();
			btm = height - 14;
			$("#background").css("top", btm+"px");
			$("#handle").css("cursor", "n-resize");    	
	    	
			$("#submenu a").removeClass("active_sub");
			$("#promenu").hide();	
			$("#info").hide();
			$("#works").hide();
			$('#viewport').hide();
			$(".menu").css("margin-top", "32px");
			$("#submenu").show();
			$.ajax({
					url: "projects.html",
					cache: false,
					success: function(html){
						$("#works").html(html);	
						$("#works").show();
					}
			});
	
	   	
	    }else if (hash.indexOf('#/projects') + 1){
	 
	 		var project_name = hash.replace('#/projects/',"");
			$("#submenu a").removeClass("active_sub");
			$("a[rel='"+project_name+"']").addClass("active_sub");
	
			var height = $(window).height();
			btm = height - 14;
			$("#background").css("top", btm+"px");
			$("#handle").css("cursor", "n-resize");
	
			
			var proj_name = project_name
			var name = $("#submenu a[rel='"+project_name+"']").html();
			$('#info').hide();
			$("#promenu").hide();
			$("#works").hide();
			$('#viewport').hide();
			$(".menu").css("margin-top", "32px");
			$("#submenu").show();
	
			$.post('projects/'+proj_name, { }, function(data) {			
				$('#info h1').html(name);
				//$('#info span').html(data.short);			
				$('#info p').html(data.long);
				$("#works").html(data.images);
				$("#works img").hide();				
				
					scroll(0,0);
					$("#works, #info").fadeIn(300);
					$("#works img:first").addClass("c_img"); 
					$(this).parent().parent().fadeIn(300);
					$(".image").each(function(){				
						var src = $(this).find("img").attr("alt");
						$(this).find("img").attr("src", src).load(function(){
							$(this).fadeIn(500);
						});																
					});
				}, "json");
			
			$("title").html("Makearea :: "+name);

	    }
	    if (hash == "#/contact"){
	
				$("#submenu a").removeClass("active_sub");
				$("#promenu a").removeClass("active_sub");
				$("#promenu").hide();
				
				stat = 0;
	
				
				$("#info").hide();
				//$("#works").html('<div class="image" style="background:transparent;"><img id="loader_contact" src="images/loader.gif"></div>');
				$("#works").show();
				$("#submenu").hide();
				$('#viewport').hide();		
				$("#background").css("top", "0px");	
				$("#handle").css("cursor", "s-resize");
				$(".menu").css("margin-top", "32px");
				$.ajax({
					url: "contact.html", 
					cache: false,
					success: function(html){
						$("#works").html(html);
						$("#works").show();
					}
				});
				$("title").html("Makearea :: Contact");
	    }
	    if (hash == "#/about"){
	    	
				$("#background").css("top", "0px");	
				$("#handle").css("cursor", "s-resize");
				$(".menu").css("margin-top", "32px");
				$("#submenu").hide();
				$('#viewport').hide();					
				$("#promenu a").removeClass("active_sub");
				$("#about_link").addClass("active_sub");
				var proj_name = $(this).attr("rel");
				var href = $(this).attr("href");			
//				$("#works").html('<div class="image" style="background:transparent;"><img id="loader_promenu" src="images/loader.gif"></div>');
				$('#info').hide();
				$.ajax({
					url: "about.html",
					cache: false,
					success: function(html){
						$("#promenu").show();
						$("#works").html(html);
						$("#works").show();
					}
				});
				$("title").html("Makearea :: About");	
	
	    }    
	    if (hash == "#/recognition"){
				$("#background").css("top", "0px");	
				$("#handle").css("cursor", "s-resize");
				$(".menu").css("margin-top", "32px");
				$("#submenu").hide();
				$('#viewport').hide();					
				$("#promenu a").removeClass("active_sub");
				$("#recognition_link").addClass("active_sub");
				var proj_name = $(this).attr("rel");
				var href = $(this).attr("href");			
				//$("#works").html('<div class="image" style="background:transparent;"><img id="loader_promenu" src="images/loader.gif"></div>');
				$('#info').hide();
				$.ajax({
					url: "recognition.html",
					cache: false,
					success: function(html){
						$("#promenu").show();
						$("#works").html(html);
						$("#works").show();
					}
				});
                                $("title").html("Makearea :: Recognition");				
	    }    
    }else{
    	global_clicked = 0;
    }

    
    
    
  });
  $(window).hashchange();	
});











$(document).ready(function(){	
	

	
	$("a").click(function(){
			$("title").html("Makearea");		
	});
	
	//for the main page
	$(".menu").css("margin-top","214px");
	
	var inner = $("#works h1").html();
	var tw = $(window).width();
	if (tw<=1064 && !inner){
		$("#works").addClass("small");
	}else if(tw>1064){
		var need= 371+(tw - 1061)*0.1;
		$("#works").css("left", need+"px");
	}
	
	$(window).load(function(){
	$(".jcarousel-prev,.jcarousel-next").hide();		
	})
	
	$("#viewport").hover(function(){
		$(".jcarousel-prev,.jcarousel-next").show();
	}, function(){
		$(".jcarousel-prev,.jcarousel-next").hide();		
	});



/*
	viewport_width = 0;
	$("#gallery").scrollLeft(0);
	speed = 0;
	var gal_width = $("#gallery").width();
	var zone = gal_width/6;
	x1 = 0;	
	x2 = zone*1;
	x3 = zone*2;
	x4 = zone*3;
	x5 = zone*4;
	x6 = zone*5;
	x7 = zone*6;	
	$("#viewport ul li").each(function(){
		viewport_width += 248;
	});
	css_left=(viewport_width-gal_width)/2;
	$("#viewport").css("width", viewport_width+"px");
	$("#gallery").mousemove(function(e) {
		
		var offp = e.pageX;
		 
		if (offp <= x1) {
			speed = -3;
		}
		if (offp > x1 && offp <= x2) {
			speed = -3;
		}
		if (offp > x2 && offp <= x3) {
			speed = -2;
		}
		if (offp > x3 && offp <= x4) {
			speed = -1;
		}

		if (offp >= x4 && offp < x5) {
			speed = 1;
		}
		if (offp >= x5 && offp < x6) {
			speed = 2;
		}
		if (offp >= x6 && offp < x7) {
			speed = 3;
		}
		if (offp >= x7) {
			speed = 3;
		}
	});
	var thumbs_scroll_interval = false;
	$("#gallery").scrollLeft(css_left);
	$("#gallery").hover(function() {
		if (moving_bg == 0) {
			thumbs_scroll_interval = setInterval(function(){
				var left = $("#gallery").scrollLeft() + speed;
				$("#gallery").scrollLeft(left);
			}, 10);
		}
	}, function(){
		clearInterval(thumbs_scroll_interval);  
	});
	  
*/	
	 
	$(".gall").hover(
            function() {
				if (moving_bg == 0) {
					$(this).find('span').show();
				}
            },
            function() {
				if (moving_bg == 0) {
					$(this).find('span').hide();
				}
            }
	);	 
	$("#background").draggable({
		handle: "#handle",
		axis: "y",
		stop: function(){
			moving_bg = 0;
			var top = $(this).offset().top - $(window).scrollTop();
			var height = $(window).height();			
			$("#handle").css("cursor", "ns-resize");
			diff = height - top;
			if (top<71 || (top>110 && top<271)){
				$(this).animate({
			    	top: '0',
				}, 300, function(){
					$("#handle").css("cursor", "s-resize");
				});	
			}else if(diff<60){
				btm =  height - 14;
				$(this).animate({
			    	top: btm,
				}, 300, function(){
					$("#handle").css("cursor", "n-resize");
				});					
			}
		},
		drag: function(){
			moving_bg = 1;	
			var scr = $(window).scrollTop();
			$(window).scrollTop(scr);			
		}
	});   
	
	$("#projects_link").click(function(){
		global_clicked = 1;
		var hash = window.location.hash;
		if (hash == "#/contact" || hash == "#/profile" ||  hash == "#/about" || hash == "#/recognition") {
		
			var height = $(window).height();
			btm = height - 14;
			$("#background").animate({
				top: btm,
			}, 500, function(){
				$("#handle").css("cursor", "n-resize");
			});
		}	
		
		$("#submenu a").removeClass("active_sub");
		$("#promenu").hide();
		stat = 0;
		query = location.href.split('#');
		if (query[1] == "airport_page"){
		stat = 0;
		}
		if (query[1] == "tokio_page"){
		stat = 1;
		}
		if (query[1] == "summer_page"){
		stat = 2;
		}
		if (query[1] == "winter_page"){
		stat = 3;
		}
		
		window.location.hash = '#/projects';
		$("#info").hide();
		$("#works").hide();

		$('#viewport').fadeOut(300, function() {
			$(".menu").animate({
				marginTop:"32px"
			}, 500, function(){
				$("#submenu").show();
				$.ajax({
						url: "projects.html",
						cache: false,
						success: function(html){
							$("#works").html(html);	
							$("#works").show();
							//$("div.all_projects:first").fadeIn(300);
						}
				});
			});	
  		});
		return false;
	});
	
	$("#contact_link").click(function(){
		global_clicked = 1;
		$("title").html("Makearea :: Contact");	
		var is_shown = $("#contacts_head").html();
		if (is_shown) {
			return false;
		}
		else {
		
			$("#submenu a").removeClass("active_sub");
			$("#promenu a").removeClass("active_sub");
			$("#promenu").hide();
			
			stat = 0;
			query = location.href.split('#');
			
			window.location.hash = '#/contact';
			
			
			$("#info").hide();
			//$("#works").html('<div class="image" style="background:transparent;"><img id="loader_contact" src="images/loader.gif"></div>');
			$("#works").show();
			$("#submenu").hide();
			$('#viewport').fadeOut(300, function(){
			
				$("#background").animate({
					top: '0',
				}, 400, function(){
					$("#handle").css("cursor", "s-resize");
				});
				
				$(".menu").animate({
					marginTop: "32px" 
				}, 500, function(){
					$.ajax({
						url: "contact.html", 
						cache: false,
						success: function(html){
							$("#works").html(html);
							$("#works").show();
						}
					});
				});
			});
			return false;
		}

	});	
	
//Profile

		
	$("#profile_link").click(function(){
		global_clicked = 1;
		$("title").html("Makearea :: About");	
		var is_shown = $("#about_head").html();
/*		if (is_shown) {
			return false;
		}
		else {
*/		
			$("#promenu a").removeClass("active_sub");
			$("#submenu a").removeClass("active_sub");
			
			stat = 0;
			query = location.href.split('#');
			
			window.location.hash = '#/about';
			
			
			$("#info").hide();
//			$("#works").html('<div class="image" style="background:transparent;"><img id="loader_profile" src="images/loader.gif"></div>');
			$("#works").show();
			$("#submenu").hide();	
			$("#about_link").addClass("active_sub");		 
			$('#viewport').fadeOut(300, function(){
			
				$("#background").animate({
					top: '0',
				}, 400, function(){
					$("#handle").css("cursor", "s-resize");
				});
				
				$(".menu").animate({
					marginTop: "32px"
				}, 500, function(){
					$.ajax({ 
						url: "about.html",
						cache: false,
						success: function(html){
							$("#promenu").show();
							$("#works").html(html);
							$("#works").show();
						}
					});
				});
			});
			return false;
//		}
	});
	
	$(".not_active").live("click", function(){
		return false;
	});	
	
	$("#main_menu span a").hover(function(){
		$(this).css("color", "#6f7278");
	},function(){
		$(this).css("color", "#9b9ea5");
	});
	
		
	$("#submenu span a").hover(function(){
		$(this).css("color", "#9b9ea5");
	},function(){
		$(this).css("color", "#6f7278");
	});	
	
	$("#promenu span a").hover(function(){
		$(this).css("color", "#6f7278");
	},function(){
		$(this).css("color", "#9b9ea5");
	});		
	
	
	$("#name_logo").click(function(){
		var hash = window.location.hash;
		if (hash == "#/contact" || hash == "#/profile" ||  hash == "#/about"  || hash == "#/recognition") {
		
			var height = $(window).height();
			btm = height - 14;
			$("#background").animate({
				top: btm,
			}, 500, function(){
				$("#handle").css("cursor", "n-resize");
			});
		}		
		
		window.location.hash = '';
		//clearInterval(thumbs_scroll_interval);
		$('#info').hide();
		$("#promenu").hide();
		$("#info").hide(); 
		$("#works").hide(); 
		$("#submenu").hide();	
		$(".menu").animate({
			marginTop:"214px"
		}, 500, function(){
			$('#viewport').fadeIn(300);
		});				


		
		return false;
	});	
	
	
	$("#submenu a").hover(function(){
		var preview = $(this).attr("rel");
		$("div.all_projects").hide();
		$("#"+preview+"_prev").show();
	}, function(){
		$("div.all_projects").hide();
	});
	
	$("#submenu a").click(function(){
		global_clicked = 1;
		//$("body").scrollTo(0, 500);
		$("#submenu a").removeClass("active_sub");
		$(this).addClass("active_sub");
		
		
		var proj_name = $(this).attr("rel");
		var name = $(this).html();
		$("title").html("Makearea :: "+name);		
		window.location.hash = '#/projects/'+proj_name;
		//$("#works").hide();
			//$("#works").html('<div class="image"><img id="loader" src="images/loader.gif"></div>');
			$('#info').hide();

			$.post('projects/'+proj_name, { }, function(data) {			
				$('#info h1').html(name);
				//$('#info span').html(data.short);			
				$('#info p').html(data.long);
				$("#works").html(data.images);
				$("#works img").hide();				
				
					scroll(0,0);
					$("#works, #info").fadeIn(300);
					$("#works img:first").addClass("c_img"); 
					$(this).parent().parent().fadeIn(300);
					$(".image").each(function(){				
						var src = $(this).find("img").attr("alt");
						$(this).find("img").attr("src", src).load(function(){
							$(this).fadeIn(500);
						});																
					});
			}, "json");
		return false;
	});	
	
	
	$("#promenu a").click(function(){
		global_clicked = 1;
		if (!$(this).hasClass("not_active")) {
			//$("body").scrollTo(0, 500);
			$("#promenu a").removeClass("active_sub");
			$(this).addClass("active_sub");
			
			
			var proj_name = $(this).attr("rel");
			var href = $(this).attr("href");
			
			window.location.hash = '#/' + proj_name;
			//$("#works").hide();
				//$("#works").html('<div class="image" style="background:transparent;"><img id="loader_promenu" src="images/loader.gif"></div>');
				$('#info').hide();
				$.ajax({
					url: href,
					cache: false,
					success: function(html){
						$("#promenu").show();
						$("#works").html(html);
						$("#works").show();
					}
				});
		}
		return false;
	});		
	
	$("#viewport ul li a").click(function(){	
		global_clicked = 1;
		$("#submenu a").removeClass("active_sub");	
		$("#info").hide();
		//$("#works").html('<div class="image"><img id="loader" src="images/loader.gif"></div>');
		//$('#info').hide();			
		var proj_name = $(this).attr("rel");
		var name = $(this).attr("alt");
		window.location.hash = '#/projects/' + proj_name;			
		$('#viewport').fadeOut(300, function() {
			$(".menu").animate({
				marginTop:"32px"
			}, 500, function(){
				$("#submenu a[rel='"+proj_name+"']").addClass("active_sub");
				$("#submenu").show();
					$.post('projects/' + proj_name, {}, function(data){
						$('#info h1').html(name);
						$('#info span').html(data.short);
						$('#info p').html(data.long);
						$("#works").html(data.images);
						scroll(0,0);
							$("#works, #info").fadeIn(300, function(){
								//$("body").scrollTo(0, 500);	
							});
							$("#works img:first").addClass("c_img");
							$(this).parent().parent().fadeIn(300);
							
							$(".image").each(function(){

									var src = $(this).find("img").attr("alt");
									$(this).find("img").attr("src", src).load(function(){
										$(this).fadeIn(800);
									});
										
							});							

					}, "json");				
			});	
  		});		
		return false;
	});	
	
	$("#works img").live("click", function(){
					
		if (!$(this).hasClass("c_img")){
			$("#works img").removeClass("c_img");
			$("#works img").removeClass("p_img");			
			$("#works img").removeClass("n_img");				
			$(this).addClass("c_img");
			$(this).parent().prev().find("img").addClass("p_img");
			$(this).parent().next().find("img").addClass("n_img");
			var curr = $(this);
			$("body").scrollTo(curr, 500, {offset:-112});			
		}else{	
			$("#works img").removeClass("c_img");
			$("#works img").removeClass("p_img");			
			$("#works img").removeClass("n_img");				
			$(this).addClass("p_img");
			$(this).parent().next().find("img").addClass("c_img");
			$(this).parent().next().next().find("img").addClass("n_img");
			var curr = $(this).parent().next().find("img");
			$("body").scrollTo(curr, 500, {offset:-112});			
		}
		
		
		//var scroll = $("#works").scrollTop();
		//alert(scroll);
		//$("body").scrollTop(scroll+300);
	});
		
		
		
	var height = $(window).height();
	$("#background").css("top", (height - 14)+"px");
	
	$(document).keydown(function(event) {
			
			if (event.keyCode == "38" || event.keyCode == "40"){
			var top = $("#background").offset().top - $(window).scrollTop();
			var height = $(window).height();			
			diff = height - top;	
			
				if (event.keyCode == "38"){
					$("#background").animate({
				    	top: '0',
					}, 400, function(){
						$("#handle").css("cursor", "s-resize");
					});	
				}else if(event.keyCode == "40"){
					btm =  height - 14;
					$("#background").animate({
				    	top: btm,
					}, 400, function(){
						$("#handle").css("cursor", "n-resize");
					});					
				}
				
			event.preventDefault();
			}
			if (event.keyCode == "37" || event.keyCode == "39"){

			
				if (event.keyCode == "37"){
					$(".jcarousel-prev").click();
				}else if(event.keyCode == "39"){
					$(".jcarousel-next").click();		
				}
				
			event.preventDefault();
			}

	});
	

	 
	
	
	

	
});


$(window).resize(function(){
	
	/*
	var gal_width = $("#gallery").width();
	var zone = gal_width/6;
	x1 = 0;	
	x2 = zone*1;
	x3 = zone*2;
	x4 = zone*3;
	x5 = zone*4;
	x6 = zone*5;
	x7 = zone*6;
	
	var thumbs_scroll_interval = false;
	*/
	var top = $("#background").offset().top;
	var height = $(window).height();			
	diff = height - top;
	if (top<300){
		$("#background").css("top", "0");
		$("#handle").css("cursor", "s-resize");
	}else if(diff<300){
		btm =  height - 14;
		$("#background").css("top", btm+ "px");
		$("#handle").css("cursor", "n-resize");				
	}		
	var full_width= $(window).width();
	if (full_width >=1150){
		$("#background").css("width", full_width);
	}else{
		$("#background").css("width", "1150px");		
	}

	var tw = $(window).width();
	var inner = $("#works h1").html();	
	if (tw<=1064 && !inner){
		$("#works").addClass("small");
	}else{
		var need= 371+(tw - 1061)*0.1;
		$("#works").css("left", need+"px");
		$("#works").removeClass("small");		
	}
	
});
