/*
Function to stripe rows or columns of a table
*/
var foo;


$.tfxStripeTable = function(options) {
	var defaults = {
		tableClassName: 'striped',
		headerClassName: 'header-row',
		oddClassName: 'odd-row',
		evenClassName: 'even-row',
		highlightRow: true,
		highlightRowClassName: 'highlight-row',
		highlightColumn: false,
		highlightColumnClassName: 'highlight-column'
	};

	var opts = $.extend(defaults, options);
	
	//Add stripes to table rows ignoring headings + rows with opts.headerClassName class
	var tableString="table."+opts.tableClassName+" > tbody";
	
	$(tableString).each(function() {
		foo=$(this).children().siblings().andSelf();
								 
		var rowString = "tr[class!="+opts.headerClassName+"]:not(:has(th))";

		$(this).find(rowString+':odd').addClass(opts.oddClassName);
		$(this).find(rowString+':even').addClass(opts.evenClassName);
	});

	//enable row mouseovers if desired
	if(opts.highlightRow) {
		var tableString = "table."+opts.tableClassName+" tr:not(:has(th))";

		$(tableString).hover(function() {
			$(this).addClass(opts.highlightRowClassName);
		}, function() {
			$(this).removeClass(opts.highlightRowClassName);
		});
	}
	
	//enable column mouseovers -- We do not highlight headings!
	if(opts.highlightColumn) {
		$('td').hover(function() {
			var index = $(this).parent().children().index(this); 
			
			var tableString = "table."+opts.tableClassName+" tr[class!="+opts.headerClassName+"]:not(:has(th))";
			
			$(tableString).each(function() {
				$(':nth-child(' + (index + 1) + ')' ,this).addClass(opts.highlightColumnClassName);
			});
		}, function() {
	    	var index = $(this).parent().children().index(this);
		    $(tableString).each(function() {
				$(':nth-child(' + (index + 1) + ')' ,this).removeClass(opts.highlightColumnClassName);		
			});
	  	});
	}

};

/*
Function to swap captcha image when clicked
*/
$.tfxSwapCaptcha = function() {
		$('#captchaTip').show();
	
		$('#captcha').click(function () { 
			$(this).hide();
			$(this).attr('src', 'http://www.teleflex.com/captcha/captcha.jpg?'+ new Date() );
			$(this).fadeIn(); 
		});
		
		$('#captcha').hover(
			function () { 
				$(this).css('cursor','pointer'); 
			},
			function () {
				$(this).css('cursor','default'); 
			}
		);
};


/**
A rare bit of Documentation
---------------------------

This code is to support multi language version of emea.  

It is included by the different language versions of the EMEA page
template and assumes the presences of a select called language-selector 
whose options have iso lang codes and language specific descriptions 
for the language.

Upon loading, this code tries to determine the language to select in 
the selector.  It does this by first deciding if this is a static html 
page or part of the commonInternet tomcat application.  

In the static case it assumes that the first path element is the country 
code.  In order that any links to commonInternet pages go to the language
specific version, it will also store the selected in the session by 
calling /commonInternet/set-langauge.  

In the commonInternet case it will call the /commonInternet/get-language 
url to get the language from the session. 

This code also detects changes in the language selector.  Again there 
are 2 cases: static or commonInternet. 

In the static case it changes the url to have the new lang in it, e.g. 
/en/some/page.html will be changed to /de/some/page.html if the user 
selects german.  The language will also be set in the session. 

In the common Internet case it will set it in the session and then 
reload the page.

**/

function langSelected(){
	var selectedLang = $("#language-selector").val();
	setSessionLang(selectedLang);
}


function langSelectorExists() {
	return $("#language-selector").length > 0;
}

function setSessionLang(lang){

	$.get('/commonInternet/resources/set-language/'+lang,null,
		function(data){
			var pathElements = document.location.pathname.split("/");
			if (pathElements[1] == 'commonInternet') {
				location.reload(true);
			} else {
				if (langSelectorExists()){
					var selectedLang = $("#language-selector").val();
					pathElements.splice(0,2,selectedLang);
					var newUrl = '/'+pathElements.join('/');
					if (document.location.pathname != newUrl){
						document.location.pathname=newUrl;
					}
				}	
			}		
		});
}

function setSelectLang(lang){
	try {
		jQuery("select#language-selectoroption[selected]").removeAttr("selected");
		selector="select#language-selector option[value='"+lang+"']";
		jQuery(selector).attr("selected","selected");
	}
	catch(e) { 
	}
}

$(document).ready(
	function(){
	
		$('#language-selector').change(langSelected);

		// work out what lang to select based on the url (either for the
		// session in the case of commoninternet or from the first path
		// element other wise)
		var pathElements = document.location.pathname.split("/");
		if (pathElements[1]=='commonInternet'){
			//getthelangfromtheapp
			$.get('/commonInternet/resources/get-language',null,
				function(data)
				{
					setSelectLang($(data).find('language').text());
				});
		} else {
			//use the first element of the path
			if (langSelectorExists()){
				//setSessionLang(pathElements[1]);
				setSelectLang(pathElements[1]);
			}
		}
	});

