var buffer = new Array();

$(document).ready(function() {
	initAll();
	registerListeners();
});

function initAll(){
}

function registerListeners() {
$('#module_eventsignup_submit').live('click', function(e){
	verifyEventSignup();
});

$('.module_newslettersignup_button').live('click', function(e){
	var _id = $(this).attr('id').replace(/\D/gi, '');
	var _url = $('.newslettersignup_url').val();
	var _email = $('#module_newslettersignup_input_'+_id).val();
	if(_email.length>4){
		newsletterSignup(_id, _url, _email);
	}
});

$('.module_searchbar_submit').live('click', function(e) {
	var id = $(this).attr('id').replace(/\D/gi, '');
	$('#module_searchbar_debug_'+id).hide();
	var term=$('#module_searchbar_term_'+id).val();
	if(term.length<4){
		$('#module_searchbar_debug_'+id).show();
	}else{
		$('#module_searchbar_form_'+id).submit();
	}
});

	register_eventsignupListeners();

	register_userListeners();

	register_contactform();
	
	register_gallery();
}

/* AJAX */
function ajax_check(_data) {
	return _data;
}

/* GALLERY */
function register_gallery() {
	$('.c_style_1 .module_gallery .module_gallery_thumbnail').live('click', function(e){
		var id = $(this).attr('id').replace(/\D/gi, '');
		getGalleryImage(id);
	});
	$('.c_style_1 .module_gallery .gallery_menu_prv').live('click', function(e){
		var id = $(this).attr('id').replace(/\D/gi, '');
		var n_id = $('#module_gallery_'+id+' .module_gallery_content img').attr('id').replace(/\D/gi, '');
		var _id = $('#module_gallery_thumbnail_'+n_id+' input[name=img_prv]').val();
		getGalleryImage(_id);
	});
	
	$('.c_style_1 .module_gallery .gallery_menu_nxt').live('click', function(e){
		var id = $(this).attr('id').replace(/\D/gi, '');
		var n_id = $('#module_gallery_'+id+' .module_gallery_content img').attr('id').replace(/\D/gi, '');
		var _id = $('#module_gallery_thumbnail_'+n_id+' input[name=img_nxt]').val();
		getGalleryImage(_id);
	});
	
	$('.c_style_1 .module_gallery .gallery_menu_zoom').live('click', function(e){
		var id = $(this).attr('id').replace(/\D/gi, '');
		var n_id = $('#module_gallery_'+id+' .module_gallery_content img').attr('id').replace(/\D/gi, '');
		var url = $('#module_gallery_'+id+' .gallery_menu_zoom input[name=gallery_single_url]').val()+n_id+'.html';
		window.location = url;
	});	
	
	
}
function getGalleryImage(_id) {
	var rid = $('#module_gallery_thumbnail_'+_id+' input[name=img_parent]').val();
	var url = $('#module_gallery_'+rid+' input[name=interface]').val();
	$.ajax({
		url: url,
		type: 'POST',
		data: ({
			req: setHeader(),
			action: 'getImage_gallery',
			id: _id
		}),
		success: function(myreturn) {
		myreturn = ajax_check(myreturn);
		if(myreturn) {
			$('#module_gallery_'+rid+' .module_gallery_content').html(myreturn);
		}
	}
	});
	
}

/* encode input for GETS */
function urlencode(str) {
	return escape(str).replace('+', '%2B').replace('%20', '_').replace('*', '%2A').replace('/', '%2F').replace('@', '%40');
}

/* CONTACT FORM */
function register_contactform() {
	$('.module_contactform button').live('click', function(e){
		var id = $(this).attr('id').replace(/\D/gi, '');
		var _url = $('#form_interface_'+id).val();
		var _title=parseText($('#module_contactform_'+id+' input[name=subject]').val());
		var _email=parseText($('#module_contactform_'+id+' input[name=email]').val());
		var _message=parseText($('#module_contactform_'+id+' textarea').val());
		
		$.ajax({
			type: 'POST',
			url: _url,
			data: ({
				req: setHeader(),
				action: 'contactform_submit',
				subject: _title,
				email: _email,
				message: _message,
				id: id
			}),
			success: function(myreturn) {
			myreturn = ajax_check(myreturn);
			if(myreturn) {
				$('#module_contactform_'+id+' .form_debug').html(myreturn);
			}
		}
		});
		
	});
}

/* event signup */
function register_eventsignupListeners() {
	$('.module_eventsignup input[type=text]').live('click', function(e){
		var temp = $(this).attr('name');
		var t_def = $('.form_content_'+temp+' > .form_input_default').html();
		if($(this).val()==t_def){
			$(this).val('');
		}
	});
	
	$('input').live('focusout', function(e){
		var temp = $(this).attr('name');
		var t_def = $('.form_content_'+temp+' > .form_input_default').html();
		if($(this).val()==''){
			$(this).val(t_def);
		}
	});
	
	$('.evt_signupItem_positions').live('click', function(e){
		var id = $(this).attr('id').replace('evt_signupItem_positions_', '');
		if($('#evt_signupItem_cansignup_'+id).val()=='1'){
			var url = $('#event_signup_item_'+id+' .evt_signupItem_status a').attr('href');
			window.location=''+url;
		}
	});
}
function verifyEventSignup(){
	var _url = $('#module_eventsignup_submit input[name=interface]').val();
	var _id = $('#module_eventsignup_submit input[name=eventid]').val();

	var _forename = parseText($('.module_eventsignup_form input[name=forename]').val());
	var _surname = parseText($('.module_eventsignup_form input[name=surname]').val());
	var _email = parseText($('.module_eventsignup_form input[name=email]').val());

	var _carrier = parseText($('.module_eventsignup_form input[name=carrier]').val());
	var _mobile = parseText($('.module_eventsignup_form input[name=mobile]').val());
	var _count = parseText($('.module_eventsignup_form select[name=count]').val());
	var _newsletter = parseText($('.module_eventsignup_form input[name=newsletter]:checked').val());

	sendSignupData(_id, _url, _forename, _surname, _email, _carrier, _mobile, _count, _newsletter);
}
function sendSignupData(_id, _url, _forename, _surname, _email, _carrier, _mobile, _count, _newsletter){
	signUpDebug('loading...');
	$.ajax({
		type: 'POST',
		url: _url,
		data: ({
			id: _id,
			forename: _forename,
			surname: _surname,
			email: _email,
			carrier: _carrier,
			mobile: _mobile,
			count: _count,
			newsletter: _newsletter
		}),
		success: function(myreturn) {
			signUpDebug(myreturn);
		}
	});	
}
function signUpDebug(_msg){
	if(_msg=='clear') {
		$('.module_eventsignup_form .form_debug').html('');
		$('.module_eventsignup_form .form_debug').hide();		
	}else{
		$('.module_eventsignup_form .form_debug').html(_msg);
		$('.module_eventsignup_form .form_debug').css('display','block');
	}
}
function newsletterSignup(_id, _url, _email){
	signUpDebug('loading...');
	$.ajax({
		type: 'POST',
		url: _url,
		data: ({
			email: parseText(_email)
		}),
		success: function(myreturn) {
			newsletterDebug(_id, myreturn);
		}
	});	
}
function newsletterDebug(_id, _msg){
	if(_msg=='clear') {
		$('#module_newslettersignup_feedback_'+_id).html('');
		$('module_newslettersignup_feedback_'+_id).hide();		
	}else{
		$('#module_newslettersignup_feedback_'+_id).html(_msg);
		$('#module_newslettersignup_feedback_'+_id).css('display','block');
	}
}

/* user settings */
function register_userListeners() {
	$('.module_userfeedback_logout').live('click', function(e){
		$('#module_userfeedback_form_logout	').submit();
	});
	
	$('.login_button').live('click', function(e){
		$('#user_login').submit();
	});
	
	$('.module_userfeedback_newsletter_status').live('click', function(e){
		$('#module_userfeedback_form_newsletter').submit();
	});
	
	$('.module_userfeedback_event_confirm').live('click', function(e){
		var _question = $('#module_userfeedback_events_prompt_cancel').val();
		var _confirm = window.confirm(_question);
		if(_confirm!=false){
			var id = $(this).attr('id').replace(/\D/gi, '');
			$('#module_userfeedback_form_event_'+id).submit();
		}
	});
}

/* fullscreen resize for background image */
(function($){
	$.fn.fullscreenr = function(options) {
		if(options.height === undefined) alert('Please supply the background image height, default values will now be used. These may be very inaccurate.');
		if(options.width === undefined) alert('Please supply the background image width, default values will now be used. These may be very inaccurate.');
		if(options.bgID === undefined) alert('Please supply the background image ID, default #bgimg will now be used.');
		var defaults = { width: 1280,  height: 1024, bgID: 'img_background' };
		var options = $.extend({}, defaults, options);
		$(document).ready(function() { $(options.bgID).fullscreenrResizer(options);	});
		$(window).bind("resize", function() { $(options.bgID).fullscreenrResizer(options); });
		return this;
	};
	$.fn.fullscreenrResizer = function(options) {
		// Set bg size
		var ratio = options.height / options.width;
		// Get browser window size
		var browserwidth = $(window).width();
		var browserheight = $(window).height();
		// Scale the image
		if ((browserheight/browserwidth) > ratio){
		    $(this).height(browserheight);
		    $(this).width(browserheight / ratio);
		} else {
		    $(this).width(browserwidth);
		    $(this).height(browserwidth * ratio);
		}
		// Center the image
		$(this).css('left', (browserwidth - $(this).width())/2);
		$(this).css('top', (browserheight - $(this).height())/2);
		return this;
	};
})(jQuery);
var FullscreenrOptions = {  width: 2000, height: 1500, bgID: '#img_background' };
jQuery.fn.fullscreenr(FullscreenrOptions);

/* set header related information */
function setHeader(){
	return '';
}
