// init onload
$( document ).ready( init );

function init()
{
	fancybox();
	$('.main-navigation > li').megadropdown();

	if ( $('body').hasClass('layout_2') ) {
		$().ready(function() {
			$('#coda-slider').codaSlider({
				autoSlide: true,
				autoSlideInterval: 5000,
				dynamicArrows: false,
				dynamicTabs: false
			});
		});
	}
}

// remove onfocus blur
$( function() 
{
	$( 'a' ).attr( 'onfocus', 'if(this.blur)this.blur()' );
} );


function fancybox()
{
	$( 'a.fancybox' ).fancybox();
}

(function( $ ){
	$.fn.megadropdown = function(){
		
		var dropdowns = new Array();
		var active = -1;
		  
		return this.each( function() {
			var $this = $(this);
			$this.internalid = dropdowns.length;
			dropdowns.push($this);
			
			var startpos =  $this.position().left;
			var offset = 870 - (Math.round($this.width()/2));
			$this.children('div.dropdown').hide();
			
			$this.children('div.dropdown').css('left',(startpos-20)+'px');
			$this.children('div.dropdown').css('min-width',($this.width()+40)+'px');
			$this.children('div.dropdown').children('div.navigmarker').css('backgroundPosition','-'+(offset-20)+'px 0px');
			
			$this.children('a').mouseenter( function(){
				if ($this.open != true) {
					$this.children('div.dropdown').fadeIn();
					$this.open = true;
					if(active != -1){
						dropdowns[active].hidedropdown();
					}
					active = $this.internalid;
					//$this.children('div.dropdown').children('div.navigdropdown').children('ul').css('left',$this.position().left+'px');
				}
				//$('#language-navigation').html('MOUSEIN');
				$this.mousein = true;
			}).mouseleave( function(){
				$this.mousein = false;
				//$('#language-navigation').html('MOUSEOUT');
				setTimeout(function() {
        			$this.hidedropdown() ;
     			},800);
			});
			
			$this.children('div.dropdown').mouseenter( function(){
				$this.mousein = true;
				//$('#language-navigation').html('MOUSEIN');
			}).mouseleave( function(){
				//$('#language-navigation').html('MOUSEOUT');
				$this.mousein = false;
				setTimeout(function() {
        			$this.hidedropdown() ;
     			},800);
			});
			
			$this.hidedropdown = function(){
				if($this.mousein != true)
				{
					$this.children('div.dropdown').fadeOut('fast');
					$this.open = false;
					if(active == $this.internalid){
						active = -1;
					}
				}
			};
		});	
	};
})( jQuery );
