$(function() {

	var $ = jQuery;
	
	var config = {
	
	};

	var CVB	=	{
	
		events : function(){
			$("div.ongoing").each(function(i){
				var $ogh = $(this).nextAll('div.ogholder').eq(0);
				$(this).appendTo($ogh);
			});
			
			$(document.body).bind("click", function(e){
				var $t = $(e.target);
				var $da = $($t).closest("div.event-line");
				
				if($t.is(".descript h4 a")){
					$($t).toggle(function(e){
						$($da).find('div.block').show('slow', function(){$($da).toggleClass('opened');});
						$($da).find('div.descript p+p').toggleClass('hide');
					}, function(){
						$($da).find('div.block').hide('slow', function(){
							$($da).toggleClass('opened').find('div.descript p+p').toggleClass('hide');
						});
					});
					$($t).trigger('click');
					return false;
				}
			});
		},
	
		tabs : function(){
			$('.tabsholder, .tabsetholder').tabs();
		},	
		
		validate : function(){
			// FREEFORM FORMS
				$(".yform").validate({
					invalidHandler: function(e, validator) {
						return false;
					}
				});

			// ENTRY FORMS

				$("#entryform").validate({
					invalidHandler: function(e, validator) {
						return false;
					}
				});
		},
		
		iform : function(div, data){
			var winW = $(window).width();
			$("body").append(data);

			var w = $(div).width();
			$(div).css('left', winW/2-w/2).fadeIn("slow");		
		},
		
		cform : function(link){
		
			var url = $(link).attr("href");
			
			$.ajax({	
				type: "POST",
				url: url,
				data: "",
				success: function(data){

					CVB.iform("#form-content", data);
					
					$("#overlay").fadeIn("slow", function(){
						// make email required if user opts in to receive email updates
						$(".yform #opt_in").change(function(){
							if($(this).is(':checked') && !$('#email').is('.required')){
								$("#email").addClass("required");
								$enreq = true;
								$("#email").parent().find("label").text("Email*:");
							} else if($enreq) {
								$("#email").removeClass("required");
								$("#email").parent().find("label").text("Email:");
							}
						})
						$(".yform").validate({
							invalidHandler: function(e, validator) {
								return false;
							},
							submitHandler: function() {
								$.ajax({
									type: "POST",
									url: $(".yform").attr("href"),
									data: $(".yform").serialize(),
									success: function(data)
									{
										$("#form-mid p").hide();
										$("#form-mid form").hide();
										
										var data = $("#Message", data);
										
										$("#form-mid").append(data);
										$('html, body').animate({scrollTop:0}, 'slow');
										
										$("#backLink a", data).live("click", function(){
											$("#Message").remove();
											$("#form-mid p").show();
											$("#form-mid form").show();	
											return false;
										});
										
										if ($(data).html() === null)
										{
											$("#form-mid").html('<h2>Thank You</h2><p>We have received your submission.</p>');
										}
									}
								});
								return false;				
							}
						});
					});
				}
			});
		},
		
		forms : function(){
		
			// GENERAL FORMS
			$("a.lbform").unbind("click").live("click", function(){
				CVB.cform(this);
				return false;	
			});		
		},

		overlays : function(callback){
			try{
				$("#overlay").fadeOut("slow", function(){
					$(this).remove();
				});
				$("#login-content").fadeOut("slow", function(){
					$(this).remove();
				});
				$("#form-content").fadeOut("slow", function(){
					$(this).remove();
				});
				$("#address-map").fadeOut("slow", function(){
					$(this).remove();
				});
				
				if(typeof callback == 'function'){
					callback.call();
				}
			}catch(e){}		
		},
	
		links : function(){
			
			$("a[rel='external']").live("click", function(){
				window.open($(this).attr("href")); return false;
			});
		},
		
		loadmap : function(loc, position){

			var h = '<div id="overlay"></div><div id="address-map"><div id="address-close-link"><a href="#" class="close">Close Map</a></div><div id="address-map-content"></div><div id="address-map-link"></div></div>';

			if (loc) {
					
				$(h).hide();
				
				$("body").append(h);
										
				var l = ($(window).width())/2 - ($("#address-map").width())/2;
					
				var t = position.top - 200;
					
				/* Position Map and Scroll Window */
				$("#address-map").css('left', l).css('top', t).fadeIn("slow");
					
				$().scrollTo({top:t - 50, left: l}, 800 );
					
				/* Show Map */
				$("#overlay").fadeIn("slow");
					
				map = new GMap2(document.getElementById('address-map-content'));
					
				geocoder = new GClientGeocoder();
					
				geocoder.getLatLng(loc, function(point){
					map.setCenter(point, 14);
					map.addOverlay(new GMarker(point));
				});
				
				$("#address-map-link").html('<p><a href="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q='+escape(loc)+'" rel="external">'+loc+'</a></p>')
				
				map.setUIToDefault();
				map.enableRotation();
			}		
		},
		
		maps : function(){
						
			$(".load-map").live("click", function(e){
			
				/* Get Positioning */
				var position = $(this).position();
				
				var loc = $(this).attr("rel");
				
				/* Get Address */
				if ($(this).hasClass("event"))
				{
					$.ajax({
						type: "GET",
						url: '/events-calendar/map/'+loc+'/',
						data: "",
						success: function(data){
							CVB.loadmap(data, position);
						}
					});
				}

				else if ($(this).hasClass("attraction"))
				{
					$.ajax({
						type: "GET",
						url: '/things-to-do/map/'+loc+'/',
						data: "",
						success: function(data){
							CVB.loadmap(data, position);
						}
					});
				}

				else if ($(this).hasClass("where-to-stay"))
				{
					$.ajax({
						type: "GET",
						url: '/where-to-stay/map/'+loc+'/',
						data: "",
						success: function(data){
							CVB.loadmap(data, position);
						}
					});
				}
				
				else
				{
					CVB.loadmap(loc, position);				
				}
					
				return false;
								
			});
		},
	
		init : function(){
			CVB.maps();
			CVB.links();
			CVB.validate();
			CVB.forms();
			CVB.tabs();
			CVB.events();
		}
	
	};
	
	CVB.init();
	


	// CLEAR SEARCH BOXES
	$('.default').each(function() {
		var default_value = this.value;
		$(this).focus(function() {
			if(this.value == default_value) {
			this.value = '';
			}
		});
		$(this).blur(function() {
			if(this.value == '') {
			this.value = default_value;
			}
		});
	});

	// MAP TOOLTIPS
	$('.lgmap .label').hover(function() {
		$('.lgmap .label').addClass('off');
		$(this).addClass('over');
	}, function() {
		$('.lgmap .label').removeClass('off');
		$(this).removeClass('over');
	});

	// LOGIN FORM
	$("a.login").live("click", function(){
		var url = $(this).attr("href");
		$.ajax({
			type: "POST",
			url: url,
			//data: "",
			success: function(data){
				var winW = $(window).width();
				$("body").append(data);
				var w = $("#login-content").width();
				$("#login-content").css('left', winW/2-w/2).fadeIn("slow");
				$("#overlay").fadeIn("slow");
			}
		});
		return false;	
	});
	//show/hide forms
	$("#username-link").live("click", function(){
		$("#login-form").css('display', 'none');
		$("#username-form").css('display', 'block');
	});
	$("#password-link").live("click", function(){
		$("#login-form").css('display', 'none');
		$("#password-form").css('display', 'block');
	});
	// close the forms
	$(document).keyup(function(event){
		if (event.keyCode == 27) {
			CVB.overlays();
		}	
	});
	$("a.close").live("click", function(){
			CVB.overlays();
			return false;
	});
	$("#overlay").live("click", function(){
			CVB.overlays();
			return false;
	});

});
