$(document).ready(function(){

	$('#typeselector').mouseout(function(){
		$(this).oneTime(500, function() {
		  $(this).hide();
		});
		$(this).mouseover(function(){
		  $(this).stopTime();
		});
	});	

	$('#typetitle').click(function(){
		$('#emneselector').hide();
	});
	
	$('#typeselector li').click(function(){
		selectthis($(this),'typetitle','typeselector','typevalue');
		
		$('#emnevalue').attr('value','')
		$('#emnetitle').html('Alle')

		$.ajax({
		  url: '/ajrgfx/ajax/emner.asp?parentlemmaid='+$(this).attr('value'),
		  success: function(data) {
		    $('#emneselector').html(data);
		  }
		});		

	});

	$('#emneselector').mouseout(function(){
		$(this).oneTime(500, function() {
		  $(this).hide();
		});
		$(this).mouseover(function(){
		  $(this).stopTime();
		});
	});	

	$('#emnetitle').click(function(){
		$('#typeselector').hide();
	});
	
	$('#emnecontainer li.emne').click(function(){
		selectthis($(this),'emnetitle','emneselector','emnevalue');
	});
	

	$('.mytab').click(function(){
	
		if( $(this).attr('class') != 'mytab tabselected') {
		
			window.clearTimeout(timeout); 
			
			$('.tabselected').css( 'backgroundImage','url(/AjrGfx/gfx/bg_quote_center.png)' )
			$(this).css( 'backgroundImage','url(/AjrGfx/gfx/bg_quote_selected_center.png)' )
		

			$('.tabselected').find('div.tabhoveron').switchClass('tabhoveron','tabhoveroff',0)
			$(this).find('div.tabhoveroff').switchClass('tabhoveroff','tabhoveron',0 );
			
			$('.tabselected').switchClass('tabselected','tab',0);
			$(this).switchClass('tab','tabselected',0 );
			 
			$('#imagenumber0').fadeOut();
			$('#imagenumber1').fadeOut();
			$('#imagenumber2').fadeOut();
			$('#imagenumber3').fadeOut();
			$('#'+$(this).attr('rel')).fadeIn();
		
		}

	});

	$("#productslider").jcarousel({
	    scroll: 4,
	    wrap: 'circular',
        animation: 500,
	    initCallback: mycarousel_initCallback,
	    // This tells jCarousel NOT to autobuild prev/next buttons
	    buttonNextHTML: null,
	    buttonPrevHTML: null
	});
	
	// do not remove-----------------------------------------------
	$(document).find('.scrambleemail').each(function(){
		scrambleEmail($(this));
	});
	
	$(".partners img").hover(
	  function () {
	    $(this).attr("src",$(this).attr("src").replace('off','on'));
	  },
	  function () {
	    $(this).attr("src",$(this).attr("src").replace('on','off'));
	  }
	);
	
	if($('.fpLargeQuoteBox').html()){
		startrotation();
	}
	
});

function toggle(id){
	$('#'+id).toggle();
}

function selectthis(valueobj,id,ddid,valueid){
	$('#'+id).html(valueobj.html())
	$('#'+valueid).attr('value',valueobj.attr('value'))
	toggle(ddid);
}

var rotationindex;
rotationindex = 1;
var timeout;

function startrotation() {
	timeout = setTimeout(function() {
	
		if(rotationindex==4){rotationindex=0}
	
		var me = $('#tab'+rotationindex)
		
		$('.tabselected').css( 'backgroundImage','url(/AjrGfx/gfx/bg_quote_center.png)' )
		me.css( 'backgroundImage','url(/AjrGfx/gfx/bg_quote_selected_center.png)' )
		

		$('.tabselected').find('div.tabhoveron').switchClass('tabhoveron','tabhoveroff',0)
		$('.tabselected').switchClass('tabselected','tab',500)
		
		me.find('div.tabhoveroff').switchClass('tabhoveroff','tabhoveron',0 );
		me.switchClass('tab','tabselected',500 );
		 
		$('#imagenumber0').fadeOut();
		$('#imagenumber1').fadeOut();
		$('#imagenumber2').fadeOut();
		$('#imagenumber3').fadeOut();
		$('#'+me.attr('rel')).fadeIn();
		
		rotationindex += 1
		
		startrotation();
		
	}, 3500 );
}


function mycarousel_initCallback(carousel) {

    jQuery('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });
 
    jQuery('#mycarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });
 
    jQuery('#mycarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
    
};

function checkcontactform(me){

	resetfieldclasses(me);
	
	if (me.name.value==''){redalert(me.name); return false;}
	if (!emailCheck(me.email.value)){redalert(me.email); return false;}
	if (me.message.value==''){redalert(me.message); return false;}
	
	me.submit();

}

function resetfieldclasses(me){
	for ( var i=0;i<me.elements.length;i++ )
	{
		if(me.elements[i].name != ''){
			if( me.elements[i].type=='text' )
			{
			me.elements[i].style.border='0';
			}
		}		
	}
}

function redalert(me){
	me.style.border='0';
	me.style.borderBottom='1px dashed red';
	me.focus();
}

function setSelectedIndex( me, valsearch )
{
  for (i = 0; i< me.options.length; i++)
  {
    if (me.options[i].value == valsearch)
    {
      me.options[i].selected = true;
      me.options[i].style.backgroundColor = '#dddddd';
      break;
    }
  }
  return;
}

function scrambleEmail(string){

	var emailarray = string.html().split('|');
	var account = emailarray[0];
	var domain = emailarray[1];
	var subject = '';
	
	if(emailarray[2]){subject=emailarray[2]};
	
	var email = account + '@' + domain;
	
	string.html('<a href="mailto:'+email+'?subject='+subject+'">'+email+'</a>');

}
