// JavaScript Document

jQuery('document').ready(function($) {
	var durate = 500;
	var current = 1;
	var imgSrc = $('#jaeContainer img:first,#jaeIndexContainer img:first').attr('src');
	var totalslides = $('#jaeContainer img').length;
	var imageEq = parseInt(getUrlVars()["img"]);
	var randomImg = ['Homepage_1.jpg','Homepage_2.jpg','Homepage_3.jpg','Homepage_4.jpg','Homepage_5.jpg','Homepage_6.jpg','Homepage_7.jpg','Homepage_8.jpg'];
	
	$('#jaeRandomImage img').attr('src','images/random_home/' + randomImg[Math.floor(Math.random() * randomImg.length)]);
	$('span#totalNum').text(totalslides);
	$('a#back').attr('href',$('a#back').attr('href') + (imageEq - 1));
	
	$('#content').append('<div id="tooltip" style="position:absolute;z-index:10;border:1px solid #999;background-color:#FFF;padding:0 3px;"></div>');
	$('#tooltip').hide();
	var $tooltip = $('#tooltip');
	var $title = $('#jaeContainer').attr('title');
		
	// setting current image (index page)
	if(imageEq) {
		var prevImgEq;
		var nextImgEq;
		
		if(imageEq == totalslides) {
			prevImgEq = totalslides - 1;
			nextImgEq = 0;
		} else {
			prevImgEq = imageEq - 1;
			nextImgEq = imageEq + 1;
		}
		
		var imgDivPrev = '#jaeContainer img:eq('+prevImgEq+')';
		var imgDivCurrent = '#jaeContainer img:eq('+imageEq+')';
		var imgDivNext = '#jaeContainer img:eq('+nextImgEq+')';
		
		var srcPathPrev = imgSrc.replace(/\d+/g,prevImgEq + 1);
		var srcPathCurrent = imgSrc.replace(/\d+/g,imageEq + 1);
		var srcPathNext = imgSrc.replace(/\d+/g,nextImgEq + 1);
		
		current = imageEq + 1;
		$('span#crtNum').text(current);
		$(imgDivCurrent).addClass('current');
		($(imgDivCurrent).attr('src')) ? null : $(imgDivCurrent).attr('src',srcPathCurrent);
		($(imgDivPrev).attr('src')) ? null : $(imgDivPrev).attr('src',srcPathPrev);
		($(imgDivNext).attr('src')) ? null : $(imgDivNext).attr('src',srcPathNext);
		
		$('a#index').attr('href',$('a#index').attr('href') + current);
	} else {
		$('#jaeContainer img:first').addClass('current');
		$('a#index').attr('href',$('a#index').attr('href') + current);
	}
	
	// showing text
	$('.switch').bind('click',function() {
		if($(this).attr('id') == 'text_btn') {
			$('#jaeContainer p,#jaeIndexContainer p').css({'z-index':4,opacity: 0.0}).animate({opacity: 1.0},durate);
			$('#photo_btn').css('display','inline');
			$(this).css('display','none');
			
		} else {
			$('#jaeContainer p,#jaeIndexContainer p').animate({opacity: 0.0},durate,function(){$('#jaeContainer p,#jaeIndex p').css('z-index',0)});
			$('#text_btn').css('display','inline');
			$(this).css('display','none');
		}
	});
	
	// slideshow toggle
	$('#jaeContainer').toggle(openJaeslideshow,closeJaeslideshow);
	function openJaeslideshow() {
		$('#wrap').css('height','100%');
		$('.jaeGallery').after('<div class="jaeScreener"></div>').animate({'margin-top':-600/2},100)
			.next().css({'opacity':0.0}).animate({'opacity':1.0},100); // margin-top was -580
		$('#header').animate(({'height':'50%'}),100);
		
		$('#footer').css('display','none');
		$('#jaeContainer').attr('title','close slide show');
		$title = $('#jaeContainer').attr('title'); // for tooltip
	}
	function closeJaeslideshow() {
		$('.jaeScreener').animate({'opacity':0.0},100,function(){$(this).remove();});
		$('#header').animate(({'height':147}),100);
		$('#footer').css('display','block');
		$('.jaeGallery').animate({'margin-top':0},100);
		$('#jaeContainer').attr('title','open slide show');
		$title = $('#jaeContainer').attr('title');// for tooltip
	}
	
	// tooltip
	$('#jaeContainer').each(function(){
		$(this).hover(function(){
			
			this.title = '';
			$tooltip.text($title).show();
		}, function(){
			this.title = $title;
			$tooltip.text('').hide();
		});

		$(this).mousemove(function(e){
			$tooltip.css({
				top: e.pageY + 20,
				left: e.pageX - 10
			});
		});
	});
	
	// slide function
	$('.controller').bind('click', function() {
		var targetImg;
		var srcPath;
		var targetImgIndex;
		// update current img
		if($(this).attr('id') == 'next') {
			++current;
			if (current > totalslides) {
				current = 1;
			}
			targetImgIndex = current;
			targetImg = '#jaeContainer img:eq('+targetImgIndex+')';//eq(2)
			srcPath = imgSrc.replace(/\d+/g,current+1);
		} else {
			--current;
			if(current < 1) {
				current = totalslides;
			}
			targetImgIndex = current-2;
			targetImg = '#jaeContainer img:eq('+targetImgIndex+')';//eq(8)
			srcPath = imgSrc.replace(/\d+/g,current-1);
		}
		// reset url crt id for index pg
		$('a#index').attr('href',$('a#index').attr('href').replace(/\d+/g,current));
		// attaching img src
		($(targetImg).attr('src')) ? null : $(targetImg).attr('src',srcPath);
		// display updated crt img num
		$('span#crtNum').text(current);
		
		// check text on n off
		if ($('#jaeContainer p').css('z-index') != 0) {
			$('#jaeContainer p').animate({opacity: 0.0},durate,function(){$(this).css('z-index',0)});
			$('#text_btn').css('display','inline');
			$('#photo_btn').css('display','none');
		}
		
		// actual transition
		($(this).attr('id') == 'next') ? rotateImages('jaeContainer','next') : rotateImages('jaeContainer','prev');
	});
	function rotateImages(slideDiv,dir) {
		var oCurPhoto = $('.jaeGallery #'+slideDiv+' img.current');
		var oNxtPhoto;
		
		(dir == 'next') ? oNxtPhoto = oCurPhoto.next() : oNxtPhoto = oCurPhoto.prev();
		
		if(current == 1) {
			oNxtPhoto = $('.jaeGallery #'+slideDiv+' img:first');
		} else if(current == totalslides) {
			oNxtPhoto = $('.jaeGallery #'+slideDiv+' img:last');
		}
		
		oCurPhoto.removeClass('current').addClass('previous');
		oNxtPhoto.addClass('current').css({ opacity: 1.0 },function() {
			oCurPhoto.removeClass('previous');
		});
	}
	
	// get variable from url
	function getUrlVars() {
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++) {
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	}
});
