var currentTooltip = false;
var previousShowBigger = false;

$(function() {

	$(".get-media,.hide-media").click(function() {
    
    $(this).parent().find('.showed-media').slideToggle("fast",
    
      function(){ 
        if (this.style.display != 'none'){
          $(this).parent().find('.hide-media').css('display', 'block');
          $(this).parent().find('.get-media').css('display', 'none');
        }
        else{
          $(this).parent().find('.hide-media').css('display', 'none');
          $(this).parent().find('.get-media').css('display', 'block');        
        }
      }
    
    );
    });
    
	// Open link in new window
	$(".external").attr("target","_blank");
	
	$("input").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
	
	// No border on proceslist in right column
	$('ul.proces:last').css('border','none');
	
	// Flatscreen filter on searchresults
  $("#flat_fltr").hide();
	$("#flat_fltr_toggle").click(function(){
    $("#flat_fltr").show();
    return false;
	});
	
		$('.media-right img, .media-left img').each(function(){
		width = this.width + 2;
		$(this.parentNode.parentNode).css('width', width + 'px');
		
		this.onload = function(){
			width = this.width + 2;
			$(this.parentNode.parentNode).css('width', width + 'px');
		}
	});
		

if ($('#slides').size() > 0) {
$('#slides').slides({
	preload: true,
	pause: 2500,
	hoverPause: true
});
}

  
  // Productconfigurator functions
  $('#productconfig li form').hide();
  $('#productconfig li .config_a').click(function() {
    $(this).hide();
    $(this).next('form').show();
  });
  $('#productconfig li form a').click(function() {
    $(this).parents('form').hide();
    $(this).parents('li').find('.config_a').show();
    return false;
  });
  
   // Productconfigurator functions
  $('#prod2-form').hide();
  $('.show-prod2').click(function() {
    $('#prod2-form').show();
  });
  
  $('.hide-prod2').click(function() {
    $('#prod2-form').hide();
  });
  
  // Userphoto functions
  $('#change_photo').hide();
  $('#gochange_photo').click(function() {
    $(this).hide();
    $('#change_photo').show();
    return false;
  });
  
  $('#change_photo a').click(function() {
    $('#gochange_photo').show();
    $(this).parents('#change_photo').hide();
    return false;
  });

  // Add more link & function to productlist
  $('.product-overview ul').each(function() {
    $(this).children('li:gt(4)').hide();
    if ($(this).children('li:gt(4)').length > 0) {
      $(this).append('<li class="more">Meer...</li>');
    }
  });
  
  $('.more').toggle(function(){
      $(this).prevAll('li').show();
      $(this).text('Minder...');
    },function(){
      $(this).parent().children('li:gt(4):not(.more)').hide();
      $(this).text('Meer...');
  });
  
  if($('#equalize').equalHeights) 
  $('#equalize').equalHeights(true);
	
	$(".hide-info, #frm-download-hidden").hide()
	$(".more-info, .hide-info, .close").click(function() {
	
		if ($(this).hasClass('more-info')){
			$(this).removeClass('more-info').addClass('hide-info');
			$(this).children('a').hide();
			$(this).children('.hide-more-info').show();
		}
		else{
			$(this).removeClass('hide-info').addClass('more-info');
			$(this).children('a').show();
			$(this).children('.hide-more-info').hide();
		}
		$("#more-text, #frm-download-hidden").slideToggle("slow");
	});
	
	// Hover and click the brandlist
	$(".news-column, .table-download tr td").click(function(){
		window.location=$(this).find("a").attr("href");return false;
	});
	$(".news-column, .table-download tr").hover(function(){
		$(this).addClass('over-pointer'); // Plaats een class op een hover
	},function(){
	$(this).removeClass('over-pointer'); // Verwijder de class op een mouseout
	});

	
	// Stripe
	$(".detail-table li:nth-child(even), .table-detail tr:nth-child(even)").addClass("striped");
	$(".page-options li:nth-child(even)").addClass("print-page");
	$(".compare tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
	$(".compare tr:nth-child(even), .table-download tr:nth-child(even)").addClass("stripe-table");
	
	$(".refine dd:last-child").addClass("bullet-refine");
	$(".address-list ul li:first-child").addClass("block-address");
	$(".address-list ul li:nth-child(2)").addClass("prev-address");
	$("#news-column dl dd:last-child").addClass("all-news");
	
	// Hide tooltip
	$(".tooltip, .hover-tip").click(function() {
		node = this;
		do{
			cn = node.className;
		}
		while(!cn.match(/tooltip(\d+)/) && (node=node.parentNode));
		
		tooltipID = cn.match(/tooltip(\d+)/);
		if (tooltipID){
			tooltipID = tooltipID[1];
			
			$('.hover-tip.tooltip' + tooltipID + ', .tooltip.tooltip' + tooltipID).animate({ opacity: 'hide' }, "slow")
		
		}
		else{
			$(".tooltip, .hover-tip").animate({ opacity: 'hide' }, "slow")
		}
    });
    
    // Show tooltip
    $(".show-tooltip").click(function() {
		if (tooltipID = this.className.match(/tooltip(\d+)/)){
			if (currentTooltip){
				$('.tooltip.tooltip' + currentTooltip).animate({ opacity: 'hide' }, "slow")
			}
			currentTooltip = tooltipID = tooltipID[1] * 1;
			$('.tooltip.tooltip' + tooltipID).animate({ opacity: 'show' }, "slow")
		}
    });

	// Show tooltip
    $(".hover-tooltip").hover(function() {
		if (tooltipID = this.className.match(/tooltip(\d+)/)){

			tooltipID = tooltipID[1] * 1;
			$('.hover-tip.tooltip' + tooltipID ).animate({ opacity: 'show' }, "slow")
		}
    }, function(){});	
	
	window.bind_click = function(){
		// remove ALL click handlers - including PrettyPhoto.click()
		$('.show-bigger').unbind('click');
	
		// Show 2nd size image in bigger image box
		// allow 3rd size image to be shown in prettyphoto box.
		$('.show-bigger').click(function(){
			relSplit = this.rel.split(/\s/);
			
			image_id = relSplit[1];
			image_url = relSplit[2];
			
			$('#' + image_id).attr('src', image_url);
			$('#' + image_id).attr('alt', this.childNodes[1].alt);
			$('#' + image_id).parent().attr('href', this.href);

			return false;
		});
	}
	window.bind_click();
	
	$(function() {
	$("a[rel^='prettyPhoto']").prettyPhoto();

        $('a[rel=prettyVideo]').click(function () {
            $.prettyPhoto.open(this.href, this.title, '');
            return false;
        });
	
	// disable prettyphoto for the small images...
	if (window.bind_click)
		window.bind_click();
		
});
	
	
	// PNG fix for IE
	$('img[src$=.png],ul#sub-tabs li a, .home h2, .play-video, ul#innerfade_list li.current a, ul#random_images li span, #random_images dt, .product-overview h2 a').ifixpng();
        
        
        // Carousel
        var carbusy = false;
        $('#carousel .btnnext, #carousel .btnprev').click(function () {
            if (carbusy) return false;
            var width = 880;
            var total = $('#carousel .wrapper ul li').size() * 220;
            var minleft = ($('#carousel .wrapper ul li').size() - 4) * 220 * -1;
            var left = $('#carousel .wrapper ul').css('width', total).position().left;
            if (left % 220 != 0) {
                left = Math.round(left / 220) * 220; // To compensate for IE8 bug
            }
            var goNext = $(this).hasClass('btnnext');
            var newleft = goNext ? left - 220 : left + 220;
            if (!goNext && newleft <= 0) {
                carbusy = true;
                $('#carousel .wrapper ul').animate({'left': newleft+'px'}, function () { carbusy = false; });
            }
            if (goNext && newleft >= minleft) {
                carbusy = true;
                $('#carousel .wrapper ul').animate({'left': newleft+'px'}, function () { carbusy = false; });
            }
            if (newleft == 0) {
                $('#carousel .btnprev').addClass('inactive');
                $('#carousel .btnnext').removeClass('inactive');
            } else if (newleft - width <= total * -1) {
                $('#carousel .btnnext').addClass('inactive');
                if (newleft < 0) {
                    $('#carousel .btnprev').removeClass('inactive');
                }
            } else if (newleft < 0) {
                $('#carousel .btnprev').removeClass('inactive');
                $('#carousel .btnnext').removeClass('inactive');
            }
        });
	$('.service .select-language a').click(function () {
		$('#lang-down').toggle();
		return false;
	});
	$('body').click(function () {
		$('#lang-down').hide();
	});
	
	// Dropdown FSF brand
	$('.ddown a').click(function () {
		if ($(this).hasClass('disabled')) return false;
		var index = $('.ddown a').index($(this));
		$('.dropdown').eq(index).toggle();
		return false;
	});
	$('body').click(function () {
		$('.dropdown').hide();
	});
	$('body').keyup(function (event) {
	    if (event.which >= 65 && event.which <= 90) { // between a - z
    	    var dd = $('.dropdown:visible');
    	    if (dd.size() > 0) {
    	        dd.find('li').each(function () {
    	            if ($(this).text().charCodeAt(0) == event.which) {
    	                var top = dd.find('li').index($(this)) * $(this).height();
    	                dd[0].scrollTop = top;
    	                return false;
    	            }
    	        });
    	    }
	    }
	});
	
	// Caroussel slider
	$('#slides .slide:first').addClass('active');
	$('#slides .slide:not(.active)').hide();
	var timeout = setTimeout('sliderNext(true)', 3000);
	$('#slides').data('auto', true).data('timeout', timeout);
	$('#slides a.next, #slides a.prev').click(function () {
    	if ($('#slides').data('auto')) {
            $('#slides').data('auto', false);
            clearTimeout($('#slides').data('timeout'));
    	}
	    sliderNext($(this).hasClass('next'));
	    return false;
	});
});

function sliderNext(next) {
    var count = $('#slides .slide').size();
    var index = $('#slides .slide').index($('#slides .slide.active'));
    if (next) {
        index = index == (count - 1) ? 0 : index + 1;
    } else {
        index = index == 0 ? count - 1 : index - 1;
    }
    $('#slides .slide.active').removeClass('active').fadeOut();
    $('#slides .slide').eq(index).addClass('active').fadeIn();
    if ($('#slides').data('auto')) {
    	var timeout = setTimeout('sliderNext(true)', 3000);
    	$('#slides').data('timeout', timeout);
    }
}
