/*
** Adams Creative
** http://www.adamscreative.co.uk/
*****************************
** Written by Simon Friend
**
*/


/** Scripts that are 
loaded on ready **/
$(document).ready(function()
{
	if ( $('.image-title').length)
	{
		$('.image-title').each( function(){
			
			// Get image title of image
			$image_title =$(this).attr('title');
			$max_image_width = (parseInt($(this).css('width')) - 6) + 'px';
			
			// Wrap a class around it and add the text
			$(this).wrap('<span class="show-image-title" />');
			$(this).parent().append('<p>' + $image_title + '</p>');
			$(this).parent().children('p').css('max-width', $max_image_width );
		});
	}
	
	// Booking form calendar functionality
	$(function() { $('.datepicker').datepicker({minDate: new Date(), dateFormat: 'D d M yy' }); });
	
	$('#nav li.main').hover( function(){
		$(this).children('.subnav').css('display', 'block');
	//	$(this).children('a.main').css('backgroundPosition', '0 -95px');
		$(this).children('.subnav').stop().animate({ top:'250px' }, 400);
		
	}, function(){
		$(this).children('.subnav').css('display', 'none');
	//	$(this).children('a.main').css('backgroundPosition', '0 0');
		$(this).children('.subnav').stop().animate({ top:'230px' }, 0);
	});
	
	$("#signup .submit").mouseover(function () {
     	$(this, 'img').stop().animate({bottom:'6px', opacity:1}, 100);
	});
	$("#signup .submit").mouseout(function () {
     	$(this, 'img').stop().animate({bottom:'9px', opacity:0.6}, 100);
	});
	
	$(".read-more").mouseover(function () {
     	$(this, 'img').stop().animate({paddingTop:'2px', opacity:1}, 100);
	});
	$(".read-more").mouseout(function () {
     	$(this, 'img').stop().animate({paddingTop:'0', opacity:0.9}, 100);
	});
	
	
	// If browser window height is less than 800 then do not scroll side menus
	if ($(window).height() > 800) $('#scrollarea .column').stickyfloat({ duration: 600, offsetY:20, lockBottom:true});

	
	//$('#product-detail .buy').click( function(){ $('#paypal_link').children('form').submit(); });
	
	$(window).politeWarning(6);
	
	$('#splash li .images').cycle({ 
		fx:    'fade', 
		speed:  4000
	 });
	
	$('#signup #email').focus( function() {
		if ($(this).val() == 'Enter email address') $(this).val('');								
	});
	$('#signup #email').blur( function() {
		if ($(this).val() == '') $(this).val('Enter email address');								
	});
	
	// setup ul.tabs to work as tabs for each div directly under div.panes
	$(function() {
		$("#contact-tabs").tabs("#contact-panes > div", {effect: 'fade', fadeOutSpeed: 400})
	});
	

	$('#book-appointment .submit-form').click( function ()
	{
		$(this).css('display', 'none');
		$(this).next().css('display', 'block');
		$('#book-appointment').children('.msg').remove();
		$('#book-appointment .error').remove();
		

		$.ajax(
		{
			type: "POST",
			url: "/includes/form-processing.php?form=book-appointment",
			data: {name: $('#form-name').val(), email: $('#form-email').val(), telephone: $('#form-tel').val(), issue: $('#form-issue').val(), date: $('#form-date').val(), time_hours: $('#form-time-hours').val(), time_minutes: $('#form-time-minutes').val(), location: $('input[name=location]').val(), comment: $('#form-comment').val() },
			success: function($datas)
			{
				$data = eval('(' + $datas + ')');
				$msg = $data['msg'];
				$status = $data['status'];
				
				
				//$data = $data.replace(/"/g, '');
				
				$('#book-appointment .ajaxloader').delay(500).fadeOut(500, function ()
					{ 
						$('#book-appointment .ajaxloader').parent().append($msg); 
						$('#book-appointment .submit-form').fadeIn(500); 
					});
				
				if ($status == 1)
				{ 
					
						$('#book-appointment input').val('');
						$('#book-appointment textarea').val('');
				}
	
			}
		});
	});
	
	$('#contact .submit-form').click( function ()
	{
		$(this).css('display', 'none');
		$(this).next().css('display', 'block');
		$('#contact').children('.msg').remove();
		$('#contact .error').remove();

		$.ajax(
		{
			type: "POST",
			url: "/includes/form-processing.php?form=contact",
			data: {name: $('#form-name').val(), email: $('#form-email').val(), telephone: $('#form-tel').val(), location: $('input[name=location]').val(), comment: $('#form-comment').val() },
			success: function($data)
			{
				$data = eval('(' + $data + ')');
				$msg = $data['msg'];
				$status = $data['status'];
				
				
				//$data = $data.replace(/"/g, '');
				
				$('#contact .ajaxloader').delay(500).fadeOut(500, function ()
					{ 
						$('#contact .ajaxloader').parent().append($msg); 
						$('#contact .submit-form').fadeIn(500); 
					});
				
				if ($status == 1)
				{ 

						$('#contact input').val('');
						$('#contact textarea').val('');
				}
	
			}
		});
	});
	
	$('#signup .submit').click( function ()
	{
		$(this).css('display', 'none');
		$(this).next().css('display', 'block');
		$('#signup').children('.msg').remove();
		
		$.ajax(
		{
			type: "POST",
			url: "/includes/form-processing.php?form=newsletter",
			data: {email: $('#email').val() },
			success: function($data)
			{
				$data = stripslashes($data);
				
				if ($data)
				{ 
					$('#signup .ajaxloader').delay(500).fadeOut(500, function ()
					{ 
						$('#signup .ajaxloader').parent().append('<p class="msg">' + $data + '</p>'); 
						$('#signup').children('.msg').fadeIn(500);
						$('#signup').children('.msg').delay(2000).fadeOut(500);
						$('#signup .submit').delay(2800).fadeIn(500); 
					});
				}
				
			}
		});
	});
	
	if ( $('.accordion').length )
	{

		$('.accordion h3').click( function() {
			
			$('.accordion h3').css('background-position', 'top');
			$('.accordion').children('p,ul').slideUp(200);
			$(this).parent().children('p,ul').slideDown(200);
			$(this).css('background-position', 'bottom');
		});
	}
	
	$('#post-comment .submit').click( function()
	{
			
			$(this).css('display', 'none');
			$(this).next().css('display', 'block');
			$('#post-comment .msg').remove();
			
			$.ajax(
			{
				type: "POST",
				url: "/includes/form-processing.php?form=blog-comments",
				data: {blog_id: $('#blog_id').val(), name: $('#blog_name').val(), email: $('#blog_email').val(), comments: $('#blog_comments').val(), recaptcha_challenge_field: $('#recaptcha_challenge_field').val(), recaptcha_response_field: $('#recaptcha_response_field').val(), blog_title: $('#blog_title').val() },
				success: function($data)
				{
					
					if ($data)
					{
						$data = eval('(' + $data + ')');
						$msg = $data['msg'];
						
						$('#post-comment .ajaxloader').delay(500).fadeOut(500, function (){
						
							$('#post-comment .ajaxloader').parent().append('<p class="msg"></p>'); 
							$('#post-comment .msg').html($msg);
							$('#post-comment .msg').fadeIn(500);
							$('#post-comment .submit').fadeIn(500);
							Recaptcha.reload();
							
							if ($data['status'] == 1)
							{
									$('#blog_name').val('');
									$('#blog_email').val('');
									$('#blog_comments').val('');
							}
							
						});
					}
				}
			});		
	});
	
});


 /* Rounded corners for IE only */
DD_roundies.addRule('.rounded', '15px');
DD_roundies.addRule('.testimonial', '15px');
DD_roundies.addRule('.error', '15px');
DD_roundies.addRule('.rounded-less', '10px');
DD_roundies.addRule('#content ul', '10px');
DD_roundies.addRule('.success', '10px');
DD_roundies.addRule('#content ol li', '10px');
DD_roundies.addRule('.accordion h2, .accordion h3, .accordion h4, .accordion h5, .accordion h6', '8px');
DD_roundies.addRule('#comments li');
DD_roundies.addRule('#post-comment');



/** Standard 
functions below **/


// Recaptcha Theme
var RecaptchaOptions = { theme : 'clean' };


/*

* stickyfloat - jQuery plugin for verticaly floating anything in a constrained area
* 
* Example: jQuery('#menu').stickyfloat({duration: 400});
* parameters:
* 		duration 	- the duration of the animation
*		startOffset - the amount of scroll offset after it the animations kicks in
*		offsetY		- the offset from the top when the object is animated
*		lockBottom	- 'true' by default, set to false if you don't want your floating box to stop at parent's bottom
* $Version: 05.16.2009 r1
* Copyright (c) 2009 Yair Even-Or
* vsync.design@gmail.com
*/

$.fn.stickyfloat = function(options, lockBottom) {
var $obj 				= this;
var parentPaddingTop 	= parseInt($obj.parent().css('padding-top'));
var startOffset 		= $obj.parent().offset().top;
var opts 				= $.extend({ startOffset: startOffset, offsetY: parentPaddingTop, duration: 200, lockBottom:true }, options);

$obj.css({ position: 'relative' });

if(opts.lockBottom){
	var bottomPos = $obj.parent().height() - $obj.height() + parentPaddingTop; //get the maximum scrollTop value
	if( bottomPos < 0 )
		bottomPos = 0;
}

$(window).scroll(function () { 
	$obj.stop(); // stop all calculations on scroll event

	var pastStartOffset			= $(document).scrollTop() > opts.startOffset;	// check if the window was scrolled down more than the start offset declared.
	var objFartherThanTopPos	= $obj.offset().top > startOffset;	// check if the object is at it's top position (starting point)
	var objBiggerThanWindow 	= $obj.outerHeight() < $(window).height();	// if the window size is smaller than the Obj size, then do not animate.
	
	// if window scrolled down more than startOffset OR obj position is greater than
	// the top position possible (+ offsetY) AND window size must be bigger than Obj size
	if( (pastStartOffset || objFartherThanTopPos) && objBiggerThanWindow ){ 
		var newpos = ($(document).scrollTop() -startOffset + opts.offsetY );
		if ( newpos > bottomPos )
			newpos = bottomPos;
		if ( $(document).scrollTop() < opts.startOffset ) // if window scrolled < starting offset, then reset Obj position (opts.offsetY);
			newpos = parentPaddingTop;

		$obj.animate({ top: newpos }, opts.duration );
	}
});
};
	

function stripslashes (str) {
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Ates Goral (http://magnetiq.com)
    // +      fixed by: Mick@el
    // +   improved by: marrtins
    // +   bugfixed by: Onno Marsman
    // +   improved by: rezna
    // +   input by: Rick Waldron
    // +   reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +   input by: Brant Messenger (http://www.brantmessenger.com/)
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: stripslashes('Kevin\'s code');
    // *     returns 1: "Kevin's code"
    // *     example 2: stripslashes('Kevin\\\'s code');
    // *     returns 2: "Kevin\'s code"
    return (str + '').replace(/\\(.?)/g, function (s, n1) {
        switch (n1) {
        case '\\':
            return '\\';
        case '0':
            return '\u0000';
        case '':
            return '';
        default:
            return n1;
        }
    });
}

