/**
 * @section	: Global JavaScript functions
 * @project	: Topform
 * @author	: Boye Oomens <boye@e-sites.nl>
 * @since	: 20-12-2010
 */

/**
 * Cache both window and document object for use later on
 */
var win = window,
	doc = win.document,
	ie = (function () {
	    var undef,
	        v = 3,
	        div = document.createElement('div'),
	        all = div.getElementsByTagName('i');

	    while (
	        div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
	        all[0]
	    );

	    return v > 4 ? v : undef;
	}());

/**
 * Handles external links based on rel="external"
 * @author Boye Oomens <boye@e-sites.nl>
 * @param none
 * @return {Boolean}
 */
function setExtLinks() {
	this.target = '_blank';
}

/**
 * Helper function as alias for getElementById, mainly used to see if a certain DOM element exists
 * @author Boye Oomens <boye@e-sites.nl>
 * @param {String} id - id selector without the hash character
 * @return {Boolean}
 */
function isset(id) {
	return !!doc.getElementById(id);
}

/**
 * Function that expands the main footer if necessary
 *
 * @author Boye Oomens <boye@e-sites.nl>
 * @param none
 * @return none
 */
function bottomAlignFooter() {

	var winheight = $(win).height(),
		wrapperHeight = $('div#wrapper').height(),
		ft = $('div#footer'),
		ftHeight = ft.height();

	if ( wrapperHeight < winheight ) {
		$('div#footer').css('height', (ftHeight + (winheight - wrapperHeight)) + 'px');
	}
}

/**
 * HTML5 autofocus plugin
 * Copyright (c) 2009, Mike Taylor, http://miketaylr.com
 * MIT Licensed: http://www.opensource.org/licenses/mit-license.php
 * @param {Object} $
 */
(function ($) {
  $.fn.autofocus = function () {
    // Test to see if autofocus is natively supported before proceeding
    return (this.first().autofocus!==true) ? this.focus() : this;
  };
})(jQuery);

/**
 * Loading aSync AddThis buttons
 * @author Joris van Summeren <joris@e-sites.nl>
 * @param none
 * @return {Boolean}
 */
function initAsyncAddthis() {
	var self = this,
		username = 'topform',
		//gaProfile = 'UA-XXXXXX-XX',
		times = 40;

	addthis_config = {
		username: username,
		services_compact: 'email,facebook,twitter,hyves,print,favorites,blogger,google,delicious,netvibes,hotmail,gmail,bitly,more',
		data_ga_tracker: null,
		data_track_clickback: true
	};

	addthis_share = {
		templates: { twitter: '{{title}} {{url}} @topform' }
	};

//	_gaq.push(function () {
//		addthis_config.data_ga_tracker = _gat._getTracker(gaProfile);
//	});

	this.init = function () {
		if (typeof addthis === 'object') {
			addthis.init();
		} else {
			if (!times) return;

			setTimeout(self.init, 25);
			times--;
		}
	}

	$.getScript('http://s7.addthis.com/js/250/addthis_widget.js#domready=1&async=1&username=' + username, self.init);
}

/**
 * Small function which simply sets the src attribute of the given container when clicked on a thumb
 * @author Boye Oomens <boye@e-sites.nl>
 * @return {Boolean} false - Stops following the actual thumb URL
 */
function enlargeThumb() {

    /* Set vars */
    var newSrc = this.href,
        container = $('img', 'div.productImg');

    /* Adjust the src attribute */
    if ( newSrc !== '' ) {
        container[0].src = newSrc;
    }

    return false;
}

/**
 * Module that holds all navigation functionality
 * @author Boye Oomens <boye@e-sites.nl>
 * @type {Object}
 */
var nav = {
	hover: function () {
		var self = this;
		clearTimeout($(this).data('timer'));
		// Hide all arrows
		$('span.arrow').animate({bottom:'-21px'}, {duration:200, queue:false});
		$('div.subs', self).stop().slideDown('fast', function () {
			$('span.arrow', self).eq(0).animate({bottom:'-37px'}, {duration:200, queue:false});
		});
	},
	out: function () {
		var self = this;
		var t = setTimeout(function () {
			$('span.arrow', 'ol#nav li.active').eq(0).animate({bottom:'-37px'}, {duration:200, queue:false});
			$('div.subs', self).stop().slideUp('fast');
			$('span.arrow', self).eq(0).animate({bottom:'-21px'}, {duration:200, queue:false});
		}, 1800);
		$(this).data('timer', t);
	},
	collectionHover: function () {
		var self = this;
		clearTimeout($(this).data('timer'));
		$('div.subs', 'ol#nav').slideUp('fast');
		$('span.arrow').animate({bottom:'-21px'}, {duration:200, queue:false});
		$('div#overlay').fadeTo('fast', 0.5, function () {
			$('ul.collectionNav', self).stop().fadeIn('fast', function () {
				$('span.topArrow', self).show();
			});
		});
	},
	collectionOut: function () {
		var self = this;
		var t = setTimeout(function () {
			$('div.subs', 'ol#nav li.active').slideDown('fast');
			$('span.arrow', 'ol#nav li.active').eq(0).animate({bottom:'-37px'}, {duration:200, queue:false});
			$('ul.collectionNav', self).stop().fadeOut('fast');
			$('span.topArrow', self).hide();
			$('div#overlay').fadeOut('fast', function () {
				$(this).removeAttr('style');
			});
		}, 700);
		$(this).data('timer', t);
	},
	clearTimer: function () {
		clearTimeout( $(this).parents('li').data('timer') );
	}
};


/**
 * Module that holds all callback functionality related to chkFrm
 * @author Boye Oomens <boye@e-sites.nl>
 * @type {Object}
 */
var callback = {

	initPreloader: function () {

		$('div#loader')
			.ajaxStart(function () {
				$(this).fadeIn('fast');
			})
			.ajaxStop(function () {
				$(this).fadeOut('fast');
			});
	},

	dealerSearch: function () {

		var self = this,
			ajaxHandle = $.post('/php/dealerSearch.php', self.getFormData() + '&ajax=true');

		ajaxHandle.success( function(data) {
			if (data !== '') {
				$('div#searchResults').html(data);
				bottomAlignFooter();
			}
		});
	},

	sendContactForm: function () {

		var self = this,
			ajaxHandle = $.post('/contact.html?ajax=true', self.getFormData() + '&ajax=true');

		ajaxHandle.success( function(data) {
			if (data !== '') {
				$('div#main').html(data);
				bottomAlignFooter();
			}
		});
	}

};

