soundManager.url = '/template/swf/';
soundManager.debugMode = false;

soundManager.defaultOptions = {
		useHTML5Audio: false,
		flashVersion: 9,
		multiShot: true,
		multiShotEvent: true,
		allowPolling: true
};

//load all sounds, include behaviour onplay and onfinish
soundManager.onready(function() {
	
	$('#soundID').val(0);
	
	var soundFiles = new Array();
	soundFiles[0] = '/template/audio/1.mp3';
	soundFiles[1] = '/template/audio/2.mp3';
	soundFiles[2] = '/template/audio/3.mp3';
	soundFiles[3] = '/template/audio/4.mp3';
	soundFiles[4] = '/template/audio/5.mp3';
	soundFiles[5] = '/template/audio/6.mp3';
	soundFiles[6] = '/template/audio/7.mp3';
	soundFiles[7] = '/template/audio/8.mp3';
	soundFiles[8] = '/template/audio/9.mp3';
	soundFiles[9] = '/template/audio/10.mp3';
	soundFiles[10] = '/template/audio/11.mp3';
	soundFiles[11] = '/template/audio/12.mp3';
	soundFiles[12] = '/template/audio/13.mp3';
	soundFiles[13] = '/template/audio/14.mp3';
	soundFiles[14] = '/template/audio/15.mp3';
	soundFiles[15] = '/template/audio/16.mp3';
	soundFiles[16] = '/template/audio/17.mp3';
	soundFiles[17] = '/template/audio/18.mp3';
	soundFiles[18] = '/template/audio/19.mp3';
	soundFiles[19] = '/template/audio/20.mp3';
	soundFiles[20] = '/template/audio/21.mp3';
	soundFiles[21] = '/template/audio/22.mp3';
	soundFiles[22] = '/template/audio/23.mp3';
	soundFiles[23] = '/template/audio/24.mp3';
	soundFiles[24] = '/template/audio/25.mp3';
	soundFiles[25] = '/template/audio/26.mp3';
	soundFiles[26] = '/template/audio/27.mp3';
	soundFiles[27] = '/template/audio/28.mp3';
	soundFiles[28] = '/template/audio/29.mp3';
	soundFiles[29] = '/template/audio/30.mp3';
	soundFiles[30] = '/template/audio/31.mp3';
	soundFiles[31] = '/template/audio/32.mp3';
	soundFiles[32] = '/template/audio/33.mp3';
	soundFiles[33] = '/template/audio/34.mp3';
	soundFiles[34] = '/template/audio/35.mp3';
	soundFiles[35] = '/template/audio/36.mp3';
	soundFiles[36] = '/template/audio/37.mp3';
	soundFiles[37] = '/template/audio/38.mp3';
	soundFiles[38] = '/template/audio/39.mp3';
	soundFiles[39] = '/template/audio/40.mp3';	
	
	var soundCount = soundFiles.length;
	var i = 0;
	var sounds = new Array();
	
	for(i = 0; i < soundCount; i++){
		var j = i + 1;
		sounds[j] = soundManager.createSound({
			autoLoad: true,
			id: 'sound' + j,
			url: soundFiles[i],
			onplay: function(){
				var thisID = this.sID.substring(5);
				$('#soundID').val(thisID);
			},
			onfinish: function(){
				var currentID = parseInt($('#soundID').val());
				var thisID = parseInt(this.sID.substring(5));
				if(currentID <= thisID){
					$('#soundID').val(0);
				}
			}
		});
	}

});

// check which sound is currently playing and play the next one
function playTones(){
	
	if($("#toggleSound").attr("class") == "disabled"){
		return false;
	} else {
		var currentID = Number($('#soundID').val());
		if(currentID < 40){
			var nextID = currentID + 1;
		} else {
			var nextID = 1;
		}
		soundManager.play('sound' + nextID);
	}
}
// replace 60px image with 100px image
function largeImg(src)
{
   var img = new RegExp("(.+)/(thumbnails/)([^/]+)\.(gif|png|jpg)", "g");
   return src.replace(img, "$1/$3.$4");
}

// replace 100px image with 60px image
function normalImg(src)
{
   var img = new RegExp("(.+)/([^/]+)\.(gif|png|jpg)", "g");
   return src.replace(img, "$1/thumbnails/$2.$3");
}

$(document).ready(function(){
	
	var windowHeight = $(window).height();
	var calcHeight = (windowHeight / 2) - 365; 	

	if(calcHeight >= 20){
		if ($.browser.msie && $.browser.version.substr(0,1)<8) {
			$('#container').css({'padding-top': calcHeight + 'px'});
		} else {
			$('#container').css({'margin-top': calcHeight + 'px'});
		}	
	}
	
	$('#showPage2').click(function(){
		$('#content ul').each(function(index) {
		    $(this).toggleClass('off').toggleClass('on');
		});
		this.src = $('#content ul.off').find('img')[0].src;
	});
	
	$('#showPage2')[0].src = $('#content ul.off').find('img')[0].src;
	
	// enlarge thumbnails and play sound on hover
	$("ul.projects li").hover(function() {
		playTones();
		var imgSrc = largeImg($(this).find('img').attr("src"));
		$(this).find('img').attr("src", imgSrc);
		$(this).css({'z-index' : '10'});
		$(this).find('img').stop()
			.animate({
				marginTop: '-50px',
				marginLeft: '-50px',
				top: '30px',
				left: '30px',
				width: '100px', 
				height: '100px'
			}, 200).addClass("hover"); 
	
		} , function() {
		$(this).css({'z-index' : '0'}); 
		$(this).find('img').removeClass("hover").stop()  
			.animate({
				width: '60px', 
				height: '60px',				
				marginTop: '0', 
				marginLeft: '0',
				top: '0',
				left: '0'
			}, 400);
		var imgSrcNorm = normalImg($(this).find('img').attr("src"));
		$(this).find('img').attr("src", imgSrcNorm);
	});
	
	$("a[rel='project']").click(function(){
		$('#overlay').css({'height': $(document).height(), 'width': ($(document).width())});
		$('#overlay').css("display", "block");
		
		$("#about").hide();
		$("#contact").hide();
		
		var title = $(this).attr('title');
		var img = $(this).attr('href');
		$('#spotlightImg').attr('src', img);
		$('#spotlightTitle').html(title);
		
		$('#spotlight').show('slow');
		if ($.browser.msie && $.browser.version.substr(0,1)<8) {
			var offsetSpot = $('#spotlight').offset();
			$('body').prepend( $('#content>#spotlight') );
			$('#spotlight').offset(offsetSpot);
			
			var offsetSide = $('#sidebuttons').offset();
			$('body').prepend( $('#sidebar>#sidebuttons') );
			$('#sidebuttons').offset(offsetSide);
			$('#sidebuttons').css({'position': 'absolute'}); 
			
		}
		return false;
	});
	
	var checkMouse = false;
	
	$('#spotlight').hover(function(){ 
	        checkMouse = false; 
	    }, function(){ 
	        checkMouse = true; 
	});

	$("body").mousedown(function(){ 
        if(checkMouse){
        	$('#overlay').css("display", "none");
        	if ($.browser.msie && $.browser.version.substr(0,1)<8) {
            	$('#content').prepend( $('body>#spotlight') );
            	$('#spotlight').css({'top': '150px', 'left': '150px'});
            	
            	$('#sidebar').prepend( $('body>#sidebuttons') );
            	$('#sidebuttons').css({'top': '0', 'left': '0', 'position': 'relative'});   
        	}
        	$('#spotlight').hide('slow');
        }
	});	
	
	$("#spotlightImg").mousedown(function(){
	        	$('#overlay').css("display", "none");
        	if ($.browser.msie && $.browser.version.substr(0,1)<8) {
            	$('#content').prepend( $('body>#spotlight') );
            	$('#spotlight').css({'top': '150px', 'left': '150px'});
            	
            	$('#sidebar').prepend( $('body>#sidebuttons') );
            	$('#sidebuttons').css({'top': '0', 'left': '0', 'position': 'relative'});   
        	}
        	$('#spotlight').hide('slow');
	});
	
	$(window).resize(function() {
		var windowHeight = $(window).height();
		var calcHeight = (windowHeight / 2) - 365; 
		
		if(calcHeight >= 20){
			if ($.browser.msie && $.browser.version.substr(0,1)<8) {
				$('#container').css({'padding-top': calcHeight + 'px'});
			} else {
				$('#container').css({'margin-top': calcHeight + 'px'});
			}	
		} else {
			if ($.browser.msie && $.browser.version.substr(0,1)<8) {
				$('#container').css({'padding-top': '20px'});
			} else {
				$('#container').css({'margin-top': '20px'});
			}			
		}
		
		$('#overlay').css({'height': $(document).height(), 'width': $(window).width()});
	});
	
	$("#about").hide();
	$("#aboutBtn").click(function(){
		$("#about").toggle('slide',{direction: "right"});
		$("#contact").hide();
	});
	
	$("#contact").hide();
	$("#contactBtn").click(function(){
		$("#contact").toggle('slide',{direction: "right"});
		$("#about").hide();
	});
	
	$("#contactBtn2").click(function(){
    	$('#overlay').css("display", "none");
    	$('#spotlight').hide('slow');
		$("#contact").toggle('slide',{direction: "right"});
		
	});
	
	$("#toggleBlack").click(function(){
		$("body").css("background", "url(/template/images/black-bg.gif) repeat center top");
		$('#overlay').css('background-color', 'black');
		$("#container").addClass("black");
		$(this).css("display", "none");
		$("#toggleWhite").css("display", "block");
	});
	
	$("#toggleWhite").click(function(){
		$("body").css("background", "url(/template/images/white-bg.gif) repeat center top");
		$('#overlay').css('background-color', 'white');
		$("#container").removeClass("black");
		$(this).css("display", "none");
		$("#toggleBlack").css("display", "block");
	});
	
	$("#toggleSound").click(function(){
		if($("#toggleSound").attr("class") == "enabled"){
			$("#toggleSound").attr("class","disabled");
		} else {
			$("#toggleSound").attr("class","enabled");
		}
	});
	
});
