/*!
 * Elektroclub javascript file
 * 
 * Copyright 2010, Marlon BVBA
 * http://www.marlon.be
 *
 * Date: 2010-02-24
 */

// Execute when DOM is ready
$(document).ready(function()
{
	// Initialize
	general.init();
	
	lightbox.init();
});

// General functions & properties
var general =
{
	/**
	 * Initialize.
	 */
	init : function()
	{
		// Enrich RTE generated content
		content.enrich();
		
		// Enrich forms
		forms.enrich();
	}
}

// Content related functions
var content =
{
	/**
	 * Enrich the RTE generated content where possible.
	 */
	enrich : function()
	{
		// The classes on this image
		var classes;
		
        // add css classes to image
        $('.rte p img').each(function(i)
        {
            if(!$(this).hasClass('noborder')){
                $(this).addClass('border align-l');
            }
        });

		// Enrich images (which have a 'border' class)
		$('img.border').each(function(i)
		{
                // Get the classes on this img
                classes = $(this).attr('class');

                // Wrap this
                $(this).wrap('<span class="img-container ' + classes + '"><span></span></span>');

                // Clear classes
                classes = null;
		});

		$.each($('img.mceItem'), function(e) {
			var width = $(this).attr('width');
			var height = $(this).attr('height');
			var id = $(this).attr('alt');
			$(this).parent().parent().replaceWith('<iframe width="' + width + '" height="' + height + '" src="http://www.youtube.com/embed/' + id + '" frameborder="0" allowfullscreen></iframe>');
		});
	}
}


// Form enrichment
var forms = 
{
	/**
	 * Enrich the RTE generated content where possible.
	 */
	enrich: function()
	{
		// Button
		if ($('a.submit').length > 0)
		{
			$('a.submit').click(function(e)
			{
				// Submit form
				$(this).closest('form').submit();
			});
		}
		
		// Focus/blur
		if ($('form input.text').length > 0)
		{
			// Focus
			$('form input.text').focus(function(e)
			{
				$(this).css('background-color', '#fdfdb4');
			});
			// Blur
			$('form input.text').blur(function(e)
			{
				$(this).css('background-color', '#fff');
			});
		}
	}
}

// Font related functionality
var fonts =
{
	/**
	 * Replace fonts marked for Cufon replacement.
	 */
	replace : function()
	{
		// Replace homepage content
		if ($('.home').length > 0)
		{
			if ($('.home .welcome h2').length > 0)
			{
				Cufon.replace('.home .welcome h2', 
				{
					textShadow: '#320206 0 1px'
				});
			}
		}
		
		// Replace generic content
		if ($('.rte h1').length > 0) 
		{
			Cufon.replace('.rte h1', 
			{
				textShadow: '#0c4494 0 2px'
			});
		}
		
		// Replace 'join' CTA text
		if ($('#join-cta a').length > 0) 
		{
			Cufon.replace('#join-cta a span', 
			{
				textShadow: '#fcea85 0 2px'
			});
		}
		
		// Replace 'question title' text
		if ($('.contest-question h2').length > 0)
		{
			Cufon.replace('.contest-question h2',
			{
				textShadow: '#0c4494 0 2px'
			});
		}

		// Replace 'answer title' text
		if ($('.contest-answer h2').length > 0)
		{
			Cufon.replace('.contest-answer h2',
			{
				textShadow: '#0c4494 0 2px'
			});
		}

		// Replace 'order title' text
		if ($('.order-form h1').length > 0)
		{
			Cufon.replace('.order-form h1',
			{
				textShadow: '#0c4494 0 2px'
			});
		}

		// Replace 'order title' text
		if ($('.order-form-school h1').length > 0)
		{
			Cufon.replace('.order-form-school h1',
			{
				textShadow: '#0c4494 0 2px'
			});
		}

		// Replace 'order title' text
		if ($('.join-form h1').length > 0)
		{
			Cufon.replace('.join-form h1',
			{
				textShadow: '#0c4494 0 2px'
			});
		}

		// Replace 'optout title' text
		if ($('.optout-form h1').length > 0)
		{
			Cufon.replace('.optout-form h1',
			{
				textShadow: '#0c4494 0 2px'
			});
		}
	}
}

var lightbox =
{
	init: function()
	{
		if ($('a[rel="lightbox"]').length) {
			$('a[rel="lightbox"]').ajaxLightbox({
				closeLabel: 'close',
				overlayColor: 'transparent',
				overlayOpacity: 0.9
			});
		}
	}
}

// Do Cufon replacement
fonts.replace();

// Make IE6 behave
Cufon.now();
