(function($) {
    
  $.fn.mainnav = function(options) {
	
	  // support mutltiple elements
        if (this.length > 1){
            this.each(function() { $(this).tabs(options) });
            return this;
        }

        // SETUP private variabls;
        var $this = this;

        // setup options
		// cache dom nodes
        var defaultOptions = {   
			mainmenubg:null,
			overtimer:null, 
			timer: null
        };
        
		var options = $.extend({}, defaultOptions, options);
 
        // PRIVATE functions;
        var intialize = function() {
			options.mainmenubg = $('#mainmenu_bg');
			$('#'+$this.attr('id')+' a').each(function () {
				$(this).mouseover(function () {
					var temp = $('#'+$(this).attr('id')+'_subnav');
					var over = setTimeout(function () {
						//temp.fadeIn('fast');	
						temp.slideDown('fast');
						
					}, 250);
					clearTimeout(temp.data('out'));
					temp.data('over',over);
					
				});
				$(this).mouseout(function () {	
					var temp = $('#'+$(this).attr('id')+'_subnav');
					var out = setTimeout(function () {
						//temp.fadeOut('fast');	
						temp.slideUp('fast');
					}, 250);
					clearTimeout(temp.data('over'));
					temp.data("out",  out);
					
					//clearTimeout(jQuery.data(temp , "overtimer"));
				});
			});
			
			$('.subnav').each(function () {
				$(this).mouseenter(function () {
					var mainnavid = '#'+($(this).attr('id').replace('_subnav',''));
					$(mainnavid).addClass('active');
					clearTimeout($(this).data('out'));
					/*if(options.timer != null)
						clearTimeout(options.timer);*/
				});
				
				$(this).mouseleave(function () {
					var temp = $(this);
					setTimeout(function () {
						var mainnavid = '#'+($(temp).attr('id').replace('_subnav',''));
						$(mainnavid).removeClass('active');
						//temp.fadeOut('fast');	
						temp.slideUp('fast');
					},250);
				});
			});
			return $this;
        };
		// PUBLIC functions
		this.open = function ()
		{
			return false;
		}
			
        return intialize();
  };
  
  
 $.fn.headline = function(options) {
	
	  // support mutltiple elements
        if (this.length > 1){
            this.each(function() { $(this).tabs(options) });
            return this;
        }

        // SETUP private variabls;
        var $this = this;

        // setup options
		// cache dom nodes
        var defaultOptions = { 
			headlineclass: '.newsheadline-wrapper .headline',  
			autorotate:true,
			index: 0,
			timer: 3000,
			animating: 0,
			circlemarkers: $('#circlemarkers a'),
			interval: null
        };
        
		var options = $.extend({}, defaultOptions, options);
 
        // PRIVATE functions;
        var intialize = function() {
			$('#mainmenu_bg').mouseenter(function () {
				options.autorotate =false;
				clearInterval(options.interval);
			});
			$('#mainmenu_bg').mouseleave(function () {
				options.autorotate =true;
				options.interval = setInterval(function () { next(); }, options.timer);
			});
			options.headlines = $(options.headlineclass);
			if(options.autorotate)
				options.interval = setInterval(function () { next(); }, options.timer);
			
			initclicks();
				
			return $this;
        };
		
		var initclicks = function () {
			var tempindex = 0;
			options.circlemarkers.each(function () {
				$(this).bind('click',{index: tempindex}, function (event) {
					removeinitclicks();
					goto(event.data.index);
					return false;
				});
				tempindex++;
			});
			
			$('#next').bind('click', function () {  
				next(); 
				return false;
			});
			$('#previous').bind('click', function () {  
				previous();  
				return false;
			});
			return false;
		};
		
		var removeinitclicks = function () {
			options.circlemarkers.each(function () {
				$(this).unbind('click');
			});
			
			$('#next').unbind('click');
			$('#previous').unbind('click');
			return false;
		};

		
		var goto = function (i)
		{
			$(options.headlines[options.index]).fadeOut('fast', function () {
				options.index = i;
				setmarker();
				$(options.headlines[i]).fadeIn('fast', function () { initclicks(); } );	
			});	
		};
		
		var previous = function () {
			removeinitclicks();
			$(options.headlines[options.index]).fadeOut('fast', function () {
				options.index--;
				if(options.index < 0)
					options.index = 3;
				setmarker();
				initclicks();	
				$(options.headlines[options.index]).fadeIn('fast');	
			});
			return false;	
		};
		
		var next = function() {
			removeinitclicks();
			$(options.headlines[options.index]).fadeOut('fast', function () {
				options.index++;
				if(options.index > 3)
					options.index = 0;
				setmarker();	
				initclicks();
				$(options.headlines[options.index]).fadeIn('fast');	
			});
			return false;
        };	
		
		var setmarker = function() {
			
			options.circlemarkers.each(function () {
				$(this).removeClass('circleon');
				$(this).addClass('circle');
			});
			$(options.circlemarkers[options.index]).addClass('circleon');
			return false;	
		};
			
        return intialize();
  };
  
  
  
})(jQuery);


function Carousel(id, options){
	this.ul = jQuery(id);
	this.index = null;
	this.lis = null;
	this.timer = null;

	var self = this;
	
	this.init = function(){
		jQuery('.carousel_btns').css('z-index','3');
		this.index = 0;
		if(!this.ul.is("ul"))
			this.ul = jQuery(id+" ul");
		this.lis = this.ul.children();
		//hide all li
		this.lis.each(function(index) {
			jQuery(this).css('position', 'absolute');
			//except for first
			if(index != 0)
			{

				jQuery(this).css('z-index', '2');
				jQuery(this).hide();
				jQuery(this).css('left', options['width']);
				jQuery(this).css('left', '0');
				jQuery(this).fadeOut();
			}
			else
			{
				jQuery(this).css('z-index', '1');
				jQuery(this).css('left', '0');
				jQuery(this).fadeIn();
			}
		  });
		//if horizontal - set overflow and with
		if(options['direction'] == 'horizontal')
		{
			this.ul.css('overflow', 'hidden');
			this.ul.css('width', options['width']*2);
			this.ul.css('height', options['height']);
		}
		
		if(options['autorotate'] == true)
		{
			this.autorotate(true);
			this.ul.mouseover(function() {
			  self.autorotate(false);
			});	
			this.ul.mouseout(function() {
			  self.autorotate(true);
			});	
		}
		jQuery(this.lis[0]).css('left', 0);
	};
	

	this.autorotate = function (flag)
	{
		
		if(flag)
			this.timer = setInterval( function () {
					self.scrollTo(self.index+1);
					//alert(self.index);
				}, options['slideduration']*1000);
		else
			clearInterval(this.timer)
	};
		
	this.scrollTo = function(int){	
		
		if(self.index != int)
		{
			
			//carousellinks[2].css('background-color', '#ff0000');
			if(int > self.lis.length-1)
				int = 0;
			var current = jQuery(self.lis[self.index]);
			
			current.css('left', '0');
			
			current.animate({ 
				opacity: 0,
				left: -options['width']
			  }, options['effectduration']*1000, function() {
				// Animation complete.
				self.index = int;
				current.css('left', options['width'] );
				current.hide();
			  });
			
			
			
			var li_item = jQuery(self.lis[int]);
			li_item.css('z-index', '2');
			current.css('z-index', '1');
			li_item.hide();
			li_item.css('left', options['width'] );
			//li_item.css('left', '0');
			
			
			li_item.show();
			li_item.animate({
				opacity: 1,
				left: 0
			  }, options['effectduration']*1000, function() {
				// Animation complete.
				self.index = int;
				current.css('left', '0' );
				current.hide();	
			  });
		 	/*li_item.fadeIn( options['effectduration']*1000, function (){
				
			  
			  });*/
						
			var carousellinks = jQuery('#rotator a');
			
			carousellinks.each(function(){
				  jQuery(this).removeClass('on');
			  });
			jQuery(carousellinks[int]).addClass('on');
			
			
			clearInterval(this.timer)
			this.timer = setInterval( function () {
					self.scrollTo(parseInt(self.index)+1);
					//alert(self.index);
				}, options['slideduration']*1000);
				
			
		}
	};

	
};

var mainnav = null;
var headline = null;
var hCarousel = null;

$(document).ready(function() {
 // mainnav = $('#mainmenu').mainnav();
  jQuery('ul.sf-menu').superfish({
		autoArrows:  false,
		speed:       'fast',
		delay:         200,    
		dropShadows:   false,               
		animation:      {opacity:'show',height:'show'}
	});

  headline = $('#newsheadline-wrapper').headline();
  
  headline = $('#newsheadline-wrapper').headline();

	
	//these are the basic options - more to be built upon later.
  	hCarousel = new Carousel("#horizontal_carousel", { 
			autorotate: true,
			direction: 'horizontal',
			width: 650,
			height: 350,
			slideduration: 12,
			effectduration: 1.5,
			//,
			//previousButton: '.previous_button',
			//nextButton: '.next_button' 
			});
	hCarousel.init();
	
	  
  $('#customers_carousel').jcarousel({
        wrap: 'circular',
		//auto: 1,
		buttonNextHTML: '<div class="next" id="customer-next">&nbsp;</div>',
		buttonPrevHTML: '<div class="previous" id="customer-previous">&nbsp;</div>',
		scroll: 8,
		visible: 8,
	 	itemFallbackDimension: 100
  });
 	
	$('.jcarousel-item').each( function () { 
		$(this).css('width', '120px');
	});
  
});





