jQuery(function($){$(document).ready(function(){

	$("#rb_search_input").on("focus",function(){
		var ip = $(this);
		if(ip.val() == ip.attr('title')){
			ip.val('');
		}
	}).on("blur",function(){
		var ip = $(this);
		if(ip.val() == ""){
			var ipt = ip.attr('title');
			ip.val(ipt);
		}
	});

	if($("body").hasClass("page-template-tpl_media-php")){
		$("#mtv_thumbs_inner a:first").addClass("current");
		$("#mtv_thumbs_inner a").live("click",function(){
			if(!$(this).hasClass("current")){
			
				$("#mtv_thumbs_inner a.current").removeClass("current");
				$(this).addClass("current");
				nv = $(this).attr("href");
				$("param[name='movie']").attr("value",nv);
				$("#main_vid").attr("src",nv);
				nc = $("#mtv_video").html();
				$("#mtv_video").fadeOut("fast",function(){
					$(this).html("").html(nc).fadeIn(2000);
				});

			}
		return false;});
		
		$.getJSON('http://gdata.youtube.com/feeds/users/MetricaMeasures/uploads?alt=json-in-script&callback=?&max-results=12', function(data) { 
			
			var jsc = 1;
			$.each(data.feed.entry, function(i, item) {
				var updated = item.updated;
				var title = item.title.$t;
				var def_url = "http://www.youtube.com/v/<def>?fs=1&amp;hl=en_GB&amp;rel=0&amp;showinfo=0"
				var url = item['media$group']['media$content'][0]['url'];
				url = url.replace("http://www.youtube.com/v/","");
				url = url.replace("?version=3&f=user_uploads&app=youtube_gdata","");
				url = def_url.replace("<def>", url);
				var thumb1 = item['media$group']['media$thumbnail'][1]['url'];
				var thumb2 = item['media$group']['media$thumbnail'][2]['url'];
				var thumb3 = item['media$group']['media$thumbnail'][3]['url'];
				var numViews = item['yt$statistics']['viewCount'];
				var new_thumb = "<a href=\""+url+"\" title=\""+title+"\"><img src=\""+thumb1+"\" alt=\""+title+"\" /><img src=\""+thumb2+"\" alt=\""+title+"\" /><img src=\""+thumb3+"\" alt=\""+title+"\" /></a>";
				$("#mtv_thumbs_inner").append(new_thumb);
				if(jsc == 1){
					$("param[name='movie']").attr("value",url);
					$("#main_vid").attr("src",url);
					nc = $("#mtv_video").html();
					$("#mtv_video").fadeOut("fast",function(){
						$(this).html("").html(nc).fadeIn(2000);
					});
				}
				jsc++;
			});
			var photo_animation_timer = 0;
			$("#mtv_thumbs_inner a").mouseenter(function(){
				var count = 0;
				var images = $("img", this);
				clearInterval(photo_animation_timer);
				photo_animation_timer = setInterval(function(){
					images.eq(count).hide();
					count++;
					if(count == 3){
						count = 0;
						images.show();
					}
				}, 500);
			}).mouseleave(function(){
				clearInterval(photo_animation_timer);
				$("img", this).each(function(){
					$(this).show();
				});
			});
		});
		
	}
	
	$("#s_keyfeatures").attr("target", "_blank");
	$("#quote_rotation").cycle({
		cleartype: false
	});
	
	$(".op_left a").hover(function(){
		var name = $("img", this).attr("alt");
		$("#hper").text(name).show();
		$(".op_left").mousemove(function(e){
			$("#hper").css('top', e.clientY).css('left', e.clientX);
		});
	},function(){
		$("#hper").hide();
	});
	$("#home_search_input").focus(function(){
		if($(this).attr("title") == $(this).val()){
			$(this).val("");
		}
	});
	$("#home_search_input").blur(function(){
		if($(this).val() == ""){
			$(this).val($(this).attr("title"));
		}
	});
	
		
	$(".nu_aside_box_links li a").live("click",function(){
		$(this).parents(".nu_aside_box_links").children(".nu_active").removeAttr("class");
		$(this).parent().addClass("nu_active");
		
		var h = $(this).attr("href");
		$(h).parent().children().each(function(){
			$(this).hide()
		});
		$(h).show();
		
	return false;});
	
	$("#side_nav li").not(".current_page_item").not(".current_page_ancestor").not(".current_page_parent").has("ul.children").children("ul.children").remove();
	
	$(".slot").live("click",function(){
		
		var pa = $(this).parents(".slot_wrap");
		if(pa.hasClass("expanded") == false){
			$(".expanded").animate({
				width:62
			}, 200).removeClass("expanded");
			$(".hotspot").fadeTo(10, 0);
			$(".hotspot", this).fadeTo(10,.2);
			var wd = pa.width();
			if(wd == 62){
				pa.animate({
					width:642
				},200).addClass("expanded");
			}
		}
	});
	$(".hotspot").fadeTo(200, 0);
	$(".hotspot").hover(function(){
		var pa = $(this).parents(".slot_wrap");
		if(pa.hasClass("expanded") == false){
			$(this).stop().fadeTo(200,.1);
		}
	},function(){
		var pa = $(this).parents(".slot_wrap");
		if(pa.hasClass("expanded") == false){
			$(this).fadeTo(200,0);
		}
	});
	
	$(".slot_wrap:first").animate({width:642}, 1);
	
	$(".hotspot:first").fadeTo(200, .2);

	$("#tweets").tweet();

	$('#menu-top_nav').superfish({ 
	            delay:       1000,
	            animation:   {opacity:'show',height:'show'},
	            speed:       'fast',
	            autoArrows:  false,
	            dropShadows: false 
	        }); 

	$(".op_left a,#client_list ul li").each(function(){ $(this).fadeTo("fast", .6); });
	
	$(".emp,.client_wrap").each(function(){	$(this).hide();	});
	$("#client_list ul li:first").addClass("ow_active").fadeTo("fast", 1);
	$(".op_left a:first").addClass("op_active").fadeTo("fast", 1);



	var active = $("#client_list ul li:first").children("a").attr("href");
	$(active).slideDown("fast").addClass("owd_active");
	
	var active = $(".op_left a:first").attr("href");
	$(active).slideDown("fast").addClass("opd_active");
	
	$("#client_list ul li a").live("click", function(){

		$(".ow_active").removeClass("ow_active").fadeTo("fast", .6);
		var toshow = $(this).attr("href");
		$(this).parent().fadeTo("fast", 1).addClass("ow_active");

		$(".owd_active").removeClass("owd_active").fadeOut("fast",function(){
			$(toshow).fadeIn("fast").addClass("owd_active");
		});
		
	
	return false;});
	
	$(".op_left a").live("click", function(){
	
		$(".op_active").removeClass("op_active").fadeTo("fast", .6);

		var toshow = $(this).attr("href");

		$(this).fadeTo("fast", 1).addClass("op_active");

		$(".opd_active").removeClass("opd_active").fadeOut("fast",function(){
			$(toshow).fadeIn("fast").addClass("opd_active");
		});
		
	return false;});
	
	$("#fsubmit").live("click",function(){
		var errors = "";
		$("#cuf_wrap .val").each(function(){
				val = $(this).val();
				tit = $(this).attr('title');
				if(val === '' || val === tit){
					errors = errors+"<li>&mdash; "+tit+" is empty<\/li>";
				}else{
					if($(this).hasClass("val_email")){
						if(!check_email($(this).val())){
							errors = errors+"<li>&mdash; "+tit+" must be a valid email<\/li>";
						}
					}
				}
				
		});
		if(errors !== ""){
			$(".error").slideUp("fast",function(){
				$(".error ul").html('');
				$(".error ul").html(errors);
			});
			$(".error").slideDown("slow");
		}else{
			$(".error").slideUp("fast");
			$("#cuf_wrap form").slideUp("fast",function(){
				$("#success").slideDown("slow");
				var info = $('#cuf_wrap form').serialize();
				var url = $('#cuf_wrap form').attr('action');
				$.ajax({ type: "POST", url: url, data: info, success: function(){ }});
			});
		}
	return false;});
	
	$("#req_pop form").submit(function(){
		var errors = "";
		$(".val", this).each(function(){
				val = $(this).val();
				tit = $(this).attr('title');
				if(val === '' || val === tit){
					errors = errors+"<li>&mdash; "+tit+" is empty<\/li>";
				}else{
					if($(this).hasClass("val_email")){
						if(!check_email($(this).val())){
							errors = errors+"<li>&mdash; "+tit+" must be a valid email<\/li>";
						}
					}
				}
				
		});
		if(errors !== ""){
			$(".rer").slideUp("fast",function(){
				$(".rer ul").html('');
				$(".rer ul").html(errors);
			});
			$(".rer").slideDown("slow");
		}else{
			$(".rer,#cuiw").slideUp("fast");
			$(".req_inn", this).slideUp("fast",function(){
				$(".success").slideDown("slow");
				var info = $('#req_pop form').serialize();
				var url = $('#req_pop form').attr('action');
				$.ajax({ type: "POST", url: url, data: info, success: function(){ }});
			});
		}

		return false;
	
	});
	
	
	$("#cuf_wrap .l_row input").each(function(){
		var val = $(this).attr("title");
		$(this).val(val);
	});
	
	$("#cuf_wrap .l_row input").blur(function(){
		var value = $(this).val();
		var title = $(this).attr('title');
		if(value === ""){
			$(this).val(title);
		}
	});
	
			
	$(".req_row input,.l_row input").focus(function(){
		if($(this).val() == $(this).attr('title')){
			$(this).val('');
		}
	});
	


	$('.val_phone,#req_phone').bind('keyup',function(){ 
	    var myValue = $(this).val();
	    $(this).val( myValue.replace(/[^0-9- ]/g,'') );
	});

	
	function check_email(emailAddress) {
	 		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	 		return pattern.test(emailAddress);
	}
	
	$("#foot_switch a").live("click",function(){
	
		var st = "show sitemap";
		var ht = "hide sitemap";
	
		$("#foot_map").slideToggle("slow");
		if($(this).text() == st){
			$(this).text(ht);
			$(this).parent().animate({paddingTop: 10},500);
			$(this).removeClass("open_foot").addClass("close_foot");
		}else if($(this).text() == ht){
			$(this).parent().animate({paddingTop: 50},500);
			$(this).text(st);
			$(this).removeClass("close_foot").addClass("open_foot");

		}

	return false;
	});
	
	$(".exp").live("click", function(){
		$(this).nextAll(".minfo").slideToggle("fast");
		if($(this).hasClass("sh")){
			$(this).removeClass("sh");
		}else{
			$(this).addClass("sh");
		}
	});
	
	$("#request_demo,#s_bookdemo,.bam").live("click",function(){
		// TRUMPETS! Oh, wait, the popup function...
		var wd = $(document).width();
		var hg = $(document).height();
		$('html, body').animate({scrollTop:0}, 'slow');
		$("#req_pop").fadeIn("fast",function(){
			$("#req_bg").width(wd).height(hg).show().fadeTo(1, 0, function(){
				$(this).addClass("black").fadeTo(1000, .3);
			})
		});
			
		return false;
	});
	
	$(document).keyup(function(e) {
	  if (e.keyCode == 27) {
	  	$("#req_pop").fadeOut("fast",function(){
	  		$("#req_bg").fadeOut("fast").removeClass("black");
	  	});
	  	return false;
	  }
	});
	
	$("#rss_email").live("click",function(){
		window.open('http://feedburner.google.com/fb/a/mailverify?uri=MeasurementMatters', 'popupwindow', 'scrollbars=yes,width=580,height=520');
/*
		var wd = $(document).width();
		var hg = $(document).height();
		$('html, body').animate({scrollTop:0}, 'slow');
		$("#burn_wrap").fadeIn("fast",function(){
			$("#burn_bg").width(wd).height(hg).show().fadeTo(1, 0, function(){
				$(this).addClass("black").fadeTo(1000, .3);
			})
		});
*/
	return false;});
	
	$("#burn_close,#burn_bg").live("click",function(){
		$("#burn_wrap").fadeOut("fast", function(){
			$("#burn_bg").fadeOut("fast");
		});
	return false;});
	
	$("#req_bg,.pclose").live("click",function(){
		$("#req_pop").fadeOut("fast",function(){
			$("#req_bg").fadeOut("fast").removeClass("black");
		});
		return false;
	});
	
	
	
return true;
});});
