var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

// custom fadein/fadeout for fixing IE and ClearType issues
(function($) {
	$.fn.myFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.myFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);

function jqExternalLinks() {
	$('a.link').filter('[href^="http://"]').attr( 'target', '_blank' );
	$('a.disable').click( function() { return false; } );
}

var main_spacer = 60, // 200,
	scrollTimer = null,
	animating = false,
	blockSoftware = null,
	blockWeb = null,
	blockPhoto = null,
	blockTravel = null;

function scrollWin( theAnchor, spacer ) {
	var scrollAnchor = $(stripSection(theAnchor));	
	$('html,body').animate( { scrollTop: scrollAnchor.offset().top - spacer }, 400 );
}

function stripSection( theAnchor ) {
	return theAnchor.replace('section-', '');
}

function createFixedNav() {
	log( 'fixed' );	
	$('#toplink').fadeIn(); 
	$('#mini-title').css( 'bottom', '23px' ).fadeIn();
	$('#navbg').removeClass('standard-nav').addClass('fixed-nav');
	$('ul#nav, ul#about').css( 'bottom', '30px' );
}

function removeFixedNav() {
	log( 'remove fixed' );
	$('#toplink').fadeOut(); 
	$('#mini-title').css( 'bottom', '0px' ).fadeOut();
	$('#navbg').removeClass('fixed-nav').addClass('standard-nav');
	$('ul#nav, ul#about').css( 'bottom', '10px' );
}

function showTopArrow() {
	$('.top-icon').fadeIn();
}

function hideTopArrow() {
	$('.top-icon').fadeOut();
}

function navbarLogic() {
	winScroll = $(window).scrollTop();
	log( winScroll );
	
	if ( winScroll > 135 ) {
		createFixedNav();
		showTopArrow();
	}
	else if ( winScroll <= 135 ) {
		removeFixedNav();
		hideTopArrow();
	}
}

var navbarTimer = null;

function timerNavbarLogic() {
	clearTimeout( navbarTimer );
	navbarTimer = setTimeout( 'navbarLogic()', 30 );
}


function carouselButtonLogic( itemsVisible ) {
	// a.filter(':last').css( 'background-color', 'red' );

	// beginning
	if ( itemsVisible.filter(':first').attr('id') == 'jump' ) {
		$('.web-nav #left').fadeOut(200);
	}
	
	// end
	else if ( itemsVisible.filter(':last').attr('id') == 'cornerstone' ) {
		$('.web-nav #right').fadeOut(200);
	}
	
	// middle
	else {
		$('.web-nav #left, .web-nav #right').fadeIn(200);
	}	
}

function carouselSetup() {
	$('#website-nav-carousel').jCarouselLite({
		visible: 4,
		mouseWheel: true,
		circular: false,
		btnPrev: '.web-nav #left',
		btnNext: '.web-nav #right',	

	    afterEnd: function( visItems ) {
	    	carouselButtonLogic( visItems );
	    }
	});
	
	var webinfoItems = $('#website-content-block');
	
	// setup display
	webinfoItems.find( '.webinfo-block' ).hide();
	webinfoItems.find( '#info-jump' ).show();
	
	// attach events to click on carousel
	var carouselItems = $('#website-nav-carousel .item-crsl');
	carouselItems.click( function() {
		// handle styles on clicked image
		carouselItems.removeClass('selected');		
		carouselItems.find('img').css('filter', 'alpha(opacity=40)').css('opacity','0.4');
		$(this).addClass('selected');
		$(this).find('img').css('filter', 'alpha(opacity=100)').css('opacity','1');
		
		// handle hide and show (fading!)
		webinfoItems.find( '.webinfo-block' ).hide();
		webinfoItems.find( '#info-' + $(this).attr('id') ).myFadeIn();
		
		// scroll to the thing
		scrollToContent( '#website-nav-block', main_spacer - 20 );
	});
	
	// fade out first left arrow (not circular)
	$('.web-nav #left').fadeOut(200);
	$('.web-nav #right').fadeOut(200).fadeIn(200);
	
	// create hover in and out for carousel items
	var carouselLI = $('#website-nav-carousel li').hover( 
		function() { 
			$(this).find('img').css('filter', 'alpha(opacity=100)').css('opacity','1'); 
		}, 
		function() { 
			if ( ! $(this).hasClass('selected') ) {
				$(this).find('img').css('filter', 'alpha(opacity=40)').css('opacity','0.4'); 
			}
		}
	);
}

function detectIE( openPopup ) {
	// BrowserDetect.browser + " " + BrowserDetect.version + " on " + BrowserDetect.OS
	if ( ( BrowserDetect.browser == "Explorer" && BrowserDetect.version <= 8 ) || openPopup == true ) {
		$(this).colorbox( { 
			href:"ie.html", title:"It's not 2004 anymore, guys.",
			width:'680px', height:'540px', 
			iframe:true, 
			open:true } );
	}
}

function scrollToContent( theAnchor, spacer ) {	
	var scrollAnchor = $(stripSection(theAnchor));
	log( 'scrollToContent: ' + stripSection(theAnchor) );
	$('html,body').animate( { scrollTop: scrollAnchor.offset().top - spacer }, 400, function() { navbarLogic(); } );
}

function handleAnchor( href ) {
	var anchor = href.substring( href.lastIndexOf('#'), href.length );
	if ( href.lastIndexOf('#') != -1 ) {
		scrollToContent( anchor, main_spacer );
	}
}

function log( item ) {
	// only enable this when needing to log debug	
	/*
	if ( console ) {
		console.log( item );
	}
	// */
}