// JavaScript Document
function initBinding() { 


	// Add class last to the last module in a module panel
	jQuery(document).ready(function(){
		jQuery('.module-panel div:last-child').addClass('last');
	});

	// hide show function for descriptions
	jQuery(document).ready(function(){
		jQuery(function() {
			// Limit 
			var limit = 200;
			// place all p tags in an array
			var chars = jQuery(".description p");
			// for each array item
			jQuery.each(chars, function() {
				// place all text within variable				   
				var charLength = jQuery(this).text();
				// if the text exceeds the limit then proceed
				if (charLength.length > limit) {
					// text under 207 place in this variable
					var visiblePart = jQuery("<span> "+ charLength.substr(0, limit-1) +"</span>");
					// creat variable for dots
					var dots = jQuery("<span class='dots'>... </span>");
					// text over 207 place in this variable
					var hiddenPart = jQuery("<span class='more'>"+ charLength.substr(limit-1) +"</span>");
					// create more and less html
					var readMore = jQuery("<span class='read-more'><img src='images/plus-button.gif' /></span>");
					var readLess = jQuery("<span class='read-less'><img src='images/close-button.gif' /></span>");
					// create click function for read more button
					readMore.click(function() {
						// hide this read more button
						jQuery(this).hide();
						// find dots and hide
						jQuery(this).parent().children(".dots").hide();
						// slide toggle the hidden part
						jQuery(this).parent().children(".more").slideToggle('slow')
						// show the read less button
						jQuery(this).parent().children(".read-less").show();
		
					});
					// create click function for read less button
					readLess.click(function() {
						// hide this read less button
						jQuery(this).hide();
						// find dots and show
						jQuery(this).parent().children(".dots").show();
						// slide toggle the hidden part
						jQuery(this).parent().children(".more").slideToggle('slow');
						// show the read more button
						jQuery(this).parent().children(".read-more").show();
					});
					// recreate the paragraph within description with the above a values for visible text, dots, hidden text, and buttons
					jQuery(this).empty()
						.append(visiblePart)
						.append(dots)
						.append(readMore)
						.append(hiddenPart)
						.append(readLess)
					jQuery('span.more').hide();
				}
			});
		});
	});


	// function for the sliding of the program selections for program pages
	jQuery(document).ready(function(){
		jQuery('.program-selection-button').unbind('click').bind('click', function(){
			jQuery('.program-selection').slideToggle(300);										  
		});
	});

	// start transforming form elements
	jQuery(function() {
		//find all form with class jqtransform and apply the plugin
		jQuery("form.jqtransform").jqTransform();
	});

	// start ui tabs for the contact section
	jQuery(function() {
		// jQuery('ul#main-contact-tabs').tabs();
		jQuery('ul#sub-contact-tabs').tabs({ fx: { opacity: 'toggle' } });
		jQuery('ul#agent-contact-tabs').tabs({ fx: { opacity: 'toggle' } });
	});

	/* fade and toggle effects for agents selection */
	jQuery(document).ready(function(){
		jQuery('#agent-contact-tabs').hide();
		jQueryagentname = jQuery('#agent-contact-tabs li:first-child').text();
		jQuery('.agentSelectWrapper span').text(jQueryagentname);
		jQuery('.agentSelectWrapper').click(function(){
			jQuery('#agent-contact-tabs').slideToggle();
			return false;
		});
		jQuery('.jqTransformSelectOpen').click(function(){
			jQuery('#agent-contact-tabs').slideToggle();	
			return false;
		});	
		jQuery('#agent-contact-tabs a').click(function(){
			jQuery('#agent-contact-tabs').slideToggle();
			jQueryagentname = jQuery(this).text();
			jQuery('.agentSelectWrapper span').text(jQueryagentname);
		});
	});


	jQuery(document).ready(function(){
		jQuery('.quick-search .forms-up .jqTransformSelectWrapper').css({zIndex: 1});
	});

	jQuery(document).ready(function(){
		jQuery('#heading .jqTransformSelectWrapper ul').addClass('buttons');
	});


	jQuery(document).ready(function(){
		jQuery("#left-col ul li a").unbind('mouseover').bind('mouseover', function(){
			jQuery(this).animate( { color: '#00aeef'}, 200 );
		});
		jQuery("#left-col ul li a").unbind('mouseout').bind('mouseout', function(){
			jQuery(this).animate( { color: '#9B9B9B' });
		});
	});


	jQuery(document).ready(function(){
		jQuery('#footer ul li:first-child').addClass('first');
	});

		
		
	jQuery(document).ready(function(){
		addContent = jQuery('.home-address .info .content div').hide();
		jQuery('.home-address .info .forms').show();
		jQuery('.home-address .info .content div:first-child').show();
		jQuery('.home-address .info .forms ul li a').click(function(){
			clickedAddId = jQuery(this).attr('href');
			var index = clickedAddId.lastIndexOf("#");
			var filename = clickedAddId.substr(index);
			clickedAddId = filename.replace(/[#]/, "");
			addContentVis = jQuery('.home-address .info .content div:visible');					
			jQuery(addContent).each(function(){
				addId = jQuery(this).attr('id');
				if(addId == clickedAddId){
					addContentFadeIn = jQuery(this);
					jQuery(addContentVis).fadeOut(function(){
						jQuery(addContentFadeIn).fadeIn();
					});
				}
			});
			return false;
		});
	});



	


	jQuery(document).ready(function(){
		jQuery('#program-data').click(function(){
			// fade out all panels
			jQuery('#right-col img').fadeOut(400);
			jQuery('#right-col .content-panel').fadeOut(400, function(){
				// ajax call
				jQuery.ajax({				
					url: 'program-data-level2.html',
					success: function(data) {
						// Queue functions
						jQuery('#right-col').queue(function(){
							// add new data - empty container and append data
							jQuery(this).html(data);
							jQuery(this).empty().append(data);
							// fade in conent and add a class
							jQuery(this).hide().fadeIn();
							// rebind jquery
							jQuery('form').removeClass('jqtransformdone');
							initBinding();
							jQuery(this).dequeue();
						});
					}
				});
				// loading image
				loading = '<div id="loading"><img src="images/ajax-loader.gif" /></div>';
				jQuery('#right-col').empty().append(loading).hide().fadeIn('slow').ajaxStart(function() {
					jQuery(loading).show();		
				}).ajaxStop(function(){
					jQuery(loading).hide();
				});			
			});
			jQuery(this).parent().addClass('selected');
			return false;
		});
	});
}


initBinding();	



//// top bar function
//jQuery(document).ready(function(){
//	var api1 = jQuery(".top-panel-wrapper").expose({api:true}); 
//	opened = false;
//	closedText = '';
//	jQuery('#close-top').hide();	
//	jQuery('#close a').click(function(){
//		jQuery('.nav').hide();
//		jQuery('.top-panel').slideToggle(function(){
//			if(opened == true){
//				api1.close()
//				opened = false;
//				jQuery('.nav').show();
//				jQuery('#header').css({zIndex: 9999});
//			} else {
//				api1.load()
//				opened = true;
//				jQuery('#close-top').fadeIn();
//				jQuery('#header').css({zIndex: 10000});
//				jQuerycontentTop = jQuery('.top-panel .content').html();	
//				if(jQuerycontentTop == ''){
//					// ajax call
//					jQuery.ajax({
//						url: '/about-ef/country-selector',
//						success: function(data) {
//							jQuery('.top-panel .content').queue(function(){
//								jQuery(this).html(data);
//								jQuery(this).empty().append(data);
//								jQuery('.top-panel .content div').hide().fadeIn();
//								jQuery('form').removeClass('jqtransformdone');
//								initBinding();	
//								jQuery(this).dequeue();
//							});
//						}
//					});
//					loading = '<div id="loading"><img src="http://media.ef.com/_imgs/funnelpages2010/ajax-loader.gif" /></div>';
//					jQuery('.top-panel .content').empty().append(loading).ajaxStart(function() {
//						jQuery(loading).show();
//					}).ajaxStop(function(){
//						jQuery(loading).hide();
//					});
//				}
//			}
//		});
//		if(closedText == 'true'){
//			jQuery(this).children().children().text(countryText);
//			jQuery(this).children().children().prepend("<img src="+ countryImg +" /> ");
//			closedText = 'false';
//			jQuery(this).children().children().removeClass();
//			jQuery(this).children().children().addClass('arrow');
//			jQuery('#close-top').hide();
//		//	jQuery(this).css({paddingLeft: 0});
//		} else {
//			countryText = jQuery(this).children().children().text();
//			countryImg = jQuery(this).children().children().children().attr('src');
//			jQuery(this).children().children().text('Close');
//			jQuery(this).children().children().removeClass();
//			jQuery(this).children().children().addClass('close');
//			closedText = 'true';
//		//	jQuery(this).css({paddingLeft: 0});
//		}
//		return false;
//    });
//	
//	jQuery('#close-top a').click(function(){
//		jQuery('#close-top').fadeOut();
//		jQuery('.top-panel').slideToggle(function(){
//			if(opened == true){
//				api1.close()
//				opened = false;
//				jQuery('.nav').show();
//				jQuery('#header').css({zIndex: 10000});
//			} else {
//				api1.load()
//				opened = true;
//				jQuery('#header').css({zIndex: 10000});
//			}
//		});
//		if(closedText == 'true'){
//			jQuery('#close a').children().children().text(countryText);
//			jQuery('#close a').children().children().prepend("<img src="+ countryImg +" /> ");
//			closedText = 'false';
//			jQuery('#close a').children().children().removeClass();
//			jQuery('#close a').children().children().addClass('arrow');
//		//	jQuery(this).css({paddingLeft: 0});
//		}
//		return false;
//    });
//});





jQuery(document).ready(function(){
	jQuery('#ajaxfade').click(function(){
		// fade out all panels
		jQuery('.home-panel').fadeOut(400);
		jQuery('.quick-search').fadeOut(400);
		jQuery('.module-panel').fadeOut(400);
		jQuery('.options').fadeOut(400);
		jQuery('#footer').fadeOut(400, function(){
			// hide home address after fade out
			jQuery('.home-address').hide();
			// ajax call
			jQuery.ajax({
				url: 'program-data.html',
				success: function(data) {
					// Queue functions
					jQuery('#inner-content').queue(function(){
						// add new data - empty container and append data
						jQuery(this).html(data);
						jQuery(this).empty().append(data);
						// fade in conent and add a class
						jQuery(this).hide().fadeIn().addClass('programs');
						// remove home class
						jQuery('#content').removeClass('home');
						// amend heading structure
						jQuery('#heading').append('<h1>&nbsp;</h1>');
						// fade in options and remove class
						jQuery('.options').removeClass('no-heading').fadeIn();
						// fade in footer
						jQuery('#footer').fadeIn();
						// rebind jquery
						jQuery('form').removeClass('jqtransformdone');
						initBinding();
						jQuery(this).dequeue();
					});
				}
			});
			// loading image
			loading = '<div id="loading"><img src="images/ajax-loader.gif" /></div>';
			jQuery('#inner-content').empty().append(loading).hide().fadeIn('slow').ajaxStart(function() {
				jQuery(loading).hide();	
			}).ajaxStop(function(){
				jQuery(loading).hide();
			});			
		});
		return false;
	});
});




// Form vallidation
jQuery(document).ready(function(){
	
	jQuery(".jqtransform").validate({				   
			// the errorPlacement has to take the table layout into account
			errorPlacement: function(error, element) {
				if ( element.is(":radio") )
					error.appendTo( element.parent().next().next() );
				else if ( element.is(":checkbox") )
					error.appendTo ( element.next() );
				else if ( element.is("select") )
					error.appendTo ( element.parent() );
				else if ( element.is("textarea") )
					error.appendTo ( element.parent().parent().parent().parent().parent().parent() );
				else
					error.appendTo( element.parent().parent().parent().parent() );
			}						   
		});
	
});



/*
Global Variables
*/

 var byfunction;
 var byregion;
 var jobpageurl = "/about-ef/careers/current-opportunity/job-list/";
 

// Capturing the change event of the Transformed Select Box using the modified method of capturing the value change event.
 jQuery(document).ready(function() {
     jQuery("#right-column .jobsearch-promobox div#byfunction div.jqTransformSelectWrapper ul a").click(function() {
         byfunction = jQuery("#right-column .jobsearch-promobox div#byfunction div.jqTransformSelectWrapper ul a.selected").attr('index');
         //alert("Value Selected = " + byfunction);
         return false; //prevent default browser action
     });


     // Capturing the change event of the Transformed Select Box using the modified method of capturing the value change event.

     jQuery("#right-column .jobsearch-promobox div#byregion div.jqTransformSelectWrapper ul a").click(function() {
         byregion = jQuery("#right-column .jobsearch-promobox div#byregion div.jqTransformSelectWrapper ul a.selected").attr('index');
         //      alert("Value Selected = " + byregion);
         return false; //prevent default browser action


     });

     var url;
     jQuery(".button").click(function() {
         if (byfunction != undefined && byregion != undefined) {
             url = jobpageurl + "?f=" + byfunction + "&r=" + byregion;
         }
         else if (byfunction != undefined) {
             url = jobpageurl + "?f=" + byfunction;
         }
         else if (byregion != undefined) {
             url = jobpageurl + "?r=" + byregion;
         }
         else
             url = jobpageurl;


         //alert("URL= " + url);
         window.location.href = url;

     });

 });

 jQuery(document).ready(function() {

     var currentAboutEFURL = window.location.pathname;

     if (currentAboutEFURL.indexOf("job-list") != -1 || currentAboutEFURL.indexOf("jobs") != -1) {
         jQuery("#current-opportunity").addClass("selected");
     }
     if (currentAboutEFURL.indexOf("release") != -1) {
         jQuery("#release").addClass("selected");
     }
 });
