var projectpanelamt = 0;
var curprojectpanel = 1;

var introArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18];
var introcurLoc = 0;
var introInt = 0;
var introiteration = 1;
var intromaxiteration = 2;
var introsimuflag = 0;

var detailpanelamt = 0;
var curdetailpanel = 1;


var infopanelamt = 0;
var curinfopanel = 1;




shuffle = function(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};



projectShift = function(pos) {
	var ntime = 600;
	var newpos = curprojectpanel + pos;
	if(newpos < 1){
		newpos = 1;
		
	} else if (newpos > projectpanelamt){
		newpos = projectpanelamt;

	} 
	if(newpos != curprojectpanel) {
		var newxposition = 0-((newpos-1)*785);
		$('#projectscroll').animate({
			left: newxposition
		}, ntime, function() {
	
		});
		
		//
		if(newpos == 1){
			$('#arr-left').hide();
			$('#arr-right').show();
		} else if(newpos == projectpanelamt){
			$('#arr-left').show();
			$('#arr-right').hide();
		} else {
			$('#arr-left').show();
			$('#arr-right').show();
		}
		
		curprojectpanel = newpos;
	}
}

startIntroAnimation = function(){
	introArray = shuffle(introArray);
	introInt = setTimeout(animateIntroNext,300);
	$('#animationcontainer').show();
	//$('#ani-1').children('img:eq(1)').fadeIn();
}
animateIntroNext = function(){

	//clearTimeout(introInt);
	var oldintroIteration = introiteration;
	introcurLoc++;
	if(introcurLoc > introArray.length) {
		introcurLoc = 1;
		introiteration++;
	}
	var anipos = introArray[introcurLoc-1];
	var newpos = introiteration-1;
	//alert($('#ani-'+introcurLoc).children('img:eq(introiteration-1)'));
	$('#ani-'+anipos).children('img:eq('+newpos+')').fadeIn(800);
	
	if(introiteration > intromaxiteration) {
		$('#animationcontainer').fadeOut(1000,function(){
		$('#animationcontainer2').fadeIn(1500,function(){
			$('#animationcontainer3').fadeIn(1500);
		});});
		introInt = setTimeout(switchPageAnimation,5000);
	} else {
		introsimuflag++;
		if(introsimuflag < 2) {
			animateIntroNext();
		} else {
			introsimuflag = 0;
			introInt = setTimeout(animateIntroNext,400);
		}
	}
	


}
switchPageAnimation = function() {
	$('#animationcontainer2').fadeOut(1000,function(){
		$('#animationcontainer3').fadeOut(1000);
	});
	introInt = setTimeout(switchPage,2500);
}
switchPage = function() {
	window.location = "aboutus.html";
}


shiftDetail = function(id) {
	$('.projdetailcontent').hide();
	$('#'+id).customFadeIn();
}
shiftDetailImg = function(idno) {
	if(idno != curdetailpanel){
		//old
		$('#detbut_'+curdetailpanel+' img').attr('src','images/detailbut02.jpg');
		$('#detbut_'+idno+' img').attr('src','images/detailbut01.jpg');
		$('#di_'+curdetailpanel).fadeOut();
		$('#di_'+idno).fadeIn();
		curdetailpanel = idno;
		$('.projdetailcontent').hide();
		
		var hasMap = $('#di_'+idno).children('map').size();
		if(hasMap > 0){
			$('#detailinstructions').show();
		} else {
			$('#detailinstructions').hide();
		}
	}
}

shiftInfoPanel = function(idno) {
	if(idno != curinfopanel){
		//old
		$('#infobut_'+curinfopanel+' img').attr('src','images/detailbut02.jpg');
		$('#infobut_'+idno+' img').attr('src','images/detailbut01.jpg');
		$('#ipanel_'+curinfopanel).customFadeOut();
		$('#ipanel_'+idno).customFadeIn();
		curinfopanel = idno;
	}
}


$(function() {
	

	//shift middle
	if($('#containerpanel').size() > 0){
		var ht = $(window).height();
		var shiftht = (ht-540)/2;
		if(shiftht > 0){
			$('#containerpanel').css('margin-top',shiftht);
		}
	}
	//navigation
	$("#navigation li").hover(
		function(){ $(".navi-subpanel", this).fadeIn("fast"); }, 
		function() {$(".navi-subpanel", this).fadeOut("fast"); } 
	);
	
	
	//projects
	if($('#projectcontainer').size() >0){
		var projectbuttons = '<div id="projectnavigation"><span id="arr-left"><img src="images/arrow-left.jpg"/></span><span id="arr-right"><img src="images/arrow-right.jpg"/></span></div>';
		
		//create
		projectpanelamt =  $('.projectpanel').size();
		if(projectpanelamt > 1){
			$('#projectscroll').css('width',projectpanelamt*785);
			$('#projectcontainer').after(projectbuttons);
			
			$('#arr-left').click(function() {
				projectShift(-1);
			});
			$('#arr-right').click(function() {
				projectShift(1);
			});
			curprojectpanel = 1;
			$('#arr-left').hide();
		}
	}
	
	//animation 
	if($('#animationcontainer').size() >0){
		$('.anipanel img').hide();
		$('.anipanel2 img').hide();
		$('.anipanel').each(function() {
			//$(this).children('img:first').show();
		});
		$('.anipanel2').each(function() {
			//$(this).children('img:first').show();
		});
		
		startIntroAnimation();
	}
	
	//Area
	
	if($('#detailimgcontainer').size() >0){
		detailpanelamt = $('.detailimg').size();
		if(detailpanelamt > 1){
			var butstr = '<div id="detailbuttoncontainer">';
			for(var q = 1;q<=detailpanelamt;q++){
				butstr += '<div id="detbut_'+q+'" class="detailbutton"><img src="images/detailbut02.jpg"/></div>';
			}
			butstr += '</div>';
			$('#detailimgcontainer').after(butstr);
			$('#detbut_1 img').attr('src','images/detailbut01.jpg');
			
			$('.detailbutton').click(function(){
				var qx = Number($(this).attr('id').split('_')[1]);
				shiftDetailImg(qx);
			});
			
		}
		$('#di_1').show();
		var hasMap = $('#di_'+1).children('map').size();
		if(hasMap > 0){
			$('#detailinstructions').show();
		} else {
			$('#detailinstructions').hide();
		}
	
	
	$('area').click(function(){
		var nx = $(this).attr('href');
		nx = nx.replace("#","");
		shiftDetail(nx);
		return false;
	});
	if($('area').size() > 0 && $('#detailimgcontainer').size() > 0) {
		$.fn.maphilight.defaults = {
		fill: true,
		fillColor: '000000',
		fillOpacity: 0.2,
		stroke: true,
		strokeColor: 'ff0000',
		strokeOpacity: 1,
		strokeWidth: 1,
		fade: true,
		alwaysOn: false,
		neverOn: false,
		groupBy: false,
		wrapClass: true
	};
		$('img[usemap]').maphilight();
	}
	}
	//Infocontainer

	infopanelamt = $('.infopanel').size();
	
	if(infopanelamt > 0){
		if(infopanelamt > 1) {
			//generate
			var infostr = '<div id="infobuttoncontainer">';
			for(var p=1;p<=infopanelamt;p++){
				infostr += '<div id="infobut_'+p+'" class="infobutton"><img src="images/detailbut02.jpg"/></div>';
				$('#infocontainer .infopanel').eq(p-1).attr('id','ipanel_'+p);
			}
			infostr += '</div>';
			$('#infocontainer').append(infostr);
			
			$('#infobut_1 img').attr('src','images/detailbut01.jpg');
			
			$('.infobutton').click(function(){
				var qx = Number($(this).attr('id').split('_')[1]);
				shiftInfoPanel(qx);
			});
			$('#ipanel_1').show();
		} else {
			$('.infopanel').show();
		}
		
	}
	if($('#abtlogo').size() >0){
		$('#abtlogo').fadeIn();
	}
	//jScrollPane
	//$('#scrollpanel').jScrollPane({scrollbarWidth:5});
});

(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);




