$(function(){
	$('.fancy').fancybox({
		'transitionIn'			:	'elastic',
		'transitionOut'			:	'elastic',
		'speedIn'				:	600, 
		'speedOut'				:	200, 
		'overlayShow'			:	false,
		'hideOnOverlayClick'	:	false
	});// init Fancybox
	var dialog = $("#dialog").dialog({resizable:false, autoOpen: false, height: "auto", zIndex: 3999});
	
	$('#request-form').validate({
		errorPlacement: function(error, element){ 
			error.hide(); 
			element.css('border','1px solid #C00').css('padding','0'); 
		},
		submitHandler: function(form){
			$(form).append('<input type="hidden" name="action" value="submit" />');
			$.post('./contact.php?action=submit', $(form).serialize(), function(msg){
				$(form).replaceWith(msg);
			});
			return false;
		}
	});
	
	$("#scrollthumbs").find('img:first').addClass("active");
	$("#scrollthumbs").find('img').each(function(key){
						$(this).attr('id', 'thumb_'+key);
					  });
	$("#scrollthumbs").scrollable({ items:'.thumbs', circular: true });
	$("#currimg").scrollable({ 
		items:'.villas', 
		circular: true,
		onBeforeSeek: function(event, i) {
			/*
			if((i%4==0)&&(i!=0)&&(!$('#scrollthumbs').find('#thumb_'+i).hasClass('active'))){
				$("#scrollthumbs").data("scrollable").next();
			}
			$('#scrollthumbs').find('.active').removeClass('active');
			$('#scrollthumbs').find('#thumb_'+i).addClass("active");
			*/
		}
	}).autoscroll({ 
		autoplay: true,
		autopause: true,
		interval: 5000 
	});	
	$("#scrollthumbs img").live('click', function(){
		var index = $(this).attr('id').replace("thumb_","");
		var sIndex = $("#currimg").data("scrollable").getIndex();
		$("#currimg").data("scrollable").stop();
		$("#currimg").data("scrollable").move(index-sIndex);
    });
	$('#next').live('click', function(){
		$("#scrollthumbs").data("scrollable").next(500);
	});
	$('#prev').live('click', function(){
		$("#scrollthumbs").data("scrollable").prev(500);
	});
	
	if($("#currimg").size()>0){
		$("#currimg").data("scrollable").pause();
	}
	if($("#villa-thumbs").find('img').size()>9){
		$("#villa-thumbs").scrollable({ circular: true });// Villa Thumb Scroller
	}else{
		$('#villa-thumbs-next').hide();
	}
	$('#masthead-toggler').live('click', function(){
		$('html').css('cursor','wait');
		if($('#masthead').is(':visible')){
			$(this).parent('div').slideUp('fast', function(){
				$('#masthead').slideUp('slow', function(){ 
					$('#masthead-toggler').html('SHOW THIS BAR').parent('div').slideDown('fast'); 
					$('html').css('cursor','default'); 
				});
			});
		}else{
			$(this).parent('div').slideUp('fast', function(){
				$('#masthead').slideDown('slow', function(){ 
					$('#masthead-toggler').html('HIDE THIS BAR').parent('div').slideDown('fast'); 
					$('html').css('cursor','default'); 
				});
			});
		}
		return false;
	});// Masthead Toggle
	jQuery.validator.messages.required = "";
	$('#email-subscribe').validate({
		errorPlacement: function(error, element) {
			error.hide();
		},
		invalidHandler: function(e, validator) {
			dialog.dialog('option','title','Subscription Error')
				  .dialog('option','buttons', {
						  "Ok" :function(){
							  dialog.dialog('close');
						  }
						})
				  .html('A Valid Email Address is Required.')
				  .dialog('open');
		},
		submitHandler: function(form) {
			var values = $('#email-subscribe').serialize();
			$.post('index.php',values, function(){
				$('#email-subscribe').find('input[name=email]').closest('tr').html('<td style="width:294px; font-weight:bold; font-size:13pt; color:#0FA3BF;" colspan="3">Subscription Submitted</td>');
			});
			return false;
		}
	});// Email Subscribe
	if($("#dd-area").size()>0){
		$("#dd-area").dropdownchecklist({ maxDropHeight: 100, emptyText: 'Desired Sibarth Area', width:213, firstItemChecksAll: true }); 
		$("#dd-amenities").dropdownchecklist({ maxDropHeight: 100, emptyText: 'Desired Amenities', width:213, firstItemChecksAll: true }); 
		$("#dd-currency").dropdownchecklist({ maxDropHeight: 100, width:62 }); 
	}
	$('#extra-description-toggler').live('click', function(){
		$('html').css('cursor','wait');
		if($('#extra-description').is(':visible')){
			$(this).parent('div').slideUp('fast', function(){
				$('#extra-description').slideUp('slow', function(){ 
					$('#extra-description-toggler').html('More Information').parent('div').slideDown('fast'); 
					$('html').css('cursor','default'); 
				});
			});
		}else{
			$(this).parent('div').slideUp('fast', function(){
				$('#extra-description').slideDown('slow', function(){ 
					$('#extra-description-toggler').html('Hide Information').parent('div').slideDown('fast'); 
					$('html').css('cursor','default'); 
				});
			});
		}
		return false;
	});// description toggler
	$('.top, .top-link').live('click', function(){
		$('html, body').animate({ scrollTop: $("#header").offset().top }, 'slow');
	});	
	$('.amenities-scroll').live('click', function(){
		$('html, body').animate({ scrollTop: $("#amenities-anchor").offset().top }, 'slow');
	});	
	$('.rates-scroll').live('click', function(){
		$('html, body').animate({ scrollTop: $("#rates-anchor").offset().top }, 'slow');
	});	
	$('.request-scroll').live('click', function(){
		$('html, body').animate({ scrollTop: $("#request-anchor").offset().top }, 'slow');
	});	
	$('#os-management').live('click', function(){
		$('html, body').animate({ scrollTop: $("#owner-services-management").offset().top }, 'slow');
	});	
	$('#os-maintenance').live('click', function(){
		$('html, body').animate({ scrollTop: $("#owner-services-maintenance").offset().top }, 'slow');
	});
	$('#os-customized').live('click', function(){
		$('html, body').animate({ scrollTop: $("#owner-services-customized").offset().top }, 'slow');
	});
	$('.list-villa').live('click', function(){
		window.location = $(this).find('a:first').attr('href');
	});
	$("#map_canvas").click(function() {
		$.fancybox({
			//'orig'			: $(this),
			'padding'		: 0,
			'href'			: 'http://www.sibarth.com/map.php',
			'title'   		: $(this).attr('title'),
			'transitionIn'			:	'elastic',
			'transitionOut'			:	'elastic',
			'speedIn'				:	600, 
			'speedOut'				:	200, 
			'overlayShow'			:	false,
			'height':600,
			'width':900,
			'type':'iframe'
		});
		return false;
	});
});
