$(document).ready(function(){
	var newsItems = $(".newsitem");
	var i = 0;
	var init = function() {
		//clearTimeout();
		if(i == newsItems.length) i = 0;
		if(newsItems[i].style.display == "none") {
			$(newsItems[i]).fadeIn();
			setTimeout(init, 4000);
		}
		else {
			i++
			$(newsItems[i - 1]).fadeOut(400, init);
		}
	};
	
	if(newsItems.length > 1) {
		newsItems.hide();
		init();
	}
	
	//$('.checkbox').customRadioCheckbox();
	
	$('input.checkbox').each(function() {
		var $this = $(this);
		$this.css("display", "none");
		var custom = $('<div class="checkbox"></div>');
		$this.parent().prepend(custom);
		custom.click(function(){
			if($(this).hasClass("checked")) {
				$(this).removeClass("checked");
				$this.attr("checked", "");
			}
			else {
				$(this).addClass("checked");
				$this.attr("checked", "true");
			}
		});
	});
	
	$("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").change(function(){
		if ($(this).val() == "") {
			if (jQuery.support.opacity == true)
				$(this).prevAll('label').first().fadeTo(100, 1);
			else
				$(this).prevAll('label').first().css("display", "block");
		}
		else {
			if (jQuery.support.opacity == true)
				$(this).prevAll('label').first().fadeTo(100, 0.1);
			else
				$(this).prevAll('label').first().css("display", "none");
		}
	});
	$('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");
		}
	});
	
	$("select").each(function() {
		$(this).css("display", "none");
		var options = $("option", this);
		var defaultValue;
		for(var i = 0; i < options.length; i++) {
			if($(options[i]).attr("selected") == true) {
				defaultValue = $(options[i]).html();
				$('.' + defaultValue).show();
				break;
			}
			else $defaultValue = $(options[0]).html();
			//alert($(options[i]).attr("selected"));
		}
		var htmlStr = '<ul class="select"><li><span class="value">' + defaultValue + '</span><ul class="options">';
		for(i = 0; i < options.length; i++) {
			htmlStr += '<li><a href="">' + $(options[i]).html() + '</a></li>';
		}
		htmlStr += "</ul></li></ul>";
		$htmlStr = $(htmlStr);
		$(this).after($htmlStr);
		if($(this).hasClass("error")) $htmlStr.addClass("error");
		$("ul.select").click(function(){
			$("ul.options").toggle();
		});
		$("ul.options li a").click(function(event) {
			$('.dynamic').filter('.visible').hide().removeClass('visible').children("input").attr("value", "");
			$('.' + $(this).html()).show().addClass('visible');
			event.preventDefault();
			for(i = 0; i < options.length; i++) {
				if($(options[i]).html() == $(this).html()) {
					$(options[i]).attr("selected", "selected");
					$(this).parent().parent().parent().children("span.value").html($(this).html());
					break;
				}
			}
		});
	});
	
	$("#main .image_container a").fancybox({"titlePosition": "inside"});

	$("ul.level_2 li, #header .mod_article").mouseover(function(){ 
		$("ul.level_2 ul").hide();
		$("ul", this).show();
	});
	if(window.swfobject === undefined) {}
	else {
		swfobject.embedSWF("/tl_files/ahlstrom/flash/ruokabanneri-467x174.swf", "banner-food", "467", "174", "9.0.0","expressInstall.swf", {}, {}, {});
	}
	
	$.datepicker.regional['fi'] = {
			closeText: 'Sulje',
			prevText: '&laquo;Edellinen',
			nextText: 'Seuraava&raquo;',
			currentText: 'T&auml;n&auml;&auml;n',
	        monthNames: ['Tammikuu','Helmikuu','Maaliskuu','Huhtikuu','Toukokuu','Kes&auml;kuu',
	        'Hein&auml;kuu','Elokuu','Syyskuu','Lokakuu','Marraskuu','Joulukuu'],
	        monthNamesShort: ['Tammi','Helmi','Maalis','Huhti','Touko','Kes&auml;',
	        'Hein&auml;','Elo','Syys','Loka','Marras','Joulu'],
			dayNamesShort: ['Su','Ma','Ti','Ke','To','Pe','Su'],
			dayNames: ['Sunnuntai','Maanantai','Tiistai','Keskiviikko','Torstai','Perjantai','Lauantai'],
			dayNamesMin: ['Su','Ma','Ti','Ke','To','Pe','La'],
			weekHeader: 'Vk',
	        dateFormat: 'dd.mm.yy',
			firstDay: 1,
			isRTL: false,
			showMonthAfterYear: false,
			yearSuffix: ''};
	    $.datepicker.setDefaults($.datepicker.regional['fi']);
	
	//$.datepicker.setDefaults( $.datepicker.regional[ "" ] );
	$("#ctrl_28, #ctrl_29").datepicker($.datepicker.regional['fi']);
	$("#slider .ce_image").slider();
});

jQuery(function($){
    
});

$.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');
	$("#sliderControls a").first().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($(".ce_image").first());
}
