/*
 * Author:		Soak Digital [developers@soak.co.uk]
 * Agency:		Soak Digital Ltd.
 * Client: 		Archant
 * Project: 	GBL website
 */
 
 /* General Config */
/* ********************************************************************************* */

if (!window.soak) {
	soak = {};
}
if (!window.SoakMaps) {
	SoakMaps = {};
}

soak.genericMessage = 'There has been a problem with your request, please try again'; // Generic AJAX error message
soak.loginErrorHtml = '';

var MPU_TIMEOUT = 15; // Seconds
var BASE_URL = '';
var BASE_SERVER = location.host.substr(location.host.indexOf('.') + 1);


/* Window onload events */
/* ********************************************************************************* */
$(window).load(function(){

    // Label new window links
    $('a.newwindow,.rss-icon a,.editorial>p a,.c-44>p a').each(function(){
        var append = '(This link opens in a new window)';
        if ($(this).attr('title') && append !== $(this).attr('title')) {
        	append = $(this).attr('title') + ' ' + append;
        }
        $(this).attr('title', append);
    });
   
});

/* Document onload events */
/* ********************************************************************************* */

$(function() {
	
	$('#header-site-select').change(function() {

		location.href = 'http://' +  $('#header-site-select').val();
	});

	$('#header-big-barn-area-select').change(function() {
		window.parent.location.href = 'http://' +  $('#header-big-barn-area-select').val() + '.bigbarn.co.uk/';
	});

	// Country/Region select interaction for competition entry and filter photos by region page.
	$('#competition-photograph-region').hide();
	$('#filter-photograph-region').hide();
	$('#frm-complete-form').hide();
	$('#competition-photograph-country-and-region').show();
	$('#filter-country-and-region').show();
	

	$('#frm-email-address').blur(function(){
		$.ajax({
			type: "POST",
			url: "/assets/ajax/getUserInformation.php",
			data: "email_address=" + $('#frm-email-address').val(),
			dataType: 'http',
			success: function(data){
				var fieldsBlank = true;
				if ('' != $('#frm-email-address-confirm').val()) {
					fieldsBlank = false;
				}
				if ('' != $('#frm-salutation').val()) {
					fieldsBlank = false;
				}
				if ('' != $('#frm-first-name').val()) {
					fieldsBlank = false;
				}
				if ('' != $('#frm-last-name').val()) {
					fieldsBlank = false;
				}
				if ('' != $('#frm-address-line-1').val()) {
					fieldsBlank = false;
				}
				if ('' != $('#frm-address-line-2').val()) {
					fieldsBlank = false;
				}
				if ('' != $('#frm-address-line-3').val()) {
					fieldsBlank = false;
				}
				if ('' != $('#frm-town').val()) {
					fieldsBlank = false;
				}
				if ('' != $('#frm-county').val()) {
					fieldsBlank = false;
				}
				if ('' != $('#frm-postcode').val()) {
					fieldsBlank = false;
				}
				if ('' != $('#frm-telephone-number').val()) {
					fieldsBlank = false;
				}
				if ($('#frm-terms-accepted').attr('checked')) {
					fieldsBlank = false;
				}
				if ($('#frm-want-newsletters').attr('checked')) {
					fieldsBlank = false;
				}
				if (fieldsBlank && data)
				{
					var userInformation = jQuery.JSON.decode(data);
					if (undefined !== userInformation['email_address']) {
						$('#frm-email-address-confirm').val(userInformation['email_address']);
					}
					if (undefined !== userInformation['salutation']) {
						$('#frm-salutation').val(userInformation['salutation']);
					}
					if (undefined !== userInformation['first_name']) {
						$('#frm-first-name').val(userInformation['first_name']);
					}
					if (undefined !== userInformation['last_name']) {
						$('#frm-last-name').val(userInformation['last_name']);
					}
					if (undefined !== userInformation['address_line_1']) {
						$('#frm-address-line-1').val(userInformation['address_line_1']);
					}
					if (undefined !== userInformation['address_line_2']) {
						$('#frm-address-line-2').val(userInformation['address_line_2']);
					}
					if (undefined !== userInformation['address_line_3']) {
						$('#frm-address-line-3').val(userInformation['address_line_3']);
					}
					if (undefined !== userInformation['town']) {
						$('#frm-town').val(userInformation['town']);
					}
					if (undefined !== userInformation['county']) {
						$('#frm-county').val(userInformation['county']);
					}
					if (undefined !== userInformation['postcode']) {
						$('#frm-postcode').val(userInformation['postcode']);
					}
					if (undefined !== userInformation['country']) {
						$('#frm-country').val(userInformation['country']);
					}
					if (undefined !== userInformation['telephone_number']) {
						$('#frm-telephone-number').val(userInformation['telephone_number']);
					}
					if (undefined !== userInformation['terms_accepted']) {
						$('#frm-terms-accepted').attr('checked', userInformation['terms_accepted']);
					}
					if (undefined !== userInformation['want_newsletters']) {
						$('#frm-want-newsletters').attr('checked', userInformation['want_newsletters']);
					}
				}
			}
		})
	});

	if (typeof competitionCountry != 'undefined') {
		$('#frm-competition-country').val(competitionCountry);
		soak.populateRegions();
	}

	if (typeof competitionRegion != 'undefined') {
		$('#frm-competition-region').val(competitionRegion);
	}

	$('#frm-competition-country').change(function() {
		soak.populateRegions();
	});
	/*populate filter region and take the selected region in the query string*/ 
	if (typeof filterCountry != 'undefined') {
		$('#frm-filter-country').val(filterCountry);
		soak.populateFilterRegions();
	}

	if (typeof filterRegion != 'undefined') {
		$('#frm-filter-region').val(filterRegion);
	}
	
	$('#frm-filter-country').change(function() {
		soak.populateFilterRegions();
	});
	//query string based on selected country
	$('#frm-filter-country').change(function() {
		
		countryFilter = $('#frm-filter-country').val();
		
		qs = location.href.split('?');
		if ('' == countryFilter) {
			location.href = qs[0];
		} else {
			location.href = qs[0] + '?country=' + countryFilter;
		}
	});
	//query string based on selected region
	$('#frm-filter-region').change(function() {
		regionFilter = $('#frm-filter-region').val();
		countryFilter = $('#frm-filter-country').val();
		qs = location.href.split('?');
		qsreg = location.href.split('?');
		if ('' == regionFilter) {
			location.href = qs[0] + '?country=' + countryFilter;
			
		} else {
			location.href = qs[0] + '?country=' + countryFilter + '&region=' + regionFilter;
		}
	});

	if($.cookie('lang') !== null && $.cookie('lang') !== 'en') {
		
		 $('body').translate( 'en', $.cookie('lang') );
	}

	// messaging component message deletion
	$('.delete-message').live('click', function() {
		var answer = confirm("Are you sure you wish to delete this message?");
		if (answer) {
			tr = $(this).closest('tr');
			tr2 = tr.prev('tr');
			soak.ajaxLink({
				element : this,
				success : function(data, textStatus) {
					tr.remove();
					tr2.remove();
				}
			});	 
		}
		return false;
	});
	/*
	$('.site-links li span').each(function() {
		if ($(this).height() > 56) {
			$(this).siblings('.img').addClass('lines-4');
		}
		else if ($(this).height() > 26) {
			$(this).siblings('.img').addClass('lines-3');
		}
		else if ($(this).height() > 16) {
			$(this).siblings('.img').addClass('lines-2');
		}
	});
	*/
	
	// jCarousel functionality
	$('.c-76').addClass('with-actions'); // show slideshow controls

	/* Main navigation action */
	/* ********************************************************************************* */
	
	var closeNav;
	var openNav;
	currentSubnav = $('#main-subnav > li').index($('#main-subnav > li.current'));
	
	$('#main-nav li > a').hover(function(){
		clearTimeout(closeNav);
		$this = $(this);
		openNav = setTimeout('$(\'#main-nav .selected\').removeClass(\'selected\');$this.parent(\'li\').addClass(\'selected\');$(\'#main-subnav > li\').removeClass(\'current\');$(\'#subnav-\'+$(\'#main-nav li\').index($this.parent(\'li\'))).addClass(\'current\');', 150);
	},
	function(){
		clearTimeout(openNav);
		closeNav = setTimeout('$(\'#main-nav .selected\').removeClass(\'selected\');$(\'#main-subnav > li\').removeClass(\'current\');$(\'#subnav-\'+currentSubnav).addClass(\'current\');', 200);
	});
	
	$('#main-subnav').hover(function(){
		clearTimeout(closeNav);
	},
	function(){
		closeNav = setTimeout('$(\'#main-nav .selected\').removeClass(\'selected\');$(\'#main-subnav > li\').removeClass(\'current\');$(\'#subnav-\'+currentSubnav).addClass(\'current\');', 1000);
	});
	
	/* Poll action */
	/* ********************************************************************************* */
	
	$('.c-31 form').each(function(i, e) {
		soak.ajaxForm(e, {
			success : function(data, textStatus) {
				if (data.redirectUrl) {
					location.href = data.redirectUrl;
					return false;
				}
				$(e).children('fieldset').children('.vote').hide();
				$(e).children('.actions').hide();
				$(e).children('fieldset').not('.actions').append(data.html);
				animatePollAnswers(e);
			}
		});
	});
	
	
	/* Initiate and bind events */
	/* ********************************************************************************* */

	// label new window links
	// documents and external links to open in new window
	$("a.newwindow,.rss-icon a").attr('target','_blank');
	$("form.newwindow").attr('target','_blank');
	$("a[href$='pdf']").attr('target','_blank');
	
	// label text inputs nicely
	$('#frm-date-from').intelliLabel('DD/MM/YYYY');
	$('#frm-date-to').intelliLabel('DD/MM/YYYY');
	$('#frm-keyword').intelliLabel();
	$('#frm_intro_text').intelliLabel('Please use this space to write a few words about yourself and your interests.');
	$('#frm-by-location').intelliLabel('Postcode or Town/City');
	
	// show hidden elements when JS is enabled
	$('.show-if-js').show();
	
	$('.dropdown').addClass('hide');
	
	// disable links with class 'disabled'
	$('.disabled').live('click',function() {
		return false;
	});
	
	// force clearing of components in IE6
	if (jQuery.browser.msie) {
		if(parseInt(jQuery.browser.version) == 6) {
			$('.component-container').not('.c-50, .c-51, .c-52, .c-43, .c-8').after('<br class="clear" />');
		}
	}
	
	
	/* Component-specific events */
	/* ********************************************************************************* */
	if($('.launch-poll').length > 0)
	{
		soak.showPollOverlay();
	}
	
	if($('.launch-mpu').length > 0)
	{
		soak.showMpuOverlay();
		$('#mpu-overlay-close').live('click',function() {
			$('#launch-mpu').hide();
			return false;
		});
	}
	
	if($('.c-0').length > 0)
	{
		$('.c-0 .component-content').css('cursor','pointer').click(function() {
			var anchor = $('.action a', $(this));
			if (anchor.hasClass('newwindow')) {
				window.open(anchor.attr('href'));
			}
			else {
				location.href = anchor.attr('href');
			}
			return false;
		});
	}
	
	if($('.c-21').length > 0)
	{
		$('.c-21 li').hover(function() {
			$(this).addClass('current').children('img').show();
			$(this).siblings('li').removeClass('current').children('img').hide();
		});
		$('.c-21 li:first-child').addClass('current').children('img').show();
	}
	
	if($('.c-25').length > 0)
	{
		var imageList = $('.c-25').children('ul:first');
		var index = 0;
		var disableHTML = '';
		if($('.c-25 > ul > li').length <= 2) { disableHTML = ' class="disabled"'; } 
		$('.c-25').append('<ul class="actions"><li class="back"><a class="disabled" href="#none"><em>Back</em></a></li><li class="next last"><a' + disableHTML + ' href="#none"><em>Next</em></a></li></ul>');
		imageList.children('li').hide();
		imageList.children('li').eq(index).show().next('li').show();
		$('.c-25 .next a').live('click', function(){
			if (!imageList.children('li:last').is(':visible')) {
				$(this).parents('.actions').children('li').children('a').removeClass('disabled');
				index += 2;
				imageList.children('li').hide();
					imageList.children('li').eq(index).fadeIn().next('li').fadeIn();
					if (imageList.children('li:last').is(':visible')) {
						$(this).addClass('disabled');
					}
			}
			return false;
		});
		$('.c-25 .back a').live('click', function(){
			if (!imageList.children('li:first').is(':visible')) {
				$(this).parents('.actions').children('li').children('a').removeClass('disabled');
				index -= 2;
				imageList.children('li').hide();
					imageList.children('li').eq(index).fadeIn().next('li').fadeIn();
					if (imageList.children('li:first').is(':visible')) {
						$(this).addClass('disabled');
					}
			}
			return false;
		});
	}
	
	if($('.c-28 h2').length) {
		$('.c-28').widgetNav();
	}
	else {
		if($('.c-28').length) {
			$('.c-28').inpageWidgetNav();
		}
	}

	if($('.c-41 .gallery').length) {
		$('.c-41 .gallery').inpageWidgetNav();
	}
	
	$('.c-53 .message td').hide();
	
	
	/* Finder panel */
	/* ********************************************************************************* */
	
	finderMap();
	
	$.preloadImages(BASE_URL + '/assets/images/bg-finder-map.jpg');
	
	$('.finder-controls label').click(function() {
		$('.finder-controls #keywords').focus();
	});
	
	$('.finder-container .close').live('click', function() {
		$('.finder-controls a').removeClass('current');
		$('.finder-container').slideUp(500, function() {
			$('.finder-container').remove();
		});
		return false;
	});
	
	$('.finder-controls .local-info').live('click', function() {
		soak.ajaxLink({
			element : this,
			success : function(data, textStatus) {
				$('.finder-controls a').removeClass('current');
				$('.finder-controls .local-info').addClass('current');
				
				if (!$('.finder-container').hasClass('local-info')) {
					if ($('.finder-container:visible').length) {
						$('.finder-container').slideUp(500, function() {
							$('.finder-container').remove();
							$('.finder-controls').after(data.html);
							$('.finder-container').slideDown(500);
						});
					}
					else {
						$('.finder-container').remove();
						$('.finder-controls').after(data.html);
						$('.finder-container').slideDown(500);
					}
				}
			}
		});
		return false;
	});
	
	$('.finder-controls .magazines').live('click', function() {
		
		soak.ajaxLink({
			element : this,
			success : function(data, textStatus) {
				$('.finder-controls a').removeClass('current');
				$('.finder-controls .magazines').addClass('current');
				
				if (!$('.finder-container').hasClass('magazines')) {
					if ($('.finder-container:visible').length) {
						$('.finder-container').slideUp(500, function() {
							$('.finder-container').remove();
							$('.finder-controls').after(data.html);
							$('.finder-container').slideDown(500);
						});
					}
					else {
						$('.finder-container').remove();
						$('.finder-controls').after(data.html);
						$('.finder-container').slideDown(500);
					}
				}
			}
		});
		return false;
	});
	
	
	/* Generic expand / collapse for components */
	/* ********************************************************************************* */
	
	$('.expandable li:not(.open) div').hide();
	$('.expand').live('click',function() {
		$(this).removeClass('expand').addClass('collapse');
		$(this).siblings('div').slideDown();
		$(this).children('em').text('Collapse');
		return false;
	});
	$('.collapse').live('click',function() {
		$(this).removeClass('collapse').addClass('expand');
		$(this).siblings('div').slideUp();
		$(this).children('em').text('Expand');
		return false;
	});
	
	
	/* Component sub-nav navigation */
	/* ********************************************************************************* */
	
	$('.c-53 .sub-nav a, .c-53 .reply, .c-53 .pagination a').live('click', function() {
		var link = $(this);

		soak.ajaxLink({
			element : this,
			success : function(data, textStatus) {
			if (data.redirectUrl) {
					location.href = data.redirectUrl;
					return false;
				}
				
				link.parent('li').addClass('current').siblings().removeClass('current');
		
				$('.message-content').slideUp(function() {
					$(this).empty().append(data.html);	
					$('.message td').hide();
					$(this).slideDown();
				});
			}
		});
		return false;
	});

	$('.c-30 .sub-nav .sent').live('click', function() {
		$(this).parent('li').addClass('current').siblings().removeClass('current');
		$('.message-content').slideUp(function() {
			$('#inbox').css('display','none');
			$('#sent').css('display','block');
			$('.message td').hide();
			$(this).siblings('.actions').children('a').attr('href', '/user/messaging/sent/');
			$(this).slideDown();
		});
		
		return false;
	});	
	
	$('.c-30 .sub-nav .inbox').live('click', function() {
		$(this).parent('li').addClass('current').siblings().removeClass('current');
		$('.message-content').slideUp(function() {
			$('#inbox').css('display','block');
			$('#sent').css('display','none');
			$('.message td').hide();
			$(this).siblings('.actions').children('a').attr('href', '/user/messaging/');
			$(this).slideDown();
		});
		
		return false;
	});	
	
	/*
	 * For object tabbed callout
	 */	
	$('.c-20 .sub-nav a').live('click',function() {
		if ($(this).parent('.current').length == 0) {
			var cssClass = $(this).attr('class');
			var parent = $(this).parents('.c-20');
			parent.find('.current').removeClass('current');
			$(this).parent('li').addClass('current');
			parent.find('div div div:visible').fadeOut('normal',function() {
				$(this).siblings('.'+cssClass).fadeIn();												 
			});
			/*var $this = $(this).parent('li');
			
			soak.ajaxLink({
				element : this,
				success : function(data, textStatus) {
					if (data.redirectUrl) {
						location.href = data.redirectUrl;
						return false;
					}
					$this.addClass('current').siblings('li').removeClass('current');
					$('.c-20 .component-content').children('.listing').fadeOut(function() {
						$(this).replaceWith(data.html);
						$('.c-20 .component-content').children('.listing').hide().fadeIn();
					});
				}
			});*/
		}
		return false;
	});
	
	
	/* Font resizing */
	/* ********************************************************************************* */
	
	$('.inc').fontSizing(1);
	$('.dec').fontSizing(-1);
	
	
	/* Datepicker */
	/* ********************************************************************************* */
	
	// append a jQuery  UI stylesheet
	$('head').append('<link type="text/css" rel="stylesheet" href="' + BASE_URL + '/assets/css/styles-ui.css" media="screen" />');
	
	// initiate jQuery UI datepicker
	$('.datepicker').datepicker({showOn: 'button', buttonImage: BASE_URL + '/assets/images/img-calendar.png', buttonImageOnly: true, duration: '', dateFormat:'dd/mm/yy'});
	
			
	/* Popup container action */
	/* ********************************************************************************* */
	
	$('._soak_overlay_close').live('click', function() {
		if (jQuery.browser.msie) {
			$(this).closest('.popup-container').remove();
		}
		else {
			$(this).closest('.popup-container').fadeOut(function() {
				$(this).remove();
			});
		}
		
		return false;
	});
	
	
	/*
	 * Controls the add contact box display and functionality. This code
	 * communicates with the back end login component and returns 
	 * back relevant errors if there are any or redirects to the
	 * relevant page if required.
	 */
	
	$('.launch-add-contact').live('click', function() {
		soak.ajaxLink({
			element : this,
			success : function(data, textStatus) {
			
				soak.popup({
					selector : '#launch-add-contact',
					html : data.html,
					destroyOnClose: false,
					onFinish: function() {
					
						$('#frm_username').focus();
						soak.ajaxForm('#launch-add-contact form', {
							success : function(data, textStatus) {
								var html = '';
								
								if (data.redirectUrl) {
									location.href = data.redirectUrl;
									return false;
								}
							}
						});
					}
				});
			}
		});
		return false;
	});

	/*
	 * Controls the translate overlay
	 */
	
	$('.launch-translate').live('click', function() {
			
		soak.ajaxLink({
			element : this,
			success : function(data, textStatus)  {
				soak.popup({
					selector : '#launch-translate',
					html : data.html,
					destroyOnClose: false,
					onFinish: function() {
						
					}
				});
			}
		});	
	
		$('#langsel').change(function(){
			
			if($.cookie('lang') !== null) {
				
				$('body').translate('en'); //default to english first
			}

	        $.cookie('lang', $(this).val(), { expires: 999, path: '/', domain: BASE_SERVER });
	        location.reload();
		});

		
		return false;
	});
	
	
	
	/*
	 * Controls the save article box display and functionality. 
	 */
	
	$('.save-article').live('click', function() {
		soak.ajaxLink({
			element : this,
			success : function(data, textStatus)  {
				soak.popup({
					selector : '#launch-save-article',
					html : data.html,
					destroyOnClose: false,
					onFinish: function() {
						
					}
				});
			}
		});	
		return false;
	});
	
	/*
	 * Controls the delete article box display and functionality. 
	 */
	
	$('.delete-article').live('click', function() {
		li = $(this).closest('li');
		soak.ajaxLink({
			element : this,
			success : function(data, textStatus)  {
				li.slideUp();
			}
		});	
		return false;
	});	
	
	/*
	 * Controls the delete article box display and functionality. 
	 */
	
	$('.listing-delete-article').live('click', function() {
		div = $(this).closest('div');
		soak.ajaxLink({
			element : this,
			success : function(data, textStatus)  {
				div.slideUp();
			}
		});	
		return false;
	});	
	
	/*
	 * Controls the login box display and functionality. This code
	 * communicates with the back end login component and returns 
	 * back relevant errors if there are any or redirects to the
	 * relevant page if required.
	 */

	$('.launch-login').live('click', function() {

		soak.ajaxLink({
			element : this,
			success : function(data, textStatus) {

				soak.popup({
					selector : '#launch-login',
					html : data.html,
					destroyOnClose: false,
					onFinish: function() {
					
						$('#launch-login #frm_email').focus();
						soak.ajaxForm('#launch-login form', {
							success : function(data, textStatus) {
								var html = '';
								
								if (data.redirectUrl) {
									location.href = data.redirectUrl;
									return false;
								}
								
								if (data.errors) {
									for (err in data.errors) {
										html += '<li>' + data.errors[err] + '</li>'
									}
									
									html = '<ul class="errors" style="display:none">' + html + '</ul>';
									
									if ($('#launch-login .errors').length) {
										$('#launch-login .errors').slideUp(function() {
											$('#launch-login .errors').remove();
											$('#launch-login h2').after(html);
											$('#launch-login .errors').slideDown();
										});
									}
									else {
										$('#launch-login h2').after(html);
										$('#launch-login .errors').slideDown();
									}
								}
							}
						});
					}
				});
			}
		});
		return false;
	});
	
	
	/*
	 * Controls the login box display and functionality. This code
	 * communicates with the back end login component and returns 
	 * back relevant errors if there are any or redirects to the
	 * relevant page if required.
	 */
	
	$('.launch-forgotten-password').live('click', function() {
		soak.ajaxLink({
			element : this,
			success : function(data, textStatus) {
			
				soak.popup({
					selector : '#launch-forgotten-password',
					html : data.html,
					destroyOnClose: false,
					onFinish: function() {
					
						$('#launch-forgotten-password #frm_email').focus();
						soak.ajaxForm('#launch-forgotten-password form', {
							success : function(data, textStatus) {
								var html = '';
								
								if (data.redirectUrl) {
									location.href = data.redirectUrl;
									return false;
								}
								
								if (data.errors) {
									for (err in data.errors) {
										html += '<li>' + data.errors[err] + '</li>'
									}
									
									html = '<ul class="errors" style="display:none">' + html + '</ul>';
									
									if ($('#launch-forgotten-password .errors').length) {
										$('#launch-forgotten-password .errors').slideUp(function() {
											$('#launch-forgotten-password .errors').remove();
											$('#launch-forgotten-password h2').after(html);
											$('#launch-forgotten-password .errors').slideDown();
										});
									}
									else {
										$('#launch-forgotten-password h2').after(html);
										$('#launch-forgotten-password .errors').slideDown();
									}
								}
								else {
									
									soak.popup({
										selector : '#launch-forgotten-password-thankyou',
										html : data.html
									});
								}
							}
						});
					}
				});
			}
		});
		return false;
	});
	
	
	/*
	 * Controls the enter competition display and functionality. 
	 */

	$('.launch-enter-competition').live('click', function(){
	
		soak.ajaxLink({
			element : this,
			success : function(data, textStatus){
			
				soak.popup({
					selector : '#launch-enter-competition',
					html : data.html,
					destroyOnClose: false,
					onFinish: function() {
					
						$('#frm_answer').focus();
						soak.ajaxForm('#launch-enter-competition form', {
							success : function(data, textStatus) {				
	
								soak.popup({
									selector : '#launch-enter-competition',
									html : data.html
								});
							}
						});
					}
				});
			}
		});

		return false;
	});


	$('.launch-trailer').click(function() {

		soak.ajaxLink({
			element : this,
			success : function(data, textStatus) {
			
				soak.popup({
					selector : '#launch-video-' + data.videoId,
					html : data.html,
					destroyOnClose: false,
					beforeFadeIn: function() {
					
						$('#launch-video-' + data.videoId + ' .popup-content span').flash({  
							swf: BASE_URL + '/assets/swf/player_flv.swf',  
							width: 320,
							height: 180,
							flashvars: {  
								buttonovercolor: 'FFFFFF',
								showtime: '1',
								showvolume: '1',
								loadingcolor: 'CC3333',
								slidercolor1: 'CC3333',
								slidercolor2: 'CC3333',
								flv: data.videoUrl,
								autoplay : '1',
								buffershowbg : '0',
								buffercolor : 'transparent',
								bufferbgcolor : 'transparent',
								width: 320,
								height: 180
							}  
						});
					},
					onFinish: function() {
						
					}
				});
			}
		});
		return false;
	});

	
	$('.launch-video').click(function() {

		soak.ajaxLink({
			element : this,
			success : function(data, textStatus) {
			
				soak.popup({
					selector : '#launch-video-' + data.videoId,
					html : data.html,
					destroyOnClose: false,
					beforeFadeIn: function() {
					
						$('#launch-video-' + data.videoId + ' .popup-content span').flash({  
							swf: BASE_URL + '/assets/swf/player_flv.swf',  
							width: 595,
							height: 417,
							flashvars: {  
								buttonovercolor: 'FFFFFF',
								showtime: '1',
								showvolume: '1',
								loadingcolor: 'CC3333',
								slidercolor1: 'CC3333',
								slidercolor2: 'CC3333',
								flv: data.videoUrl,
								autoplay : '1',
								buffershowbg : '0',
								buffercolor : 'transparent',
								bufferbgcolor : 'transparent',
								width: 595,
								height: 417
							}  
						});
					},
					onFinish: function() {
						
					}
				});
			}
		});
		return false;
	});
	
	
	$('.postcode-lookup').click(function() {
		$.ajax({
			type: "GET",
			dataType: 'json',
			url: "/test/address_finder.php?postcode="+$('#frm_postcode').val(),
			success: function(data) {
				soak.popup({
					selector : '#launch-address-finder',
					html : data.html,
					destroyOnClose: false,
					beforeFadeIn: function() {
						
					},
					onFinish: function() {
						
					}
				});
			}
		});	
		return false;
	});
	
	/*
	 * Controls the send to friend display and functionality. This code
	 * communicates with the back end login component and returns 
	 * back relevant errors if there are any or redirects to the
	 * relevant page if required.
	 */
	
	$('.launch-send-to-friend').live('click', function() {
		soak.ajaxLink({
			element : this,
			success : function(data, textStatus) {
			
				soak.popup({
					selector : '#launch-send-to-friend',
					html : data.html,
					destroyOnClose: false,
					onFinish: function() {
						
						soak.ajaxForm('#launch-send-to-friend form', {
							success : function(data, textStatus) {
								var html = '';
								
								if (data.redirectUrl) {
									location.href = data.redirectUrl;
									return false;
								}
								
								if (data.errors) {
									for (err in data.errors) {
										html += '<li>' + data.errors[err] + '</li>'
									}
									
									html = '<ul class="errors" style="display:none">' + html + '</ul>';
									
									if ($('#launch-send-to-friend .errors').length) {
										$('#launch-send-to-friend .errors').slideUp(function() {
											$('#launch-send-to-friend .errors').remove();
											$('#launch-send-to-friend h2').after(html);
											$('#launch-send-to-friend .errors').slideDown();
										});
									}
									else {
										$('#launch-send-to-friend h2').after(html);
										$('#launch-send-to-friend .errors').slideDown();
									}
								}
								else {
									
									soak.popup({
										selector : '#launch-send-to-friend-thanks',
										html : data.html
									});
								}
							}
						});
					}
				});
			}
		});
		return false;
	});


	$('.rating li a').live('click', function() {
		soak.ajaxLink({
			element : this,
			success : function(data, textStatus) {
				soak.popup({
					selector : '#launch-rating-response',
					html : data.html,
					destroyOnClose: false
				});
			}
		});
		return false;
	});
	
	
	$('.article-gallery-list .launch-image,.editorial-gallery .launch-image, .editorial-gallery-list .launch-image, #launch-image .prev, #launch-image .next, .c-55 .launch-image').live('click', function() {
		var positionTop;
		if ($(this).hasClass('prev') || $(this).hasClass('next')) {
			positionTop = $('#launch-image').offset().top;
		}
		else {
			positionTop = $(this).offset().top;
		}
		soak.ajaxLink({
			element : this,
			success : function(data, textStatus) {
				soak.popup({
					selector : '#launch-image',
					html : data.html
				}, positionTop);
			}
		});
		return false;
	});
	
	$('.article-gallery-list .pagination a').live('click', function() {
		
		$('.article-gallery-list').html('');
		
		$this = this;
		
		$('.article-gallery-list').slideUp(function(){
			$(this).addClass('loading');
			soak.ajaxLink({
				element : $this,
				success : function(data, textStatus) {
					$('.article-gallery-list').html(data.html);
				}
			});
		});
		
		$('.article-gallery-list').slideDown(function(){
			$(this).removeClass('loading');
		});
		
		return false;		
	});                                       
	
	
	/*
	 * Controls the deletion of a user's gallery image.
	 */
	
	$('.launch-image-delete').live('click', function() {
		soak.ajaxLink({
			element : this,
			success : function(data, textStatus) {
				soak.popup({
					selector : '#launch-image-delete',
					html : data.html,
					destroyOnClose: false,
					onFinish: function() {
					
						soak.ajaxForm('#launch-image-delete form', {
							success : function(data, textStatus) {
							
								if (data.redirectUrl) {
									location.href = data.redirectUrl;
									return false;
								}
								
								soak.popup({
									selector : '#launch-image-delete',
									html : data.html
								});
							}
						});
					}
				});
			}
		});
		return false;
	});
	
	/*
	 * Controls the deletion of a forum post.
	 */
	
	$('.delete-post').live('click', function() {
		
		li = $(this).closest('li');
		soak.ajaxLink({
			element : this,
			success : function(data, textStatus) {
				soak.popup({
					selector : '#launch-post-delete',
					html : data.html,
					destroyOnClose: false,
					onFinish: function() {
					
						soak.ajaxForm('#launch-post-delete form', {
							success : function(data, textStatus) {
							li.remove();
								if (data.redirectUrl) {
									location.href = data.redirectUrl;
									return false;
								}
								
								soak.popup({
									selector : '#launch-post-delete',
									html : data.html
								});
							}
						});
					}
				});
			}
		});
		return false;
	});	
	
	/*
	 * Controls the deletion of a forum post.
	 */
	
	$('.delete-thread').live('click', function() {
		
		soak.ajaxLink({
			element : this,
			success : function(data, textStatus) {
				soak.popup({
					selector : '#launch-thread-delete',
					html : data.html,
					destroyOnClose: false,
					onFinish: function() {
					
						soak.ajaxForm('#launch-thread-delete form', {
							success : function(data, textStatus) {
								if (data.redirectUrl) {
									location.href = data.redirectUrl;
									return false;
								}
								
								soak.popup({
									selector : '#launch-thread-delete',
									html : data.html
								});
							}
						});
					}
				});
			}
		});
		return false;
	});		
	
	/*
	 * View a message from either inbox or sent items.
	 */

	$('.c-53 .subject,.messaging-callout .subject').live('click', function() {

		if ($(this).closest('tr').next().hasClass('message') == false) {
			var $this = $(this);
			
			soak.ajaxLink({
				element : this,
				async: true,
				success: function(data, textStatus) {
					$this.closest('tr').after(data.html);
				}
			});
		}
		$(this).closest('tr').removeClass('unread').next('.message').children('td').toggle();
		return false;
	});
	
	
	$(".credentials a img").click(function() {
		var parentFieldset = $(this).parents('fieldset');
		var dropdown = parentFieldset.next('fieldset');
		if(dropdown.hasClass('hide'))
		{
			var newSrc = $(this).attr('src').replace(/change/,'cancel');
			$(this).attr('src',newSrc);
			dropdown.slideDown('fast',function() {
				dropdown.removeClass('hide');
			});
		}
		else
		{
			var newSrc = $(this).attr('src').replace(/cancel/,'change');
			$(this).attr('src',newSrc);
			dropdown.slideUp('fast',function() {
				dropdown.addClass('hide');
			});
		}
		$(this).blur();
		return false;
	});
	
	$('body').click(function() {
		//remove any infopops
		$('#infopop').remove();
	});
	
	$('a.help').click(function(e) {
		//remove any infopops
		$('#infopop').remove();
		
		//construct infopop, append to content
		var content = '<div id="infopop"><div><p>Helpful hints</p><p class="info"></p></div></div>';
		$('body').append(content);
		$('#infopop div .info').append($(this).siblings('.w3c').text());
		
		//getting height and width of the message box
		var height = $('#infopop').height();
		var width = $('#infopop').width();
		
		//calculate offset from selected link and apply to infopop
		var offset = $(this).offset();

		leftVal = offset.left  + "px";
		topVal = offset.top  + "px";
		$('#infopop').css({
			left: leftVal,
			top: topVal
		})
		
		//remove infopop and restore image
		$(this).blur(function() {
			$('#infopop').remove();
		});
		
		return false;
	});
	
	$('.rating').each(function() {
		$(this).data('rating', $(this).children('div').attr('class'));
	});
	
	$('.rating a').hover(function() {
		var newClass = $(this).attr('class');
		$(this).parents('.rating').children('div').attr('class',newClass);	
	});
	
	$('.rating').not('.disabled').bind('mouseleave',function() {
		$(this).children('div').attr('class', $(this).data('rating'));										
	});
	
	
	/*
	* markItUp!
	* Copyright (C) 2008 Jay Salvat (http://markitup.jaysalvat.com/)
	*/
	
	myBbcodeSettings = {
	  nameSpace: "bbcode", // Useful to prevent multi-instances CSS conflict
	  previewParserPath: "~/bbcode/preview.php",
	  markupSet: [
		  {name:'Bold', key:'B', openWith:'[b]', closeWith:'[/b]'}, 
		  {name:'Italic', key:'I', openWith:'[i]', closeWith:'[/i]'},
		  {name:'Link', key:'L', openWith:'[url=[![Url]!]]', closeWith:'[/url]', placeHolder:'Your text to link here...'},
		  {name:'Bulleted list', openWith:'[list]\n[*] ', closeWith:'\n[/list]'}, 
		  {name:'Numeric list', openWith:'[list=[![Starting number]!]]\n[*]', closeWith:'\n[/list]'}, 
		  {name:'List item', openWith:'[*] '}, 
		  {name:'Quotes', openWith:'[quote]', closeWith:'[/quote]'}, 
		  {separator:'&nbsp;' },
		  {name:'Clean', className:"clean", replaceWith:function(h) { return h.selection.replace(/\[(.*?)\]/g, "") } }
	   ]
	}
	
	$('.bbcode').markItUp(myBbcodeSettings);
	
	$('#emoticons a').click(function() {
		emoticon = $(this).attr("title");
		$.markItUp( { replaceWith:emoticon } );
	});
	
	if($('.c-73 form').length)
	{
		$('.c-73 form').show();
	}
	
	$('.c-73 select').live('change',function() {
		if($(this).val() != '')
		{
			window.location = $(this).val();
		}
	});
	
	$('.results-filter select').change(function(){
		
		qs = location.href.split('?');
		if (qs[1]) {
			qsOrder = qs[1];
			qsStart = qsOrder.split('order=');
			if (qsStart[1]) {
				newQs = qs[0] + '?' + qsStart[0]  + 'order=' + $('.sort-order',$(this).parents('form')).val() +'&items=' + $('.items-per-page',$(this).parents('form')).val();
			} else {
				newQs = qs[0] + '?' + qsStart[0]  + '&order=' + $('.sort-order',$(this).parents('form')).val() +'&items=' + $('.items-per-page',$(this).parents('form')).val();
			}
		} else {
			newQs = location.href + '?order=' + $('.sort-order',$(this).parents('form')).val() +'&items=' + $('.items-per-page',$(this).parents('form')).val();
		}
		
		pageless = newQs.split('/page/');
		if (pageless[1]) {
			qsEnd = pageless[1];
			qsEnd = qsEnd.split('/');
			if (qsEnd[1]) {
				newQs = pageless[0] + '/' + qsEnd[1];
			} else {
				newQs = pageless[0] + '/';
			}
		}		
		location.href = newQs;
		return false;
	});
});

/* Rollover effect for finder map */
/* ********************************************************************************* */

function finderMap() {
	$('.map img').css('opacity','0');
	$('#_imgmap-1 area').hover(function() {
		$('.map').css('background-position','0 -' + (($('#_imgmap-1 area').index(this) + 1) * 169) + 'px');
	},
	function() {
		$('.map').css('background-position','0 0');
	});
	
	$('#_imgmap-1 area').click(function() {
		var area = $(this).attr('alt');
		$('#frm-region').fadeTo(100, 0.2, function() {
			$('#frm-region option[value=' + area + ']').attr('selected','selected');
			if ($('.finder-container.magazines').length > 0) {
				updateRegions();
			}
			$(this).fadeTo(200,1);
		});
		return false;
	});
}

/* Update regions on finder map */
/* ********************************************************************************* */

function updateRegions() {

	$('#frm-region-magazines option').remove();
	$('#frm-region-magazines').append('<option value="0"> -- any -- </option>'); 
	$count = 0;
	
	for (var i = 0; i < regions.length; i++) {
		if (regions[i].region == $('#frm-region').val()) {
			$count++;
			$('#frm-region-magazines').append(
				'<option value="' + regions[i].id + '" class="frm-region-magazines-options">' 
				+ regions[i].name + '</option>'
			); 	
		}
	}
	
	if ($count == 1)  {
		$('.frm-region-magazines-options:last').attr('selected', 'selected');
	}
}

/* Animation for displaying results in poll component */
/* ********************************************************************************* */

function animatePollAnswers(form) {
	$(form).find('.results em').each(function() {
		var amount = $(this).css('border-right-width');
		$(this).css('border-right-width','0');
		$(this).animate({
			borderRightWidth: amount
     	}, 1500, "linear");
	});
}

/* Moves label into a text input and handles focus / blur events */
/* ********************************************************************************* */

jQuery.fn.intelliLabel = function(label) {

	if (!label) {
		var label = $('label[for=' + $(this).attr('id') + ']').text();
	}
	
	$('#' + $(this).attr('id')).focus(function() {
		if ($(this).val() === label) $(this).val('');
	}).blur(function() {
		// simulate a delay before reverting to placeholder
		$(this).fadeTo(500, 1, function() {
			if ($(this).val() === '') $(this).val(label);
		});
	});
	
	if ($(this).val() === '') {
		$(this).val(label);
	}
}

/* Simulate rollover on form buttons.  Inputs of type="button" must have a rollover image '-on.gif' */
/* ********************************************************************************* */

jQuery.fn.rolloverImage = function() {
	var originalSrc = '';
	var originalFile = '';
	$(this).hover( function() {
		originalSrc = $(this).attr('src');
		originalFile = originalSrc.split(".")[0];
		$(this).attr('src', originalFile + '-on.gif')
	},
	function() {
		$(this).attr('src', originalSrc)
	});
}

/* Handle passing of parameters by querystring */
/* ********************************************************************************* */

jQuery.query = function(s) {
	var r = {};
	if (s) {
		var q = s.substring(s.indexOf('?') + 1); // remove everything up to the ?
		q = q.replace(/\&$/, ''); // remove the trailing &
		jQuery.each(q.split('&'), function() {
			var splitted = this.split('=');
			var key = splitted[0];
			var val = splitted[1];
			// convert numbers
			if (/^[0-9.]+$/.test(val)) val = parseFloat(val);
			// convert booleans
			if (val == 'true') val = true;
			if (val == 'false') val = false;
			// ignore empty values
			if (typeof val == 'number' || typeof val == 'boolean' || val.length > 0) r[key] = val;
		});
	}
	return r;
};

/* Preload images */
/* ********************************************************************************* */

jQuery.preloadImages = function() {
	for(var i = 0; i<arguments.length; i++) {
	$('<img>').attr('src', arguments[i]);}
}

/* Gallery component navigation */
/* ********************************************************************************* */
	
jQuery.fn.widgetNav = function() {
	
	$('.next,.prev', $(this)).click(function() {

		var $this = $(this).closest('.editorial-gallery');
		
		soak.ajaxLink({
			element : this,
			success : function(data, textStatus) {
		
				$('.thumb .current img',$this).fadeOut(300, function() {

					$('.thumb .current img',$this).hide();											 
					$('.thumb .loader',$this).show();							   
					$('.thumb .current img:not(.magnify)',$this).load(function () {
						
						$('.thumb .loader',$this).hide();							   												  
						$('.thumb .current img',$this).fadeIn(300);
					}).attr('src', data.image.src);	

					var linkTemplate = $('.next',$this).attr('href').substr(0,$('.next',$this).attr('href').lastIndexOf('/'));
					linkTemplate = linkTemplate.substr(0,linkTemplate.lastIndexOf('/')) + '/';
					var linkSuffix = $('.next',$this).attr('href').substr($('.next',$this).attr('href').lastIndexOf('/'));
					var newNextUrl = linkTemplate + data.image.nextId + linkSuffix;
					var newPrevUrl = linkTemplate + data.image.prevId + linkSuffix;
					var newEnlargeUrl = linkTemplate + data.image.id + linkSuffix;

					$('.next',$this).attr('href',newNextUrl);
					$('.prev',$this).attr('href',newPrevUrl);
					$('.launch-image',$this).attr('href',newEnlargeUrl);
					
					$this.find('.page-current').text(data.image.position);

					if (typeof data.image.article_snippet != 'undefined') {
						$this.find('.article-snippet').text(data.image.article_snippet).append('...');
						$this.find('.article-url').text(data.image.article_title).attr('href',data.image.article_url);
						$this.find('.read-full-article').attr('href',data.image.article_url);
						$this.find('.thumb-image-link').attr('href',data.image.article_url);
						$this.find('.comments').attr('href',data.image.article_url + '#comments');
						$this.find('.launch-send-to-friend').attr('href','user/recommend-to-friend/type/article/id/' + data.image.id + '/');
						$this.find('.comments').text(data.image.comment_count);
					}
				});
			}
		});
		return false;
	});
}

jQuery.fn.inpageWidgetNav = function() {

	$('ul a', $(this)).click(function() {

		id = $(this).attr('id').substr(4);

		$(this).parent().siblings().removeClass('current');

		$(this).parent().addClass('current');

		$this = $(this).parents('.component-container');

		$('.thumb .current', $this).fadeOut(200, function() {

			$(this).removeClass('current');

			$('#gi-' + id).addClass('current').fadeIn(400);

		});

		return false;

	});

	$('.next', $(this)).click(function() {

		$this = $(this).parents('.component-container');

		$('.thumb .current', $this).fadeOut(200, function() {

			$(this).removeClass('current');

			if ($(this).next().length > 0) {

				$('.sub-nav .current', $this).removeClass('current').next().addClass('current');

				$(this).next().addClass('current').hide().fadeIn(400);

			}

			else {

				$('.sub-nav .current', $this).removeClass('current');

				$('.sub-nav li:first', $this).addClass('current');

				$('.thumb .launch-image:first', $this).addClass('current').hide().fadeIn(400);

			}

			$('.sub-nav span.page-current', $this).html($('.thumb .current').attr('rel'));

			

		});

		return false;

	});

	$('.prev', $(this)).click(function() {

		$this = $(this).parents('.component-container');

		$('.thumb .current', $this).fadeOut(200, function() {

			$(this).removeClass('current');

			if ($(this).prev().length > 0) {

				$('.sub-nav .current', $this).removeClass('current').prev().addClass('current');

				$(this).prev().addClass('current').hide().fadeIn(400);

			}

			else {

				$('.sub-nav .current', $this).removeClass('current');

				$('.sub-nav li:last', $this).addClass('current');

				$('.thumb .launch-image:last', $this).addClass('current').hide().fadeIn(400);

			}

			$('.sub-nav span.page-current', $this).html($('.thumb .current').attr('rel'));

		});

		return false;

	});

}


/* Font sizing */
/* ********************************************************************************* */

jQuery.fn.fontSizing = function (p) {
	var sizes = new Array('xs','s','m','l','xl','xxl');
	var currentFontSize = 2;
	if ($.cookie('font-size')) {
		currentFontSize = $.cookie('font-size');
		$('body')
		.removeClass()
		.addClass('resize')
		.addClass(sizes[currentFontSize]);
	}
	$('body').data('font-size', parseInt(currentFontSize))
	$(this).click(function() {
		currentFontSize = $('body').data('font-size');
		if (currentFontSize + parseInt(p) >= 0 && currentFontSize + parseInt(p) <= 5) {
			currentFontSize = currentFontSize + parseInt(p);
			$.cookie('font-size', currentFontSize, { expires: 999, path: '/', domain: BASE_SERVER });
			$('body')
			.data('font-size', currentFontSize)
			.removeClass()
			.addClass('resize')
			.addClass(sizes[currentFontSize]);
		}
		return false;
	});
}

/* Centre any element */
/* ********************************************************************************* */

jQuery.fn.center = function (positionTop) {
	this.css('position','absolute');
	if ( isNaN(positionTop) ) positionTop = ( $(window).height() - this.height() ) / 2+$(window).scrollTop();
	if ( positionTop < 0 ) positionTop = 50;
	this.css('top', positionTop + 'px');
	this.css('left', ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + 'px');
	return this;
}


/*
 * Add as an overlay to the page. Takes an options object
 * parameter which can contain the following parameteres:
 * 
 *  - selector: a CSS ID (including the hash)
 *  - html: the raw HTML for the overlay
 *  
 * In reality, it's the soak.addOverlay function that does
 * most of the work.
 */

soak.popup = function(o, positionTop) {

	var positionTop;
	if ($('._soak_overlay').length) {
		if (jQuery.browser.msie) {
			$('._soak_overlay').remove();
			soak.addOverlay(o, positionTop);
		}
		else {
			$('._soak_overlay').fadeOut(function() {
				$('._soak_overlay').remove();
				soak.addOverlay(o, positionTop);
			});
		}
	}
	else {
		soak.addOverlay(o, positionTop);
	}
	return false;
}


/*
 * This actually places the html in the page and fades in 
 * the overlay. It also binds a click event to the standard
 * Soak close class selector '_soak_overlay_close'.
 */

soak.addOverlay = function(o, positionTop) {

	$('body').append(o.html);
	
	if (o.beforeFadeIn) {
		o.beforeFadeIn();
	}	
	
	$(o.selector).center(positionTop);

	if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
		$(o.selector).append('<iframe src="' + BASE_URL + '/assets/ie6iframe.html" class="iframer">&nbsp;</iframe>');
		$('.iframer').css('width', ($(o.selector).width() - 0) + 'px');
		$('.iframer').css('height', ($(o.selector).height() - 14) + 'px');
	}
	if (jQuery.browser.msie) {
		$(o.selector).show(1);
		if (o.onFinish) {
			setTimeout(o.onFinish,100);
		}
	}
	else {
		$(o.selector).fadeIn(o.onFinish);
	}
	return false;	
};


/*
 *  This is called to setup a form to be submitted via AJAX.
 *  It's effectively just a wrapper around the jquery.form.js
 *  plugin in order for us to put in the Soak hidden field
 *  of '_soak_ajax_request' which tells the server side that
 *  the request has come from an AJAX form submission.
 */

soak.ajaxForm = function(form, o) {

	o = $.extend({
		dataType : 'json'
	}, o || {});

	appendTo = form = $(form);

	if (form.find('fieldset').length) {
		appendTo = $(form.find('fieldset').eq(0));
	}

	appendTo.prepend('<input type="hidden" name="_soak_ajax_request" value="1"/>');

	form.ajaxForm(o);
}


/*
 * This is called when you want to request a page via AJAX. This may
 * not behave how you expect though. Because of the component system
 * we're using, it will only return the main component of the target
 * page, rather than the entire page. It will also return an AJAX
 * specific version of the main component which will likely be
 * different from the one you'd see if you navigated to the page 
 * directly.
 */

soak.ajaxLink = function(o) {
	
	o = $.extend({
		element : false,
		success : false,
		async: false
	}, o || {});

	if (!o.element || !o.success) {
		
		return true;
	}

	el = $(o.element);
	url = el.attr('href');
	
	splitUrl = url.split('#');
	
	if (splitUrl[1] != undefined) {
		
		anchor = '#' + splitUrl[1];
	}
	else {
		
		anchor = '';
	}
	


	if (el.attr('href').indexOf('?') == -1) {
		url = splitUrl[0] + '?_soak_ajax_request=1&rand=' + new Date().getTime() + anchor;
	}
	else {
		url = splitUrl[0] + '&_soak_ajax_request=1&rand=' + new Date().getTime() + anchor;
	}
	
	$.ajax({
		type: 'GET',
		url: url,
		dataType: 'json',
		async: o.async,
		success: function(data, textStatus) {	
			if (data.redirectUrl) {
				location.href = data.redirectUrl
				return false;
			}
			o.success(data, textStatus);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert(soak.genericMessage);
			if (o.error) {
				o.error(XMLHttpRequest, textStatus, errorThrown);
			}
		}
	});
	return false;
}

soak.initCarousel = function(carousel) {
	var $parent = carousel.container.parent();
	
	$('.carousel-control ul a', $parent).bind('click', function() {
		carousel.scroll(jQuery.jcarousel.intval($(this).attr('rel')));
		return false;
	});

	$('.carousel-next', $parent).bind('click', function() {
		if ($('.carousel-control .active', $parent).next('li').length > 0) {
			carousel.next();
		}
		return false;
	});
 
	$('.carousel-prev', $parent).bind('click', function() {
		if ($('.carousel-control .active', $parent).prev('li').length > 0) {
			carousel.prev();
		}
		return false;
	});
}

// update jCarousel navigation
soak.afterCarousel = function(carousel, object, index, action) {
	var $parent = carousel.container.parent();

	index = index % $('.carousel-control li', $parent).length || $('.carousel-control li', $parent).length;
	$('.carousel-control li', $parent).removeClass('active').find('a[rel="' + index + '"]').parent().addClass('active');
}

/* Animation for displaying results in poll component */
/* ********************************************************************************* */
/* Function to populate regions in the form to upload images when javascript enabled*/
soak.populateRegions = function () {
	$('#frm-competition-region').val('');
	$('#frm-competition-region').children().remove();
	var countryId = $('#frm-competition-country').val();
	if (countryId) {
		for (var key in competitionRegionsData[countryId]) {
			  if (competitionRegionsData[countryId].hasOwnProperty(key)) {
				  $('#frm-competition-region').append('<option value="' + key + '">' + competitionRegionsData[countryId][key] + '</option>');
			  }
		}
	} else {
		$('#frm-competition-region').append('<option value=""> -- please select -- </option>');
	}
}
/* Function to populate regions in the filter when javascript enabled*/
soak.populateFilterRegions = function () {
	$('#frm-filter-region').val('');
	$('#frm-filter-region').children().remove();
	var countryId = $('#frm-filter-country').val();
	if (countryId) {
		for (var key in competitionRegionsData[countryId]) {
			  if (competitionRegionsData[countryId].hasOwnProperty(key)) {
				  $('#frm-filter-region').append('<option value="' + key + '">' + competitionRegionsData[countryId][key] + '</option>');
			  }
		}
	} else {
		$('#frm-filter-region').append('<option value=""> -- please select -- </option>');
	}
}

/* Function to launch the poll overlay */
soak.showPollOverlay = function () {
	var pollobj = $('.launch-poll');
	soak.ajaxLink({
		element : pollobj,
		success : function(data, textStatus){

			soak.popup({
				selector : '#launch-poll',
				html : data.html,
				destroyOnClose: false,
				onFinish: function() {

					soak.ajaxForm('#launch-poll form', {
						success : function(data, textStatus) {				

							soak.popup({
								selector : '#launch-poll',
								html : data.html
							});
						}
					});
				}
			});
		}
	});
}

/* Function to launch the MPU overlay */
soak.showMpuOverlay = function () {
	var mpuobj = $('.launch-mpu');
	soak.ajaxLink({
		element : mpuobj,
		success : function(data, textStatus){

			soak.popup({
				selector : '#launch-mpu',
				html : data.html,
				destroyOnClose: true
			});

			setTimeout("$('#launch-mpu').hide()", 1000 * MPU_TIMEOUT);
		}
	});
}

