
jQuery(window).load(function() {

    var $ = jQuery;

    ////////////////////////////////////////////////
    // clear default
    ////////////////////////////////////////////////
    jQuery.fn.clearDefault = function(){
        return this.each(function(){
            var default_value = $(this).val();
            $(this).focus(function(){
                if ($(this).val() == default_value) $(this).val("");
            });
            $(this).blur(function(){
                if ($(this).val() == "") $(this).val(default_value);
            });
        });
    };
    
    $('#srch_fld').clearDefault();
    $('#searchForm').submit(function(){
        var f = $('#srch_fld');
        var val = f.val();
        var def = f.attr('defaultValue');
        if(val == '' || val == def){
            f.val(def);
            return false;
        }
    })
    
    
    $.ajax({ 
        url: '/xml/homepage_quotes.xml', 
        success: buildSlideShow
    });
    
    function buildSlideShow(data){
        $('#branding_carousel').removeClass('ajax_loading');
        var image_wrapper = $('#image_wrapper');
        var image_meta_wrapper = $('#image_meta_wrapper');
        var slideshow_tools = $('<ul class="controls clearfix">')
            .appendTo($('#slideshow_tools'));
        $('quote0, quote1, quote2, quote3, quote4', data).each(function(i,e){
            var image = $('image', e).text();
            image_wrapper.append('<img src="'+ image +'" alt="" class="carousel_item" />');
            var caption_text = $('text', e).text();
            var link = $('link', e).text();
            image_meta_wrapper.append('' +
            '<div class="image_meta">' +
                '<h3 class="image_caption"><a href="' + link + '"><span>'+ caption_text +'</span></a></h3>' +
                '<p class="learn_more"><a href="' + link + '">Learn more</a></p>' +
            '</div>');
            slideshow_tools.append('<li><a href="#">Slide '+ i +'</a></li>');
        });
        startSlideShow();
    }
    
    function startSlideShow(){
	    $('.carousel_item:first').show();
    	$('.carousel_item:first img').hide();
    	$('.carousel_item:first img').fadeIn(1600);
	
    	$('.controls li:first').addClass('active');
    	$('#image_meta_wrapper div.image_meta:first').show();
	
    	var current = 0;
    	var next_slide = 1;
    	var total_slides = $('.carousel_item').length - 1;
    	var interval = slideShowInterval();
	
    	function slideShowInterval(){
    		return setInterval(function() {

    			current = next_slide;
    			show(current);
    			if (current == total_slides ){
    				next_slide = 0;
    			} else{
    				next_slide = current + 1;
    			}

    		}, 8000);	
    	}
	
    	// this happens when you click on a bullet
    	$('.controls li a').click(function(){
    		clearInterval(interval);
    		current = $(this).parent().index();
    		next_slide = current; 
    		show(current);
    		if (current == total_slides ){
    			next_slide = 0;
    		} else{
    			next_slide = current + 1;
    		}
    		interval = slideShowInterval();
    		return false;
    	});
	
    	function show(n){
    		var slide = $('.carousel_item').get(n);
    		var active_bullet = $('.controls li').get(n);
    		var slide_meta = $('#image_meta_wrapper div.image_meta').get(n);
		
    		$('.carousel_item').fadeOut(1600);
    		$(slide).fadeIn(1600);
		
    		$('.controls .active').removeClass('active');
    		$(active_bullet).addClass('active');
		
    		$('#image_meta_wrapper div.image_meta').hide();
    		$(slide_meta).show();
    	}
    }
});
