$(document).ready(function() {
	
	// set main menu selected item
	var pathParts = location.pathname.split('/');
	var menu = pathParts[1];
	if(menu == '' || menu == 'index.php') {
		menu = '/';
	} else {
		menu = '/' + menu + '/';
	}
	$('#main_menu a[href="' + menu + '"]').addClass('selected');
	
	// open external links in new tab
	$('a:not(#social .youtube li a, .map_position, #portfolio_thumbs a)').each(function() {
		if($(this).attr('href').substr(0, 14) != 'http://wizpip.' && $(this).attr('href').substr(0, 7) == 'http://') {
			$(this).click(function() {
				window.open($(this).attr('href'));
				return false;
			});
		}
	});
	
	// start homepage slideshow
	if($('.slideshow').length) {
		$('.slideshow').slideshow();
		$('.slideshow li').css({left: "0"});
	}
	
	// start homepage reel carousel
	if($('#social .flickr ul').length) {
		$('#social .flickr ul').reelCarousel();
	}
	
	// change selected video 
	$('#social .youtube li a').click(function() {
		var image = $(this).find('img').attr('src').split("/");
		var video = image[4];
		$(this).parents('.youtube').find('object').attr('data', 'http://www.youtube.com/v/' + video + '&amp;hl=en');
		$(this).parents('.youtube').find('param[name="movie"]').attr('value', 'http://www.youtube.com/v/' + video + '&amp;hl=en');
		// need to pull it out and re-insert it into the dom for all browsers (except FF) to work properly
		var html = $(this).parents('.youtube').clone(true);
		$(this).parents('.youtube').after(html).remove();
		return false;
	});
	
	// beautify code samples
	if($('pre.code').length) {
		$.beautyOfCode.init({
			brushes: ['Xml', 'JScript', 'CSharp', 'Plain', 'Php']
		});
	}
	
	// start accordions
	if($('.accordion').length) {
		$('.accordion').accordion();
	}
	
	// gmap homepage
	$('.map_position').click(function() {
		if($('#map_container').length == 0) {
			$(this).parents('.box').append('<div id="map_container"><a class="close" href="#"></a><div id="map"></div></div>');
			$('#map_container').fadeOut(0).fadeIn(1000);
		}
		var latlng = $(this).find('.coords').text().split(/, /);
		latlng = new google.maps.LatLng(latlng[0], latlng[1]);
		var options = {
			zoom: 15,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(document.getElementById("map"), options);
		var marker = new google.maps.Marker({
			position: latlng
		});
		marker.setMap(map);
		$('#map_container .close').click(function() {
			$('#map_container').fadeOut(1000, function() {
				$(this).remove();
			});
			return false;
		});
		return false;
	});
	
	
	// portfolio stuff
	if($('#portfolio_main_piece').length) {
		$('#portfolio_thumbs li:first').addClass('current');
		$('#portfolio_thumbs li:first').hide();
		$('#portfolio_thumbs').height($('#portfolio_thumbs').height());
		$('#portfolio_thumbs > li').each(function() {
			var position = $(this).position();
			$(this).css({left: position.left, top: position.top});
		});
		$('#portfolio_thumbs > li').css('position', 'absolute');
		$('#portfolio_main_piece').html($('#portfolio_thumbs li:first').html());
		$('#portfolio_main_piece a').click(function() {
			window.open($(this).attr('href'));
			return false;
		});
		$('#portfolio_thumbs .current').css({top: '-383px', left: '10px'}).find('img').css('width', '500px');
		$('#portfolio_thumbs .image a').click(function() {
			if($('#portfolio_thumbs li:animated').length) return false;
			var obj = $(this);
			$('#portfolio_main_piece .content').fadeOut(500, function() {
				$(this).html(obj.parents('li').find('.content').html());
				$(this).fadeIn(500);
			});
			$('#portfolio_main_piece .image').hide();
			$('#portfolio_thumbs .current').show().animate({top: $(this).parents('li').css('top'), left: $(this).parents('li').css('left')}, 1000);
			$('#portfolio_thumbs .current img').animate({width: '225px'}, 1000);
			$('#portfolio_thumbs .current').removeClass('current');
			$(this).parents('li').addClass('paper').animate({top: '-383px', left: '10px'}, 1000, function() { $(this).removeClass('paper'); });
			$(this).parents('li').find('img').animate({width: '500px'}, 1000, function() {
				$('#portfolio_main_piece .image').html($(this).parents('li').find('.image').html()).show();
				$(this).parents('li').addClass('current').hide();
				$('#portfolio_main_piece a').click(function() {
					window.open($(this).attr('href'));
					return false;
				});				
			});
			return false;
		});
	}
	
});