/* 

Delia's Bootup Script
v. 1.0

last mod: 6-1-09
by: Mark


*/

var previous;
var current;
var offset;

function doSlide(target, method) {
	var targetPos = $(target).position();
	if (targetPos.left == 0) { targetPos.left = offset; }
	
	previous = '#' + $('ul#photos li.current').attr('id');
	current = '#' + $(target).attr('id');
	$('ul#photos li').removeClass('current');
	$(target).addClass('current');
	
	$('ul#photos li img.photo').fadeTo('fast', .50);
	$(target + ' img.photo').fadeTo('fast',1);
	
	/* scroll or jump */
	(method == 'jump') ? $('ul#photos').stop().css('left','-' + (targetPos.left - offset) + 'px') : $('ul#photos').stop().animate({ 'left' : '-' + (targetPos.left - offset) + 'px' }, 1000, 'swing');	
}

function slideGallery(target, method) {
	
	if (target != '#undefined') {
		doSlide(target, method);		
	} else {
		if ($(current).is(':last-child')) {
			target = '#' + $('ul#photos li:first-child').attr('id');
		} else if ($(current).is(':first-child')) {
			target = '#' + $('ul#photos li:last-child').attr('id');
		} else {  }
		
		doSlide(target, method);		
	}
	
	/* to fade out next/prev */
	/*if ($(current).is(':last-child')) {	$('ul#gallery-nav li#next').fadeTo('fast', .25);
	} else if ($(current).is(':first-child')) { $('ul#gallery-nav li#prev').fadeTo('fast', .25);
	} else { $('ul#gallery-nav li#next').fadeTo('fast', 1); $('ul#gallery-nav li#prev').fadeTo('fast', 1); }*/
	
}

function togglePlay(toggle) {
	if  (toggle != 'stop') {
		if ($('ul#gallery-nav li#play-pause').hasClass('play')) {
			$(document).everyTime(7000, function(i) {
				var target = '#' + $(current).next().attr('id');
				slideGallery(target, 'slide');
			});
		} else {
			$(document).stopTime();
		}	
		$('ul#gallery-nav li#play-pause').hasClass('play') ? $('ul#gallery-nav li#play-pause a').text('Pause') : $('ul#gallery-nav li#play-pause a').text('Play');
		$('ul#gallery-nav li#play-pause').toggleClass('play');
	} else {
		$(document).stopTime();
		$('ul#gallery-nav li#play-pause a').text('Play');
		$('ul#gallery-nav li#play-pause').addClass('play');
	}
}


$(document).ready(function() {

	/* based on 980 grid */
	offset = ($(document).outerWidth()/2) - 490;
	
	$('#gallery').addClass('reel');
	
	$('#gallery').append('<ul id="gallery-nav"><li id="prev"><a href="#" title="Slide to Previous Photo">Previous Photo</a></li><li id="next" title="Slide to Next Photo"><a href="#">Next Photo</a></li><li id="play-pause" title="Play/Pause Animation"><a href="#">Pause</a></li><li id="show-hide" title="Show/Hide Boxes"><a href="#">Hide Boxes</a></li></ul>');
	
		// Set current to 1st
		previous = '';
		current = '#' + $('ul#photos li.start').attr('id');
		$('ul#photos li.start').addClass('current');
		var target = '#' + $(current).attr('id');
		slideGallery(target, 'jump');
		
	
	$('ul#gallery-nav li#next a').click(function() {
		togglePlay('stop');
		var target = '#' + $(current).next().attr('id');
		slideGallery(target, 'slide');
		return false;
	});
	
	$('ul#gallery-nav li#prev a').click(function() {
		togglePlay('stop');
		var target = '#' + $(current).prev().attr('id');
		slideGallery(target, 'slide');
		return false;
	});
	
	/* auto scroll */
	$(document).everyTime(7000, function(i) {
		var target = '#' + $(current).next().attr('id');
		slideGallery(target, 'slide');
	});
	
	$('ul#gallery-nav li#play-pause a').click(function() {
		togglePlay('auto');
		return false;
	});
	
	$('ul#gallery-nav li#show-hide a').click(function() {
		$('ul#gallery-nav li#show-hide').hasClass('show') ? $('#content .box').fadeIn('fast') : $('#content .box').fadeOut('fast');
		$('ul#gallery-nav li#show-hide').hasClass('show') ? $('#secondary-content').fadeIn('fast') : $('#secondary-content').fadeOut('fast');
		$('ul#gallery-nav li#show-hide').hasClass('show') ? $('ul#gallery-nav li#show-hide a').text('Hide Boxes') : $('ul#gallery-nav li#show-hide a').text('Show Boxes');
		$('ul#gallery-nav li#show-hide').toggleClass('show');
		
		return false;
	});
	
	$(".scrollable div.box-scroll").jScrollPane({scrollbarWidth: 9, showArrows: true});
	
	
	// map's API = ABQIAAAAohVKrwnd9-zycewM-ffVJxTYbdWeYibrB0B4Q24JW1Xb0v4SzRTQX1-S9f0M5Ucb_m5lkuiw1KfQdg
	if ($('#map').length > 0) {
		setAddress("209 Swamp Fox Rd., Alexandria, VA 22314", "Delia's Mediterranean Grill &amp; Brick Oven Pizza", "209 Swamp Fox Rd., Alexandria, VA 22314");
	}
	
},function(){
	GUnload();
});

$(window).resize(function() {
	offset = ($(document).outerWidth()/2) - 490;
	var target = '#' + $(current).attr('id');
	slideGallery(target, 'jump');
});
