jQuery(document).ready(function() {

	// Simon Search input text
	$('#search_input').focus(function() {
		if ($(this).val() == $(this)[0].title) {
			$(this).removeClass("suggested_text");
			$(this).val("");
		}
	});
	
	$('#search_input').blur(function() {
		if ($(this).val() === "") {
			$(this).val($(this)[0].title);
			$(this).addClass("suggested_text");
		}
	});
	
	$.each($('#search_input'), function() {
		$(this).blur();
	});
	// Add Title to Input field
	
	
	// Cycle Homepage Slides 
	$('#slideshow_wrap') 
		.before('<div id="slide_numbers_wrap">') 
		.cycle({ 
		    fx:     'fade', 
		    speed:  1000, 
		    timeout: 5000, 
		    pager:  '#slide_numbers_wrap',
			pause:   1 
	});

	$('a.printPage').click(function(e) {
		e.preventDefault();
		window.print();
	});
	
	// ul.expander 
	var selector = 'ul.expander';
	var liHeight = '100px';
	var rowHeights = [];
	var rowContent = [];
	

	
	
		$(selector + ' li').each(function(index, value){
		
			var rowHeight = $(this).css('height').replace('px','');
			rowHeight = parseInt(rowHeight) + 20;
		
			rowHeights.push(
				 rowHeight
			);
		
		});
	
		$(selector + ' li:odd').each(function(index){
		
			rowContent.push('');
		
			rowContent.push(
				$(this).html()
			);
		
			$(this).html('');
		
		});
	
		hideOddRows();
	
		$(selector + ' li:even').each(function(index, value){
		
			$(this).attr('class', 'question');
			$(this).html('<a class="hiding" href="#' + index + '">' + $(this).html() + '</a>');
		
		});
	
		$(selector + ' li:odd').each(function(index, value){
		
			$(this).html('<span class="padding">' + $(this).html() + '</span>');
		
		});
	
	
	
	$(selector + ' li:even a').live('click', function(e) {
		
		var liIndex = $(this).parent('li').index((selector + ' li'));
		
		e.preventDefault();
		
		if( $(this).parent('li').next('li').css('height') != rowHeights[liIndex+1] + 'px'){
			collapseOddRows($(this));
			expandNextRow($(this), rowHeights[liIndex+1], liIndex+1);
			$(this).css('backgroundImage', 'url(/images/minus_bg.png)');
		} else {
			collapseOddRows($(this));
		}
		
	});
	
	function expandNextRow(clickedLink, height, dex){
		
		clickedLink.parent('li').next('li').animate({
				height: height + 'px'
			}, 
			250, 
			function() {
				clickedLink.parent('li').next('li').html(rowContent[dex]);
		 	}
		);
		
	}
	
	function collapseOddRows(clickedLink){
		
		$(selector + ' li:odd').each(function(index){

			$(this).html('');

		});
		
		$(selector + ' li:odd').animate({
				height: '0px'
			}, 
			250/*, 
			function() {
		    	
		 	}*/
		);
		
		$(selector + ' li:even a').css('backgroundImage', 'url(/images/plus_bg.png)');
		
	}
	
	function hideOddRows(){

		$(selector + ' li:odd').css('overflow', 'hidden');
		$(selector + ' li:odd').css('height', '0');

	}
	
});
