var querystring = location.search.replace( '?', '' ).split( '&' );
var queryObj = {};
for ( var i=0; i<querystring.length; i++ ) {
	  var name = querystring[i].split('=')[0];
	  var value = querystring[i].split('=')[1];
	  queryObj[name] = value;
}
if ( queryObj[ "id" ] === "1" ) {
	  tab_crevents.show()
}
	
$(function() {	   
	$('a[rel*=external]').click( function() {
		window.open(this.href);
		return false;
	});
});

$(document).ready(function(){
	
	var pageHeight = $(window).height();
	var bodyHeight = $("body").height();
	
	if (pageHeight >= 836) {
		$('#HOME .main').css({'top':((pageHeight-836)/2)+'px'}).css({'margin-bottom':(pageHeight-836)+'px'});
	} else {
		$('#HOME .main').css({'top':(0)+'px'}).css({'margin-bottom':(0)+'px'});
	}
    
	$(window).resize(function(){
        var pageHeight = $(this).height();
		if (pageHeight >= 836) {
			$('#HOME .main').css({'top':((pageHeight-836)/2)+'px'}).css({'margin-bottom':(pageHeight-836)+'px'});
		} else {
			$('#HOME .main').css({'top':(0)+'px'}).css({'margin-bottom':(0)+'px'});
		}
    });
	
	$("#expand, .expand").hide();
	$("#ajax").html('<div id="loader"></div>');
						   
	var $elem = $('#container');
	var $pageID = "http://reko.tv/work/" + queryObj["item"];

	if (queryObj["item"]) {
		$("#WORK #ajax").load($pageID, function(response, status, xhr){
			if (status == "error") {
				var msg = "Sorry but there was an error: ";
				$("#ajax").html("<div class='error'>" + msg + xhr.status + " " + xhr.statusText + "</div>");
			};
			$("#expand").slideToggle(500, function(){
				$('html, body').animate({
					scrollTop:$elem.height()
				}, 800);
			});
		});
		
		var $origHeight = $(this).parent().height();
		$("#post" + queryObj["item"] + " .postClickable").addClass("unclickable");
		$("#post" + queryObj["item"] + " .moreWrap").css('visibility','hidden');
		$("#post" + queryObj["item"] + " .expand").slideToggle(500, function() {
			$(".ua-firefox body, .ua-safari body").removeAttr("style");
			$(".ua-firefox body, .ua-safari body").css("height", $("#container").height() + "px");
		});
	}
	
	// WORK section
	$("#WORK .row span, #showreel span").click(function(e){		

		var $contentID = $(this).attr('id');

		$(".row span, #showreel span").removeClass("unclickable");
		$(this).addClass("unclickable");
		
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
		} else {
			$(".logo", this).show();
		}
		
		if ($("#expand").is(':hidden')){
			$("#ajax").load($contentID, function(response, status, xhr){
				if (status == "error") {
					var msg = "Sorry but there was an error: ";
					$("#ajax").html("<div class='error'>" + msg + xhr.status + " " + xhr.statusText + "</div>");
				};
				$("#expand").slideToggle(500, function(){
					$('html, body').animate({
						scrollTop:$elem.height()
					}, 800);
				});
			});
		} else {
			$('html, body').animate({
				scrollTop:$elem.height()
			}, 800);
			$("#ajax").load($contentID, function(response, status, xhr){
				if (status == "error") {
					var msg = "Sorry but there was an error: ";
					$("#ajax").html("<div class='error'>" + msg + xhr.status + " " + xhr.statusText + "</div>");
				};
				return false;
			});
		}
							
	});
	
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
	} else {
		$("#WORK .row span, #showreel span").hover(
			function () {$(".logo", this).show();}, 
			function () {$(".logo", this).hide();}
		);
	}
	
	$("#WORK .close-up, #WORK .close-x").click(function(e){
		$('html, body').animate({scrollTop: '0px'}, 800);								
		$(".row span, #showreel span").removeClass("unclickable");
		$("#expand").delay(800).slideToggle(200);
		return false;
    });	
	
	// BLOG section
	$(".postClickable").click(function(e){
		var $origHeight = $(this).parent().height();
		$(this).addClass("unclickable");
		$(".moreWrap",this).css('visibility','hidden');
		$(this).next().slideToggle(500, function() {
			$(".ua-firefox body, .ua-safari body").removeAttr("style");
			$(".ua-firefox body, .ua-safari body").css("height", $("#container").height() + "px");
		});
	});
	
	$(".postClickable").hover(
	  function () {
		$(this).parent().addClass("hovered");
		$(this).parent().next().addClass("hovered");
	  }, 
	  function () {
		$(this).parent().removeClass("hovered");
		$(this).parent().next().removeClass("hovered");
	  }
	);
	
	$("#BLOG .close-up, #BLOG .close-x").click(function(){
		$(this).parent().css("top","-31px");
		$(this).parent().slideToggle(500, function() {					   
			$(this).prev().children(".moreWrap").css('visibility','visible');
			$(".ua-firefox body, .ua-safari body").removeAttr("style");
		});
		$(this).parent().prev().removeClass("unclickable");
    });
    
    /* TEMP FIX FOR NEW SERVER */
	$('nav a').each(function() {
		var link = $(this).html();
		link = link.toLowerCase();
		$(this).attr('href', '/'+link);
	});
	
	$('a[href*=?phpMyAdmin]').each(function() {
		var href = $(this).attr('href');
		var newhref = href.replace(/\?phpMyAdmin=.*$/, '');
		$(this).attr('href', newhref);
	});
	
});
