jQuery(document).ready(function() {

  $('.png, #gal_top, #gal_bottom, #gal_middle').supersleight({shim: '/theme/images/x.gif'});

	try {
		Cufon.replace('h2');
		Cufon.replace('h3:not(.noCufon)');
		Cufon.replace('h4');
		Cufon.replace('h5');
	  Cufon.now();
	} catch(ex) {}
  
  jQuery('a[rel="external"]').click( function() {
    window.open(jQuery(this).attr('href'));
    return false;
  });
 
  jQuery('#wrapper input:checkbox').checkbox({cls:'jquery-safari-checkbox' ,empty: '/theme/images/empty.png'}); 
  
  jQuery('.config .ckbox img').click( function(){
    if(jQuery(this).parents(".deactivated").length > 0){
      return false;
    }
  });
  
  jQuery(".configSelect #mainOptions input:checkbox, .configDetail #mainOptions input:checkbox").bind("check", function(){
    jQuery('.options').removeClass("deactivated");
    jQuery(this).parent().siblings('.abstand').find('.jquery-safari-checkbox-checked img').click();
    return false;
  });
  
  jQuery(".configSelect #mainOptions input:checkbox").bind("uncheck", function(){
    if(jQuery("#mainOptions .jquery-safari-checkbox-checked").length == 0){
      jQuery(".options").addClass("deactivated");
    }
  });    
  
  jQuery(".configSelect input:checkbox").bind("click", function(){
    jQuery(".configDetail #"+jQuery(this).attr("id")).parent().find("img").click();
  });
  
  jQuery('.configSelect .ckbox img').click( function(){
    if(jQuery(this).parents(".deactivated").length == 0){
      var checkboxNode = jQuery(this).parent().parent();
      var pricetag = checkboxNode.parent().next().find(".amount").text();
      
      var labelTarget;
      if(checkboxNode.parents("#mainOptions").length > 0){
        labelTarget = "#chosenOptions .model";
      } else {
        if(pricetag){
          labelTarget = "#chosenOptions .pricedOptions";
        } else {
          labelTarget = "#furtherOptions";
        }
      }
      
      var label = checkboxNode.parent().next().find("div").html().replace(/<span class=\"amount\">.*span>/gi, "").replace(/\s?›\s?/g, "");
      if(labelTarget == "#furtherOptions"){ 
        label = label.replace(/\s*?<span class=\"yellow\">.*span>/g, "");
        label = label.replace(/\&nbsp\;/g, "");
      }
      
      label = trim(label.replace(/<.*?>/gi, ""));
            
      var currency = pricetag.match(/^.{1,3} /);
    
      if(pricetag.match(/\,.$|\.\d\d\d/)){ decimalpoint = ","; thousandssep = "."; }
      if(pricetag.match(/\..$|\,\d\d\d/)){ decimalpoint = "."; thousandssep = ","; }
    
      if(currency && currency.toString().length >= 1) jQuery('#total .currency').html(currency.toString());
    
      
      if(checkboxNode.hasClass("jquery-safari-checkbox-checked")){
        var buildSum = parseInt(digitsOnly(jQuery('#total .amount').text())) - parseInt(digitsOnly(pricetag));
        var label = trim(label);
        jQuery(labelTarget).text(trim(jQuery(labelTarget).text().replace(label.toString(), "").replace(/^,\s?/, "").replace(/,\s?$/, "")));
      } else {
        var buildSum = parseInt(digitsOnly(jQuery('#total .amount').text())) + parseInt(digitsOnly(pricetag));
        jQuery(labelTarget).append(", "+label);
        jQuery(labelTarget).text(trim(jQuery(labelTarget).text().replace(/^, /, "")));
				jQuery(labelTarget).html(jQuery(labelTarget).html().replace("eco", "<i>eco</i>"));
      }
    
      if(parseInt(buildSum)>=0){
        jQuery('#total .amount').text(addCommas(buildSum, decimalpoint, thousandssep));
        jQuery('#total').show();
      }
     }
  });
  
  jQuery("#requestQuote").click( function(){
    jQuery(".config").toggle();
    jQuery("#accordion").accordion('activate', '#'+jQuery("#requestQuote").attr("rel"));
  });

	jQuery("#formContact").validate();

});

jQuery(function(){
		
	// HORZNAV
	
	var speed = 350;
	var delay = 400;
	var fade = 500;
	var test = 0;
	var alpha = 0;
	
	// hide submenu
	$('#navigation UL li ul').hide();
	
  $('#navigation UL li ul:not(.small) li:not(.li-line, .li-sub-last)').hover(function(){
	      $(this).css("background","url(/theme/images/bg-sub-on.png) no-repeat").addClass("hover");
        $(this).find(".menu-1").css("color","#ffffff");
        $(this).find('.menu-2').css("color","#ffed00");
    },
    function () {
        $(this).css("background","url(/theme/images/bg-sub-off.png) no-repeat").removeClass("hover");
        $(this).find('.menu-1').css("color","#acafb4");
        $(this).find('.menu-2').css("color","#acafb4");
	});
  
  $('#navigation UL li ul.small li:not(.li-line) a').hover(function(){
    $(this).parent(':not(.li-sub-last)').css("background","url(/theme/images/bg-sub-small-on.png) no-repeat");
    $(this).find("a").css("color","#ffed00");
    
    },
    function () {
    $(this).parent(':not(.li-sub-last)').css("background","url(/theme/images/bg-sub-small-off.png) no-repeat");
     $(this).find("a").css("color","#acafb4");
    
	});
  
	$('#navigation UL li a.mainnav').hover(function(){
		
		$('#navigation UL li ul').stop(true, true).hide();
		$('#navigation UL li a').stop(true, true).removeClass('active2');
		
		$(this).parent().children('#navigation UL li ul:not(.actsub)').each(function(){
			$('#navigation UL li ul').removeClass('actsub');
			$(this).parent().children('#navigation UL li ul').addClass('actsub').slideDown(speed);
			});
		
		$(this).parent().children('.actsub').each(function(){
			$(this).show();
			});
								
		clearTimeout(alpha);
		clearTimeout(test);	
	
		}, function(){
				
		alpha = setTimeout("$(this).removeClass('active2')", delay);	
		test = setTimeout("$('.actsub').stop(true, true).hide()", delay);
		});
	
  $('#navigation UL li').hover(function(){
    $(this).find('a.mainnav').css('background',"url(/theme/images/pfeil_gelb.gif) no-repeat scroll center bottom");
  },
  function(){
    $(this).find('a.mainnav').css('background',"none");
  });
  
	$('#navigation UL li ul').hover(function(){
		
		$(this).parent().children('#navigation UL li a').stop(true, true).addClass('active2');						
		clearTimeout(alpha);							
		clearTimeout(test);	
		
		}, function(){
		
		alpha = setTimeout("$('#navigation UL li a').removeClass('active2')", delay);	
		test = setTimeout("$('.actsub').stop(true, true).hide().removeClass('actsub')", delay);
		});
		
	$('#content .clickplus A').hover(
	  function(){ $(this).addClass("active"); },
	  function(){ $(this).removeClass("active"); }
	);
	
	/*// TECHNISCHE DATEN - tablehover
	
	$('.tecdata tbody td, .tecdata tbody th').hover(function(){
		$(this).addClass('readline');
		},function(){
		$(this).removeClass('readline');				
		});*/
	
	});	
function addCommas(someNum)
{
   someNum = someNum.toString();
   while (someNum.match(/^\d\d{3}/))
   {
      someNum = someNum.replace(/(\d)(\d{3}(\.|,|$))/, '$1' + thousandssep + '$2');
      someNum += decimalpoint + '–';
   }
   return someNum;
}

function digitsOnly(string)
{
  return (string.replace(/\D+/g, ""));
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
	
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

