$(document).ready(function() {
	$('.rollover').imgflip();

	$('.popUpNVF').popup();
	$('.popUpChat').popup({
		target: '_blank',
		href: '/livehelp',
		params: 'scrollbars=no,resizable=no,height=480,width=640',
		query: 'popup=true'
	});


	$('#home_top_area_default').removeClass('show');
	$('#home_top_area_default').addClass('hide');
	$('#home_top_area_js').removeClass('hide');
	$('#home_top_area_js').addClass('show');	
	$('.slideshow').slideshow({wait: 6000, fade: 'normal', slideSelector: 'li'});



	$('#slideshow_btn_area li a').each(function() {
		(function($this) {
			$this.bind('mouseover', function() {
				if ( $this.hasClass('btn_active') ) {
				
				} else {
					$this.addClass('btn_on');
				}
			})
			.bind('mouseout', function() {
				$this.removeClass('btn_on');
			});
		})($(this));
	});


	$('#secondary_nav li:not(.active) ul')
		.addClass('hide');

	$('#secondary_nav li .current').removeClass('hide');
	$('#secondary_nav li .current').addClass('show');
	$('#secondary_nav li .current').parent().addClass('active');

	
	$('#secondary_nav li:has(ul) > a').bind('click', function() {


		var $this = $(this);
		if ( $this.parent().hasClass('active') ) {
			$('ul', $this.parent()).removeClass('show');
			$('ul', $this.parent()).addClass('hide');

			$this.parent().removeClass('active');
		} else {
		
			$('#secondary_nav li.active').children().removeClass('show');
			$('#secondary_nav li.active').children().addClass('hide');
			$('#secondary_nav li.active').removeClass('active');
		
			$('ul', $this.parent()).removeClass('hide');
			$('ul', $this.parent()).addClass('show');

			$this.parent().addClass('active');
		}
		return false;
	});

	$('#primary_nav li')
		.bind('mouseover', function() {
			$(this).addClass('hover');
		})
		.bind('mouseout', function() {
			$(this).removeClass('hover');
		});


//DROP DOWN for HOME page NEWS area and BLOG area
	$('.first-child .story_heading').addClass('open');

	$('.home_news_block .story_subject').not( $(".first-child .story_subject") ).hide();
	$('.home_news_block p.story_heading').click(function() {
		$('.home_news_block .story_heading').not(this).removeClass('open').next().slideUp(150);
		$(this).toggleClass('open').next().slideToggle(150);
		return false;
	});

	$('.home_blog_block .story_subject').not( $(".first-child .story_subject") ).hide();
	$('.home_blog_block p.story_heading').click(function() {
		$('.home_blog_block .story_heading').not(this).removeClass('open').next().slideUp(150);
		$(this).toggleClass('open').next().slideToggle(150);
		return false;
	});



	$('#search_text, #newsletter_text').each(function() {
		(function($this) {
			if ( $this.attr('value') == '' ) $this.attr({value: $this.attr('title')});
			$this
				.bind('focus', function() {
					$this.addClass('focus');
					if ( $this.attr('value') == $this.attr('title') ) $this.attr({value: ''});
				})
				.bind('blur', function() {
					$this.removeClass('focus');
					if ( $this.attr('value') == '' ) $this.attr({value: $this.attr('title')});
				});
		})($(this));
	});

	$('.calendar #calendar_controls #event_submit').hide();
	$('.calendar #calendar_controls select').bind('change', function() { $('#calendar_control_form').submit() });

	//$('#primary_nav li span').sifr({flashSrc: 'AvantGardeMonoITCTTBold_bolded.swf'});
	$('#primary_nav li span').sifr({flashSrc: '/include/AvantGardeMonoITCTTBold.swf', xhovercolor: '#FF0000', color: "default", yOffset: -4});
//	$('.calendar #calendar_control_form select').customselect({submit: true});
//	$('.addedit select').customselect();

	$('#calendar_item #back a').bind('click', function() {
		history.back();
		return false;
	});


	
	$('#text_resizer').removeClass('hide');	

	$('#text_resizer .increase_btn').bind('click', function() {
		$('.cms').addClass('large_text');
		$('.cms').children().addClass('large_text');
		$('.cms a').addClass('large_text');

	});
	
	$('#text_resizer .decrease_btn').bind('click', function() {
		$('.cms').removeClass('large_text');
		$('.cms').children().removeClass('large_text');
		$('.cms a').removeClass('large_text');


	});


	

});

//home page tab navigation for NEWS BLOCK and LIFELINE block
$(function () {
    var textBlocks = $('div.home_news_block > div');
    $('div.home_news_block ul.home_news a').click(function () {
		$('.home_news_block .story_heading').removeClass('open');
		$('.home_news_block .story_subject').not( $(".first-child .story_subject") ).hide();

		$('.home_news_block .first-child .story_heading').addClass('open');
		$('.home_news_block .first-child .story_subject').show();

        textBlocks.hide().filter(this.hash).show();     
        $('div.home_news_block ul.home_news a').removeClass('selected');
        $(this).addClass('selected');      
        return false;
    }).filter(':first').click();
});

$(function () {
    var textBlocks = $('div.home_resource_block > div');
    $('div.home_resource_block ul.home_resources a').click(function () {
        textBlocks.hide().filter(this.hash).show();     
        $('div.home_resource_block ul.home_resources a').removeClass('selected');
        $(this).addClass('selected');      
        return false;
    }).filter(':first').click();
});

//home page tab navigation for BLOG BLOCK and DONATION block

$(function () {
    var textBlocks = $('div.home_blog_block > div');
    $('div.home_blog_block ul.home_blog a').click(function () {
		$('.home_blog_block .story_heading').removeClass('open');
		$('.home_blog_block .story_subject').not( $(".first-child .story_subject") ).hide();

		$('.home_blog_block .first-child .story_heading').addClass('open');
		$('.home_blog_block .first-child .story_subject').show();

        textBlocks.hide().filter(this.hash).show();     
        $('div.home_blog_block ul.home_blog a').removeClass('selected_blog');
        $(this).addClass('selected_blog');      
        return false;
    }).filter(':first').click();
});

$(function () {
    var textBlocks = $('div.home_donation_block > div');
    $('div.home_donation_block ul.home_donation a').click(function () {
        textBlocks.hide().filter(this.hash).show();     
        $('div.home_donation_block ul.home_donation a').removeClass('selected_blog');
        $(this).addClass('selected_blog');      
        return false;
    }).filter(':first').click();
});



jQuery.fn.slideshow = function(settings) {
	var options = jQuery.extend({
		wait: 6000,
		selected_wait: 8000,
		fade: 'slow',
		prepend: 'slideshow',
		slideSelector: 'img',
		controls: true,
		counter: 1,
		current: null
	}, settings || {});

	return this.each(function() {
		var time_out = null;
		var fading = false;
		var is_clicking = false;

		$this = $(this)
			.css({position: 'relative'})
			.addClass(options.prepend + 'Main');

		$slides = jQuery(options.slideSelector, this);

		if ( jQuery.browser.safari ) {
			$slides
				.bind('load', function() {
					this.complete = true;
				});
		}

		$slides.css({position: 'absolute', top: 0, left: 0, display: 'none'});
		jQuery($slides[0]).show();


		var change_btn_image = function() {

			if( options.counter > 5 ) {
				options.counter = 2;
			}

			if( options.counter === 1 ) {
				$("#home #slideshow_btn_text_1").removeClass('btn_off');
				$("#home #slideshow_btn_text_1").addClass('btn_active');
			}
			if( options.counter === 2 ) {
				$("#home #slideshow_btn_text_1").removeClass('btn_active');
				$("#home #slideshow_btn_text_1").addClass('btn_off');

				$("#home #slideshow_btn_text_2").removeClass('btn_off');
				$("#home #slideshow_btn_text_2").addClass('btn_active');

			}
			if( options.counter === 3 ) {
				$("#home #slideshow_btn_text_2").removeClass('btn_active');
				$("#home #slideshow_btn_text_2").addClass('btn_off');

				$("#home #slideshow_btn_text_3").removeClass('btn_off');
				$("#home #slideshow_btn_text_3").addClass('btn_active');			
			
			}			
			if( options.counter === 4 ) {
				$("#home #slideshow_btn_text_3").removeClass('btn_active');
				$("#home #slideshow_btn_text_3").addClass('btn_off');

				$("#home #slideshow_btn_text_4").removeClass('btn_off');
				$("#home #slideshow_btn_text_4").addClass('btn_active');			
			
			}
			if( options.counter === 5 ) {
			
				$("#home #slideshow_btn_text_4").removeClass('btn_active');
				$("#home #slideshow_btn_text_4").addClass('btn_off');

				$("#home #slideshow_btn_text_1").removeClass('btn_off');
				$("#home #slideshow_btn_text_1").addClass('btn_active');
			
			}		
			options.counter++;		

		};
		
		
		var next = function() {
			if ( fading ) return;
			var prev = null;
			if ( options.current === null ) {
				options.current = 0;
			} else if ( options.current == $slides.length-1 ) {
				options.current = 0;
				prev = $slides.length-1;
			} else {
				prev = options.current;
				options.current++;
			}

			if ( prev !== null ) {
				var $slide = jQuery($slides[options.current]).css({zIndex: 1});
				var $imgs;
				var complete = false;
				if ( $slides[options.current].nodeName == 'IMG' ) {
					if ( $slides[options.current].complete ) complete = true;
				} else {
					$imgs = jQuery('img', $slide);
					if ( $imgs[0] ) {
						complete = true;
						$imgs.each(function() {
							if ( !this.complete ) {
								complete = false;
								return;
							}
						});
					}
				}

				if ( !complete ) {
					options.current--;
				} else {
					jQuery($slides[prev]).css({zIndex: 0});
					fading = true;
					$slide.fadeIn(options.fade, function() {
						jQuery($slides[prev]).hide();
						fading = false;
					});
				}
			}
		};

		var previous = function() {
			if ( fading ) return;
			var prev = null;
			if ( options.current === null ) {
				options.current = 0;
			} else if ( options.current == 0 ) {
				options.current = $slides.length-1;
				prev = 0;
			} else {
				prev = options.current;
				options.current--;
			}

			if ( prev !== null ) {
				var $slide = jQuery($slides[options.current]).css({zIndex: 100});
				var $imgs;
				var complete = false;
				if ( $slides[options.current].nodeName == 'IMG' ) {
					if ( $slides[options.current].complete ) complete = true;
				} else {
					$imgs = jQuery('img', $slide);
					if ( $imgs[0] ) {
						complete = true;
						$imgs.each(function() {
							if ( !this.complete ) {
								complete = false;
								return;
							}
						});
					}
				}

				if ( !complete ) {
					options.current++;
				} else {
					jQuery($slides[prev]).css({zIndex: 0});
					fading = true;
					$slide.fadeIn(options.fade, function() {
						jQuery($slides[prev]).hide();
						fading = false;
					});
				}
			}
		};

		var pause = function() {
			if ( click_pause ) $this.addClass(options.prepend + 'Paused');
			clearTimeout(time_out);
			time_out = null;
		};

		var unpause = function() {
			$this.removeClass(options.prepend + 'Paused');
			time_out = setTimeout(run, options.wait);
		};

		var showing_all_slides = false;
		var view_all_toggle = function() {
			if ( !showing_all_slides ) {
				click_pause = true;
				pause();
				$slides.css({
					position: 'relative',
					top: null,
					left: null
					})
					.show('fast');
				$this.addClass(options.prepend + 'ViewingAll');
				showing_all_slides = true;
			} else {
				showing_all_slides = false;
				$slides.css({position: 'absolute', top: 0, left: 0, display: 'none'});
				jQuery($slides[options.current]).css({display: 'block'});
				$this.removeClass(options.prepend + 'ViewingAll');
				click_pause = false;
				unpause();
			}
		};

		$slides
			.bind('mouseover', function() {
				if ( !click_pause ) pause();
			})
			.bind('mouseout', function() {
				if ( !click_pause ) unpause();
			});

		var click_pause = false;

		if ( options.controls ) {
			$previous_link = jQuery('<a href="javascript:;">Previous Image</a>')
				.bind('mouseover', function() {
					jQuery(this).addClass('hover');
				})
				.bind('mouseout', function() {
					jQuery(this).removeClass('hover');
				})
				.bind('mousedown', function() {
					is_clicking = true;
				})
				.bind('mouseup', function() {
					is_clicking = false;
				})
				.bind('focus', function() {
					if ( !is_clicking ) jQuery(this).addClass('focus');
				})
				.bind('blur', function() {
					jQuery(this).removeClass('focus');
				})
				.bind('click', previous);

			$previous_container = jQuery('<p/>')
				.addClass(options.prepend + 'Previous')
				.append($previous_link);

			$pause_link = jQuery('<a href="javascript:;">Pause</a>')
				.bind('mouseover', function() {
					jQuery(this).addClass('hover');
				})
				.bind('mouseout', function() {
					jQuery(this).removeClass('hover');
				})
				.bind('mousedown', function() {
					is_clicking = true;
				})
				.bind('mouseup', function() {
					is_clicking = false;
				})
				.bind('focus', function() {
					if ( !is_clicking ) jQuery(this).addClass('focus');
				})
				.bind('blur', function() {
					jQuery(this).removeClass('focus');
				})
				.bind('click', function() {
					if ( time_out ) {
						click_pause = true;
						pause();
					} else {
						click_pause = false;
						unpause();
					}
				});

			$pause_container = jQuery('<p/>')
				.addClass(options.prepend + 'Pause')
				.append($pause_link);

			$next_link = jQuery('<a href="javascript:;">Next Image</a>')
				.bind('mouseover', function() {
					jQuery(this).addClass('hover');
				})
				.bind('mouseout', function() {
					jQuery(this).removeClass('hover');
				})
				.bind('mousedown', function() {
					is_clicking = true;
				})
				.bind('mouseup', function() {
					is_clicking = false;
				})
				.bind('focus', function() {
					if ( !is_clicking ) jQuery(this).addClass('focus');
				})
				.bind('blur', function() {
					jQuery(this).removeClass('focus');
				})
				.bind('click', next);

			$next_container = jQuery('<p/>')
				.addClass(options.prepend + 'Next')
				.append($next_link);

			$view_all_link = jQuery('<a href="javascript:;">View All slides</a>')
				.bind('mouseover', function() {
					jQuery(this).addClass('hover');
				})
				.bind('mouseout', function() {
					jQuery(this).removeClass('hover');
				})
				.bind('mousedown', function() {
					is_clicking = true;
				})
				.bind('mouseup', function() {
					is_clicking = false;
				})
				.bind('focus', function() {
					if ( !is_clicking ) jQuery(this).addClass('focus');
				})
				.bind('blur', function() {
					jQuery(this).removeClass('focus');
				})
				.bind('click', view_all_toggle);

			$view_all_container = jQuery('<p/>')
				.addClass(options.prepend + 'ViewAll')
				.append($view_all_link);

			$this
				.append($view_all_container)
				.append($previous_container)
				.append($pause_container)
				.append($next_container);
		}

		var run = function() {

			change_btn_image();
			next();
			time_out = setTimeout(run, options.wait);

		};

			
		$("#slideshow_btn_text_1").bind("click", function() { 
				
			clearTimeout(time_out);
			time_out = null;



			$("#home #slideshow_btn_text_1").removeClass('btn_off');
			$("#home #slideshow_btn_text_1").addClass('btn_active');
			$("#home #slideshow_btn_text_2").removeClass('btn_active');
			$("#home #slideshow_btn_text_2").addClass('btn_off');
			$("#home #slideshow_btn_text_3").removeClass('btn_active');
			$("#home #slideshow_btn_text_3").addClass('btn_off');
			$("#home #slideshow_btn_text_4").removeClass('btn_active');
			$("#home #slideshow_btn_text_4").addClass('btn_off');

			jQuery($slides[0]).show();
			jQuery($slides[1]).hide();
			jQuery($slides[2]).hide();
			jQuery($slides[3]).hide();
			jQuery($slides[0]).attr("z-index","1");
			jQuery($slides[1]).attr("z-index","0");
			jQuery($slides[2]).attr("z-index","0");
			jQuery($slides[3]).attr("z-index","0");	

			options.current = 0;
			options.counter = 2;
			time_out = setTimeout(run, options.selected_wait);
				
		});
		$("#slideshow_btn_text_2").bind("click", function() { 
				
			clearTimeout(time_out);
			time_out = null;

			$("#home #slideshow_btn_text_1").removeClass('btn_active');
			$("#home #slideshow_btn_text_1").addClass('btn_off');
			$("#home #slideshow_btn_text_2").removeClass('btn_off');
			$("#home #slideshow_btn_text_2").addClass('btn_active');
			$("#home #slideshow_btn_text_3").removeClass('btn_active');
			$("#home #slideshow_btn_text_3").addClass('btn_off');
			$("#home #slideshow_btn_text_4").removeClass('btn_active');
			$("#home #slideshow_btn_text_4").addClass('btn_off');

			jQuery($slides[0]).hide();
			jQuery($slides[1]).show();
			jQuery($slides[2]).hide();
			jQuery($slides[3]).hide();
			jQuery($slides[0]).attr("z-index","0");
			jQuery($slides[1]).attr("z-index","1");
			jQuery($slides[2]).attr("z-index","0");
			jQuery($slides[3]).attr("z-index","0");	

			options.current = 1;
			options.counter = 3;
			time_out = setTimeout(run, options.selected_wait);
				
		});
		$("#slideshow_btn_text_3").bind("click", function() { 
				
			clearTimeout(time_out);
			time_out = null;

			$("#home #slideshow_btn_text_1").removeClass('btn_active');
			$("#home #slideshow_btn_text_1").addClass('btn_off');
			$("#home #slideshow_btn_text_2").removeClass('btn_active');
			$("#home #slideshow_btn_text_2").addClass('btn_off');
			$("#home #slideshow_btn_text_3").removeClass('btn_off');
			$("#home #slideshow_btn_text_3").addClass('btn_active');			
			$("#home #slideshow_btn_text_4").removeClass('btn_active');
			$("#home #slideshow_btn_text_4").addClass('btn_off');

			jQuery($slides[0]).hide();
			jQuery($slides[1]).hide();
			jQuery($slides[2]).show();
			jQuery($slides[3]).hide();
			jQuery($slides[0]).attr("z-index","0");
			jQuery($slides[1]).attr("z-index","0");
			jQuery($slides[2]).attr("z-index","1");
			jQuery($slides[3]).attr("z-index","0");	

			options.current = 2;
			options.counter = 4;
			time_out = setTimeout(run, options.selected_wait);
				
		});
		$("#slideshow_btn_text_4").bind("click", function() { 
				
			clearTimeout(time_out);
			time_out = null;

			$("#home #slideshow_btn_text_1").removeClass('btn_active');
			$("#home #slideshow_btn_text_1").addClass('btn_off');
			$("#home #slideshow_btn_text_2").removeClass('btn_active');
			$("#home #slideshow_btn_text_2").addClass('btn_off');
			$("#home #slideshow_btn_text_3").removeClass('btn_active');
			$("#home #slideshow_btn_text_3").addClass('btn_off');
			$("#home #slideshow_btn_text_4").removeClass('btn_off');
			$("#home #slideshow_btn_text_4").addClass('btn_active');

			jQuery($slides[0]).hide();
			jQuery($slides[1]).hide();
			jQuery($slides[2]).hide();
			jQuery($slides[3]).show();
			jQuery($slides[0]).attr("z-index","0");
			jQuery($slides[1]).attr("z-index","0");
			jQuery($slides[2]).attr("z-index","0");
			jQuery($slides[3]).attr("z-index","1");	

			options.current = 3;
			options.counter = 5;
			time_out = setTimeout(run, options.selected_wait);
				
		});

	
		time_out = setTimeout(run, 0);
	});
}


