function eq_heights() {
    var elems = $(arguments);
    var top_height = 0;
    for (var i = 0; i < arguments.length; i++) {
        var obj = $(arguments[i]);
        obj.each(function () {
            var height = $(this).height();
            if (height > top_height) {
                top_height = height;
            }
        });
    }
    for (var i = 0; i < arguments.length; i++) {
       $(arguments[i]).height(top_height);
    }
}

/* jQuery plugin for Input hints (R. L. Bekkema, 9 Nov 2011) */
(function ($) {
    $.fn.hint = function (hint_text, hint_class) {
        if (!hint_class) var hint_class = 'hint';
        var doHint = function () {
            if ($(this).val() == '' || $(this).val() == hint_text) {
                $(this).val(hint_text).addClass(hint_class);
            }
        }
        var undoHint = function () {
            if ($(this).hasClass(hint_class)) {
                $(this).removeClass(hint_class).val('');
            }
        }
        this.each(doHint);
        this.blur(doHint);
        this.focus(undoHint);
        // Also clear the hint when submitted
        this.each(function () {
            var input = this;
            $(this.form).submit(function () {
                undoHint.call(input);
            });
        });
    }
})(jQuery);
    
function fillStars() {
    $('.star-bg, .star-bg-small, .star-bg-big').each(function () {
        var front_class, width;
        if ($(this).hasClass('star-bg-small')) {
            front_class = '.star-front-small';
            width = 67;
        }
        else if ($(this).hasClass('star-bg')) {
            front_class = '.star-front';
            width = 100;
        }
        else if ($(this).hasClass('star-bg-big')) {
            front_class = '.star-front-big';
            width = 97;
        }
        var front = $(this).children(front_class);
        var rat = Math.round(front.text() * 2) / 2;
        var width = (width / 5) * rat;
        front.css('width', width);
    });
}

$(window.document).ready(function () {
	if ($.browser.msie) {
	    if ($.browser.version.substr(0,1) < 7) {
	        $('body').addClass('ie6');
	    } else if ($.browser.version.substr(0,1) < 8) {
	        $('body').addClass('ie7');
	    } else if ($.browser.version.substr(0,1) < 9) {
	        $('body').addClass('ie8');
	    } else {
	        $('body').addClass('ie9');
	    }
    }
    fillStars();
    $('#tab_main_5').click(fillStars);
    
    $('.garage_search_left input[name=postcode]').hint('Postcode of plaats');
    
    // Call me back
    $('#callme-form').submit(function () {
        var phone = $.trim($(this).find('input[name=phone]').val());
        if (phone) {
            var opts = {'phone': phone};
            $.get(site_root+'ajax/callmeback.php', opts, function () {
                $('#callme-form').replaceWith('<p>Hartelijk dank voor het invoeren van uw telefoonnummer. U wordt binnen 15 minuten teruggebeld.</p>');
            });
        }
    });
    
    // Inline APK check
    $('.apk-check').submit(function () {
        var form = $(this);
        form.children('.apk-info').remove();
        var licence = $.trim($(this).find('input[type=text]').val());
        if (licence) {
            $(this).find('[type=submit]').hide().after('<img class="loader" src="'+site_root+'images/loadingAnimation.gif" alt="Laden" />');
        
            var opts = {'licence': licence};
            $.get(site_root+'ajax/apkcheck.php', opts, function (data) {
                form.find('[type=submit]').show().next('.loader').remove();
                var data = eval('('+data+')');
                if (data.html) {
                    var div = $('<div class="apk-info" />').append(data.html).hide();
                    div.append('<p><a class="more-info" href="javascript:;">Een ander kenteken proberen</a></p>');
                    div.find('.more-info').click(function () {
                        div.fadeOut();
                    });
                    form.append(div);
                    div.fadeIn();
                }
            });
        }
        return false;
    });

$(".actie:nth-child(3n)").css("margin-right","0");
eq_heights('.actie');

$("a[rel=external]").each(function(i){
  $(this).addClass("external");
  //this.target="_blank";
  
  $("#add_car").hide();
	 
	 $("#add_car_cta").click(function() {
	   $("#add_car").show;
	   this.hide();
	 })
	 
});

    if ($('.apkcheck_old').size()) {
        var default_licence = 'XX-XX-XX';
        $('.apkcheck_old .input_text').val(default_licence);
        $('.apkcheck_old .input_text').focus(function () {
            if ($(this).val() == default_licence) {
                $(this).val('');
            }
        }).blur(function () {
            if ($(this).val() == '') {
                $(this).val(default_licence);
            }
        });
    }



    if ($('.options_list dd label').tooltip != undefined) {
    	$('.options_list dd label').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        showBody: " - ",
        fade: 250
    	});

    	$('.options_list dd input').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        showBody: " - ",
        fade: 250
    	});
    }

	if (capt = document.getElementById('capcha-li')){
		capt.style.display = 'none';
		var inp = document.createElement('input');
		inp.type = 'hidden';
		inp.name = 'capcha-bypass';
		inp.value = '1234';
		capt.appendChild(inp);
	}

            var searchtext_apk = "";

    $(".input_apk").each(function (e) {
        if ($(this).val() == '') {
            $(this).val(searchtext_apk);
        }
    });

  $(".input_apk").focus(function () {
    if ($(this).val() == searchtext_apk) {
      $(this).val('');
    }
  });

  $(".input_apk").blur(function () {
    if ($(this).val() == '') {
      $(this).val(searchtext_apk);
    }
  });
});

/*
 * jQuery ifixpng plugin
 * (previously known as pngfix)
 * Version 2.0  (04/11/2007)
 * @requires jQuery v1.1.3 or above
 *
 * Examples at: http://jquery.khurshid.com
 * Copyright (c) 2007 Kush M.
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 */

 /**
  *
  * @example
  *
  * optional if location of pixel.gif if different to default which is images/pixel.gif
  * $.ifixpng('media/pixel.gif');
  *
  * $('img[@src$=.png], #panel').ifixpng();
  *
  * @apply hack to all png images and #panel which icluded png img in its css
  *
  * @name ifixpng
  * @type jQuery
  * @cat Plugins/Image
  * @return jQuery
  * @author jQuery Community
  */

(function($) {

	/**
	 * helper variables and function
	 */
	$.ifixpng = function(customPixel) {
		$.ifixpng.pixel = customPixel;
	};

	$.ifixpng.getPixel = function() {
		return $.ifixpng.pixel || 'images/pixel.gif';
	};

	var hack = {
		ltie7  : $.browser.msie && $.browser.version < 7,
		filter : function(src) {
			return "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src='"+src+"')";
		}
	};

	/**
	 * Applies ie png hack to selected dom elements
	 *
	 * $('img[@src$=.png]').ifixpng();
	 * @desc apply hack to all images with png extensions
	 *
	 * $('#panel, img[@src$=.png]').ifixpng();
	 * @desc apply hack to element #panel and all images with png extensions
	 *
	 * @name ifixpng
	 */

	$.fn.ifixpng = hack.ltie7 ? function() {
    	return this.each(function() {
			var $$ = $(this);
			var base = $('base').attr('href'); // need to use this in case you are using rewriting urls
			if ($$.is('img') || $$.is('input')) { // hack image tags present in dom
				if ($$.attr('src')) {
					if ($$.attr('src').match(/.*\.png([?].*)?$/i)) { // make sure it is png image
						// use source tag value if set
						var source = (base && $$.attr('src').substring(0,1)!='/') ? base + $$.attr('src') : $$.attr('src');
						// apply filter
						$$.css({filter:hack.filter(source), width:$$.width(), height:$$.height()})
						  .attr({src:$.ifixpng.getPixel()})
						  .positionFix();
					}
				}
			} else { // hack png css properties present inside css
				var image = $$.css('backgroundImage');
				if (image.match(/^url\(["']?(.*\.png([?].*)?)["']?\)$/i)) {
					image = RegExp.$1;
					$$.css({backgroundImage:'none', filter:hack.filter(image)})
					  .children().children().positionFix();
				}
			}
		});
	} : function() {return this;};

	/**
	 * Removes any png hack that may have been applied previously
	 *
	 * $('img[@src$=.png]').iunfixpng();
	 * @desc revert hack on all images with png extensions
	 *
	 * $('#panel, img[@src$=.png]').iunfixpng();
	 * @desc revert hack on element #panel and all images with png extensions
	 *
	 * @name iunfixpng
	 */

	$.fn.iunfixpng = hack.ltie7 ? function() {
    	return this.each(function() {
			var $$ = $(this);
			var src = $$.css('filter');
			if (src.match(/src=["']?(.*\.png([?].*)?)["']?/i)) { // get img source from filter
				src = RegExp.$1;
				if ($$.is('img') || $$.is('input')) {
					$$.attr({src:src}).css({filter:''});
				} else {
					$$.css({filter:'', background:'url('+src+')'});
				}
			}
		});
	} : function() {return this;};

	/**
	 * positions selected item relatively
	 */

	$.fn.positionFix = function() {
		return this.each(function() {
			var $$ = $(this);
			var position = $$.css('position');
			if (position != 'absolute' && position != 'relative') {
				$$.css({position:'relative'});
			}
		});
	};

})(jQuery);


function formatLicence(licence) {
    var licence = licence.replace('-', '');

    var result = '';
    // Gewoon kenteken (vb. df45hj)
    if (licence.match(/([a-z]{2}|[0-9]{2})([a-z]{2}|[0-9]{2})([a-z]{2}|[0-9]{2})/i)) {
        result = licence.substr(0, 2)+'-'+licence.substr(2, 2)+'-'+licence.substr(4, 2);
    }
    // Nieuw kenteken (vb. 4fds56)
    else if (licence.match(/[0-9]{1}[a-z]{3}[0-9]{2}/i)) {
        result = licence.substr(0, 1)+'-'+licence.substr(1, 3)+'-'+licence.substr(4, 2);
    }
    // 45dfs3
    else if (licence.match(/[0-9]{2}[a-z]{3}[0-9]{1}/i)) {
        result = licence.substr(0, 2)+'-'+licence.substr(2, 3)+'-'+licence.substr(5, 1);
    }
    return result;
}

$(document).ready(function () {
    if ($.browser.msie && $.browser.version.substring(0,1) === '6') {
        $('a.enquete').css({'position': 'absolute'});
        $(window).scroll(positionEnquete);
        positionEnquete();
    }
});

function positionEnquete() {
    var vw = $(window).width();
    var vh = $(window).height() + $(window).scrollTop();
    var enq = $('a.enquete');
    enq.css({'left': (vw - enq.width())+'px', 'top': (vh - enq.height())+'px'});
}
