﻿// Script for Top Fading Buttons (jquery.disabled)
jQuery.extend(jQuery.expr[":"], {
        enabled : function(a){ return !jQuery.attr(a, 'disabled'); },
        disabled : function(a){ return jQuery.attr(a, "disabled"); }
});


jQuery.fn.extend({
    disable : function() {
        return this.each(function(){ jQuery(this).attr("disabled", true); });
    },
    enable : function() {
        return this.each(function(){ jQuery(this).removeAttr("disabled"); });
    },
    toggleDisabled : function() {
        return this.each(function(){
            $(this)[$(this).is(":enabled") ? "disable" : "enable"]();
        });
    }
});


// Script for slideshow (Top slideshow)
jQuery.fn.slideshow = function(options) {
	var settings = {
		timeout: '2000',
		type: 'sequence',
		pauselink: null,
		playcallback: null,
		pausecallback: null
	}
	if(options)
		jQuery.extend(settings, options);
	
	var pauseState = 0;
	var current = 1;
	var last = 0;
	var timer = '';
	
	var change = function () {
		if ( pauseState == 0 ) {
			for (var i = 0; i < slides.length; i++) {
				jQuery(slides[i]).css('display', 'none');
			}
			jQuery(slides[last]).css('display', 'block').css('zIndex', '0');
			jQuery(slides[current]).css('zIndex', '1').fadeIn('slow');
			
			if ( settings.type == 'sequence' ) {
				if ( ( current + 1 ) < slides.length ) {
					current = current + 1;
					last = current - 1;
				}
				else {
					current = 0;
					last = slides.length - 1;
				}
			}
			else if ( settings.type == 'random' ) {
				last = current;
				while (	current == last ) {
					current = Math.floor ( Math.random ( ) * ( slides.length ) );
				}
			}
			else {
				alert('type must either be \'sequence\' or \'random\'');
			}
			timer = setTimeout(change, settings.timeout);
		}
	}
	
	var pause = function() {
		if ( pauseState == 0 ) {
			pauseState = 1;
			clearTimeout(timer);
			if ( settings.playcallback != null ) {
				settings.pausecallback(jQuery('#' + settings.pauselink));
			}
		}
		else {
			pauseState = 0;
			change();
			if ( settings.playcallback != null ) {
				settings.playcallback(jQuery('#' + settings.pauselink));
			}
		}
		return false;
	}
	
	this.css('position', 'relative');
	var slides = this.find('img').get();
	jQuery.each(slides, function(i){
		jQuery(slides[i]).css('zIndex', slides.length - i).css('position', 'absolute').css('top', '0').css('left', '0');
	});
	if ( settings.type == 'sequence' ) {
		timer = setTimeout(change, settings.timeout);
	}
	else if ( settings.type == 'random' ) {
		do { current = Math.floor ( Math.random ( ) * ( slides.length ) ); } while ( current == 0 )
		timer = setTimeout(change, settings.timeout);
	}
	else {
		alert('type must either be \'sequence\' or \'random\'');
	}
	
	if ( settings.pauselink != null ) {
		jQuery('#' + settings.pauselink).click(pause);
	}
	
	return this;
};


// Script for sliding effect navigation (Nav menu)
$(document).ready(function()
{
	slide("#sliding-navigation", 25, 15, 200, .8);
});

function slide(navigation_id, pad_out, pad_in, time, multiplier)
{
	// creates the target paths
	var list_elements = navigation_id + " li.sliding-element";
	var link_elements = list_elements + " a";
	
	// initiates the timer used for the sliding animation
	var timer = 0;
	
	// creates the slide animation for all list elements 
	$(list_elements).each(function(i)
	{
		// margin left = - ([width of element] + [total vertical padding of element])
		$(this).css("margin-left","-200px");
		// updates timer
		timer = (timer*multiplier + time);
		$(this).animate({ marginLeft: "0" }, timer);
		$(this).animate({ marginLeft: "15px" }, timer);
		$(this).animate({ marginLeft: "0" }, timer);
	});

	// creates the hover-slide effect for all link elements 		
	$(link_elements).each(function(i)
	{
		$(this).hover(
		function()
		{
			$(this).animate({ paddingLeft: pad_out }, 200);
		},		
		function()
		{
			$(this).animate({ paddingLeft: pad_in }, 200);
		});
	});
}