﻿var intCurrentIndex = 0;
var intMaxIndex;
var oInterval;
var bUp = true;
var runAutoSwitch = true;
function SwitchImage(intIndex) {
    $($('.counter li')[0]).children('a').removeClass('active');
	if (intIndex == intCurrentIndex) {
		return;
	}
	//$('.gallery ul li').hide();
	//$($('.gallery ul li')[intIndex]).show();
	var intSlideAmount = 0;
	//slide right
	
	if (intIndex < intCurrentIndex) {
		intSlideAmount = (intCurrentIndex - intIndex) * 862;
		$('.galleryarea ul.gallery').animate({
			left: '+=' + intSlideAmount
		}, 500);
	}
	else {
		intSlideAmount = (intIndex - intCurrentIndex) * 862;
		$('.galleryarea ul.gallery').animate({
			left: '-=' + intSlideAmount

        }, 500);
	}
	intCurrentIndex = intIndex;
	//visa vilken som är vald
	$('.counter li a').removeClass('active');
	$($('.counter li')[intIndex]).children('a').addClass('active');
}
function AutoSwitcher() {
	if (intCurrentIndex == intMaxIndex) {
		bUp = false;
	}
	else if (intCurrentIndex == 0) {
		bUp = true;
	}

	if (bUp) {
		SwitchImage(intCurrentIndex + 1);
	}
	else {
		SwitchImage(intCurrentIndex - 1);
	}
}


function GetCenterValuesHoriz(intDialogWidth) { //Returns the value position left depending on the width of window
            var windowWidth = $(window).width();
            var popupWidth = intDialogWidth;
            return (windowWidth / 2 - popupWidth / 2);
}
function GetCenterValuesVertic(intDialogHeight) { //Returns the value position top depending on the height of window
            var windowHeight = $(window).height();
            var popupHeight = intDialogHeight;
            return (windowHeight / 2 - popupHeight / 2);
}

function shadowBox(){
	//Show shadowbox
	$('#gallery li').click(function () {
		data = $(this).children('img').attr('data-film');
		if (data){
			clearInterval(oInterval);
			runAutoSwitch = false;
			$('#shadowWrapper').css({
				'display':'block'
			});
			$('#video').css({
			'display':'block',
			'top': GetCenterValuesVertic($('#video').height()),
			'left': GetCenterValuesHoriz($('#video').width())
			});
			$('#close').css({
				'display':'block'
			});
			id = $(this).children('img').attr('data-filmId');
			$.get('/roligast_ajax.html', function(data){
				$('#video .filmen').html(data);
			});
		}
	});
	
	//Closeshadowbox
	$('.close').click(function () {
			runAutoSwitch=true;
			oInterval = setInterval(AutoSwitcher, 5000);
			$('.galleryarea').css({'border':'3px solid #ffffff'});
			$('#shadowWrapper').css({'display':'none'});
	});

	//Centerar videobox
	$(window).resize(function () {
		$("#video").css({
		"top": GetCenterValuesVertic($("#video").height()),
		"left": GetCenterValuesHoriz($("#video").width())
		});
	});
	

}

function playButton(){
	$('.gallery li').each( function(data){
		data = $(this).children('img').attr('data-film');
		if(data){
			$(this).append("<div class='playButton' ></div>")
		}
	});
	
}

$(document).ready(function () {
    $($('.counter li')[0]).children('a').addClass('active');
    $('.counter li').click(function () {
        SwitchImage($(this).index());
    });
    intMaxIndex = $('.gallery li').length - 1;
    oInterval = setInterval(AutoSwitcher, 5000);
	
	$('.galleryarea').mouseenter(function(){
		clearInterval(oInterval);
		$(this).css({'outline':'1px solid #52aa54'});
	}).mouseleave(function(){
		if (runAutoSwitch){
			oInterval = setInterval(AutoSwitcher, 5000);
			$(this).css({'outline':'1px solid #ffffff'});
		}
	});
	shadowBox();
	playButton();

	
	
	/*
    $('#start').click(function () {
        if (intCurrentIndex > 0) {
            intSlideAmount = ($(this).index() - intCurrentIndex) * 778;
            $('.galleryarea ul.gallery').animate({
                left: '-=' + intSlideAmount
            }, 500);
            intCurrentIndex = 0;
            $($('.counter li')).children('a').removeClass('active');
            $($('.counter li')[intCurrentIndex]).children('a').addClass('active');
        }
    });

    $('#end').click(function () {
        if (intCurrentIndex < intMaxIndex) {
            intSlideAmount = (intMaxIndex - intCurrentIndex) * 778;
            $('.galleryarea ul.gallery').animate({
                left: '-=' + intSlideAmount
            }, 500);
            intCurrentIndex = intMaxIndex;
            $($('.counter li')).children('a').removeClass('active');
            $($('.counter li')[intCurrentIndex]).children('a').addClass('active');
        }
    });
	*/
});


