jQuery.fn.reverse = function() {
	return this.pushStack(this.get().reverse(), arguments);
};

function resizeArtists(newHeight){
	$('#artistsFlash').css('height', newHeight + 'px');
}


(function($){
	$.fn.shuffle = function() {
		return this.each(function(){
			var items = $(this).children();
			return (items.length) ? $(this).html($.shuffle(items)) : this;
		});
	}
	$.shuffle = function(arr) {
		for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
		return arr;
	}
})(jQuery);

jQuery.fn.toggleEach = function(options) {
	// Default settings
	var settings = jQuery.extend({
		delay:     4000,  // How long each image will be shown (in ms)
		fadeSpeed: 200,   // Duration of fade (in ms)
		randomize: true   // Randomize images
	}, options);

	if(settings.randomize){
		jQuery(this).shuffle();
	}

	//jQuery(this).children().css('position', 'absolute');
	jQuery(this).children().hide();
	jQuery(this).children().eq(0).show();
	jQuery(this).children().eq(0).css('z-index', 1);

	this.toggleEachIndex = 0;
	this.toggleEachNext = function(){
		var previousIndex = this.toggleEachIndex % (jQuery(this).children().length);
		this.toggleEachIndex += 1;
		var arrayIndex = this.toggleEachIndex % (jQuery(this).children().length);
		var nextImage = jQuery(this).children().eq(arrayIndex);
		nextImage.css('z-index', (this.toggleEachIndex + 1));
		nextImage.fadeIn(settings.fadeSpeed);
		jQuery(this).children().eq(previousIndex).fadeOut(settings.fadeSpeed);
	}
	var container = this;
	this.toggleEachInterval = setInterval(function(){
		container.toggleEachNext();
	}, settings.delay);
}
// center backgrounds plz
function fixJoggingBug(){
	var width = ($(window).width() < 1080) ? 1080 : $(window).width();

	$('#top, #middle, #bottom').each(function(){
		var elemWidth = (this.id == 'top') ? 1280 : 1080;
		if($(this).css('background-position')) {
			var backgroundPosition = $(this).css('background-position').split(/\s+/);
			backgroundPosition[0] = Math.round((width - elemWidth)/2).toString() + 'px';
			$(this).css('background-position', backgroundPosition.join(' '));
		}
	});
	
	$('#main, #footer').each(function(){
		$(this).css('margin-left', Math.round((width - 1080)/2).toString()+'px');
	});
	
}
$(window).resize(fixJoggingBug);
$(document).ready(fixJoggingBug);

// hax
function openSubNav(){
	$('#nav li.info').addClass("selected");
	$('#subnav').show();
}
function closeSubNav(){
	$('#subnav').hide();
	$('#nav li.info').removeClass("selected");
}

$(document).ready(function(){
	$('#sponsorLoop').toggleEach();
	$('#header .logo').click(function(){
		document.location = "http://slottsfjell.no";
	});
	$('.post:first, .scene:first').css({
		'margin-top': '0',
		'padding-top': '0',
		'background-image': 'none'
	});
	$('#livetwitter').liveTwitter("slottsfjell OR slottsfjellfestival OR slottsfjellfestivalen", {limit: 8});
	// refresh captcha
	$(".captcha_refresh").click(function(){
		Recaptcha.reload();
	});
	$('.comments tr:odd td').addClass('zebra');
	
	// hover
	$('#nav li.info, #subnav').hover(function(){
		openSubNav();
	}, function(){
		closeSubNav();
	});
	
	// sharing
	$('a.facebook').click(function(){
		window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(this.href)+'&t='+encodeURIComponent(document.title),'sharer','toolbar=0,status=0,width=626,height=436');
		return false;
	});
	$('a.twitter').click(function(){
		document.location = "http://twitter.com/home?status="+encodeURIComponent(document.title)+': '+encodeURIComponent(this.href);
		return false;
	});
});

