/* _js/structure.js */


// - run these functions when ready
$(document).ready(function() {
	
	// -- toggle javascript-only / no-javascript objects
	$('.js_only').show();
	$('.no_js').hide();
	
	// -- initialize the main menu
	mainMenuInit();	
	
	
	// -- intialize expandable lists
	elInit();

	
	// -- initialize testimonials
	testimonialsInit();
});



// - main menu functionality


// -- object to store vars
var v_mm = new Object();


// -- vars (config)
v_mm.tier2 = { // tier 2
	"speed" : 250, // (miliseconds) speed of transitions
	"wait" : 350 // (miliseconds) to wait for tier2 to close
};


// -- initialize the main menu
function mainMenuInit() {
	
	// -- loop each teir1 submenu parent li
	$('ul#mm_tier1 > li.submenu_parent').each(function(index) {
		
		index++;
		v_mm.tier2['timeout_'+index] = 0; // set a timeout interval for each tier2 nav
		
		$(this)
		
			// --- set hover handler
			.hover(function() {
				clearTimeout(v_mm.tier2['timeout_'+$(this).attr('rel')]);
				if($(this).hasClass('active')) return;						
				$(this)
					.addClass('active')
					.find('ul:first')
						.slideDown(v_mm.tier2.speed);
			}, function() {
				var t_which = $(this).attr('rel');
				v_mm.tier2['timeout_'+t_which] = setTimeout(function() {
					mainMenuTier2Hide(t_which);
				}, v_mm.tier2.wait);					
			})
		
			// --- add a rel for reference
			.attr('rel', (index+1)) 
			.find('div:first').width($(this).find('a:first').outerWidth())
				.parent()
			// --- determine optimal width for ul. we need to unhide it to calculate...	
			.find('ul:first').each(function() {
					
					$(this)
						.css('left', -50000)
						.removeClass('hidden');
					
					var t_width = $(this).outerWidth();
					var t_max_width = parseInt($(this).css('max-width'));
					
					if(t_width < t_max_width) {
						$(this).width(t_width);
					} else {
						$(this).width(t_max_width);
					}
					
					$(this)
						.hide()
						.css('left', 0);
				});
	});
}

// -- hides tier2 menu items
function mainMenuTier2Hide(which) {
	$('ul#mm_tier1 > li.submenu_parent[rel='+which+'] > ul:first')
		.slideUp(v_mm.tier2.speed, function() {
			$(this).parent()
				.removeClass('active');					
		});
}



// - expandable list functionality

var el_animation_speed = 500; // (miliseconds) to transition toggling extended info

function elInit() {
	$('.expandable_list_right').each(function() {
		$(this).find('ul.expanded:first')
			.hide();
		
		// -- hover state
		$('.expandable_list_right').hover(function() {
			$(this).addClass('expandable_list_right_active');				
		}, function() {
			if(!$(this).hasClass('is_expanded')) {
				$(this).removeClass('expandable_list_right_active');
			}
		});
		
		$(this).find('.read_more')
			.removeClass('hidden')
			.click(function() {
				$(this)
					.addClass('hidden')
					.parents('.expandable_list_right:first')
						.addClass('is_expanded')
						.find('ul.expanded')
							.slideDown(el_animation_speed)
						.parent().find('.close')
							.removeClass('hidden');
			});
			
		$(this).find('.close')
			.click(function() {
				$(this)
					.addClass('hidden')
					.parents('.expandable_list_right:first')
						.removeClass('is_expanded')
						.find('ul.expanded')
							.slideUp(el_animation_speed)
						.parent().find('.read_more')
							.removeClass('hidden');
			});	
	});
}



// - testimonials functionality


// -- object to store vars
var v_tstmnl = new Object();

// -- vars (config)
v_tstmnl.speed = 1000; // (miliseconds) speed of transitions
v_tstmnl.delay = 7500; // (miliseconds) to delay between cycles

// -- vars (private)
v_tstmnl.total = 0; // how many testimonials do we have
v_tstmnl.current = 0; // which item are we currently viewing?
v_tstmnl.target = 0; // which item are we viewing next?
v_tstmnl.interval = 0; // reference to the interval handler



// -- initialize testimonials
function testimonialsInit() {
	
	$('div#testimonials')
		.randomize('ul', 'li') // randomize the elements' order
		.find('ul:first')
			.addClass('cycling'); // apply "cycling" class to ul
		
	// --- loop each li	
	$('div#testimonials > ul > li').each(function(index) {
		index++;
		v_tstmnl.total++;
		$(this)
			.attr('rel', index)
			.css('opacity', 0)
			.hide();
	});
	
	testimonialsCycle();
	testimonialsSetCycle();
}


// -- cycle testimonials
function testimonialsCycle() {
	
	
	v_tstmnl.target = (v_tstmnl.current == v_tstmnl.total) ? 1 : v_tstmnl.current+1;
	
	// --- fade out current
	$('div#testimonials > ul > li[rel='+v_tstmnl.current+']')
		.fadeTo(v_tstmnl.speed, 0, function() {
			$(this).hide();
		});
		
	// --- fade in target	
	$('div#testimonials > ul > li[rel='+ v_tstmnl.target+']')
		.show()		
		.fadeTo(v_tstmnl.speed, 1, function(){
			if($.browser.msie && $(this).css('filter') && $(this).css('opacity')==1)	this.style.removeAttribute('filter')	
		});
		
	v_tstmnl.current = v_tstmnl.target;
	
}


// -- set inertval for cycling testimonials
function testimonialsSetCycle() {
	v_tstmnl.interval = setInterval('testimonialsCycle()', v_tstmnl.delay);
}
