var Slideshow = {
	div: null,
	index: 0,
	numSlides: 21,//22,
	
	init: function() {
		Slideshow.div = $('#slideshow');
		if (!Slideshow.div.length) return;
		$('<div />').attr('id', 'arrowleft').bind('click', Slideshow.leftHandler).appendTo(Slideshow.div);
		$('<div />').attr('id', 'arrowright').bind('click', Slideshow.rightHandler).appendTo(Slideshow.div);
		Slideshow.polish(0);
	},
	
	leftHandler: function(e) {
		e.preventDefault();
		var i = Slideshow.index;
		Slideshow.moveTo((i > 0 ? i - 1 : Slideshow.numSlides - 1));
	},
	
	rightHandler: function(e) {
		e.preventDefault();
		var i = Slideshow.index;
		Slideshow.moveTo((i < Slideshow.numSlides - 1 ? i + 1 : 0))
	},
	
	moveTo: function(index) {
		if (index == Slideshow.index) return;
		Slideshow.index = index;
		Slideshow.div.children('.content').fadeOut(500, Slideshow.fadeOutComplete);
		var slide = Slideshow.indexToSlide(index);
		if (!slide || !slide.length) return;
		var content = $('<div />').addClass('content').append(slide);
		if (slide[0].nodeName == 'IMG') {
			slide.bind('load', Slideshow.imageLoaded);
			content.css('visibility', 'hidden');
		}
		else content.hide().fadeIn(500);
		Slideshow.div.append(content);
		Slideshow.polish(index);
	},
	
	imageLoaded: function(e) {
		$(e.target).parent().css('visibility', 'visible').hide().fadeIn(500);
	},
	
	fadeOutComplete: function() {
		$(this).remove();
	},
	
	indexToSlide: function(index) {
		switch (index) {
		case 0:			return $('<div />').addClass('videoPlayer').append($('<div />').attr('id', 'trailercontainer'));
		//case 21:		return $('<form />').attr('action', 'smspaminnelse.php').attr('method', 'post').append($('<div />').addClass('smsform').append($('<input />').attr('type', 'text').attr('name', 'smsnumber').attr('id', 'smsnumber').attr('size', 15).attr('maxlength', 20)).append($('<input />').attr('type', 'image').attr('id', 'smssendbutton').attr('src', 'images/button_send_sms_form.png').attr('alt', 'Skicka')));
		default:
			if (index >= 1 && index <= 20) return $('<img />').attr('src', 'images/slides/' + index + '.jpg').attr('alt', 'Bild ' + (index + 1));
			else return null;
		}
	},
	
	polish: function(index) {
		if (index == 0) swfobject.embedSWF('http://www.tv4play.se/polopoly_fs/1.939636!approot/tv4video.swf?autoload=false&startupView=views/superspelaren_whitelabel.xml&vpId=0&videoId=1.1368369&width=690&height=422', 'trailercontainer', '690', '422', '9.0.0', null, null, {bgcolor: '#000000', allowfullscreen: 'true', allowScriptAccess: 'always', base: 'http://www.tv4play.se/polopoly_fs/1.939636!approot/'});
		/*else if (index == 21) {
			Slideshow.div.pngFix({blankgif: 'images/blank.gif'});
			$('#smsnumber').focus();
		}*/
	}
};

var background = null;
var backgroundWidth = 0;
var backgroundHeight = 0;

function setupBackground() {
	if ((jQuery.browser.msie && jQuery.browser.version == "6.0") || (jQuery.browser.mozilla && jQuery.browser.version.substr(0, 3) == "1.8"))
		$(document.body).css('background-image', 'url(images/background.jpg)').css('background-attachment', 'fixed').css('background-repeat', 'no-repeat').css('background-position', 'top center');
	else
		background = $('<img />').attr('src', 'images/background.jpg').attr('alt', 'Bakgrundsbild').attr('id', 'background').load(backgroundLoaded);
}

function backgroundLoaded() {
	background.prependTo($(document.body));
	backgroundWidth = background.width();
	backgroundHeight = background.height();
	resizeBackground();
	$(window).bind('resize', resizeBackground);
}

function resizeBackground() {
	var w = $(window).width(), h = $(window).height();
	var wr = w / backgroundWidth, hr = h / backgroundHeight;
	
	if (wr > hr) {
		background.css('width', Math.round(w));
		background.css('height', Math.round(backgroundHeight * wr));
	}
	else {
		background.css('width', Math.round(backgroundWidth * hr));
		background.css('height', Math.round(h));
	}
	
	background.css('left', Math.round((w - parseInt(background.css('width'))) * 0.5));
	background.css('top', Math.round((h - parseInt(background.css('height'))) * 0.5));
}

function initSite() {
	Slideshow.init();
	$(document).pngFix({blankgif: 'images/blank.gif'});
	setupBackground();
}

$(document).ready(initSite);