$(document).ready(function(){
	$(".menuItem").bind({
		mouseover: function(){$(this).addClass("menuItemOver");},	
		mouseout: function menuStyleeOut(){$(this).removeClass("menuItemOver");},
		click: function(){
				var element = $(this);
				$(".menuItem.menuItemClicked").each(function(){$(this).removeClass("menuItemClicked")});
				element.addClass("menuItemClicked");
				var div = $("#innerContent");
				var arr = element.attr("id").split("_");
				var to = arr[0] + "/" + arr[0] +arr[1]+".html";
				div.fadeOut(600, function(){div.load(to).fadeIn();});
				}
		});
	$(".menuItemInner").bind({
		mouseover: function(){$(this).addClass("menuItemOver");},	
		mouseout: function menuStyleeOut(){$(this).removeClass("menuItemOver");},
		click: function(){
				$(".menuItemInner.menuItemClicked").each(function(){$(this).removeClass("menuItemClicked")});
				var element = $(this);
				element.addClass("menuItemClicked");
				var div = $("#innerContent");
				var arr = element.attr("id").split("_");
				var to = arr[0] + "/" + arr[0] +arr[1]+".html";
				div.fadeOut(600, function(){div.load(to).fadeIn();});
				}
		});
	$("#rc").bind({
		mouseover: function(){
			var t = $('#rc *');
			if(t.length == 0){
				//$(this).append($('<div>', {id: '76_s', html: 'Are you ready to fly?!?', onclick: 'inputter()'}).css('color', '#000000'));
				$(this).append($('<div>', {id: '76_s', html: 'Are you ready to fly?!?'}).css('color', '#000000').click(function(){inputter();$(this).remove();}));
				//$(this).append($("<div id='76_s' onclick='inputter();$(this).remove();' style='color: #000000;'>Are you ready to fly?!?</div>"));
			}
		},
		mouseout: function(){
			setTimeout("$('#76_s').remove()",300);
		}
	});
	$("dl dt img").bind({
		mouseover: function(){$(this).css('opacity', '1')},
		mouseout: function(){$(this).css('opacity', '0.7')}
	});
});



function inputter(){
	var tmp = $('#76, #76_1');
	if(tmp.length == 0){
		$('#rc').append($('<input>', {id: '76_i', type: 'text', width: "100"}).css('border', '1px solid #CCCCCC').blur(function(){flight();}));
	}
}

function removeDelay(me, t){
	setTimeout("$('#"+me+"').remove()",t);
}

function flight(){
	var tmp = $("#76_i").val();
	hit = false;
	$.getJSON('76/asc.json', function(data){
		$.each(data.users, function(a, u) {
    		$.each(u.user, function(n,d){	
				if(d == tmp){
					$("#rc").append($('<a>', {id: "76", href: "76/76.html", target: "_blank", html: "Take off!"}));	
					hit = true;
					removeDelay('76', 700);
				}
			});
		});
		if(!hit){ 
			$("#rc").append($('<div>', {id: "76", html: "CRASH!"}));
			removeDelay("76", 300);
		}
	});
	$("#76_i").remove();
}


function bindMenuItem(){
	$(".menuItem").bind({
	mouseover: function(){$(this).addClass("menuItemOver");},	
	mouseout: function menuStyleeOut(){$(this).removeClass("menuItemOver");},
	click: function(){
			$(".menuItemClicked").each(function(){$(this).removeClass("menuItemClicked")});
			var element = $(this);
			element.addClass("menuItemClicked");
			var div = $("#innerContent");
			var arr = element.attr("id").split("_");
			var to = arr[0] + "/" + arr[0] +arr[1]+".html";
			div.fadeOut(600, function(){div.load(to).fadeIn();});
			}
	});
}

function openHTML(namespace, obj, target){
		var div = $("#"+target);
		var page = namespace+"/"+namespace+obj+".html";
		div.fadeOut(600, function(){div.load(page).fadeIn();});
}

function openAlbum(obj, target){
		var div = $(target);
		var to = "albums/pictures_"+obj+".html";
		div.fadeOut(600, function(){div.load(to).fadeIn();});
}

function openAlbumEasy(obj, target){
	var div = $('#'+target);
		var to = obj+".html";
		div.fadeOut(600, function(){div.load(to).fadeIn();});
}

function handleAlbums(album, status){
	if(status == 0){
		$("div.albumWrapper > dl > dt > img").each(function(n){
			var current = this;
			src = current.src;
			var tmp = src.split(".jpg");
			image = $('<img>', {id: current.id,src: tmp[0] + '_mini.jpg', title: current.alt, alt: current.title,click: function(){handleAlbums(current.id, 1)} });
			if(current.id == album){
				image.addClass('selectedMini');	
				$('#albumHeader').html(current.title);
			}else{
				image.addClass('notSelectedMini');	
			}
			$("#miniAlbums").append(image);
			//$(this).hide();
		});
		$("#miniAlbumWrapper").show("slow");
		albumLabel($('#albumLab'));toggle('id', 'albumLab');
	}else{
		$("#miniAlbums > img.selectedMini").removeClass('selectedMini').addClass('notSelectedMini');
		$("#"+album).removeClass('notSelectedMini').addClass('selectedMini');
		$('#albumHeader').html($("#"+album).attr('alt'));
	}
	$('.albums').hide('slow');
	$('.cont').height(775);
	openAlbum(album, '#innerContent');
}

function toggle(type, arg){
	var display;
	if(type == 'class'){
		display = $('.'+arg);
	}else if(type == 'id'){
		display = $('#'+arg);
	}
	
	if(display.is(':hidden')){
		display.show('slow');
	}else {
		display.hide('slow');
	}
	
}

function albumLabel(d){
	hideLabel = 'D'+unescape("%F6")+'lj album';
	element = $(d);
	if(element.html() == hideLabel || element.html().length == 0){
		element.html('Visa '+unescape("%F6")+'vriga album');$('.cont').css('min-height', '650px');
	}else{element.html(hideLabel);$('.cont').css('min-height', '750px');}
}

function dia(id){
	$("#"+id).dialog();	
}

function diaHTML(id, url, titleStr){
	var air = $("#air");
	console.log(air);
	if(air.length == 0){
		var html = $('<iframe style="width: 100%; height: 100%;" id="air">');
		html.attr('src', url);
		$("#"+id).append(html);
	}
	
	$("#"+id).dialog({title: titleStr, background: 'black', modal: true, resizable: true});
}

function diaImg(id, url, titleStr){
	var html = $('<img id="air"></img>');
	html.attr('src', url);
	var w = html[0].width + 30;
	var h = html[0].height + 50;
	$("#"+id).html(html);
	$("#"+id).dialog({title: titleStr, background: 'black', modal: true, resizable: true});
	$(".ui-dialog").css('height', h);
	$(".ui-dialog").css('width', w);
}
