/**
 * main navigation
 */
var MainNav = {
		
	isOver : false,
	curSubMenu : null,
	curLink : null,
	
	init: function()
	{
		// mouseover - event
	
		$( '#mainNav a' ).mouseover( function(){			
			// only elements of main-menu
			if( $( this ).next().length ) 
			{
				// hide visible menu
				if( MainNav.curLink )
				{
					MainNav.curLink.removeClass( 'hover' );
					MainNav.curSubMenu.hide();
				}
				
				MainNav.isOver = true;
				MainNav.curSubMenu = $( this ).next();
				MainNav.curLink = $( this );
				
				MainNav.curLink.addClass( 'hover' );
				MainNav.curSubMenu.show();
			}			
		});	
		
		$( '.jsSubMenu' ).mouseover( function(){			
			MainNav.isOver = true;			
		});	
		
		// mouseout - event
		
		$( '#mainNav a' ).mouseout( function(){			
			// only elements of main-menu
			if( $( this ).next().length ) 
			{
				MainNav.isOver = false;
				window.setTimeout( 'MainNav.delayMouseout()', 1000 );
			}			
		});
		
		$( '.jsSubMenu' ).mouseout( function(){					
			MainNav.isOver = false;
			window.setTimeout( 'MainNav.delayMouseout()', 1000 );						
		});
	},
	
	delayMouseout: function()
	{
		if( ! MainNav.isOver )
		{
			MainNav.hideMenu();
		}
	},
	
	hideMenu: function()
	{	
		if( MainNav.curLink )
		{
			MainNav.curLink.removeClass( 'hover' );
			MainNav.curSubMenu.hide();
		}
		
		MainNav.curSubMenu = null;
		MainNav.curLink = null;			
	}
};

/**
 * product navigation
 */
var ProductNav = {
		
	isOver : false,
	curSubMenu : null,
	curLink : null,
	
	init: function()
	{
		// mouseover - event
	
		$( '.productNav a' ).mouseover( function(){
			// only elements of main-menu
			if( $( this ).next().length ) 
			{
				// hide visible menu
				if( ProductNav.curLink )
				{
					ProductNav.curLink.removeClass( 'hover' );
					ProductNav.curSubMenu.hide();
				}
				
				ProductNav.isOver = true;
				ProductNav.curSubMenu = $( this ).next();
				ProductNav.curLink = $( this );
				
				ProductNav.curLink.addClass( 'hover' );
				ProductNav.curSubMenu.show();
			}			
		});	
		
		$( '.jsSubMenu' ).mouseover( function(){			
			ProductNav.isOver = true;			
		});	
		
		// mouseout - event
		
		$( '.productNav a' ).mouseout( function(){			
			// only elements of main-menu
			if( $( this ).next().length ) 
			{
				ProductNav.isOver = false;
				window.setTimeout( 'ProductNav.delayMouseout()', 1000 );
			}			
		});
		
		$( '.jsSubMenu' ).mouseout( function(){					
			ProductNav.isOver = false;
			window.setTimeout( 'ProductNav.delayMouseout()', 1000 );						
		});
	},
	
	delayMouseout: function()
	{
		if( ! ProductNav.isOver )
		{
			ProductNav.hideMenu();
		}
	},
	
	hideMenu: function()
	{	
		if( ProductNav.curLink )
		{
			ProductNav.curLink.removeClass( 'hover' );
			ProductNav.curSubMenu.hide();
		}
		
		ProductNav.curSubMenu = null;
		ProductNav.curLink = null;			
	}
};

/**
 * contactform on detailspage
 */

var ContactManager = {
		
	init: function()
	{
		// show contactform
		$( '.tx-mmsddaskproducts-pi1 .contact a.submit' ).click(function(){			
			$( this ).parents( '.el' ).find( '.contactLayer' ).show();				
		});
		
		// hide contactform
		$( '.tx-mmsddaskproducts-pi1 .contactLayer a.layerClose' ).click(function(){			
			$( this ).parents( '.el' ).find( '.contactLayer' ).hide();						
		});
	},

	sendForm: function( element )
	{			
		form = element.parents( '.el' ).find( '.contactLayer form' );		
		url = form.find( 'input.url' ).val();
		vals = form.find('.tippable');
		$.each(vals,function(i,val){
			if(vals[i].value == vals[i].title)
				vals[i].value="";
		});
		$.ajax({ 
			url: url,	
			data: form.serialize(),
			cache: false,
			type: 'POST',
			dataType: 'html',
			success: function( res ){
				
	        	form.html( res );				
				Tippables.init();
	      	}
		});
	}	
};

/**
 * toggle of category container
 */

var CategoryToggle = {
		
	init: function()
	{
		$( '.tx-mmsddaskproducts-pi1 .el h3' ).click(function(){			
			$( this ).next().toggle();
			$( this ).toggleClass("active");
		});
	}	
};

/**
 * hover effect on Worldmap
 */

var HoverWorldmap = {
    
  init: function()
  {
  //to clear the gover on browser back button
  $('div#nomaphilight area').stop();
  $('div#nomaphilight area').unbind('mouseenter').unbind('mouseleave');
  if($('div#mouseoverimages a.World').html()=='empty'){
    $('div#mouseoverimages a.World').html($('#worldmap').attr('src'));
  }
  $('div#nomaphilight area').hover(function() {
    var currentId = $(this).attr('class').split(/\b/)[0];
  //set the links
    $('div#nomaphilight area.'+currentId).attr('href',$('div#mouseoverimages a.'+currentId).attr('href'));
    $('#worldmap').attr('src',$('div#mouseoverimages a.'+currentId).html());
}, function() {
  $('#worldmap').attr('src',$('div#mouseoverimages a.World').html());
});
  $("div#maphilight img#worldmap").maphilight();
  $("div#nomaphilight map#Global area").cluetip({clickThrough:true, width: 125, splitTitle:'---', waitImage:false, hoverIntent:false, sticky:false, closeText:'',mouseOutClose:true,dropShadow:false,showTitle: false});
  $("div#maphilight map#Global area").cluetip({width: 225,leftOffset:-6,topOffset:-6,splitTitle:'---', waitImage:false, hoverIntent:false, sticky:true, closeText:'',mouseOutClose:true,dropShadow:false,showTitle: false}); 
  } 
};

/** 
 * product switcher
 */
var SwitchProductview = {
		
		init: function(){},
		
		showTable: function(){
			$( '.tx-mmsddaskproducts-pi1 .products .tableView' ).show();
			$( '.tx-mmsddaskproducts-pi1 .products ul.productsView a.table' ).addClass('active');
			$( '.tx-mmsddaskproducts-pi1 .products .listView' ).hide();
			$( '.tx-mmsddaskproducts-pi1 .products ul.productsView a.list' ).addClass('inactive');
		},
		
		showList: function(){
			$( '.tx-mmsddaskproducts-pi1 .products .listView' ).show();
			$( '.tx-mmsddaskproducts-pi1 .products ul.productsView a.table' ).removeClass('active');
			$( '.tx-mmsddaskproducts-pi1 .products .tableView' ).hide();
			$( '.tx-mmsddaskproducts-pi1 .products ul.productsView a.list' ).removeClass('inactive');
		}
};

/**
 * news slider 
 */

var NewsSlider = {
		
	navLeft: null,
	navRight: null,
	count: null,
	index: 0,
	duration: 250,
	fontSize: 12,
	infoEl: null,
	intervall:null,
		
	init: function()
	{	
		NewsSlider.count = $(".news-latest-items ul li").length;
		NewsSlider.infoEl = $(".news-latest-navInfo span");
		NewsSlider.navLeft = $(".news-latest-nav .navLeft");
		NewsSlider.navRight = $(".news-latest-nav .navRight");
		
		var width = ( NewsSlider.count * 10 ) / NewsSlider.fontSize;
		NewsSlider.infoEl.css({'width':width + 'em'});	
		NewsSlider.infoEl.parent().show();
		
		NewsSlider.intervall = window.setInterval( 'NewsSlider.move( 1 )', 5000 );		
	},
	
	move: function( dir )
	{		
		if( dir == 1 && ( NewsSlider.index + 1 ) < NewsSlider.count )
			NewsSlider.moveRight();
		
		if( dir == -1 && NewsSlider.index > 0 )
			NewsSlider.moveLeft();
	},
	
	moveRight: function()
	{
		NewsSlider.navLeft.removeClass( 'navLeft_inactive' );
		
		$(".news-latest-items ul").animate({
	        marginLeft: "-=290px"
	    }, NewsSlider.duration );
		
		NewsSlider.index++;		
		NewsSlider.calcInfoBG( 1 );
		
		if( ( NewsSlider.index + 1 ) ==  NewsSlider.count )
			NewsSlider.navRight.addClass( 'navRight_inactive' );
		else
			NewsSlider.navRight.removeClass( 'navRight_inactive' );
			
	},
	
	moveLeft: function()
	{
		NewsSlider.navRight.removeClass( 'navRight_inactive' );
		
		$(".news-latest-items ul").animate({
	        marginLeft: "+=290px"
	    }, NewsSlider.duration );
		
		NewsSlider.index--;
		NewsSlider.calcInfoBG( -1 );
		
		if( NewsSlider.index == 0 )
			NewsSlider.navLeft.addClass( 'navLeft_inactive' );
		else
			NewsSlider.navLeft.removeClass( 'navLeft_inactive' );
	},
	
	calcInfoBG: function( dir )
	{
		bgPos = 0;
		
		if( $.browser.msie )
		{
			bgPos = NewsSlider.infoEl.css( 'background-position-x' );
		}
		else
		{
			bgPos = NewsSlider.infoEl.css( 'background-position' );
			bgPos = bgPos.split( ' ' )[ 0 ];			
		}
		
		bgPos = bgPos.replace( 'px', '' );
		bgPos = ( dir == -1 ) ? Number( bgPos ) - 10 : Number( bgPos ) + 10;
		NewsSlider.infoEl.css({'background-position' : bgPos + 'px 0'});
	}
};

var SearchOptions = {
		
	formular: null,
	url: null,
	selGroup : null,
	selCat: null,
	selContact: null,
		
	init: function()
	{
		SearchOptions.formular = $( '#productSearch form' );
		SearchOptions.url = SearchOptions.formular.find( 'input.url' ).val();
		SearchOptions.selGroup = $( '#selGroup' );
		SearchOptions.selCat = $( '#selCat' );
		SearchOptions.selContact = $( '#selContact' );
		
		if( SearchOptions.formular.length )
			SearchOptions.getOptions();
	},	
	
	getOptions: function()
	{	
		$.ajax({ 
			url: SearchOptions.url,
			data: SearchOptions.formular.serialize(),
			cache: false,
			type: 'POST',
			dataType: 'json',
			success: function( res ){
				SearchOptions.renderOptions( res );
	      	}
		});
	},
	
	renderOptions: function( result )
	{
		SearchOptions.selGroup.html( result.group );
		SearchOptions.selCat.html( result.cat );
		SearchOptions.selContact.html( result.contact );
	}	
};

/**
 * main application for global functions
 */
var Application = {
		
	init: function()
	{
		NewsSlider.init();
		MainNav.init();
		ProductNav.init();
		ContactManager.init();
		CategoryToggle.init();
		SwitchProductview.init();
		Tippables.init();
		SearchOptions.init();
		HoverWorldmap.init();
	}	
};

window.onload = function(){ Application.init() };
