/*
 * jQuery Nivo Slider v1.9
 * http://nivo.dev7studios.com
 *
 * Copyright 2010, Gilbert Pellegrom
 * Free to use and abuse under the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * April 2010 - controlNavThumbs option added by Jamie Thompson (http://jamiethompson.co.uk)
 * March 2010 - manualAdvance option added by HelloPablo (http://hellopablo.co.uk)
 */

(function($) {

	$.fn.nivoSlider = function(options) {

		//Defaults are below
		var settings = $.extend({}, $.fn.nivoSlider.defaults, options);

		return this.each(function() {
			//Useful variables. Play carefully.
			var vars = {
				currentSlide: 0,
				currentImage: '',
				totalSlides: 0,
				randAnim: '',
				running: false,
				paused: false,
				stop:false
			};
		
			//Get this slider
			var slider = $(this);
			slider.data('nivo:vars', vars);
			slider.css('position','relative');
			slider.width('1px');
			slider.height('1px');
			slider.addClass('nivoSlider');
			
			//Find our slider children
			var kids = slider.children();
			kids.each(function() {
				var child = $(this);
				if(!child.is('img')){
					if(child.is('a')){
						child.addClass('nivo-imageLink');
					}
					child = child.find('img:first');
				}
				//Don't ask
				var childWidth = child.width();
				if(childWidth == 0) childWidth = child.attr('width');
				var childHeight = child.height();
				if(childHeight == 0) childHeight = child.attr('height');
				//Resize the slider
				if(childWidth > slider.width()){
					slider.width(childWidth);
				}
				if(childHeight > slider.height()){
					slider.height(childHeight);
				}
				child.css('display','none');
				vars.totalSlides++;
			});
			
			//Set startSlide
			if(settings.startSlide > 0){
				if(settings.startSlide >= vars.totalSlides) settings.startSlide = vars.totalSlides - 1;
				vars.currentSlide = settings.startSlide;
			}
			
			//Get initial image
			if($(kids[vars.currentSlide]).is('img')){
				vars.currentImage = $(kids[vars.currentSlide]);
			} else {
				vars.currentImage = $(kids[vars.currentSlide]).find('img:first');
			}
			
			//Show initial link
			if($(kids[vars.currentSlide]).is('a')){
				$(kids[vars.currentSlide]).css('display','block');
			}
			
			//Set first background
			slider.css('background','url('+ vars.currentImage.attr('src') +') no-repeat');
			
			//Add initial slices
			for(var i = 0; i < settings.slices; i++){
				var sliceWidth = Math.round(slider.width()/settings.slices);
				if(i == settings.slices-1){
					slider.append(
						$('<div class="nivo-slice"></div>').css({ left:(sliceWidth*i)+'px', width:(slider.width()-(sliceWidth*i))+'px' })
					);
				} else {
					slider.append(
						$('<div class="nivo-slice"></div>').css({ left:(sliceWidth*i)+'px', width:sliceWidth+'px' })
					);
				}
			}
			
			//Create caption
			slider.append(
				$('<div class="nivo-caption"><p></p></div>').css({ display:'none', opacity:settings.captionOpacity })
			);			
			//Process initial  caption
			if(vars.currentImage.attr('title') != ''){
				$('.nivo-caption p', slider).html(vars.currentImage.attr('title'));					
				$('.nivo-caption', slider).fadeIn(settings.animSpeed);
			}
			
			//In the words of Super Mario "let's a go!"
			var timer = 0;
			if(!settings.manualAdvance){
				timer = setInterval(function(){ nivoRun(slider, kids, settings, false); }, settings.pauseTime);
			}

			//Add Direction nav
			if(settings.directionNav){
				slider.append('<div class="nivo-directionNav"><a class="nivo-prevNav">Prev</a><a class="nivo-nextNav">Next</a></div>');
				
				//Hide Direction nav
				if(settings.directionNavHide){
					$('.nivo-directionNav', slider).hide();
					slider.hover(function(){
						$('.nivo-directionNav', slider).show();
					}, function(){
						$('.nivo-directionNav', slider).hide();
					});
				}
				
				$('a.nivo-prevNav', slider).live('click', function(){
					if(vars.running) return false;
					clearInterval(timer);
					timer = '';
					vars.currentSlide-=2;
					nivoRun(slider, kids, settings, 'prev');
				});
				
				$('a.nivo-nextNav', slider).live('click', function(){
					if(vars.running) return false;
					clearInterval(timer);
					timer = '';
					nivoRun(slider, kids, settings, 'next');
				});
			}
			
			//Add Control nav
			if(settings.controlNav){
				var nivoControl = $('<div class="nivo-controlNav"></div>');
				slider.append(nivoControl);
				for(var i = 0; i < kids.length; i++){
					if(settings.controlNavThumbs){
						var child = kids.eq(i);
						if(!child.is('img')){
							child = child.find('img:first');
						}
						nivoControl.append('<a class="nivo-control" rel="'+ i +'"><img src="'+ child.attr('src').replace(settings.controlNavThumbsSearch, settings.controlNavThumbsReplace) +'"></a>');
					} else {
						nivoControl.append('<a class="nivo-control" rel="'+ i +'">'+ (i + 1) +'</a>');
					}
					
				}
				//Set initial active link
				$('.nivo-controlNav a:eq('+ vars.currentSlide +')', slider).addClass('active');
				
				$('.nivo-controlNav a', slider).live('click', function(){
					if(vars.running) return false;
					if($(this).hasClass('active')) return false;
					clearInterval(timer);
					timer = '';
					slider.css('background','url('+ vars.currentImage.attr('src') +') no-repeat');
					vars.currentSlide = $(this).attr('rel') - 1;
					nivoRun(slider, kids, settings, 'control');
				});
			}
			
			//Keyboard Navigation
			if(settings.keyboardNav){
				$(window).keypress(function(event){
					//Left
					if(event.keyCode == '37'){
						if(vars.running) return false;
						clearInterval(timer);
						timer = '';
						vars.currentSlide-=2;
						nivoRun(slider, kids, settings, 'prev');
					}
					//Right
					if(event.keyCode == '39'){
						if(vars.running) return false;
						clearInterval(timer);
						timer = '';
						nivoRun(slider, kids, settings, 'next');
					}
				});
			}
			
			//For pauseOnHover setting
			if(settings.pauseOnHover){
				slider.hover(function(){
					vars.paused = true;
					clearInterval(timer);
					timer = '';
				}, function(){
					vars.paused = false;
					//Restart the timer
					if(timer == '' && !settings.manualAdvance){
						timer = setInterval(function(){ nivoRun(slider, kids, settings, false); }, settings.pauseTime);
					}
				});
			}
			
			//Event when Animation finishes
			slider.bind('nivo:animFinished', function(){ 
				vars.running = false; 
				//Hide child links
				$(kids).each(function(){
					if($(this).is('a')){
						$(this).css('display','none');
					}
				});
				//Show current link
				if($(kids[vars.currentSlide]).is('a')){
					$(kids[vars.currentSlide]).css('display','block');
				}
				//Restart the timer
				if(timer == '' && !vars.paused && !settings.manualAdvance){
					timer = setInterval(function(){ nivoRun(slider, kids, settings, false); }, settings.pauseTime);
				}
				//Trigger the afterChange callback
				settings.afterChange.call(this);
			});
		});
		
		function nivoRun(slider, kids, settings, nudge){
			//Get our vars
			var vars = slider.data('nivo:vars');
			if((!vars || vars.stop) && !nudge) return false;
			
			//Trigger the beforeChange callback
			settings.beforeChange.call(this);
					
			//Set current background before change
			if(!nudge){
				slider.css('background','url('+ vars.currentImage.attr('src') +') no-repeat');
			} else {
				if(nudge == 'prev'){
					slider.css('background','url('+ vars.currentImage.attr('src') +') no-repeat');
				}
				if(nudge == 'next'){
					slider.css('background','url('+ vars.currentImage.attr('src') +') no-repeat');
				}
			}
			vars.currentSlide++;
			if(vars.currentSlide == vars.totalSlides){ 
				vars.currentSlide = 0;
				//Trigger the slideshowEnd callback
				settings.slideshowEnd.call(this);
			}
			if(vars.currentSlide < 0) vars.currentSlide = (vars.totalSlides - 1);
			//Set vars.currentImage
			if($(kids[vars.currentSlide]).is('img')){
				vars.currentImage = $(kids[vars.currentSlide]);
			} else {
				vars.currentImage = $(kids[vars.currentSlide]).find('img:first');
			}
			
			//Set acitve links
			if(settings.controlNav){
				$('.nivo-controlNav a', slider).removeClass('active');
				$('.nivo-controlNav a:eq('+ vars.currentSlide +')', slider).addClass('active');
			}
			
			//Process caption
			if(vars.currentImage.attr('title') != ''){
				if($('.nivo-caption', slider).css('display') == 'block'){
					$('.nivo-caption p', slider).fadeOut(settings.animSpeed, function(){
						$(this).html(vars.currentImage.attr('title'));
						$(this).fadeIn(settings.animSpeed);
					});
				} else {
					$('.nivo-caption p', slider).html(vars.currentImage.attr('title'));
				}					
				$('.nivo-caption', slider).fadeIn(settings.animSpeed);
			} else {
				$('.nivo-caption', slider).fadeOut(settings.animSpeed);
			}
			
			//Set new slice backgrounds
			var  i = 0;
			$('.nivo-slice', slider).each(function(){
				var sliceWidth = Math.round(slider.width()/settings.slices);
				$(this).css({ height:'0px', opacity:'0', 
					background: 'url('+ vars.currentImage.attr('src') +') no-repeat -'+ ((sliceWidth + (i * sliceWidth)) - sliceWidth) +'px 0%' });
				i++;
			});
			
			if(settings.effect == 'random'){
				var anims = new Array("sliceDownRight","sliceDownLeft","sliceUpRight","sliceUpLeft","sliceUpDown","sliceUpDownLeft","fold","fade");
				vars.randAnim = anims[Math.floor(Math.random()*(anims.length + 1))];
				if(vars.randAnim == undefined) vars.randAnim = 'fade';
			}
		
			//Run effects
			vars.running = true;
			if(settings.effect == 'sliceDown' || settings.effect == 'sliceDownRight' || vars.randAnim == 'sliceDownRight' ||
				settings.effect == 'sliceDownLeft' || vars.randAnim == 'sliceDownLeft'){
				var timeBuff = 0;
				var i = 0;
				var slices = $('.nivo-slice', slider);
				if(settings.effect == 'sliceDownLeft' || vars.randAnim == 'sliceDownLeft') slices = $('.nivo-slice', slider).reverse();
				slices.each(function(){
					var slice = $(this);
					slice.css('top','0px');
					if(i == settings.slices-1){
						setTimeout(function(){
							slice.animate({ height:'100%', opacity:'1.0' }, settings.animSpeed, '', function(){ slider.trigger('nivo:animFinished'); });
						}, (100 + timeBuff));
					} else {
						setTimeout(function(){
							slice.animate({ height:'100%', opacity:'1.0' }, settings.animSpeed);
						}, (100 + timeBuff));
					}
					timeBuff += 50;
					i++;
				});
			} 
			else if(settings.effect == 'sliceUp' || settings.effect == 'sliceUpRight' || vars.randAnim == 'sliceUpRight' ||
					settings.effect == 'sliceUpLeft' || vars.randAnim == 'sliceUpLeft'){
				var timeBuff = 0;
				var i = 0;
				var slices = $('.nivo-slice', slider);
				if(settings.effect == 'sliceUpLeft' || vars.randAnim == 'sliceUpLeft') slices = $('.nivo-slice', slider).reverse();
				slices.each(function(){
					var slice = $(this);
					slice.css('bottom','0px');
					if(i == settings.slices-1){
						setTimeout(function(){
							slice.animate({ height:'100%', opacity:'1.0' }, settings.animSpeed, '', function(){ slider.trigger('nivo:animFinished'); });
						}, (100 + timeBuff));
					} else {
						setTimeout(function(){
							slice.animate({ height:'100%', opacity:'1.0' }, settings.animSpeed);
						}, (100 + timeBuff));
					}
					timeBuff += 50;
					i++;
				});
			} 
			else if(settings.effect == 'sliceUpDown' || settings.effect == 'sliceUpDownRight' || vars.randAnim == 'sliceUpDown' || 
					settings.effect == 'sliceUpDownLeft' || vars.randAnim == 'sliceUpDownLeft'){
				var timeBuff = 0;
				var i = 0;
				var v = 0;
				var slices = $('.nivo-slice', slider);
				if(settings.effect == 'sliceUpDownLeft' || vars.randAnim == 'sliceUpDownLeft') slices = $('.nivo-slice', slider).reverse();
				slices.each(function(){
					var slice = $(this);
					if(i == 0){
						slice.css('top','0px');
						i++;
					} else {
						slice.css('bottom','0px');
						i = 0;
					}
					
					if(v == settings.slices-1){
						setTimeout(function(){
							slice.animate({ height:'100%', opacity:'1.0' }, settings.animSpeed, '', function(){ slider.trigger('nivo:animFinished'); });
						}, (100 + timeBuff));
					} else {
						setTimeout(function(){
							slice.animate({ height:'100%', opacity:'1.0' }, settings.animSpeed);
						}, (100 + timeBuff));
					}
					timeBuff += 50;
					v++;
				});
			} 
			else if(settings.effect == 'fold' || vars.randAnim == 'fold'){
				var timeBuff = 0;
				var i = 0;
				$('.nivo-slice', slider).each(function(){
					var slice = $(this);
					var origWidth = slice.width();
					slice.css({ top:'0px', height:'100%', width:'0px' });
					if(i == settings.slices-1){
						setTimeout(function(){
							slice.animate({ width:origWidth, opacity:'1.0' }, settings.animSpeed, '', function(){ slider.trigger('nivo:animFinished'); });
						}, (100 + timeBuff));
					} else {
						setTimeout(function(){
							slice.animate({ width:origWidth, opacity:'1.0' }, settings.animSpeed);
						}, (100 + timeBuff));
					}
					timeBuff += 50;
					i++;
				});
			}  
			else if(settings.effect == 'fade' || vars.randAnim == 'fade'){
				var i = 0;
				$('.nivo-slice', slider).each(function(){
					$(this).css('height','100%');
					if(i == settings.slices-1){
						$(this).animate({ opacity:'1.0' }, (settings.animSpeed*2), '', function(){ slider.trigger('nivo:animFinished'); });
					} else {
						$(this).animate({ opacity:'1.0' }, (settings.animSpeed*2));
					}
					i++;
				});
			}
		}
	};
	
	//Default settings
	$.fn.nivoSlider.defaults = {
		effect:'random',
		slices:15,
		animSpeed:500,
		pauseTime:3000,
		startSlide:0,
		directionNav:true,
		directionNavHide:true,
		controlNav:true,
		controlNavThumbs:false,
		controlNavThumbsSearch: '.jpg',
		controlNavThumbsReplace: '_thumb.jpg',
		keyboardNav:true,
		pauseOnHover:true,
		manualAdvance:false,
		captionOpacity:0.8,
		beforeChange: function(){},
		afterChange: function(){},
		slideshowEnd: function(){}
	};
	
	$.fn.reverse = [].reverse;
	
})(jQuery);

/**
 * sfPhotoGallery
 *
 * @version: 1.0
 * @author SimpleFlame http://www.simpleflame.com/
 *
 * Available options:
 *  transition - by default "fade", can also use FALSE to disable transition or any of the jQuery UI effects if UI is included. !NOTICE - will throw an exception if you try to use an effect without including UI
 *  transitionSpeed - photo transition speed, by default 300 ms
 *  transitionInOptions - options used on the "in" transition if one of the UI effects is used 
 *  transitionOutOptions - options used on the "out" transition if one of the UI effects is used 
 *  goodbarry - boolean flag, by default "true". If turned on and no ".thumb" element is present in the data list, then GB image resize utility will be used
 *  thumbsLocation - top|right|bottom|left - location of thumbs slider in reference to the main image area, by default "left"
 *  descriptionLocation - top|right|bottom|left - location of the description area in reference to the main image area, by default bottom
 *  autorotate - boolean, if true will add an option to start/top autorotation, by default false
 *  autorotateDuration - if "autorotate" option is set to true, it determines how long image is visilbe, by default 3000 ms
 *  thumbsPerPage - number of thumbnails visible at once, by default 8
 *  numbersPerPaginationPage - number of items visible on one page in the pagination list. This option has to be set depending on the layout, as it won't work otherwise
 *  thumbWidth - width of thumbnail to show/generate
 *  thumbHeight - height of thumbnail to show/generate
 */
(function(){var sfPhotoGallery=function(el,options){this.dataList=jQuery(el).hide();this.options={};this.options.transition=typeof options.transition==="undefined"?'fade':options.transition;this.options.transitionSpeed=options.transitionSpeed||300;this.options.transitionInOptions=options.transitionInOptions||{};this.options.transitionOutOptions=options.transitionOutOptions||{};this.options.goodbarry=typeof options.goodbarry==='boolean'?options.goodbarry:true;this.options.thumbsLocation=options.thumbsLocation||'left';this.options.descriptionLocation=options.descriptionLocation||'bottom';this.options.autorotate=typeof options.autorotate==='boolean'?options.autorotate:false;this.options.autorotateDuration=options.autorotateDuration||3000;this.options.thumbsPerPage=options.thumbsPerPage||8;this.options.numbersPerPaginationPage=options.numbersPerPaginationPage||20;this.options.thumbWidth=options.thumbWidth||120;this.options.thumbHeight=options.thumbHeight||80;this.dataItems=this.dataList.find('li').map(function(index,item){item=jQuery(item);return{index:index,media:item.find('.media').html(),thumb:item.find('.thumb').attr('src'),title:item.find('.title').html(),description:item.find('.description').html()};});this.count=this.dataItems.length;};sfPhotoGallery.prototype.build=function(){this.thumbsSlider=this.createThumbsSlider();this.paginationWrapper=this.createPagination();this.mediaWrapper=this.createMediaWrapper();this.descriptionWrapper=this.createDescriptionWrapper();this.dataList.replaceWith(this.buildStructure());this.show(0);this.thumbsContainerWidth=this.thumbsSlider.width();this.$slider.width(this.thumbsContainerWidth*this.thumbPagesCount);this.paginationContainerWidth=Math.floor(this.paginationWrapper.find('.sfmg-pagination-pages').width()-this.paginationWrapper.find('.sfmg-pagination-pages li').width()*2);this.$pagination.width(this.paginationContainerWidth*this.paginationPagesCount);};sfPhotoGallery.prototype.buildStructure=function(){var container=jQuery('<div class="sfmg"/>'),colA=jQuery('<div class="sfmg-wrapper-a"/>'),colB=jQuery('<div class="sfmg-wrapper-b"/>'),t=this.thumbsSlider,m=jQuery('<div class="sfmg-wrapper-c"/>').append(this.mediaWrapper,this.paginationWrapper),d=this.descriptionWrapper;container.append(colA,colB);colA.append(t);colB.append(m,d);var location=this.options.descriptionLocation+'-'+this.options.thumbsLocation;switch(location){case'top-top':case'left-left':colA.append(d,t);colB.append(m);break;case'top-right':case'top-bottom':colA.append(d,m);colB.append(t);break;case'top-left':colA.append(t);colB.append(d,m);break;case'right-right':case'bottom-bottom':colA.append(m);colB.append(d,t);break;case'right-bottom':colA.append(m,t);colB.append(d);break;case'right-top':case'right-left':colA.append(t,m);colB.append(d);break;case'bottom-top':case'bottom-left':colA.append(t);colB.append(m,d);break;case'bottom-right':colA.append(m,d);colB.append(t);break;case'left-top':colA.append(d);colB.append(t,m);break;case'left-right':case'left-bottom':colA.append(d);colB.append(m,t);break;}
if(location.match(/(left|right)/)){colA.addClass('sfmg-col-left');colB.addClass('sfmg-col-right');}
return container;};sfPhotoGallery.prototype.restartAutorotate=function(){var self=this;this.stopAutorotate();var duration=this.options.autorotateDuration+2*this.options.transitionSpeed;this.$autorotate=window.setInterval(function(){var nextItem=self.currentItem+1;if(nextItem===self.count){nextItem=0;}
self.show(nextItem);},duration);};sfPhotoGallery.prototype.stopAutorotate=function(){window.clearInterval(this.$autorotate);};sfPhotoGallery.prototype.createMediaWrapper=function(){var media=jQuery('<div class="sfmg-main"/>');this.$media=jQuery('<div class="sfmg-main-media"/>');media.append(this.$media);return media;};sfPhotoGallery.prototype.createDescriptionWrapper=function(){var description=jQuery('<div class="sfmg-description"/>"');this.$title=jQuery('<h3 class="sfmg-title"/>');this.$description=jQuery('<p class="sfmg-content"/>');description.append(this.$title,this.$description);return description;};sfPhotoGallery.prototype.createThumbsSlider=function(){var self=this;this.thumbPagesCount=Math.ceil(this.count/this.options.thumbsPerPage);this.thumbsContainerWidth=0;this.currentThumbPage=0;var slider=jQuery('<div class="sfmg-thumbs"><ul class="sfmg-thumbs-nav"><li class="sfmg-thumbs-nav-prev"/><li class="sfmg-thumbs-nav-next"/></ul></div>');this.$slider=jQuery('<div class="sfmg-thumbs-wrapper"/>');slider.prepend(this.$slider);this.$thumbPrev=jQuery('<a href="./">more</a>').click(function(e){e.preventDefault();self.cycleThumbs(self.currentThumbPage-1);});this.$thumbNext=jQuery('<a href="./">more</a>').click(function(e){e.preventDefault();self.cycleThumbs(self.currentThumbPage+1);});slider.find('.sfmg-thumbs-nav-prev').append(this.$thumbPrev);slider.find('.sfmg-thumbs-nav-next').append(this.$thumbNext);var appendItem=function(index,item){var li=self.createThumbnail(item);slider.find('.sfmg-thumbs-wrapper ul:last').append(li);};for(var i=0,start,end;i<this.thumbPagesCount;i++){slider.find('.sfmg-thumbs-wrapper').append('<ul/>');start=i*this.options.thumbsPerPage;end=start+this.options.thumbsPerPage;jQuery.each(this.dataItems.slice(start,end),appendItem);}
if(this.options.autorotate){var ar=jQuery('<p class="sfmg-autorotate"/>'),arTrigger=jQuery('<a href="./">Start</a>').click(function(e){e.preventDefault();if(jQuery(this).hasClass('active')){jQuery(this).removeClass('active').html('Start');self.stopAutorotate();}
else{jQuery(this).addClass('active').html('Stop');self.restartAutorotate();}});ar.append(arTrigger);slider.prepend(ar);}
return slider;};sfPhotoGallery.prototype.createPagination=function(){var self=this;this.paginationPagesCount=Math.ceil((this.count-2)/(this.options.numbersPerPaginationPage-2));this.paginationContainerWidth=0;this.currentPaginationPage=0;var pagination=jQuery('<div class="sfmg-pagination"><ul class="sfmg-pagination-steps"><li class="sfmg-pagination-steps-prev"/><li class="sfmg-pagination-steps-next"/></ul><div class="sfmg-pagination-pages"></div></div>');this.$pagination=jQuery('<ul/>');pagination.find('.sfmg-pagination-pages').append(this.$pagination);var paginationItemClick=function(e){e.preventDefault();self.show(e.data.index);};jQuery.each(this.dataItems,function(index,item){var li=jQuery('<li />');var trigger=jQuery('<a href="./">'+(index+1)+'</a>').bind('click',{"index":index},paginationItemClick);li.append(trigger);pagination.find('.sfmg-pagination-pages ul').append(li);});this.$paginationPrev=jQuery('<a href="./">«</a>').click(function(e){e.preventDefault();self.show(self.currentItem-1);});pagination.find('.sfmg-pagination-steps-prev').append(this.$paginationPrev);this.$paginationNext=jQuery('<a href="./">»</a>').click(function(e){e.preventDefault();self.show(self.currentItem+1);});pagination.find('.sfmg-pagination-steps-next').append(this.$paginationNext);return pagination;};sfPhotoGallery.prototype.createThumbnail=function(item){var li=jQuery('<li/>'),trigger,thumb,self=this,thumbURL;if(item.thumb){thumbURL=item.thumb;}
else{thumbURL=jQuery(item.media).attr('src');if(this.options.goodbarry){thumbURL='/Utilities/ShowThumbnail.aspx?'+jQuery.param({"USM":1,"W":this.options.thumbWidth,"H":this.options.thumbHeight,"R":1,"Img":thumbURL});}}
trigger=jQuery('<a href="./"/>').bind('click',{index:item.index},function(e){e.preventDefault();self.show(e.data.index);});thumb=jQuery('<img src="'+thumbURL+'" alt="'+item.title+'" width="'+this.options.thumbWidth+'" height="'+this.options.thumbHeight+'" />');trigger.append(thumb);li.append(trigger);return li;};sfPhotoGallery.prototype.show=function(index){if(index===this.currentItem||index<0||index>=this.count){return false;}
if(this.$media.is(':animated')||this.$slider.is(':animated')){return false;}
this.currentItem=index;var item=this.dataItems[index];this.switchItem(item);this.$pagination.find('li').removeClass('active').eq(index).addClass('active');this.$slider.find('li').removeClass('active').eq(index).addClass('active');if(this.options.autorotate&&this.$autorotate){this.restartAutorotate();}
this.cycleThumbs(Math.floor(index/this.options.thumbsPerPage));this.cyclePaging(index);};sfPhotoGallery.prototype.switchItem=function(i){var $ui=(typeof jQuery.fn.effect==='function'),s=this.options.transitionSpeed,t=this.options.transition,media=this.$media,title=this.$title,desc=this.$description;switch(t){case false:media.html(i.media);title.html(i.title);desc.html(i.description);break;case'fade':media.fadeOut(s,function(){media.html(i.media).fadeIn(s);});title.fadeOut(s,function(){title.html(i.title).fadeIn(s);});desc.fadeOut(s,function(){desc.html(i.description).fadeIn(s);});break;default:if($ui===false){throw"Unable to load jQuery UI";}
media.hide(t,s,function(){media.html(i.media).show(t,s);});title.hide(t,s,function(){title.html(i.title).show(t,s);});desc.hide(t,s,function(){desc.html(i.description).show(t,s);});break;}};sfPhotoGallery.prototype.cycleThumbs=function(page){if(page!==this.currentThumbPage){this.$slider.animate({'marginLeft':-1*page*this.thumbsContainerWidth},300);this.currentThumbPage=page;}
if(page===0){this.$thumbPrev.hide();}
else{this.$thumbPrev.show();}
if(page+1<this.thumbPagesCount){this.$thumbNext.show();}
else{this.$thumbNext.hide();}};sfPhotoGallery.prototype.cyclePaging=function(index){var page=Math.floor((index-1)/(this.options.numbersPerPaginationPage-2));if(page<0){page=0;}
if(page!==this.currentPaginationPage&&page<this.paginationPagesCount){this.$pagination.animate({'marginLeft':-1*page*this.paginationContainerWidth},300);this.currentPaginationPage=page;}
if(index===0){this.$paginationPrev.hide();}
else{this.$paginationPrev.show();}
if(index+1<this.count){this.$paginationNext.show();}
else{this.$paginationNext.hide();}};jQuery.fn.sfPhotoGallery=function(options){options=options||{};return this.each(function(){var pf=new sfPhotoGallery(this,options);pf.build();});};})();


/**
 * Compact labels plugin
 * Takes one option
 *  - labelOpacity [default: true] - set to false to disable label opacity change on empty input focus
 */
(function($){$.fn.compactize=function(options){var defaults={labelOpacity:true};options=$.extend(defaults,options);return this.each(function(){var label=$(this),input=$('#'+label.attr('for'));input.focus(function(){if(options.labelOpacity){if(input.val()===''){label.css('opacity','0.5');}} else{label.hide();}});if(options.labelOpacity){input.keydown(function(){label.hide();label.css('opacity',1);});} input.blur(function(){if(input.val()===''){label.show();} if(options.labelOpacity){label.css('opacity',1);}});window.setTimeout(function(){if(input.val()!==''){label.hide();}},50);});};})(jQuery);


/*
 * hrefID jQuery extention - returns a valid #hash string from link href attribute in Internet Explorer
 */
(function($){$.fn.extend({hrefId:function(){return $(this).attr('href').substr($(this).attr('href').indexOf('#'));}});})(jQuery);

/*
 * Scripts
 *
 */
jQuery(function($) {
 
	var Engine = {
		utils : {
			links : function(){
				$('a[rel*=external]').click(function(e){
					e.preventDefault();
					window.open($(this).attr('href'));						  
				});
			},
			mails : function(){
				$('a[href^=mailto:]').each(function(){
					var mail = $(this).attr('href').replace('mailto:','');
					var replaced = mail.replace('/at/','@');
					$(this).attr('href','mailto:'+replaced);
					if($(this).text() == mail) {
						$(this).text(replaced);
					}
				});
			},
			labels : function(){
				$('.form-b label, .search-form label').compactize();
			}			
		},
		ui : {
			rotator : function(){			
				$('#slider').nivoSlider({
					effect: 'random'
				});
			},
			gallery : function(){
				$('#gallery > div').sfPhotoGallery({
					'goodbarry' : false
				});		
			}
		},
		fixes : {
			blog : function(){
				// no comments/trackbacks + alternative
				var $comments = $('div.comments-a');
				$comments.each(function(){
					if($(this).find('div.comment').length == 0){
						var fixed = $(this).html().replace('</h2>','</h2><p class="empty">') + '</p>';
						$(this).html(fixed);
					} else {
						$(this).find('div.comment:odd').addClass('alt');
					}
				});
				
				// show/hide comments/trackbacks
				var $links = $('div.post-a p.info a.comments, div.post-a p.info a.trackbacks');
				$links.click(function(){
					$($(this).hrefId()).toggle();
					if($(this).is('.comments')) $($(this).hrefId()).next('div.add-comment-a:first').toggle();
					return false;
				});
			
				// single post (show trackbacks and comments)
				if($('div.post-a').length == 1){
					$('div.comments-a, div.add-comment-a').show();
				}
			},			
		
		pagination : function(){
				$('.pagination').contents().each(function(){
					if (this.nodeType === 3 && this.nodeValue !== null && /[0-9]+/.test(this.nodeValue)) {
						$(this).replaceWith(' <strong>'+$.trim(this.nodeValue)+'</strong> ')
					}
				});
			}
		}
	};

	Engine.utils.links();
	Engine.utils.mails();
	Engine.utils.labels();
	Engine.ui.rotator();
	Engine.ui.gallery();
	Engine.fixes.blog();
	Engine.fixes.pagination();

});
