//FLASH slideshow
var flashOpenStatus = -1;
var flashOpenAfterDelay = 1000;
var flashClassOpened = 'flashOpened';
var flashClassClosed = 'flashClosed';
var flashToggler = '#flashButton';
var flashTransitionDuration = 500;
var flashContentHeight = 0;
var windowHeight = 0;
var resizeTimer = null;
var showCount = 0;

function onFlashOpened(){
	if(showCount == 1) sendToActionScript("opened");
}
function onFlashClosed(){
	if(showCount == 1) sendToActionScript("closed");
}

var jsReady = false;
function isReady() {
	return jsReady;
}
function pageInit() {
	jsReady = true;
}

function sendToActionScript(value) {
	if (navigator.appName.indexOf("Microsoft") != -1) window.imageStage.sendToActionScript(value);
	else document.imageStage.sendToActionScript(value);
}
function sendToJavaScript(value) {
	//alert("ActionScript says: " + value);
}

function flashSlideshowInit(){
	
	$("#slidesContainer").remove();
	
	swfobject.embedSWF("fileadmin/slideshow/ga-filmstrip.swf", "imageStage", "100%", "100%", "9.0.0", false, flashVars, flashParams);
	flashPositioning();
	if(flashOpenStatus > 0) onFlashOpened();
	else onFlashClosed();
	showCount = 1;
	$(function() {
		$(flashToggler).css('cursor','pointer');
		$(flashToggler).mouseover(
			function() {
				if(flashOpenStatus > 0){ // opened
					flashClose();
				} else if(flashOpenStatus < 0) { // closed
					flashOpen();
				}
			}
		);
	});
	if (flashOpenAfterDelay) {
		setTimeout(flashOpen, flashOpenAfterDelay);
	}
}

function flashOpen() {
	flashOpenStatus = 0; // in transition
	$("#flashContainer").animate({top: 40}, flashTransitionDuration); // open
	$("#flashContainer").addClass(flashClassOpened);
	$("#flashContainer").removeClass(flashClassClosed);
	statusTimer = $.timer(flashTransitionDuration,function(){
		// set to open after transistion
		flashOpenStatus = 1; 
		if(flashAvailable)onFlashOpened();
	});
}

function flashClose() {
	flashOpenStatus = 0; // in transition
	$("#flashContainer").animate({top: -flashContentHeight +100}, flashTransitionDuration); // close
	$("#flashContainer").addClass(flashClassClosed);
	$("#flashContainer").removeClass(flashClassOpened);
	statusTimer = $.timer(flashTransitionDuration,function(){
		// set to closed after transition
		flashOpenStatus = -1; 
		if(flashAvailable)onFlashClosed();
	});
}

function flashPositioning(){
	windowHeight = $(window).height();
	flashContentHeight = windowHeight - 188;
	$('#imageStage').css('height', flashContentHeight);
	$('#flashContainer').css('height',windowHeight -40);
	$('#flashContainer').css('width','100%');
	if(flashOpenStatus > 0){ // opened
		$('#flashContainer').css('top',20);
	} else if(flashOpenStatus < 0) { // closed
		$('div#flashContainer').css('top', 280 - windowHeight );
	}
	$('#flashContainer').css('display','block');
}


//JS slideshow
var imgArray = new Array();
var _index = 0;
var shownav = false;
var currArrowPrevColor = 'black';
var currArrowNextColor = 'black';
var slidesArray = new Array();
var currArray = new Array();
var slidePointsArray = new Array();
var currPos = 1;
var slideWidthMin = 0;
var slideWidthMax = -10000;
var currProject;
var newContainerWidth = 0;
var newWidth = 0;
var slideWidth = 0;
var slideInnerSlideWidth = 0;
var captionText = '';
var destinationX = 0;
var middle = 0;

function jsSlideshowInit(){
	
	$('#imageStage').css('height','40%');
	$('#imageStage').css('overflow','hidden');
	$('#imageStage').css('position','absolute');
	$('#imageStage').css('top','0');
	$('#imageStage').css('left','40px');
	$('#imageStage').css('right','0');
	$('#imageStage').css('bottom','0');

	flashPositioning();
	
	$(function() {
		$(flashToggler).css('cursor','pointer');
		$(flashToggler).mouseover(
			function() {
				if(flashOpenStatus > 0){ // opened
					flashClose();
				} else if(flashOpenStatus < 0) { // closed
					flashOpen();
				}
			}
		);
	});
	
	$('#slidesContainer').css('display','block');
	
	fillSlideArray();
		
	setSliderWidths();
	
	setNaviPosition();
	
	$('#prevCont').click(function(){slideImages('prev');});
	$('#nextCont').click(function(){slideImages('next');});
	
	$(".slide").mouseenter(function() {
	  $(this).find('p.captionText').css('display','block');
	  toggleNaviArrow('both', 'white');
	});
	
	$(".slide").mouseleave(function() {
	  $(this).find('p.captionText').css('display','none');
	  toggleNaviArrow('both', 'black');
	});
	
	$("#prevCont").mouseenter(function() {
	  toggleNaviArrow($(this), 'red');
	});
	
	$("#nextCont").mouseenter(function() {
	  toggleNaviArrow($(this), 'red');
	});
	
	flashOpen();
	
}

function toggleNaviArrow(btn, color){
	
	if(btn == 'both'){
		$('#prevCont').find('.'+currArrowPrevColor).css('display','none');
		$('#prevCont').find('.'+color).css('display','block');
		currArrowPrevColor = color;
		
		$('#nextCont').find('.'+currArrowNextColor).css('display','none');
		$('#nextCont').find('.'+color).css('display','block');
		currArrowNextColor = color;
	}else{
		if(btn.attr('id') == 'prevCont'){
			btn.find('.'+currArrowPrevColor).css('display','none');
			btn.find('.'+color).css('display','block');
			currArrowPrevColor = color;
		}else{
			btn.find('.'+currArrowNextColor).css('display','none');
			btn.find('.'+color).css('display','block');
			currArrowNextColor = color;
		}
	}
}

function setNaviPosition(){
	
	var marginTop = (Math.floor($("#slideInner").height()/2) - 49)+'px';
	
	$('#prevCont').css('top', marginTop);
	$('#nextCont').css('top', marginTop);
}

function fillSlideArray(){
	$('.slide').each(function() {
		slidesArray.push($(this));
	});
}

function setSliderWidths(){
	slidePointsArray = new Array();
	newContainerWidth = 0;
	
	$('#slideInner').width('10000px');
	
	for(var i = 0; i < slidesArray.length; i++){
								
		var	slide = slidesArray[i];		
		
		currArray = new Array();
		
		newWidth = 0;
		
		if(slide.find('img')[0] != undefined){
			newWidth = Math.floor(slide.find('img').width());
		}else if(slide.find('.slideText')[0] != undefined){
			newWidth = Math.floor(slide.find('.slideText').width());
		}else if(slide.find('video')[0] != undefined){
			newWidth = Math.floor(slide.find('video').width());
		}else if(slide.hasClass('text')){
			newWidth = setTextFontSize(slide);
		}
		
		slide.width(newWidth+'px');
		
		newContainerWidth += newWidth + 20;
		
		destinationX = Math.floor(slide.position().left)*-1;
	
		currArray.push(destinationX);
		currArray.push(slide.find('p.captionText').html());
		
		slide.find('p.captionText').width(newWidth-40);
		
		slidePointsArray.push(currArray);

		if(!slide.hasClass('text')){
			setCaptionFontSize(slide);
		}
	}
	$('#slideInner').width(newContainerWidth+'px');
	
	slideWidthMax = Math.floor(newContainerWidth*-1) + $('#slidesContainer').width();
	
	if(slidePointsArray[currPos-1]) slideInnerSlideWidth = (slidePointsArray[currPos-1][0]);
	
	if(slideInnerSlideWidth < slideWidthMax){
		slideInnerSlideWidth = slideWidthMax;
	}else if(slideInnerSlideWidth > slideWidthMin){
		slideInnerSlideWidth = slideWidthMin;
	}
	
	$('#slideInner').css('margin-left', slideInnerSlideWidth+'px');
	
	setNaviPosition();
}

function slideImages(direction){
	
	if(slidesArray.length <= 1) return;
	
	if(direction == 'prev'){
		currPos -= 1;
		
	}else if(direction == 'next'){
		currPos += 1;
	}
	
	if(currPos < 1) currPos = 1;
	if(currPos > slidePointsArray.length) currPos = slidePointsArray.length;
	
	slideWidth = slidePointsArray[currPos-1][0];
	//captionText = slidePointsArray[currPos-1][1];
	
	//if(slideWidth < slideWidthMax) slideWidth = slideWidthMax;
	
	$('#slideInner').stop().animate({
		'marginLeft' : slideWidth+'px'
		}, 700, 'easeOutQuad', function() {
			
			if(currPos == 1){
				$('#prevCont').css('display','none');
			}else{
				if($('#prevCont').css('display') == 'none') $('#prevCont').css('display','inline-block');
			}
			
			if(currPos == slidesArray.length){
				$('#nextCont').css('display','none');
			}else{
				if($('#nextCont').css('display') == 'none') $('#nextCont').css('display','inline-block');
			}	
	});
}

// Font-Scaling
function setCaptionFontSize(sn){
	var scaleSource = sn.width()+1;
	var scaleFac = 0.7;
	var fontSize = scaleSource*scaleFac;
	sn.css('font-size', fontSize + '%');
}

function setTextFontSize(sn){
	
	var checkString = '';
	checkString = sn.find('p').html();
	checkString = checkString.split('<br>');
	
	var scaleSource = checkString[0].length;
	
	for(var i = 0; i < checkString.length; i++){
		if(scaleSource < checkString[i].length) scaleSource = checkString[i].length;
	}
	
	scaleSource = ($('#imageStage').height()/410)*400;
	
	var scaleFac = 1;
	var fontSize = Math.round(scaleSource*scaleFac);
	sn.css('width', '1000px');
	sn.css('font-size', scaleSource + '%');
	
	return sn.find('p').width()+20;
}


// init the show 
if(flashAvailable){
	$(window).bind('load', function(){ flashSlideshowInit(); pageInit();});
	$(window).bind('resize', function() { if (resizeTimer) clearTimeout(resizeTimer); resizeTimer = setTimeout(flashPositioning, 200);});
}else{
	$(window).bind('load', function(){jsSlideshowInit(); });
	$(window).bind('resize', function() {flashPositioning(); setSliderWidths();});
}
