/**
* jQuery slide rotator plugin.
* How to call:
* 		$("#parent-div").rotator(".class-that-all-children-have");
* Or if you need to set some settings...
* 		$("#parent-div").rotator(".class-that-all-children-have", { delay: 1000, fadeSpeed: 500 } );
*/
(function($){
   $.fn.rotator=function(childSelector, _opt){
	   var opt=$.extend({
		   delay:		5500,
		   fadeSpeed:	"slow",
		   last:		-1,
		   current:	0
	   }, _opt);
	   var parent=$(this);
	   var children=$(childSelector, parent[0]);
	   children.hide();
	   var _rotateNext=function(){
		   if(opt.last>=0) $(children[opt.last]).fadeOut(opt.fadeSpeed);
		   $(children[opt.current]).fadeIn(opt.fadeSpeed);
		   opt.last=opt.current;
		   opt.current=(opt.current+1)%children.length;
	   };
	   setInterval(_rotateNext, opt.delay);
	   _rotateNext();
	   return this;
   };
})(jQuery);
$(document).ready(function()
{	
	if(swfobject)
	{
		$('.flashcontent').each(function(i)
		{
			var rel=$(this).attr("rel");
			if(rel)
			{
				var parts=rel.split(";");
				if(!this.id) this.id="swf-"+i;
				swfobject.embedSWF(parts[0], this.id, parts[1], parts[2], "9.0.0",null,(parts.length==4?parts[3]:null));
			}
		});
	}
	if($.fn.slimbox)
	{
		$("a[rel^='lightbox']").slimbox({counterText: 'Kuva {x} / {y}'}, null, function(el) {
			return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
		});
	}
	if($.fn.fancyZoom) $('.flash_link').fancyZoom();
	
	$(".has_submenu").hover(
		function(){
			var target = $('#' + $(this).attr("id") + '_submenu');
			if(!target.data("timer"))
			{
				var z=target;
				var timerId=setTimeout(function(){
					z.data("timer", null).fadeIn("fast");
				}, 250);
				z.data("timer", timerId);
			}
		},
		function(){
			if(!$(".active_submenu",this).length)
			{
				var target=$('#' + $(this).attr("id") + '_submenu');
				if(target.data("timer")) clearTimeout(target.data("timer"));
				target.data("timer", null).fadeOut("fast");
			}
		}
	);

	$("select.template-theme-select").change(function () {
		$("option:selected", this).each(function () {
			var id = $(this).val();
			$('.active-preview', $(this).closest("div.template-preview")).removeClass('active-preview');
			$("#"+id).addClass('active-preview');
		});
	  
	})
	.change();

});
