var $j = jQuery.noConflict();

var $mode;
var $currentImage;
var $hasSlideshow;
var $slideshowData;
var $slideshowMap;
var $numberOfContentAttributes = 6;
var $index;
var $linkindex;
var $slideshowFadeTime = 500;
var $slideshowContentOffset = -40;
var $downloadContentOffset = 0;
var $activeBox = false;

function initSlideshow(theData, theMap) {
	$slideshowData = theData;
	$slideshowMap = theMap;
}

var $downloadData;
var $numberOfDownloadAttributes = 5;

function initDownloadBox(theDownloadData) {
	$downloadData = theDownloadData;
}

$j(document).ready(documentReady);

function documentReady(){

/////////////// boxes ///////////////
$j('#boxes').children('.box').hover(function(){
	$activeBox = true;
	$j(this).animate({backgroundColor: '#ffe75d'}, 'fast');
	$j(this).children('.boxtext').animate({opacity: 1.0}, 'fast');
	$j(this).children('.boxlink').animate({opacity: 1.0}, 'fast');
	//$j(this).children('.boximage').animate({opacity: 1.0}, 'fast');
	},function(){
	$activeBox = false;
	$j(this).animate({backgroundColor: '#fff'}, 'fast');
	$j(this).children('.boxtext').animate({opacity: 0.0}, 'fast');
	$j(this).children('.boxlink').animate({opacity: 0.0}, 'fast');
	//$j(this).children('.boximage').animate({opacity: 0.3}, 'fast');
});

$j('#boxes').children('.box2').hover(function(){
	$activeBox = true;
	$j(this).animate({backgroundColor: '#ffe75d'}, 'fast');
	$j(this).children('.boxtext').animate({opacity: 1.0}, 'fast');
	$j(this).children('.boxlink').animate({opacity: 1.0}, 'fast');
	//$j(this).children('.boximage').animate({opacity: 1.0}, 'fast');
	},function(){
	$activeBox = false;
	$j(this).animate({backgroundColor: '#fff'}, 'fast');
	$j(this).children('.boxtext').animate({opacity: 0.0}, 'fast');
	$j(this).children('.boxlink').animate({opacity: 0.0}, 'fast');
	//$j(this).children('.boximage').animate({opacity: 0.3}, 'fast');
});

$j('#boxes').children('.boxstatic').hover(function(){
	$j(this).children('.boxtext').animate({opacity: 1.0}, 'fast');
	//$j(this).children('.boximage').animate({opacity: 1.0}, $slideshowFadeTime);
	},function(){
	$j(this).children('.boxtext').animate({opacity: 0.0}, 'fast');
	//$j(this).children('.boximage').animate({opacity: 0.0}, $slideshowFadeTime);
});

$j('#boxes').children('.boxstatic').hover(function(){
	$j(this).children('.boximagebw').animate({opacity: 0.0}, $slideshowFadeTime);
	},function(){
	$j(this).children('.boximagebw').animate({opacity: 1.0}, $slideshowFadeTime);
});

$j('.boxlink').each(function(ind,obj){
	$j(this).animate({opacity: 0.0}, 0);
});

$j('.boxtext').each(function(ind,obj){
	$j(this).animate({opacity: 0.0}, 0);
});

$j('.boxblank').each(function(ind,obj){
	var $height = 150 + Math.random() * 100;
	$j(this).css("height", $height+'px');
});

$j('.boxlink .boxlinklink').click(function(event) {
	$linkindex = parseInt($j(this).attr("id"));
});

$j('.boxlink .externallink').click(function(event) {

});

$j('.box').click(function(event) {
	var attr = $j(this).attr("id");
	var id_string = attr.split("_");
	$mode = id_string[0];
	var id = id_string[1];
	if ($mode == "slideshow") {
		openSlideShow(id);
		showSlideshowElements();
	}
	if ($mode == "download") {
		openDownloadModule(id);
		showDownloadModuleElements();
	}
});

$j('.box2').click(function(event) {
	var attr = $j(this).attr("id");
	var id_string = attr.split("_");
	$mode = id_string[0];
	var id = parseInt(id_string[1]);
	if ($mode == "slideshow") {
		openSlideShow(id);
		showSlideshowElements();
	}
	if ($mode == "download") {
		openDownloadModule(id);
		showDownloadModuleElements();
	}
});

/////////////// masonry ///////////////
$j('#boxes').masonry({
});

$j(window).scroll(function () { 
	keepCentered();
});

$j(window).resize(function() {
	keepCentered();
});

/////////////// slideshow ///////////////

// init
$j('#slideshowcontent').fadeOut(0, function() {
  $j('#slideshowbackground').fadeOut(0, function() {});
});

$j('#slideshowtext').fadeOut(0, function() {});

// open
function showSlideshowElements() {
if (!$hasSlideshow) {
  	return;
  }
  $j('#slideshow').fadeIn($slideshowFadeTime, function() {});
  $j('#slideshowcontent').fadeIn($slideshowFadeTime, function() {});
  $j('#slideshowtext').fadeIn($slideshowFadeTime, function() {});
  $j('#slideshowbackground').fadeIn(0, function() {});
  
  $j('#slideshowbackground').animate({opacity: 0.7}, 0);
  $j('#slideshowbackground').resizeToWindow();
  
  $j('#slideshowcontent').center(0,$slideshowContentOffset);
  
  $j("#slideshowbackground").css("visibility", "visible");
  $j("#slideshowcontent").css("visibility", "visible");
}

function openSlideShow(theID) {
	$index = parseInt(theID);
	$hasSlideshow = false;
	
	if ($slideshowData[$index].length >= 1) {
		$hasSlideshow = true;
	}
	
	if (!$hasSlideshow) {
		return;
	}
	
	
	// offset based on clicked link
	var $offset = 0;
	for (var i = 0; i < $linkindex; i++) {
		$offset += $slideshowMap[$index][i];
	}
	$currentImage = 0 + $offset * $numberOfContentAttributes;
	
	// switch image
	switchImage();
	$j("#slideshowimage").css("visibility", "visible");
	$j('#preload').fadeOut(100, function() {});
		
	// assign new info to textfield
	if ($hasSlideshow) {
		$imageText = '<span class="imagetitle">' + $slideshowData[$index][$currentImage + 4] + '</span>';
		$imageText += '<br/>';
		$imageText += '<span class="imagecaption">' + $slideshowData[$index][$currentImage + 5] + '</span>';
		$j('#slideshowtext').get(0).innerHTML = $imageText;
	}
	
	if ($hasSlideshow) {
		$j('#number').get(0).innerHTML = ($currentImage / $numberOfContentAttributes + 1) + "/" + $slideshowData[$index].length / $numberOfContentAttributes;
	}
	
	// start animation
	startSlideshowAnimation();
}

function slideSwitch(theDirection) {
	if (!$hasSlideshow) {
		return;
	}
	
	// count
	$currentImage += (theDirection * $numberOfContentAttributes);
	if ($currentImage < 0) {
		$currentImage = $slideshowData[$index].length - $numberOfContentAttributes;
	}
	$currentImage = $currentImage % $slideshowData[$index].length;
	
	// switch image
	switchImage();
    
	// assign new info to textfield
	if ($hasSlideshow) {
		$imageText = '<span class="imagetitle">' + $slideshowData[$index][$currentImage + 4] + '</span>';
		$imageText += '<br/>';
		$imageText += '<span class="imagecaption">' + $slideshowData[$index][$currentImage + 5] + '</span>';
		$j('#slideshowtext').get(0).innerHTML = $imageText;
	}
	
	// update number
	if ($hasSlideshow) {
		$j('#number').get(0).innerHTML = ($currentImage / $numberOfContentAttributes +1) + "/" + $slideshowData[$index].length / $numberOfContentAttributes;
	}
}

function switchImage() {
	$j("#slideshowimage").css("visibility", "hidden");
	$j('#preload').fadeIn(100, function() {});
	$j("#slideshowimage").get(0).src = $slideshowData[$index][$currentImage + 0];
	centerImage($slideshowData[$index][$currentImage + 2], $slideshowData[$index][$currentImage + 3]);
	$j("#slideshowimage").get(0).onload = function(){
		$j("#slideshowimage").css("visibility", "visible");
		$j('#preload').fadeOut(100, function() {});
	}
}

function centerImage(theWidth, theHeight) {
	var $scaler = 90;
	var $scalerRatio = $scaler / 100.0;
		
	$j("#slideshowimage").css("width", (theWidth*$scalerRatio)+"px");
	$j("#slideshowimage").css("height", (theHeight*$scalerRatio)+"px");

	$j("#slideshowimage").css("left", ( ($j('#slideshowcontent').width() - (theWidth*$scalerRatio) ) / 2 + "px"));
	$j("#slideshowimage").css("top", ( ($j('#slideshowcontent').height() - (theHeight*$scalerRatio) ) / 2 - 12 + "px"));
}

$j('#slideshowcontent #prev').click(function() {
	if ($mode == "slideshow") {
		slideSwitch(-1);
	}
	if ($mode == "download") {
		downloadSwitch(-1);
	}
	
	resetSlideshowAnimation();
});

$j('#slideshowcontent #play').click(function() {
	if ($mode == "slideshow") {
		startSlideshowAnimation();
		$j("#slideshowcontent #play").css("visibility", "hidden");
		$j("#slideshowcontent #pause").css("visibility", "visible");
	}
});

$j('#slideshowcontent #pause').click(function() {
	if ($mode == "slideshow") {
		stopSlideshowAnimation();
		$j("#slideshowcontent #play").css("visibility", "visible");
		$j("#slideshowcontent #pause").css("visibility", "hidden");
	}
});

$j('#slideshowcontent #next').click(function() {
	if ($mode == "slideshow") {
		slideSwitch(1);
	}
	if ($mode == "download") {
		downloadSwitch(1);
	}
	
	resetSlideshowAnimation();
});

$j('#slideshowcontent #close').click(function() {
	closeSlideshow()
});

$j('#slideshowbackground').click(function() {
	closeSlideshow()
});

function closeSlideshow() {
  $j('#slideshowcontent').fadeOut($slideshowFadeTime, function() {
  	  $j('#slideshowbackground').fadeOut(0, function() {});    
  	  $j("#slideshowbackground").css("visibility", "hidden");
      $j("#slideshowcontent").css("visibility", "hidden");
  });
  $j('#slideshowtext').fadeOut($slideshowFadeTime, function() {});
  
  $index = 0;
  $linkindex = 0;
  
  stopSlideshowAnimation();
}

// keep centered
function keepCentered() {
if ($mode == "slideshow") {
	$j('#slideshowcontent').center(0, $slideshowContentOffset);
  	$j('#downloadlayer').center(0, $slideshowContentOffset);
  	$j('#slideshowbackground').resizeToWindow();
}
if ($mode == "download") {
	$j('#slideshowcontent').center(0, $downloadContentOffset);
  	$j('#downloadlayer').center(0, $downloadContentOffset);
  	$j('#slideshowbackground').resizeToWindow();
}
}

/////////////// animation ///////////////

var $slideshowAnimationWatch;

function startSlideshowAnimation() {
	clearInterval($slideshowAnimationWatch);
	$slideshowAnimationWatch = setInterval(nextSlide, 5000);
}

function resetSlideshowAnimation() {
	clearInterval($slideshowAnimationWatch);
	$slideshowAnimationWatch = setInterval(nextSlide, 5000);
}

function stopSlideshowAnimation() {
	clearInterval($slideshowAnimationWatch);
}

function nextSlide() {
	slideSwitch(1);
}

/////////////// download module ///////////////

// init
$j('#downloadlayer').fadeOut(0, function() {
  $j('#slideshowbackground').fadeOut(0, function() {});
});

$j('#slideshowtext').fadeOut(0, function() {});

function showDownloadModuleElements() {
  if (!$hasSlideshow) {
  	return;
  }
  $j('#slideshow').fadeIn($slideshowFadeTime, function() {});
  $j('#downloadlayer').fadeIn($slideshowFadeTime, function() {});
  //$j('#slideshowtext').fadeIn($slideshowFadeTime, function() {});
  $j('#slideshowbackground').fadeIn(0, function() {});
  
  $j('#slideshowbackground').animate({opacity: 0.7}, 0);
  $j('#slideshowbackground').resizeToWindow();
  
  $j('#downloadlayer').center(0,$downloadContentOffset);
  
  $j("#slideshowbackground").css("visibility", "visible");
  $j("#downloadlayer").css("visibility", "visible");
}

function openDownloadModule(theID) {
	$index = theID;
	$hasSlideshow = true;
	
	$linkindex = $linkindex * $numberOfDownloadAttributes;
	
	$j('#downloadimage').get(0).innerHTML = '<div id="container"></div>';
	if($downloadData[$index][$linkindex + 1] != "") {
		$j('#container').get(0).innerHTML += '<img src="' + $downloadData[$index][$linkindex + 1] + '"/>';
		$j('#container').get(0).innerHTML += '<div class="caption">' + $downloadData[$index][$linkindex + 4] + '</div>';
	}
	$j('#downloadimage').get(0).innerHTML += '<div id="downloadlink"></div>';
	for (var i = 0; i < $downloadData[$index][$linkindex + 3][0].length; i++) {
		if ($downloadData[$index][$linkindex + 3][0][i].indexOf("attachment") == -1 &&
		$downloadData[$index][$linkindex + 3][0][i].indexOf("img") == -1) {
			$j('#downloadlink').get(0).innerHTML += $downloadData[$index][$linkindex + 3][0][i];
			$j('#downloadlink').get(0).innerHTML += "<br/>";
		}
	}
	$j('#downloadtext').get(0).innerHTML = $downloadData[$index][$linkindex + 2];
	$j('#number').get(0).innerHTML = ($linkindex / $numberOfDownloadAttributes +1) + "/" + $downloadData[$index].length / $numberOfDownloadAttributes;
	
	if ($downloadData[$index].length / $numberOfDownloadAttributes == 1) {
		$j('#number').css("visibility", "hidden");
		$j('#next').css("visibility", "hidden");
		$j('#prev').css("visibility", "hidden");
	}
	if ($downloadData[$index].length / $numberOfDownloadAttributes > 1) {
		$j('#number').css("visibility", "visible");
		$j('#next').css("visibility", "visible");
		$j('#prev').css("visibility", "visible");
	}
}

function downloadSwitch(theDirection) {	
	$linkindex += (theDirection * $numberOfDownloadAttributes);
	if ($linkindex < 0) {
		$linkindex = $downloadData[$index].length - $numberOfDownloadAttributes;
	}
	$linkindex = $linkindex % $downloadData[$index].length;
	$j('#downloadimage').get(0).innerHTML = '<div id="container"></div>';
	if($downloadData[$index][$linkindex + 1] != "") {
		$j('#container').get(0).innerHTML += '<img src="' + $downloadData[$index][$linkindex + 1] + '"/>';
		$j('#container').get(0).innerHTML += '<div class="caption">' + $downloadData[$index][$linkindex + 4] + '</div>';
	}
	$j('#downloadimage').get(0).innerHTML += '<div id="downloadlink"></div>';
	for (var i = 0; i < $downloadData[$index][$linkindex + 3][0].length; i++) {
		if ($downloadData[$index][$linkindex + 3][0][i].indexOf("attachment") == -1 &&
		$downloadData[$index][$linkindex + 3][0][i].indexOf("img") == -1) {
			$j('#downloadlink').get(0).innerHTML += $downloadData[$index][$linkindex + 3][0][i];
			$j('#downloadlink').get(0).innerHTML += "<br/>";
		}
	}
	$j('#downloadtext').get(0).innerHTML = $downloadData[$index][$linkindex + 2];
	$j('#number').get(0).innerHTML = ($linkindex / $numberOfDownloadAttributes +1) + "/" + $downloadData[$index].length / $numberOfDownloadAttributes;
}

function closeDownloadModule() {
  $j('#downloadlayer').fadeOut($slideshowFadeTime, function() {
  	  $j('#slideshowbackground').fadeOut(0, function() {});    
  	  $j("#slideshowbackground").css("visibility", "hidden");
      $j("#downloadlayer").css("visibility", "hidden");
  });
  $j('#slideshowtext').fadeOut($slideshowFadeTime, function() {});
  
  $index = 0;
  $linkindex = 0;
}

$j('#downloadlayer #prev').click(function() {
	downloadSwitch(-1);
});

$j('#downloadlayer #next').click(function() {
	downloadSwitch(1);
});

$j('#downloadlayer #close').click(function() {
	closeDownloadModule()
});

$j('#slideshowbackground').click(function() {
	closeDownloadModule()
});

startAutoplay();

}



var loadWatch;
var shouldrebuild = true;

function rebuild() {
	if (shouldrebuild) {
		loadWatch = setInterval(rebuilding, 500);
		shouldrebuild = false;
	}
}

function rebuilding() {
	$j('#boxes').masonry({
	});
	clearInterval(loadWatch);
	shouldrebuild = true;
}

var state;
var allBoxes;
var randomBoxIndex;

function startAutoplay() {
	setTimeout ( "autoplay()", 1000 );
	allBoxes = $j('#boxes').children('.box');
}

function autoplay() {
	if($activeBox === true) {
		var boxID = '#slideshow_' + randomBoxIndex;
		$j('#boxes').children('.box').eq(randomBoxIndex).children('.boxtext').animate({opacity: 0.0}, 'slow');
		$j('#boxes').children('.box').eq(randomBoxIndex).children('.boxlink').animate({opacity: 0.0}, 'slow');
		setTimeout ( "autoplay()", 1000 );
		return;
	}
	if (state == 0) {
		state = 1;
		setTimeout ( "autoplay()", 3000 );
		
		var newRandomBoxIndex = Math.floor(Math.random() * 9);
		for (var i = 0; i < 3; i++) {
			if (newRandomBoxIndex != randomBoxIndex) {
				break;
			} else {
				newRandomBoxIndex = Math.floor(Math.random() * 9);
			}
		}
		randomBoxIndex = newRandomBoxIndex;
		
		var boxID = '#slideshow_' + randomBoxIndex;
		$j('#boxes').children('.box').eq(randomBoxIndex).children('.boxtext').animate({opacity: 1.0}, 'slow');
		$j('#boxes').children('.box').eq(randomBoxIndex).children('.boxlink').animate({opacity: 1.0}, 'slow');
		//console.log($j('#boxes').eq(randomBoxIndex));
		//console.log($activeBox);
	} else {
		state = 0;
		setTimeout ( "autoplay()", 1000 );
		
		var boxID = '#slideshow_' + randomBoxIndex;
		$j('#boxes').children('.box').eq(randomBoxIndex).children('.boxtext').animate({opacity: 0.0}, 'slow');
		$j('#boxes').children('.box').eq(randomBoxIndex).children('.boxlink').animate({opacity: 0.0}, 'slow');
	}
}


