$.extend(Function.prototype, {
    use: function() {
		var method = this, args = Array.prototype.slice.call(arguments), object = args.shift();
		return function() {
			return method.apply(object, args.concat(Array.prototype.slice.call(arguments)));
		}
    },
    useEL: function() {
        var method = this, args = Array.prototype.slice.call(arguments), object = args.shift();
        return function(event) {
            return method.apply(object, [event || window.event].concat(Array.prototype.slice.call(arguments)));
        }
    }
});
/**
* Helper method for stopping events.
**/
$.extend(jQuery.Event.prototype, {
    stop: function() {
        this.stopPropagation();
        this.preventDefault();
    }
});

$(document).ready(function() {
    
    /**
    * Fade the search label down on focus
    * Hide the label when the user is typing
    * Show the label on blur if the field is empty
    **/
    labelover = 'label[for=search], .labelover label';
    $inputs = $('#search, .labelover input');
    $inputs.focus(function() {
        if($(this).val() == '') {
            $(this).prev(labelover).stop().animate({ opacity: 0.4 }, 200);
        }
    }).blur(function() {
        if($(this).val() == '') {
            $(this).prev(labelover).stop().animate({ opacity: 1 }, 200);
        }
    }).keydown(function() {
        if($(this).css('opacity')) {
            $(this).prev(labelover).stop().animate({ opacity: 0 }, 200);
        }
    });
    
    /**
    * Remove the value from the header submit button for IE
    **/
    $('#header input[type="submit"]').val('');
    
    
    //$('a').each(function() {
    //    
    //    /**
    //    * Open links to other sites in the #content section in a new window
    //    **/
    //    var a = new RegExp('/' + window.location.host + '/');
    //    if(!a.test(this.href)) {
    //        $(this).click(function(event) {
    //            event.stop();
    //            window.open(this.href, '_blank');
    //        });
    //    }
    //    
    //    /**
    //    * Automatically append class names to files
    //    **/
    //    if((ext = $(this).attr('href').match(/[.](rss|atom|doc|docx|xls|xlsx|pdf|zip)$/))) { 
    //        $(this).addClass('icon').addClass(ext[1]); 
    //    }
    //    
    //});
    
    $('.fact, .testimonial').hide();
    $('.fact:first').show();
    var $facts = $('.fact'), 
        $testiominals = $('.testimonial'),
        $nextFact = $('#nextFact'),
        $nextTestimonial = $('#nextTestimonial')
    var factIndex = 0, testimonialIndex = 0, showing = 'facts';
    
    function showNextFact(event) {
        event.stop();
        
        $($facts.get(factIndex)).slideUp('500');
        
        factIndex = (factIndex < $facts.length - 1) ? factIndex + 1 : 0;
        
        $($facts.get(factIndex)).slideDown('500');
    };
 
    function showNextTestimonial(event) {
        event.stop();
        
        $($testiominals.get(testimonialIndex)).slideUp('500');
        
        testimonialIndex = (testimonialIndex < $testiominals.length - 1) ? testimonialIndex + 1 : 0;
        
        $($testiominals.get(testimonialIndex)).slideDown('500');
    };
        
    function swapFactTestimonial(event) {
        event.stop();
        
        switch(showing) {
            case 'facts' :
                $facts.slideUp('500');
                $($testiominals.get(testimonialIndex)).slideDown('500');
                
                $nextFact.unbind('click.next').removeClass('right').addClass('down').bind('click.init', swapFactTestimonial).text('View Facts');
                $nextTestimonial.unbind('click.init').removeClass('down').addClass('right').bind('click.next', showNextTestimonial).text('Next Testimonial');
                
                showing = 'testimonials';
            break;
            case 'testimonials' :
                $testiominals.slideUp('500');
                $($facts.get(factIndex)).slideDown('500');
                
                $nextTestimonial.unbind('click.next').removeClass('right').addClass('down').bind('click.init', swapFactTestimonial).text('View Testimonials');
                $nextFact.unbind('click.init').removeClass('down').addClass('right').bind('click.next', showNextFact).text('Next Fact');
                
                showing = 'facts';
            break;
        }
    }
    
    $nextFact.bind('click.next', showNextFact);
    $nextTestimonial.bind('click.init', swapFactTestimonial);
    
    $('#contactform .additional').hide();
    $('#contactform #subject').change(function() {
        $this = $(this);
        if($this.val() == $($this.children('option').get(1)).text()) {
            $('#contactform .additional').slideDown(500);
        } else {
            $('#contactform .additional').slideUp(500);
        }
    })
    
});

