$(document).ready(function() {

	// Fix parent hover
	$('#pri-nav li ul').hover(
		function () {
			$(this).parent('li').addClass('hover');
		}, 
		function () {
			$(this).parent('li').removeClass('hover');
		}
	);
	
	// Open all external links in a new window
	$("a[href^=http]").each(
		function(){
			if(this.href.indexOf(location.hostname) == -1) {
				$(this).attr('target', '_blank');
			}
		}
	);
	
	
	/* THUMB COLUMNS PAGE */
	if ( $('.thumb-cols') ) {
	
		$('.thumb-col:last-child').css('marginRight','0');
		$('.col-thumbs ul li:last-child').css('marginBottom','0');
		
		var thumbScrollSpeed = 200,
			thumbHeight = 125,
			thumbPadding = 10,
			thumbStep = thumbHeight + thumbPadding,
			thumbsShown = 3,
			thumbcols = $('.thumb-col').length,
			colTitleH = 0;
		
		for ( i = 0; i < thumbcols; i++ ) {
			tempH = $('.col-title:eq('+i+')').height();
			if ( tempH > colTitleH ) { 
				colTitleH = tempH -10;
			}
		};
		
		$('.col-title').height(colTitleH);
		
		/* GIVE COLS ID NUMBERS */
		for ( i = 0; i < thumbcols; i++ ) {
			$('.thumb-col:eq('+i+') ul').attr('id', 'thumbgroup-' + i );
			
			if ( $('#thumbgroup-' + i + ' li').length > 3 ) {
				$('.thumb-col:eq('+i+') .nav-down').attr('href', i );
				$('.thumb-col:eq('+i+') .nav-up').attr('href', i );
				$('#thumbgroup-' + i).attr('rel', 0);
			} else {
				$('.thumb-col:eq('+i+') .col-nav').hide();
			}
		};
		
		/* CLICK HANDLERS FOR THUMB NAV */
		$('.col-nav .nav-down, .col-nav .nav-up').click( function(e) {
			e.preventDefault();
			
			var colNum = $(this).attr('href'),
				totalThumbs = $('#thumbgroup-' + colNum + ' li').length,
				thumbsH = ( (thumbHeight + thumbPadding) * (totalThumbs) ) - thumbPadding,
				difference = ( (thumbHeight + thumbPadding) * thumbsShown) - thumbPadding,
				margin = $('#thumbgroup-' + colNum).attr('rel');
				
			if ( $(this).hasClass('nav-down') ){
				
				if ( margin != (thumbsH - difference) * -1) {
					$('#thumbgroup-' + colNum).attr('rel', (parseInt(margin) - (thumbHeight + thumbPadding) ))
					.animate({
						'marginTop' : '-=' + thumbStep + 'px'
					}, thumbScrollSpeed);
				};
				
			} else {
			
				if ( margin != 0 ) {
					$('#thumbgroup-' + colNum).attr('rel', (parseInt(margin) + (thumbHeight + thumbPadding) ))
					.animate({
						'marginTop' : '+=' + thumbStep + 'px'
					}, thumbScrollSpeed);
				};
				
			};
		});
		
		/* THUMB ZOOM ON HOVER */
		var trigger; // Needed for slimbox slideshow initiation	
			
		$('.col-thumbs ul li').hover( function() {
			var pos = $(this).position(),
				img = $(this).html(),
				name = $(this).children('a').attr('data-name'),
				location = $(this).children('a').attr('data-location');
				
			$('#thumb-zoom').css({
				'left' : pos.left,
				'top' : pos.top
			});
			
			$('#thumb-zoom .zoom-image').html(img);
			$('#thumb-zoom .zoom-name').text(name);
			$('#thumb-zoom .zoom-location').text(location);
			$('#thumb-zoom a').click(function(e) {
				e.preventDefault();
			});
			
			trigger = $(this).children('a').attr('rel');

			$(this).children('a').removeAttr('rel');
			
			modal();
			
			//$('.col-thumbs ul li a:first-child').trigger('click');

			
			$('#thumb-zoom').show();
		
		}, function(){
			
			$(this).children('a').attr('rel', trigger);
		
		});
		
		$('#thumb-zoom .zoom-image').mouseleave( function() {
			$('#thumb-zoom').hide();
		});
	
	};
	
	
	//$("a[rel^='prettyPhoto']").prettyPhoto();
	
	
});

function modal() {
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animation_speed: 'fast', /* fast/slow/normal */
		slideshow: 5000, /* false OR interval time in ms */
		autoplay_slideshow: false, /* true/false */
		opacity: 0.80, /* Value between 0 and 1 */
		show_title: false, /* true/false */
		allow_resize: false, /* Resize the photos bigger than viewport. true/false */
		default_width: 500,
		default_height: 344,
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'pp_default', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
		horizontal_padding: 20, /* The padding on each side of the picture */
		hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
		wmode: 'opaque', /* Set the flash wmode attribute */
		autoplay: true, /* Automatically start videos: True/False */
		modal: false, /* If set to true, only the close button will close the window */
		deeplinking: false, /* Allow prettyPhoto to update the url to enable deeplinking. */
		overlay_gallery: true, /* If set to true, a gallery will overlay the fullscreen image on mouse over */
		keyboard_shortcuts: false, /* Set to false if you open forms inside prettyPhoto */
		changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
		callback: function(){}, /* Called when prettyPhoto is closed */
		ie6_fallback: true,
		markup: '<div class="pp_pic_holder"> \
					<div class="ppt">&nbsp;</div> \
					<div class="pp_content_container"> \
						<div class="pp_content"> \
							<div class="pp_loaderIcon"></div> \
							<div class="pp_fade"> \
								<a href="#" class="pp_expand" title="Expand the image">Expand</a> \
								<div class="pp_hoverContainer"> \
									<a class="pp_next" href="#">next</a> \
									<a class="pp_previous" href="#">previous</a> \
								</div> \
								<div id="pp_full_res"></div> \
								<div class="pp_details"> \
									<a class="pp_close" href="#">Close</a> \
									<p class="pp_description"></p> \
								</div> \
								<div class="clear">&nbsp;</div> \
							</div> \
						</div> \
					</div> \
				</div> \
				<div class="pp_overlay"></div>',
				
		gallery_markup: '<div class="pp_gallery"> \
							<div> \
								<div class="gallery-slider"> \
									<ul> \
										{gallery} \
									</ul> \
								</div> \
							</div> \
						</div>',
						
		image_markup: '<img id="fullResImage" src="{path}" />',
		flash_markup: '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{width}" height="{height}"><param name="wmode" value="{wmode}" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="{path}" /><embed src="{path}" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="{width}" height="{height}" wmode="{wmode}"></embed></object>',
		quicktime_markup: '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" height="{height}" width="{width}"><param name="src" value="{path}"><param name="autoplay" value="{autoplay}"><param name="type" value="video/quicktime"><embed src="{path}" height="{height}" width="{width}" autoplay="{autoplay}" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/"></embed></object>',
		iframe_markup: '<iframe src ="{path}" width="{width}" height="{height}" frameborder="no"></iframe>',
		inline_markup: '<div class="pp_inline">{content}</div>',
		custom_markup: '',
		social_tools: 'false' /* html or false to disable */
	});
};
