jQuery.expr[':'].Contains = function(a, i, m) {
  return jQuery(a).text().toUpperCase()
      .indexOf(m[3].toUpperCase()) >= 0;
};
jQuery.expr[':'].contains = function(a, i, m) {
  return jQuery(a).text().toUpperCase()
      .indexOf(m[3].toUpperCase()) >= 0;
};



(function ($) {
$.fn.vAlign = function($align, $type) 
{
	return this.each(function(i)
    {
    	var ah = $(this).height();
    	var ph = $(this).parent().height();
        
        switch ($align)
        {
            case 'top':
                var mh = 0;
            break;
            case 'middle':
                var mh = Math.ceil((ph-ah) / 2);
            break;
            case 'bottom':
                var mh = Math.ceil(ph-ah);
            break;
        }
        switch ($type)
        {
            case 'padding':
                $(this).css('padding-top', mh);
            break;
            case 'margin':
            default:
                $(this).css('margin-top', mh);
            break;
            
        }
        $(this).css('visibility', 'visible');
	});
};
})(jQuery);

(function ($) {
$.fn.hAlign = function($align, $type) 
{
	return this.each(function(i)
    {
    	var aw = $(this).width();
    	var pw = $(this).parent().width();
        
        switch ($align)
        {
            case 'left':
                var mw = 0;
            break;
            case 'center':
                var mw = Math.ceil((pw-aw) / 2);
            break;
            case 'right':
                var mw = Math.ceil(pw-aw);
            break;
        }
        switch ($type)
        {
            case 'padding':
                $(this).css('padding-left', mw);
            break;
            case 'margin':
            default:
                $(this).css('margin-left', mw);
            break;
        }
        $(this).css('visibility', 'visible');
	});
};
})(jQuery);

$(document).ready(function()
{
    if($('#productList').width() < 1000) {
        $('#productList').hAlign('center');   
    }
    
    categories = 'golfbanen,sportparken,landschap,overig';
    $('input[name^=dealerCategory]').change(function() {
        categories = '';
        $.each($('input[name=dealerCategory[]]:checked'), function() {
            categories += $(this).val() + ',';
        });
        categories = categories.substr(0, categories.length - 1);
        if ($('#addressInput').val() == 'Type hier uw postcode') {
            searchLocationsNear(map.getCenter(), "yes", categories);    
        } else {
            searchLocationsNear(map.getCenter(), "no", categories);
        }
    });
    
    $('#clientGroupList li').mouseover(function() {
        $(this).children('.clienstImage').children('p').hide();
    });
    $('#clientGroupList li').mouseout(function() {
        $(this).children('.clienstImage').children('p').show();
    });
    
    $('#productList li').mouseover(function() {
        $(this).children('.clienstImage').children('p').hide();
    });
    $('#productList li').mouseout(function() {
        $(this).children('.clienstImage').children('p').show();
    });
    
    $('.archiefBar').click(function() {
        if($(this).hasClass('open')) {
            $(this).next('.archiefText').slideUp();
            $(this).removeClass('open');
            $('.archiefBar').next('.archiefText').slideUp();
            $('.archiefBar').removeClass('open');
        } else {
            $('.archiefBar').next('.archiefText').slideUp();
            $('.archiefBar').removeClass('open');
            $(this).next('.archiefText').slideDown();
            $(this).addClass('open');
        }
    });
    
    $('.faqBar').click(function() {
        if($(this).hasClass('open')) {
            $(this).next('.faqText').slideUp();
            $(this).removeClass('open');
            $('.faqBar').next('.faqText').slideUp();
            $('.faqBar').removeClass('open');
        } else {
            $('.faqBar').next('.faqText').slideUp();
            $('.faqBar').removeClass('open');
            $(this).next('.faqText').slideDown();
            $(this).addClass('open');
        }
    });
    
    $('#faqSearch').keyup(function() {
        $("#faqs li").not(":contains('" + $(this).val() + "')").hide();
        $("#faqs li:contains('" + $(this).val() + "')").show();
    });
    
    $('#searchDownloads').keyup(function() {
        $("#actueelArchief li").not(":contains('" + $(this).val() + "')").hide();
        $("#actueelArchief li:contains('" + $(this).val() + "')").show();
    });
    
    $('.hasSubmenu').mouseover(function() {
        $(this).children('ul').show();
    });
    
    $('.hasSubmenu').mouseout(function() {
        $(this).children('ul').hide();
    });
    
    $("#mycarousel").jcarousel({
        scroll: 1,
        auto: 5,
        wrap: 'last',
        initCallback: mycarousel_initCallback,
        itemVisibleInCallback: {
            onAfterAnimation: mycarousel_move
        },
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
    /*
	$("a[rel='lightbox']").colorbox({ 
        maxHeight: '90%',
        maxWidth: '90%'
    });
    */
    
    $('a[rel=lightbox]').live('click', function() {
        url = this.href; // this is the url of the element event is triggered from
        $.fn.colorbox({href: url, maxWidth: "80%", maxHeight: "80%"});
        return false;
    });
    
    
    $('.innerfade').innerfade({
		speed: 750,
		timeout: 5000,
		type: 'random'
	});
    
    $(".validateForm").validate({
        errorPlacement: function(error, element) {
			//error.prependTo( element.parent().next() );
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		},
        rules: 
        {
            naam: { NotEqual : '' },
            achternaam: { NotEqual : '' },
            telefoonnummer: { NotEqual : '' },
            onderwerp: { NotEqual : '' },
            vraag: { NotEqual : '' },
            email: { NotEqual : '' },
            'CustomFields[2]': { NotEqual : '' },
            'CustomFields[3]': { NotEqual : '' },
            'CustomFields[4]': { NotEqual : '' }
        }
    });
    
    $.validator.methods.NotEqual = function(value, element) {
        return value != element.defaultValue;
    }; 
    
    $(".clearInput").focus(function(srcc)
    {
        if ($(this).val() == this.defaultValue)
        {
            $(this).val("");
            $(this).css('color', '#999');
        }
    });
    
    $(".clearInput").blur(function()
    {
        if ($(this).val() == "")
        {
            $(this).val(this.defaultValue);
        }
    });
    
    $(".clearInput").blur();
    
    $( "#slider-range" ).slider({
			range: true,
			min: 0,
			max: maxPrice,
			values: [ 0, maxPrice ],
			slide: function( event, ui ) {
				$( "#amount" ).html( "Min: &euro; " + ui.values[ 0 ] + " - Max: &euro; " + ui.values[ 1 ] );
                $('input[name=minPrice]').val(ui.values[ 0 ]);
                $('input[name=maxPrice]').val(ui.values[ 1 ]);
			},
            stop: function(event, ui) {
                occasionSearch();
            }
		});
		$( "#amount" ).html( "Min: &euro; " + $( "#slider-range" ).slider( "values", 0 ) +
			" - Max &euro; " + $( "#slider-range" ).slider( "values", 1 ) );
            
   $('select[name=category]').change(function() {
        occasionSearch();
   });
   
   $('select[name=sort]').change(function() {
        occasionSearch();
   });
   
   $('input[name^=brands]').change(function() {
        occasionSearch();
   });
   
   //setInterval('productCarousel()', 6000);
   
   $('#nextProduct').click(function() {
        $('#subPageInsight').stop().animate({
            scrollLeft: '+=200px'
        }, 800);
   });
   
   $('#previousProduct').click(function() {
        $('#subPageInsight').stop().animate({
            scrollLeft: '-=200px'
        }, 800);
   });
   
   var currentPage = 1;
   
   $('.page').live('click', function() {
        $('.activePage').addClass('page');
        $('.activePage').removeClass('activePage');
        $(this).removeClass('page');
        $(this).addClass('activePage');
        var pageNumber = $(this).attr('id').split('_')[1];
        currentPage = pageNumber;
        $('#subPageInsight').scrollTo((pageNumber * 1000) - 1000, 800);
   });
   
   $('#previousPage').click(function() {
        if (currentPage > 1) {
            $('#page_' + currentPage).addClass('page');
            $('#page_' + currentPage).removeClass('activePage');
            currentPage = parseInt(currentPage) - 1;
            $('#page_' + currentPage).removeClass('page');
            $('#page_' + currentPage).addClass('activePage');
            $('#subPageInsight').scrollTo((currentPage * 1000) - 1000, 800);
        }
   });
   
   $('#nextPage').click(function() {
        if (currentPage != pageCount) {
            $('#page_' + currentPage).addClass('page');
            $('#page_' + currentPage).removeClass('activePage');
            currentPage = parseInt(currentPage) + 1;
            $('#page_' + currentPage).removeClass('page');
            $('#page_' + currentPage).addClass('activePage');
            $('#subPageInsight').scrollTo((currentPage * 1000) - 1000, 800);
        }
   });
   
   $('#searchProductsForm').submit(function() {
        $.post(location.href, {searchProducts: true, searchTerm: $('input[name=searchTerm]').val()}, function(response) {
             $('#searchResults').html(response);
        });
        return false;
   });
   
   $('.logo img').vAlign('middle');
});

function productCarousel()
{
    console.log($('#productList').css('width'));
    if (1000 + parseInt($('#subPageInsight').scrollLeft()) + 'px' == $('#productList').css('width')) {
        $('#previousProduct').click();
    } else {
        $('#nextProduct').click();   
    }
}

function occasionSearch()
{
    $.post(location.href, {filterOccasions: true, idPage: $('input[name=idPage]').val(), sort: $('select[name=sort]').val(), lang: $('input[name=lang]').val(), category: $('select[name=category]').val(), minPrice: $('input[name=minPrice]').val(), maxPrice: $('input[name=maxPrice]').val(), brands: $('input[name^=brands]').serializeArray()}, function(data) {
        $('#occasionOverview').html(data);
    });
}

function reloadCaptcha()
{
	document.getElementById('captcha').src = document.getElementById('captcha').src+ '?' +new Date();
}

function mycarousel_initCallback(carousel) {
    $('.jcarousel-control a').bind('click', function() {
        $('.jcarousel-control a').removeClass('active');
        $(this).addClass('active');
        var temp = $(this).attr('id').split('_');
        
        carousel.scroll($.jcarousel.intval(temp[1]));
        return false;
    });
};

function mycarousel_move(carousel, temp1, temp2, temp3)
{
    $('.control').removeClass('active');
    $('#counter_'+temp2).addClass('active');
}


