// JavaScript Document
// jquery code for support area
// used for licensing and download FAQ (11.1 release)

$(document).ready(function(){    

    // tooltip hover
	$('a.info').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		showBody: " - ",
		fade: 250
	});
    $("input#srch-field").focus(function()
	 { 
	   $(this).attr("value","").css('color','black');
	 });
 
	// include for support navigation section (devices)
	var divIncludeDevices = 'div.includeFileDevices';
	var includeDevicesUrl = $(divIncludeDevices).text();
	$(divIncludeDevices).load(includeDevicesUrl, function()
		{ //callback function after page is loaded
           

		    $(divIncludeDevices).show();   
			var $content = $('#support-content');	
			var $nav = $('#support-nav');	
            var commonLinks = $('#common-links-wrap').html();	
  			// clone content into #support-content area
			$('.cmenu1', $nav).clone(true).appendTo($content);
			$('#loading').fadeOut('normal');
			// replace the href value with anchor tags in support-nav section
			$('.catlink-cmenu2', $nav).each( function(index) {
				$(this).attr('href' , '#catlink-cmenu2-'+index);
			});
            
            var cmenu1HTML = $('.catlink-cmenu1').html();
			$('.catlink-cmenu1', $nav).each (function(index) {
				   var cmenu1HTML = $(this).html();
                   $(this).remove();
                   $('.topcat1:eq('+index+')', $nav).prepend('<h3>' + cmenu1HTML + '</h3>');
			});
			// remove empty .cmenu3 and .alert leaf level items in support-nav section
	        $('.cmenu3', $nav).remove();
			$('.alert:empty', $content).remove();
			$('.cmenu1 a[@class=catlink-cmenu1]', $content).remove();
			// add anchor tags to all .cmenu1 and .cmenu2 items in support-content section
			$('.catlink-cmenu2', $content).each( function(index) {
				$(this).attr('id','catlink-cmenu2-'+index).append('<span class=\'seeAll\'>(see all documentation)</span>').before('<div class=\'prop\'></div>');
            
			});
            // appends Design Resources links to FPGA Device Families
            $('.topcat1:eq(0) .topcat2', $content).append(commonLinks);
            // appends Design Resources links to CPLD Device Families
            $('.topcat1:eq(1) .topcat2', $content).append(commonLinks);
			// find all "alert" docs and move them to div
			$('.cmenu2', $content).each( function(index) {					
                    $(this).append('<div class="alert-wrap"></div>');
					$('.alert-wrap:eq('+index+')').append($(this).find('.alert p'));						
			});
            // remove all empty alert DIVs
			$('.alert-wrap:empty', $content).remove();
            // prepend remaining alert DIVs with <h4> 
			$('.alert-wrap', $content).prepend('<h4 class="alertTitle">Document Alert&nbsp;&frasl;&nbsp;Changes</h4>');
            // add Documentation title to sections that contain more than 1 child (checking 
            // if .cmenu divs exists
			$('.catlink-cmenu2', $content).each(function() {
                var cmenu3Count = $(this).parent('.topcat2').children().length;
				if (cmenu3Count > 1) {
                	$(this).after('<h3 class="nounderline">Documentation</h3>');
				} else {
                    $(this).parent('.topcat2').css('height','60px');
				}
			});
          $('.topcat1', $nav).each(function() {
			  // var cmenu2Count = $(this).children('cmenu2').length;
				//  alert('cmenu2Countx = ' + cmenu2Count);
				  if ($(this).children('.cmenu2').length == 0) {
                      $(this).remove();
				  }
			});
            // hide all content in support-content DIV
            $('.cmenu2', $content).hide();         
            // show Virtex-6 content by default
            $('.cmenu2 a[@href=#catlink-cmenu2-0]').parents('div.cmenu2').addClass('hiliteSN');
            $('a[@id=catlink-cmenu2-0]', $content).parents('div.cmenu2').show();
            // when user clicks link in nav section, the mathing links appears
			$('.catlink-cmenu2', $nav).click(function(){
                     $('.cmenu2', $content).hide();
                     var $navID = $(this).attr('href'); 
                   
					 var offset = $(this).offset({border:true, padding:true, relativeTo:$nav});
                     var cmenu2Height = $('a'+ $navID).parents('div.cmenu2').height();
                   
					 $content.css({'margin-top':offset.top-455,'height' : cmenu2Height});
			         $('a'+ $navID).parents('div.cmenu2').css('filter','').fadeIn('slow');
                 
                     $($nav).find('div[@class*=hiliteSN]').removeClass('hiliteSN');
                     $(this).parents('div.cmenu2').addClass('hiliteSN');					
                     return false;

			});
            $('.seeAll').hover(
            function() {
                  $(this).css('color','#ee3424');
			}, 
            function() {
				  $(this).css('color','#888');
			});
		}); // end callback function for IncludeDevicesNav
	});