var newsCurrentId = 0;
var newsCursor = 0;
var newsItems;


$( function() {
	init();
	// setup news ticker
	newsInit();

	initBanners();

	if($('.slideshow').length > 0) {
		$('.slideshow #bigimg').cycle({
	    fx:     'fade',
	    speed:  'fast',
	    timeout: 0,
	    pager:  '.slideshow #thumbs',
	    pagerAnchorBuilder: function(idx, slide) {
	      // return selector string for existing anchor
	      return '.slideshow #thumbs img:eq(' + idx + ')';
	    }
		});
	}

	$('.back').click(function() {
		history.back();
		return false;
	});
/*
	$('.linky:has(a)').each(function() {
		var container = $(this);
		var link = container.find('a');

		container.click(function() {
			window.location = link.attr('href');
		}).css({cursor:'pointer'});
	});
*/
	$('#dropdownnav, #dropdownnav *').click(function(event) {
		$('#fullcat').slideDown('fast');
		$('#dropdownnav').hide();
		$('body').bind('click', hideCategories);
		event.stopPropagation();
	});

	$('a.lightbox').lightBox();
});

var language;

function init() {
		// get paths from URL
	var a = location.href.split('/');
	language = a[3];
	if (!language)
		language = 'en';
}

function hideCategories(event) {
	$('#fullcat').slideUp('fast');
	$('#dropdownnav').show();
	$('body').unbind('click', hideCategories);
	event.stopPropagation();
}

function initBanners()
{
	// temp kak
	/*
	$('.banner').each( function() {
		if (this.style.backgroundImage == 'url()')
		{
			this.style.backgroundColor = '#000';
			this.style.opacity = '0.1';
			this.style.cursor = 'default';
		}
	});*/
}

function newsInit()
{
	newsItems = $('#newsticker p');
	if (newsItems.length)
		setTimeout(newsShow, 500);
}

function newsShow()
{
	newsCursor++;
	var text = newsItems[newsCurrentId].innerHTML;
	$('#newsticker span')[0].innerHTML = text.substr(0, newsCursor);
	if (newsCursor < text.length)
		setTimeout(newsShow, 25);
	else
		setTimeout(newsHide, 5000);
}

function newsHide()
{
//	newsCursor--;
//	var text = newsItems[newsCurrentId].innerHTML;
//	$('#newsticker span')[0].innerHTML = text.substr(0, newsCursor);
//	if (newsCursor > 0)
//		setTimeout(newsHide, 25);
//	else
	{
		newsCursor = 0;
		newsCurrentId = (newsCurrentId + 1) % newsItems.length;
		setTimeout(newsShow, 250);
	}
}

function showListingMap(id)
{
	html =
		'<div id="map-overlay" style="z-index:9" onclick="hideListingMap();">' +
			'<div class="bg"></div>' +
			'<div class="map-wrapper"><img class="map" src="/images/listings/' + id + '/map.png" /></div>' +
			'<img class="marker" src="/images/touch/red-marker.png"/>' +
			'<div class="attribution">POWERED BY WWW.OPENSTREETMAP.ORG</div>' +
			'<div class="touch-anywhere" onclick="hideListingMap()"></div>' +
		'</div>';
	$(document.body).append(html);
	return false;
}

function hideListingMap()
{
	var $map = $('#map-overlay');
	$map.remove();

	return false;
}

function remote(content, params, callback)
{
	params._accountURL = '';
	params._language = language;
	params.content = content;
	$.get('/site-ajax.php', params, callback);
}

function registerUsage(action, listingId, otherId, url)
{
	remote(
		'usage',
		{
			action: action,
			listingId: listingId,
			otherId: otherId
		},
		function () {
			window.location = url;
		}
	);

	return false;
}
