//---------------------------------------------------------
//
//  General javascript file for 't Maanfabriekje
//
//  Authors:	Davy De Pauw (davy@marlon.be)
//				Gerrit Bertier (gerrit@marlon.be)
//
//---------------------------------------------------------

/**
 * On DOM ready
 */
$(document).ready(function()
{
	// General javascript interaction
	general.init();

	// Lay-out changes
	layout.init();

	// Init search box
	search.init();

	// Forms
	forms.init();

    // Staf
    staf.init();

    // Basket
    basket.init();

	// Init picture box functions (if needed)
	if ($('.product-pictures').html() != null)
	{
		pictures.init();
	}

	// Do Cufon replacement (removes IE6 delay)
	Cufon.now();
});

/**
 * General JS functions
 */
var general =
{
	/**
	 * Initialize
	 */
	init: function()
	{
		// Set hoverables
		this.hoverable();

		// Set indicator for required fields
		this.setRequiredIndicators();

		// Form on page?
		if($('.form').length > 0) forms.init();
	},

	/**
	 * Set hoverables.
	 *
	 * @author Davy De Pauw
	 */
	hoverable: function()
	{
		var hoverIn = function() { $(this).addClass('hover'); };
		var hoverOut = function() { $(this).removeClass('hover'); }

		// Generic hoverable function
		if($('ul.hoverable').length > 0) $('.hoverable li').hover(hoverIn, hoverOut);

		// Generic hoverable function
		if ($('tr.hoverable').length > 0)
		{
			// Add hover behaviour
			$('tr.hoverable').hover(hoverIn, hoverOut);

			// Listen for TR click
			$('tr.hoverable').bind("click", function()
			{
				// Get URL
				var url = $(this).find('a').attr('href');

				// Redirect
				window.location = url;
			});
		}
	},

	/**
	 * setRequiredIndicators adds * to elements which have a class="required"
	 *
	 * @author Davy De Pauw
	 */

	setRequiredIndicators: function()
	{
		$('.required label').append('<span class="indicator">*</span>');
	}
}

/**
 * Layout specific functions
 */
var layout =
{
	/**
	 * Initialize
	 */
	init: function()
	{
		// Replace fonts with cufon where needed
		this.replaceFonts();

		// Set min height
		this.setMinHeight();
		
		// Graphic assistant: add wrapping div
		if ($('.graphic-assistant').length > 0)
		{
			$('.graphic-assistant').wrap('<div></div>');
		}
		
		// Set shop assistant minimum height
		if ($('.shop-assistant').length > 0) 
		{
			this.setShopAssistantMinHeight();
		}
	},

	/**
	 * replaceFonts replace all texts marked for replacement (class="cufon-replace") with cufon text
	 *
	 * @author Gerrit Bertier
	 */
	replaceFonts: function()
	{
		// Add 'change-font' class to all elements replaced with cufon, so the font-size adjusts to the size needed for cufon
		$('.cufon-replace').addClass('change-font');

		// Replace the marked elements
		Cufon.replace('.cufon-replace');
	},

	/**
	 * set minimum content height
	 *
	 * @author Gerrit Bertier
	 */
	setMinHeight: function()
	{
		if ($('#content').height() < $('#aside').height())
		{
			$('#content').height($('#aside').height());
		}
	},
	
	/**
	 * Set minimum shop assistant height (and equal height).
	 *
	 * @author Gerrit Bertier
	 */
	setShopAssistantMinHeight: function()
	{
		// Get all heights
		var s1h = $('.step1 .step-wrapper').height();
		var s2h = $('.step2 .step-wrapper').height();
		var s3h = $('.step3 .step-wrapper').height();
		var maxH = 0;
		
		// Check for highest container
		if (s1h >= s2h)
		{
			maxH = s1h;
		}
		else
		{
			maxH = s2h;
		}
		
		if (maxH < s3h)
		{
			maxH = s3h;
		}
		
		// Set minimum height (if needed)
		if (maxH < 120) { maxH = 120; }
		
		// Make divideable by 60 (for background repeat)
		var mod = maxH % 60;
		
		if (mod > 0) 
		{
			maxH = maxH - mod + 60;
		}
		
		// Set heights
		$('.step1').height(maxH);
		$('.step2').height(maxH);
		$('.step3').height(maxH);
		
		$('.step-wrapper').height(maxH);
		$('.step-content').height(maxH - 20); // 20px padding-top
	}
}

/**
 * Forms
 */
var forms =
{
	init: function()
	{
		// All normal inputs
		$('dd .field').focus(function(e) {
			// Assign focus class to dd parent
			if ($(this).attr('class').indexOf('nofocus') == -1) 
			{
				$(this).parent().addClass('focus');
			}
		});

		$('dd .field').blur(function(e) {
			// Remove focus class form dd parent
			if ($(this).attr('class').indexOf('nofocus') == -1) 
			{
				$(this).parent().removeClass('focus');
			}
		});

		// Radiobuttons
		$('dd label .radio').focus(function(e) {
			// Assign focus class to dd parent
			if ($(this).attr('class').indexOf('nofocus') == -1)
			{
				$(this).parent().parent().addClass('focus');
			}
		});

		$('dd label .radio').blur(function(e) {
			// Remove focus class form dd parent
			if ($(this).attr('class').indexOf('nofocus') == -1)
			{
				$(this).parent().parent().removeClass('focus');
			}
		});

		// Checkboxes
		$('dd label .check').focus(function(e) {
			// Assign focus class to dd parent
			if ($(this).attr('class').indexOf('nofocus') == -1) 
			{
				$(this).parent().parent().addClass('focus');
			}
		});

		$('dd label .check').blur(function(e) {
			// Remove focus class form dd parent
			if ($(this).attr('class').indexOf('nofocus') == -1) 
			{
				$(this).parent().parent().removeClass('focus');
			}
		});
	}
}

/**
 * Pictures on product detail
 */
var pictures =
{
	/**
	 * Init
	 */
	init: function()
	{
		// Get img
		var img = $('#product-image');

		// Get picture array
		var pics = $('.product-pictures ol').find('li a');

		// Define current picture
		var currentPic = 0;

		// Register 'prev' listener
		$('.picture-controls .button-prev').bind('click', function(e)
		{
			// Set class
			$('.product-pictures').addClass('product-picture-loading');

			// Change picture index
			if (currentPic > 0)
			{
				currentPic--;
			}
			else if (currentPic == 0)
			{
				currentPic = pics.length - 1;
			}

			img.load(function()
			{
				// Remove class
				$('.product-pictures').removeClass('product-picture-loading');
			});

			// Set image source
			img.attr('src', pics[currentPic]);
		});

		// Register 'next' listener
		$('.picture-controls .button-next').bind('click', function(e)
		{
			// Change picture index
			if (currentPic < pics.length - 1)
			{
				currentPic++;
			}
			else if (currentPic == pics.length - 1)
			{
				currentPic = 0;
			}

			// Set image source
			img.attr('src', pics[currentPic]);
		});
	}
}

/**
 * Search specific functions
 */
var search =
{
	init: function()
	{
		$searchbox = $('#txt-search-keyword');

		if ($searchbox.length > 0)
		{
			// Extract the label
			var initialValue = $searchbox.val();

			// Create temp array to store the initial value of the searchbox
			var object = new Array();
			object['initialValue'] = initialValue;

			// Function to call the process action
			var func = function(e)
			{
				search.handleEvent(e, object)
			};

			// Attach listener for initialValue change
			$searchbox.focus(func);
			$searchbox.blur(func);
		}

		$searchbox.trigger('blur');

        $('#cbo-search-brand').change(function(e){
            if($(this).val()) document.location.href = $(this).classData('url') + "/" + $(this).val();
        });
	},

	handleEvent: function(e, obj)
	{
		// Which type of event is fired?
		switch (e.type)
		{
			case 'focus':
				// Hide the initialValue
				if (e.target.value == obj.initialValue)
				{
					e.target.value = '';
					$(e.target).removeClass('inactive');
				}
				break;

			case 'blur':
				// Show the initialValue
				if (e.target.value == '')
				{
					e.target.value = obj.initialValue;
					$(e.target).addClass('inactive');
				}
				break;
		}
	}
}

/**
 * Send to a friend specific JS
 */
var staf =
{
    init: function()
    {
        if($('#message'))
        {
            $('#message').focus(function(e){
                $('#rbt-text-3').attr('checked', 'checked');
            });
        }
    }
}


/**
 * Basket specific JS
 */
var basket =
{
    init: function()
    {
    	/*
         $('#basketitems .ico-add').each(function(i){
                // click page working title
                $(this).click(function(e){
					quantity = parseInt(document.getElementById("basketitem"+this.rel).value);
					document.getElementById("basketitem"+this.rel).value = quantity + 1;
                });
          });
         $('#basketitems .ico-subtract').each(function(i){
                // click page working title
                $(this).click(function(e){
					quantity = parseInt(document.getElementById("basketitem"+this.rel).value);
					if (quantity > 0){
						document.getElementById("basketitem"+this.rel).value = quantity - 1;
					}
                });
          });
		*/
    }
}