	
	var advertBlocks = [];
	
	$(document).ready(function() {
	
		/* weird homepage radio-links */
	
		$(".findandbook").each(function(){
			var panel = $(this);
			
			$(this).find(".radio-links").hide().after('<div class="radios"></div><a target="_blank" class="go-button-a"><input type="submit" class="go-button" value="GO"/></a>');
			var rcount = 0;
			$(this).find(".radio-link").each(function(){
				var href= $(this).attr('href');
				var text= $(this).text();
				panel.find(".radios").append('<div class="radio-line"><input id="r'+rcount+'" type="radio" name="href" value="'+href+'"><label for="r'+rcount+'" > '+text+'</label></div>');
				rcount++;
			});
			
			var submitlink = $(this).find(".go-button-a");
			
			$(this).find(".radio-line input:first").attr('checked','checked');
			submitlink.attr('href', $(this).find(".radio-line input:first").attr('value'));
			
			$(this).find(".radio-line input").click(function(){
				var href = $(this).attr('value');
				submitlink.attr('href', href);
			});
			
			
		});	
		
		
	
		/* tabbed functionality */
		
		$(".group-tabs").each(function(){
		
			var tabIndex;
			
			// tabs
			tabIndex = 0;
			$(this).find(".field-field-tab .field-item").each(function(){
				tabIndex ++;
				$(this).attr("command", tabIndex);
				$(this).html($(this).html() + "<div class=\"right\"></div>");
				if (tabIndex == 1){$(this).addClass("active")}
				$(this).click(function(){
					var activateTab = $(this).attr("command");
					var group = $(this).parents(".group-tabs");
					group.find(".field-field-tab .field-item").removeClass("active");
					$(this).addClass("active");
					group.find(".field-field-tab-content .field-item").hide();
					group.find(".field-field-tab-content .field-item-"+activateTab).show();
					
				});
			});
			
			// content
			tabIndex = 0;
			$(this).find(".field-field-tab-content .field-item").each(function(){
				tabIndex ++;
				if (tabIndex != 1){$(this).hide()}
				$(this).addClass("field-item-"+tabIndex);
			});
			
			// get current tab if linked to from somewhere else
			//$.getUrlVar('tab')
			// link should be ?tab=selectedtab
			
		});
		
		/* Multi-ad functionality */
		
		
		$(".view-Advertisements").each(function(){
			var advertBlock = {};
			advertBlock.index = advertBlocks.length;
			advertBlock.parent = $(this);
			advertBlock.timer = setTimeout( "nextAdByBlockIndex("+advertBlock.index+")", 10000 );
			
			
			$(this).attr("command", advertBlock.index);
			$(this).addClass("advertBlock-" + advertBlock.index);
			
			var advertCount = 0;
			$(this).find(".views-row").each(function(){
				advertCount ++;
				$(this).attr("command", advertCount);
			});
			$(this).find(".views-row:first").addClass("active");
						
			if (advertCount > 1){
				$(this).find(".view-header").html("<div class=\"left\">&laquo;</div>" + $(this).find(".view-header").html() + "<div class=\"right\">&raquo;</div>")
			}
			
			$(this).find(".view-header .left").click(function(){
				prevAd($(this).parents(".view-Advertisements"));
			});
			$(this).find(".view-header .right").click(function(){
				nextAd($(this).parents(".view-Advertisements"));
			});
			
			advertBlocks.push(advertBlock);
		});
		
	
   		/* label & button replacements */
   		
		$("#edit-comment-wrapper label").text("Comment:");
		$("#edit-name-1-wrapper label").text("Name:");
		$("#edit-name-wrapper label").text("Name:");
		$("#edit-mail-wrapper label").text("Email:");
		
		$("#edit-message-wrapper label").text("Message:");
		$("#edit-captcha-response-wrapper label").text("Code:");
		
		$("#edit-subject").val("Tubemap.com Contact Form");
		$("#edit-subject-wrapper").hide();
		$("#edit-copy-wrapper").hide();
		
		
		$(".service-updates a").each(function(){
			$(this).attr("href", "http://www.tfl.gov.uk" + $(this).attr("href"));
			$(this).attr("target", "_blank");
		});
		

	});
	
	function nextAdByBlockIndex(input){
		nextAd($(".advertBlock-" + input+":first"));
	}
	function prevAd (jAdBlock){
		var current = jAdBlock.find(".views-row.active");
		var target = current.prev();
		if(!target || target.length == 0){
			target = jAdBlock.find(".views-row:last");
		}
		jAdBlock.find(".views-row").removeClass("active");
		target.addClass("active");
		showAd(jAdBlock);
	}
	function nextAd (jAdBlock){
		var current = jAdBlock.find(".views-row.active");
		var target = current.next();
		if(!target || target.length == 0){
			target = jAdBlock.find(".views-row:first");
		}
		jAdBlock.find(".views-row").removeClass("active");
		target.addClass("active");
		showAd(jAdBlock);
	}
	function showAd(jAdBlock){
		var current = jAdBlock.find(".views-row.active");
		if(!current || current.length == 0){
			current = jAdBlock.find(".views-row:first");
		}
		var index = parseInt(current.attr("command"));
		//alert(index);
		jAdBlock.find(".view-content").animate({marginLeft: -parseInt(index-1)*300}, 800);
		
		var blockIndex = jAdBlock.attr("command");
		//alert(jAdBlock.length);
		var advertBlock = advertBlocks[blockIndex];
		clearTimeout(advertBlock.timer);
		advertBlock.timer = setTimeout( "nextAdByBlockIndex("+advertBlock.index+")", 8000 );
		
	}
	
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});
		
		
		
		
