$(document).ready(function() {
 
var message= $("#message").attr("message");


if (message!="") {

	if(message=="403") { message="403"; } else { message="404"; }

	$("#message").html("<img class='message png' src='core/image_"+message+".png'/>");
	$("#message").show();
	$("#message").click(function () {
		$("#message").hide();
	});	
}
else
{
$("#message").hide();
}
	$("#menu").load("includes/menu.php", function() {
		enableMenu();
		$.history.init(pageload);
	});
});

function pageload(hash) {

	if(hash) {
		enableContent(hash); 
	} else {
		enableContent("updates"); 
	}
	
	if(hash=="updates")
	{
	//$("#related,#backicon").hide();
	}
	
}

function enableMenu()
{
	$('.menubox,.menutext').hover(function () {
		var test = $(this).parent().find('.rollover').attr('src'); if (test=="core/image_menub_norm.png") { $(this).parent().find('.rollover').attr('src', 'core/image_menub_over.png'); }
	}, function () {
		var test = $(this).parent().find('.rollover').attr('src'); if (test=="core/image_menub_over.png") {	$(this).parent().find('.rollover').attr('src', 'core/image_menub_norm.png'); }
	});

	$(".menubox,.menutext").click(function () {
	
		var page = $(this).parent().attr("id");
		$.history.load(page);
		$("#menu").load("includes/menu.php?page="+page+"", function() { enableMenu();  });
		
		
	});
}

function enableContent(hash)
{
	$("#items").hide();
	$("#items").html("<img src='http://c44.statcounter.com/t.php?sc_project=3656341&camefrom=www.niroku.com&u=$http://www.niroku.com/"+hash+"&t=$nirokugraphic&java=1&security=2dbc330e&invisible=1' width='0' height='0' />");
	$("#items").load("includes/content.php?page="+hash+"",function() {
	
		if ((hash == "print") || (hash == "website") || (hash == "updates" )|| (hash == "bio" )|| (hash == "logos" )|| (hash == "software") || (hash == "motion")) {
			$("#menu").load("includes/menu.php?page="+hash+"", function() { enableMenu(); $("#related").hide(); });	
		}

		var maximages=1;
		var currentimage="";
		var px=0;

		var item = hash;
		var imagecount = $("html").find(".data").attr("imagecount");

		px=0;
		currentimage=1;
		maximages=imagecount;
		
		$("#window,#next").hover(function () {
			if (maximages==1) { var pointerstate="default"; } else { var pointerstate="pointer"; } $("#window").css("cursor",pointerstate);
			if (currentimage == maximages || maximages==1) { $(this).parent().find("#next").attr("src", "core/image_back_over.png"); } else {	$(this).parent().find("#next").attr("src", "core/image_next_over.png");	}

			if (maximages>1) { $("#next").show(); }

			$("#count").show(); 

		}, function () { $("#next,#count").hide(); });

		$("#window,#next").click(function() {
			if (currentimage == maximages-1 || maximages==1) { $(this).parent().find("#next").attr("src", "core/image_back_over.png"); } else {	$(this).parent().find("#next").attr("src", "core/image_next_over.png");	}
			if (currentimage == maximages) { px=0; currentimage=1; $("#images").animate({ left: px}, 260); $(".counttext").html("1 / "+maximages);} else {
				px=px-974;	$("#images").animate({ left: px}, 260); currentimage++; 
				$(".counttext").html(currentimage+" / "+maximages);
			}
		});
		$("#downloadqt").hover(function () { $(this).parent().find("#downloadqt").attr("src", "core/image_dlqt_over.png"); }, function () { $(this).parent().find("#downloadqt").attr("src", "core/image_dlqt_norm.png");	});
		$("#downloadwm").hover(function () { $(this).attr("src", "core/image_dlwm_over.png"); }, function () { $(this).attr("src", "core/image_dlwm_norm.png");	});
		$("#buy,#buyicon").hover(function () { $(this).parent().find("#buyicon").attr("src", "core/image_buy_print_over.png"); }, function () { $(this).parent().find("#buyicon").attr("src", "core/image_buy_print_norm.png");	}); 
		$("#visit,#visiticon").hover(function () { $(this).parent().find("#visiticon").attr("src", "core/image_visit_over.png"); }, function () { $(this).parent().find("#visiticon").attr("src", "core/image_visit_norm.png");	});
		
		$('#infoicon').hover(function () {
			var hidebuttonstate = $(this).attr("src");
			if (hidebuttonstate == "core/image_less_norm.png") { $(this).attr("src", "core/image_less_over.png"); } else { $(this).attr("src", "core/image_more_over.png"); }
		}, function () {
			var hidebuttonstate = $(this).attr("src");
			if (hidebuttonstate == "core/image_less_over.png") { $(this).attr("src", "core/image_less_norm.png"); } else { $(this).attr("src", "core/image_more_norm.png"); }
		});

		$("#infoicon").click(function () {
			var hidebuttonstate = $(this).attr("src");
			if (hidebuttonstate == "core/image_less_over.png") { $(this).attr("src","core/image_more_over.png"); } else{ $(this).attr("src","core/image_less_over.png"); }
		});
		
		$("#infoicon").toggle(function () {
			$("#moreinfo").show();
		}, function () {
			$("#moreinfo").hide();
		});
		
		$(".thumbbg,.thumbtitle,.thumbdesc,.thumbimage").hover(function () {
			$(this).parent().find('.rollover').attr('src', 'core/image_thumbb_over.png');
		}, function () {
			$(this).parent().find('.rollover').attr('src', 'core/image_thumbb_norm.png');
		});
		
		$("#backicon").hover(function () {
			$(this).attr('src', 'core/image_backsmall_over.png');
		}, function () {
			$(this).attr('src', 'core/image_backsmall_norm.png');
		});

		var backto=$("#backicon").attr("backto");
		
		$("#backicon").click(function () {
			//$("#backicon,#related").hide();
			$.history.load(backto);
			
		});
		
		$(".thumbbg,.thumbtitle,.thumbdesc,.thumbimage").click(function () {
		
			$("#backicon").show();
			
			if (hash == "motion") { $("#backicon").css("top","23px"); backto = "motion"; }
			if (hash == "print") { $("#backicon").css("top","46px"); backto = "print"; }
			if (hash == "logos") { $("#backicon").css("top","115px"); backto = "logos"; }
			if (hash == "website") { $("#backicon").css("top","69px"); backto = "website"; }
			if (hash == "software") { $("#backicon").css("top","92px"); backto = "software"; }
			if (hash == "stuff") { $("#backicon").css("top","115px"); backto = "misc"; }

			page = $(this).parent(".thumbitem").attr("projectcode");
			
			$.history.load(page);
			
			//type = $(this).parent(".thumbitem").attr("related");
			related = $(this).parent(".thumbitem").attr("related");
			$("#related").load("includes/related.php?related="+related+"&item="+page+"", function() {
			//alert(
			
				//$("#menu").load("includes/menu.php?page="+backto+"", function() { enableMenu();  });
				
				$("#related").show();
				
				$(".submenubg,.submenutitle,.submenudesc").hover(function () {
					$(this).parent().find('.rollover').attr('src', 'core/image_submenub_over.png');
				}, function () {
					$(this).parent().find('.rollover').attr('src', 'core/image_submenub_norm.png');
				});
				
				$(".submenubg,.submenutitle,.submenudesc").click(function () {
					page = $(this).parent(".submenuitem").attr("projectcode");
					$.history.load(page);
				});
			});
		});
				
		$(function() {
			$("img,div").disableTextSelect();
		});
		$("#items").show();
		
		$(function() {  
		
			var tweet = "";  
			var tweeturl = "http://twitter.com/status/user_timeline/nirokugraphic.json?count=6&callback=?";
			
			$.getJSON(tweeturl, function(d) {
				$.each(d, function(i, item) {  
				var url;
				var date = item.created_at;
				var url = getUrl(item.text);
				var dateShort = date.substring(0,10);
				tweet += "<div class='newsitem'>";
				
				if (url != null) {
				tweet += "<img src='core/image_news_norm.png'>";
				tweet += "<div class='newsurl'>";
					tweet += "<a href='"+url+"' target='_new'>";
					tweet += "<img class='newstext png' src='includes/typegen.php?size=8&width=365&colour=191c1f&text="+dateShort+": "+item.text+"'>";
					tweet += "<img class='nexticon' src='core/image_nextsmall_norm.png'></a>";
					tweet += "</div>";
				tweet += "<img src='core/image_news_norm.png'>";
				}
				else {
				tweet += "<img src='core/image_news_over.png'>";
				tweet += "<div class='newsbg'>";
					tweet += "<img class='newstext png' src='includes/typegen.php?size=8&width=370&text="+dateShort+": "+item.text+"'>";
					tweet += "</div>";
				tweet += "<img src='core/image_news_over.png'>";
				}
				
				tweet += "</div>";
				});  
      	    tweet += "";  						
        	$('#twitter').html(tweet); 
			});  
		}); 
	});
	
	
}
function getUrl(text) {   
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;   
return text.match(exp,"<a href='$1'>$1</a>");
} 

