var cache = [];
var spd = 1000;
var tickerSpd = 4000;

if (typeof console == 'undefined') {
	console = function(){}
	console.log = function(){}
	console.warn = function(){}
} else console.log('Console Active');

$.fn.preLoadImages = function() {
	var args_len = arguments.length;
	for (var i = args_len; i--;) {
		var cacheImage = document.createElement('img');
		cacheImage.src = arguments[i];
		cache.push(cacheImage);
	}
}

$.fn.stepcarouselNav = function(galleryid) {
	$(this).css({'cursor':'pointer'}).data('galleryid',galleryid);
	if ($(this).hasClass('left')||$(this).hasClass('right')) {
		$(this).data('bgImg',$(this).css('background-image'));
		$(this).css({'background-image':'none'});
	}
	$(this).mouseenter(function(e){
		if ($(this).hasClass('left')||$(this).hasClass('right'))
			$(this).css({'background-image':$(this).data('bgImg')});
	}).mouseleave(function(e){
		if ($(this).hasClass('left')||$(this).hasClass('right'))
			$(this).css({'background-image':'none'});
	}).click(function(e){
		e.preventDefault();
		var step = 0;
		if ($(this).hasClass('left'))
			step = -1;
		else if ($(this).hasClass('right'))
			step = 1;
		else {
			stepcarousel.stepTo($(this).data('galleryid'),$(this).attr('move'));
			return true;
		}
		stepcarousel.stepBy($(this).data('galleryid'),step);
		return true;
	});
}

function tickerRotate() {
	var tickerWidth = parseInt($('#tickernews').find('.panel:first').outerWidth());
	$firstPanel = $('#tickernews').find('.panel:eq(0)').clone()
	$('#tickernews').find('.belt').append($firstPanel);
	$('#tickernews').find('.belt').animate({left:-tickerWidth+'px'},tickerSpd/2,function(){
		$(this).css({left:'0px'})
			.find('.panel:first').remove();	
	});
}

$(function(){
	$leftcol = $('#leftcol').not('#homearealeft').not('#homearearight')
	$leftcol.find('.blockcentre').each(function(){
		if (!$(this).prev('.title3').length) {
			var $obj = $(this);
			for (var i=0;i<$(this).siblings('div').length;i++) {
				if ($obj.prev('.blockbase2').length) {
					$obj.prev('.blockbase2').remove();
					break;
				} else $obj = $(this).prev();
			}
		}
	});
	$leftcol.find('.title3').each(function(){
		if ($(this).next('.title3').length||$(this).next().next('.title3').length)
			$(this).after('<div class="blockbase2">');
	});
	$('#tickernews').find('.belt').show();
	$('#tickernews').css({overflow:'hidden',position:'relative',height:$('.belt .panel:first',this).height()})
		.find('.belt').css({width:(parseInt($('#tickernews').width())*($(this).find('.panel').length+1))+'px',position:'absolute'})
		.find('.panel').css({float:'left',width:$('#tickernews').width()+'px'});
		//.find('.panel:gt(0)').hide();
	ticker = setInterval(tickerRotate,tickerSpd);
	$('#tickernews').bind('mouseover.pause',function(e){clearInterval(ticker)})
		.bind('mouseleave.pause',function(e){ticker = setInterval(tickerRotate,tickerSpd);});
});
