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

function fixWidths(nd){
	var ch = $(nd).children().size();
	var width = 20*ch+5*ch;
	var ml = 905-width;
	var ml = ml+"px";
	$(nd).width(width).css("marginLeft",ml);
}
/*  print works  */

	$("#print_link").live("click",function(){
		$("#print_window").fadeIn("slow");
		epicShadow("show");
		return false;
	});
	var nuca = $(".adv_carousel div").parent().prev(".nucaco");
	$(".adv_carousel div").cycle({pause: 1,pager: nuca,before: function(){
		var title = $(this).attr('alt');
		var text  = $(this).attr('title');
		$("#nuca_title,#nuca_text").fadeOut("fast",function(){
			$("#nuca_title").text(title).fadeIn("slow");
			$("#nuca_text").text(text).fadeIn("slow");
			
		});
		
	}});
	fixWidths(nuca);
	

/* /print works  */

/*  clients page  */

jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);

};


$(".client_wrap a").live("click",function(){
	var clientid = $(this).closest("div").attr("id");
	var clientshow = clientid+'w';
	$("#"+clientshow).fadeToggle("slow");
return false;
});


$(".pl_launch").bind("click", function() {
	var fake = $(this).attr('href');
	$(fake).fadeIn('fast');
	epicShadow("show");
});
$(".content_close").live("click",function(){
	$(this).closest(".new_large_previews").fadeOut("fast");
	epicShadow("hide");
	return false;
});

$(".mlinfo").live("click", function(){
	if($(this).closest(".new_de").height() == 71){
	$(this).closest(".new_de").animate({
	  height: '471'
	}, 500);
	$(this).closest(".new_de").prev().animate({
	  height: '129'
	}, 500);
	$(".mlinfo").each(function(){
		$(this).text("less info");
	});
	}else{
	$(this).closest(".new_de").animate({
	  height: '71'
	}, 500);
	$(this).closest(".new_de").prev().animate({
	  height: '529'
	}, 500);
	$(".mlinfo").each(function(){
		$(this).text("more info");
	});
	}
	return false;
});


$(".enable_carousel div").each(function(){
	var nucacoid = $(this).parent().prev(".nucaco").attr('id');
	var nucacoid = "#"+nucacoid;
	$(this).cycle({pause: 1,pager: nucacoid});
	fixWidths(nucacoid);
	
});
$(".enable_carousel").bind("mouseenter",function(){
	      $(".new_controls").fadeIn("fast");
	    }).bind("mouseleave",function(){
	      $(".new_controls").fadeOut("fast");
	    });

/* /clients page  */

$(".req input").blur(function(){
	var thval = $(this).attr('value');
	if(thval == ""){
		$(this).prev(".err").fadeIn("slow");
	}else{
		var isemail = $(this).attr("id");
		if(isemail == "e_email"){
			var email = $(this).val();
			if(isValidEmailAddress(email)){
				$(this).prev('.err').fadeOut("fast");
				$(".row_subm input").removeAttr("disabled");
			}else {
				$(this).prev(".err").fadeIn("slow");
				$(".row_subm input").attr("disabled","disabled");
				
			}

		}else{
		$(this).prev('.err').fadeOut("fast");
		}
		
	}
});



$("#megaform").submit(function(){
var post = $("#megaform").serialize();    
var f_name = $("#e_name").val();
var f_surn = $("#e_surn").val();
var f_emai = $("#e_email").val();
if(f_name == ""){ $("#e_name").prev(".err").fadeIn("slow"); var rf = 1;}
if(f_surn == ""){ $("#e_surn").prev(".err").fadeIn("slow"); var rf = 1;}
if(f_emai == ""){ $("#e_email").prev(".err").fadeIn("slow"); var rf = 1;}
if(rf == 1){return false;}
$.ajax({
      url: "http://bythescruff.com/wp-content/themes/bts/thebrains.php",
      global: false,
      type: "POST",
      data: post,
      dataType: "html",
      success: function(msg){
         if(msg == "Your message has been sent. You should hear from us shortly."){
         	$("#megaform").slideUp("slow", function(){
         		$("#epicform").append("<p id='awesome'>Your message has been sent. You should hear from us shortly.</p>");
         		$("#awesome").fadeIn("slow");
         	});
         }else{
         alert(msg);
         }
      }
   }
).responseText;
return false;
});



	
	function isValidEmailAddress(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);
	}

$("#megaform").live("submit",function(){
	validateItAll();
	return false;
});

function epicShadow(act){
	if(act == 'show'){
		$("body").prepend("<div id='shadow'></div>");
		$("#shadow").css({"height":$(document).height(),"width":$(document).width()});
		$("#shadow").fadeTo('slow', 0.5);
	}else if(act == 'hide'){
		$("#shadow").fadeTo('slow', 0,function(){ $("#shadow").remove(); });
	}else{}
}
$("#shadow").live("click",function(){
	$(".new_large_previews:visible").fadeOut("slow");
	$(".new_large_features:visible").fadeOut("slow");
	$(this).fadeOut("fast");
});

$("#feature_wrap").cycle({fx: 'scrollLeft',pager: '#new_pager'});

$("#showreel").live('click',function(){
	epicShadow('show');
	$("#showreel_preview").fadeIn('slow');
	return false;
});

$("#croner").live('click',function(){
	epicShadow('show');
	$("#croner_preview").fadeIn('slow');
	return false;
});

$(".ccc").live('click',function(){
	$(this).parent().fadeOut('fast');
	epicShadow('hide');
	return false;
});



$("#control").mouseenter(function(){
$("#framehover").fadeIn("fast");
}).mouseleave(function(){
$("#framehover").fadeOut("fast");
});

 
$("#share_launcher a").click(function(){

$("#share_window").fadeIn("slow");
return false;
});
$("#share_close").click(function(){
$("#share_window").fadeOut("slow");
});
$("#share_email_btn").click(function(){
$("#share_form_wrap").slideDown("slow");
$("#share_media_wrap").slideUp("slow");
return false;
});
$("#share_media_btn").click(function(){
$("#share_form_wrap").slideUp("slow");
$("#share_media_wrap").slideDown("slow");
return false;
});



$("#submit_button").click(function(){
	
jQuery("#your_email").validate({
expression: "if (VAL.match(/^[^\\W][a-zA-Z0-9\\_\\-\\.]+([a-zA-Z0-9\\_\\-\\.]+)*\\@[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)*\\.[a-zA-Z]{2,4}$/)) return true; else return false;",
message: "This should be a valid email."
});
jQuery("#your_name").validate({
expression: "if (VAL) return true; else return false;",
message: "This field may not be blank."
});
jQuery("#friends_email").validate({
expression: "if (VAL.match(/^[^\\W][a-zA-Z0-9\\_\\-\\.]+([a-zA-Z0-9\\_\\-\\.]+)*\\@[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)*\\.[a-zA-Z]{2,4}$/)) return true; else return false;",
message: "This should be a valid email."
});
jQuery("#friends_name").validate({
expression: "if (VAL) return true; else return false;",
message: "This field may not be blank."
});
    var options = { 
        target:        '#result',
        beforeSubmit:  showRequest, 
        success:       showResponse

    }; 
 
    $('#share_email').submit(function() { 

        $(this).ajaxSubmit(options); 

        return false; 
    }); 

function showRequest(formData, jqForm, options) { 
    var queryString = $.param(formData); 
    return true; 
}

function showResponse(responseText, statusText)  { 
	var len = responseText.length;
	if(len == 12){$("#share_form_wrap").slideUp("slow");}
	} 
});
$("object").click(function(){
	return false;
});
$('.cycle').cycle({fx:'fade',speed:3000,timeout:2500,sync:1,pause:1});
$(".client_box,.client_single").click(function(){
	location.replace('http://bythescruff.com/our-work/');
	});
	if ($(".mmf-mail-sent-ok").length > 0){
		$(".mmf-form").slideUp("slow");
	}

return true;
});});