var slider = 0.0;
var sliderinc = 800;
var slidecount = 5;
var mode = 3;
var time;
var inProg = false;
var cachethumbs = '';
var cachepics = '';
var i = 1;
while(i<photos.length) {
	$('#hidden').append('<img src="/p/f/'+photos[i]+'"/><img src="/p/t/'+i+'/'+photos[i]+'"/>');
	i++;
}

$(window).ready(function() {
	if(top.location.pathname=="/book.html") {
		modeswitch(false);
	}
	if($('#slideCont').length) {
		mode = 0;
		$('.slide img').each(function() {
			var h = $(this).height();
			var w = $(this).width();
			if(h>0)
				$(this).parent().css('padding-top', parseInt((518-h)/2)+"px");
		});
		slider = 800.0;
		initEvents();
		time = setInterval("fade()",3000);
	}
	if($('.vid').length) {
		loadVid();
		
	}
	$('a.pSwitch').live("click", function() {
		clearInterval(time);
		pageSwitch($(this));
		return false;
	});
	
	$('a#home').click(function() {
		if(mode>0) {
			return true;
		}
		return false;
	});
	
	$('a.modeSwitch').live('click', function() {return modeswitch(false)});
	
	$('a.collage').each(function() {
		$('#hidden').append('<img src="'+$(this).attr('href')+'"/>');
	});
	
	$('a.collage').live('click', function() {
		var target = $(this).attr('href');
		$('#content').html('<div class="ccont"><div id="leftControl" class="controls"></div><div id="slideCont"><div id="slider"><div class="slide" style="display:block;"><img src="'+target+'" alt=""/></div></div></div><div id="rightControl" class="controls"></div>');
		var img = $('.slide img');
		var h = $(img).innerHeight() ? $(img).innerHeight() : 518;
		var w = $(img).innerWidth() ? $(img).innerWidth() : 800;
		$(img).parent().css('padding-top', parseInt((518-h)/2)+"px");
		$(img).after('<a href="/about.html" class="ticons close" style="right: '+parseInt((800-w)/2)+'px;top: '+parseInt((518-h)/2)+'px;">To Gallery</a>');
		
		setTimeout(fixPadding,10);
		
		$('a.close').click(function() {
			pageSwitch($('#menu .active a'));
			return false;
		});
		
		return false;
	});
	
});

function fixPadding() {
	$('.slide img').each(function() {
		var h = $(this).innerHeight() ? $(this).innerHeight() : 518;
		var w = $(this).innerWidth() ? $(this).innerWidth() : 800;
		$(this).parent().css('padding-top', parseInt((518-h)/2)+"px");
		$(this).next('a.ticons').css({
			right: parseInt((800-w)/2)+'px'
		});
		$(this).next('a.close').css({
			top: parseInt((518-h)/2)+'px'
		});
	});
	
}

function advance(dir) {
	if(!inProg) {
		var newpos = slider-(sliderinc*dir);
		
		if(-newpos>=sliderinc*(slidecount+1)) {
			newpos = -800;
			inProg = true;
			$('#slider').animate({ 
				left: slider-800+'px'
			}, 500, function() {
				inProg = false;
				$('#slider').css('left', '-810px');
			});
		} else if(newpos>-800) {
			newpos = -sliderinc*(slidecount-1)-800;
			inProg = true;
			$('#slider').animate({ 
				left: -10+'px'
			}, 500, function() {
				inProg = false;
				$('#slider').css('left', -sliderinc*slidecount-10+'px');
			});
			
		} else {
			inProg = true;
			$('#slider').animate({ 
				left: newpos-10+'px'
			}, 500, function() {
				inProg = false;
				
			});
		}
		slider = newpos;
	}
	return false;
}

function fade() {
	$('.slide:visible').fadeOut('500')
	if($('.slide:visible').next().length) {
		$('.slide:visible').next().fadeIn('500');
	} else {
		$('.slide:first').fadeIn('500');
	}
	
}

function modeswitch(num) {
	if(mode==0) {
		$('.controls a').fadeIn(500);
	}
	clearInterval(time);
	if(mode<2) {
		// Thumbnail View
		if(cachethumbs=='') {
			var count = 0;
			var string = '';
			i = 1;
			var first = '';
			var temp = '';
			while(i<photos.length) {
				if(count>9)
					count = 0;
				
				string += '<div class="slide">';
				temp = '';
				for(count=0; count<=9; count++) {
					temp += '<div class="th"><img src="/p/t/'+i+'/'+photos[i]+'" class="thum" id="pic'+i+'" alt="" style="width: 138px; height: 138px;"/></div>';
					i++;
				}
				string += temp;
				string += '</div>';
				
				if(i==11) {
					first = string;
				}
			}
			string = '<div class="slide">'+temp+'</div>'+string;
			slidecount = parseInt((photos.length-1)/10);
			
			cachethumbs = string+first;
		}
		slider = -800;
		slidecount = parseInt((photos.length-1)/10);
		$('#slider').fadeOut(500, function() {
			$('#slider').removeClass('front').css('left', '-810px').show().html(cachethumbs);
			$('.slide').css('padding-top', '110px').fadeIn(500);
		});
		mode = 2;
	} else if(mode==2) {
		// Picture View
		if(cachepics=='') {
			var string = '';
			i = 1;
			while(i<photos.length) {
				string += '<div class="slide"><img src="/p/f/'+photos[i]+'" id="pic'+i+'" alt=""/></div>';
				i++;
			}
			string = '<div class="slide"><img src="/p/f/'+photos[(photos.length-1)]+'" id="pic'+i+'" alt=""/></div>'+string;
			
			cachepics = string+'<div class="slide"><img src="/p/f/'+photos[1]+'" id="pic'+i+'" alt=""/></div>';
		}
		slider = -800;
		slidecount = parseInt(photos.length)-1;
		$('#slider').fadeOut(500, function() {
			
			var goto = '-810px';
			if(num) {
				var newpos = (parseInt(num.substr(3))-1)*sliderinc;
				goto = -newpos-810+'px';
				slider = -newpos-800;
			}
			$('#slider').css('left', goto).show();
			$('#slider').html(cachepics);
			
			$('.slide').css('padding-top', '0').fadeIn(500);
			
			
			$('.slide img').each(function() {
				var h = $(this).height();
				var w = $(this).width();
				$(this).parent().css('padding-top', parseInt((518-h)/2)+"px");
				$(this).parent().append('<a href="#" class="ticons" onclick="modeswitch(false);return false;" style="right: '+parseInt((800-w)/2)+'px;">To Gallery</a>');
			});
			setTimeout(fixPadding,10);
		});
		
		
		mode = 1;
	} else if(mode==3) {
		$('#content>div').fadeOut(500, function() {
			mode = 1;
			$('#content').html('<div class="ccont"><div id="leftControl" class="controls"><a href="#" style="display: none;">Move Left</a></div><div id="slideCont"><div id="slider"><div></div></div></div><div id="rightControl" class="controls"><a href="#" style="display: none;">Move Right</a></div>');
			$('.controls a').fadeIn(500);
			modeswitch(false);
			initEvents();
		});
	}
	$('#head li.active').removeClass('active');
	$('.modeSwitch').parent().addClass('active');
	
	return false;
}

function pageSwitch(dest) {
	$('#content>div').fadeOut(500, function() {
		$(this).detach();
		$('#content').load($(dest).attr('href')+" #content", function() {
			$('#content>div').css('display', 'none').fadeIn(500);
			mode = 3;
			if($('.vid').length) {
				loadVid();
			}
			
			if($('a.collage').length) {
				$('a.collage').each(function() {
					$('#hidden').append('<img src="'+$(this).attr('href')+'"/>');
				});
			}
		});
		$('#head li.active').removeClass('active');
		$(dest).parent().addClass('active');
	})
}

function loadVid() {
	if(navigator.userAgent.match(/iPad|iPhone/i)) {
		$('.vid').html('<video src="/m/tibor-reel_sm.mov" width="780" height="440" controls preload></video>');
	} else {
		AC_FL_RunContent(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
			'width', '780',
			'height', '440',
			'src', '/m/vplayer2',
			'quality', 'high',
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'align', 'middle',
			'play', 'true',
			'loop', 'true',
			'container', $('.vid'),
			'scale', 'exactfit',
			'wmode', 'window',
			'devicefont', 'false',
			'id', 'fPlayer',
			'bgcolor', '#000000',
			'name', 'fPlayer',
			'menu', 'false',
			'FlashVars', 'video=/m/2011Reel-web3.mov&ar=16x9&w=780&h=440',
			'allowFullScreen', 'true',
			'allowScriptAccess','sameDomain',
			'movie', '/m/vplayer2'
			); //end AC code
	}
}

function initEvents() {
	
	$('#rightControl a').live("click", function() {clearInterval(time); return advance(1)});
	$('#leftControl a').live("click", function() {clearInterval(time); return advance(-1)});
	$('.front .slide img').click(function() {modeswitch(false)});
	$('img.thum').live('click', function() {return modeswitch($(this).attr('id'))});
	$('img.thum').live('mouseover mouseout', function(event) {
		if (event.type == 'mouseover') {
			$(this).stop().animate({opacity:1}, 500);
		} else {
			$(this).stop().animate({opacity:0.6}, 500);
		}
	});

}

