/**
 * jquery-custom.js
 *
 * Custom, additional javascript and jQuery plugins
 *
 * @version						1.0.0
 * @date						yyyy-mm-dd
 * @lastmodified				yyyy-mm-dd hh:mm
 * @author						Studio Dumbar (Jonathan van Wunnik)
 * @project						Project Name
 * @link						http://www.website.com
 *
**/


Modernizr._fontfaceready(function(bool){
  setTimeout(function(){ document.documentElement.className += ' fontfaceready'; }, 10);
});

/*
 * MR -> 29-03-2010
 * To manage the compatibility between mootools and jQuery
 */
(function($){
$.extend($.fn, {
	handleMethod: function(){
	    var method = $(this).attr('class').match(/method:(\w+)/);
	    if (!method) return false;
	    var formId = $(this).attr('class').match(/form:(\w+)/);
		var form = !formId ? $(this).parents('form:first') : $('#' + formId[1]);
		form.find('input[name=method]').val(method[1]);
	    form.trigger('submit', this);
	    return true;
	}
});

$(document).ready( function() {

/**
 * Smooth scroll
 *
 * @note						smooth scrolling to anchor in page
**/
// live() instead of bind() for handling also links added later (eg: ajax update on recalculate form)
$('.content a[href*=#],.content-add a[href*=#]').click(function() {
	if ($(this).handleMethod()) return false;

    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
        var $target = $(this.hash);
        $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
        if ($target.length) {
            var targetOffset = $target.offset().top;
            $('html,body').animate({scrollTop: targetOffset}, 1000);
            return false;
        }
    }
});


/**
 * FAQ
 *
 * @section						FAQ ('veelgestelde vragen'); expand and collapse functionality
**/

$('.faq dt').click(function() { $(this).toggleClass('open').next('dd').toggleClass('open'); });
$('.faq dt').hover(function() { $(this).toggleClass('hover'); });


/**
 * MINE
 *
 * @section						MINE ('Mijn Allsecur'); expand and collapse functionality
**/



$('.mine').each(function(){
	
	$('> ul > li > h3 > a', this).click(function() { 
		var  li = $(this).closest('li');
		li.toggleClass('open');
	});
	
	/* open on hash */
	if (document.location.hash) $('#'+document.location.hash.replace('#','_')).addClass('open');
	
	/* make area clickable in insurance overview */
	$('tr.more-info', this).each(function(){
		var a = $('a', this);
		var tbody = $(this).closest('tbody');
		var thead = tbody.prev('thead');
		tbody.add(thead).hover(function(){
			tbody.add(thead).addClass('hover');
		},function(){
			tbody.add(thead).removeClass('hover');
		}).click(function(e){
			if (!$(e.target).closest('tr.buttons').length && a.attr('href')) document.location.href = a.attr('href'); 
		});
	});
});




/**
 * Premium info
 *
 * @section						Premium ('hoofd- en aanvullende dekkingen') information, expand and collapse functionality (question mark)
**/
/*
 * MR -> 2010-04-13
 * 
 */
window.updatePremiumList = function($){
	
	$('.premium dt,.premium-list .info').each(function() { $(this).append('<a href="#" title="Meer informatie" class="info-more"><span>Meer informatie</span></a>'); });
	
	$('.premium dt .info-more').click(function() {
		
		if($(this).parents('dt').hasClass('checked')) 
		{
			$(this).parent().toggleClass('open').next('dd').addClass('checked open');
		}
		else
		{
			$(this).parent().toggleClass('open').next('dd').toggleClass('open');
		}
		
		return false;
	});
	
	$('.premium-list .info-more').click(function() {
		$(this).parent().toggleClass('open');
		return false;
	});
	
};

window.updatePremiumList($);

window.updateNavForm = function($){
	$('.nav-form a[href*=#]').click(function() {
		if ($(this).handleMethod()) return false;
	});
};

/**
 * Premium selection
 *
 * @section						Premium ('hoofd- en aanvullende dekkingen') selection, toggle
**/

// init styles based on checkbox values
$('.main :radio, .additional :checkbox').each(function(){
	var checked = $(this).attr('checked');
	var disabled = $(this).attr('disabled');

	$(this).parents('dt').removeClass('checked').addClass(checked?'checked':'').next().removeClass('checked').addClass(checked?'checked':'');
	$(this).parents('dt').removeClass('disabled').addClass(disabled?'disabled':'').next().removeClass('disabled').addClass(disabled?'disabled':'');
});

$('.main label').click(function() {
	if ( !$(this).parent().hasClass('disabled') ) {
		$('dl.main dt, dl.main dd').removeClass('checked');
		$(this).find('input:first').attr('checked','checked');
		$(this).parent().addClass('checked').next().addClass('checked');
	}
});

$('.additional label').click(function(event) {
	if ( !$(this).parent().hasClass('disabled') ) {
		if ($(':checkbox',this).attr('checked')) {
			$(this).parents('dt').addClass('checked').next().addClass('checked').next('.extra').removeClass('disabled').find(':checkbox').attr('disabled',false);
		}
		else {
			$(this).parents('dt').removeClass('checked').next().removeClass('checked').next('.extra').addClass('disabled').removeClass('checked').find(':checkbox').attr('disabled',true).attr('checked',false).parents('dt').next().removeClass('checked');
		}
	}
});


/**
 * Carousel
 *
 * @section						Carousel functionality on home- and 'mijn allsecur' pages
**/


$('.carousel').each(function(){
	var carousel = this;

	var str = '';
	var c = 0;
	$('li', carousel).each(function(){
		c++;
		str += '<li>'+c+'</li>';
	});
	if (c > 1) {
		str = '<ul class="carousel-indicator">'+str+'</ul>';
		$(carousel).before(str);
		$(carousel).prev('.carousel-indicator').find('li').click(function(){
			carousel.current = $(this).text() - 2;
			showCarouselItem();
		});
	}




	$(carousel).hover(function(){carousel.hovered = true;},function(){carousel.hovered = false;});
	$(carousel).prev('.carousel-indicator').hover(function(){carousel.hovered = true;},function(){carousel.hovered = false;});

	$('li a',carousel).each(function(){
		var link = $(this).attr('href');
		if (link) {
			$(this).parents('li').css({cursor: 'pointer'}).click(function(){
				document.location.href = link;
				return false;
			});
		}
	});


	$('li:not(:first)', carousel).css({opacity: 0, display: 'none'});
	$(carousel).prev('.carousel-indicator').find('li:first').addClass('active');

	carousel.current = 0;
	var carouselPlayer = setInterval(function(){

		if (!carousel.hovered) showCarouselItem();

	},6000);
	function showCarouselItem() {
		carousel.current = carousel.current < $('li',carousel).size() - 1 ? carousel.current + 1 : 0;
		$(carousel).prev('.carousel-indicator').find('li').removeClass('active');
		$(carousel).prev('.carousel-indicator').find('li:eq('+carousel.current+')').addClass('active');
		$('li:eq('+carousel.current+')',carousel).css({zIndex: 2, display: 'block'}).animate({opacity: 1},500,'',function(){
			$('li:not(:eq('+carousel.current+'))',carousel).css({zIndex: 2, opacity: 0, display: 'none'});
			$(this).css({zIndex: 1});
		});
	}
});

$('.same-height').each(function(){
	var minHeight = 0;
	$('.inner > *',this).each(function(){
		minHeight = Math.max(minHeight,$(this).height());
	});
	$('.inner > *', this).height(minHeight);
});

setTimeout(function() {
	$('label.default-value').each(function(){
		var input = $(this).next('input');
		if ($(input).val()=='') $(this).css({display: 'inline'});
		$(input).focus(function(){
			$(this).prev('label').hide();
	
		}).blur(function(){
			if ($(this).val()=='') {
				$(this).prev('label').css({display: 'inline'});
			}
		});
	});
}, 200);

/**
 * Border radius fall-back
 *
 * @section						Check if border-radius is supported (with modernizr.js), if not than add rounded corners with images
**/

if (!Modernizr.borderradius) {
	$('nav li, a.button, .call-to-action').css({position: 'relative'}).append('<span class="c lt"></span><span class="c rt"></span><span class="c lb"></span><span class="c rb"></span>');
	// correction of the odd width/height and positioning with right and bottom
	$('span.c').each(function(){
		if ($(this).parent().width() % 2) {
			$(this).parent().addClass('ie-width');
		}
		if ($(this).parent().height() % 2) {
			$(this).parent().addClass('ie-height');
		}
	});
}


$('.help-trigger').click(function() {
	var $this = $(this);
	var $li = $this.parents('li:first');
	$li.toggleClass('help-open');
	var action = $this.attr('class').match(/scTrackAction:(\w+)/);
	if (action && $li.hasClass('help-open')) sc_trackAction(cmsAS_WApagename, action[1]);
	return false;
});

$('.customer-service-list p').contents().each(function() {
	if (this.nodeType == 3) {
		$(this).wrap('<span />').parent().click(function(e) {
			e.preventDefault();
			location.href = $(this).closest('a').attr('href').replace('#', '?q=' + $.trim($(this).text()).toLowerCase().replace(/\W/g, '-') + '#');
		});
	}
});

});
})(jQuery);

