jQuery(document).ready(function(){
	
	
	// Video/Photo application
	$('.mediaResults .details').bgiframe();
	
	var indexToLoad = 0;
	if(document.location.hash!='') {
		var hash = location.hash;
		// is it a content anchor link?
		if(hash.substr(1,1) == 'c' && hash.substr(2) == parseInt(hash.substr(2))) {
			var el = $(location.hash);
			if(el.length) {
				var parentPanel = el.parents('.tab-panel').filter(':first');
				var indexToLoad = $('.tab-panel').index(parentPanel);
				scroll(0,0);
			}
		}
	} 


	jQuery('.tabsWrap').each(function() {
		el = $(this);
		el.tabs();
		el.tabs('select',indexToLoad);
	});
	

	/* wrap the primary sub navs in div strictly for presentational purposes - see style.css */
	jQuery('ul.nav ul').wrap('<div class="sub"><div class="sub-inner"></div></div>');

	/* superfish jquery */
	if((jQuery.browser.msie)&&(parseInt(jQuery.browser.version)<7)){
		jQuery("ul.nav > li").hover(function(){
			jQuery(this).addClass("sfhover")
		},function(){	
			jQuery(this).removeClass("sfhover")
		});
	}

	/* subscribe lightbox */
	$(".subscribePop").fancybox({
		'frameWidth': ($.browser.msie && $.browser.version.substr(0,1)<7) ? 410 : 365, 
		'frameHeight': ($.browser.msie && $.browser.version.substr(0,1)<7) ? 270 : 220,
		'hideOnOverlayClick' : false,
		'hideOnContentClick' : false
	});
	
	// Home page hero
	hero.init();
	
	// IE6 .png transparency fixes
	pngFixes.init()
	
	$('#search-input').click(function() {
		if(this.value=='Search'){this.value = '';}
	}).blur(function() {
		if(this.value==''){this.value = 'Search';}
	});
	
});

var pngFixes = {
	init : function() {
		// IE png fix
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			DD_belatedPNG.fix('.promo-overlay, a#donate img, .promo .tab, .promo #tabs li a');
			DD_belatedPNG.fix('.pngFix, .mediaResults .detailsTop, .mediaResults .detailsMid, .mediaResults .detailsBottom, .resultsList .controls, .resultsList .thumblink');
		}
	}
}

var hero = {
	autoAdvance: true,
	interval: 4000,
	showing: 0,
	currentCategory: 'protecting',
	rItems: new Array(),
	rControls: new Array(),
	catKeys: {},
	pause: false,
	count: 0,
	checkInit: function() {
		if($('#hero').length > 0) return true;
		return false;
	},
	init: function() {
		
		if(!this.checkInit()) return false;
		// opacity fix for FF2 Mac
		jQuery.each(jQuery.browser, function(i, val) {
		  if(i=="mozilla" && jQuery.browser.version.substr(0,3)=="1.8")
			$('ul#nav').css('opacity', 0.9999);	// fix for opacity bug in mac FF		
		});
		this.rItems = $('#rotator .rItem');
		
		// setup cat keys
		prevKey = false;
		this.rItems.each(function(i) {
			var newKey = $(this).attr('rel');
			if(newKey != prevKey) {
				hero.catKeys[newKey] = i;
			}
			prevKey = newKey;
		})

		$('#tab-supporting').hide();
		$('#tab-adventure').hide();
		
		// preload tabs
//		tab1 = new Image(461,134); 
//		tab1.src="fileadmin/templates/images/bg-home-tab-protect.png"; 
//		tab2 = new Image(461,134); 
//		tab2.src="fileadmin/templates/images/bg-home-tab-support.png"; 
//		tab3 = new Image(461,134); 
//		tab3.src="fileadmin/templates/images/bg-home-tab-adventure.png"; 
		

		this.rControls = $('#rotatorControls li');
		this.activeTab = 'protecting';
		
		this.count = this.rItems.size();
		this.setTriggers();
		this.currentCategory = false;
		setTimeout(function() {hero.change('advance',true)},hero.interval);
	},
	setActiveItem: function(pos) {
		this.rControls.each(function() {
			if($(this).hasClass('active')) $(this).removeClass('active');
		})
		$(this.rControls[pos]).addClass('active');
	},
	setTriggers: function() {
		this.rItems.each(function(index) {
			$(this).hover(function(){
				hero.pause = true;
			},function(){
				hero.pause = false;
			});
		});
	
		thumbKeys = new Array('protecting','supporting','adventure');
		for (var i in thumbKeys ) {
			var key = thumbKeys[i];
			triggerEl = $('#thumb-' + key);
			heroIndex = hero.catKeys[key];
			triggerEl.bind("click", {index: heroIndex}, function(e){
				heroIndex = e.data.index;
				if(heroIndex != hero.showing) {
					hero.change(heroIndex,false);
					this.autoAdvance = false;
					return false;
				} else {
					return false;
				}
			});	
		}
	},
	rBreak: function() {
		this.autoAdvance = false;
	},
	changeTab: function(tabKey) {
		var showTabId = '#tab-'+tabKey;
		var hideTabId = '#tab-'+this.activeTab;
		var showThumbId = '#thumb-'+tabKey;
		var hideThumbId = '#thumb-'+this.activeTab;
		
		
		$(hideTabId).slideUp(300,function() {
			$(showTabId).slideDown(300);
		});
		$(showThumbId).addClass('selected');
		$(hideThumbId).removeClass('selected');
		
		this.activeTab = tabKey;
	},
	change: function(action,isAuto) {
		if(!isNaN(parseInt(action))) {
			show = parseInt(action);
		} else {
			if(action == 'advance') {
				var show = this.showing + 1;
			} else if(action == 'retreat') {
				var show = this.showing - 1;
			}				
		}
		
		if(show < 0) show = this.count - 1;
		if(show == this.count) show = 0;
		if(this.pause == true && isAuto == true ) {
			// skip the advance...
		} else {
			$(this.rItems[this.showing]).fadeOut();
			$(this.rItems[show]).fadeIn();
			this.setActiveItem(show);
			this.showing = show;
		
			// handle tab changes
			if(this.currentCategory == false) this.currentCategory = 'protecting';
			var newCategory = $(this.rItems[show]).attr('rel');
			if(newCategory != this.currentCategory) this.changeTab(newCategory)
			this.currentCategory = newCategory;
			
			// pause for an interval after a click
			if(isAuto != true) {
				this.pause = true;
				setTimeout(function() {
					hero.pause = false
				},hero.interval);				
			}			
		}
		
		if(isAuto == true) {
			if(action == 'advance') {
				setTimeout(function() {
					hero.change(action,true)
				},hero.interval);				
			}
		} 
	}
};
