$(document).ready(function() {
/*
	Simple image gallery. Use default settings
*/

$('.fancybox').fancybox();

/*
	Different effects
*/

// Change title type, overlay opening speed and opacity
$(".fancybox-effects-a").fancybox({
	helpers: {
		title : {
			type : 'outside'
		},
		overlay : {
			speedIn : 500,
			opacity : 0.95
		}
	}
});

// Disable opening and closing animations, change title type
$(".fancybox-effects-b").fancybox({
	openEffect  : 'none',
	closeEffect	: 'none',

	helpers : {
		title : {
			type : 'over'
		}
	}
});

// Set custom style, close if clicked, change title type and overlay color
$(".fancybox-effects-c").fancybox({
	wrapCSS    : 'fancybox-custom',
	closeClick : true,

	helpers : {
		title : {
			type : 'inside'
		},
		overlay : {
			css : {
				'background-color' : '#eee'	
			}
		}
	}
});

// Remove padding, set opening and closing animations, close if clicked and disable overlay
$(".fancybox-effects-d").fancybox({
	padding: 0,

	openEffect : 'elastic',
	openSpeed  : 150,

	closeEffect : 'elastic',
	closeSpeed  : 150,

	closeClick : true,

	helpers : {
		overlay : null
	}
});

/*
	Button helper. Disable animations, hide close button, change title type and content
*/

$('.fancybox-buttons').fancybox({
	openEffect  : 'none',
	closeEffect : 'none',

	prevEffect : 'none',
	nextEffect : 'none',

	closeBtn  : false,

	helpers : {
		title : {
			type : 'inside'
		},
		buttons	: {}
	},

	afterLoad : function() {
		this.title = 'Image ' + (this.index + 1) + ' of ' + this.group.length + (this.title ? ' - ' + this.title : '');
	}
});


/*
	Thumbnail helper. Disable animations, hide close button, arrows and slide to next gallery item if clicked
*/

$('.img_link').fancybox({ 
	wrapCSS    : 'fancybox-custom',
	prevEffect : 'fade',
	nextEffect : 'fade',
	prevSpeed : 500,
	nextSpeed : 500,

	helpers : { 
		thumbs : {
			position : 'top',
			width  : 50,
			height : 50
		},
		title : {
			type : 'inside'
		},
		buttons	: {
			tpl : '<div id="fancybox-buttons"><ul><li><a class="btnToggle" title="Toggle size" href="javascript:;"></a></li></ul></div>',
			position : 'bottom'
		},
		overlay : {
			css : {
				'background-color' : 'none'
			}
		}
	},
	afterLoad : function() {
		
		this.title = 'Image ' + (this.index + 1) + ' of ' + this.group.length + (this.title ? ' : <strong>' + this.title + '</strong>' : '') + ' : By Stephen Zerne';
	}
});


// This is for smooth scrolling
function filterPath(string) {
	
	return string
	.replace(/^\//,'')
	.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
	.replace(/\/$/,'');
}

var locationPath = filterPath(location.pathname);
var scrollElem = scrollableElement('html', 'body');

$('a[href*=#]').each(function() {
	
	var thisPath = filterPath(this.pathname) || locationPath;
	
	if (  locationPath == thisPath
		&& (location.hostname == this.hostname || !this.hostname)
		&& this.hash.replace(/#/,'')
//		&& ["fancybox"] in oc(this.className.split(/\s+/))
	) {
		var $target = $(this.hash), target = this.hash;
		if (target) {
			var targetOffset = $target.offset().top;
			$(this).click(function(event) {
				event.preventDefault();
				if(['fancybox'] in oc(this.className.split(/\s+/))){
//					alert("wtf!");
				}else{
					$(scrollElem).animate({scrollTop: targetOffset}, 550, function() {
						location.hash = target;
					});
					
					/* THE FOLLOWING LINE IS FOR FORCING GOOGLE ANALYTICS TO CATCH EACH CLICK  */
					_gaq.push(['_trackPageview', location.pathname + location.search + $(this).attr('href')]);
					
				}
				
			});
		}
	}
}); 

// use the first element that is "scrollable"
function scrollableElement(els) {
	for (var i = 0, argLength = arguments.length; i <argLength; i++) {
		var el = arguments[i],
		$scrollElement = $(el);
		if ($scrollElement.scrollTop()> 0) {
			return el;
		} else {
			$scrollElement.scrollTop(1);
			var isScrollable = $scrollElement.scrollTop()> 0;
			$scrollElement.scrollTop(0);
			if (isScrollable) {
				return el;
			}
		}
	}
	return [];
}

//check array to see if value exists
function oc(a)
{
  var o = {};
  for(var i=0;i<a.length;i++)
  {
    o[a[i]]='';
  }
  return o;
}
});
