
(function($) {
	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
})(jQuery);

function wbr(str, num) {
  return str.replace(RegExp("(\\w{" + num + "})(\\w)", "g"), function(all, text, num){
    return text + "<wbr>" + num;
  });
}


function theRotator() {
	$('.tx-kkrotator-pi1 ul li').css({opacity: 0.0});
	$('.tx-kkrotator-pi1 ul li:first').css({opacity: 1.0}).addClass('show');
	setInterval('rotate()',7000);
}

function rotate() {
	var current = ($('.tx-kkrotator-pi1 ul li.show')?  $('.tx-kkrotator-pi1 ul li.show') : $('.tx-kkrotator-pi1 ul li:first'));
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('.tx-kkrotator-pi1 ul li:first') :current.next()) : $('.tx-kkrotator-pi1 ul li:first'));	

	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 2000);

	current.animate({opacity: 0.0}, 2000)
	.removeClass('show');
};

function fetch_twits(options) {
	var defaults = {
		l: 'lv'
	};
	var settings = $.extend({}, defaults, options);
	$.getJSON('/twitter_' + settings.l + '.php', function(data) {
		var i = 0;
		for (i = 0; i < data.length; i++)
		{
			t = data[i].text;
			
			t = t.replace(/((?:http:\/\/|https:\/\/)(?:(?:[a-z0-9\&\.?=\-_\[\]\/])*))/gi,"<a href=\"$1\">$1</a>");
			t = wbr(t, 6);
			$('.twitter .entries_' + settings.l).append('<div><p>'+t+'</p></div>');
		}
		$('.twitter .entries_' + settings.l).jScrollPane();
		$('.twitter .entries_' + settings.l + ' a').each(function(){
			link = $(this);
			link.attr('href', link.attr('href').replace(/<wbr>/gi,""));
		});
	});	
}

$(document).ready(function() {
	theRotator();
	if($('.entries_lv').length != 0) fetch_twits({ l: 'lv'});
	if($('.entries_ru').length != 0) fetch_twits({ l: 'ru'});
	//if($('.twitter').length != 0) fetch_twits();
	$('.menu ul').lavaLamp({ fx: "jswing", speed: 400 });
	$('.advertblock')
	.mouseover(function(){
		$(this).stop().animate({backgroundPosition:"(0px 0px)"}, {duration:500})
	})
	.mouseout(function(){
		$(this).stop().animate({backgroundPosition:"(-200px 0px)"}, {duration:200, complete:function(){
			$(this).css({backgroundPosition: "-200px 0px"})
		}})
	});
	$('.rounded').append('<div class="tl"></div><div class="tr"></div><div class="bl"></div><div class="br"></div>');
	
	// if($('#scrollable').length != 0) $('#scrollable').scrollable({circular: true}).autoscroll({
	//	interval: 5000,
	// 	autoplay: true
	// });
	
	 
	// if($('#scrollablenoloop').length != 0) $('#scrollable').scrollable();
	
	/*
	
	 */
	
	$('a.moreinfo[rel!=external]').click(function(e){
		$('.foldout').slideToggle();
		e.preventDefault(e);
	});
	
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
	    //return '<div id="tip7-title">'+$('.scrollable .items div:not(.cloned) a:eq('+currentIndex+') strong').text()+' – '+$('.scrollable .items div:not(.cloned) a:eq('+currentIndex+') span').text()+'</div>';
	}
	
	$('.scrollable').scrollable({circular: true}).autoscroll({ autoplay: true });
	
	$('a.map').fancybox({
		'type' 	: 'iframe',
		'width'	: 700,
		'height': 450
	});
	$("a[href$=.jpg],a[href$=.png],a[href$=.gif]").fancybox();
	


}); 

DD_belatedPNG.fix('.logo h1 a');