/* Activate Cufon font replacement */
Cufon.replace('.bucket h2');
Cufon.replace('#container #main .entry-content.standard p');
Cufon.replace('h3.widget-title');

$.fn.cycle.defaults = { 
    timeout:       4000,  // milliseconds between slide transitions (0 to disable auto advance) 
    speed:         1000,  // speed of the transition (any valid fx speed value) 
    next:          null,  // id of element to use as click trigger for next slide 
    prev:          null,  // id of element to use as click trigger for previous slide 
    before:        null,  // transition callback (scope set to element to be shown) 
    after:         null,  // transition callback (scope set to element that was shown) 
    height:       'auto', // container height 
    sync:          0,     // true if in/out transitions should occur simultaneously 
    fit:           0,     // force slides to fit container 
    pause:         0,     // true to enable "pause on hover" 
    delay:         0,     // additional delay (in ms) for first transition (hint: can be negative) 
    slideExpr:     null   // expression for selecting slides (if something other than all children is required) 
}; 

$(document).ready(function() {
	/* Include custom each delay script for gallery fade ins */
	jQuery.fn.eachDelay = function(callback, speed){
		return jQuery.eachDelay( this, callback, speed)
	}
	jQuery.extend({
		eachDelay: function(object,callback, speed){ 
			var name, i = -1, length = object.length, $div = $('<div>'), id;
			if (length === undefined) { //not an array process as object
				var arr = [], x = -1;
				for (name in object) arr[++x] = name; 
				id = window.setInterval(function(){
				 if( ++i === arr.length || callback.call(object[ arr[i] ], arr[i], object[ arr[i] ]) === false) 
				 	 clearInterval(id);
				}, speed);	
			}
			else { //array-compatible element ie. [], jQuery Object
				id = window.setInterval(function(){ 
					if (++i === object.length || callback.call(object[i], i, object[i]) === false) 
						clearInterval(id);
				}, speed);
			}
			return object;
		}
	});
	/* End each delay script */
	$('header nav ul li').after('<li>|</li>');
	$('header nav ul li:last').remove();
	/* Adding custom span to WordPress navigation */
	$('header nav ul li').append('<span></span>');
	$('header nav ul li.menu-item-23 a').attr('target', '_blank');
	$("#container.home #main .page .entry-content .ngg-slideshow").wrap("<a href="+"/?page_id=7"+"></a>");
	$('#container.home #main .page .entry-content p a').attr('href', '?cat=3');
	$('#container.home #main .page .entry-content p a').cycle()
	$('.contact-form form p br').remove();
	/* Look for the gallery pages and add the gallery class to the content well */
	var galleryPage = $('.ngg-galleryoverview').attr('class');
	if (galleryPage == 'ngg-galleryoverview') {
		$('.entry-content').addClass('gallery');
	} else if (galleryPage == 'ngg-galleryoverview carousel') {
		$('.entry-content').addClass('carousel');
	}
	/* Grab child page ID to set up URL rewrite to direct gallery images to carousel gallery page */
	/* child-page-var div is set up on page.php line 52 */
	var childPageID = $('#child-page-var').attr("title");
	$('.ngg-gallery-thumbnail a').each ( function () {
		var imageID = $(this).attr('href');
		var galleryThumbURL = '?page_id='+childPageID+'&pid='+imageID;
		$(this).attr('href', galleryThumbURL);
	});

	/* Remove right margin from last images in gallery rows */
	$('.ngg-galleryoverview .ngg-gallery-thumbnail-box:nth-child(5n) div').addClass('last');
	/* Empty text from gallery navigation links */
	$('.ngg-navigation a').empty();
	$('a.yapb-image-link').append('<span></span>');
	$('a.yapb-image-link').hover(
		function () {
			$('span', this).fadeIn(300);
		},
		function () {
			$('span', this).fadeOut(300);
		}
	);
	$('#content #filmstrip ul li a').hover(
		function () {
			$('div', this).fadeIn(300);
		},
		function () {
			$('div', this).fadeOut(300);
		}
	);
	$('.ngg-gallery-list li a').hover(
		function () {
			$('.hover', this).fadeIn(300);
		},
		function () {
			$('.hover', this).fadeOut(300);
		}
	);
	// Old carousel page image fade in
	// Carousel page main image fade in
	$('.pic').fadeIn(1000);
	$('.yapb-image').fadeIn(1000);
	// Gallery page image fade in
	$('.ngg-gallery-thumbnail-box').eachDelay(function(index) {
	    $(this).fadeIn('slow');
	  }, 125);
	$('.ngg-gallery-thumbnail').hover(
		function() {
			$('.hover', this).fadeIn(300);
		},
		function() {
			$('.hover', this).fadeOut(300);
		}
	);
	$('#photo-nav').hover(
		function () {
			$('#photo-nav a .hover').fadeIn(800);
		},
		function () {
			$('#photo-nav a .hover').fadeOut('slow');
		}
	);
	$('.contact-form form .wpcf7-submit').replaceWith('<input type="image" src="http://www.auberginephotography.com/wp-content/themes/aubergine/images/contact/contact-form-submit.gif" value="submit" class="submit" />');
		$.extend({
		getUrlVars: function(){
			var vars = [], hash;
			var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
			for(var i = 0; i < hashes.length; i++)
		{
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	},
	getUrlVar: function(name){
	return $.getUrlVars()[name];
	}
	});
	var currentImage = $.getUrlVar('pid');
	var	imageFinder = '#ngg-image-'+currentImage;
	var listItem = $('#ngg-image-9');
	var imageIndex = $('li.ngg-thumbnail-list').index(listItem);
	var currentPage = $.getUrlVar('p');
	var currentPageClass = '.' + $.getUrlVar('p');
	$(currentPageClass).addClass('selected');
		
	$(function() {
		// Look for next and previous images to set up image navigation
		var currentNavImage = $.getUrlVar('pid');
		currentNavImage = 'li#ngg-image-'+currentNavImage;
		var nextNavImage = $(currentNavImage).next().children('a').attr('href');
		var prevNavImage = $(currentNavImage).prev().children('a').attr('href');
		if(nextNavImage) {
			$('#photo-nav #next a').empty().append('<div class="hover"></span>').attr('href', nextNavImage);
		} else {
			$('#photo-nav #next a').hide();
		}
		if(prevNavImage) {
			$('#photo-nav #prev a').empty().append('<div class="hover"></span>').attr('href', prevNavImage);
		} else {
			$('#photo-nav #prev a').hide();
		}
		// Find current image and round down to nearest 10 to set the startVar for the carousel.
		var currentImage = $.getUrlVar('pid');
		var currentImage = '#ngg-image-'+currentImage;
		var currentImage = $(currentImage);
		var currentImage = $('li.ngg-thumbnail-list').index(currentImage);
		startVar = Math.floor((currentImage/10))*10;
		// Initialize the carousel
		$("#gallery-navigation").jCarouselLite({
			btnNext: "#gallery-navigation-controls .arrow-right",
			btnPrev: "#gallery-navigation-controls .arrow-left",
			visible: 10,
			circular: false,
			scroll: 10,
			speed: 800,
			easing: "easeinout",
			start: startVar
		});
		// Set up arrow hiding for gallery navigation on first and last pages
		// Find the count of all of the images and round up to nearest 10
		var imageCount = $('li.ngg-thumbnail-list').size();
		setCount = Math.ceil((imageCount/10))*10;
		setCount = setCount-10;
		// Find the current image and round up to nearest 10
		imageSet = Math.floor((currentImage/10))*10;
		// Use the comparison between imageCount and imageSet to determine what controls should be shown on page load.
		// Hide the left arrow on the first set
		if(imageSet == 0) {
			$('#gallery-navigation-controls .arrow-left').hide();
		}
		// Hide the right arrow on the last set
		if(setCount == imageSet) {
			$('#gallery-navigation-controls .arrow-right').hide();
		}
		// If you click the left arrow, increase the counter appropriately and check the displayed arrows.
		$('#gallery-navigation-controls .arrow-right').click(function () {
			$('#gallery-navigation-controls .arrow-left').show();
			$('#gallery-navigation-controls .arrow-right').show();
			imageSet = imageSet+10;
			if(imageSet == setCount) {
				$('#gallery-navigation-controls .arrow-right').hide();
			}
			return true;
		});
		$('#gallery-navigation-controls .arrow-left').click(function() {
			$('#gallery-navigation-controls .arrow-left').show();
			$('#gallery-navigation-controls .arrow-right').show();
			imageSet = imageSet-10;
			if(imageSet == 0) {
				$('#gallery-navigation-controls .arrow-left').hide();
			}
			return true;
		});
	});
	var currentPage = $.getUrlVar('p');
	var currentPageClass = '.' + $.getUrlVar('p');
	$(currentPageClass).addClass('selected');
	$(function() {
		if(currentPage == '60' || currentPage == '58' || currentPage == '56' || currentPage == '54' || currentPage == '52' || currentPage == '50' || currentPage == '48' || currentPage == '46' || currentPage == '44' || currentPage == '38') {
			startVar = 10;
			$('#filmstrip-controls .arrow-right').hide();
		} else {
			startVar = 0;
			$('#filmstrip-controls .arrow-left').hide();
		}
		$("#filmstrip").jCarouselLite({
			btnNext: "#filmstrip-controls .arrow-right",
			btnPrev: "#filmstrip-controls .arrow-left",
			visible: 10,
			circular: false,
			scroll: 10,
			speed: 800,
			easing: "easeinout",
			start: startVar
		});
		$('#filmstrip-controls .arrow-right').click(function () {
			$(this).hide();
			$('#filmstrip-controls .arrow-left').show();
			return true;
		});
		$('#filmstrip-controls .arrow-left').click(function () {
			$(this).hide();
			$('#filmstrip-controls .arrow-right').show();
			return true;
		});
	});
});
