/**
 * @author davy
 */

$(document).ready(function() {
	// General javascript interaction
	general.init();
});

var general = {
	init: function()
	{		
		this.hoverable();
		this.setMinHeight();

		// Set indicator for required fields
		this.setRequiredIndicators();
		
		// Product detail page?
		if($('.product #photos').length > 0) product.initPhotos();
		
		// Form on page?
		if($('.form').length > 0) forms.init();
		
		// Content table?
		if ($('.content table').length > 0) tables.init();

		//checkout basket?
		//if($('#basketplaceholder').length > 0) checkoutbasket.init()
	},
	
	hoverable: function()
	{
		var hoverIn = function() { $(this).addClass('hover'); };
		var hoverOut = function() { $(this).removeClass('hover'); }
				
		// Generic hoverable function
		if($('.hoverable').length > 0) $('.hoverable li').hover(hoverIn, hoverOut);		
		// Hover over product thumbnails (in productlist)
		if($('.products .thumb').length > 0) $('.products .thumb').hover(hoverIn, hoverOut);
		// Hover over buttons
		//if($('div.button').length > 0) $('div.button').hover(hoverIn, hoverOut);
	},
	
	/**
	 * Set the minimum height of main div depending on the height of the absolutely positioned subnav
	 * 	 
	 * @author Davy De Pauw
	 */
	
	setMinHeight: function() {
		if($('#aside').length > 0) {
			$('#main').css('min-height', ($('#aside').outerHeight() + 70) + 'px');
		}
		
		if($('.product #photos').length > 0) {
			$('.product').css('min-height', ($('#photos').outerHeight()) + 'px');			
		}
	},
	
	/**
	 * setRequiredIndicators adds * to elements which have a class="required"
	 * 
	 * @author Davy De Pauw
	 */
	
	setRequiredIndicators: function() {
		$('.required label').append('<span class="indicator">*</span>');
	}	
}

var forms = {
	init: function() {
		$('dd .field').focus(function(e) {
			// Assign focus class to dd parent
			$(this).parent().parent().addClass('focus');
		});

		$('dd .field').blur(function(e) {
			// Remove focus class form dd parent
			$(this).parent().parent().removeClass('focus');
		});

		if ($('.photoupload').length > 0) {
			$('.photoupload').change(function() {
				var val = $(this).val();
				$('#fakeinput').attr('value', val);
			});
		}

		// Initialize datapicker(s) or masked input
		if ($(".datepicker").length > 0) {
			/*
			$(".datepicker").datepicker({
				showOtherMonths: true,
				firstDay: 1,
				dateFormat: 'dd/mm/yy',
				showOn: "both",
				buttonImage: "/0.0.1/img/ico_calendar.png",
				buttonImageOnly: true,
				duration: '',
				maxDate: '-1y',
				yearRange: '1900:2009'
			});
			*/

			$(".datepicker").mask("99/99/9999");
		}
		
		if ($("dd.expandable .help").length > 0) {
			$("dd.expandable .help").each(function(i)
			{
				$(this).parent().css('margin-bottom',(($(this).height() - $(this).parent().height() + 12) + 'px'));
			});
		}
	}
}

var tables = {
	init: function()
	{
		$('.content table tr:even').addClass('even');
		$('.content table td:first').addClass('first');
		$('.content table th:first').addClass('first');
	}
}

var product = {
	initPhotos: function()
	{
	    $('#photos ol li a').each(function(i)
	    {
	        $(this).hover(function(e)
	        {
				$('#photos ol li').className = '';
	
	            $(this).parent().get(0).className = "selected";
	
	            // Set src of #photo to href of clicked item
	            $('#photo').attr('src', $(this).attr('href'));
	        }, function(e)
	        {
	            //Do nothing, stay there
	        });
	
	        $(this).click(function(e)
	        {
	            e.preventDefault();
	
				$('#photos ol li').className = '';
	
	            $(this).parent().get(0).className = "selected";
	
	            // Set src of #photo to href of clicked item
	            $('#photo').attr('src', $(this).attr('href'));
	
	        });
	    });
	
	},
	
	resetProductPhotosActiveStates: function() {
		// Get all li items in specified ol
		$('#photos ol li').className = '';
	}
}

var checkoutbasket = { init: function() {
      
    }
}



/**
 * Listboxes are recurring site components in which products are displayed
 * 
 * @author Davy De Pauw
 */

var listbox = {
	init: function()
	{
		
	}
}
