$(document).ready(function() {
	$(window).load(function() {
		if ($('#nav').length) {
			$("#nav ul li").each(function(i) {
				if ($(this).find('a').hasClass('act')) {
					$(this).addClass('active');
					$(this).find('a').addClass('active').wrapInner('<span />');
				}
			});
		}
		$(":input").each(function(i) {
			type = this.type;
			if (type == "submit" || type == "button") {
				this.className = "button";
			}
			var asiakaskysely = false;
			if ($('#asiakaskysely').length) {
			    asiakaskysely = true;
			}
			if (type == "radio" && !asiakaskysely) {
				this.className = "no-border";
			}
			if (type == "checkbox") {
				this.className = "no-border";
			}
		 });
	});
	
	
	//block-menu
	/*
	$('div.video_thumb').hover(
		  function () {
			$(this).css('cursor','pointer');
			$(this).append('<div class="video_show"><img src="/images/play_video.png" alt=""  /><br />Katso video!</div>');
			$(this).find(".video_show").fadeIn();
		  }, 
		  function () {
			$(this).find(".video_show").fadeOut("fast").remove();
		  }
		);
		$('div.video_thumb').click(function() {
			window.location.href = $(this).find("a").attr('href');
	})
	*/
	
	if ($('.block-menu').length) {
		$('.block-menu').find('.cols_1 h2 a').each(function (i) {
			$(this).parent().parent().hover(function(){
				$(this).css('cursor','pointer');
				$(this).css('background','#fffcee');
			},
			function () {
				$(this).css('background','none');
		  	});
			$(this).parent().parent().click(function() {
				window.location.href = $(this).find('h2 a').attr('href');
			});
			
		});
	}
	
	if ($('#vertical-submenu').length) {
		$('#vertical-submenu').find('li').each(function (i) {
			$(this).addClass('item-'+i);
			$(this).find('a').append('<span style="display:none"></span>');
			$(this).find('a span').fadeIn(1000);
		})
	}

	
	if ($('.video_thumbs').length) {
		$('div.video_thumb').hover(
		  function () {
			$(this).css('cursor','pointer');
			$(this).append('<div class="video_show"><img src="/images/play_video.png" alt=""  /><br />Katso video!</div>');
			$(this).find(".video_show").fadeIn();
		  }, 
		  function () {
			$(this).find(".video_show").fadeOut("fast").remove();
		  }
		);
		$('div.video_thumb').click(function() {
			window.location.href = $(this).find("a").attr('href');
		})
	}
	
	if ($("#cart").length) {
		$('#cart').a_cart();
	}
	
	if ($("a[rel=lightbox-gallery]").length) {
		$("a[rel=lightbox-gallery]").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': false });
	}
	
	if ($("a[class=lightbox-gallery]").length) {
		$("a[class=lightbox-gallery]").each(function(index) {
			h = $(this).find('img').height() - 50;
			w = $(this).find('img').width();
			$(this).css('position','relative').css('display','block').css('width',w+'px');
			$(this).append('<span class="zoom_in"  style="top:'+h+'px;"></span>');
		});
		$("a[class=lightbox-gallery]").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true });
	}
	
	if ($("#compare").length) {
		initCompare();
	}
	
	if ($(".compareBox").length) {
		$(".compareBox").fancybox(
			{
				'autoDimensions'	: false,
				'width'				: 940,
				'height'			: '85%',
				'autoScale'			: false,
				'href'				: '/index.php?id=1063&get_page_html=1&ajax=1',
				'type'				: 'iframe',
				'onComplete'		: initCompare
			}
		);
	}
	
	if ($(".compare_link").length) {
		$(".compare_link").fancybox(
			{
				'autoDimensions'	: false,
				'width'				: 940,
				'height'			: '85%',
				'autoScale'			: false,
				'href'				: 'http://www.liiketoimintasovellus.fi/index.php?id=1063&get_page_html=1&ajax=1',
				'type'				: 'iframe',
				'onComplete'		: initCompare
			}
		);
	}
	
	if ($(".contact_form").length) {
		$(".contact_form").fancybox(
			{
				'autoDimensions'	: false,
				'width'				: 450,
				'height'			: 540,
				'autoScale'			: false,
				'href'				: 'http://www.liiketoimintasovellus.fi/index.php?id=1161',
				'type'				: 'iframe'
			}
		);
	}
	
	function initCompare() {
		$(".toggle-next").each(function(i) {
			$(this).css('cursor','pointer');
			$(this).hover(
				function() {
					$(this).find('td').addClass('td_hover');
				},
				function() {
					$(this).find('td').removeClass('td_hover');
				}
			);
			$(this).next().hide();
		});
		
		$(".toggle-next").click(function(i) {
			$(this).find("div").toggleClass("arrow-down");
			$(this).next().toggle();
		});
		
		$("#showAll").click(function(i) {
			$(".toggle-next").each(function(i) {
				$(this).find("div").addClass("arrow-down");
				$(this).next().show();
			});
		});
		
		$("table.more_features_table").delegate('td','mouseover mouseleave', function(e) {
			if (e.type == 'mouseover') {
			  $(this).parent().find('td').addClass("showHover");
			}
			else {
			  $(this).parent().find('td').removeClass("showHover");

			}
		});
			
	}
	
	
	
	
	if ($(".openBox").length) {
		
		$(".openBox").each(function(i) {
				$(this).attr("href",$(this).attr("href")+'&get_page_html=1&ajax=1');
		});
		
		$(".openBox").fancybox(
			{
				'autoDimensions'	: false,
				'width'				: 760,
				'autoScale'			: true,
				'height'			: '85%',
				'type'				: 'iframe'
			}
		);
	}
	
	if ($(".openVideoBox").length) {
		$(".openVideoBox").each(function(i) {
			$(this).attr("href",$(this).attr("href")+'&get_page_html=1&ajax=1');
		});
		$(".openVideoBox").fancybox(
			{
				'autoDimensions'	: false,
				'width'				: 1100,
				'autoScale'			: true,
				'height'			: 780,
				'type'				: 'iframe'
			}
		);
		 if (window.location.hash != "undefined") {
			bhash = window.location.hash;
			var shash = bhash.replace(/#/g, '');
			$("a.openVideoBox").each(function(i) {
				if (shash == $(this).attr('id')) {
					$(this).removeAttr('id');
					window.scrollTo(0, 0);
					$.fancybox(
						{
							'autoDimensions'	: false,
							'width'				: 1100,
							'autoScale'			: true,
							'height'			: 780,
							'type'				: 'iframe',
							'href': $(this).attr("href")+'&get_page_html=1&ajax=1'
						}
					);
					return false;
				}
			});
		 }
	}
	
	
	if ($(".openBox-small").length) {
		
		$(".openBox-small").each(function(i) {
				$(this).attr("href",$(this).attr("href")+'&get_page_html=1');
		});
		
		$(".openBox-small").fancybox(
			{
				'autoDimensions'	: false,
				'width'				: 520,
				'autoScale'			: true
			}
		);
	}
	
	if ($(".contents").length) {
		$(".contents").fancybox(
			{
				'autoDimensions'	: false,
				'width'				: 550,
				'autoScale'			: true,
				'href'				: '/index.php?id=352&get_page_html=1&ajax=1',
				'height'			: '85%'
			}
		);
	}
	
	if ($(".news_ticker").length) {
		var i = 0;
		var tickerItems = [];
		$(".news_ticker li").each(function(i) {
			tickerItems[i] = ($(this).html());
		});
		newsTicker();
	}
	function newsTicker(){
		if( i == tickerItems.length ){
			i = 0;
		}
		$('#show_news_ticker').fadeOut("slow", function(){
			$(this).html(tickerItems[i]).fadeIn("slow");
			i++;
		});
		setTimeout(function() { newsTicker() }, 5000);
	}
	
	if ($(".image_ticker").length) {
		var ticker_images = 0;
		var tickerImageItems = [];
		$('.image_ticker').shuffle();
		$(".image_ticker li").each(function(j) {
			tickerImageItems[j] = ($(this).html());
		});
		imageTicker();
	}
	function imageTicker(){
		if( ticker_images == tickerImageItems.length ){
			ticker_images = 0;
		}
		$('#show_image_ticker').fadeOut("slow", function(){
			$(this).html(tickerImageItems[ticker_images]).fadeIn("slow");
			ticker_images++;
		});
		if (tickerImageItems.length > 0) {
			setTimeout(function() { imageTicker() }, 8000);
		}
	}
	/*
	// Sivun arvostelu
	if ($("#arvio").length) {
		$("#arvio_slider").slider({ 
			value:$("#arvio").val(), min: 1, max: 5, step: 1,
			slide: function(event,ui) { $("#arvio").val(ui.value); }
		});
		$("#arvio").val($("#arvio_slider").slider("value"));
	}
	*/
	if ($("#arviointilomake").length) {
		$("#ratings").children().not(":radio").hide();
		$("#ratings").stars({
			cancelShow: false
		});
		$("#q1").focus(function(event) {
			$(this).text("");
			$(this).unbind(event);
		});
	}
	
	if ($(".acc_container").length) {
	$('.acc_container').hide();
		$('.acc_trigger').click(function(){
			if( $(this).next().is(':hidden') ) {
				$('.acc_trigger').removeClass('active').next().slideUp();
				$(this).toggleClass('active').next().slideDown();
			}
			return false;
		});
	}
});


em = function(name1,name2,text,subject,domain) {
  if (!domain) {domain='arkhimedes.net'}
  if (!name2) { name=name1; } else { name=name1+'.'+name2}
  if (!text) {
  	 if (!name2) {text=name1+'&#64;'+domain; }
	 else { text=name1+'.'+name2+'&#64;'+domain; }
  }
  if (!subject) {document.write('<a href="mailto:'+name+'&#64;'+domain+'">'+text+'</a>');}
  else {document.write('<a href="mailto:'+name+'&#64;'+domain+'?subject='+escape(subject)+'">'+text+'</a>');}
}


$(document).ready(function(){
 
	function buildSlider () {
		$('#slider').append('<ul id="slider2"></ul>');
		$('#sliderContent').find('div.slide').each(function(index) {
			if ($(this).find('div.embedContent').html().length > 50) {
				text = $(this).find('div.sliderText').html();
				video = $(this).find('div.embedContent').html();
				title = $(this).find('div.slideTitle').html();
				$('#slider2').append('<li><div class="videoSlide">'+title+'<div class="slideVideoContent">'+video+'</div><div class="slideText"><div class="textContent">'+text+'</div></div></div></li>');
				$(this).remove();
			}
		});
		count = 0;
		total_count = $('#sliderContent').find('div.slide').length;
		total_count = total_count-1;
		$('#sliderContent').find('div.slide').each(function(index) {
			if (count==0) {
				textLeft = $(this).find('div.sliderText').html();
				textDescLeft = $(this).find('div.sliderDesc').html();
				textLeft = '<div class="slideTextLeft"><div class="descContent">'+textDescLeft+'</div><div class="textContent">'+textLeft+'</div></div>';
			}
			if (count==1 || index==total_count) {
				textRight = "";
				if (count==1) {
					textRight = $(this).find('div.sliderText').html();
					textDescRight = $(this).find('div.sliderDesc').html();
					textRight = '<div class="slideTextRight"><div class="descContent">'+textDescRight+'</div><div class="textContent">'+textRight+'</div></div>';
				}
				$('#slider2').append('<li><div class="textSlide">'+textLeft+textRight+'</div></li>');
				count=0;
				textLeft="";
			} else {
				count=1;	
			}
		});
		$('#sliderContent').empty()
		activateSlider();		 
	}
 
 	function activateSlider () {
	 $('#slider2')
	  .anythingSlider({
	   width               : 898,
	   height              : 435,
	   startStopped        : true,
	   resizeContents      : true
	  });
 	}
	
	if ($('#sliderContent').length) {
		buildSlider ();
	}
	
});

(function($){
  $.fn.shuffle = function() {
	return this.each(function(){
	  var items = $(this).children();
	  return (items.length)
		? $(this).html($.shuffle(items))
		: this;
	});
  }
 
  $.shuffle = function(arr) {
	for(
	  var j, x, i = arr.length; i;
	  j = parseInt(Math.random() * i),
	  x = arr[--i], arr[i] = arr[j], arr[j] = x
	);
	return arr;
  }
})(jQuery);
