/*
VARIABLES
-----------------------------------------------------------------------------------------*/
var moved = 			1;
var total_images = 		0;
var window_width = 		$(window).width();
var window_height = 	$(window).height();

/*
EVENTS
-----------------------------------------------------------------------------------------*/
$(window).resize(function() {
	if(window_width > 650){
		scroll_to("#home");
		//set_page();
		$(".navigation a").removeClass("active");
		$(".navigation a.first").addClass("active");
		$("#left,#right").scrollify({speed:250});
	}
});
$(document).ready(function(){
	$(".goto").click(function(e){
		e.preventDefault();
		$("#navigation a").removeClass("active");
		$(this).addClass("active");
		scroll_to( $(this).attr("href"));
	});
	
	if(document.location.hash){
		var hash = document.location.hash;
		$("#navigation a").removeClass("active");
		$("#navigation a").each(function(){
			var href = $(this).attr("href");										 
			if(href ==  hash){
				$(this).addClass("active");
			}
		});
	}
	
	$(".list a").click(function(e){
		e.preventDefault();
		
		var ml = ((window_width/2)-300)-10
		
		$("#portfolio #logos").stop().animate(
			{
				"margin-left":ml+"px",
				"margin-top":"30px"
			},
			{
				easing:"easeInOutElastic",
				duration:1000,
				step:function(){
				},
				complete:function(){
					
				}
			}		
		);
	});

	
	/*CLICK EVENTS-----------------------------------------------------------------------------------------*/
	$("#earthquake").click(function(){
		earthquake();
	});
	$("#nightlife").click(function(){
		nightlife();
	});
	$("#shootitup").click(function(){
		gunrange();
	});
	
	$("#movethings").click(function(){
		move_around();
		switch(moved){
			case 2:
				open_message("Did you see it?","message-green");
				break;
			case 4:
				open_message("Look Closer!","message-green");
				break;
			case 6:
				open_message("It's the tags and pins in the background.","message-green");
				break;
			case 7:
				open_message("They move. :)","message-green");
				break;
			case 10:
				open_message("This is fun for you, huh?","message-red");
				break;
			case 15:
				open_message("OK. I think that's enough.","message-red");
				break;
			case 20:
				open_message("It's getting old, bud!","message-red");
				break;
			case 50:
				open_message("Grrrrrrrrr!","message-black");
				break;
		}
		moved++;
		return false;
	});
	
	/*
	$("#left,#right").stop().animate(
		{top: "30px"},
		{easing:"swing",duration:1000,complete:function()
			{
				
			}
		}		
	);
	*/
		
	setTimeout(function(){
		var load_hash = document.location.hash;
		scroll_to(load_hash);
		//set_page();
	},200);
	
	/*KUDOS SUBMIT-----------------------------------------------------------------------------------------*/
	$("#form_kudos").submit(function(){
		if($("#text_kudos").val().trim().length > 0){
			if(confirm("Are you sure you want to TXT me?")){
				$.ajax({
					type:"post",
					data:{text_kudos:$("#text_kudos").val()},
					url:"/mail.php",
					success: function(msg){
						$("#form_kudos").remove();
						$("#kudos .message").css({"display":"table-cell"});
					}
				});
			}
		}else{
			open_message("Enter Something!","message-red")
		}
		return false;
	});

	$(".bubble").altify();
	
	$("#left,#right").scrollify({speed:250});
	
	var total_pages = 		$(".page:visible").length;
	var total_height = 		(window_height*total_pages);
	var left_range = 		[5,window_width];
	
	/*TAGS-----------------------------------------------------------------------------------------*/
	var tag_classes = ["one","two","three","four","five","six","seven","eight","nine","ten","eleven"];
	$.each(tag_classes, function(key, value) { 
		var top = rn(5,total_height);
		var left = rn(left_range[0],left_range[1]);
		var new_tag = $("<div>").addClass("tag").addClass("move_xy").addClass(value).css({top:top+"px",left:left+"px"});
		$("#tags").append(new_tag);
	});
	
	/*TACKS-----------------------------------------------------------------------------------------*/
	var tack_classes = ["one","two","three","four","one","two","three","four","one","two","three","four","one","two","three","four","one","two","three","four","one","two","three","four","one","two","three","four","one","two","three","four"];
	$.each(tack_classes, function(key, value) { 
		var top = rn(5,total_height);
		var left = rn(left_range[0],left_range[1]);
		var new_tag = $("<div>").addClass("tack").addClass("move_xy").addClass(value).css({top:top+"px",left:left+"px"});
		$("#tacks").append(new_tag);
	});
	
	$("#pages center").fadeIn("slow",function(){});
	$(".delete").remove();
	
	$('#preload img').load(function(){
		total_images++;
		if(total_images == $('#preload img').length){	
		}
	});
	
	$(".far").each(function(i,o){
		var rel = Number($(this).attr("rel"));
		$(this).css({"left":"4000px","top":"0px"})
	});
});


/*
FUNCTIONS
-----------------------------------------------------------------------------------------*/
//function set_page(){
	//$(".page").css({"width":$(window).width(),"height":$(window).height()})
//}
function scroll_to(href){
	href = (href != "") ? href : "#home";
	
	$("body").stop().animate(
		{scrollTop: $(href).offset().top},
		{easing:"swing",duration:2000,complete:function()
			{
				//$("#left,#right").scrollify({speed:250});
				if(href != "#home"){
					document.location.hash = href;
				}else{
					document.location.hash = "";
				}
			}
		}		
	);
}

/*MESSAGE-----------------------------------------------------------------------------------------*/
function open_message(str,class_name){
	var id = "message_"+rn(11111,99999);
	var message = $("<div>").attr("id",id).addClass("top_message").append($("<div>").addClass("inner"));
	$("body").append(message);
	
	$("#"+id+" .inner").html(str);
	
	var message_width = $("#"+id).width();
	$("#"+id).css("margin-left",(-1*(message_width/2))+"px")
	$("#"+id).animate({height:'50px'},500,function(){}).addClass(class_name);
	var delay = (arguments[2] == undefined) ? 3000 : arguments[2];
	to = setTimeout(function(){close_message(id)},delay);
};
function close_message(id){
	$("#"+id).animate({height:'0px'},500,function(){});
};

function isScrolledIntoView(elem){
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
}

function gunrange(){
	scroll_to("#gunrange");
}
function earthquake(){
	$('.earthquake').effect("shake", { times:60, distance:5 }, 8);
}
function nightlife(){
	scroll_to("#nightclub");
	var lights = $("<div>").attr("id","flashing_lights");
	var beer = $("<div>").attr("class","beer");
	
	$("body").append(lights);
	
	var pos = $("#nightclub").position();
	lights.css("top",pos.top+"px")
	
	var s = 1500;
	var c = {b:'black',r:'red',g:'green',u:'blue',y:'yellow',t:'transparent'};
	
	lights.animate({backgroundColor: c.b},s,
		function(){
			open_message("Through the door...","message-black")
			lights.animate({backgroundColor: c.r},s,
				function(){
					open_message("Grab a drink...","message-black")
					lights.animate({backgroundColor: c.g},s,
						function(){
							open_message("sip... sip... sip...","message-black")
							lights.animate({backgroundColor: c.u},s,
								function(){
									lights.animate({backgroundColor: c.y},s,
										function(){
											open_message("Time to get serious...","message-black")
											lights.animate({backgroundColor: c.b},s,
												function(){
													lights.animate({opacity: 0},s,
														function(){
															lights.remove();
															scroll_to("#home");
														}
													);
												}
											);
										}
									);
								}
							);
						}
					);
				}
			);
		}
	);
}

function move_x(obj,range){
	obj.each(function(i){
		$(this).animate({ 
		left: rn((-1*range),range)+"px"
		}, 1500 );
	});
}
function move_y(obj,range){
	obj.each(function(i){
		$(this).animate({ 
			top: rn((-1*range),range)+"px"
		}, 1500 );
	});
}
function move_xy(obj,range){
	
	obj.each(function(i){
		
		var left = Number($(this).css("left").replace("px",""));
		var top = Number($(this).css("top").replace("px",""));
		var left_rn = rn((left-range),(left+range));
		var top_rn = rn((top-range),(top+range));

		$(this).stop().animate({ 
			left: left_rn+"px",
			top: top_rn+"px"
		}, 1500 );
		
	});
}
function move_around(){
	move_xy($(".move_xy"),100);
}
function Reset(){
	$('.move_x').animate({ 
		left: "0px",
		top: "0px"
	}, 1500 );
	$('.move_y').animate({ 
		left: "0px",
		top: "0px"
	}, 1500 );
	$('.move_xy').animate({ 
		left: "0px",
		top: "0px"
	}, 1500 );
}
function rn(min, max) {
	var offset = min;
	var range = (max - min) + 1;

	var randomNumber = Math.floor( Math.random() * range) + offset;
	return randomNumber;
}

