var userAgent = navigator.userAgent;

if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/iPod/i)) {
	alert("Use 2 Fingers to scroll throught the Content Boxes.")
};

$(window).load(function () {
	var curl = (document.URL);
	if (curl.indexOf( "category" ) !== -1) {
		$("input[id=n_Portfolio]").attr("checked", "checked");
			$("#bottomContainer :not(.game) .ybox, .content:has(.xbox) .vbox, #textBoxProfile, #textBoxNews, #textBoxShop, #contact, #exhibitions, #awards, #buy").hide();
	} else {
		$("input[id=n_News]").attr("checked", "checked");
			$("#bottomContainer :not(.game) .ybox, .content:has(.xbox) .vbox, #textBoxProfile, #textBoxPortfolio, #textBoxShop, #contact, #exhibitions, #awards, #buy").hide();
			$("#textBoxNews").show();
	}
	
	$("#textBox .subnav, .content, .rimg").each(function() {
		$(this).find("input:first").attr("checked", "checked");
	});
});

$(document).ready(function () {

/* ########### CHECK IMAGE SIZE AND DEFINE .INSIDE ########### */

	function newsMargin_o(counter) {
		var html = $('.content');
		var num = html.find('img').length;
		if (num > 0) {
			$(html).find('img').load(function(){
				counter++;
				if (counter == num) {
					allElementsAreLoaded();
				}
			});
		} else {
			allElementsAreLoaded();
		}
	};
	newsMargin_o(0);

	function allElementsAreLoaded(){
		$(".inside").each(function() {
			$(this).width( $(this).children("div").find("img").length * 600 );
		});
	};
	allElementsAreLoaded();

/* ########### CHANGING CONTENT IN TOP BOXES ########### */

	$(".topContainer input[name=n_main]").click(function(event) {
		$("#textBox").scrollTo(0, 500).children("div").hide();
		var path = $(this).attr("id").substr(2);
		$("#textBox #textBox"+path).show();
		event.stopPropagation();
	});
	
	$(".subnav input").click(function(event) {
		var path = $(this).attr("id").substr(2);
		$(this).parent().parent().parent().children("div[id]").hide();
		$("#"+path).show();
		event.stopPropagation();
	});
	
	$(".shopNav .subnav input").click(function(event) {
		var path = $(this).attr("id").substr(2);
		$(this).parent().parent().parent().parent().children("div[id]").hide();
		$("#"+path).show();
		event.stopPropagation();
	});
	
	
	/*
	
	if ($(this).parent().children(".xbox:not(.noFix)")) {
	} else {
		alert("true");
	}
	
	*/
	
/* ########### SHOPNAV ########### */

	$("#textBox").scroll(function() {
		var spos = $("#textBoxShop").position();
		if (spos.top < 0) {
			$(".shopNav").css("top", (-spos.top));
		} else {
			$(".shopNav").attr("style", "");
		}
	});

/* ########### CHANGING CONTENT IN CONTENT BOXES ########### */

	$(".content .txt").live("click",function(event) {
		$(this).parent().children(".ybox, .vbox").css("height", $(this).parent().children(".xbox").height());
		$(this).parent().children(".xbox, .vbox").hide().parent().children(".ybox").show();
		event.stopPropagation();
	});
	$(".content .img").live("click", function(event) {
		$(this).parent().children(".ybox, .vbox").hide().parent().children(".xbox").show();
		event.stopPropagation();
	});
	
	$(".content .vid").live("click", function(event) {
		$(this).parent().children(".ybox, .vbox").css("height", $(this).parent().children(".xbox").height());
		$(this).parent().children(".ybox, .xbox").hide().parent().children(".vbox").show();
		event.stopPropagation();
	});
	
	$(".radio input").live("click", function(event) {
		$(this).parent().parent().children(".rimg").children("img").hide().parent().children("#i"+$(this).attr("id").substr(1)).show();
		event.stopPropagation();
	});
	
	$(".category_list a").click(function(event) {
		event.stopPropagation();
	})

/* ########### SCROLLING THROUGH CONTENT ( X BOXES) ########### */

	$(".xbox").live("click", function(event) {
		var outside  = $(this).width();
		var inside   = $(this).children("div:visible").width();
		var outsideP = $(this).position();
		var insideP  = $(this).children("div:visible").position();
		if((inside+insideP.left-outsideP.left) > outside) {
			$(this).scrollTo({top: 0, left: "+="+outside}, 500);
		} else {
			$(this).scrollTo(0, 500);
		}
		event.stopPropagation();
	});
	
	
	$(".xbox .project div").live("click", function(event) {
		var pics  = $(this).parent().find("div").size();
		var index = $(this).prevAll().length+2;
		if(index <= pics) {
			$(this).parent().parent().scrollTo($(this).next("div"), 500);
		} else {
			$(this).parent().parent().scrollTo(0, 500);
		}
		event.stopPropagation();
	})

/* ########### SCROLLING THROUGH IMG ( Y BOXES) ########### */

	$(".ybox").live("click", function(event) {
		var outside = $(this).height();
		var inside  = $(this).children("div:visible").height();
		var insideP = $(this).children("div:visible").position();
		if((inside+insideP.top) > outside) {
			$(this).scrollTo({top:"+="+outside, left: 0}, 500);
		} else {
			$(this).scrollTo(0, 500);
		}
		event.stopPropagation();
	});

/* ########### BACKGROUND ########### */

	$("#background").click(function() {
		$(window).scrollTo({top: "+="+$(window).height(), left: 0}, 1000);
	});
	
/* ########### OVERLAY ########### */
	
	$(".overlay div").scroll(function(){
		var posOut = $(this).position();
		var posIn  = $(this).children().position();
		$(this).find("img").stop().css({
			top  : (posOut.top - posIn.top),
			left : (posOut.left - posIn.left)
		});
	});
	
	$(".rotimg").scroll(function() {
		var posIn  = $(this).children("div").position();
		$(this).children("div").children("img").stop().css({
			top              : (-posIn.top),
			 WebkitTransform : 'rotate(' + posIn.top + 'deg)',
			'-moz-transform' : 'rotate(' + posIn.top + 'deg)'
		});
	});
	
	function aroundTheWorld() {
		$(".around input").each(function() {
			var imgh = $(this).parent().parent().children(".rimg").children("img").height();
			var imgw = $(this).parent().parent().children(".rimg").children("img").width();
			if ($(this).is(':nth-child(4n+1)')){
				$(this).css({
					top  : -imgh-15,
					left : imgw/2-15
				});
			}
			if ($(this).is(':nth-child(4n+2)')){
				$(this).css({
					top  : -imgh/2,
					left : imgw-15
				});
			}
			if ($(this).is(':nth-child(4n+3)')){
				$(this).css({
					left : imgw/2-15
				});
			}
			if ($(this).is(':nth-child(4n)')){
				$(this).css({
					top      : -imgh/2,
					left     : -15
				});
			}
		});
	}
	aroundTheWorld();
	
	$(".content").scrollTo({top: 0, left: 0}, 0);

/* ########### WINDOW SCROLLING (NOISE) ########### */

	$("#noise a").click(function(event) {
		$(window).scrollTo(0, 1000);
		event.stopPropagation();
	});

/* ########### INFINITE SCROLL ########### */

	$('.bottomContainer').infinitescroll({
		navSelector  : "div.scrollNav",
		nextSelector : "div.scrollNav a:first",
		itemSelector : ".bottomContainer div.content",
		loadingText  : "Just a second",
		loadingImg   : "/include/img/loading.gif",
		donetext     : "FIN"
	}, function(newStuff) {
		allElementsAreLoaded();
		aroundTheWorld();
		$(newStuff).children(".content:not(.game) .ybox, .content:has(.xbox) .vbox").hide();
		$(newStuff).find("input[type=radio]:first").attr("checked", "checked");
	});

});
