
var $i = 0, ext_search = '', nav_vis = true, logo_vis = true,
	pop_open = false,
	scrollTop = document.documentElement ? document.documentElement.scrollTop : document.body.scrollTop;

function toggleLogo() {
	if (logo_vis) {
		$('#outer-logo-box').slideUp();
		$('#show-logo').show();
		logo_vis = false;
	}
	else {
		$('#outer-logo-box').slideDown();
		$('#show-logo').hide();
		logo_vis = true;
	}
	return false;
}

function toggleNav() {
	if (nav_vis) {
		$('#nav li').slideUp();
		$('#show-nav').show();
		$('#hide-nav, #show-logo').hide();
		nav_vis = false;
	}
	else {
		$('#hide-nav').show();
		if (!logo_vis) $('#show-logo').show()
		$('#show-nav').hide();
		$('#nav li').slideDown();
		nav_vis = true;
	}
	return false;
}

function callSearch(engine){
	ext_search = ext_search.replace(/\s+/g, ' ');
	if (engine == 'wiki') {
		window.location.href='http://en.wikipedia.org/w/index.php?search=' + encodeURI(ext_search);
	}
	else if (engine == 'google') {
		window.location.href='http://www.google.com/search?q=' + encodeURI(ext_search);
	}
	else if (engine == 'bing') {
		window.location.href='http://www.bing.com/search?q=' + encodeURI(ext_search);
	}
	else {
		return false; // nuttin'
	}
}

function toggleFlip() {
	$('#flip').animate({
		left: parseInt($('#flip').css('left'), 10) == 0 ? -$('#flip').outerWidth() : 0
	});
	return false;	
}
var bg_img = ['/assets/home-bg-milford-sound.jpg',
	'/assets/home-bg-ne.jpg',
	'/assets/home-bg-white-cloud.jpg',
	'/assets/home-bg-canyon.jpg',
	'/assets/home-bg-sd.jpg',
	'/assets/home-bg-mt.jpg',
	'/assets/home-bg-henge.jpg',
	'/assets/home-bg-osage.jpg'];

var h1_color = ['#fff',
	'#2883C2',
	'#2883C2',
	'#2370A6',
	'#2D8DCF',
	'#2370A6',
	'#2370A6',
	'#fff'];

function toggleGalleryInfo(){
	$('#album-how').toggle();
	$('#less-link').toggle();
	$('#more-link').toggle();
	return false;
}

function isNotAlbum(){return (!$('body').hasClass('album') && !$('body').hasClass('x-album'))}
function isAlbum(){ return (!isNotAlbum())}

$(document).ready(function(){
	$('#btop').click(function(){$('#bbox').trigger('click');window.scrollTo(0,0); return false;});
	$('#bback').click(function(){$('#bbox').trigger('click');history.back();return false});
	if (isNotAlbum()) { // albums don't have bbox or pop-nav
		$('#bbox').css('height',
			(Math.round(($('#footer-flux').offset().top - $('#bbox').offset().top)) - 40) + 'px').mouseenter(
				function(e){
					var fire = jQuery.Event('click');
					fire.pageY = e.pageY;
					fire.pageX = e.pageX;
					$(this).trigger(fire);
				}).mouseout(
					function(e){
						var p = $('#bbox');
						if (pop_open) {
							if ((p.offset().left + p.width() < e.pageX) ||
							 	(e.pageX < p.offset().left) ||
								(e.pageY < p.offset().top) || 
								(e.pageY > p.offset().top + p.height())) {
									$(this).trigger('click');
							}							
					}
					}).click(function(e) {
						if (pop_open) {
							$('#pop-nav').hide();
							pop_open = false;
						}
						else {
							if ($('body').scrollTop() !== 0) {
								$('#btop').parent().show();
							}
							else if (document.documentElement && document.documentElement.scrollTop) {
								$('#btop').parent().show();
							}
							else {
								$('#btop').parent().hide();
							}
							// this 'move' code keeps the menu inside of bbox
							var move = ((e.pageY < ($('#bbox').offset().top) + $('#pop-nav').height()/2)) ? -20 : $('#pop-nav').height()/2;
							move = (($('#footer-flux').offset().top - $('#pop-nav').height()) < (move + e.pageY)) ?
								$('#pop-nav').height() :
								move;
							$('#pop-nav').css('top', (e.pageY - (move + $('#bbox').offset().top)) + 'px').show(333);
							pop_open = true;
						}
					});
	}
	$('#pop-nav, #album-how, #less-link').toggle();		
	if ($(window).width() > 767) {
		if (isNotAlbum()) {
			var bd = Math.floor(Math.random()*8);
			$('body').css('background-image', 'url(' + assets_path + bg_img[bd] + ')');
			$('h1').css('color', h1_color[bd]);		
		}
	}
	if (!$('body').hasClass('home')) { // only show the logo on the home page
		$('#outer-logo-box').css('display', 'none');
		$('#show-logo').show();
		logo_vis = false;
	}
	window.onorientationchange = function() {
		if ($('body').hasClass('album')) {
		    window.location.reload();			
		}
	}
	$('#outer-logo-box').click(function(){return toggleLogo()});
	$('a[href=#yelp]').click(function(){alert('Sorry! Not available yet.'); return false;})
	$('.screen').css({ opacity: 0.75 });
	var re = new RegExp('/$');
	if (re.test(window.location.href)) {
		$('#index').addClass('active');
	}
	else {
		$('a.sleft').each(function(){
			var re = new RegExp($(this).attr('id'));

			if (re.test(window.location.href)) {
				$(this).addClass('active');
			}
		});	
	}

	$('a.sleft').click(function(){
		if ($(this).hasClass('active')) {
			return toggleFlip();
		}
	});

	$('img.resize').each(function(){
			var img = $(this)[0];
			img.oldx = img.offsetWidth;
			img.oldy = img.offsetHeight;
			img.oldw = $(this).attr('width');
			img.oldh = $(this).attr('height');
			img.resizable = true;
			img.oldz = 1.0;
			resizeImg(img);
		}
	);
	
	$('#winx').html($(window).width());
	$('#winy').html($(window).height());
	
	$(window).resize(function() {
		$('img.resize').each(function(){ resizeImg($(this)[0])});
		$('#winx').html($(window).width());
		$('#winy').html($(window).height());
		if ($('#bbox').css('height')) {
			$('#bbox').css('height', (Math.round(($('#footer-flux').offset().top - $('#bbox').offset().top)) - 40) + 'px')
		}
	});

	$('#contact-div a, #contact-li a').click(function(){
		return toggleContact();
	});

	$('a.sleft').click(function(){
		if ($(this).hasClass('active')) {
			return toggleFlip();
		}
	});
	$('#contact').click(function(){return toggleContact()});

	$('#search-popup').css('display', 'block');
	if ($('a.ext-search').cluetip) {
		$('a.ext-search').cluetip({
			cluetipClass: 'jtip',
			positionBy: 'auto',
			width:236,
			arrows: false,
			dropShadow: true,
			hoverIntent: true,
			sticky: true,
			mouseOutClose: true,
			closePosition: 'title',
			closeText: 'x',
			local:true,
			showTitle: false,
			activation: 'click',
			fx: { open: 'fadeIn', openSpeed: 'slow' },
			onActivate: function(e) {ext_search = e.prev().html(); return true;}
		});
	}
});

function toggleContact() {
	var p = $('#contact');
	if (p.hasClass('open')) {
		$('#contact').slideUp(250);			
		p.removeClass('open');
	}
	else {
		p.slideDown(250);
		p.addClass('open');
	}
	return false;
}

function resizeImg(img) {
	if (img.resizable) {
		img.style.width="100%";
		var screen_ratio = (img.offsetWidth/$(window).width());
		var ratio = Math.round((img.offsetWidth / img.oldx) * 10) / 10;
		if (screen_ratio > 0.5) {ratio = 0.60}
		if (ratio >= 1) {
			img.style.width = '';
			img.style.height = '';
		}
		else {
			if (ratio != img.oldz) {
				img.style.height = (img.oldy * ratio) + 'px';
				img.style.width = (img.oldx * ratio) + 'px'
			}
			img.oldz = ratio;					
		}
	}	
}
/*	Begin :: Flip */
function flipIt(pID) {
	
}
/* End :: Flip */

function say(car, cdr, text) {
    var a = new Array('o', 't', 'l', 'i', 'a', 'm');
    a.reverse(); car.reverse(); cdr.reverse();
    var a_joined = a.join('');
    var j = car.join('') + '&' + '#6' + '4;' + cdr.join('.');
    if ((text == null) || (text == '')) { text = j }
    document.write('<a href="' + a_joined + ':' + j + '">' + text + '</a>');
    return true;
}

