$(function(){

	$('#nav_main li').each(function(i){
		$(this).addClass('nav_main_item_'+i);
	});
	//Update from DOS to UNIX format 02/05/2010 - Anne-Marie Keithley
	// Much easier than doing this with TS, and the odds of it happening often are slim
	// Hide the Page Navigation widget if there are no child pages
	var childPages = $('#section_page_nav .content_collapsible li').length;
	if(childPages == 0) {
		//$('#section_page_nav').parent('.widgets_wrapper').hide();
	}
	
	//Collapses and expands right column widgets
	$('.section_sub_column .collapsible').click(function(){
		$(this).parents('.section_sub_column').toggleClass('collapsed');
	});
	
	// on page load, set the select list to the current domain
	$('#select_region option').each(function(){
		var current_url = document.domain;
		var option_url = $(this).val().replace('http://', '').replace('/', '');
		
		if( current_url == option_url )
		{
			$(this).attr('selected', true);
		}
	});
	
	// change sites
	$('#select_region').change(function(){
		var url = $(this).val();
		if(url) window.location = url;
	});
	
	// TYPO3 generated Bulleted List. If a content editor creates a list with the list object,
	// they can't add HTML to make the first word bold, so lets do that for them
	$('.section_details .csc-bulletlist li').each(function(){
		var self = $(this);
		var text = self.text().split(':');
		self.html( '<strong>'+text.shift()+':</strong> '+text.join(':') );
	});
		
	// Highlights last child in breadcrumb menu
	$("#breadcrumb li:last-child").addClass("current");
	$('#breadcrumb li').each(function(i){
		$(this).addClass('level_'+ parseInt(i + 1));
	});
	
	// Hover states on Product Categories
	$('.categories .entry').hover(
		function() {$(this).addClass('hover')},
		function() {$(this).removeClass('hover')}
	);
	
	// If any menu appears after the 4th, make sure it is positioned differently.
	// $('.nav_main_item').each(function(i){
	// 	if(i >= 4) $(this).children('.ajax').addClass('align_right');
	// })
	
	$('#nav_main li.nav_main_item').each(function(){
		var slug = $(this).children('a').attr('href').slice(0, -1);
		//var subnav = slug + slug +'-subnav';
		var subnav = slug +'-nav';
		var self = $(this);
		
		$(self).children('.ajax').html('<div class="nav_main_sub"><p style="padding: 10px;"><img src="/fileadmin/images/ajax-loader.gif" /></p></div>');
		// try an ajax load, and if it doesn't return a 404, then 
		// a sub nav exists... so load it in place.
		$.ajax({
			 url: subnav,
			 success: function(html){
				$(self).children('.ajax').html(html);
			 }
		});
	});
	
	$('.section_tabbed').tabbedContent();
	
	// Home page banner slides
	$('.banner_slides .banner_img ul').cycle({ 
	    fx: 'scrollLeft', 
	    speed: 1000, 
	    timeout: 9000, 
	    pager: '#slide_nav .paging',
		sync: 1,
		next: '.slide_nav_next',
		prev: '.slide_nav_prev'
	});
	// hide the slide nav if there are no slides present
	if(	$('.banner_slides .banner_img ul li').length === 0 ){
		$('.banner_slides #slide_nav').hide();
	}
	
	// Make the page banner slides clickable.
	$('.banner_slides .banner_slide').each(function(){
		var self = $(this);
		var link = self.find('.link');
		
		if(link.html() != ''){
			var url = link.children('a').attr('href');
			self.css('cursor','pointer');
			self.click(function(){
				window.location.href = url;
			});
		}
	});
	
	
	$('body.products .csc-textpic-image a').each(function(i){
		$('body').prepend('<div class="overlay" id="overlay_'+ i +'"><div class="content"></div></div>');
		$(this).append('<span class="overlay_image_text">click to enlarge</div>').attr('rel', '#overlay_'+ i).addClass('overlay_image');
	});
	$('body.products .csc-textpic-image a').overlay({
		expose: { 
            color: '#000000', 
            opacity: 0.44, 
            closeSpeed: 1000 
        }
	}).click(function(){
		var id = $(this).attr('rel');
		var image = $(this).attr('href');
		$(id +' .content').html('<img src="'+ image +'" />');
	}); 


	// Hover states on tabbed content entries
	$('.content_tabbed .tab_list li').hover(
		function() {$(this).addClass('hover')},
		function() {$(this).removeClass('hover')}
	);

	// Because TYPO3 insists on printing navigation in a DL
	$('.content_tabbed .csc-menu dt').mouseover(function(){
		$(this).addClass('hover').next('dd').addClass('hover');
	}).mouseout(function(){
		$(this).removeClass('hover').next('dd').removeClass('hover');
	});
	$('.content_tabbed .csc-menu dd').mouseover(function(){
		$(this).addClass('hover').prev('dt').addClass('hover');
	}).mouseout(function(){
		$(this).removeClass('hover').prev('dt').removeClass('hover');
	});
	
	
	$('a.popup').popUp();
	
	
	// This menu should only appear on the Contact pages. See if the menu exists, and if so,
	// hide all the .content_tabbed groups, and show the groups depending on which language
	// is choosen in the menu. .content_tabbed groups must appear in the same order as
	// their respective languages in the jump menu to work.
	if( $('.content_jump_menu').length > 0 ){
		$('.section_tabbed:gt(0)').hide();
		$('.typo3_lib_object:eq(0)').remove().clone(true).insertBefore('.section_tabbed:eq(0)');
		
		$('.content_jump_menu').change(function(){
			var index = $(this)[0].selectedIndex;
			$('.section_tabbed').hide();
			$('.section_tabbed:eq('+ index +')').show();
		});
	}
	
	$('.table_news, .table_events').each(function(){
		var self = $(this);
		var header = self.prev('div').children('h1').hide().text();
		self.find('thead th:eq(0)').text(header);
	});
	
	// make the category lists display proplery, so the floats don't break the layout
	$('.category_list li:even').css('clear', 'left');
	
	$('.table_news, .table_events').next('p').addClass('link_more').css('margin-top', 0);
	
	$('#banner p, #banner h1').widont();
	
	$('.csc-textpic-imagewrap').each(function(){
		var self = $(this);
		self.width( self.find('img').eq(0).width() );
	});
	
	/* This is pretty hack, but it works....
	When I try to wrap {leveltitle:-3} in a link TYPO3 generates the following HTML. Can you say bad parsing engine?
	<a&#160;href=".. ..="">Services</a&#160;href="..> - look at temp.nav_siblings = HMENU in the Template Setup
	What we're doing here is if the h2 contains the string value of the root node name (com, uk, nl etc) then 
	just hide the header b/c we don't want to link back. If it contains something other than one of those values, 
	then it's a valid header. The really hack part here is the rel attribute contains the value ../ or ../../, which 
	is set in the TypoScript. These values are used to create the actual link to the proper page.
	*/
	var nav_page_parent = $('.typo3_nav_page h2.nav_page_parent');
	var nav_page_parent_rel = nav_page_parent.attr('rel');
	if(/^(?:com|uk|es|nl|sk|de|fr)$/.test( nav_page_parent.text() )){
		nav_page_parent.hide();
	} else {
		nav_page_parent.wrapInner('<a href="'+ nav_page_parent_rel +'"></a>');
	}
	
	// Make up for IE 7 and 6 deficiences
	if( $.browser.msie && $.browser.version <= 7)
	{
		$('.tab_list').next('.csc-header').addClass('tab_list_csc_header');
		
		$('#nav_main li').mouseover(function(){
			$(this).children('.ajax').show();
		}).mouseout(function(){
			$(this).children('.ajax').hide();
		});
	}
	
	$("form[action='https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8']").attr('enctype','');

});



jQuery.fn.tabbedContent = function(options)
{
	var settings = {};

    if(options) { $.extend(settings, options); };

    return this.each(function() 
	{
		var self = $(this);
		
		self.find('li:first').addClass('current'); // Set current tab to first tab		
		self.find('.content_tabbed div:gt(0)').removeClass('current'); // Hide content sections except for first section
	    self.find('.content_tabbed:gt(0)').hide(); // Hide tabs
	
	    // Move tabs from inbetween content sections to inside the first tab div
	    //self.find('.nav_tabbed li:gt(0)').appendTo('.nav_tabbed ul:first');
		self.find('.nav_tabbed li:gt(0)').each(function(){
			self.find('.nav_tabbed ul:first').append( $(this) );
		});
	
	    self.find('.nav_tabbed:gt(0)').remove(); //Remove empty div's from old tabs

	    // Handle clicks on tabs
	    self.find('.nav_tabbed li').click(function() {
	        var tabIndex = self.find('.nav_tabbed li').index($(this)); // Get position of tab clicked
	        self.find('.content_tabbed').hide(); // Hide all currently visisble content sections
	        self.find('.content_tabbed:eq('+tabIndex+')').addClass('current').show(); // Show content section of same index as tab
	        self.find('.nav_tabbed .current').removeClass("current");
			$(this).addClass('current');
	    });
	});
}



jQuery.fn.popUp = function(options)
{
	var settings = {
		name: 'newWindow',
		width: 500,
		height: 600,
		left: false,
		top: false,
		scrollbars: 'yes',
		resizeable: 'no',
		statusbar: 'no',
		menubar: 'no',
		toolbar: 'no'
	}

    if(options) {
        jQuery.extend(settings, options);
    };

	this.each(function(){
		$(this).click(function(){
			var rel = $(this).attr('rel');
			var href = $(this).attr('href');
			if(!rel){
				window.open(href, settings.name);
			} else {
				var rel_split = rel.split('|');
				var width = rel_split[0] ? rel_split[0] : settings.width;
				var height = rel_split[1] ? rel_split[1] : settings.height;
				var scrollbars = rel_split[2] ? rel_split[2] : settings.scrollbars;
				var leftPos = settings.left ? settings.left : (screen.width-width)/2;
				var topPos = settings.right ? settings.right : (screen.height-height)/2;
				
				var config = 'width='+ width +',height='+ height +', \
							left='+ leftPos +',top='+ topPos +', \
							scrollbars='+ scrollbars +', \
							resizable='+ settings.resizeable +', \
							statusbar='+ settings.statusbar +', \
							menubar='+ settings.menubar +', \
							toolbar='+ settings.toolbar;
					
				window.open(href,settings.name, config);
			}
			return false;
		});
	});
}