
	jQuery.ajaxSetup({'beforeSend':function(xhr){xhr.setRequestHeader('Accept','application/json')}})



	//	Post Comments With Ajax
	;(function($) {
		$.fn.postWithAjax = function(settings) {
			var option = $.extend({}, $.fn.postWithAjax.defaultSettings, settings)

			return this.submit(function() {
				form = $(this)

				if ($('.alert.error')[0]) $('.alert.error').slideUp(500, function() { $(this).remove() })

				form.append('<input name="ajax" type="hidden" value="true" />')
				$.post(form.attr('action'), form.serialize(), handleResponse, option.type)
				return false;
			})

			function handleResponse(data) {
				var text = $('body').data('text')

				switch (data.status) {
					case 'win' :
						form.find('button').fadeOut()
						form.find('textarea')
							.fadeOut(250, function() {
								$(this).remove()
								success_message = '<div class="alert done"><h3>'+text.comment_posted+'</h3><p>'+text.comment_success+'</p></div>';
								$(success_message).css({ display : 'none' }).prependTo('.textarea_holder').slideDown()
							})
					break;

					case 'fail' :
						error_message = '<div class="alert error">';
						error_message+= '<h3>'+text.crikey+'</h3>';
						error_message+= '<p>'+text.went_wrong+'</p>';
						error_message+= '<ul>';

						for (i = data.errors.length - 1; i < data.errors.length; i++) {
							field = data.errors[i].field;
							error = data.errors[i].error;

							$('#' + field).addClass('error')

							error_message += '<li>' + error + '</li>';
						}

						error_message += '</ul>';
						error_message += '</div>';

						$(error_message).css({ display : 'none' }).prependTo('.textarea_holder').slideDown()
					break;
				}
			}
		}

		$.fn.postWithAjax.defaultSettings = { type : 'html' }
	})(jQuery);



	//	Warn on Exit
	;(function($) {
		$.fn.warnOnExit = function() {
			return this.click(function() {

				$('object').fadeOut(250)

				link = $(this)

				lang       = $('html').attr('lang')
				target_url = link.attr('href')

				qs = 'lang='+lang+'&target_url=' + target_url;

				if (link.parent().hasClass('clic_yes')) qs += '&clic_site=true';

				link.colorbox({
					href:    '/incs/actions/warn_on_exit.php?' + qs,
					opacity: .5,
					open:    true,
					width:   300
				})

				return false;
			})
		}

		$("a.colorbox_close").live('click', function() {
			$.fn.colorbox.close()
			return false;
		})
	})(jQuery);



	//	Build and Trigger qTip
	;(function($) {
		var text = $('body').data('text')

		$.fn.buildAndTriggerQtip = function(settings) {
			var option = $.extend({}, $.fn.buildAndTriggerQtip.defaultSettings, settings)

			qtip_config = { position : { corner : { target : 'topMiddle', tooltip : 'bottomMiddle' } }, hide : { delay : 3000, when : { event: 'inactive' } }, show : { when : { event: 'focus' } }, style : { border : { radius : 5, width : 5 }, name : option.styleType, tip : { corner : 'bottomMiddle', size : { x : 8, y : 6 } } }, text : false }

			return this
				.attr('title', option.titleContent)
				.qtip(qtip_config)
				.focus()
				.blur()
		}

		$.fn.buildAndTriggerQtip.defaultSettings = {
			titleContent : 'No flippin\' message!',
			styleType : 'red'
		};
	})(jQuery);



	//	Increase a comment's "like" count with Ajax, doesn't it?
	;(function($) {
		$.fn.likeCommentWithAjax = function() {
			var text = $('body').data('text')

			return this.click(function() {
				like_link = $(this)

				$.getJSON(like_link.attr('href'), { ajax : true }, function(data) {
					if (data.status == 'win') {
						like_count = like_link.parent().find('.like_count')

						current_count = parseInt(like_count.text())

						like_count
							.text(current_count + 1)
							.buildAndTriggerQtip({
								titleContent : text.thanks_like,
								styleType : 'green'
							})

						like_link.replaceWith('<span>'+text.you_like+'</span>')
					} else {
						like_link.buildAndTriggerQtip({
							titleContent : text.like_login,
							styleType : 'red'
						})
					}
				})

				return false;
			})
		}
	})(jQuery);



	//	Report Abuse and that
	;(function($) {
		$.fn.reportAbuseWithAjax = function() {
			return this.one('click', function() {
				link = $(this)

				$.getJSON(link.attr('href'), { ajax : true }, function(data) {
					var text = $('body').data('text')

					if (data.status == 'win') {
						link
							.buildAndTriggerQtip({
								titleContent : text.thanks_help,
								styleType : 'green'
							})
					} else {
						link
							.buildAndTriggerQtip({
								titleContent : text.oh_dear,
								styleType : 'red'
							})
					}
				})

				link.removeAttr('href')

				return false;
			})
		}
	})(jQuery);



	//	Multi Images
	;(function($) {
		$.fn.multiImages = function(settings) {
			var option = $.extend({}, $.fn.multiImages.defaultSettings, settings)

			this.each(function() {
				multi  = $(this)

				images = $('ul.full li:not(.video) img')
				$('<ul class="clearfix thumbs"></ul>').insertAfter('ul.full')

				$('ul.full li.video').each(function() {
					video = $(this)

					video_info = video.attr('class').split(' ')[1];

					video_type = video_info.split('!')[0];
					video_id   = video_info.split('!')[1];

					video_config = {
						params: { allowfullscreen: true, allowscriptaccess: 'always', wmode: 'transparent' },
						height: 280,
						width: 370
					}

					if (video_type == 'youtube') {
						video_config.swf = 'http://www.youtube.com/v/'+video_id;
						video_config.flashvars = { fs: 1, hl: 'en', rel: 0 }
					} else {
						video_config.swf = 'http://vimeo.com/moogaloop.swf';
						video_config.flashvars = { clip_id: video_id, color: 'ffffff', fullscreen: 1, server: 'vimeo.com', show_byline: 1, show_portrait: 0, show_title: 1 }
					}

					video.flash(video_config)

					if (images.length > 0) $('<li class="video"><a href="#"><img src="/incs/themes/common/img/icons/video.png" alt="" /></a></li>').appendTo('ul.thumbs')
				})

				if (images.length > 0 || $('ul.full li.video')[0]) {
					images.each(function() {
						image     = $(this)
						image_src = image.attr('src')
						src_array = image_src.split('/')
						filename  = src_array[src_array.length - 1];

						$('<li><a href="#"><img src="/img/thumbs/' + filename + '" alt="" /></a></li>').appendTo('ul.thumbs')
					})
				}

				$('.full').cycle({
					speed:   250,
					timeout: 0,
					pager:   '.thumbs',
					pagerAnchorBuilder: function(idx) {  return '.thumbs li:eq(' + idx + ') a'; }
				})
			})

			return this;
		}

		$.fn.multiImages.defaultSettings = {}
	})(jQuery);



	//	BOTB Switcher
	;(function($) {
		$.fn.botbSwitcher = function() {
			var text = $('body').data('text')

			return this.each(function() {
				switcher = $(this)

				$('<ul><li><a class="botb_pic_link" href="#">'+text.photo+'</a></li><li><a class="botb_vid_link" href="#">'+text.video+'</a></li></ul>').appendTo(switcher)

				$('a.botb_pic_link', switcher).click(function() {
					$(this).closest('ul').siblings('.botb_image').css({ display: 'block', visibility: 'visible' })
					$(this).closest('ul').siblings('.botb_video').css({ display: 'none',  visibility: 'hidden'  })

					return false;
				}).click()

				$('a.botb_vid_link', switcher).click(function() {
					$(this).closest('ul').siblings('.botb_image').css({ display: 'none',  visibility: 'hidden'  })
					$(this).closest('ul').siblings('.botb_video').css({ display: 'block', visibility: 'visible' })

					return false;
				})
			})

		}
	})(jQuery);



	//	BOTB Video
	;(function($) {
		$.fn.botbVideo = function() {
			return this.each(function() {
				video = $(this)

				video_info = video.attr('class').split(' ')[1];

				video_type = video_info.split('!')[0];
				video_id   = video_info.split('!')[1];

				video_config = {
					params: {
						allowfullscreen: true,
						allowscriptaccess: 'always',
						wmode: 'transparent'
					},
					height: 211,
					width: 280
				}

				if (video_type == 'youtube') {
					video_config.swf = 'http://www.youtube.com/v/' + video_id;
					video_config.flashvars = { fs: 1, hl: 'en', rel: 0 }
				} else {
					video_config.swf = 'http://vimeo.com/moogaloop.swf';
					video_config.flashvars = {
						clip_id: video_id,
						color: 'ffffff',
						fullscreen: 1,
						server: 'vimeo.com',
						show_byline: 1,
						show_portrait: 0,
						show_title: 1
					}
				}

				video.flash(video_config)
			})
		}
	})(jQuery);



	//	Vote for Band
	;(function($) {
		$.fn.botbVoteWithAjax = function() {
			var text = $('body').data('text')

			return this.click(function() {
				link = $(this)
				href = $(this).attr('href')

				link.removeAttr('href')

				if ($('.alert')[0]) $('.alert').remove()

				$.getJSON(href, { ajax : true }, function(data) {
					if (data.status == 'win') {
						$('.bands_list p.voted').replaceWith('<p class="vote"><a href="/incs/actions/vote_for_band.php?band_id='+band_id+'">'+text.switch_vote+'</a></p>')
						link.parent().replaceWith('<p class="voted">'+text.voted+'</p>')
						band_id = href.split('=')[1];
						$('.bands_list p.vote a').replaceWith('<p class="vote"><a href="/incs/actions/vote_for_band.php?band_id='+band_id+'">'+text.switch_vote+'</a></p>')
					} else {
						error_message = '<div class="alert warning">';
						error_message+= '<h3>'+text.brother+'</h3>';
						error_message+= '<ul>';

						for (i = 0; i < data.errors.length; i++) {
							field = data.errors[i].field;
							error = data.errors[i].error;

							$('#' + field).addClass('error')

							error_message += '<li>' + error + '</li>';
						}

						error_message += '</ul>';
						error_message += '</div>';

						from_p = link.parent().siblings('p.from')

						$(error_message).insertAfter(from_p)
					}
				})

				link.attr('href', href)

				return false;
			})
		}
	})(jQuery);



	//	Fieldify
	;(function($) {
		$fieldify = $.fieldify = function() {
			if ($.browser.msie && parseInt($.browser.version) == 6) {
				$.each([$('button'), $('input'), $('select'), $('textarea')], function() {
					el = $(this)
					el
						.hover(
							function() { $(this).addClass('hover') },
							function() { $(this).removeClass('hover') }
						)
						.focus(function() { $(this).addClass('focus') })
						.blur(function() { $(this).removeClass('focus') })
				})
			}

			return this;
		}
	})(jQuery);



	//	is IE 6
	;(function($) {
		$isIE6 = $.isIE6 = function() {
			return $.browser.msie && parseInt($.browser.version) == 6;
		}
	})(jQuery);

	//	is IE 7
	;(function($) {
		$isIE7 = $.isIE7 = function() {
			return $.browser.msie && parseInt($.browser.version) == 7;
		}
	})(jQuery);

	//	is lt IE 8
	;(function($) {
		$is_lt_IE8 = $.is_lt_IE8 = function() {
			return $.browser.msie && parseInt($.browser.version) < 8;
		}
	})(jQuery);



	//	BG Position Picker
	;(function($) {
		$.fn.bgPosPicker = function() {
			bg_pos = $(this)

			$('input:checked', bg_pos).closest('li').addClass('checked')

			$('li', bg_pos).click(function() {
				$('input', bg_pos).removeAttr('checked')
				$('li.checked', bg_pos).removeClass('checked')
				$(this)
					.addClass('checked')
					.find('input')
						.attr('checked', 'checked')
			})

			return this;
		}
	})(jQuery);



	//	Network Banner Map
	;(function($) {
		$clicmap = $.clicmap = function() {
//			var text = $('body').data('text')

			$('<img />').attr('src', '/incs/themes/common/img/network_banner/network_hanger.png')
			$('<img />').attr('src', '/incs/themes/common/img/network_banner/county_hanger.png')

//			$('<div class="hanger_close">'+text.close_this+'</div>')
			$('<div class="hanger_close">Close This</div>')
				.appendTo('.hanger')
					.click(function() {
						$(this)
							.parent()
							.css({ display: 'none', visibility : 'hidden' })
						return false;
					})

			$('.hanger_open').click(function() {
				hanger = '.' + $(this).attr('id')

				$('.hanger').css({ display: 'none', visibility : 'hidden' })

				if ($(this).attr('id') != 'county_picker') $(hanger).css({ left : ($(this).position().left - 10) + 'px' })

				$(hanger).css({ display : 'block', visibility : 'visible' })

				return false;
			})

			counties_pos = { start: 0, anglesey: 242, blaenau: 484, bridgend: 726, caerphilly: 968, cardiff: 1210, carmarthenshire: 1452, ceredigion: 1694, conwy: 1936, denbighshire: 2178, flintshire: 2420, gwynedd: 2662, merthyr: 2904, monmouthshire: 3146, neath: 3388, newport: 3630, pembrokeshire: 3872, powys: 4114, rhondda: 4356, swansea: 4598, torfaen: 4840, vale: 5082, wrexham: 5324 };

			var map_src = '/incs/themes/common/img/network_banner/county_map_js.png';

			$('<img />')
				.attr('src', map_src)
				.load(function() {
					$('.county_map').css('background-image', 'url(' + map_src + ')')

					if ($('a.current_site')[0]) {
						var current_county = $('a.current_site').parent().attr('id').split('-')[0];

						$('.county_map').css({ backgroundPosition : '-'+counties_pos[current_county]+'px 0'})
					}

					$('.county_map img')
						.attr('src', '/incs/themes/common/img/network_banner/trans.png')
						.css('background-image', 'url(' + map_src + ')')

					$('map#counties area').hover(
						function() { map_highlight($(this).attr('class')) },
						function() { map_highlight('start') }
					)

					$('.counties_list ul li a').hover(
						function() { map_highlight($(this).parent().attr('id')) },
						function() { map_highlight('start') }
					)

					map_highlight('start')
				})

			$(".county_picker *[href]").warnOnExit()
		}

		function map_highlight(county) {
			if (county) county_pos = county.split('-')[0];

			$('.county_map img').css({ backgroundPosition : '-'+counties_pos[county_pos]+'px -300px'})

			$('.counties_list a').removeClass('hover')
			$('.counties_list .'+county+' a').addClass('hover')
		}
	})(jQuery);



	//	Vimeo Thinger
	;(function($) {
		$.fn.vimeo = function(settings) {
			var option = $.extend({}, $.fn.vimeo.defaultSettings, settings)

			return this.each(function() {
				el = $(this)
				el.flash({
					params: {
						allowfullscreen: true,
						allowscriptaccess: 'always',
						wmode: 'transparent'
					},
					height: option.height,
					width: option.width,
					swf: 'http://vimeo.com/moogaloop.swf',
					flashvars: {
						clip_id: option.id,
						color: '09d551',
						fullscreen: 1,
						server: 'vimeo.com',
						show_byline: 1,
						show_portrait: 0,
						show_title: 1
					}
				})
			})
		}

		$.fn.vimeo.defaultSettings = { id: false, width: 300, height: 225 }
	})(jQuery);



	//	Simple Email Address Obfuscation.. Not perfect, but will do for now..
	;(function($) {
		$.fn.desEmail = function() {
			return this.each(function() {
				email = $(this)

				email_array = email.text().split(' AT ')
				local_part  = email_array[0];
				domain      = email_array[1];

				address = local_part + '@' + domain;

				email
					.empty()
					.replaceWith('<a href="mailto:' + address + '">' + address + '</a>')
			})
		}
	})(jQuery);



	;(function($) {
		$.fn.prepThemeForm = function() {
			if ($('input#colour')[0]) {
				$('<div id="colour_picker"></div>').insertBefore('input#colour')
				$('#colour_picker').farbtastic('input#colour')

				$('.bg_pos').bgPosPicker()
			}

			return this;
		}
	})(jQuery);



	;(function($) {
		$.fn.themeRater = function() {
			return this.each(function() {
				rater = $(this)
				$('button', rater).remove()

				rater_options = {
					callback: function (ui, type, value) {
						current_rater = ui.element;

						theme_id   = $('input[name=theme_id]',   current_rater).val()
						tip_anchor = $('.ui-stars-star-on:last', current_rater)

						if (value == 0) tip_anchor = $('.ui-stars-cancel', current_rater)

						post_action = current_rater.attr('action')

						post_vars = {
							ajax:     true,
							rating:   value,
							theme_id: theme_id
						}

						$.post(post_action, post_vars, function(data) {
							qTipStyle = data.status == 'win' ? 'green' : 'red';
							tip_anchor.buildAndTriggerQtip({ titleContent: data.message, styleType: qTipStyle })
						}, 'json')
					}
				}

				if (rater.hasClass('disabled')) rater_options.disabled = true;

				rater.stars(rater_options)
			})
		}
	})(jQuery);



	;(function($) {
		$.fn.registerWhat = function() {
			var org_p = $(this)

			var text = $('body').data('text')

			var org_label = $('label', org_p)
			var org_span  = $('span',  org_p)

			org_span.css('display', 'none')

			$('<span class="reg_trigger">(<a href="#">' + text.what_is_this + '</a>)</span>').insertAfter(org_label)

			$('.reg_trigger a').click(function() {
				org_span.toggle()
				return false;
			})

			return this;
		}
	})(jQuery);



	//	That's the anthem, get your damn hands up!
	$(function() {

		var translations = {
			en : {
				brother:         'Oh brother!',
				close_this:      'Close This',
				comment_posted:  'Your Comment Has Been Posted',
				comment_success: 'Your comment was successfully submitted and is awaiting approval. It will appear here once it has been moderated.',
				crikey:          'Crikey',
				like_login:      'You can\'t like a comment if you\'re not logged in!',
				no_message:      'Oh no! There isn\'t a message to display!',
				oh_dear:         'Oh dear.. That didn\'t quite work as planned..',
				photo:           'Photo',
				switch_vote:     'Switch your vote',
				thanks_help:     'Thanks for your help!',
				thanks_like:     'Thank you for liking this comment!',
				video:           'Video',
				voted:           'You voted for this band!',
				went_wrong:      'Something went wrong while we were submitting your comment. Check these errors and try again!',
				what_is_this:    'What is this?',
				you_like:        'You Like This'
			},
			cym : {
				brother:         'O Na!',
				close_this:      'Caewch Hwn',
				comment_posted:  'Postiwyd eich Sylw',
				comment_success: 'Cyflwynwyd eich sylw’n llwyddiannus ac mae’n disgwyl am ei gymeradwyo. Bydd yn ymddangos yma wedi iddo gael ei safoni.',
				crikey:          'O diar',
				like_login:      'Ni allwch roi sylw os nad ydych wedi logio i mewn!',
				no_message:      'O na! Does dim neges i’w harddangos!',
				oh_dear:         'O diar... Doedd hyn’na ddim wedi gweithio fel y dylai..',
				photo:           'Ffotograffau',
				switch_vote:     'CYM - Switch your vote',
				thanks_help:     'Diolch i chi am eich cymorth!',
				thanks_like:     'Diolch i chi am hoffi’r sylw hwn!',
				video:           'Fideo',
				voted:           'CYM - You voted for this band!',
				went_wrong:      'Aeth rhywbeth o’i le pan oeddech yn cyflwyno eich sylw. Gwiriwch y gwallau hyn a rhowch gynnig arall arni!',
				what_is_this:    'What is this?',
				you_like:        'Rydych yn Hoffi Hwn'
			}
		};

		var language = $('html').attr('lang')

		var text = language == 'en' ? translations.en : translations.cym;

		$('body')
			.addClass('js')
			.data('text', text)

		$.fieldify()

		if (!$.isIE6() && !$.isIE7()) $.clicmap()

		$('.multi').multiImages()
		$('.like_this').likeCommentWithAjax()
		$('.report_abuse').reportAbuseWithAjax()
		$('.ajax_form').postWithAjax({ type: 'json' })
		$('.simple_email').desEmail()
		$("#amp-slider").easySlider({ auto: false, continuous: false })
		$("#amp-tabs").tabs()
		$('.star_rater').themeRater()
		$('input#colour').prepThemeForm()
		$('.orgs_checks').registerWhat()
		$('.botb_switcher').botbSwitcher()
		$('.botb_video').botbVideo()
		$('.bands_list p.vote a').botbVoteWithAjax()
	})
