/**
 * 
 */

$(document).ready(function(){
	$("#slides").jqFancyTransitions({width: 975, height: 260, titleOpacity: 1, direction: "fountain", position: "top", navigation: true, links: true});
	$("#lomake").submit(function(e){
		$('input.req', $(this)).each(function(){
			if($(this).val() == "" || $(this).val() == "Sähköpostiosoite tai puhelinnumero") {
				e.preventDefault();
				$(this).css('border', '1px solid red');
				$("#lomake").prepend($('<p class="error">Ole hyvä ja täytä merkityt kentät!</p>'));
			}
		});
	});
	
	$('.links li code').hide();  
	$('.links li p').click(function() {
	    $(this).next().slideToggle('fast');
	});
	$("#navmenu-h li,#navmenu-v li").hover(
	    function() { $(this).addClass("iehover"); },
	    function() { $(this).removeClass("iehover"); }
	);
	  
	$("a.open_close").click(function(event) {
	  	event.preventDefault();
	  	$(this).parent().next().toggle();
	});
	$("a.close").click(function(event){
	  	event.preventDefault();
	  	$(this).parent().parent().toggle();
	});
	
	  
	$("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");
		}
	});
	
	$("#search-box input.submit").hover(function() {
		$(this).addClass('hover');
	}, function (){
		$(this).removeClass('hover');
	});
	$('#search-box .q').focus(function() {
		$(this).addClass('focus');
	}); 
	
	$('#search-box .q').blur(function() {
		$(this).removeClass('focus');
	}); 
	
	$('label').click(function(){
		$(this).next('input').focus();
	});
	
	$('.slideshow img').slider();
	
	customFormElements();
	
});

$.fn.slider = function() {
	var $this = this;
	$this.each(function(){
		var element = $(this);
		var control = $('<a href="#"></a>').appendTo("#sliderControls");
		control.click(function(e){
			e.preventDefault();
			$("#sliderControls .activeControl").removeClass('activeControl');
			$(this).addClass("activeControl");
			$this.doFadeIn(element);
			window.clearInterval($this.interval);
			$this.interval = setInterval(function(){ $this.fadeNext(); },4000);
		});
	});
	$this.doFadeIn = function(arg) {
		$this.filter('.visible').removeClass('visible').fadeOut(1000);
		arg.fadeIn(1000);
		arg.addClass('visible');
	};
	$("#sliderControls a").first().addClass('activeControl');
	$this.fadeNext = function() {
		var nextControl = $('#sliderControls .activeControl').next();
		if(nextControl.length == 0) nextControl = $('#sliderControls a').first();
		nextControl.trigger('click');
	};
	$this.doFadeIn($this.first());
	$this.interval = setInterval(function(){ $this.fadeNext(); },4000);
}

function customFormElements() {
	$("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");
		}
	});
	
	$("select").each(function() {
		var select = $(this);
		$(this).css("display", "none");
		var options = $("option", this);
		var defaultValue;
		for(var i = 0; i < options.length; i++) {
			if($(options[i]).attr("selected") == "selected") {
				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) {
			event.preventDefault();
			//alert($('option[selected]', select).index());
			var i = $(this).parent().index();
			var j = $('option[selected]', select).index();
			$(options[i]).attr("selected", "selected");
			$(options[j]).removeAttr("selected");
			$(this).parent().parent().parent().children("span.value").html($(this).html());
			var openThis = $(options[i]).attr('value');
			$('.sinun-kiropraktikkosi-sivu .sinun-kiropraktikkosi').hide();
			$('.sinun-kiropraktikkosi-sivu #' + openThis).show();
		});
	});
	$('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.removeAttr("checked");
			}
			else {
				$(this).addClass("checked");
				$this.attr("checked", "true");
			}
		});
	});
}
