var Vovici={	
	
	/**
	 * Initializes the Vovici application
	 * @param object jQuery optional
	 * @return nothing
	**/
   
   _onAppStart:function($){
	
		/**
		 * We need a global variable defined to store the first nav content
		**/

		$.extend({nodeStore:null});	
	
		/**
		 * Assign click handlers and mouse effects to left nav <li> tags
		 * We also re-address the "title" attribute for the dynamic nav as nested list items
		 * will grab the title of the parent due to proagation if not explicity 
		 * defined.
		**/

		var inavlist=$('#internal-nav>ul>li');

		inavlist.each(function(){
			$(this)
			.click(Vovici._onNavClick)
			.hover(	function(){$(this).css({cursor:document.all ? 'hand' : 'pointer'})},function(){return true})
			.attr({title:$(this).get(0).firstChild.nodeValue.replace(/^\s+|\s+$/,'')})     
		});	   
		
		$('#help-callout>ul>li').bind('click',Vovici._onNavActivate);
		
		/**
		 * Add pseudo classed to <li> list elements for non CSS 3 browsers
		**/    
		
		$('ul>li:first-child').each(function(){
			$(this).addClass('first-child')
		}); 
		
		$('ul>li:last-child').each(function(){
			$(this).addClass('last-child')
		}); 
		
		/**
		 * Set title on blank title attributes for a tags
		**/		
		$('a').each(function(){
			var a=$(this);
			if(a.attr('title')=='' || typeof a.attr('title')=='undefined'){
				a.attr({'title':a.text()});				
			}			
		});
		
		Vovici._enableTabNav();
		

	},	      


	/**
	 * Executes action for <li> nav items
	 * @param object HTMLElement implied event source target
	 * @return nothing
	**/

   _onNavClick:function(){
	   
		var mainnav=$('#internal-nav>ul>li:eq(0)>ul');

		$('#internal-nav>ul>li').addClass('inactive');
		
		if($.nodeStore==null){$.nodeStore=mainnav.get(0).cloneNode(true);           
		}

		/**
		 * If the current <li> is the same as the first one, use the nodeStore
		 * value.  Otherwise, use the <ul> from the HTML
		**/

		var navitems=$('#internal-nav>ul>li:eq(0)').get(0)==this ? $($.nodeStore).find('li') : $(this).find('ul>li');

		$(this).removeClass('inactive');

		/**
		 * Clear the visible list and append the new values
		**/

		mainnav.empty();

		navitems.each(function(){	  
			var clone=$(this).get(0).cloneNode(true);
			$(clone).css({opacity:.1});                   
			mainnav.append($(clone));	                     
		});
		
		mainnav.find('li').animate({opacity:.99});
	},
	_onNavActivate:function(event){		
		
		var objLi	= $(this);		
		var objUl	= objLi.parent();		
		var hgt		= objUl.height();		
		var offset	= objLi.get(0).offsetTop;		
		var _objLi	= objLi.clone(true);

		_objLi.css({
			position:'absolute',
			top: offset +'px'
		})
		.unbind('click',Vovici._onNavActivate); // Remove activate event from clone
		
		/**
		 * Reset the styles for the cloned element
		**/		
		_objLi.find('a').css({
			opacity:.90,
			backgroundColor:'#000',
			fontWeight:'bold',
			color:'#FFF'
		});
		
		/**
		 * "Gently" hide current <li> elements
		**/		
		objUl.find('li').animate({opacity:0},750);
		
		objLi.before(_objLi); // Append our clone before the current element
		
		/**
		 * Animate the position transition of the cloned element
		***/		

		_objLi.animate({top:'0px'},750	,function(){
			
			var ohgt = _objLi.get(0).offsetHeight;
			
			/**
			 * We are creating a <div> tag with the content for the
			 * element's space
			**/		
			var objDiv=$('<div>').css({
				position : 'absolute',
				left:'2px',
				top: ohgt+'px',
				color:'#FFF',
				overflow:'auto',
				padding:'10px',
				font:'normal 13px/17px Arial,sans-serif',
				height: (hgt - ohgt-20) +'px',
				backgroundColor:'#000',
				width:(_objLi.width()-23)+'px',
				opacity:.1
			}).html(_objLi.find('div.help-content').html());		
			

			_objLi.after(objDiv);	
			
			objDiv.animate({opacity:.75},250);	
			
			var img=$('<img>').attr({
				src:'images/backtolist.gif',
				title:'Back to list'
			})
			.css({
				'float':'right',
				width:'24px',
				opacity:.99,
				height:'24px',
				border:'none',
				cursor:'pointer'
			}).click(function(){
				clearTimeout(objDiv.get(0).navTimer);
				Vovici._onNavWait.apply(this,[_objLi,objDiv]);				
			});
			
			var a = _objLi.find('a')
			
			var txt = a.text();
			
			a.empty().append(img,txt);
			
			objDiv.get(0).navTimer=setTimeout(function(){
				Vovici._onNavWait(_objLi,objDiv)
			}
			,10000);	
			
		});	
		
	},
	_onNavWait:function(objClone,objDiv){		
		
		objClone.animate({opacity:0.1},500,function(){$(this).remove()});
		
		objDiv.animate({opacity:0.1},500,function(){$(this).remove()});	
		
		$('#help-callout>ul>li').animate({opacity:.99},250);		
	},
	_enableTabNav:function(){
		
		var tabs = $('.tab-style>ul.tabs>li');
		var items = $('.tab-style').find('div.content-item');
		
		for(i=0;i< tabs.length; i++){
			var me = $(tabs[i]);
			var cnt = me.parent().parent();
			var ref = cnt.find('div.content-item:nth-child('+(i+1)+')');
			
			if(ref){	
				me.find('a').bind('click', {position:(i+1)},Vovici._getTabContent);		
				me.bind('click', {position:(i+1)},Vovici._getTabContent);				
			}					
		}	
	},
	_getTabContent:function(e){		
		
		var el = $(e.target);
		if(el.get(0).tagName.toLowerCase() == 'a'){
			el = el.parent();
		}
		var par = el.parent().parent();
		var coll = par.find('div.content-item');
		var cnt = par.find('div.content-item:nth-child('+e.data.position+')');
		
		$('.tab-style>ul.tabs>li').removeClass('active');

		if(cnt){
			for(i=0;i<coll.length;i++){
				$(coll[i]).hide();				
			}	
			
			$(cnt).show();
			el.addClass('active');		
		}		
		e.stopPropagation();
		e.preventDefault();
		return false;
	}
}

// Rollover Call-to-actions Menu

		// Preload all rollovers
	$(document).ready(function() {
		
		// Preload all rollovers
		$("#callToActions img").each(function() {
			// Set the original src
			rollsrc = $(this).attr("src");
			rollON = rollsrc.replace(/.jpg$/ig,"_over.html");
			$("<img>").attr("src", rollON);
		});
		
		// Navigation rollovers
		$("#callToActions a").mouseover(function(){
			imgsrc = $(this).children("img").attr("src");
			matches = imgsrc.match(/_over/);
			
			// don't do the rollover if state is already ON
			if (!matches) {
			imgsrcON = imgsrc.replace(/.jpg$/ig,"_over.html"); // strip off extension
			$(this).children("img").attr("src", imgsrcON);
			}
			
		});
		$("#callToActions a").mouseout(function(){
			$(this).children("img").attr("src", imgsrc);
		});
		
	
	});


// Make Top Nav Menu works in IE 5.5 
	

navHover = function(tnmenu) {
	var lis = document.getElementById("topnavmenu").getElementsByTagName("LI");
	for (var i=0; i<lis.length; i++) {
		lis[i].onmouseover=function() {
			this.className+=" iehover";
		}
		lis[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" iehover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", navHover);

