/**
 * @section	: Unobtrusive JavaScript events and function calls, triggered on DOMContentLoaded.
 * @project	: Topform
 * @author	: Boye Oomens <boye@e-sites.nl>
 * @since	: 20-12-2010
 */

$(doc).ready(function () {

	/* Apply pngfix for MSIE6 and below */
	if (!window.XMLHttpRequest && typeof DD_belatedPNG !== 'undefined'){
		DD_belatedPNG.fix('.spr, #footer, .icon, .homepage img, #content, .subNav .active, .name, .productNavBtn, .productTitle, .productOverview h2');
	}

	// Print functionality
	$('a.print', 'div#main').bind('click', function () {
		window.print();
		return false;
	});

	// Navigation dropdown
	$('> li:has(.subs):not(.collection, .active)', 'ol#nav').hoverIntent(nav.hover, nav.out);

	// Collection dropdown (it differs from the default one)
	$('li.collection', 'ol#nav').hoverIntent(nav.collectionHover, nav.collectionOut);

	$('a.mainItem, div.subs, ol.horizontal a', 'ol#nav').hover(function () {
		$('div.subs', 'ol#nav li.active').css('display', 'block');
	});

	/* Opens a new window for anchors with rel=external */
	$('a[rel=external]').bind('click', setExtLinks);

	// Init placeholder script
	$('input, textarea', 'form').placeholder();

	// Init autofocus
	$('input[autofocus]').autofocus();

	// Footer fix
	if ( !isset('bottomFooter') ) {
		bottomAlignFooter();
		$(win).bind('resize', bottomAlignFooter);
	} else {
		$('body').css('height', $(doc).height() + 'px');
	}

	// Form validation
	$('form.dealerLookup:not(#dynamicDealerSearch)').chkFrm();
	$('form.newsletterSignup').chkFrm();
	$('form#contact').chkFrm({
		footMsg: $('#errorFootMsg').val(),
		errorMessages: false,
		onBeforeValidate: function () {
			callback.initPreloader();
		},
		onFail: function () {
			this.getForm().find(':input.error').eq(0).focus();
		},
		onSuccess: callback.sendContactForm
	});
	// Dynamic dealer search
	$('form#dynamicDealerSearch').chkFrm({
		onBeforeValidate: function () {
			callback.initPreloader();
		},
		onSuccess: callback.dealerSearch
	});

	// Asynchronous loading AddThis button
	if ( $('div.addthis_toolbox').length && window.XMLHttpRequest ) {
		initAsyncAddthis();
	}

	// Enlarge thumbs
	$('a.enlarge', 'div.productThumbs').bind('click', enlargeThumb);

	// Filter functionality
	$('select#sort', 'form.sortForm').bind('change keyup', function () {
		if ( this.value !== '' ) {
			win.location = '/collectie/' + this.value + '-' + this.options[this.selectedIndex].text + '.html';
		}
	});
});
