/* GLOBAL .JS */

var Site = {

	ie6: $.browser.msie && parseInt($.browser.version) === 6 && typeof window['XMLHttpRequest'] !== "object",

	/* 
	 * Setup Cufon font replacement
	 */
	setupCufon: function(){
		$('ul.leftnav h4.header span').addClass('numeral');
		Cufon('h1', { fontFamily: 'DejaVu' });
		Cufon('h2', { fontFamily: 'DejaVu' });
		Cufon('h3', { fontFamily: 'DejaVu' });
		Cufon('.staff p', { fontFamily: 'DejaVu' });
		Cufon('h4', { fontFamily: 'Edelsans' });
		Cufon('h5', { fontFamily: 'Edelsans' });
		Cufon('.lower-third', { fontFamily: 'Edelsans' });
		//Cufon('.leftnav li ul li a', { fontFamily: 'DejaVu' });
		//Cufon('.leftnav li ul li span', { fontFamily: 'DejaVu' });
	},


	/* 
	 * Setup AJAX functionality for Subscribe Widget
	 */
	setupSubscribeWidget: function(){
		// Add Ajax functionality to subscribe form
		$('#subscribeForm').submit(function(event) {
			event.preventDefault(); 
			var $form = $(this);
			$('#pre-submit').fadeOut(500,function(){
				var email = $form.find('input[name=email]').val();
				var url = $form.attr('action');
				$.post( url, { 'email' : email, 'action' : 'subscribe' },
				function(data){
					$('#subscribeResult p').empty().append(data);
					$('#subscribeResult').hide();
					$('#subscribeResult').fadeIn(500);
					$('#resetSubscribeLink').click(function(event){
						event.preventDefault();
						$('#subscribeResult').fadeOut(500, function(){
							$('#pre-submit').fadeIn(500);
						});

					});
				});
			});
		});
	},
	
	setupLeftNav: function(){
		
		$('.publication .leftnav li h4 a:not(.non-opening)').click(function(e){
			e.preventDefault();
			if(Site.ie6) return;
			// Close all open menus
			$('.leftnav li.open').each(function(){
				$(this).find('ul').slideUp(300, function(){
					$(this).closest('li').removeClass('open');
				});
			});

			// Open the menu clicked, unless we just closed it
			if(!$(this).closest('li').hasClass('open')){
				$(this).closest('li').addClass('open');
				$(this).closest('li').find('ul').slideDown(300);
			}
		});
		
		if(Site.ie6) return;
			
		// Close all open menus
		$('.leftnav li.open').each(function(){
			$(this).find('ul').hide();
			$(this).closest('li').removeClass('open');
		});

		// AUTO-HIGHLIGHT NAV ITEM
		var page = window.location.href.split("/"); // after the slash at the end of the domain name
		page = page[page.length-1].split("?")[0];
		page = page.split("#")[0];
		if (page == "") { page = "index.php"; }
		var ref = $('.leftnav');
		$('.leftnav li').each(function(){
			var thisLI = this;
			$(this).find('a[href|="'+page+'"]').each(function(){ //ul li a[href|="'+page+'"]'
				$(this).addClass('on');
				$(thisLI).addClass('open');
				$(thisLI).find('ul').show();
			});
		});
		
		// ADD HOVER STATE
		$('.leftnav li h4:not(.header)').hover(
			function(){
				$(this).closest('li').addClass('rollover');
			},
			function(){
				$(this).closest('li').removeClass('rollover');
			}
		); 

	},
	
	setupHomePageHero: function(){ // Make lower third clickable
		$('#hero ul li').each(function(i){
			var href = $(this).find('a').attr('href');
			$(this).find('.lower-third').click(function(){location.href = href;});
			
			// create menu element
			var a = document.createElement('a');
				a.setAttribute('href','#'+i);
			var li = document.createElement('li');
				li.appendChild(a);
			$('ul#hero-controller').append(li);
		});
		$('ul#hero-controller li a').click(function(e){
			e.preventDefault();
			id = $(this).attr('href');
			id = id.substring(1);
			HeroMarquee.animator(id);
		})
//		HeroMarquee.animatorSetup();
	},
	
	setupIEfixes: function(){
		// remove focus
		if(!$.browser.msie) return;
		if(document.all){
			for(var i in document.links){
				document.links[i].onfocus = document.links[i].blur;
			}
		}
		if(Site.ie6){ 
			//$(".arrow").pngfix(); //no need to fix, converted to gif
			
			// fix bottom-nav centering issue
			var w = 0;
			$('#bottom-nav li').each(function(){
				w += $(this).outerWidth();
			});
			$('#bottom-nav').width(w+5);
		}
		
	},
	setupHeaderNavHighlight: function(){
		$('#topnav').ready(function() {
			var page = window.location.href.split("/")[3]; // after the slash at the end of the domain name
			var page = page.split("?")[0];
			if (page == "") { page = "index.php"; }
			$('#topnav').children().find('a[href|="/'+page+'"]').addClass('on'); 
		});		
	},

	setupHomeNewsScroller: function(){
		if(!$('ul.news-items')) return;
		var run;
		var pause = 8000;			// Pause between each animation
		var duration = 1500; 		// Speed of animation
		var page_predelay = 0; 		// Delay before starting (adds to pause)
		function animator(){
			var itemHeight = $('ul.news-items li:first').outerHeight();
			$('ul.news-items').animate({top : itemHeight * (-1)}, {
				'duration' : duration,
				'easing'   : 'easeInOutCubic',
				'complete' : function(){
					$('ul.news-items').css('top',0);				
					$('ul.news-items li:last').after($('ul.news-items li:first'));
				}
			});
		}
		function animatorStart(){
			run = setInterval(animator, pause);
		}
		function animatorStop(){
			clearInterval(run);
		}
		$('ul.news-items').hover(
			function(){
				animatorStop();
			},
			function(){
				animatorStart();
			}
		);
		$(document).delay(page_predelay).queue(function(){animatorStart();});
	},
	
	
	/* 
	 * Setup TCR global elements
	 */
	setup: function(){
		
		Site.setupCufon();
		Site.setupSubscribeWidget();
		Site.setupLeftNav();
		Site.setupHomePageHero();
		HeroMarquee.animatorSetup();
		Site.setupHeaderNavHighlight();
		Site.setupIEfixes();
		Site.setupHomeNewsScroller();
	}
	
}




var HeroMarquee = { 

	run : false,
	pause : 7000,			// Pause between each animation
	duration : 1500,		// Speed of animation
	page_predelay : 0,		// Delay before starting (adds to pause)
	current_item_index : 0,
	next_item_index : 0, 
	animatorSetup: function(){
		this.containerElement = $("#hero");
		this.theItems = $('#hero .items li');
		this.numItems = $('#hero .items li').length;
        
//		console.log('items: ' + this.numItems);
		$(this.containerElement).hover(function(){HeroMarquee.animatorStop();}, function(){HeroMarquee.animatorStart();});
		HeroMarquee.updateButton(0);
		HeroMarquee.animatorStart();
	},
	animator: function(goID){
		HeroMarquee.next_item_index = (HeroMarquee.current_item_index >= HeroMarquee.numItems-1) ? 0 : HeroMarquee.current_item_index+1;
		if(goID != null){ HeroMarquee.next_item_index = parseInt(goID);}
//		console.log(HeroMarquee.current_item_index, HeroMarquee.next_item_index);
//		console.log(HeroMarquee.current_item_index, HeroMarquee.next_item_index);
		$(HeroMarquee.theItems[HeroMarquee.current_item_index]).fadeOut(HeroMarquee.duration);
		$(HeroMarquee.theItems[HeroMarquee.next_item_index]).fadeIn(HeroMarquee.duration, function(){});
		HeroMarquee.updateButton(HeroMarquee.next_item_index);
		HeroMarquee.current_item_index = HeroMarquee.next_item_index;
	},
	animatorStart: function(){
		HeroMarquee.run = setInterval(function(){HeroMarquee.animator();}, HeroMarquee.pause);
	},
	animatorStop: function(){
		clearInterval(HeroMarquee.run);
	},
	updateButton: function(id){
		$('ul#hero-controller li a').removeClass('on');
		$('ul#hero-controller li a:eq(' + id + ')').addClass('on');
	}
}





/* START applesearch object */
		
if (!applesearch)	var applesearch = {};

applesearch.init = function ()
{
	// add applesearch css for non-safari, dom-capable browsers
	if ( navigator.userAgent.toLowerCase().indexOf('safari') < 0  && document.getElementById )
	{
		this.clearBtn = false;
		
		// add style sheet if not safari
		var dummy = document.getElementById("dummy_css");
		if (dummy)	dummy.href = "/css/applesearch.css";
	}

}

// called when on user input - toggles clear fld btn
applesearch.onChange = function (fldID, btnID)
{
	// check whether to show delete button
	var fld = document.getElementById( fldID );
	var btn = document.getElementById( btnID );
	if (fld.value.length > 0 && !this.clearBtn)
	{
		btn.style.background = "white url('/img/srch_r_f2.gif') no-repeat top left";
		btn.fldID = fldID; // btn remembers it's field
		btn.onclick = this.clearBtnClick;
		this.clearBtn = true;
	} else if (fld.value.length == 0 && this.clearBtn)
	{
		btn.style.background = "white url('/img/srch_r.gif') no-repeat top left";
		btn.onclick = null;
		this.clearBtn = false;
	}
}


// clears field
applesearch.clearFld = function (fldID,btnID)
{
	var fld = document.getElementById( fldID );
	fld.value = "";
	this.onChange(fldID,btnID);
}

// called by btn.onclick event handler - calls clearFld for this button
applesearch.clearBtnClick = function ()
{
	applesearch.clearFld(this.fldID, this.id);
}

/* END applesearch object */

$('#search').ready(applesearch.init);

$('document').ready(Site.setup);


/*
 * Old Global Functions used somewhere?
 * 
 *
 * 
 */
function donothing() {} 
function gotosite(site) {            
	if (site != "") {                    
		self.location=site; 
	}
 }
function goBack(){
	history.go(-1);
}
function mailTo(whom,whom2){
	document.location = "mai" + "lto" + ":" + whom + whom2 + "@" + "cortlandreview" + "." + "com";
}

