// If JavaScript is enabled remove no-js class
jQuery('html').removeClass('no-js');

/* ---------------------------------------------------------------------- */
/*	Main Navigation
/* ---------------------------------------------------------------------- */

$(function(){

	// Navigation Dropdown
	$('#nav li').hover(function() {
		if( $(this).children('ul').length ) $(this).addClass('hover');
		$(this).children('ul').hide().stop(true, true).slideDown(200);
	}, function() {
		$(this).removeClass('hover');
		$(this).children('ul').stop(true, true).hide();
	});

	// Search Bar
	$('.site-search-icon').click(function(e) {
		var $this      = $(this)
			$searchBar = $('nav .site-search');

		if( $searchBar.is(':hidden') ) {
			$this.addClass('active');
			$searchBar.fadeIn(100);
		} else {
			$this.removeClass('active');
			$searchBar.hide();
		}

		e.preventDefault();

	});

});

/* end Main Navigation */

/* ---------------------------------------------------------------------- */
/*	Input Placeholders
/* ---------------------------------------------------------------------- */

$(function(){

	$('input[placeholder], textarea[placeholder]').placeholder();

});

/* end Input Placeholders */

/* ---------------------------------------------------------------------- */
/*	Fancybox
/* ---------------------------------------------------------------------- */

$(function(){

	// Images
	$('.single-image, .image-gallery').fancybox({
		'transitionIn'  : 'fade',
		'transitionOut' : 'fade',
		'titlePosition' : 'over'
	}).each(function() {
		$(this).append('<span class="zoom">&nbsp;</span>');
	});

	// Iframe
	$('.iframe').fancybox({
		'autoScale'     : false,
		'transitionIn'  : 'fade',
		'transitionOut' : 'fade',
		'type'          : 'iframe',
		'titleShow'     : false
	}).each(function() {
		$(this).append('<span class="zoom">&nbsp;</span>');
	});

});

/* end Fancybox */

/* ---------------------------------------------------------------------- */
/*	Homepage Slider
/* ---------------------------------------------------------------------- */

$(function(){

	var $slider = $('#slider');

	if( $slider.length ) {

		$slider.asyncSlider({
			easing      : 'easeInOutBack',
			minTime     : 500,
			maxTime     : 1350,
			autoswitch  : 3000,
			random      : true,
			prevNextNav : $slider,
			slidesNav   : $slider
		});

		// Center navs
		var $arrowNav      = $('#asyncslider_next_prev_nav'),
			$dotsNav       = $('#asyncslider_slides_nav'),
			arrowNavHeight = $arrowNav.height(),
			dotsNavWidth   = $dotsNav.width();
		
		$arrowNav.css({
			'height'     : arrowNavHeight,
			'margin-top' : -arrowNavHeight/2
		});

		$dotsNav.css({
			'margin-left' : -dotsNavWidth/2,
			'width'       : dotsNavWidth
		});

		// Pause on hover
		$slider.hover(function() {
			$slider.asyncSlider('set', 'autoswitch', 'pause');
		}, function() {
			$slider.asyncSlider('set', 'autoswitch', 'play');
		});
	}

});

/* end Homepage Slider */

/* ---------------------------------------------------------------------- */
/*	Gallery Filter
/* ---------------------------------------------------------------------- */

$(function(){

	var $originalGalleryFilter = $('.gallery-filter');

	if( $originalGalleryFilter.length ) {

		var selectedOption = $originalGalleryFilter.find('option:selected'),
			$container     = $('#gallery-items'),
			$newGalleryFilter;

		// Hide it!
		$originalGalleryFilter.hide();

		// Create new dropdown
		$originalGalleryFilter.after('<dl class="gallery-filter-dropdown">');
		$newGalleryFilter = $('.gallery-filter-dropdown');

		// Copy options to new dropdown
		$newGalleryFilter.append('<dt><a href="#" data-value="' + selectedOption.val() + '">' + selectedOption.text() + '</a></dt><dd><ul>');
		$('option', $originalGalleryFilter).each(function(i){
			var $this = $(this);
			if( $this.val() ) $('dd ul', $newGalleryFilter).append('<li><a href="#" data-value="' + $this.val() + '">' + $this.text() + '</a></li>');
		});

		// Open dropdown
		$('dt a', $newGalleryFilter).click(function(e) {
			$newGalleryFilter.addClass('active');
			$('dd ul', $newGalleryFilter).slideDown(200);

			e.preventDefault();
		});

		// Off click (close)
		$(document).bind('click', function(e) {
			var clicked = $(e.target);

			if ( !clicked.parents().hasClass('gallery-filter-dropdown') ) {
				$newGalleryFilter.removeClass('active');
				$('dd ul', $newGalleryFilter).slideUp(100);
			}
		});

		// Select option
		$('dd a', $newGalleryFilter).click(function(e) {
			var $this       = $(this),
				optionLabel = $this.html(),
				optionValue = $this.attr('data-value');

			$('dt a', $newGalleryFilter).attr('data-value', optionValue).html(optionLabel);
			$originalGalleryFilter.val(optionValue);

			$newGalleryFilter.removeClass('active');
			$('dd ul', $newGalleryFilter).slideUp(100);

			// Trigger
			$originalGalleryFilter.trigger('change');

			e.preventDefault();
		});

		// Copy categories to item classes
		$('article', $container).each(function(i) {
			var $this = $(this);
			$this.addClass(  $this.attr('data-categories') );
		});

		// Run Isotope when all images are fully loaded
		$(window).load(function() {

			$container.isotope({
				itemSelector : 'article',
				layoutMode   : 'fitRows'
			});

		});
		
		// Select option
		$('.gallery-filter').change(function(e) {
			var currentOption = $(this).val();

			if(currentOption) {
				if( currentOption !== '*' ) currentOption = currentOption.replace(currentOption, '.' + currentOption)
				$container.isotope({ filter : currentOption });
			}

			e.preventDefault();
		});

	}

});

/* end Gallery Filter */

/* ---------------------------------------------------------------------- */
/*	Single Gallery Slider
/* ---------------------------------------------------------------------- */

$(function(){

	var $slider = $('.single-gallery-slider ul');

	if( $slider.length ) {

		// Run slider when all images are fully loaded
		$(window).load(function() {

			$slider
				.after('<div class="single-gallery-slider-nav">')
				.each(function(){
					var p = this.parentNode;
					$(this).cycle({
						easing            : 'easeInOutExpo',
						fx                : 'scrollHorz',
						pager             :  $('.single-gallery-slider-nav', p),
						pause             : true,
						pauseOnPagerHover : true,
						speed             : 600,
						timeout           : 3000
					});
				});
			
		});

	}

});

/* end Single Gallery Slider */

/* ---------------------------------------------------------------------- */
/*	Google Maps
/* ---------------------------------------------------------------------- */

$(function(){

	$('#map').gMap({
		address: 'Level 13, 2 Elizabeth St, Melbourne Victoria 3000 Australia',
		zoom: 16,
		markers: [
			{ 'address' : 'Level 13, 2 Elizabeth St, Melbourne Victoria 3000 Australia' }
		]
	});

});

/* end Google Maps */

/* ---------------------------------------------------------------------- */
/*	Sidebar Tabs
/* ---------------------------------------------------------------------- */

$(function(){

	$('.tabs-content').hide();

	$('.tabs-nav li:first').addClass('active');
	$('.tabs-content:first').show();

	$('.tabs-nav a').click(function(e) {
		var $this   = $(this),
		    activeTab = $this.attr('href');

		$('.tabs-nav .active').removeClass('active');
		$('.tabs-content').stop(true,true).hide();

		$this.parent().addClass('active');
		$(activeTab).stop(true,true).fadeIn(300);

		e.preventDefault();
	});

});

/* end Sidebar Tabs */

/* ---------------------------------------------------------------------- */
/*	PHP Widgets
/* ---------------------------------------------------------------------- */

$(function(){

	function fetchFeed( url, element ) {

		element.html('<img src="img/loader.gif" height="11" width="16" alt="Loading..." />');

		$.ajax({
			url: url,
			dataType: 'html',
			timeout: 5000,
			type: 'GET',
			error:
				function(xhr, status, error) {
					element.html('An error occured: ' + error);
				},
			success:
				function(data, status, xhr) {
					element.html(data);
				}
		});
		
	}

	// Latest Tweets
	fetchFeed( 'php/tweets.php', $('.tweets') )

	// Latest Flickr Images
	fetchFeed( 'php/flickr.php', $('.flickr-images') )

});
	
/* end PHP Widgets */

/* ---------------------------------------------------------------------- */
/*	Contact Form
/* ---------------------------------------------------------------------- */

$(function(){

	var submitButton = $('.contact-form input[type=submit]'),
	    loader       = '<img src="img/loader.gif" height="11" width="16" alt="Loading..." />';

	submitButton.next('.clear').after('<div class="hidden" id="response">');
	
	// Do what we need to when form is submitted.
	submitButton.click(function(e){
	
		// Setup any needed variables.
		var inputName    = $('#contact-name').val(),
			inputEmail   = $('#contact-email').val(),
			inputWebsite = $('#contact-url').val(),
			inputSubject = $('#contact-subject').val(),
			inputMessage = $('#contact-message').val(),
			response     = $('#response');

		// Hide any previous response text 
		response.hide();
		
		// Change response text to 'loading...'
		response.html(loader).show();
		
		// Make AJAX request 
		$.post('php/contact-send.php', {name: inputName, email: inputEmail, url: inputWebsite, subject: inputSubject, message: inputMessage}, function(data){
			response.html(data);
		});
		
		// Cancel default action
		e.preventDefault();
	});

});

/* end Contact Form */

/* ---------------------------------------------------------------------- */
/*	Custom Functions
/* ---------------------------------------------------------------------- */

/* end Custom Functions */
