
// check if user is using ie6
var ie6browser = false;

if ($.browser.msie == true && parseInt(jQuery.browser.version) < 7) {
  ie6browser = true;
} 


/*
 * IE6 help for topnav dropdowns
 */
sfHover = function() {
  if(document.getElementById("nav_links")){
    var sfEls = document.getElementById("nav_links").getElementsByTagName("LI");
    
    for (var i=0; i<sfEls.length; i++) {
      sfEls[i].onmouseover=function() {
        this.className+=" sfhover";
      }
      sfEls[i].onmouseout=function() {
        this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
      }
    }
  }
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

/*
 * remove image from search box when user starts typing
 */
 
$(document).ready(function() {

	$('#searchtext').bind('focus', function() {
		$('#searchtext').addClass('focused');
	});
});

/* contact popup */
$(document).ready(function() {

   $('#contact_link').click(function(){
     $('#contact_popup').toggleClass('on');
     $('#contact_link').toggleClass('on');
     return false;
   });

});

/* toggle contact popup interior links */
$(document).ready(function(){

	//Hide (Collapse) the toggle containers on load
	$('#contact_popup a.toggle').next().hide(); 

	//Switch the "Open" and "Close" state per click
	$('#contact_popup a.toggle').toggle(function(){
		$(this).addClass("on");
		}, function () {
		$(this).removeClass("on");
	});

	//Slide up and down on click
	$('#contact_popup a.toggle').click(function(){
		$(this).next().slideToggle("slow");
	});  

});

//More-info popup form


$(document).ready(function(){

  $('a.more_info_popup').click(function() {      
    var $mydialog = $('<div></div>')
    .html('<iframe style="height: 100%; width: 100%;" marginWidth="0" marginHeight="0" frameBorder="0" src="https://www.infoprint.com/apps/sales/psdcpctool.nsf/testform" allowTransparency="true"/>')
    .dialog({
      autoOpen: false,
      modal: true,
      height: 385,
      width: 340,
      bgiframe: ie6browser, // ie6browser is true or false, and only ie6 needs bgiframe set
      title: '',
      closeText: 'X'      
    });
    
    $mydialog.dialog('open');    
    
    // don't toggle the contact popup for links with the class "no_contact_popup"
    if( $(this).parents("div[id=contact_popup]").length > 0 ) {
      $('#contact_popup').toggleClass('on');
      $('#contact_link').toggleClass('on');
    }    
    return false;
  });
  
  
  $('form.form_popup').submit(function(){
  
    var queryStr = '&' + $(this).serialize();
    
    var $mydialog = $('<div></div>')
    
    .html('<iframe style="height: 100%; width: 100%;" marginWidth="0" marginHeight="0" frameBorder="0" src="forms_international_contacts?open' + queryStr + '" allowTransparency="true"/>')
    .dialog({
      autoOpen: false,
      modal: true,
      height: 385,
      width: 340,
      bgiframe: ie6browser, // ie6browser is true or false, and only ie6 needs bgiframe set
      title: '',
      closeText: 'X'      
    });
    
    $mydialog.dialog('open');
    
    // hide the contact popup
    $('#contact_popup').toggleClass('on');
    $('#contact_link').toggleClass('on');
    
    return false;
  });
  
});  


$(document).ready(function()
{
	//content from another
	$('.linkpopover').click(function() {
		var $link = $(this);
		var $pageToLoad = $link.attr('href');
		//default to medium if size not specified
		var $width = 400;
		var $height = 400;
		//set height and width based on size class
		if ($link.hasClass('small')) { $width = 300; $height = 300; }
		else if ($link.hasClass('medium')) { $width = 400; $height = 400;}
		else if ($link.hasClass('large')) { $width = 600; $height = 600; }

		var customWidth = $link.attr('class').match( /\bpw\d{1,4}\b/i );
		if ( customWidth && customWidth[0].length>2 ) { $width = customWidth[0].substr(2); }
		var customHeight = $link.attr('class').match( /\bph\d{1,4}\b/i );
		if (customHeight && customHeight[0].length>2) { $height = customHeight[0].substr(2); }

		var $dialog = $link.getDialog();

		if ( $dialog.html() == '' )
		{
			$dialog
				.html('<iframe style="height: 100%; width: 100%;" marginWidth="0" marginHeight="0" frameBorder="0" src="' + $pageToLoad + '" allowTransparency="true"/>')
				.dialog({
					autoOpen: false,
					modal: true,
					height: $height,
					width: $width,
					bgiframe: ie6browser, // ie6browser is true or false, and only ie6 needs bgiframe set
					title: '',
					closeText: 'X'      
				});
		}
		$dialog.dialog('open');
		return false;
	});
});
// get the corresponding dialog for a popover link
(function($){
  $.fn.getDialog = function() {
    var dialog;
    var dialogLink = $(this).attr('href');
    if (dialogLink)
    {
      var dialogId = 'd_'+dialogLink.replace( /[^\w]/g, '' );
      var dialog = $('#'+dialogId);
      if ( dialog.length==0 )
      {
        return $('<div></div>').attr('id', dialogId)
      }
    }
    return dialog;
  }
})(jQuery);
(function($){
  $.fn.closeDialog = function() {
    if(this.getDialog())
      $(this).getDialog().dialog('close');
  }
  $.fn.destroyDialog = function() {
    if(this.getDialog())
    {
      $(this).getDialog().dialog('destroy');
      $(this).getDialog().remove();
    }
  }
})(jQuery);

// Tooltips
$(document).ready(function(){
	if( $.tooltip )
	{
		$('.tipped').tooltip({
			showURL: false,
			showBody: " - ",
			top: -5,
			left: 10,
			extraClass: "opacity"
		});
	}
});

// gets the index of the tab for a given tab name (hash)
function getHashTabIndex(tabs, hash)
{
	var listItem = $('ul:first li a[href=' + hash + ']:first', tabs).parent();
	return $('ul:first li', tabs).index( listItem );
}

// disable anchors, change location.hash, re-enable anchors
function locationHash( locHash ) {
	var fx, node = $( locHash );
	if ( node.length ) {
		fx = $( '<div></div>' )
						.css({
								position:'absolute',
								visibility:'hidden',
								top: $(window).scrollTop() + 'px'
						})
						.attr( 'id', locHash.substr(1) )
						.appendTo( document.body );
		node.attr( 'id', '' );
	}
	document.location.hash = locHash; // +'/';  // the '/' causes IE to not add clicks on this anchor to its history
	if ( node.length ) {
		fx.remove();
		node.attr( 'id', locHash.substr(1) );
	}
}

// detect the current hash (selected tab) repeatedly to catch browser history (back/forward) (doesn't work in IE 6 & 7 (8 ok?))
$(document).ready(function(){
	// only execute on pages with #tabs
	if( $('#tabs ul').length > 0 )
	{
		var recentHash = location.hash;
		if ( recentHash == '' ) recentHash = $('#tabs ul:first li:first a:first').attr('href'); // default to first tab
		function checkHashChange()
		{
			var currentHash = location.hash;
			if ( currentHash == '' ) currentHash = $('#tabs ul:first li:first a:first').attr('href'); // default to first tab
			if ( currentHash != recentHash)
			{
				if ( $('.ui-tabs').length>0 )
				{
					var tab = currentHash.split('/');
					var subTab = tab[1];
					tab = tab[0];

					$("#tabs").tabs('select', getHashTabIndex( $("#tabs"), tab ) );
					$(tab + " .ui-tabs:first").tabs('select', getHashTabIndex( $(tab + " .ui-tabs:first"), '#'+subTab ) );
					recentHash = currentHash;
				}
			}
		}
		var hashCheck = window.setInterval( checkHashChange,  250 );
	}
});

// check for subtab in url on pageload
$(document).ready(function(){
	// only execute on pages with #tabs
	if( $.scrollTo && $('#tabs ul').length > 0 )
	{
		if ( location.hash.indexOf('/') )
		{
			var aHash = location.hash.split('/');
			var subTab = aHash[1];
			aHash = aHash[0];

			// tabs haven't been initialized yet -- ui-tabs class not assigned!
			// Activate specified tab when they're available:
			var urlTabCheck = window.setInterval( function(){
				if ( $('.ui-tabs').length>0 )
				{
					$("#tabs").tabs('select', getHashTabIndex( $("#tabs"), aHash ) );
					$(aHash + " .ui-tabs:first").tabs('select', getHashTabIndex( $(aHash + " .ui-tabs:first"), '#'+subTab ) );
					clearInterval( urlTabCheck );
				}
			}, 250 );
		}
	}
});

// Footnote click - select tab, change location hash, scroll to footnote, return false
$(document).ready(function(){
	// only execute on pages with #tabs
	if( $.scrollTo && $('#tabs ul').length > 0 )
	{
		$('.footnote_ref').click(function(){
			var aLocation = getLocation( $(this).attr('href') );
			var aHash = aLocation.hash.split('/');
			var subTab = aHash[1];
			aHash = aHash[0];
			// if the hash reference is on this page
			if (aLocation.pathname==location.pathname)
			{
				// check if this hash points to a top-level tab
				if ( $('#tabs ul:first a[href="'+aHash+'"]').length > 0 )
				{
					$("#tabs").tabs('select', getHashTabIndex( $("#tabs"), aHash ) );
					if ( subTab )
					{
						$(aHash + " .ui-tabs:first").tabs('select', getHashTabIndex( $(aHash + " .ui-tabs:first"), '#'+subTab ) );
						document.location.hash = aHash + '/' + subTab;
					} else
						locationHash( aHash );
					$.scrollTo( '#tabs ' + aHash + ' .footnote:first' );
					return false;
				}
			}
		});
	}
});

// fix anchor links to tabs - links anchors pointing to tabs to the tab
//  - find all hash links in tabs, see if they match a tab,
//  - set location, change tab, return false
$(document).ready(function(){
	// only execute on pages with #tabs
	if( $('#tabs ul').length > 0 )
	{
		// for every anchor whose href contains a hash
		$('a[href*="#"]').each( function(){
			var aLocation = getLocation(this.href);
			var aHash = aLocation.hash.split('/');
			var subTab = aHash[1];
			aHash = aHash[0];
			// if the hash reference is on this page
			if (aLocation.pathname==location.pathname)
			{
				// and we're not dealing with a top-level tab link
				if ( this != $('#tabs ul:first a[href="'+aHash+'"]')[0] )
				{
					// check if this hash points to a top-level tab
					if ( $('#tabs ul:first a[href="'+aHash+'"]').length > 0 )
					{
						// make the anchor activate the tab
						$( this ).click( function() {
							$("#tabs").tabs('select', getHashTabIndex( $("#tabs"), aHash ) );
							if ( subTab )
							{
								$(aHash + " .ui-tabs:first").tabs('select', getHashTabIndex( $(aHash + " .ui-tabs:first"), '#'+subTab ) );
								document.location.hash = aHash + '/' + subTab;
							} else
								locationHash( aHash );
							return false;
						});
					}
				}
			}
		});
	}
});

// fix tab links so they change the location bar for bookmark-ability
$(document).ready(function(){
	// only execute on pages with #tabs
	if( $('#tabs ul').length > 0 )
	{
		// for every top-level tab anchor
		$('#tabs ul:first a').each( function(){
			var aLocation = getLocation(this.href);
			// if the hash reference is on this page
			if (aLocation.pathname==location.pathname)
			{
				var aHash = aLocation.hash;
				$( this ).click( function() {
//					locationHash( aHash );
					document.location.hash = aHash + '/';  // the '/' causes IE to not add clicks on this anchor to its history
					return false;
				});
			}
		} );

		// tabs haven't been initialized yet -- ui-tabs class not assigned!
		// Wire-up tab anchors when they're available:
		var anchorTabCheck = window.setInterval( function(){
			if ( $('.ui-tabs').length>0 )
			{
				$('#tabs div.ui-tabs-panel').each( function(){
					var parentTab = $(this).attr('id');
					$('div.ui-tabs ul:first a', $(this)).each( function(){
						var aLocation = getLocation(this.href);
						if (aLocation.pathname==location.pathname)
						{
							var aHash = aLocation.hash;
							$( this ).click( function() {
								document.location.hash = parentTab + '/' + aHash.substr(1);
								return false;
							});
						}
					});
				});
				clearInterval( anchorTabCheck );
			}
		}, 250 );

	}
});

// fix Back to Top links in diaglogs
$(document).ready(function(){
	if ($('body.dialog').length>0)
	{
		$('a[href*="#popover_top"]').click( function(){
			try{
			// $('.ui-dialog-content', window.parent.document )[0].scrollTop = 0;
			// $('.ui-dialog-content iframe', window.parent.document )[0].scrollTop = 0;
			$('html')[0].scrollTop = 0;
			// $('body')[0].scrollTop = 0;
			// $('div')[0].scrollTop = 0;
			} catch(e) { }
			return false;
		} );
	}
});

/*
 * prevent location hashes from scrolling to corresponding id's
 * http://www.w3.org/TR/html401/struct/links.html - "Destination anchors...may be specified...by any...element (naming with the id attribute)"
 */
hashStopper = function( offset ) {
	if (location.hash){
		if ( $('a[name='+location.hash.substr(1)+']').length == 0 ){
			if ( !offset ) offset = 0;
			// there is no <a name=...> for this hash
			$(window).scrollTop( offset );
		}
	}
}

$(document).ready( function() { hashStopper(0) } );
// chrome honors URL hash after DOM is ready, but removing ready() causes jumpy behavior for FF
$(document).load( function() { hashStopper(0) } );
// IE6 & 7 are hopeless!


// convert an anchor href into a faux location object
var getLocation = function( href )
{
	var aLocation = {};
	aLocation.href = href;
	aLocation.host = null;
	aLocation.hostname = null;
	aLocation.port = '';
	aLocation.search = '';
	aLocation.hash = null;
	aLocation.protocol = aLocation.href.match(/^(https?:)?\/\//);
	if (aLocation.protocol != null) {
		if (aLocation.protocol[1]==undefined)
		{
			aLocation.protocol = location.protocol;
			aLocation.href = aLocation.protocol + aLocation.href;
		} else
			aLocation.protocol = aLocation.protocol[1];
		var iSlash = aLocation.href.indexOf('/', aLocation.protocol.length+2);
		aLocation.hostname = aLocation.href.substring( aLocation.protocol.length+2, iSlash);
		aLocation.hostname = aLocation.hostname.split(':');
		if ( aLocation.hostname.length==2)
			aLocation.port = aLocation.hostname[1];
		aLocation.hostname = aLocation.hostname[0];
		aLocation.pathname = aLocation.href.substr(iSlash);
		aLocation.host=aLocation.hostname + ':' + aLocation.port;
	} else {
		if (aLocation.href.indexOf('/')==0)
			aLocation.pathname = aLocation.href;
		else
		{
			if (aLocation.href.indexOf('#')==0)
				aLocation.pathname=location.pathname + location.search + aLocation.href;
			else if (aLocation.href.indexOf('?')==0)
				aLocation.pathname=location.pathname + aLocation.href;
			else {
				var path=location.pathname;
				path=path.substring(0,path.lastIndexOf('/')+1);
				aLocation.pathname = path + aLocation.href;
			}
		}
	}
	if ( aLocation.pathname.indexOf('?') > 0 )
	{
		aLocation.search = aLocation.pathname.substr(aLocation.pathname.indexOf('?'));
		aLocation.search = aLocation.search.split('#')[0];
	}
	if ( aLocation.pathname.indexOf('#') > 0 )
		aLocation.hash = aLocation.pathname.substr(aLocation.pathname.indexOf('#'));
	aLocation.pathname = aLocation.pathname.split(/[#?]/)[0];

/*
	alert(
		aLocation.href +
		"\n" +
		aLocation.protocol +
		" // " +
		aLocation.host +
		" " +
		aLocation.pathname +
		" " +
		aLocation.search +
		" " +
		aLocation.hash
	);
*/
  return aLocation;
}


