$(function() {
	$('.rounded').round();
	
	$('.menu li').mouseover(function() {
		$(this).addClass('over');
	}).mouseout(function() {
		$(this).removeClass('over');
	});

	$('#index .menu li').click(function() {
		window.location = $(this).find('a').attr('href');
	});
	
	$('#content .menu:not(.sec) li').click(function(e) {
		e.preventDefault();

		$(this).arrow().submenu();
	});
	
	$('#content .sec li').click(function(e) {
		e.preventDefault();

		$(this).arrow().loadContent();
	});
	
	if(location.href.indexOf('#') >= 0) {
		var url = location.href.split('#');
		$('#'+url[1]).click();
	}
	
	try {
		$('#slideshow').cycle({
			prev:   '#prev', 
			next:   '#next'
		});
	} catch (e) {}
	});

$.fn.round = function() {
	return this.prepend('<span class="corner tl" /><span class="corner tr" />').append('<span class="corner bl" /><span class="corner br" />');
};

$.fn.arrow = function() {
	var list = this.parents('ul');

	$(list).find('li').not(this).removeClass('current').find('.current-arrow').remove();
	this.toggleClass('current');

	if(this.hasClass('current')) {
		this.css({'position': 'relative'}).append('<span class="current-arrow" />');
	} else {
		this.find('.current-arrow').remove();
	}
	
	return this;
};

$.fn.submenu = function() {
	var target = this.find('a').attr('href');
	
	var total = $(target).find('li').size();

	$('li.display').arrow().loadContent();

	$('ul.display').not(target).css({'display': 'none', 'width': '0', 'opacity': '0'}).removeClass('display');

	if(total === 1)
	{
		$(target+' li:first').click();
		$('#loadedContent').css({'width': '70%'})
	} else {
		if($(target).hasClass('display')) {
			$(target).animate({width: "0", opacity: 0}, 300, function() {
				$(this).css({'display': 'none'}).removeClass('display');
			});
		} else {
			$(target).animate({width: "25%", opacity: 1}, 300).addClass('display');
		}
	}
};

$.fn.loadContent = function() {
	var target = this.find('a').attr('href');

	if(target) {
		$('li.display').not(this).removeClass('display');
		$('#loadedContent').remove();

		if(this.hasClass('display')) {
			this.removeClass('display');
		} else {
			$('<div id="loadedContent" />').insertAfter('.sec:last');
			$('#loadedContent').load(target+' #data').fadeIn('slow', function() {
				$('.mov').fancybox({
					'overlayShow': true,
					'frameWidth': '512',
					'frameHeight': '288'
				});
				
				$('.thumb-links a').fancybox({
					'overlayShow': true
				});
			});
			this.addClass('display');
		}
	}
};

$.fn.nocomplete = function() {
	i = 0;
	d = new Date;
	time = d.getTime();
	return this.each(function() {
		newName = time+''+i;
		oldName = this.name;
		this.name = newName;
		$(this).keyup(function(e) {
			$(this).next()[0].value = $(this).attr('value');
		}).after('<input type="hidden" name="'+oldName+'" />');
		i++;
	});
};


$.fn.liveUpdate = function(list) {
	list = $(list);
	if(list.length) {
    	var rows = list.children('li'), cache = rows.map(function() {
        	return $('a', this)[0].innerHTML.toLowerCase();
      	});

		this.keyup(filter).keyup().parents('form').submit(function() {
			return false;
		});
	}

	return this;
    
	function filter() {
    	var term = $.trim($(this).val().toLowerCase()), scores = [];
		
		if(!term) {
			rows.fadeOut('fast', function() {
				$('#search-help').show();
				$('#file-list').fadeIn('slow');
			});
		} else {
			$('#search-help').hide();
			rows.hide();

			if(term === '*') {
				rows.show();
			} else {
				cache.each(function(i) {
					var score = this.score(term);
					if(score > 0) { 
						scores.push([score, i]); 
					}
				});

				$.each(scores.sort(function(a, b) {
					return b[0] - a[0];
				}), function() {
	        		$(rows[this[1]]).fadeIn('fast', function() {
						$(this).css({'display': 'inline-block'});
					});
				});
			}
		}
  	}
};
