var ajaxPost;

$(document).ready(function() {
	/*******************************************************************************************************************************/
	
	/* Global Variables */
	var win_width = $(window).width();
	var win_height = $(window).height();
	
	var mast_height = 50;
	var top = $('#top');
	
	// slideshow variables
	var img_ratio = 0.44860557768924303;
	var img_height = Math.floor(win_width * img_ratio);
	var img_num = $('.slideshow ul.imgs img').size();
	var slide_speed = 1000;
	var slide_position = 0;
	var li_array = new Array();
	var active = 0;
	var slide_inc = 1;
	var slide_interval = null;
	var interval_speed = 10000;
	
	var det_height;
	var headline_height = 90;
	var counter_num_height = 20;
		
	/* If Browser is Safari */
	//if ($.browser.safari) $('body').addClass('webkit');
	
	/* If on Home Page */
	if ( $('.home').length > 0 ) buildSlideshow();
	
	//for 'most popular posts' widget
	$('a.wppgen').addClass('hover_img');
		
	
	/*******************************************************************************************************************************/
	// main navigation:
	var pathname = window.location.pathname;
	
	switch (pathname) {
		case '/':
			// is home
			break;
		
		case '/journey/':
			// is journey
			$('#header a.journey, #footer a.journey').addClass('active');
			break;
			
		case '/our-universe/':
			// is our universe
			$('#header a.universe, #footer a.universe').addClass('active');
			break;
		
		case '/explorers/':
			// is our universe
			$('#header a.explorers, #footer a.explorers').addClass('active');
			$('aside').hide();
			break;
		
		default:
			// is blog
			$('#header a.blog, #footer a.blog').addClass('active');
			break;
	}
	/*******************************************************************************************************************************/
	
	
	
	/*******************************************************************************************************************************/
	// home slideshow
	function buildSlideshow() {
		$.ajax({
			type: "GET",
			url: "/wp-content/themes/lutionmedia/slideshow.xml",
			dataType: "xml",
			success: parseXml,
			complete: setupSlideshow
		});
	}
	
	// parse slideshow xml
	function parseXml(xml) {
		var counter = 0;
		$(xml).find("slide").each(function() {
			var image = $(this).find('image').text();
			var headline = $(this).find('headline').text();
			var blurb = $(this).find('blurb').text();
			var hyperlink = $(this).find('hyperlink').text();
			counter++;
			
			$('.imgs').append('<li><img src="' + image + '" alt="" /></li>');
			$('.counter ul').append('<li>0' + counter + '</li>');
			$('.headlines ul').append('<li>' + headline + '</li>');
			$('.desc ul').append('<li>' + blurb + '</li>');
			$('.hyperlink ul').append('<li><a class="view" href="' + hyperlink + '">View</a></li>');
		});	
	}
	
	// slideshow setup
	function setupSlideshow() {
		// descriptions
		$('.slideshow .desc ul li').each(function() {
			li_array.push($(this).height());
		});
		$('.slideshow .desc').css('height', li_array[0]);
		
		handleResize();
		setupGalleryControls();
	}
	
	// change slide function
	function changeSlide(inNum) {
		
		active = inNum;
		slide_position = -img_height * slide_inc;
		
		// IMAGE SELECTORS
		$('.selectors a').removeClass('active');
		$('.selectors li:nth-child('+(slide_inc+1)+') a').addClass('active');
		
		// SLIDES
		$('ul.imgs').stop(true,false).animate({
			'top' : Math.floor(-img_height) * slide_inc
		},{
			duration : slide_speed,
			easing   : 'easeInOutExpo'
		});
		
		// NUMBERS
		$('.counter ul').stop(true,false).animate({
			'top' : (-counter_num_height * slide_inc)
		},{
			duration : slide_speed,
			easing   : 'easeInOutExpo'
		});
		
		// HEADLINES
		$('.headlines ul').stop(true,false).animate({
			'top' : (-headline_height * slide_inc)
		},{
			duration : slide_speed,
			easing   : 'easeInOutExpo'
		});
		
		// DESCRIPTIONS
		$('.desc').stop(true,false).animate({
			'height' : li_array[slide_inc]
		},{
			duration : slide_speed,
			easing   : 'easeInOutExpo'
		});
		
		// get the postion for the .desc ul
		if (slide_inc == 0 ) {
			var ul_top = 0;
		} else if (slide_inc == 1) {
			var ul_top = li_array[0] * -1;
		} else if (slide_inc == 2) {
			var ul_top = (li_array[0] + li_array[1]) * -1;
		}
		$('.desc ul').stop(true,false).animate({
			'top' : ul_top
		},{
			duration : slide_speed,
			easing   : 'easeInOutExpo'
		});
		
		// HYPERLINKS
		$('.hyperlink ul').stop(true,false).animate({
			'top' : -25 * slide_inc
		});
		
		// set .details .center height (used for window resize)
		det_height = $('.slideshow .details .center').height() + 100;
		
		// change slide_inc
		(slide_inc != 2) ? slide_inc++ : slide_inc = 0;
	}
	
	// slidewhow controls setup
	function setupGalleryControls() {
		
		$('.slideshow').each(function() {
			
			var images = $('ul.imgs', this);
			var li = $('ul.imgs li', this);
			var img = $('ul.imgs li img', this);
			
			var headlines = $('.headlines ul', this);
			var title = $('.giant', this);
			
			var desc = $('.desc', this);
			
			var i = 0;
			
			var counter = $('.counter ul', this);
			
			var prev = $('.prev');
			var next = $('.next');
			var selector = $('.selectors a', this);
			var play = $('.play');
			var playing = true;
			var slide_interval = setInterval(function() {
				changeSlide(slide_inc);
			}, interval_speed);
			
			// prev/next buttons
			$(next).click(function() {
				playing = false;
				clearInterval(slide_interval);
				$(play).css('background-position', '-14px 0');
				(slide_inc != 2) ? slide_inc++ : slide_inc = 0;
				changeSlide(slide_inc);
				return false;
			});
			
			$(prev).click(function() {
				playing = false;
				$(play).css('background-position', '-14px 0');
				clearInterval(slide_interval);
				changeSlide(slide_inc);
				return false;
			});
			
			// play/pause button
			$(play).mouseover(function() {
				if (playing == true) {
					// stop slideshow
					$(this).css('background-position', '0 -14px');
				} else {
					// start slideshow
					$(this).css('background-position', '-14px -14px');
				}
			}).mouseout(function() {
				if (playing == true) {
					// stop slideshow
					$(this).css('background-position', '0 0');
				} else {
					// start slideshow
					$(this).css('background-position', '-14px 0');
				}
			}).click(function() {
				if (playing == true) {
					// stop slideshow
					playing = false;
					$(this).css('background-position', '0 0');
					clearInterval(slide_interval);
				} else {
					// start slideshow
					playing = true;
					$(this).css('background-position', '-14px 0');
					clearInterval(slide_interval);
					changeSlide(slide_inc);
					slide_interval = setInterval(function() {
						changeSlide(slide_inc);
					}, interval_speed);
				}
			});
			
			// image selectors (dots)
			var tally = 0;
			$(selector).each(function() {
				var num = tally;
				tally++;
				
				$(this).click(function() {
					playing = false;
					$(play).css('background-position', '-14px 0');
					clearInterval(slide_interval);
					slide_inc = num;
					changeSlide(slide_inc);
					
					$(selector).removeClass('active');
					$(this).addClass('active');
					
					return false;
				});
			});
						
			// Continue Button
			$('.continue').click(function() {
				$.scrollTo({'top':win_height - mast_height, 'left':0}, 450);
				return false;
			});
			
		});
	
	}
	/*******************************************************************************************************************************/
	
	
	
	/*******************************************************************************************************************************/
	// WINDOW RESIZE HANDLER
	$(window).resize(function() {
		handleResize();
	});
	
	function handleResize() {
		
		/*************************************************/
		// content resize
		win_width = $(window).width();
		win_height = $(window).height();
		
		img_height = Math.floor(win_width * img_ratio);
		img_num = $('.slideshow ul.imgs img').size();
		
		$('.section').css('height', win_height+'px');
		$('.section:nth(0)').css('height', win_height - 50);
		
		/*************************************************/
		// slideshow resize
		slide_position = -img_height * active;
		
		$('.slideshow').css({
			'height' : win_height - mast_height
		});
		$('.slideshow .mask').css({
			'width'  : win_width,
			'height' : img_height
		});
		$('.slideshow ul.imgs').css({
			'width' : win_width,
			'height': img_height * img_num,
			'top'   : slide_position
		});
		$('.slideshow ul.imgs img').css({
			'width' : win_width
		});
		$('.continue').css({
			'right' : (win_width - 960) / 2,
			'bottom': 0
		});
				
		//check details size and adjust
		var show_height = $('.slideshow').height();
		var mask_height = $('.slideshow .mask').height();
		var details_height = $('.slideshow .details').height();
		
		// if the remaining area under the mask is 
		// less than that of the detail's height:
		if ( details_height > (show_height - mask_height) ) {
			// too small
			$('.slideshow .details').css({
				'top' : (show_height - details_height)
			});
		} else {
			// just right
			$('.slideshow .details').css({
				'top'   : mask_height,
				'height': ($('.slideshow .details .center').height() + 60)
				//'height': (show_height - mask_height)
			});
		}
		
		/*************************************************/
		// 'scale' class resize
		$('.scale').each(function() {
			$(this).css('width', win_width);
		});
		
		/************************************************/
		// AJAX POST: background image resize
		if ( $('#ajax_post').length > 0 ) {
			$('#ajax_post .bg img').css('width', win_width);
		}
		
		/************************************************/
		// resize .single_wrap & .var_width 
		if ( win_width > 1290 ) {
			$('.single_wrap').css({
				'width' : 1290
			});
			$('.var_width').css({
				'width' : 1290
			});
			$('.hide_div').css({
				'display' : 'block'
			});
			$('#latest_info .col_2').css({
				'width' : 600
			});
		} else if ( win_width > 966 ) {
			$('.single_wrap').css({
				'width' : 966
			});
			$('.var_width').css({
				'width' : 966
			});
			$('.hide_div').css({
				'display' : 'none'
			});
			$('#latest_info .col_2').css({
				'width' : 300
			});
		}
		
		$('.single_wrap .left_col').height( $('.single_wrap').height() );
	}
	/*******************************************************************************************************************************/
	
	
	/*******************************************************************************************************************************/
	// HOVERING IMAGE BORDERS
	$('a.hover_img img, a.hover_img_white img', this).imagesLoaded(initSqueeze());
	
	function initSqueeze() {
		$('a.hover_img').imgSqueeze ({
			speed     : 250,
			color_on  : '#0b0b0b',
			color_off : '#0b0b0b',
			ease_on   : 'easeOutQuad',
			ease_off  : 'easeInQuad'
		});
		$('a.hover_img_white').imgSqueeze ({
			speed     : 250,
			color_on  : '#fff',
			color_off : '#fff',
			ease_on   : 'easeOutQuad',
			ease_off  : 'easeInQuad'
		});
	}
	/*******************************************************************************************************************************/
	
	
	/*******************************************************************************************************************************/
	// DISABLE LINKS TO CATEGORIES
	$('a[rel="category tag"]').live('click', function() {
		return false;
	});
	/*******************************************************************************************************************************/
	
	
	/*******************************************************************************************************************************/
	// Our Universe Gallery Slideshow
	if ( $('body.page-template-page-universe-php').length > 0 ) {
		$('#history_slides').each(function() {
			var ul_item = $('ul li', this);
			var button = $('ol li a', this);
			
			$(button).each(function() {
				$(this).click(function() {
					$(button).removeClass('active');
					$(this).addClass('active');
					$(ul_item).fadeOut()
					$('ul li:nth-child('+$(this).html()+')').fadeIn();
					return false;
				});
			});
		});
	}
	/*******************************************************************************************************************************/
	
	
	/*******************************************************************************************************************************/
	// Grid Post width correction
	$('.grid_post').each(function() {
		var new_width = $('.thumb', this).width();
		$(this).css('width', new_width);
	});
	/*******************************************************************************************************************************/
	
	
	/*******************************************************************************************************************************/
	// Ajax Posts
	var top_gap = 68;
	var use_height;
	
	ajaxPost = function (post_id, inParent) {
		
		$.ajaxSetup({cache:false});
		
		var top_gap = 68;
		
		$('#ajax_wrap').height(0);
		$('#ajax_post').load('/'+inParent+'-ajax-post/',{id:post_id}, function() {
			setupPost(inParent);
			$('#ajax_post .bg img').css('width', win_width);
			//$('#ajax_post a.hover_img_white').imgSqueeze(250, '#fff');
			$('#ajax_post a.hover_img_white', this).imagesLoaded(initSqueeze());
		});
		
		$.scrollTo({'top':$('#ajax_wrap').offset().top - top_gap, 'left':0}, 450);
		$('#load_bg').css('width','0%').show();
		$('#ajax_wrap, #load_wrap').animate({height:'5px'},{queue:false, duration:300, easing:'easeInOutQuad'});
		
		$('#load_bar').animate({height:'5px'},300, function() {
			$('#load_bg').animate({width:'100%'},2500, function(){
				$('#load_wrap').height(0);
				
				handleResize();
				
				var win_height = $(window).height() - top_gap;
				var drawer_height = $('#ajax_post .desc').height() + 300;
				(win_height > drawer_height) ? use_height = win_height : use_height = drawer_height;
				
				$('#ajax_wrap, #ajax_post, #ajax_wrap .post').animate({
					height : use_height
				},{
					queue:false, 
					duration:1000, 
					easing:'easeInOutExpo'
				});
				
			});
		});
		
	}
	
	function setupPost(inParent) {
		$('#ajax_post').each(function() {
			
			// close button
			$('.close').click(function() {
				$('#ajax_wrap, #ajax_post').animate({'height':0}, 800, function() { $('#ajax_post').html(''); });
			});
			
			// 'back to list' button
			$('.back_to_list').click(function() {
				$.scrollTo({'top':$('#ajax_post').offset().top + use_height - 50, 'left':0}, 450);
			});
			
			/***************************************/
			if (inParent == 'journey') {
				var video = true;
				var desc = $('.desc', this);
				var feat = $('.featured', this);
				
				// show/hide project description
				$('a.arrow_sm', this).click(function() {
					if (video) {
						/* show desc */
						$(feat).css('display','none');
						$(desc).css('display','block');
						video = false;
					} else {
						/* show video */
						$(desc).css('display','none');
						$(feat).css('display','block');
						video = true;
					}
					return false;
				});
			} else if (inParent == 'explorers') {
				var bio = $('.desc', this);
				var projects = $('.projects', this);
				
				if ( $('#bio_text').html() != '' ) {
					$(bio).show();
				} else {
					$(projects).show();
					$('.btn-explorers-proj a').addClass('active');
					$('.btn-explorers-bio a').hide();
				}
				
				$('.btn-explorers-bio a').click(function() {
					$(bio).css('display','block');
					$(projects).css('display','none');
					
					$(this).addClass('active');
					$('.btn-explorers-proj a').removeClass('active');
					
					return false;
				});
				$('.btn-explorers-proj a').click(function() {
					$(bio).css('display','none');
					$(projects).css('display','block');
					
					$(this).addClass('active');
					$('.btn-explorers-bio a').removeClass('active');
					
					return false;
				});
			}
		});
	}
	/*******************************************************************************************************************************/
	
	
	/*******************************************************************************************************************************/
	// initialize ajax caller links
	$('.grid_post .entry a').live('click', function() {
		if ( $('body').hasClass('page-template-page-journey-php') ) {
			var page_type = 'journey';
		} else {
			var page_type = 'explorers';
		}
		ajaxPost($(this).attr("rel"), page_type);
		return false;
	});
	/*******************************************************************************************************************************/
	
	
	/*******************************************************************************************************************************/
	// Initialize
	var t = setTimeout(function() {
		handleResize();
	}, 300);
	/*******************************************************************************************************************************/
	
}).ajaxComplete(function(){
	try{
		FB.XFBML.parse(); 
	}catch(ex){}
});
