var boxCount = 0;
var boxPerPage = 5;
var boxWidth = 195;
var lastBox;
var scrollPosition = 0;
var wheelTotal = 0;
var lastWheel=0;
var currentSlide=false;
var currentPlayIntro = false;

$(document).ready(function() {
    $('#next_btn').click(next);
    $('#prev_btn').click(prev);
    var containerWidth = 0;
    $('.box').each(function(i) {
	boxCount++;
	containerWidth+= boxWidth;
    });
    containerWidth+=400;
    $('#container').width(containerWidth);
    $('#container .box_container').click(onBoxClick);
    $('#button').click(onClickTopBtn);

    showAndHideButtons();
    initSlider();

    //Hide BOX:
    if ($('#showBoxAtStart').val() == 'false' ){
	$('#bg').css('height',0);
	$('#button').css('bottom',49);
	/*
	setTimeout(function(){
	    $('#bg').animate({
		height: 233
	    },1400);
	    $('#button').animate({
		bottom: 282
	    },1400);
	},2000);
	*/
    }
    
    //Scroll:
    initMouseWheel();
});

function initSlider(){
    var flashvars = {};
    if($('#startContentType').val() == "nothing" || $('#startContentType').val() == undefined){
	flashvars.data = $('#container .box:first').children('.data').val();
	flashvars.type = $('#container .box:first').children('.type').val();
	flashvars.autostart = 'n';
	selectBox($('#container .box_container:first'));
    }else if ($('#startContentType').val() == 'slideshow'){
	flashvars.data = $('#startContentSlideshow').val();
	flashvars.type = 'image';
	flashvars.autostart = 'y';
    }else if ($('#startContentType').val() == 'video'){
	flashvars.data = $('#startContentVideo').val();
	flashvars.type = 'video';
	flashvars.autostart = 'y';
    }
	
    //Starttext:
    if ($('#startText').val() != undefined){
	flashvars.startText = $('#startText').val();
	if ($('#showStartClock').val() != undefined){
	    flashvars.startClock = $('#showStartClock').val();
	}
    }
	
	
    var params = {
	scale: "exactFit"
    };
    params.allowFullScreen = "true";
    params.menu = "false";
    params.allowScriptAccess = "true";
    params.wmode = "opaque";
    params.bgcolor = "#FFF";
    var attributes = {};
    attributes.id = "flash";
    attributes.wmode = "opaque";

    swfobject.embedSWF("/fileadmin/template/swf/main.swf", "flash", "100%", "100%", "9.0.124", false, flashvars, params,attributes);


    //Button Startseite Skip
    if ($('#showBoxAtStart').val() == 'false'){
	$('#button').html('skip');
	currentPlayIntro = true;
    }

}

function initMouseWheel(){
    if ($('#container').length >0){
	$('#container').mousewheel(function(objEvent, intDelta){
	    if (intDelta > 0){
		wheelTotal++;
	    }else if (intDelta < 0){
		wheelTotal--;
	    }
	    if (wheelTotal>lastWheel+6){
		prev();
		lastWheel = wheelTotal;
	    }
	    if (wheelTotal<lastWheel-6){
		next();
		lastWheel = wheelTotal;
	    }
	});
    }
}


function prev(){
    if (scrollPosition>0){
	$('#container .box_container').animate({
	    left: '+='+boxWidth
	    },600);
	scrollPosition--;
    }
    hideDetails();
    showAndHideButtons();
}
function next(){
    if (scrollPosition<boxCount-boxPerPage){
	$('#container .box_container').animate( {
	    left:  '-='+boxWidth
	    },600);
	scrollPosition++;
    }
    hideDetails();
    showAndHideButtons();
}

function scrollTo(num){
    //$('#button').html(num);
    $('#container .box_container').animate( {
	left: '-='+(num-scrollPosition)*boxWidth
	},600);
    scrollPosition = num;
    showAndHideButtons();
}

function showAndHideButtons(){ 
    if (scrollPosition>0){
	$('#prev_btn').css('display','block');
    }else {
	$('#prev_btn').css('display','none');
    }
    if (scrollPosition<boxCount-boxPerPage){
	$('#next_btn').css('display','block');
    }else {
	$('#next_btn').css('display','none');
    }
}

function onBoxClick(event){
    //$('#button').html($(event.currentTarget).attr('class'));
    if (currentSlide==false){
	if (!$(this).children('.box').hasClass("selected")){
	    selectBox(this);
	    //setFlashData(this);
	}
	if (!$(this).children('.box').hasClass("extended")){
	    //scrollTo($('#content .box').index( $(event.target).parent() ));
	    scrollTo($('#content .box_container').index( $(event.currentTarget)));
	    showDetails(this);
	}
    }
}

function selectBox(target) {
    if (lastBox) {
	//$(lastBox).children('.box').removeClass("selected");
	hideDetails();
    }
    $(target).children('.box').addClass("selected");
}

function showDetails(target) {
    hideCursorPointer();
    currentSlide = true;

    hideDetails();
    lastBox = target;

    $(target).children('.box').addClass("extended");
    $(target).children('.box').animate({
	'margin-right': '400px'
    },600, function(){
	showCursorPointer();
	currentSlide = false;
	if ($(target).children('.box').hasClass("extended")){
	    setFlashData(target);
	    $(target).children('.extended_text').css('display','block');
	}
    });
}
function hideDetails() {
   // alert (lastBox);
    if (lastBox) {
	$(lastBox).children('.box').removeClass("selected");
	$(lastBox).children('.box').removeClass("extended");
	$(lastBox).children('.extended_text').css('display','none');
	$(lastBox).children('.box').animate({
	    'margin-right':'20px'
	},600);
    }
    lastBox = null;
}

function showCursorPointer(){
    $('.overlay').css('cursor','pointer');
}

function hideCursorPointer(){
    $('.overlay').css('cursor','default');
}


function setFlashData(target){
    var type = $(target).find('.type').val();
    var data = $(target).find('.data').val();
    thisMovie('flash').setContent(type, data, 'n');
}

//TOP BUTTON:
function onClickTopBtn(event){
    //Start Intro Skip:
    if (currentPlayIntro==true){
	currentPlayIntro=false;
	$('#button').html('news');
	openBox();
	$('#container .box_container:first').click();
    }else {
	if ($('#bg').height() >0 ){
	    $('#bg').animate({
		height: 0
	    },600);
	    $('#button').animate({
		bottom: 49
	    },600);
	    $('#button').addClass('down');
	}else {
	    $('#bg').animate({
		height: 233
	    },600);
	    $('#button').animate({
		bottom: 282
	    },600);
	    $('#button').removeClass('down');
	}
    }
    
}

//GET FLASH CONTAINER:
function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
	return window[movieName];
    } else {
	return document[movieName];
    }
}

function openBox() {
    $('#bg').animate({
	height: 233
    },1400);
    $('#button').animate({
	bottom: 282
    },1400);
}
function closeBox() {
    $('#bg').animate({
	height: 0
    },1400);
    $('#button').animate({
	bottom: 49
    },1400);
}


// Flash Dispatches:

function onFlaVidPlay() {
    /* alert ('onFlaVidPlay'); */
    closeBox();
}

function onFlaVidComplete() {
    //alert ('onFlaVidComplete');
    openBox();

    if ($('#showBoxAtStart').val() == 'false'){
	$('#container .box_container:first').click();
	$('#button').html('news');
	currentPlayIntro=false;

	//showDetails($('#container .box_container:first'));
	//selectBox($('#container .box_container:first'));
	//scrollTo($('#content .box_container').index( $('#container .box_container:first') ));
    }
}

function onFlaContentLoaded() {
alert ('onFlaContentLoaded');
}
