

$(document).ready(function(){
	var windowHeight = $(window).height();
	windowHeight = windowHeight + 1;
	$("#outerwrapper").css({'height':windowHeight+'px'});
	
	var DocumentHeight = $(document).height();
	var DocumentHeight = DocumentHeight+70;

	$("<div id='modalLayer'></div>").css({"height":DocumentHeight+"px","opacity":0.5}).insertAfter("#outerwrapper").hide();
});


$(document).ready(function() {
  $('#expandList').find('dd').hide().end().find('dt').click(function() {
     $(this).next().slideToggle();
 	$('#expandList').find('dd').hide().end();
 	return false;
   });   
});

$(document).ready(function() {
  $('#capIndex').find('dd').hide().end().find('dt').click(function() {
     $(this).next().slideToggle();
 	$('#capIndex').find('dd').hide().end();
 	return false;
   });   
});


$(document).ready(function(){
	var Errors = $(".errors");
	Errors.hide();
	Errors.slideDown("slow");
});

$(document).ready(function() 
{$(".trigger").tooltip({ 
    position:['center','right'],
    offset: [0,100], 
    effect: 'fade',
    opacity: 0.9, 
    tip: '.tooltip' 
});
});


$(document).ready(function(){
	$("form:not(.search) input:not(:submit),textarea ").focus(function(){
		$(this).addClass("inputFocus");
	}).
	blur(function(){
		$(this).removeClass("inputFocus");
	}).
	keyup(function(){
		$(this).removeClass("inputFocus");
	});
});

$(document).ready(function(){
	$("fieldset").each(function(){
		var heading = $("legend",this).remove().text();
		$("<h3></h3>").text(heading).prependTo(this);
	});
});

// for use with ajax functions to get values from the url see testcart

function getUrlVars(url)
{
 
	var vars = [], hash;
    var hashes = url.slice(url.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;
}

$(document).ready(function(){
	
	var WrapperWidth = $("#wrapper").width();
	var ContainerWidth = $("#feedbackContainer").width();
	var DiffWidth = (WrapperWidth - ContainerWidth)/2;

	var Title = $("#feedbackTitle").html();
	
	$("#feedbackContainer").css({left:DiffWidth+"px",top:"0px"});

	$("form.feedback").hide();
	
	$("a.feedback").click(function() {
		$(this).hide();
		$("#modalLayer").show();	
		$("#feedbackError").hide();
		$("#feedbackContainer").css({"z-index":100}).fadeIn("slow",function(){
			$("form.feedback").slideDown("slow",function(){
				$("#feedbackTitle").html("<h2>Send us a message</h2>");
			});
			
		})
		return false;
	});
	
	$("a.close").click(function(){
		$("#feedbackContainer").slideUp("slow",function(){
			$("#modalLayer").fadeOut("slow");
			$("a.feedback").show();
			$("label").removeClass("req");
			$("label span.errorMessage").hide();
			$("#feedbackTitle").html(Title)
			$("form.feedback").clearForm().hide();
			$("#feedbackError").hide();
		});
		return false;
	});
		
});

$(document).ready(function() { 
    $('form.feedback').ajaxForm({ 
    	dataType: 'json',
    	//target: '#feedbackError',
    	url: 'ajax/feedbackAjax.php',
        success:processjson 
    }); 
});

function processjson(data)
{
	
	if (data.goodresult)  // there are no errors in the form
	{
		$("label span.errorMessage").hide();
		$("label").removeClass("req");
		
		$("form.feedback").slideUp("slow",function(){
			$("#feedbackTitle").html("<h2>Your message has been sent!</h2>");
			$("#feedbackContainer").delay(2000).fadeOut("slow",function() {
				$("#modalLayer").fadeOut("slow");
				$("a.feedback").show();
				$("#feedbackTitle").html("<h2>Loading...</h2>")
				$(this).clearForm();
			});
		});
		
	}
	else
	{
		$("label span.errorMessage").hide();
		$("label").removeClass("req");
		//$("#feedbackTitle").html("<h2>There are errors - please fix</h2>");
	
		for (var key in data) 
		{
  			if (data.hasOwnProperty(key)) 
  			{
    			$(".feedback :input#"+key).prev("label").append("<span class='errorMessage'>"+data[key]+"</span>");
    			$(".feedback :input#"+key).prev("label").addClass("req");
    			if (data.repeat)
    			{
    				$("#feedbackError").fadeIn("slow").html(data.repeat);
    			}
  			}
		}
	}	
}

$(document).ready(function() {
  $('#loading').ajaxStart(function() {
    $(this).show();
  }).ajaxStop(function() {
    $(this).hide();
  });
});


