/* Author: 

*/

$(document).ready(function() {
	$('ul.sf-menu').superfish({
		autoArrows: true
	});

	$(".carousel").jCarouselLite({
		btnNext: ".next",
		btnPrev: ".prev"
	});	

	//Photo gallery
	if ($('ul.gallery').length) {
		 $('ul.gallery li a').fancybox({overlayOpacity:'0.4'});
	}

	//Featured Section
	if ($('ul.feature').length) {
		if ($('ul.feature li').length > 1) {
			$('ul.feature').cycle({
				backwards:true,
				pager:$('div#featured_items ul.feature-ctrl'),
				pagerAnchorBuilder:function(i,e) {
					return '<li><a href="#"><span class="displace">'+(i+1)+'</span></a></li>';
				},
				containerResize: 0
			});
		} else {
			$('ul.feature li').css('display','block');
		}
	}

	//Remove empty property listing categories
	$('#location_list p.none').parent().hide();

	//Sidebar submenus
	$('#content_sidebar .submenu ul li:last-child').addClass("last");
 
	$('#content_sidebar .submenu > ul > li > a').click(function() {
		if ($(this).parent('li').has('ul').length) {
			if (!$(this).parent('li').hasClass('active')) {
				closeMenu();
 
				//Do this only if the item clicked is not the currently active item
				$(this).parent('li').addClass('active');
				$('ul', $(this).parent('li')).slideDown('fast');
			} else {
				closeMenu();
			}
			return false;
		}
	});

	//Email updates button
	$('#email_updates img').click(function() {
		$('#email_updates_form').slideToggle();
		$('#email_signup-fname').focus();
	});

	/* FORM: Email Signup */
	if ($('form#email_signup').length) {
		$('form#email_signup').validate({
			debug:true,
			rules: {
				fname:'required',
				lname:'required',
				email: {
					required:true,
					email:true
				}
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				$(f).before('<div class="alert"></div>');
				var fid = $(f).attr('id');
				formShade(f);
				$.ajax({
					type:'POST',
					url:$(f).attr('action'),
					dataType:'json',
					data:{
						jssub:'1',
						fname:$('input#'+fid+'-fname',f).val(),
						lname:$('input#'+fid+'-lname',f).val(),
						email:$('input#'+fid+'-email',f).val()
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							//m = '<h2>Thanks for registering!</h2><p>'+json.message+'</p>';
							m = '<h3 class="thanks">Thanks for registering!</h3>';
						} else {
							m = '<h1>There was a problem registering.</h1><p>'+json.message+'</p>';
						}
						$('div.alert').html(m).show(200);
					},
					error:function(x,t,e) {
						$('div.alert').html('<h1>There was a problem submitting the form.</h1>').show(200);
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			errorPlacement: function(err,el) {
				var fn = $(el).siblings('span.title').text();
				$(el).siblings('span.error').remove();
				if(!$(el).siblings('span.error').length) {
					// $(el).after('<span class="error">Please provide your <strong>'+fn+'</strong>.</span>');
				}
			}
		});
	}

	/* FORM: CONTACT */
	if ($('form#contact').length) {
		$('form#contact').validate({
			debug:true,
			rules: {
				fname: 'required',
				lname: 'required',
				email: {
					required:true,
					email:true
				},
				comments: 'required'
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				$(f).before('<div class="alert"></div>');
				var fid = $(f).attr('id');
				formShade(f);
				$.ajax({
					type:'POST',
					url:$(f).attr('action'),
					dataType:'json',
					data:{
						jssub:'1',
						fname:$('input#'+fid+'-fname',f).val(),
						lname:$('input#'+fid+'-lname',f).val(),
						address:$('input#'+fid+'-address',f).val(),
						city:$('input#'+fid+'-city',f).val(),
						state:$('select#'+fid+'-state',f).val(),
						zip:$('input#'+fid+'-zip',f).val(),
						phone:$('input#'+fid+'-phone',f).val(),
						email:$('input#'+fid+'-email',f).val(),
						category:$('select#'+fid+'-category',f).val(),
						//emsub:($('input#'+fid+'-em-sub:checked').length)?'y':'n',
						comments:$('textarea#'+fid+'-comments',f).val()
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<h2>Thanks for your comments.</h2><p>'+json.message+'</p>';
						} else {
							m = '<h2>There was a problem sending your message.</h2><p>'+json.message+'</p>';
						}
						$('div.alert').html(m).show(200);
					},
					error:function(x,t,e) {
						$('div.alert').html('<h2>There was a problem submitting the form.</h2>').show(200);
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			errorPlacement: function(err,el) {
				var fn = $(el).siblings('span.title').text();
				$(el).siblings('span.error').remove();
				if(!$(el).siblings('span.error').length) {
					// $(el).after('<span class="error">Please provide your <strong>'+fn+'</strong>.</span>');
				}
			}
		});
	}

	//Property Map
	var img_base_url = 'http://ushotels-resorts.clickrain.com/assets/images/map/';
	
	if ($('div.map-base').length) {
		$('map#region-map area')
			.hover(
				function() {
					var regID = $(this).attr('class');
					$('img#region-map-pane')
						.stop()
						.animate({opacity:0},10,function() {
							$(this).attr('src',img_base_url+'map_'+regID+'_region.png').animate({opacity:1},400);
						});
				},
				function() {
					$('img#region-map-pane')
						.stop()
						.animate({opacity:0},200,function() {
							$(this).attr('src',img_base_url+'map_base.png');
						});
				}
			)
			.click(
				function() {
					// was img#loc-map-zoom
					$('div.loc-map-zoom')
						.css('display','block')
						.animate({opacity:1},200);
					$('img#region-map-overlay').css('display','none');
					$('ul#all-locations-list')
						.animate({opacity:0},200,function() {
							$(this).css('display','none');
							});
					$('img#map-shade')
						.css('display','block')
						.animate({opacity:1},200);
					var regID = $(this).attr('class');
					$('ul#location-list > li:not(.'+regID+')').css('display','none');
					$('ul#location-list > li.'+regID).css('display','block');
					
					
					
					/* New Here */
					$('ul#location-list').css({'background':'transparent url('+img_base_url+'map_'+regID+'_region_lg.png) no-repeat left top'});
					$('div.location-list')
						.css({'display':'block'})
						.animate({
							opacity:1
							},400,function() {
								if($.browser.msie && $.browser.version==6) {
									$('ul#location-list,ul#location-list li').css('opacity','1');
								}
							})
						.click(function() {
							$(this).stop().animate({opacity:0},200,function() {
								// was img#loc-map-zoom
								$('div.loc-map-zoom')
									.animate({opacity:0},200,function() {
										$(this).css('display','block');
										});
								$('img#region-map-overlay').css('display','block');
								$('ul#all-locations-list')
									.css('display','block')
									.animate({opacity:1},200);
								$('img#map-shade')
									.animate({opacity:0},200,function() {
										$(this).css('display','none');
									});
								$(this)
									.css({
										'display':'none',
										'background':'none'
										})
									.attr('usemap','');
							});
						});
					
					return false;
				}
			);
		// was img#loc-map-zoom
		$('div.loc-map-zoom')
			.click(function() {
				// was ul#location-list
				$('div.location-list').stop().animate({opacity:0},200,function() {
					// was img#loc-map-zoom
					$('div.loc-map-zoom')
						.animate({opacity:0},200,function() {
							$(this).css('display','block');
							});
					$('img#region-map-overlay').css('display','block');
					$('ul#all-locations-list')
						.css('display','block')
						.animate({opacity:1},200,function() {
							if($.browser.msie && $.browser.version ==6) {
								$('li',this).css('background-color','transparent').css('opacity','1');
								}
						});
					$('img#map-shade')
						.animate({opacity:0},200,function() {
							$(this).css('display','none');
						});
					$(this)
						.css({
							'display':'none',
							'background':'none'
							})
						.attr('usemap','');
				});
			});
		$('ul#location-list li a')
			.click(function() {
				$('div#info-bubble').css('display','block');
				var par = $(this).parent('li');
				var t = parseInt($(par).css('top').replace('px',''),10);
				var l = parseInt($(par).css('left').replace('px',''),10);
				var vCName = (t>170)?'n':'s';
				var hCName = (l>280)?'w':'e';
				var bh = $('div#info-bubble div.body').html($(par).html()).height();
				//var vPos = (vCName=='n')?{top:(t-bh-21)+'px'}:{top:(t-7)+'px'};
				var vPos = (vCName=='n')?{top:(t-bh-14)+'px'}:{top:t+'px'};
				var hPos = (hCName=='w')?{left:(l-221)+'px'}:{left:(l-7)+'px'};
				$('div#info-bubble')
					.attr({'class':vCName+hCName})
					.css(vPos)
					.css(hPos)
					.stop()
					.animate({opacity:1},200);
				return false;
				});
		$('div#info-bubble div.head a.close')
			.click(function() {
				$('div#info-bubble').stop().animate({opacity:0},200,function() {
					$(this).css('display','none');
					});
				return false;
				});
	}
});

function closeMenu() {
	$('#content_sidebar li.active ul').slideUp('fast');
	$('#content_sidebar li').removeClass('active');
}

function formShade(el,notify) {
	$('div.alert',el).remove();
	$(el).fadeOut(400, function() {
		if (notify !== undefined && notify !== '') {
			$(el).before('<div class="notify">'+notify+'</div>');
		}
	});
}

function formReveal(el,str,kf) {
	$('div.notify').remove();
	$(el)
		.fadeOut(200,function() {
			if (kf===true) {
				$(this).prepend(str);
			} else {	
				$(this).html(str);
			}
		})
		.fadeIn(400);
}

