// JavaScript Document

$(document).ready(function() {
	$(".advBanner").advBanner();
	$("form[name=form]").each(function() {
		$(this).validate({
			rules: {
				title: {
					required: true
				},
				firstnames: {
					required: true
				},
				lastname: {
					required: true
				},
				sub_email: {
					required: true,
					email: true
				},
				sub_email_confirm: {
					required: true,
					email: true,
					equalTo: "#sub_email"
				},
				address1_1: {
					required: true
				},
				tel_home: {
					required: true
				},
				address1_postcode: {
					required: true
				}
			}
		});
	});
});

// jQuery Functions
(function($) {
	jQuery.fn.advBanner = function(options) {
		var defaults = {
			height: '180'
		};
		var options = $.extend(defaults, options);
		var img_style = 'display: none; position: absolute; top: 0px; left: 0px; width: 100%; height: '+options.height+'px; background-color: transparent;';
		var txt_style = 'display: block; position: absolute; left: 0px; bottom: 0px; width: 95%; _width: 100%;';
		var navi_style = 'background-repeat: no-repeat; background-position: 50% 50%;';
		return $(this).each(function() {
			function swapIt(i, container) {
				if(i == '+1') {
					i = container.next().find('a.selected').attr('rel');
					i++;
					if(i == count) i = 0;
				} else if(i == '-1') {
					i = container.next().find('a.selected').attr('rel');
					i--;
					if(i == '-1') i = (count-1);
				}
				container.next().find('a[rel='+i+']').addClass('selected').siblings().removeClass('selected');
				container.append('<div class="img" style="'+img_style+'background-image:url('+path_arr[i]+')"><span class="txt" style="'+txt_style+'color:'+color_arr[i]+'">'+title_arr[i]+'</span></div>');
				container.find('div:last').fadeIn(function() {
					container.find('div:first').remove();
				});
				// check if url exist
				if(url_arr[i] != null) {
					container.find('div:last').attr({title: url_arr[i]}).css({cursor: 'pointer'}).bind('click', function() {
						document.location = url_arr[i];
					})
				}
			}
			// add links container below the banner
			$(this).after('<div class=advBannerLinks></div>');
			// build title, url image path array
			var count = $(this).find('img').length;
			var title_arr = new Array();
			for (i=0; i<=count-1; i++) {
				title_arr.push($(this).find('img').eq(i).attr('title'));
			}
			var url_arr = new Array();
			for (i=0; i<=count-1; i++) {
				url_arr.push($(this).find('img').eq(i).attr('rel'));
			}
			var path_arr = new Array();
			for (i=0; i<=count-1; i++) {
				path_arr.push($(this).find('img').eq(i).attr('src'));
				$(this).next().append('<a href=#adv rel='+i+'>'+(i+1)+'</a>');
			}
			var color_arr = new Array();
			for (i=0; i<=count-1; i++) {
				if($(this).find('img').eq(i).attr('color'))
					color_arr.push($(this).find('img').eq(i).attr('color'));
				else
					color_arr.push('#000000');
			}
			// add first image container
			$(this).append('<div title="'+title_arr[0]+'" class="img" style="'+img_style+'background-image:url('+path_arr[0]+')"><span class="txt" style="'+txt_style+'color:'+color_arr[0]+'">'+title_arr[0]+'</span></div>').children().show();
			if(url_arr[0] != null) {
				$(this).find('div:last').attr({title: url_arr[0]}).css({cursor: 'pointer'}).bind('click', function() {
					document.location = url_arr[0];
				})
			}
			$(this).next().find('a:first').addClass('selected');
			// add arrows
			$(this).next().prepend('<a href="#prev" rel="-1" class="leftArrow" style="'+navi_style+'">&nbsp;</a>').append('<a href="#next" rel="+1" class="rightArrow" style="'+navi_style+'">&nbsp;</a>')
			// remove all img's
			$(this).find('img').remove();
			$(this).css({position: 'relative', height: options.height + 'px', background: 'url(images/loading.gif) no-repeat 50% 50%'}).show();
			// add listener to each link
			$(this).next().find('a').bind('click', function() {
				swapIt($(this).attr('rel'), $(this).parent().prev());
				clearTimeout(t);
			})
			// auto-swap function
			function autoSwap(i, container) {
				if(i == count) {
					clearTimeout(t);
					swapIt(0, container)
				} else {
					swapIt(i, container);
					i++
					t = setTimeout(function() { autoSwap(i, container); }, 5000);
				}
			}
			autoSwap(0, $(this));
		});
	};
})(jQuery);