$(document).ready(function() {
	function bezaras() {
		$("#bg").animate({
			opacity: 0,
		}, 750, '', function() {
			$("#bg").remove();
		});
		$(".lightbox").animate({
			opacity: 0,
		}, 750, '', function() {
			$(".lightbox").remove();
		});
		$("#preloader").remove();
		document.location = (document.location.toString()).split("-")[0];
	}
	function nagy_kep(szamozas, title) {
		$("body").append('<div id="bg"></div>');
		$("body").append('<div id="preloader"><img src="http://ilia.filin.eu/imgs/preloader.gif" alt="preloader" /></div>');
/* 		$("body").append('<div class="lightbox"><img src="http://ilia.filin.eu/imgs/portfolio/'+html+'.jpg?'+ (Math.ceil(Math.random()*9999999)) +'" class="lightbox_kep" /></div>'); */
		$("#bg").animate({
			opacity: 0.6,
		}, 750);
		$("#preloader").animate({
			opacity: 1,
		}, 750);
					
		if (parseInt(szamozas) > 6) {
			bezaras();
			return "hello";
		}
		
		if (szamozas.split("-")[0] == 'photos') {
			szamozas = parseInt(szamozas.split("-")[1])+6;
		} else {
			szamozas = szamozas.split("-")[1];
		}
		
		var datastr = "mitis=nagy_kep&szamozas="+ szamozas +"&szelesseg="+ $(window).width() +"&magassag="+ $(window).height();
		$.ajax({
			type: "POST",
			url: "tartalom/ajax.php",
			data: datastr,
			success: function(html){
				//alert(html);
				if (html != "false") {
/*
					$("body").append('<div id="bg"></div>');
					$("body").append('<div id="preloader"><img src="http://ilia.filin.eu/imgs/preloader.gif" alt="preloader" /></div>');
*/
					$("body").append('<div class="lightbox"><img src="http://ilia.filin.eu/imgs/portfolio/'+html+'.jpg?'+ (Math.ceil(Math.random()*9999999)) +'" class="lightbox_kep" /></div>');
					if (title != "") {
						$(".lightbox").append('<br /><a href="http://www.'+ title +'/" target="_blank">Visit it</a>');
					}
/*
					$("#bg").animate({
						opacity: 0.6,
					}, 750);
					$("#preloader").animate({
						opacity: 1,
					}, 750);
*/
					$(".lightbox_kep").load(function() {
						$(".lightbox").css({
							"margin-left": "-"+(parseInt($(".lightbox_kep").width()) / 2)+"px",
							"margin-top": "-"+(parseInt($(".lightbox_kep").height()) / 2)+"px",
						});
						$(".lightbox").animate({
							"opacity": 1,
						}, 500);
						$("#preloader").remove();
					});
				} else {
					bezaras();
					document.location = "#" + document.location.toString().split("#")[1].split("-")[0];
				}
			}
		});
	}

	
	$(".work").each(function() {
/*
		var szamozas = parseInt($(this).attr("alt").split("-")[1]);
		if ($(this).attr("alt").split("-")[0] == 'photos') {
			szamozas += 6;
		}
		var datastr = "mitis=kis_kep&szamozas="+ szamozas +"&szelesseg="+ $(window).width() +"&magassag="+ $(window).height();
		$.ajax({
			type: "POST",
			url: "tartalom/ajax.php",
			async: false,
			data: datastr,
			success: function(html){
				//$(this).attr("src", "http://ilia.filin.eu/imgs/portfolio/"+ html +".jpg");
				//alert($(this).attr("src"));
				var uj_kiskep = "http://ilia.filin.eu/imgs/portfolio/"+ html +".jpg";
			}
		});
		//$(this).attr("src", uj_kiskep);
		//alert(uj_kiskep);
*/
		randomNum = parseInt(Math.ceil(Math.random()*12))-6;
		$(this).css("-webkit-transform", "rotate("+ randomNum +"deg)");
		$(this).css("-moz-transform", "rotate("+ randomNum +"deg)");
		$(this).css("transform", "rotate("+ randomNum +"deg)");
	});

	$(".site").each(function() {
		$(this).css("height", $(window).height() + "px");
	});
	
	var pozicio = document.location.toString();
	var horgony = pozicio.split('#')[1];
	if (horgony) { 
		var kivalszott = horgony.split('-')[1];
		horgony = horgony.split('-')[0];
	}
	
	
	var szorzo = 1;
	
	if (horgony == 'photos') {
		szorzo = 2;
	} else if (horgony == 'webs') {
		szorzo = 0;
	}
	if (kivalszott) {
		nagy_kep(horgony + "-" + kivalszott);
	}
	
	$("#container").css("margin-top", "-"+($(window).height() * parseInt(szorzo))+"px");
	$("body").css("background-position-y", "-"+(parseInt($(window).height())/2)+"px");

	var lock = false;
	$(window).load(function() {
		//var amit = ['name', 'whatdoido'];
		//$.each(amit, function(index, value) {
			$("#welcome").css("margin-left", "-"+(parseInt($("#welcome").width())/2)+"px");
			$("#welcome").css("margin-top", "-"+(parseInt($("#welcome").height())/2)+"px");
		//});
	});
	$("#welcome").mouseover(function() {
		if (!lock) {
			$(".arrow").clearQueue();
			$(".arrow").animate({
				opacity: 0.4,
			}, 1000);
		}
	});
	$("#welcome").mouseout(function() {
		if (!lock) {
			$(".arrow").clearQueue();
			$(".arrow").animate({
				opacity: 0,
			}, 1000);
		}
	});
	$(".menu").mouseover(function() {
		lock = true;
		$(".arrow").clearQueue();
		var jelenlegi = $(this).attr('id').split('_')[1];
		$("#arrow_"+jelenlegi).animate({
			opacity: 1,
		}, 1000);
	});
	$(".menu").mouseout(function() {
		$(".arrow").clearQueue();
		lock = false;
	});
	$(".menu").click(function() {
		var menu = $(this).attr('id').split('_')[1];
		if (menu == 'photos') {
			var mennyire = (parseInt($(window).height()) * 2);
			var link = 'photos';
		} else {
			var mennyire = "0";
			var link = 'webs';
		}
		$("#container").animate({
			"margin-top": "-"+mennyire+"px",
		}, 1000);
		$("body").animate({
			"background-position-y": "-"+(parseInt(mennyire)/2)+"px",
		}, 1000);
		document.location = '#'+link;
	});
	$(".back").click(function() {
		$("#container").animate({
			"margin-top": "-" + $(window).height() + "px",
		}, 1000);
		$("body").animate({
			"background-position-y": "-"+(parseInt($(window).height())/2)+"px",
		}, 1000);
		document.location = '#hello';

	});
	$(".work").mouseover(function() {
		if ($(this).attr('id') != "") {
			$("body").append('<div id="kollab">'+$(this).attr('id')+'</div>');
			$('#kollab').animate({
				opacity: 1,
			}, 150 );
	
			$(document).bind('mousemove', function(e){
				$('#kollab').css({
				   left:  e.pageX,
				   top:   e.pageY,
				});
			});
		}
		$(this).animate({
			opacity: 1,
		}, 200);
	});
	$(".work").mouseout(function() {
		$('#kollab').animate({
			opacity: 0,
		}, 150, '', function(){
			$('#kollab').remove();
		});
		$(this).animate({
			opacity: 0.9,
		}, 200);
	});
	$(".work").live("click", function() {
		//var valami = document.location.toString().split("#")[0];
		document.location = "#"+ $(this).attr("alt");
		nagy_kep($(this).attr("alt"), $(this).attr("title"));
/*
		$("body").append('<div id="bg"></div>');
		$("body").append('<div id="preloader"><img src="http://ilia.filin.eu/imgs/preloader.gif" alt="preloader" /></div>');
		$("body").append('<div class="lightbox"><img src="http://ilia.filin.eu/imgs/portfolio/'+$(this).attr('id')+'.jpg?'+ (Math.ceil(Math.random()*9999999)) +'" class="lightbox_kep" /></div>');
		if ($(this).attr('title') != "") {
			$(".lightbox").append('<br /><a href="http://www.'+$(this).attr('title')+'/" target="_blank">Visit it</a>');
		}
		$("#bg").animate({
			opacity: 0.6,
		}, 750);
		$("#preloader").animate({
			opacity: 1,
		}, 750);
		$(".lightbox_kep").load(function() {
			$(".lightbox").css({
				"margin-left": "-"+(parseInt($(".lightbox_kep").width()) / 2)+"px",
				"margin-top": "-"+(parseInt($(".lightbox_kep").height()) / 2)+"px",
			});
			$(".lightbox").animate({
				"opacity": 1,
			}, 500);
			$("#preloader").remove();
		});
*/
	});
	
	$("#bg").live("click", function() {
		bezaras();
	});
	$(".lightbox_kep").live("click", function() {
		bezaras();
	});
	
	var szelesseg = $(document).width();
	var magassag = $(document).height();
	
	$(document).bind('mousemove', function(e){
		if ($(".lightbox").length) {
			var x_hely_welcome = ((parseInt(szelesseg)/2) - parseInt(e.pageX) / 90);
			var y_hely_welcome = ((parseInt(magassag)/2) - parseInt(e.pageY) / 90);
		
			$('.lightbox').css({
			   left:  x_hely_welcome,
			   top:   y_hely_welcome,
			});
		}
	});

});

