var FirstAsset = {
	Modal: function () {
		// $("a[rel]").overlay({
		// 			mask: '#000',
		// 			effect: 'default',
		// 			onBeforeLoad: function () {
		// 				var wrap = this.getOverlay().find(".contentWrap");
		// 				wrap.load(this.getTrigger().attr("href"));
		// 				var str = this.getTrigger().text();
		// 				$('#modal-title').text(str);
		// 			}
		// 		});
		$("a.modal").overlay({
			mask: '#000',
			effect: 'default',
			onBeforeLoad : function() {
				var wrap = this.getOverlay().find(".contentWrap");
				wrap.load(this.getTrigger().attr("href"));	
			}
		});
	},
	SubmitForm: function () {
		$('#modal-form, #valuation-form, #contact-form').live('submit', function (event) {
			event.preventDefault();
			//get the relevant form details
			var form = $(this),
				action = $('input[type=submit]', form).attr('name'),
				values = form.serialize() + '&' + 'action[hr_contact_case]=submit',
				posturl = 'http://www.1stasset.co.uk/ajaxpost/',
				mode = $('input[type=submit]', form).attr('id');
			//send it
			$.ajax({
				type: "POST",
				url: posturl,
				data: values,
				statusCode: {
					500: function () {
						form.submit();
					}
				},
				success: function (response) {
					form.prepend('<p class="success">Thank you, your message has been received.</p>');
				}
			});
			//return false;
		});
	},
	Tabs: function () {
		$(".tabs").tabs("div.panes > .pane", {
			effect: 'fade',
			fadeOutSpeed: 'slow',
			rotate: true,
			initialIndex: 0,
		});/*.slideshow({
			autoplay: true,
			clickable: false,
			interval: 6000
		});*/
	},
	Carousel: function () {
		$("#scrollable").scrollable({
			vertical: true,
			mousewheel: false,
			circular: false
		});
	},
	DropDownMenu: function () {
		var servicenav = $('#nav-services');
		var dropdown = servicenav.find('ul');
		servicenav.bind('mouseenter', function (e) {
			$('a.level-1', this).addClass('hover');
			dropdown.css('display', 'block');
		}).bind('mouseleave', function (e) {
			$('a.level-1', this).removeClass('hover');
			dropdown.css('display', 'none');
		});
	},
	PageScroll: function () {
		$('a.scroll-anchor').each(function (index) {
			var link = $(this);
			var element = link.attr("href");
			var position = $(element).offset().top;
			// if (window.location.hash) {
			// 		var position = $(window.location.hash).offset().top;		
			// 		scrollPage(position);
			// 	};	
			link.bind('click', function (event) {
				event.preventDefault();
				$('body,html').animate({
					scrollTop: position
				}, 800);
			});
		});
	},
	Gallery: function () {
		var slideInterval, slideContainer = $('#gallery').css('background-color', '#ffffff'),
			slide = $('.pane', slideContainer),
			slideCount = slide.length,
			oldSlide = 0,
			currentSlide = 0;
		$('.pane:first', slideContainer).show();
		//do the rotation

		function slideRotate() {
			currentSlide = (oldSlide + 1) % slideCount;
			$(".pane:eq(" + oldSlide + ")", slideContainer).stop().animate({
				opacity: "hide"
			}, {
				queue: false,
				duration: 1000
			}, 'linear');
			$(".pane:eq(" + currentSlide + ")", slideContainer).stop().animate({
				opacity: "show"
			}, {
				queue: false,
				duration: 1000
			}, 'swing');
			oldSlide = currentSlide;
		};
		slideInterval = setInterval(slideRotate, 4000);
	},
	expandLink: function () {
		var root = $('#why');
		root.find('figure').each(function (index, elem) {
			var el = $(elem),
				link = el.find('a')[0].href;
			el.bind('click', function (event) {
				window.location = link;
			});
		});
	}
}

$(function () {
	//modal on everypage
	//FirstAsset.Modal();
	//FirstAsset.SubmitForm();
	FirstAsset.DropDownMenu();
	$('.modal').colorbox({
		iframe:true, 
		innerWidth:580, 
		innerHeight:500,
		slideshow:false
	});
	H5F.setup(document.getElementById("valuation-form"));
	//
	if ($('#page-home').length) {
		FirstAsset.Tabs();
		FirstAsset.expandLink();
	};
	if ($('#scrollable').length) {
		FirstAsset.Carousel();
	};
	//$("#valuation-form").validationEngine('attach', {promptPosition : "topLeft"});
	//
	if ($('#page-why-1st-asset').length) {
		FirstAsset.PageScroll();
	};
	if ($('#gallery').length) {
		FirstAsset.Gallery();
	};
});

