/* 	general.js - Contains miscellaneuos js functions.
------------------------------------------------------------------------*/



function fadeIn(id, old_id ){
	
	$(old_id).fadeOut();

	// Add a delay to the fade in (looks smoother)	
	setTimeout(function(){ 
		$(id).fadeIn(); 
	}, 200);
}


//DOM ready
$(document).ready(function(){
	
	// check if we are on one of the pages housing a component viewer
	if ($('#viewer-container')) {
		
		// Hide components on page load
		$('.highlight .highlight-image').hide();
		$('.sub-cat-items').hide();
		$('.product-links').hide();
		$('.viewer').hide();

		// Show home panel as default
		$('#home_panel').show();

		// Open / Close Sub Navs

		$(".sub-cat").click(function(){

			if ($(this).hasClass("open")) {
				$(this).parent().children(("ul")).hide();
				$(this).removeClass("open");
			} else {
				$(this).parent().children(("ul")).show();
				$(this).addClass("open");
			}
			return false;
		});


		// Highlight component on hover
		$('.highlight').hover(
			function() {
					
				// fade in
								
				if ($(this).children(("a")).hasClass('off')) {
						//do nothing if 'on' class is present
				} else {
						$('.highlight-image', this).fadeIn();
				}			
					
			},
			function() {
					
				// fade out
					
				if ($(this).children(("a")).hasClass('on')) {
					//do nothing if 'on' class is present
				} else {
					$('.highlight-image', this).fadeOut();
				}
			}
		);

		//Fade in elments based on href
		$(".fader").click(function () {			
			var id = $(this).attr("href");
	
			fadeIn(id , $('.product-links'));
		
			//remove all instances of 'on' class
			$('.on .highlight-image').fadeOut();
			$('a').removeClass('on');
		
			// add 'on' class here to keep component highlighted	
			$(this).addClass("on");
			return false;	
		});
		
	};
	

});


