$(function(){
	$("html").removeClass("no-js");
    
	//CYCLE HOMEPAGE IMAGES.
	/*
    if ($("#banner").size()) {
		$("#banner").append("<div class=\"bannerNav\"></div>").cycle({
			fx: "scrollUp",
			slideExpr: "img",
			timeout: 5000,
			speed: 1000,
			pause: 1,
			pager: ".bannerNav"
		});
	};
    */

	$('#footer-panels .panel').click(function() {
		window.location = $('a:first', this).attr('href');
	});
    
    form_step_count = $('.form_step').size();
    if (form_step_count)
    {
        form_step_last = form_step_count-1;
        $('.form_step:gt(0)').slideUp(300);
        
        $('.form_step:gt(0)').append('<div class="form_button" id="back"><a href="#" onclick="return form_continue(false);">Back</a></div>');
        $('.form_step:lt('+form_step_last+')').append('<div class="form_button"><a href="#" onclick="return form_continue(true);">Continue</a></div>');
    
    };   
    
    $('#sent_by_ajax').val('1');
    $("#contact form").after('<div id="display_message" />');
        
    $('#contact form').submit(function() {
        $.ajax({
            url: $(this).attr('action'),
            type: 'POST',
            data: $(this).serialize(),
            dataType: 'json',
            success: function(data, textStatus)
            {
                $("#display_message").html('');
                if (data.error) {                    
                    for (x in data.error) {
                        $("#display_message").append('<strong class="error">'+data.error[x]+'</strong>');
                    };
                } else if (data.success) {
                    $("#display_message").append('<strong class="success">'+data.success+'</strong>');
                    
                    $("#contact form").unbind('submit').submit(function() {
                        $("#display_message").append('<p>Your enquiry has already sent! Refresh the page if you wish to send another enquiry.');
                        return false;
                    });
                };
            }   
            
        });
        return false;
    });
    
    if ($("#testimonials #content").size()) {
        
        $("#testimonials #content").before('<div id="testimonials_buttons"><div class="pager"></div><a href="#" class="prev">Previous</a><a href="#" class="next">Next</a></div>').cycle({
            slideExpr: ".testimonial",
            fx: "scrollHorz",
            timeout: 0,
            nowrap: 1,
            slideResize: 1,
            prev: "#testimonials_buttons .prev",
            next: "#testimonials_buttons .next",
            pager: "#testimonials_buttons .pager",
            after: function(currSlideElement, nextSlideElement, options, forwardFlag) {
                
                var scroll_height = $(nextSlideElement).outerHeight(true);
                //alert(scroll_height);
                
                $('#testimonials #content').animate({                    
                    height: scroll_height+'px'
                }, 500);
            }
        });
        
        $(".pager").append(" of "+$(".pager a:last").text());
    
    };
    
	var gallery_in_process = 0;

    if ($('#gallery #content').size()) {
    
    	$("#gallery #content").before('<div id="gallery_buttons"><a href="#" class="prev">Previous</a><a href="#" class="next">Next</a></div>');
    
    	$('.gallery_thumb:first').addClass('active');
	    
	    $('.gallery_thumb a').click(function() {
	    	if ($(this).parent('.gallery_thumb').hasClass('active') || gallery_in_process == 1) {
	    		return false;
	    	} else {
		    	return gallery_load($(this));
		    };
	    });
	    
	    $('#gallery_buttons .next').click(function() {

	    	if ($('.gallery_thumb.active').next('.gallery_thumb').size() && gallery_in_process == 0) {
	    		return gallery_load($('.gallery_thumb.active').next('.gallery_thumb').children('a'));
	    	} else {
	    		return false;
	    	};	    
	    	
	    });
	    
	    $('#gallery_buttons .prev').click(function() {

	    	if ($('.gallery_thumb.active').prev('.gallery_thumb').size() && gallery_in_process == 0) {
	    		return gallery_load($('.gallery_thumb.active').prev('.gallery_thumb').children('a'));
	    	} else {
	    		return false;
	    	};	    
	    	
	    });
	    	    
	    function gallery_load(gallery_thumb) {
	    	
			gallery_in_process = 1;
	
	    	$('.gallery_thumb').removeClass('active');
	    	$(gallery_thumb).parent('.gallery_thumb').addClass('active');
	    	
	    	$('<img />').attr('src', $(gallery_thumb).attr('href')).hide().load(function() {
	    		
	    		var new_image = $(this);
	       		$('#gallery_big img').fadeOut(500, function() {
	    			$(this).remove();
	    			$('#gallery_big').append(new_image);
	    			$(new_image).fadeIn(500, function() {
						gallery_in_process = 0;
					});
	    		});    		
	    		
	    	});
	    	
	    	return false;
	    	
	    };
    
    };

    
});

var form_step_count = 0;
var form_step_current = 0;
function form_continue(direction)
{
    
    if (direction)
    {
        var form_step_next = form_step_current+1;
    }
    else
    {
        var form_step_next = form_step_current-1;
    };
    $('.form_step:eq('+form_step_current+')').slideUp(500);
    $('.form_step:eq('+form_step_next+')').slideDown(500);
    form_step_current = form_step_next;
    
    return false;
    
}



