//MODIFY MENU
function modifyMenu(element){
	//RESET
	urlFound = false;
	
	//REPLACE STRING WITH GET QUERY
	if(element == '.submenu') pageUrl = queryString;

	//SET ACTIVE
	$(element + ' a').each(function(){		
		//SET ACTIVE BUTTON
		if($(this).attr('href').indexOf(pageUrl) != -1){
			urlFound = true;
			$(this).addClass('active');
		} else {
			$(this).removeClass('active');
		}
	});
	
	//SET DEFAULT
	if(!urlFound) $(element + ' a:first').addClass('active');
	//GET CSS
	backgroundWidth = $(element + 'Background img').width();
	linkPadding = parseInt($(element + ' .active').css('paddingLeft'));
	linkWidth = $(element + ' .active').width() + linkPadding;
	linkPosition = $(document).width() - $(element + ' .active').offset().left - linkWidth - (linkPadding / 2) - backgroundWidth;
	
	//MODIFY BACKGROUND
	$(element + 'Background').css({width: linkWidth + (backgroundWidth * 2), right:linkPosition});
}

//PERCENTAGE CONVERSION
function percentage(){
	//PERCENTAGE MOVED
	leftMargin = parseInt(scrollbarDiv.css('marginLeft')) - scrollbarMargin;
	totalSpace = pageWidth - scrollbarWidth - (scrollbarMargin*2);
	return(parseInt(leftMargin/totalSpace*divisor));
}

//SCROLLBAR
function scrollbar(e){
	previousPosition = e.pageX;
	$(document).bind("mousemove", function(e){
		//STOP SELECTION GLITCHES
		e.preventDefault();
		
		//POSITION
		currentMargin = parseInt(scrollbarDiv.css('marginLeft'));
		currentDifference = previousPosition - e.pageX;
		
		//LEFT BOUNDARY
		if(currentDifference > 0){
			if(currentMargin - currentDifference >= scrollbarMargin){
				scrollbarDiv.css('marginLeft', currentMargin - currentDifference);
			} else {
				scrollbarDiv.css('marginLeft', scrollbarMargin);
			}
		//RIGHT BOUNDARY
		} else {
			if(currentMargin + scrollbarWidth - currentDifference <= pageWidth - scrollbarMargin){
				scrollbarDiv.css('marginLeft', currentMargin - currentDifference);
			} else {
				scrollbarDiv.css('marginLeft', pageWidth - scrollbarWidth - scrollbarMargin);
			}
		}
		
		//NEW POSITION
		previousPosition = e.pageX;
		percentageMoved = percentage();
		
		//MOVE IMAGES
		overlap = (wrapperWidth - pageWidth) / divisor;
		slideshowDiv.css('marginLeft', parseInt(0 - percentageMoved * overlap));
	});
}

function scrollbarResize(){
	//DIMENSIONS
	pageWidth = $(document).width();
	totalSpace = pageWidth - scrollbarWidth - (scrollbarMargin*2);
	leftMargin = scrollbarMargin + parseInt((totalSpace / divisor) * percentageMoved);
	overlap = (wrapperWidth - pageWidth) / divisor;
	
	//SET
	scrollbarDiv.css('marginLeft', leftMargin);
	slideshowDiv.css('marginLeft', 0 - parseInt(percentageMoved * overlap));
}

//DOM SUPPORT
function scrollbarSetup(){	
	//ELEMENTS
	scrollbarDiv = $('.scrollbar div');
	slideshowDiv = $('.slideshow div:first');
	slideshowAnchor = $('.slideshow a:first');
	slideshowTitles = $('.slideshow span img');

	//GLOBALS
	scrollbarMargin = parseInt(scrollbarDiv.css('marginLeft'));
	scrollbarWidth = scrollbarDiv.width();

	imageWidth = parseInt(slideshowAnchor.css('width'));
	imagePadding = parseInt(slideshowAnchor.css('marginRight'));
	
	leftMargin = parseInt(scrollbarDiv.css('marginLeft')) - scrollbarMargin;
	totalImages = $('.slideshow a').length;
	
	titleHeight = 81;
	oldIndex = 0;
	
	backgroundWidth = 6000;
	wrapperWidth = 0;
	$('.slideshow img').each(function(){
		wrapperWidth += $(this).width();
	});
	wrapperWidth += (totalImages - 1) * imagePadding;
	
	//SET SLIDESHOW WIDTH
	$('.slideshow div').css('width', wrapperWidth + 'px');
	
	//VARIABLES
	divisor = 1000;
	percentageMoved = percentage();	
	
	//SCROLLBAR START
	scrollbarDiv.mousedown(function(e){
		//START SCROLLBAR
		e.preventDefault();
		scrollbar(e);
	});
	
	//SCROLLBAR STOP
	$(document).mouseup(function(e){
		$(document).unbind("mousemove");
	});
	
	//SCROLLBAR RESIZE
	$(window).resize(function(){
		scrollbarResize();
	});
	
	//SLIMBOX
	$("a[rel^='lightbox']").slimbox({}, null, function(el) {
		return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
	});
}

//PORTFOLIO
function printPortfolio(data){
	message = '<div>';
	$.each(data, function(index){
		//URL
		url = this.replace('Thumbs', 'Large');
		url = url.substring(3);
		
		//TITLE
		if(this.indexOf('image00') != -1){
			stringStart = 'portfolioThumbs/';
			stringStop = '/image0';
			title = this.substring(this.indexOf(stringStart) + stringStart.length, this.indexOf(stringStop));
		}
		
		//LINK
		message += '<a href="' + url + '" rel="lightbox-' + title + '">';
		message += '<img src="' + this.substring(3) + '" alt="Image ' + (index + 1) + ' of ' + data.length + '" title="Image ' + (index + 1) + ' of ' + data.length + '" width="400" height="300" /></a>';
	});
	
	//PRINT MESSAGE
	message += '</div>';
	$('.slideshow').html(message);
	
	scrollbarSetup();
}

//READY
$(window).load(function(){
	//SET DEFAULT TO ONLY ONE DIRECTORY
	getFirst = 1;
	
	//GET URL AND QUERY STRING
	pageUrl = $(location).attr('href');
	if(pageUrl.indexOf('=') == -1){
		queryString = '';
	} else {
		getFirst = 0;
		queryString = '/' + pageUrl.substring(pageUrl.lastIndexOf('=') + 1);
	}
	pageUrl = pageUrl.substring(pageUrl.lastIndexOf('/') + 1, pageUrl.lastIndexOf('.'));
		
	//GET PAGE WIDTH
	pageWidth = $(document).width();
	
	//FIX MENU
	modifyMenu('.menu');
		
	//PORTFOLIO
	if($('.portfolio').length > 0){
		//PORTFOLIO VARIABLES
		portfolio = $('.portfolio');
		
		//PORTFOLIO IMAGES
		$.ajax({
			url: 'src/php/slideshow.php',
			data: 'fileDirectory=../../images/portfolioThumbs' + queryString + '&getFirst=' + getFirst,
			success: function(data){ printPortfolio(data) },
			dataType: 'JSON'
		});
		
		//RESET QIERY STRING FOR MENU FUNCTION
		if(queryString == ''){
			queryString = null;
		} else {
			queryString = queryString.substring(1);
		}

		//FIX SUBMENU
		modifyMenu('.submenu');
	} else {
		portfolio = '';
		scrollbarSetup();
	}
});
