(function($) {

    $.fn.innerfade = function(options) {
        return this.each(function() {   
            $.innerfade(this, options);
        });
    };

    $.innerfade = function(container, options) {
        var settings = {
        		'animationtype':    'fade',
            'speed':            'normal',
            'type':             'sequence',
            'timeout':          2000,
            'containerheight':  'auto',
            'runningclass':     'innerfade',
            'children':         null
        };
        if (options)
            $.extend(settings, options);
        if (settings.children === null)
            var elements = $(container).children();
        else
            var elements = $(container).children(settings.children);
        if (elements.length > 1) {
            $(container).css('position', 'relative').css('height', settings.containerheight).addClass(settings.runningclass);
            for (var i = 0; i < elements.length; i++) {
                $(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide();
            };
            if (settings.type == "sequence") {
                setTimeout(function() {
                    $.innerfade.next(elements, settings, 1, 0);
                }, settings.timeout);
                $(elements[0]).show();
            } else if (settings.type == "random") {
            		var last = Math.floor ( Math.random () * ( elements.length ) );
                setTimeout(function() {
                    do { 
												current = Math.floor ( Math.random ( ) * ( elements.length ) );
										} while (last == current );             
										$.innerfade.next(elements, settings, current, last);
                }, settings.timeout);
                $(elements[last]).show();
						} else if ( settings.type == 'random_start' ) {
								settings.type = 'sequence';
								var current = Math.floor ( Math.random () * ( elements.length ) );
								setTimeout(function(){
									$.innerfade.next(elements, settings, (current + 1) %  elements.length, current);
								}, settings.timeout);
								$(elements[current]).show();
						}	else {
							alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
						}
				}
    };

    $.innerfade.next = function(elements, settings, current, last) {
        if (settings.animationtype == 'slide') {
            $(elements[last]).slideUp(settings.speed);
            $(elements[current]).slideDown(settings.speed);
        } else if (settings.animationtype == 'fade') {
            $(elements[last]).fadeOut(settings.speed);
            $(elements[current]).fadeIn(settings.speed, function() {
							removeFilter($(this)[0]);
						});
        } else
            alert('Innerfade-animationtype must either be \'slide\' or \'fade\'');
        if (settings.type == "sequence") {
            if ((current + 1) < elements.length) {
                current = current + 1;
                last = current - 1;
            } else {
                current = 0;
                last = elements.length - 1;
            }
        } else if (settings.type == "random") {
            last = current;
            while (current == last)
                current = Math.floor(Math.random() * elements.length);
        } else
            alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
        setTimeout((function() {
            $.innerfade.next(elements, settings, current, last);
        }), settings.timeout);
    };

})(jQuery);

// **** remove Opacity-Filter in ie ****
function removeFilter(element) {
	if(element.style.removeAttribute){
		element.style.removeAttribute('filter');
	}
}


function previewEvent(x) {
		// Grab event text...
		$("div#event_information div").hide();
/*
if(x < 173) {
		if(x==156) $("div#e156").show();
		if(x==157) $("div#e157").show();
		if(x==158) $("div#e158").show();
		if(x==159) $("div#e159").show();
		if(x==160) $("div#e160").show();
		if(x==161) $("div#e161").show();
		if(x==162) $("div#e162").show();
		if(x==163) $("div#e163").show();
		if(x==164) $("div#e164").show();
		if(x==165) $("div#e165").show();
		if(x==166) $("div#e166").show();
		if(x==167) $("div#e167").show();
		if(x==168) $("div#e168").show();
		if(x==169) $("div#e169").show();
		if(x==170) $("div#e170").show();
		if(x==171) $("div#e171").show();
		if(x==172) $("div#e172").show();
	} else {
*/
		$("div#e" + x).show();	
/* 	} */
}


$(document).ready(function() {

$("a.lightbox").lightBox();

function setupMenu() {
$("ul#nv>li").each(function(){				// positioning needs to update when font size is changed so we encapsuate it
		var offset = $(this).offset();
		offset.top += ($("li#search").height() + 1);
		$(this).children("ul").css({position:"absolute",zIndex:"90000000",display:"block",top:offset.top,left:offset.left+1});	
});
}

setupMenu();

$('ul#nv').superfish({animation:{height:'show'},dropShadows:false,delay:250});

$("ul#photoselector a").click(function(){
  $("div#photopane img").hide().attr({"src": $(this).attr("href"), "title": $("> img", this).attr("title")});
  return false;
});

$("div#photopane img").load(function(){$("div#photopane img:hidden").show(); ///fadeIn("slow")   
});

$("input#subscribe").focus(function() {
$(this).removeClass("placeholder");
$(this).attr("value", "");
});

$("input#searchterm").focus(function() {
$(this).removeClass("placeholder");
$(this).attr("value", "");
});

$("input#searchterm").blur(function() {
if($(this).attr("value").length<1){
$(this).attr("value", "search");
$(this).addClass("placeholder");
}
});

$("input#subscribe").blur(function() {
if($(this).attr("value").length<1){
$(this).attr("value", "your email address");
$(this).addClass("placeholder");
}
});

// Sponsors fader
/*
if($('.sponsor_list').length>0) {
	$('.sponsor_list').innerfade({
				speed: 'fast',
				timeout: 3000,
				type: 'sequence',
				containerheight: '150px'
			});
}
*/

	$('#photopane-fading').innerfade({
				speed: 'slow',
				timeout: 6000,
				type: 'sequence',
				containerheight: '300px'
			});

// Home page images
if($('body.home').length>0) {
        $('#large_image').innerfade({
                                speed: 'fast',
                                timeout: 5000,
                                type: 'sequence',
				containerheight: '32.7em'
                        });
}


/* Events Scroller */
if($('#slideshow').length>0) {
if($('#slideshow ul').children().length < 6) { $("#slider_prev").hide(); $("#slider_next").hide(); }
$('#slideshow').serialScroll({
		items:'li',
		prev:'#slider a.prev',
		next:'#slider a.next',
		offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
		start:0, //as we are centering it, start at the 2nd
		duration:1200,
		force:true,
		stop:true,
		lock:false,
		cycle:true,
//		easing:'easeOutQuart', //use this easing equation for a funny effect
		jump: false //click on the images to scroll to them
	});
}

});
