var _intervalId, _currentHash;

function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
	//$('#loadto').hide();
	//$(window).bind('hashchange', init);
	$("a.iframe").fancybox({ height: 560 });
	
	/*$('#left a').bind('click', function(evt) {
		evt.preventDefault();
		if($(this).get(0).pathname.charAt(0) === '/') {
			location.hash = $(this).get(0).pathname.substring(1).slice(0, -5);
		} else {
			location.hash = $(this).get(0).pathname.slice(0, -5);
		}
	});*/
	
	$('.takeaway').live('click', function(event) {
		event.preventDefault();
		var $this = $(this);
		if($this.attr('rel') == 'wholepage') {
			var $price = $('.price');
			var $taprice = $('.taprice');	
		} else {
			var $price = $this.next().find('.price');
			var $taprice = $this.next().find('.taprice');
		}
		
		if($(this).text() == 'Klicka för avhämtningspriser') {
			$price.hide();
			$taprice.show();
			$this.text('Klicka för restaurangpriser');
		} else {
			$price.show();
			$taprice.hide();
			$this.text('Klicka för avhämtningspriser');
		}
		return false;
	});
	
	init();
});

function init() {
	/*
	if(!location.hash) {
		if(location.pathname.charAt(0) === '/') {
			var page = location.pathname.substring(1).slice(0, -5);
		} else {
			var page = location.pathname.slice(0, -5);			
		}
		if(page) {
			document.location.href = document.location.href.replace(location.pathname, '#' + page); 
		} else {
			location.hash = 'index';
			_currentHash = 'index';
		}
	}

	if(location.hash != _currentHash) {
		if(location.hash.charAt(0) === '/') {
			var page = location.hash.substring(1) + '.html';
		} else {
			var page = location.hash + '.html';
		}
		_currentHash = location.hash;
		$('#loadto').fadeOut(function(){
			$('#loadto').load(page + '?ajax=1 #loadfrom', function(){
				$('#left li').removeClass('current');
				$('a[href^="' + page + '"]').parent('li').addClass('current');
				_gaq.push(['_trackPageview', '/' + page]);
				FB.XFBML.parse( );
				$('#loadto').fadeIn();
			});			
		});
	}*/
	
	if($('#slideshow').length && !_intervalId)
		_intervalId = setInterval( "slideSwitch()", 5000);

}
