var general = { 
	
	// OnLoad functions
	init: function(){
		this.bubbleEvt();
	},
	
	bubbleEvt: function() {
		$(".menu li, ul.lang-select li").not('.selected').hover( function () {
			$(this).addClass("hover");
		}, function () {
			$(this).removeClass("hover");
		});
		
		// Chrome couldn't get Dom div height
		if(navigator.userAgent.toLowerCase().indexOf('chrome') > -1)
		{
			setTimeout(function(){
						general.equalHeight($(".startpage .blurb"));
						general.equalHeight($(".startpage .two-col"));
					}
				,500);
		}
		else
		{
			general.equalHeight($(".startpage .blurb"));
			general.equalHeight($(".startpage .two-col"));
		}
		
		general.addLinkIcon();
		
		
		if($(".img-rotator img").length>1) {
			var turn = window.setInterval(general.rotate, 3000);	
		}
		
		
		$(".product-col, ul.job li, ul.news li").addClass("linked");
		$(".product-col, ul.job li, ul.news li").click(function(){
			var navigateTo = $(this).find("a").attr("href");
			window.location = navigateTo;
			return false;
		});	
		
		
		
		if( $('.flashwrapper').length>0 ) {
			var flashUrl = $('.flashwrapper img').attr("alt");
			var useLater = $.flash.create({
				swf: flashUrl,
				height: 309,
				width: 609
			});
			$('.flashwrapper').html(useLater);
		}
		
		
		
		
		
		$(".lang-select-link a").click(function(){
			general.handleLangSelectIn();
			return false;
		});	
		
		$('.lang-select .close').click(function(){
			general.handleLangSelectOut();
			return false;
		});	
		
		$('.button-link-ask .button-link').click(function(){
			general.handleAskForm(this);
			return false;
		});	
			
		$('.button-link-ask span.button-link, .button-link-ask a.button-link').hover( function () {
			$(this).addClass("hover");
		}, function () {
			$(this).removeClass("hover");
		});
		
		$('.content-rotator .button-link-placer .button-link').mouseover(function() {
			$('.application-layer').show()
			$('.content-rotator .button-link-placer .button-link').addClass('selected')
		});
		
		
		$('.application-layer').mouseleave(function() {
			$('.application-layer').hide()
			$('.content-rotator .button-link-placer .button-link').removeClass('selected')
		});
			
		$('.product-holder .read-more a').click(function(){		
			var startHeight = 160;		
			if ($(this).hasClass('more') ) {
				$('.product-holder .content').css('height',startHeight)
				$(this).parent().parent().find('.content').css('height','auto')
				$(this).parent().addClass('selected')
				$(this).parent().find('.more').css('display','none')
				$(this).parent().find('.less').css('display','block')
			} else {
				$(this).parent().parent().find('.content').css('height',startHeight)
				$(this).parent().removeClass('selected')
				$(this).parent().find('.more').css('display','block')
				$(this).parent().find('.less').css('display','none')				
			}
			return false;
		});
		
		$("a.fancybox").fancybox({
			'overlayColor' : '#fff',
			'overlayOpacity' : '0.7',
			'centerOnScroll' : true
		});


		$(".table-holder td").hover(function() {
		    $(this).parent('tr').addClass("tr-hover");
		}, function() {
		    $(this).parent('tr').removeClass("tr-hover");
		});
		
	},
	
	handleLangSelectIn: function() {
		$('.lang-select-link').animate({
			top: '-40px'
		}, 200, function() {
			$('.lang-select').fadeIn('fast');
		});
	},
	
	handleLangSelectOut: function() {
		$('.lang-select').fadeOut('fast', function() {
			$('.lang-select-link').animate({
				top: '0px'
			}, 200, function() {
				
			});
		});
	},	
	
	handleAskForm: function(e) {
		var linkParent = $(e).parent();
		
		if (linkParent.hasClass('selected')) {
			linkParent.removeClass('selected')
			$('.ask-form-holder').hide()
		} else {
			linkParent.addClass('selected')
			$('.ask-form-holder').show()
		}
	},
	
	hideAskForm: function(e) {
		$('.ask-form-holder').hide()
	},	
	
	equalHeight: function(group) {
		var tallest = 0;
		group.each(function() {
			var thisHeight = $(this).height();
			if(thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		group.height(tallest);
	},
	
	
	
	rotate: function rotate() {
				var active = $('.img-rotator .active');
				if($(active).next().length>0) {
					$(active).next().fadeIn(1400, function(){$(active).removeClass("active").hide();}).addClass("active");
				} else {
					$(active).fadeOut(1400, function(){$(active).hide()}).removeClass("active");
					$('.img-rotator img:first').show().addClass("active");
				}
	},
	
	
	
	
addLinkIcon: function(e) {
		// Add pdf icons to pdf links
		//$("a[href$='.pdf']").before('<span class="pdf"></span>&nbsp;').addClass("link-icon");

		// Add ppt icons to ppt links
		//$("a[href$='.ppt'], a[href$='.pptx']").before('<span class="powerpoint"></span>&nbsp;').addClass("link-icon");

		// Add txt icons to document links (doc, rtf, txt)
		//$("a[href$='.doc'], a[href$='.docx'], a[href$='.txt'], a[href$='.rft']").before('<span class="txt"></span>&nbsp;').addClass("link-icon");

		// Add zip icons to Zip file links (zip, rar)
		//$("a[href$='.zip'], a[href$='.rar']").before('<span class="zip"></span>&nbsp;').addClass("link-icon"); 

		// Add email icons to email links
		//$("a[href^='mailto:']").after('<span class="email"></span>&nbsp;').addClass("link-icon");
		
		//Add external link icon to external links - 
        $('a').not("a[href^='mailto:'], a.more-product-image, #fancybox-outer a, .PagingContainer a, .lang-select a").filter(function () {
			//Compare the anchor tag's host name with location's host name
			if (this.hostname !== location.hostname) {
				$(this).append("<span class='ext'>&raquo;</span>")
			}
		});
	}	
		
}

$(document).ready(function(){			   
	general.init();
});

/*
jQuery Clear-Input plugin
v1.0
Author: Aidan Feldman

USAGE
Add the class .clear-input to any text input element
whose value you want cleared when it gains focus.
The initial value will be replaced when the input
loses focus, and no new text has been entered.

If you prefer to not add classes to your elements,
you can alternatively call clearInput() on any jQuery
input object.
*/
(function( $ ){
  // define the initialValue() function
  $.fn.initialValue = function(value) {
    if (value) {
      return this.attr('data-initial-value', value);
    } else {
      return this.attr('data-initial-value');
    }
  };
  
  $.fn.clearInput = function() {
    return this
      .focus(function(){
        if (this.value == $(this).initialValue()) {
          this.value = '';
        }
      })
      .blur(function(){
        if (this.value == '') {
          this.value = $(this).initialValue();
        }
      })
      .each(function(index, elt) {
        $(this).initialValue(this.value);
      });
  };

  // apply plugin to all inputs with class ".clear-input"
  $(function() {
    $('input.clear-input').clearInput();
  });
})( jQuery );
