var cardWidth = 230;
var cardHeight = 130;
var padding = 20;
var detailsWidth = 740;
var map_initialized = false;

var welcomeShowed = false;

/* check for IE 6 and 7 */

if($.browser.msie && $.browser.version < 8) {
	location.href = "sorry.html";
}


/* address handling */	
if ($.address.value() == '') {
	$.address.value("work");
}
$.address.init(function(event) {
    $('.internal').address();
}).change(function(event) {
	handleURL(event.pathNames);	
}).externalChange(function(event) {
    
}).history(true);


	
$(document).ready(function() {

	$("article a").click(function(){
		var p = $(this).parent().attr("id");
		showProject(p);
	});	
	
	
	$("#welcome").delay(6000).slideUp("slow" );
	
		
	$("#project-nav-left").live("mouseenter", function() {
		$(this).animate({left: -100, opacity: 1}, 300);	
	});
	
	$("#project-nav-left").live("mouseleave", function() {
		$(this).animate({left: -180, opacity: 0.8}, 300);	
	});
	
	
	$("#project-nav-right").live("mouseenter", function() {
		$(this).animate({right: -100, opacity: 1}, 300);	
	});
	
	$("#project-nav-right").live("mouseleave", function() {
		$(this).animate({right: -180, opacity: 0.8}, 300);	
	});
	
	
	$("#down-arrow").click(function(){
		//$('#about').snowfall('clear');
		$('#about').animate({ scrollTop: $("#bg-1").height()}, 1400);
	
	});
		
	$("#btnLogo").click(function(){
		setButtonsOff();
		$("#btnWork").addClass("nav-on");

	});
	
	$(window).bind("resize", function(){
		centerProjects();
	});
	
		
	centerProjects();
});

$(window).load(function() {
      centerProjects();
});

function centerProjects() {
	if(!isMobile() && !isIpad()) {
		var pWidth = $(document).width();	
		var pHeight = $(document).height();	
	} else {
		var pWidth = $(window).width();	
		var pHeight = $(window).height();	
	}
	
	
	
	var numAcross;
	if(isIpad()) {
		 numAcross = Math.floor((pWidth) / (cardWidth + padding));
	} else {
		 numAcross = Math.floor((pWidth - 20) / (cardWidth + padding));
	}
	var myMargin = pWidth - (numAcross * (cardWidth + padding));
		
	$("#projects").css("marginLeft", myMargin * 0.5);
	if(!isMobile() && !isIpad()) {
		
		$("#site-top").css("paddingLeft", myMargin * 0.5 + padding * 0.5);
		$("#site-top").css("paddingRight", myMargin * 0.5 + padding * 0.5);
		
		$("#site-top").width(numAcross * (cardWidth + padding) - padding);
		if(!isIpad()) {
			$("#details").css("marginLeft", pWidth * 0.5 - detailsWidth * 0.5);
		}
	}
	
	if(isIpad()) {
		$("#details").css("marginLeft", pWidth * 0.5 - 360);
	}
	
	
	$("#project-detail").height($(window).height() - 80);
			
	if(!isMobile() && !isIpad()) {
		$("#projects-loader").css("left", (pWidth * 0.5) - ($("#projects-loader").width() * 0.5));
	} else {
		$("#projects-loader").css("left", (pWidth * 0.5) - ($("#projects-loader").width() * 0.5) - 10);
	}
	$("#projects-loader").css("top", (pHeight * 0.5) - ($("#projects-loader").height() * 0.5));
		
	$("#project-nav-left").css("top", (pHeight * 0.5) - (cardHeight * 0.5) - 100);
	$("#project-nav-right").css("top", (pHeight * 0.5) - (cardHeight * 0.5) - 100);
	
	/* all the about trickery */
	if(!isMobile() && !isIpad()) {
		$(".about-bg").css("width", pWidth); 
		var myHeight = pHeight;
		if(pHeight < 730) {
			myHeight = 730;
		}
			
		$(".about-bg").css("height", myHeight); 
		$("#bg-1").css("height", myHeight);
		$("#bg-2").css("height", myHeight);
		$("#bg-3").css("height", myHeight);
		var ph = myHeight - $("#about-about").height();
		$("#about-about").css("marginTop", ph);
		
		var titlePos = (ph * 0.5) - $("#about-title").height() * 0.5;
		if(titlePos < 80)
			titlePos = 80;
			
		$("#about-title").css("top", titlePos);		
		$("#about-page2").css("top", myHeight + (myHeight * 0.5) - ($("#about-page2").height() * 0.5));		
		$("#about-page3").css("top", myHeight * 2 + (myHeight * 0.5) - 300);
	}	
	
	if(isIpad()) {
		$("#about").css("height", pHeight); 
	}
}

function handleURL(path) {
	disableLeftRight();
	switch(path[0]) {
		case "contact":
			$("#welcome").hide();
			showContact();
		break;
		case "about":
			$("#welcome").hide();
			if(path.length == 1) {
				showAbout();
			} else {
				showAbout(path[1]);
			}
		break;
		case "work":
			if(path.length == 1) {
				showAllWork();
			} else {
				$("#welcome").hide();
				enableLeftRight();
				showProject(path[1]);
			}
		break;
		case "tags":
			$("#welcome").hide();
			if(path.length == 1) {
				showAllWork();
			} else {
				getProjectsCategory(path[1]);
			}
		break;
		default:
			showAllWork();
			if(!isMobile() && !isIpad()) {
				$("#welcome").show();
			} else {
				$("#welcome").hide();
			}
		break;
	}
}

function setButtonsOff() {
	$("#btnWork").removeClass("nav-on");
	$("#btnAbout").removeClass("nav-on");
	$("#btnContact").removeClass("nav-on");
}

function showProject(slug) {
	$("#projects").fadeOut("fast");
	//$("#projects").animate({"marginRight": $(window).width()}, 1000);
	
	getProjectDetail(slug);
}
	
function showAllWork() {
	setButtonsOff();
	$.address.title("Lolz LLC");
	if(!isMobile() && !isIpad()) { $('#about').snowfall('clear'); }
	getProjects();
	$("#btnWork").addClass("nav-on");
	$("#project-detail").fadeOut("fast");
	$("#contact").fadeOut("fast");
	$("#about").fadeOut("fast");
	$("#projects").delay(300).fadeIn("fast");
}

function getProjects() {
	$("#project-detail").fadeOut("fast");
	$("#projects").fadeOut("fast", function(){
		$("#projects-loader").show();
		$.get('/cms/', function(data) {
			$("#projects").html(data);
			$("#projects").fadeIn("fast");
			$("#projects-loader").hide();
			
			if(!isMobile() && !isIpad()) {
				$("#projects article").hover(function(){
					$(this).find(".black-overlay").fadeIn("fast");	
				},function(){
					$(this).find(".black-overlay").fadeOut("fast");	
				});
			}
		});
	});
}

function getProjectsCategory(slug) {
	$("#project-detail").fadeOut("fast");
	$("#projects").fadeOut("fast", function(){
		$("#projects-loader").show();
		$.get('/cms/category/' + slug + '/', function(data) {
			$("#projects").html(data);
			$.address.title("Work / "+slug+" - Lolz LLC");
			$("#projects").fadeIn("fast");
			$("#projects-loader").hide();
			
			if(!isMobile() && !isIpad()) {
				$("#projects article").hover(function(){
					$(this).find(".black-overlay").fadeIn("fast");	
				},function(){
					$(this).find(".black-overlay").fadeOut("fast");	
				});
			}
		});
	});
}

function getProjectDetail(slug) {
	$("#project-detail").fadeOut("fast", function() {
		$("#projects-loader").fadeIn("slow", function(){
			loadTheProject(slug);
		});			
	});
}


function loadTheProject(slug){
	$("#projects-loader").show();
	$("#project-detail").css("background-image", "none");

	$.get('/cms/' + slug + '/', function(data) {
		$("#project-detail").html(data);
		$.address.title("Work / " + $("#project-detail h1").html() + " - Lolz LLC");
		centerProjects();
		$("#project-nav-left").css("left", -cardWidth);
		$("#project-nav-right").css("right", -cardWidth);
		
		if($("#slider").html() != "" || $("#slider").html() != null) {
			if(!isMobile() && !isIpad()) {
				$('#slider').nivoSlider({pauseTime:5000, controlNav:false, directionNav: true, prevText: '', nextText: '', effect: 'random'});
			} else {
				$('#slider').nivoSlider({pauseTime:5000, controlNav:false, directionNav: false, prevText: '', nextText: '', effect: 'slideInRight'});
			}
		}		
		
		if(!isMobile()) {
			bgcssimg = $("#details .background-css-image").html();
			if(bgcssimg != "" && bgcssimg != null) {
				$("#project-detail").css("background-image", bgcssimg);
			}
			
			bgcsspos = $("#details .background-css-position").html();
			if(bgcsspos != "" && bgcsspos != null) {
				$("#project-detail").css("background-position", bgcsspos);
			}
			
			bgcssrepeat = $("#details .background-css-repeat").html();
			if(bgcssrepeat != "" && bgcssrepeat != null) {
				$("#project-detail").css("background-repeat", bgcssrepeat);
			}
		}		
		
		$("#project-detail").delay(500).fadeIn("fast", function() {
			$("#project-nav-left").animate({left: -180, opacity: 0.8}, 300);	
			$("#project-nav-right").animate({right: -180, opacity: 0.8}, 300);	
		});
		$("#projects-loader").hide();

	});
}

function enableLeftRight() {
	var myhref;
	$(document).bind("keydown", function(e){
	    if (e.keyCode == 37) { 
	       myhref = $("#project-nav-left a").attr("href");
	    }
	    
	     if (e.keyCode == 39) { 
	       myhref = $("#project-nav-right a").attr("href");
	    }
	    
	    if(myhref != "" && myhref != null)
			location.href = myhref;
		return false;
	});
}

function disableLeftRight() {
	$(document).unbind("keydown");
}
	
function showAbout(p) {
	$.address.title("About - Lolz LLC");
	p = typeof(p) != 'undefined' ? p : null;
	
	setButtonsOff();
	$("#btnAbout").addClass("nav-on");
		
	$("#projects").fadeOut("fast");
	$("#project-detail").fadeOut("fast");
	$("#contact").fadeOut("fast");
	centerProjects();
	$("#about").delay(300).fadeIn("fast", function() {
		centerProjects();
		if(!isMobile() && !isIpad()) { 
			$(this).snowfall({flakeCount : 100, maxSpeed : 10}); 
			switch(p) {
				case "page2":
					$('#about').animate({ scrollTop: $("#bg-1").height()}, 1400);
				break;
			}
		
		}
	});	
}
	
function showContact() {
	$.address.title("Contact - Lolz LLC");
	setButtonsOff();
	$("#btnContact").addClass("nav-on");
	if(!isMobile() && !isIpad()) { $('#about').snowfall('clear'); }
	
	
		
	$("#projects").fadeOut("fast");
	$("#project-detail").fadeOut("fast");	
	$("#about").fadeOut("fast");
	$("#contact").delay(300).fadeIn("fast", function(){
		if(!map_initialized)
			initialize_map();
	
	});
}

function initialize_map() {
    map_initialized = true;
    var latlng = new google.maps.LatLng(40.7261, -73.9956);
    var myMarker = "images/map-marker.png";
    var myOptions = {
      zoom: 16,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.TERRAIN
    };
  	var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	var lolzMarker = new google.maps.Marker({
		position: latlng,
		map: map,
		icon: myMarker
	});
	
}

function isMobile(){
    return (
        (navigator.platform.indexOf("iPhone") != -1) ||
        (navigator.platform.indexOf("Android") != -1)
    );
}

function isIpad() {
	return (navigator.platform.indexOf("iPad") != -1);
}
