$.fn.slider = function() {
	$this = this;
	this.interval = setInterval('$this.fadeNext()',6000);
	this.images = new Array();
	this.controls = new Array();
	this.i = 0;
	this.each(function(){
		var image = $(this);
		var control = $('<a href="#"></a>').appendTo("#sliderControls");
		var src = $("img", this).attr("src");
		$(this).css("background-image", 'url("' + src + '")');
		$(".image_container", this).remove();
		control.click(function(e){
			e.preventDefault();
			$("#sliderControls .activeControl").removeClass('activeControl');
			this.addClass("activeControl");
			$this.fadeIn(image);
			window.clearInterval($this.interval);
			$this.interval = setInterval('$this.fadeNext()',6000);
		});
		$this.images.push(image);
		$this.controls.push(control);
	});
	this.controls[0].addClass('activeControl');
	this.fadeIn = function(arg) {
		$('#slider .visible').removeClass('visible').fadeOut(2000);
		if(typeof arg == 'number') {
			$this.images[arg].fadeIn(2000);
			$this.images[arg].addClass('visible');
		}
		else {
			arg.fadeIn(2000);
			arg.addClass('visible');
		}
	};
	this.fadeNext = function() {
		var nextControl = $('#sliderControls .activeControl').next();
		if(nextControl.length == 0) nextControl = $('#sliderControls a').first();
		nextControl.trigger('click');
	};
	this.fadeIn(0);
}

jQuery.fn.navi = function() {
	$("li", this).hover(function(){
		$("ul", this).first().show();
	}, function() {
		$("ul", this).first().hide();
	});
};

$(document).ready(function(){
	$("#slider .ce_image").slider();
	$(".topnavi").navi();
	
	$("input, textarea").focusin(function(){
		if(jQuery.support.opacity == true) $(this).prevAll('label').first().fadeTo(100, 0.1);
		else $(this).prevAll('label').first().css("display", "none");
	});
	$("input, textarea").focusout(function(){
		if($(this).val() == "") {
			if(jQuery.support.opacity == true) $(this).prevAll('label').first().fadeTo(100, 1);
			else $(this).prevAll('label').first().css("display", "block");
		}
	});
	
	$('input, textarea').each(function(){
		if($(this).val() != "") {
			if(jQuery.support.opacity == true) $(this).prevAll('label').first().fadeTo(100, 0.1);
			else $(this).prevAll('label').first().css("display", "none");
		}
	});
	$('.videolink').fancybox();
});
