jQuery(document).ready(function(){
$("li.sub_cat,.t_desc").hide();
/*
$("li.par_cat").click(function() {
	var el_count = $(this).nextUntil("li.par_cat").size();
	if(el_count == 0){
		// I'll add something here later.
	}else{
	$(this).nextUntil("li.par_cat").each(function() {
		$(this).slideToggle("slow");
	});
	return false;
	}
});
*/
$(".faded").fadeTo('fast', 0.5);
$(".faded").hover(function(){
		$(this).fadeTo('fast', 1);
	},function(){
	$(this).fadeTo('slow', 0.5);
		if($("body").attr('class') == 'multimedia'){ $(".multimedia .btn_multimedia").fadeTo('fast',1);	}
		if($("body").attr('class') == 'gaming'){ $(".gaming .btn_gaming").fadeTo('fast',1);	}
		if($("body").attr('class') == 'phone'){ $(".phone .btn_phone").fadeTo('fast',1);	}
});
	$(".tab").slideUp("fast",function(){
		var first_to_open = $(".active_tab a").attr('href');
		$(first_to_open).slideDown("slow");
	});
	
	$(".tab_nav").click(function(){
		var to_open = $(this).attr('href');
		$(".active_tab").removeClass("active_tab");
		$(this).parent().addClass("active_tab");
		$(".tab").slideUp("fast");
		$(to_open).slideDown("slow");

		return false;
	});

//	alert($("li.current_cat").hasClass('sub_cat'));
	var ch_count = $("li.current_cat").nextUntil("li.par_cat").size();
	if(ch_count == 0){
		// I'll add something here later.
	}else{
	$("li.current_cat").nextUntil("li.par_cat").each(function() {
		$(this).slideDown("slow");
	});

	}
	var par_act = $('li.current_sub').prevAll('.par_cat:first');
	$(par_act).addClass('hl').nextUntil("li.par_cat").each(function() {
		$(this).slideDown("slow");
	});
	$(".breadcrumbs ul li a:first").addClass('noclick').fadeTo('fast', .6).click(function(){	return false;	});
	var to_hl = $(".breadcrumbs ul li a:last").attr('class');
	var hl_div = "#"+to_hl;
	if($(hl_div).attr('class') == 'sub_cat'){

		$(hl_div).addClass('current_sub').prevAll('.par_cat:first').addClass('hl').nextUntil("li.par_cat").each(function() {
			$(this).slideDown("slow");
		});
		
		
		
	}
	$(".multimedia .btn_multimedia").fadeTo('fast',1);
	$(".phone .btn_phone").fadeTo('fast',1);
	$(".gaming .btn_gaming").fadeTo('fast',1);
	$("a[rel=fancy]").fancybox({
					'transitionIn'		: 'elastic',
					'transitionOut'		: 'elastic',
					'titlePosition' 	: 'over',
					'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
					    return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
					}
				});
	
	$("#contact .requiredRow input,#Comment").blur(function(){
		var thval = $(this).attr('value');
		if(thval == ""){
			$(this).next().fadeIn("slow");
		}else{
			var isemail = $(this).attr("id");
			if(isemail == "Email"){
				var email = $(this).val();
				if(isValidEmailAddress(email))
				{
					$(this).next().fadeOut("fast");
					$("#row_submit input").removeAttr("disabled");
				}
				else {
					$(this).next().fadeIn("fast");
					$(this).next().text("must be a valid email");
					$(this).next().fadeIn("fast");
				}
	
			}else{
			$(this).next().fadeOut("fast");
			}
			
		}
	});
	$(".cform").submit(function(){
	var post = $('.cform').serialize();    
	var f_name = $("#Name").val();
	var f_comp = $("#Company").val();
	var f_emai = $("#Email").val();
	var f_comm = $("textarea#Comment").val();
	var f_post = $("#Postcode").val();
	var f_coun = $("#Country").val();
	var f_phon = $("#Telephone").val();
	var f_subj = $("#Subject").val();
	if(f_name == "" || f_comp == "" || f_emai == "" || f_comm == "" || f_post == "" || f_coun == "" || f_phon == "" || f_subj == ""){
	if(f_name == ""){ $("#Name").next().fadeIn("fast");}
	if(f_comp == ""){ $("#Company").next().fadeIn("fast");}
	if(f_emai == ""){ $("#Email").next().fadeIn("fast");}
	if(f_comm == ""){ $("#Comment").next().fadeIn("fast");}
	if(f_post == ""){ $("#Postcode").next().fadeIn("fast");}
	if(f_coun == ""){ $("#Country").next().fadeIn("fast");}
	if(f_phon == ""){ $("#Telephone").next().fadeIn("fast");}
	if(f_subj == ""){ $("#Subject").next().fadeIn("fast");}
	if($("#EmailConfirm").val() != $("#Email").val()){
	$("#EmailConfirm").next().text("must be the same as \"Email address\".").fadeIn("slow");
	}else{
	$("#EmailConfirm").next().fadeOut("slow");
	}
	return false;
	}
	
	$.post("http://www.adapt-mobile.com/contact/mailer/", post, function(data) { 
		$("#contact_form h2").slideUp("slow");
		$(".cform").slideUp("slow", function(){
			$("#post_back").fadeIn("slow");
		});
	});
	return false;
	});
	$("#EmailConfirm").keyup(function(){
		if($(this).val() != $("#Email").val()){
		$(this).next().text("must be the same as \"Email address\".").fadeIn("slow");
		}else{
		$(this).next().fadeOut("slow");
		}
	});
	$("#EmailConfirm").blur(function(){
		if($(this).val() != $("#Email").val()){
		$(this).next().text("must be the same as \"Email address\".").fadeIn("slow");
		}else{
		$(this).next().fadeOut("slow");
		}
	});
	
	
	
		
		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);
		}
	
	$("#open_login").click(function(){
		$("#login_form").fadeIn("slow");
		return false;
	});
	
	$(".rotation").cycle({ 
	    prev:   '#prev', 
	    next:   '#next',
	    pause:	true
	});

	$("#feature_wrap").bind("mouseenter",function(){
	      $("#mid_ban_nav").fadeIn("fast");
	    }).bind("mouseleave",function(){
	      $("#mid_ban_nav").fadeOut("fast");
	    });
	$("#carousel_ul li a").bind("mouseenter",function(){
			$("span", this).fadeTo('slow', 0.5);
		}).bind("mouseleave",function(){
			$("span", this).fadeOut("fast");
		});
});