$(document).ready(function(){
	/**Search Form**/
	$('#searchForm input#q').css('borderColor','#6DA553'); //Change it back
	$('#searchForm input#q').css('padding','2px'); //Change it back
	$('#searchForm input#q').focus(function() {
		if($(this).val() == 'search') {
			$(this).val('');	
		}
	});
	$('#searchForm input#q').blur(function() {
		if($(this).val() == '') {
			$(this).css('background', '#ffffff'); //Stupid google search thingy
			$(this).val('search');
		}
	});
	/**End search form**/

	/**tabs**/
	$('#container-1').tabs();
	/**End tabs**/

	/**Text resizer**/
	if($.cookie('textSize')){
		$('#container').css('font-size', $.cookie('textSize')+'px');
	}
	$("#textIncrease").click(function(){
		var currentFontSize = $('#container').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		if(currentFontSizeNum < 16) {
			var newFontSize = currentFontSizeNum+2;
			$('#container').css('font-size', newFontSize+'px');
			$.cookie('textSize', newFontSize, { path: '/'});
		}
		return false;
	});
	$("#textDecrease").click(function(){
		var currentFontSize = $('#container').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		if(currentFontSizeNum > 10) {
			var newFontSize = currentFontSizeNum-2;
			$('#container').css('font-size', newFontSize);
			$.cookie('textSize', newFontSize, { path: '/'});
		}
		return false;
	});
	/**End text resize**/
	
	/**Striping**/
	$("tbody.striped tr:nth-child(odd)").addClass("odd");
	$("tbody.striped tr:nth-child(even)").addClass("even");

	/**Striping**/
	$("table.striped tbody tr:nth-child(odd)").addClass("odd");
	$("table.striped tbody tr:nth-child(even)").addClass("even");

	$("ul.striped li:nth-child(odd)").addClass("odd");
	$("ul.striped li:nth-child(even)").addClass("even");
	/**End striping**/
	
	/**Lightbox**/
	$('a[rel*=lightbox]').facebox();
	jQuery.each($('a[rel*=lightbox]'),function() {
		var curUrl = $(this).attr('href');
		if(curUrl.indexOf('gif') == -1) {
			if(curUrl.indexOf('?') != -1) {
				$(this).attr('href',curUrl+'&modal=true');
			} else {
				$(this).attr('href',curUrl+'?modal=true');	
			}
		}
	});
	/**End lightbox**/

	// Striping //
	$(".striped li:odd").addClass("odd");
	$("table.striped tr:odd").addClass("odd");
	$("table.striped tr:even").addClass("even");
	$("ol.striped div:odd").addClass("odd");
	$("ol.striped div:even").addClass("even");
	$("div.striped div:odd").addClass("odd");
	$("div.striped div:even").addClass("even");
	
	/**Member login box**/
	$('#memberLoginLink').click(function() {
		$('#utilityLogin').slideToggle();
		return false;
	});
	/**End member login box**/

	/**Detect pdf Links to display download link**/
	if($('a[href$=.pdf]').length > 0) {
		$("div#pdf_module").fadeIn("slow");
	}
	/**end Detect pdf Links to display download link**/
	
	/**State Allocation Charts**/
	$('select#stateAllocationCharts').change(function() {
		if($(this).val() != '') {
			window.location = '/files/advocacy/campaigns/' + $(this).val() + '.pdf';
		}
	});
	/**End state allocation charts**/

	/**State Sample Letters to Congress**/
	$('select#stateSampleLettersCongress').change(function() {
		if($(this).val() != '') {
			window.location = '/files/advocacy/campaigns/' + $(this).val() + '.pdf';
		}
	});
	/**State Sample Letters to Congress**/
	
	/**Member dues contacts**/
	//$('ol.contacts > li:not(:first-child)').hide().addClass('hidden');
	if (document.agencyInfoForm){
		var numberCurrentContacts = (document.agencyInfoForm.numberCurrentContacts.value - 1);
		for (i=numberCurrentContacts;i<4;i++){
			$('ol.contacts > li').eq(i).hide().addClass('hidden');
		}
		
		$('ol.contacts').after('<p class="addContact"><a href="#">Add Another Contact</a></p>');
		$('ol.contacts + p.addContact a').click(function() {
			if($('ol.contacts > li.hidden').length > 0) {
				$('ol.contacts > li.hidden:first').fadeIn('normal').removeClass('hidden');
				if($('ol.contacts > li.hidden').length == 0) {
					$('ol.contacts + p.addContact').hide();
				}
			}
			
			return false;
		});
	}
	if (numberCurrentContacts == 4){
		$('ol.contacts + p.addContact').hide();
	}
	/**End member dues contacts**/

	$(function() 
	{
		$(".ovea-slider ul")
		.cycle({
			prev:   '.ovea-slider .controls .prev', 
			next:   '.ovea-slider .controls .next',
			timeout: 3000,
			pager:   '.ovea-slider .controls .pages',
			pagerAnchorBuilder: pagerFactory,
			before: function(Ec, En, o, f) 
			{			
				var classTarget = $(Ec);
				var nextClassTarget = $(En);
				
				classTarget = classTarget.context.attributes.rel.value;
				nextClassTarget = nextClassTarget.context.attributes.rel.value;
				
				$('.ovea-slider .controls .' + classTarget).removeClass('selected');
				$('.ovea-slider .controls .' + nextClassTarget).addClass('selected');
			}
		});
		
		// Pager Function
		function pagerFactory(idx, slide) 
		{
			var selected = (idx == 0 ? 'selected' : null);
			return '<img src="images/blank.gif" width="13" height="14" class="number '+ selected + ' ' + parseInt(idx+1)+'" />';
		};
	});

	$("li:last-child").addClass("last");

	/**Focus first item that has a class of focus**/
	//$('.focus:first').focus();
	/**End focus first item**/
});
