/*
	Quetools Renderer Custom Script
	For Quetools Renderer
	Copyright 2010 Decomplexity
*/

/*
	Executed on DOM Ready	
*/
var domLoaded = function(){

	/*
		Set up Menus
	*/
	var arrow;
	var closeDelay = 400;
	
	$("ul.sf-menu").supersubs({ 
		minWidth:   12,
		maxWidth:   27,
		extraWidth: 1
	}).superfish({
		animation:  {opacity:'show',height:'show'},
		speed:		300,
		easing:		'swing',
		delay:		closeDelay,
		onBeforeShow: function(){ 
			
			arrow = ($(this).prev().after('<img class="arrow" src="'+serverroot+'/images/subnav_arrow.png" alt="" />'));
			$(this).prev().animate({opacity:'show'},300);
			$(this).addClass('open');
			
			
		},
		onHide: function(){ 

			if($(this).hasClass('open')){
				$(this).prev('.arrow').remove();
				$(this).removeClass('open');
				arrow = null;
			}

		}
	});
	
	$('.dAnimalEntryParents').each(function(i, e){ 
	
		var text = $(e).html();
		text = text.replace('Dam:', '<br />Dam:');
		$(e).html(text);
		
	});
	
	$('.dAnimalListEntryColour').each(function(i, e){ 
	
		var text = $(e).html();
		text = text.replace('Horn: ', '');
		$(e).html(text);
		
	});
	
	$('.dAnimalTab').hide();
	$('.dAnimalTabBar a[href="#"]').click(function(e){
		
		e.preventDefault();
		
	});
	resetTabs('.dAnimalTab a:first-child','#pedigreeContainer');
	
	
	$(".dAnimalListEntryObject a").fancybox({
			onComplete: function(e){
				
				// Process thumbnails, make em open in big foto
				$(".aAnimalDetailImage").each(function(i, e){
	
					$(e).click(function(el){
	
						el.preventDefault();
						
						if($(this).children('img')[0]){ 
						
							$('#dAnimalMaximisedPhoto').attr('src', $(this).children('img')[0].src.replace('maxx=120', 'maxx=400').replace('maxy=120', 'maxy=400'));
						
							// Add Title
							var title = $(this).attr('title').split("::");
							if(title) $("#dAnimalDescription").html("<strong>"+title[0]+"</strong> "+title[1]);
							
						}
					
					});
				
				});
				
				// Fix EBVs
				
				$('.ebvVal').each(function(i, e){
				
					if($(e).html() != ""){
					
						if($(e).html()[0] != "-") $(e).html("+"+$(e).html());
					
					}
				
				});
				
				$('.accuracy').each(function(i, e){
				
					if($(e).html() != ""){
					
						$(e).html($(e).html()+"%");
					
					}
				
				});
				
				if($('#ebvContainer').html() == ""){
				
					$('#ebvlnk').hide();
				
				}
				
				if($('#notesContainer').html() == ""){
				
					$('#noteslnk').hide();
				
				} 
				
			}
		});
	
	
	
	

	// Autosizes the mainmenu items to fill their containing UL element.
	// A width must be set on the top level <ul> element of the menu
	//elastoNav('#dMenu ul');
	
	// Calendar Function
	highlightSelectedDay();
	
	/* Alternate Table Row Styling */
	$('.altRows tr:even').addClass('altColor');
	
}


/*
	Executed on Window Load
*/
var contentLoaded = function(){

	//cleanup(); // Clear extra whitespace from content
	
	
	hideOverlay();
	
	/*
		Set up any UI Elements
	*/
	
	if($('.date')){
		
		$('.date').datepicker({
			dateFormat: "dd/mm/yy"
		});
		
		$('.date').removeClass('date');
		$('.date').removeAttr('date');
		
	}
	
	if($('.dRadioSet')) $('.dRadioSet').buttonset();
	if($('.dCheckSet')) $('.dCheckSet').buttonset();
	if($('.button, .termsbutton')) $('.button, .termsbutton').button();
	
	//$('#dScroller').nivoSlider();
	
	//$('.nivo-controlNav').css('width', $('.nivo-controlNav').width());
	
	// Autosizes the mainmenu items to fill their containing UL element
	// Run for a second time to check after images have loaded
	// A width must be set on the top level <ul> element of the menu
	elastoNav('#dMenu ul');

}

/*
	Tick Loading Animation
*/
var loadingTick = function(progress, canvas){
	
	// Draw loading animation here
	
}

var resetTabs = function(a,e){
	
	$('.dAnimalTabBar a').removeClass('active');
	$('.dAnimalTab').hide();
	$(a).addClass('active');
	$(e).toggle()

}
