if (!Array.prototype.join) {
	Array.prototype.join = function(sep) {
		var len = this.length;
		var res = '';
		for (var i = 0; i < len; i++) {
			res += this[i] + ((i < len - 1) ? sep : '');
		}
		
		return res;
	}
}

function trackGoogle() {
	var typ = $('#slReception').val();
	var ort = $('#slCity').val();
	
	if (typ && typ.indexOf('_') > -1)
		typ = typ.split('_')[0];
		
	if ("console" in window && console.log)
		console.log("Tracking", typ, ort);
		
	_gaq.push(['_trackEvent', 'Hitta_mottagning', typ, ort]);
}

var loadTimeout = null;
$(function() {
	$('#slReception, #slCity').uniform();
	$('#slReception').change(function() {
		if ($(this).val() == '-1')
			return;
			
		var recInfo = $(this).val().split('_');
		loadTimeout = setTimeout(getCities_ShowLoader, 150);
		$.get(FindUnitConfig.citiesPortletURL, { reception: recInfo[1], type: recInfo[0] }, getCities_Complete);
	});
	
	$('#btnShowReceptions').click(function() {
		var slCity = $('#slCity');
		var slReception = $('#slReception');
		
		var type = slReception.val();
		if (type == '-1') return;
		var url = '/' + type.split('_')[1] + '.html';
		var ort = slCity.val();
		var ortIndex = slCity.get(0).selectedIndex;
		
		window.location.href = url + '?' + $.param({
			type: type,
			ort: ort,
			ortIndex: ortIndex
		});
	});
	
	$('.frontpage-button a[title!=""]').each(function() {
		var me = $(this);
		var title = me.attr('title');
		me.attr('title', me.text());
		
		me.click(function() {
			_gaq.push(['_trackEvent', title]);
		});
	});
	
	var isFirst = true,
		cItemIndex = 0,
		bullets = $('<div class="bullets"></div>');
		
	$('.bildspel-container').append(bullets);
		
	var items = $('.bildspel-container .bildspel-item').each(function() {
		var item = $(this);
		if (!isFirst)
			item.hide();
			
		bullets.append('<span class="bullet' + (isFirst ? ' bullet-filled' : '') + '"></span>');
		
		isFirst = false;
	});
	
	/*bullets.css({
		left: '50%',
		marginLeft: -bullets.width()/2
	});*/
	
	bullets = bullets.find('.bullet');
	
	var showNextItem = function() {
		var nextIndex = cItemIndex + 1;
		if (nextIndex >= items.length)
			nextIndex = 0;
			
		var next = items.eq(nextIndex);
		var curr = items.eq(cItemIndex);
		
		var cBull = bullets.eq(cItemIndex);
		var nBull = bullets.eq(nextIndex);
		
		next.hide();
		next.css('zIndex', 3);
		curr.css('zIndex', 2);
		
		next.fadeIn('normal', function() {
			curr.hide();
		});
		
		cBull.toggleClass('bullet-filled');
		nBull.toggleClass('bullet-filled');
		
		cItemIndex = nextIndex;
	};
	
	setInterval(showNextItem, 10000);
});

function getCities_ShowLoader() {
	var slCity = $('#slCity');
	slCity.empty().html('<option value="alla order">Laddar...</option>');
}

function getCities_Complete(json) {
	if (loadTimeout) {
		clearTimeout(loadTimeout);
		loadTimeout = null;
	}
	
	var data = eval('(' + json + ')');
	var slCity = $('#slCity');
	if (data) {
		slCity.empty();
		for (var i = 0; i < data.length; i++)
			data[i] = '<option value="' + data[i] + '">' + data[i] + '</option>';
			
		slCity.append('<option value="alla orter">Alla orter</option>' + data.join(''));
	}
}
