// Document ready.....
$(document).ready(function() {
	scroller()
	showmenu()
	showhide()				// Show hide shows based on filter and do odd even again.
	imagesfancybox()		// Fancybox for images.
	externallinks()
	clearinputext()
	videofancybox()
	homepageshows()			// Accordian for shows on the home page
	artistautocomplete()	// Ajax get artist data(xml) parse and apply autocomplete to div
	competitionpopup()
	showlightbox()
});

function showlightbox() {
	$("a[href=#emailform]").fancybox({
		"hideOnContentClick": false,
		"frameWidth": 500, 
		"frameHeight": 400
	});
}

function showmenu() {
	$("#more").click(function() {
		var state = $(this).hasClass("closed");
		if (state) {
    		$(this).removeClass("closed").addClass("open");
			$("#more-options").fadeIn("fast");
  		} else {
    		$(this).removeClass("open").addClass("closed");
			$("#more-options").fadeOut("fast");
  		}
		return false;
	});
	$(this).not("a#more").click(function(){
		//alert('not more');
		var state = $("#more").hasClass("open");
		if (state) {
    		$("#more").removeClass("open").addClass("closed");
			$("#more-options").fadeOut("fast");
  		}
	});
}

function competitionpopup(){
	$("a[href='#competition']").fancybox({
		'autoDimensions': false,
        'width': 400,
        'height': 'auto',
        'hideOnContentClick': false,
        'titleShow': false,
        'showNavArrows': false
	});
}

function scroller() {
	$("#right").click(function() {
		$("#content-upcoming li:first-child").addClass("moving");	//tag the left most visiable item
		var moveleft = $(".moving").outerWidth(true);				//get the width of the item to be scrolled
		$(".moving").clone().appendTo($("#content-upcoming ul")).removeClass("moving");	//copy it and stick on the begining
		
		$("#content-upcoming ul").animate({
			left: ['-='+moveleft, 'swing']							//slide the menu left
		}, 500, "swing", function() {
			$(".moving").remove();									//delete the buffer item
			$("#content-upcoming ul").removeAttr("style");			//reset the margin
		});
		return false;
	});
	
	$("#left").click(function() {
		$("#content-upcoming li:last-child").addClass("moving");
		var moveright = $(".moving").outerWidth(true);
		$(".moving").clone().prependTo($("#content-upcoming ul")).removeClass("moving");
		$("#content-upcoming ul").css("left", "-"+moveright+"px");	//shift the whole ul so the copied item can't be seen
		
		$("#content-upcoming ul").animate({
			left: ['0', 'swing']
		}, 500, "swing", function() {
			$(".moving").remove();
			$("#content-upcoming ul").removeAttr("style");
		});
		return false;
	});
}

function homepageshows() {
	$(".show-1").addClass("selected");
	
	// if there is only one image then add the single class so it is bigger than the standard thumbnail.
	$('.show-images').each(function(index) {
		if($(this).children().size() == 1) {
			$(this).find('li').addClass('single')
		}
	});
	
	$("#content-london li").click(function() {
		if($(this).hasClass("selected") == true){return};
		$(this).addClass("clicked").children().fadeOut(200);
		$("#content-london li.selected").addClass("closing").children().fadeOut(200);
		$("#content-london li.selected").removeClass("selected", 500, function() {
			$(".closing .show-date,.closing .show-title,.closing .show-artists").fadeIn(200, function () {
				$("#content-london li.closing").children().removeAttr("style");
				$("#content-london li.closing").removeClass("closing");
			});
		});
		$("#content-london li.clicked").addClass("selected", 500, function() {
			$("#content-london li.clicked").children().fadeIn(500)
			$("#content-london li.clicked").removeClass("clicked");
		});
	});
	
	$("#content-nottingham li").click(function() {
		if($(this).hasClass("selected") == true){return};
		$(this).addClass("clicked").children().fadeOut(200);
		$("#content-nottingham li.selected").addClass("closing").children().fadeOut(200);
		$("#content-nottingham li.selected").removeClass("selected", 500, function() {
			$(".closing .show-date,.closing .show-title,.closing .show-artists").fadeIn(200, function () {
				$("#content-nottingham li.closing").children().removeAttr("style");
				$("#content-nottingham li.closing").removeClass("closing");
			});
		});
		$("#content-nottingham li.clicked").addClass("selected", 500, function() {
			$("#content-nottingham li.clicked").children().fadeIn(500)
			$("#content-nottingham li.clicked").removeClass("clicked");
		});
	});
}

// add target="blank" for external links
function externallinks() {
	$("a[rel='external']").attr("target", "_blank");
}

function clearinputext() {
	var defaulttext = ""
	$("input[type=text]").focusin(function(){
		defaulttext = $(this).val();
		if (defaulttext.indexOf("...") > 0) {
			$(this).val("");
		}
	});
	$("input[type=text]").focusout(function(){
		if ($(this).val() == "") {
			$(this).val(defaulttext);
		}
	});
}

function showhide(){
	$(".showhide").click(function() {
		var filter = $(this).attr('id');
		switch (filter)
		{
			case 'all': {
				$("div.sixth").show();
				break 
			}
			default: { 
				$("div.sixth").hide();
				$("." + filter).show();
				break
			}
		}
		return false;
	});
}

function imagesfancybox(){
	if ($("li.artist-image").length){ // No point burdening every page with this request.
		$("li.artist-image a:has(img)").fancybox({
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'autoScale'		: 	true,
		'speedIn'		:	600, 
		'speedOut'		:	200,
		'cyclic'		: 	true,
		'titlePosition' :	'over' 
		});
		return false;
	}
}

function videofancybox(){
	if ($("img.video-thumb").length){ // No point burdening every page with this request.
		$("img.video-thumb").click(function() {
		var url = $(this).attr('id');
		$.fancybox({
				'padding'			: 0,
				'autoScale'			: true,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'title'				: this.title,
				'href'				: url.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'				: 'swf',
				'swf'				: {
				'wmode'				: 'transparent',
				'allowfullscreen'	: 'true'
				}
			});
		return false;
		});
	}
}

function artistautocomplete(){
	if ($("#artistname").length){ // No point burdening every page with this request.
		$.ajax({
			type: "POST",
			url: '/writeXML/',
			data: 'type=artists',
			success: function(xmlResponse) {
				// alert(xmlResponse);
				// Get each node from the returned xml
				var data = $("child", xmlResponse).map(function() {
					return {
						// Get the name and id for each one
						value: $("artistname", this).text(),
						url: $("urlname", this).text()
					};
				}).get();
				$("#artistname").autocomplete({
					source: data,
					minLength: 0,
					select: function(event, ui) {
						window.location.href = '/comedian/' + (ui.item.url) + '/';					
					}
				});
			}
		})
	}
}
