// JavaScript Document
// jquery code for support area

$(document).ready(function(){   
						   
	var ie = $.browser.msie; 
    var $tabs = $('#tabs > ul');
	$tabs.tabs({
//	    event: {event: 'change'},
//        selected: 3,
		ajaxOptions: { cache: false },
	    fx: { opacity: 'toggle' } //,
	//	cookie: { expires: 30 }
	}); // end $tabs.tabs
	// --------------------------------------------------------------------
//   
//   BBQ - BACK BUTTON AND QUERY
//   USES BEN ALMANS JQUERY.BBQ PLUGIN LOCATED IN UNIVERSAL.MIN.JS
//
// --------------------------------------------------------------------	

  // The "tab widgets" to handle.
//  var tabs = $('#tab'),
    
    // This selector will be reused when selecting actual tab widget A elements.
//    tab_a_selector = 'ul.ui-tabs-nav a';
  
  // Enable tabs on all tab widgets. The `event` property must be overridden so
  // that the tabs aren't changed on click, and any custom event name can be
  // specified. Note that if you define a callback for the 'select' event, it
  // will be executed for the selected tab whenever the hash changes.
 
//  tabs.tabs();
  
  // Define our own click handler for the tabs, overriding the default.
  // Used to push the tab and nav states to browser history
  
  // pushes paramater changes for each tab click
  // reads in selected nav link and pushes parameter changes
  $('#tabs li a').bind( "click", function() {
    var tabLinkID = $(this).attr('href').replace( /^#/, '' );
	var tabID = $(this).parent('li').attr('id');
    var state = {},
      
	// Get the id of this tab widget.
	id = 'tab',      
	// Get the index of this tab.
	idx = tabID,	
	// Set the id of the nav parameter
	nav = 'nav'
	// Get the id of the selected nav link
	navID = $('div#' + tabLinkID + ' a.hiliteSN').attr('id');
		
    // Set the state!
    state[ id ] = idx;
	state[ nav ] = navID;
    $.bbq.pushState( state );
  });
/*  
  $(window).bind('hashchange', function(){
    // Alerts every time the hash changes!
    alert( location.hash );
    var hash = location.hash;
  // Bind an event to window.onhashchange that, when the history state changes,
  // iterates over all tab widgets, changing the current tab as necessary.
 // $(window).bind( 'hashchange', function(e) {
 //   	  alert('in hashchange function >>>> ');
    // Iterate over all tab widgets.
   tabs.each(function(){
      
      // Get the index for this tab widget from the hash, based on the
      // appropriate id property. In jQuery 1.4, you should use e.getState()
      // instead of $.bbq.getState(). The second, 'true' argument coerces the
      // string value to a number.
     var idx = $.bbq.getState( this.id, true ) || 0;

      
      // Select the appropriate tab for this tab widget by triggering the custom
      // event specified in the .tabs() init above (you could keep track of what
      // tab each widget is on using .data, and only select a tab if it has
      // changed).
      $(this).find( tab_a_selector ).eq( idx ).triggerHandler( 'change' );
    });
  })
*/ 
  // Since the event is only triggered when the hash changes, we need to trigger
  // the event now, to handle the hash the page may have loaded with.
  $(window).trigger( 'hashchange' );
	
// --------------------------------------------------------------------
//   
//   FUNCTIONS DEFINED
//
// --------------------------------------------------------------------
// --------------------------------------------------------
// EXPAND AND COLLAPSE INDIVIDUALLY
// --------------------------------------------------------

    function expand($productContent) {
		//alert('in expand function');
		$('.eclinkDocs', $productContent).addClass('expand')
		// changing class styles on both hover on and hover off
		.hover(
			function () {
				$(this).parent().addClass('echover');
			},
			function () {
				$(this).parent().removeClass('echover');
			})
		
		// functionality of expanding or collapsing hidden div based on current class
		.click(
			function () {
				// $(this).parent().removeClass('ecbackground');
				var classValue = $(this).attr('class'); // grab class value
				var eclinkText = $(this).text(); // grab link text
				var classExpand = /expand/; 
				var classCollapse = /collapse/;
				if (classExpand.test(classValue) == true) //if class value contains expand then show hidden content
					{
					$(this).removeClass('expand').addClass('collapse').parent().children('div.ec-contentDocs').show();
					return false;
					}
				else  // if class doesn't contain expand, then hide visible content
					{
					$(this).removeClass('collapse').addClass('expand').parent().children('div.ec-contentDocs').hide();
					return false;
					}
		});
    }
	function filter() {
		// function that handles filtering nav links within each tab
		$('input.filter').focus(function() {
  			var navIDStart = $(this).attr('id');
			var navID = navIDStart + '-category-nav';
			//$(this).quicksearch('div#' + navID + ' a');
			$(this).quicksearch('div#' + navID + ' a', {
				'delay': 200,
				//'selector': 'th',
				//'stripeRows': ['odd', 'even'],
				//'loader': 'span.loading',
				'bind': 'keyup click',
				'show': function () {
					this.style.color = '';
				},
				'hide': function () {
					this.style.color = '#ccc';
				}//,
				//'prepareQuery': function (val) {
				//	return new RegExp(val, "i");
				//},
				//'testQuery': function (query, txt, _row) {
				//	return query.test(txt);
				//}
			});

			//$('div#' + navID +' p').hide();
			//$('div#' + navIDStart + '-category-content').hide();
		});
	}
	function getUrlVars() {
			// function that returns an array/object with URL parameters and their values
			// example: http://www.example.com/?me=myValue&name2=SomeOtherValue
			// returns and array:
			// 	{ "me"     : "myValue"
			//    "name2"  : "SomeOtherValue" }
			// To get a value fo the first parameter
			// var first = getUrlVars () ["me"];
				var vars = [], hash;
				var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1).split('&');
				for(var i = 0; i < hashes.length; i++)
				{
					hash = hashes[i].split('=');
					vars.push(hash[0]);
					vars[hash[0]] = hash[1];
				}
				return vars;
	} // end function getUrlVars
	function getAlertDocs($productContent) { // $productContent = div.product_family for current tab
				$productContent.append('<div class="alert-wrap"><h4>Document Alert&nbsp;/&nbsp;Changes</h4></div>');
				$('.alert-wrap', $productContent).append($($productContent).find('.alert_doc'));			
	} // end function getAlertDocs
	function bbqVersion(navLinkID) {
			var state = {},
			nav = 'nav',			  
			navID = navLinkID;
			// Set the state!
			state[ nav ] = navID;
			$.bbq.pushState( state );	
	} // end function bbqVersion
	function loadTabs(bookmark, bookmarkTabID, versionID, tabContentID, $vContent) {
			var divIncludeSBPFile = 'div.includeSBPFile';
			var numIncludeSBPFile = $(divIncludeSBPFile).length; // determines the number of CSI files
			for (i = 0; i < numIncludeSBPFile; i++)
			{
				var includeSBPFileUrl = $(divIncludeSBPFile+':eq('+i+')').text(); // grabs the CSI URL 
				$(divIncludeSBPFile+':eq('+ i +')').load(includeSBPFileUrl, function() 
				{ //callback function after page is loaded
					var tabID = $(this).parent().attr('id'); // ex tab-sd-content (tab panel ID)
					var $tabDIV = $('div#' + tabID); //ex div#tab-sd-content (tab panel div)
					var $contentFile = $(this).find('div[class$="-Files"]'); // finds the child div that has a class that ends in '-Files'
					if (tabID == tabContentID) {
						var bookmarkContentIncludeURL = $('a#' + versionID).attr('href'); // grab CSI URL from bookmark link
						$contentFile.load(bookmarkContentIncludeURL, function() {
							var $productContent = $('div.product_family', $contentFile);												  
							$('a.hiliteSN', $tabDIV).removeClass('hiliteSN');
							$('a#' + versionID, $tabDIV).addClass('hiliteSN');
							if($('.alert_doc', $tabDIV).length > 0) {
								getAlertDocs($productContent); }
							if($('.eclinkDocs', $tabDIV).length > 0) {
								expand($productContent);}
							$('#tabs div.ui-tabs-panel:not([ui-tabs-hide])').addClass('ui-tabs-hide').removeAttr('style');
							$('#tabs li.ui-tabs-selected').removeClass('ui-tabs-selected');
							$('#tabs li#' + bookmarkTabID).addClass('ui-tabs-selected');		
							$vContent.css('display','block').removeClass('ui-tabs-hide');	
							filter();
						 });
					} else {
						var $defaultContentIncludeLink = $(this).find('.nav a.version:first'); // locate first nav item
			    		$defaultContentIncludeLink.addClass('hiliteSN'); // hilite first nav item
						var contentIncludeURL = $defaultContentIncludeLink.attr('href'); // grabs CSI URL of first nav item
						$contentFile.load(contentIncludeURL, function() { // loads CSI file into div.include-by-devices-Files 
							var $productContent = $('div.product_family', $contentFile);
							if($('.alert_doc', $tabDIV).length > 0) {		  
								getAlertDocs($productContent); }
							if($('.eclinkDocs', $tabDIV).length > 0) { 
								expand($productContent);}
							filter();
						}); // end $contentFile.load
					} // end else
					$('a.version').click( function() {
						var $navLink = $(this);
						var $tabDIV = $navLink.parent('div').parent('div').parent('div'); // target tab panel div
						var navLinkID = $(this).attr('id');
						var navLinkURL = $(this).attr('href');
						bbqVersion(navLinkID, $tabDIV);
						changeNav($navLink, navLinkID, navLinkURL, $tabDIV);
						return false;
					}); // end $('a.version').click
				}); // end includeSBPFileURL
			} // end for loop	
	} // end function loadTabs
	function changeNav($navLink, navLinkID, navLinkURL, $tabDIV) {
				var $contentFile = $navLink.parent().next().find('div[class$="-Files"]');				
				var $vnav = $navLink.parent();
				$contentFile.html('').hide().fadeIn('slow');
				$('a.hiliteSN', $vnav).removeClass('hiliteSN');
				$navLink.addClass('hiliteSN');
			    $contentFile.load(navLinkURL, function() {
					var $productContent = $('div.product_family', $contentFile);								   
					if($('.alert_doc', $tabDIV).length > 0) {								  
						getAlertDocs($productContent); }
					if($('.eclinkDocs', $tabDIV).length > 0) {
						expand($productContent);}
					filter();
				}); // end $contentInclude.load		
	} // end function changeNav
	
	
// --------------------------------------------------------------------
//   
//   INITIAL PAGE LOAD
//
// --------------------------------------------------------------------	
	
	if (window.location.href.indexOf("#") >= 0) { // bookmark parameters are passed, then open specific tab and show specific product content
		var bookmark = 1;
		//alert('bookmark = ' + bookmark);
		var versionID = getUrlVars() ["nav"]; // grab the nav link ID
		var bookmarkTabID = getUrlVars() ["tab"];	// grab the tab ID
		var tabContentID = $('li#'+ bookmarkTabID + ' a').attr('href').replace( /^#/, '' ); // grabs the tab panel ID where content is loaded into
		var $vContent = $('div#' + tabContentID); // tab panel DIV
		loadTabs(bookmark, bookmarkTabID, versionID, tabContentID, $vContent); // load tabs
	} else { // no bookmark parameters are passed, then load tabs with initial content
		var bookmark = 0;
		//alert('bookmark = ' + bookmark);
		var divIncludeSBPFile = 'div.includeSBPFile';
		var numIncludeSBPFile = $(divIncludeSBPFile).length;
		for (i = 0; i < numIncludeSBPFile; i++)
		{
			var includeSBPFileUrl = $(divIncludeSBPFile+':eq('+i+')').text();
			$(divIncludeSBPFile+':eq('+ i +')').load(includeSBPFileUrl, function()
			{ //callback function after page is loaded
				var tabID = $(this).parent().attr('id');
				var $tabDIV = $('div#' + tabID);
				var $contentFile = $(this).find('div[class$="Files"]');
				var $defaultContentIncludeLink = $(this).find('.nav a.version:first');
				var navLinkID = $defaultContentIncludeLink.attr('id');
				if (tabID == 'tab-sd-content') {
					// sets default parameters in URL for first tab and first nav item
					bbqVersion(navLinkID);
					var state = {},      
					// Get the id of this tab widget.
					id = 'tab',      
					// Get the index of this tab.
					idx = 'tab-sd'						
					// Set the state!
					state[ id ] = idx;
					$.bbq.pushState( state );
				}
			    $defaultContentIncludeLink.addClass('hiliteSN');
				var contentIncludeURL = $defaultContentIncludeLink.attr('href');
				$contentFile.load(contentIncludeURL, function() {	
					var $productContent = $('div.product_family', $contentFile);	
					if($('.alert_doc', $tabDIV).length > 0) {								  
						getAlertDocs($productContent); }
					if($('.eclinkDocs', $tabDIV).length > 0) {
						expand($productContent);}
					$(this).find('.nav a.version:first').addClass('hiliteSN');
				}); // end $contentFile.load
					$('a.version').click( function() {
						var $navLink = $(this);
						var $tabDIV = $navLink.parent('div').parent('div').parent('div'); // target tab panel div
						var navLinkID = $(this).attr('id');
						var navLinkURL = $(this).attr('href');
						bbqVersion(navLinkID, $tabDIV);
						changeNav($navLink, navLinkID, navLinkURL, $tabDIV);
						return false;
					}); // end $('a.version').click
                filter();
			}); // end callback function for divInclude
		} // end for loop
	} // end else
		
  var timer;
function button1_click(event)
{
     $(".slide").css("visibility","hidden");
     $("#div1").css("visibility","visible");
     $("#div1").css("opacity","0");
    $("#div1").animate({"opacity":1},300, "linear", null);
     $("ul.buttons li").removeClass("active");
    $("#div1").animate({"opacity":1},300, "linear", null);
     $("#button1").addClass("active");
     clearTimeout(timer);
     timer = setTimeout(eval("button2_click"),"7000");
    $("#div1").animate({"opacity":1},300, "linear", null);
}

function button2_click(event)
{
     $(".slide").css("visibility","hidden");
     $("#div2").css("visibility","visible");
     $("#div2").css("opacity","0");
    $("#div2").animate({"opacity":1},300, "linear", null);
     $("ul.buttons li").removeClass("active");
    $("#div2").animate({"opacity":1},300, "linear", null);
     $("#button2").addClass("active");
     clearTimeout(timer);
     timer = setTimeout(eval("button1_click"),"7000");
    $("#div2").animate({"opacity":1},300, "linear", null);
}

function OnLoad(event)
{
     clearTimeout(timer);
     timer = setTimeout(eval("button2_click"),"7000");
}

$('#button1').bind('click', button1_click);

$('#button2').bind('click', button2_click);

OnLoad();
 
}); // end document ready

