$(document).ready(function(){
	// image preload function
	function preloadImages()
	{
		var imageObject = arguments[0]; // take of the first argurment which is the object
		//alert(arguments[0]);
		$.each(imageObject.imageList, function(key, value) {  		
			jQuery("<img>").attr("src", imageObject.imagePath + value);
		});
	}
	
	// add markup for rounded corners
	if($('.no-borderradius')){
		// for all browsers except ie6
		if(($.browser.msie && parseFloat($.browser.version) > 6) || $.browser.msie == false){
			// ignore old browsers
			$('.rounded').each(function(){
				$(this).prepend('<div class="top"><span class="tl"></span><span class="tr"></span></div><!--[if IE ]><![endif]-->').append('<div class="bottom"><span class="bl"></span><span class="br"></span></div><!--[if IE ]><![endif]-->');
			});
		}
	}
	
	//apply form validation
	function jqueryValidate(){
		
		if(jQuery.fn.validate){
			
			//setup default actions for validate plugin
			$.validator.setDefaults({
				submitHandler: function(form) {
					//on submit make sure that any inputs that have the same value as their label then blank the value
					
					//pass form id to local var
					var formId = jQuery(form).attr('id');
					

					//iterate through submit elements
					$('#'+formId+' input[type=text],select,textarea,radio,checkbox').each(function(){
						
						//get current_input 
						var current_input = $(this);
						//get label for current_input 
						var current_label = $('label[for=' + current_input.attr('id') + ']');
						//check that the element is either a text or textarea
						if(current_input.attr('type') == 'text' || current_input.attr('cols')){
							//if label and input the same then blank the value
							if(current_label.text() == current_input.val()){	
								current_input.val('');
							}
						}
					});

					//submit form
					form.submit();
				},
				highlight: function(element, errorClass) {
						$(element).animate({
							opacity: 1.0
						}, 400, function () {
							$(element).css('color','#000000'); //red background for invalid data
							$(element).css('background-color','#FBE3E4'); //red background for invalid data
							$(element).css('border-color','#FBC2C4'); //red border for invalid data
						});
				},
				unhighlight: function(element, errorClass) {
					$(element).animate({
						opacity: 1.0
					}, 400, function () {
						$(element).css('color','#000000'); //red background for invalid data
						$(element).css('background-color','#fff'); //green background for valid data
						$(element).css('border-color','#C6D880'); //green border for valid data
					});
				}
			});
			
			if($('#callback').length > 0) { // check if element is on page
				
				// change the hidden fields in the form to send to the ajax form template ajax-form
				$('#callback input[name="URI"]').val('ajax-success');
				$('#callback input[name="return"]').val('ajax-success');
				$('#callback input[name="RET"]').val('http://dev.saracen-datastore.co.uk/ajax-success');
				
				// pre-submit callback 
				function showRequest(formData, jqForm, options) { 
					//console.log('contact form');
					// formData is an array; here we use $.param to convert it to a string to display it 
					// but the form plugin does this for you automatically when it submits the data 
					var queryString = $.param(formData); 
				 	// console.log(queryString);
					// jqForm is a jQuery object encapsulating the form element.  To access the 
					// DOM element for the form do this: 
					// var formElement = jqForm[0]; 
					//pass options response element into var fo use in showReponse method
			
					target = null;
					target = options.target;
			
					//remove any old error/success messages
					if($("#submit-error")){
						$("#submit-error").remove();
					}
					if($("#submit-success")){
						$("#submit-success").remove();
					}
				 
					var message = 'Please wait whilst the page loads';
					
					//$('#response').hide();
					$(target).html("<div id='loading'><img src='/images/ajax-loader.gif' alt='page loading' /><p>"+message+"</p></div>"); //fill contents of #reponse div
					$(target).show(); //fade in div
			
					
			
					// here we could return false to prevent the form from being submitted; 
					// returning anything other than false will allow the form submit to continue 
					return true; 
				}
				
				// post-submit callback 
				function showResponse(responseHTML, statusText)  { 
					
					
					$(target).hide(); //continue fade out of div
					
					var message = $('ul', responseHTML).html(); //assign XHTML ul node to variable
					
					//console.log(responseHTML);
			
					if(responseHTML=="success"){
					
						message = "Thank you your form has been submitted. We will get back to you as soon as we can."
			
						var html = "<div id='submit-success' class='success'><p>"+message+"</p></div><div id='submit-error'></div>"; //pass message from XML into variable
						
						$(target).after(html); //replace <div id='response'> div with content of html var
						$("#callback").hide(); 
					}else{
						showError(message);
						return false;
					}			  
								  
				}

			
				// post-submit callback 
				function showError(responseHTML)  { 
			
					$(target).hide();
					var err_msg = responseHTML; 
					
					if(err_msg){
						var message = err_msg;
					}else{
						var message = 'Error : The operation you requested has failed. Please try again';
					}
					
					var html = "<div class='error' id='submit-error'><h4>Please correct the following errors:</h4><ul>"+message+"</li></div>"; //pass message var into variable
					
					$(target).after(html); //replace <div id='response'> div with content of html var
					
					$("#submit-error").fadeIn('slow');	//fade in error div
				}
				
				var options = { 
					target:         '#response',   // target element(s) to be updated with server response 
					beforeSubmit:   showRequest,  // pre-submit callback 
					success:        showResponse,  // post-submit callback 
			 
					// other available options: 
					/*url:       	    '/ajax-success',        // override for form's 'action' attribute this means that only a javascript call can request this URL*/
					type:           'post',  // 'get' or 'post', override for form's 'method' attribute 
					dataType:       'html',   // 'xml', 'script', or 'json' (expected server response type) 
					clearForm:	    true,   // clear all form fields after successful submit 
					resetForm:		true,       // reset the form after successful submit 
			 
					// $.ajax options can be used here too, for example: 
					timeout:   		10000,	//set timeout to 10 seconds
					error:			showError //call error function to display error
					
				};
							

				if($('#callback').length > 0 ){ // check if element is on page	
					$("#callback").validate({
						rules: {
							your_name: {  
								required: true,
								notLabelText: true                     
							}, 
							your_phone: {  
								required: true,  
								phoneUK: true,
								notLabelText: true         
							}
						},
						messages: {
							your_name: "Please enter your name.", 
							your_phone: {
											required: "Please enter your telephone number.",
											phoneUK: "Invalid telephone number."
									   }
						},
						submitHandler: function(form) { //use validate event handle to submit form
							
							//pass form id to local var
							var formId = jQuery(form).attr('id');
							
		
							//iterate through submit elements
							$('#'+formId+' input[type=text],select,textarea,radio,checkbox').each(function(){
								
								//get current_input 
								var current_input = $(this);
								//get label for current_input 
								var current_label = $('label[for=' + current_input.attr('id') + ']');
								//check that the element is either a text or textarea
								if(current_input.attr('type') == 'text' || current_input.attr('cols')){
									//if label and input the same then blank the value
									//console.log(current_label.text());
									if(current_label.text() == current_input.val()){	
										current_input.val('');
									}
								}
							});
							
							jQuery(form).ajaxSubmit(options);
						}
	
					});
				}
				if($('#contact-form').length > 0 ){ // check if element is on page	
					$("#contact-form").validate({
						rules: {
							ctc_name: {  
								required: true,
								notLabelText: true                     
							}, 
							ctc_email: {  
								required: true,  
								email: true,
								notLabelText: true         
							}
						},
						messages: {
							ctc_name: "Please enter your name.", 
							ctc_email: {
											required: "Please enter an email address",
											email: "Invalid email address"
									   }
						}	
					});
				}
				if($('#callback-two').length > 0 ){ // check if element is on page	
					$("#callback-two").validate({
						rules: {
							cb_name: {  
								required: true,
								notLabelText: true                     
							}, 
							cb_telephone: {  
								required: true,  
								notLabelText: true         
							}, 
							cb_company: {  
								required: true,  
								notLabelText: true         
							}, 
							cb_when_call: {  
								required: true,  
								notLabelText: true         
							}
						},
						messages: {
							cb_name: "Please enter your name", 
							cb_telephone: "Please enter your telephone number", 
							cb_company: "Please enter your company", 
							cb_when_call: "Please let us know when you'd like us to call"									   
						}	
					});
				}
				if($('#request-a-quote-form').length > 0 ){ // check if element is on page	
					$("#request-a-quote-form").validate({
						rules: {
							raq_name: {  
								required: true,
								notLabelText: true                     
							}, 
							raq_email: {  
								required: true,  
								email: true,
								notLabelText: true         
							}
						},
						messages: {
							raq_name: "Please enter your name", 
							raq_email: {
											required: "Please enter an email address",
											email: "Invalid email address"
									   }						   
						}	
					});
				}

			
				if($('#frmSS63').length > 0 ){ // check if element is on page	
					$("#frmSS63").validate({
						rules: {
							email: {  
								required: true,  
								email: true,
								notLabelText: true         
							}
						},
						messages: {
							email: {
										required: "Please enter an email address",
										email: "Invalid email address"
									}
						}	
					});
				}

			}

		}
	}
	//fire off validation setup
	jqueryValidate();
	
	/*if($('#top-heading nav.nav').length > 0){ // check if element is on page
		
		var parent_ul = $('#top-heading nav.nav > ul');
		
		$('#top-heading nav.nav ul > li').each(function(){
		
			if($(this).children('ul').length > 0){
				
				// assign vars
				var parent_li = $(this);
				var child_ul = $(this).children('ul');
				var child_width = 0;
				
				// set child width by cuumulative value of each child li's width
				child_ul.children('li').each(function(){
					child_width += $(this).width();	
				});
				
				// calculate remaining horizontal space from parent li left position
				horizontal_space = parent_ul.width() - parent_li.position().left;
				
				// if child ul is wider
				if(horizontal_space < child_width){
					
					// calculate left offset
					width_offset = child_width - horizontal_space;
					// reset the child ul left position
					child_ul.css({'left':'-'+width_offset+'px'});
				}				
			}
			// don't run for ie 6		
			if(!($.browser.msie && $.browser.version == 6)){
				// bind mouseover behaviour
				if($(this).hasClass('selected') == false){
					
					var hide;
				
					function hideSelected(){
					
						$('#top-heading nav.nav ul li.selected').addClass('unselected');
						$('#top-heading nav.nav ul li.selected').removeClass('selected');
					}
					
					$(this).hover(function(){
						hide = setTimeout(hideSelected, .5*100);
					},function(){
						clearTimeout(hide);
					});
				}
			}
			
		});
		
		// don't run for ie 6
		if(!($.browser.msie && $.browser.version == 6)){
			var show;
			
			function showSelected(){
				$('#top-heading nav.nav ul li.unselected').addClass('selected');
				$('#top-heading nav.nav ul li.unselected').removeClass('unselected');
			}
			
			$('#top-heading nav.nav ul:first').mouseleave(function(){
				show = setTimeout(showSelected, .5*500);
			});
		}
	}*/
	
	if($('#request-callback a').length > 0){
	
		var $link = $('#request-callback a');
		var $target1 = $('#sliding-panel');
		var $target2 = $('#site-wrapper');
		
		function slideDown(){
			
			$link.addClass('animating');
			
			$target1.stop().animate(
					{ top: 0 }, // what we are animating
					{
						duration: 100, // how fast we are animating
						easing: 'swing', // the type of easing
						complete: function() { // the callback
							// Animation complete.
							$link.removeClass('animating').addClass('active');
					   	}
				});
				
			$target2.stop().animate(
					{ marginTop: 153 }, // what we are animating
					{
						duration: 100, // how fast we are animating
						easing: 'swing' // the type of easing
					});
		}
		
		function slideUp(){
			
			$('#request-callback a').addClass('animating');
			
			$target1.stop().animate(
					{ top: -153 }, // what we are animating
					{
						duration: 300, // how fast we are animating
						easing: 'swing', // the type of easing
						complete: function() { // the callback
							// Animation complete.						
							$link.removeClass('active');
							$link.removeClass('animating')
					   	}
			});
			$target2.stop().animate(
					{ marginTop: 0 }, // what we are animating
					{
						duration: 300, // how fast we are animating
						easing: 'swing' // the type of easing
					});
		}
		
		// bind click behaviour to contact link
		$link.bind('click', function(){
		
			if($(this).hasClass('active')){
				// slide up if active
				slideUp();			
			}else{
				// slide down
				slideDown();
			}
			
			return false;
		});
		
	}

	if($('#banner .col-1 ul').length > 0){
		$('#banner .col-1 ul').before('<div id="cycle-nav">').cycle({ 
			fx: 'scrollUp', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			speed: 200,
			timeout: 5000,
			pager: '#cycle-nav'
		});
	}
	if($('#inner .document-storage #main #main-center-panel .row-2 .col-2 ul').length > 0){
		$('#inner .document-storage #main #main-center-panel .row-2 .col-2 ul').before('<div id="quote-nav">').cycle({ 
			fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			speed: 200,
			pager: '#quote-nav'
		});
	}
	
	if($('#inner .offsite-tape-storage #main #main-center-panel .row-2 .col-2 ul').length > 0){
		$('#inner .offsite-tape-storage #main #main-center-panel .row-2 .col-2 ul').before('<div id="quote-nav">').cycle({ 
			fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			speed: 200,
			pager: '#quote-nav'
		});
	}
		
	$('#tabs #tab-nav li a.showhide').showhide({
		animate:false,
		event:'click',
		autoScroll: false,
		wrapper:'#tabs'
	});
	
	$('#tabs #tab-nav li').bind('mousedown',function(){
		$item=$(this).index();
		$item=$item+1;
		$('#tabs #tab-nav').removeClass();
		$('#tabs #tab-nav').addClass('active'+$item);
	});

	$('#tab-block1 ul li a').showhide({
		animate:false,
		event:'click',
		autoScroll: false,	
		autoScroll: true,
		autoScrollTimeout:5000,
		wrapper:'#tab-block1'
	});
	
	$('#tab-block2 ul li a').showhide({
		animate:false,
		event:'click',
		autoScroll: false,
		autoScroll: true,
		autoScrollTimeout:5000,
		wrapper:'#tab-block2'
	});
	
	$('#tab-block3 ul li a').showhide({
		animate:false,
		event:'click',
		autoScroll: false,
		autoScroll: true,
		autoScrollTimeout:5000,
		wrapper:'#tab-block3'
	});
	                       
	if(jQuery.fn.inputfocus){
		if($('#newsletter .input').length > 0){
			$('#newsletter .input').inputfocus({
				bgColourFocus:'#ffffff',
				bgColour:'#f7f7f7',			
				animate:true
			});
		}
		if($('#sliding-panel .input').length > 0){
			$('#sliding-panel .input').inputfocus({
				bgColourFocus:'#fff',
				bgColour:'#f7f7f7',					
				wrapType: 'inner',
				animate:true
			});
		}
		if($('.form-container .input').length > 0){
			$('.form-container .input').inputfocus({
				bgColourFocus:'#fff',
				bgColour:'#fff',		
				animate:true
			});
		}
		if($('#raq-sidebar-form .input').length > 0){
			$('#raq-sidebar-form .input').inputfocus({
				bgColourFocus:'#fff',
				bgColour:'#fff',		
				animate:false
			});
		}
		if($('#baa-sidebar-form .input').length > 0){
			$('#baa-sidebar-form .input').inputfocus({
				bgColourFocus:'#fff',
				bgColour:'#fff',		
				animate:false
			});
		}
	}   
	
			
});
			
$(document).ready(function() {
	$('div#contact-block> div').hide();
	$('div#contact-block> h3').click(function() {
		$(this).addClass('active')
 		.siblings('h3').removeClass('active');
		$(this).next('div').slideToggle('fast')
 		.siblings('div:visible').slideUp('fast');
 	});
});			

if ( !($.browser.msie && $.browser.version == 7.0) ){
	
	$(document).ready(function() {
		$('div#qa-accordion div.qa-content').hide();
		$('div#qa-accordion h2').click(function() {
			if($(this).hasClass('active')){
				$(this).removeClass('active')
		 		.siblings('h2').removeClass('active');
	 		}else{
				$(this).addClass('active')
	 	 		.siblings('h2').removeClass('active');
	 	 		}
			$(this).next('div.qa-content').slideToggle('fast')
	 		.siblings('div:visible').slideUp('fast');
	 	});
	 	// content active via hash tag    
	    if(window.location.hash && window.location.hash.match('ht')) {
	    	//console.log(window.location.hash);
	        $('div#qa-accordion div.qa-content').hide();
	        $(window.location.hash).addClass('active').siblings('h2').removeClass('active');
	        $(window.location.hash).next('div:first').show();
	    }
	});		

}

$(document).ready(function() {
	$('div#page-accordion div.section-content').hide();
	$('div#page-accordion h2').click(function() {
		if($(this).hasClass('active')){
			$(this).removeClass('active')
	 		.siblings('h2').removeClass('active');
 		}else{
			$(this).addClass('active')
 	 		.siblings('h2').removeClass('active');
 	 		}
		$(this).next('div.section-content').slideToggle('fast')
 		.siblings('div:visible').slideUp('fast');
 	});
 	// content active via hash tag    
    if(window.location.hash && window.location.hash.match('ht')) {
    	//console.log(window.location.hash);
        $('div#qa-accordion div.qa-content').hide();
        $(window.location.hash).addClass('active').siblings('h2').removeClass('active');
        $(window.location.hash).next('div:first').show();
    }
});			


$(function() {
	$('.date-pick').datePicker({clickInput:true,createButton:false})
});
/*raq form*/
$(document).ready(function() {
	$('.quote-form-details').hide();
	
	$('#raq_document').click(function() {
   		$(".document-storage-details").slideToggle(this.checked);
	});
	
	$('#raq_destruction').click(function() {
   		$(".destruction-services-details").slideToggle(this.checked);
	});
	
	$('#raq_deed').click(function() {
   		$(".deed-storage-details").slideToggle(this.checked);
	});
	
	$('#raq_business').click(function() {
   		$(".business-storage-details").slideToggle(this.checked);
	});
	
	$('#raq_tape').click(function() {
   		$(".tape-storage-details").slideToggle(this.checked);
	});
});			

$(document).ready(function() {

    //Default Action
    $(".map-content").css({'visibility':'hidden' , 'position':'absolute'});
    $("ul#maps-nav li:first").addClass("active").show(); 
    $(".map-content:first").css({'visibility':'visible' , 'position':'absolute'}); 

    //On Click Event
    $("ul#maps-nav li").click(function() {
        $("ul#maps-nav li").removeClass("active");
        $(this).addClass("active"); 
        $(".map-content").css({'visibility':'hidden' , 'position':'absolute'}); 
        var activeTab = $(this).find("a").attr("href"); 
        $(activeTab).css({'visibility':'visible'  , 'position':'absolute'});
        return false;
    });

});



