var _isLoaded = false, _currentSiteVersion = "v1.0";

jQuery(document).ready(function($){


	// subscribe menu
	$("#sub-menu").hover(function(){         
		$("#sub-menu").stop().animate({height:'520px'},{queue:false, duration:600, easing: 'easeOutQuint'});
		$(this).toggleClass("sub-menu-active"); return false;
	},
	function() {
		$("#sub-menu").stop().animate({height:'32px'},{queue:false, duration:600, easing: 'easeInQuint'});
		$(this).toggleClass("sub-menu-active"); return true;
	});	

	// portland cats menu
	$("#navbar-portland").hover(function(){         
		$("#navbar-portland-list").show();
	},
	function() {
		$("#navbar-portland-list").hide();
	});	
	
	// maps menu
	$("#navbar-maps").hover(function(){         
		$("#navbar-maps-list").show();
	},
	function() {
		$("#navbar-maps-list").hide();
	});	
	// aboutus menu
	$("#navbar-aboutus").hover(function(){         
		$("#navbar-aboutus-list").show();
	},
	function() {
		$("#navbar-aboutus-list").hide();
	});	


	//generic toggle
	$("a.itemtoggler").click(function(){$(".toggleItems").toggle(); return false;});
	
	$("#eventMap").click(function(){
		//$(".toggleItems").toggle(); 
		center = mapEvents.getCenter();
		zoom = mapEvents.getZoom();
		mapEvents.checkResize();
		map.setCenter(center, zoom);
		return false;
	});
	
	//$('.pullquote-quotes').borderImage('url("/images/v1.0/pullquote-quotes.png") 30 0 30 0');
	
	var adjustheight = 13;
	$("#neighborhoodToggle").toggle(function() {
	$('#neighborhoodlist').css('height', 'auto').css('overflow', 'visible');
	$(this).text("Fewer neighborhoods");
	}, function() {
	$('#neighborhoodlist').css('height', adjustheight).css('overflow', 'hidden');
	$(this).text("More neighborhoods");
	});

	// quadrant tips
	$("a.np").hover(function() {
		$(".quadrant-guide").html("<p><span class='np'>North Portland</span><br />From Arbor Lodge to University Park.</p>");
	},
	function() {
		$(".quadrant-guide").html("<p>Click on the compass above to explore the 95 neighborhoods in <a href=\"/maps/\" title=\"View all the Portland Neighborhoods\">Portland, Oregon.</p>");
	});

	$("a.nw").hover(function() {
		$(".quadrant-guide").html("<p><span class='nw'>Northwest Portland</span><br />From Forest Park to Pearl District.</p>");
	},
	function() {
		$(".quadrant-guide").html("<p>Click on the compass above to explore the 95 neighborhoods in <a href=\"/maps/\" title=\"View all the Portland Neighborhoods\">Portland, Oregon.</p>");
	});

	$("a.ne").hover(function() {
		$(".quadrant-guide").html("<p><span class='ne'>Northeast Portland</span><br />From Alameda to Woodlawn.</p>");
	},
	function() {
		$(".quadrant-guide").html("<p>Click on the compass above to explore the 95 neighborhoods in <a href=\"/maps/\" title=\"View all the Portland Neighborhoods\">Portland, Oregon.</p>");
	});

	$("a.sw").hover(function() {
		$(".quadrant-guide").html("<p><span class='sw'>Southwest Portland</span><br />From Arlington Heights to West Portland Park.</p>");
	},
	function() {
		$(".quadrant-guide").html("<p>Click on the compass above to explore the 95 neighborhoods in <a href=\"/maps/\" title=\"View all the Portland Neighborhoods\">Portland, Oregon.</p>");
	});

	$("a.se").hover(function() {
		$(".quadrant-guide").html("<p><span class='se'>Southeast Portland</span><br />From Ardenwald-Johnson Creek to Woodstock.</p>");
	},
	function() {
		$(".quadrant-guide").html("<p>Click on the compass above to explore the 95 neighborhoods in <a href=\"/maps/\" title=\"View all the Portland Neighborhoods\">Portland, Oregon.</p>");
	});
	
	$("a.ep").hover(function() {
		$(".quadrant-guide").html("<p><span class='ep'>East Portland</span><br />From Argay to Woodland Park.</p>");
	},
	function() {
		$(".quadrant-guide").html("<p>Click on the compass above to explore the 95 neighborhoods in <a href=\"/maps/\" title=\"View all the Portland Neighborhoods\">Portland, Oregon.</p>");
	});				


	// tabbed sidebar lists 
	$('div.bestof div').hide();
	$('div.t1').show();
	$('div.bestof ul.tabs li.t1 a').addClass('tab-current');

	$('div.bestof ul.tabs li a').click(function(){
		var thisClass = this.className.slice(0,2);
		$('div.bestof div').hide();
		$('div.' + thisClass).show();
		$('div.bestof ul.tabs li a').removeClass('tab-current');
		$(this).addClass('tab-current');
	});


	// featured slideshow
    $('.slideshow, .slideshow-copy').cycle({
        fx:     'fade',
        speed:  1000,
        timeout: 5000,
        pager:  '.thumbs',
        pagerEvent: 'click',
        pagerAnchorBuilder: function(idx, slide) {
            // return sel string for existing anchor
            return '.thumbs li:eq(' + (idx) + ') a';
        }
    });

	// -------------------------------------------------

	$("#postPrint").click(function() {
		window.print();
		return false;
	});
	/*
	$("#postEmail").dialog();
*/
	$("a.blank").click(function() {
		this.target = "_blank";
	});
	$("form.blank").click(function() {
		this.target = "_blank";
	});
	$("span.post-unsubscribe").click(function() {
		var _postID		= 0;
		var _email 		= '';
		var _classNames	= $(this).attr("class").split(" ");
		var _rows		= _classNames.length;

		for (var _i=0;_i<_rows;_i++) {
			if (/^post-id-([0-9]+)$/.test(_classNames[_i]) === true) {
				_postID = parseInt(/^post-id-([0-9]+)$/.exec(_classNames[_i])[1],10);
			}
			if (/^comment-email-([^" ]+)$/.test(_classNames[_i]) === true) {
				_email = /^comment-email-([^" ]+)$/.exec(_classNames[_i])[1];
			}
		}

		if (_postID !== 0 && _email !== '') {
			$.get("/ajax/mod/post-unsubscribe/" + _postID.toString() + "/" + _email, function(_data) {
				if (_data === "success") {
					postUnsubscribeSuccess();
				} else {
					//alert("broke.");
				}
			});
		}
	
	});
	$("div.post-vote-down").click(function() {
		var _postID		= 0;
		var _classNames	= $(this).attr("class").split(" ");
		var _rows		= _classNames.length
		for (var _i=0;_i<_rows;_i++) {
			if (/^post-id-([0-9]+)$/.test(_classNames[_i]) === true) {
				_postID = parseInt(/^post-id-([0-9]+)$/.exec(_classNames[_i])[1],10);
			}
		}

		if (_postID !== 0) {
			$.get("/ajax/add/post-vote/" + _postID.toString() + "/down", function(_data) {
				if (_data === "success") {
					postVoteSuccess();
				} else {
					alert("It looks like you've already voted for this article.");
				}
			});
		}
	});
	$("div.post-vote-up").click(function() {
		var _postID		= 0;
		var _classNames	= $(this).attr("class").split(" ");
		var _rows		= _classNames.length
		for (var _i=0;_i<_rows;_i++) {
			if (/^post-id-([0-9]+)$/.test(_classNames[_i]) === true) {
				_postID = parseInt(/^post-id-([0-9]+)$/.exec(_classNames[_i])[1],10);
			}
		}

		if (_postID !== 0) {
			$.get("/ajax/add/post-vote/" + _postID.toString() + "/up", function(_data) {
				if (_data === "success") {
					postVoteSuccess();
				} else {
					alert("It looks like you've already voted for this article.");
				}
			});
		}
	});
	
$.fn.idle = function(time)
  {
      var o = $(this);
      o.queue(function()
      {
         setTimeout(function()
         {
            o.dequeue();
         }, time);
      });
      return this;              //****
  }

	postVoteSuccess = function () {
		//$("#voting").fadeOut("fast");
		//$("div.post-vote-up").hide("slow");
		//$('#voting').fadeOut("fast").idle(1000);
		$('#voting1').text('Thanks for voting!'); 
		$('#voting2').text('Thanks for voting!'); 
		//$('#voting').text('Thanks for voting!')
		//$("#voting").fadeIn("medium");

	}
	
	postUnsubscribeSuccess = function () {
		$("p.comment-unsubscribe").fadeOut("medium", function() {$("p.comment-unsubscribe").text("You will no longer receive comments via email for this post.");});
		$("p.comment-unsubscribe").fadeIn("medium");
	}
	
	
	
	// PORTED
	addComment = {
		moveForm: function(d,f,i) {
			var m = this, a, h = m.I(d), b = m.I(i), l = m.I("cancel-comment-reply-link"), j = m.I("postCommentParentID");
			if (!h || !b || !l || !j) {
				return;
			}
			m.respondId = i;
			if (!m.I("comment-temp-form-div")) {
				a = document.createElement("div");
				a.id = "comment-temp-form-div";
				a.style.display = "none";
				b.parentNode.insertBefore(a,b);
			}
	
			h.parentNode.insertBefore(b,h.nextSibling);
			j.value = f;
			l.style.display = "";
			l.onclick = function() {
				var n = addComment, e = n.I("comment-temp-form-div"), o = n.I(n.respondId);
				if (!e || !o) {
					return;
				}
				n.I("postCommentParentID").value = "0";
				e.parentNode.insertBefore(o,e);
				e.parentNode.removeChild(e);
				this.style.display = "none";
				this.onclick = null;
	
				return false;
			}
			try {
				m.I("comment").focus();
			} catch(g) {}
		},
	
		I: function(a){
			return document.getElementById(a)
		}
	};
	// / PORTED
	
	submitPostComment = function() {
		if ($("#postCommentName")[0].value.trim() === "") {
			alert("Please enter your Name");
			$("#postCommentName")[0].focus();
			return false;
		}
	
		if ($("#postCommentEmailAddress")[0].value.match(/^[a-zA-Z0-9._%\-+]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,6}$/) === null ||
			$("#postCommentEmailAddress")[0].value.match(/^[a-zA-Z0-9._%\-+]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,6}$/)[0] !== $("#postCommentEmailAddress")[0].value) {
			alert("Please enter your Email Address");
			$("#postCommentEmailAddress")[0].focus();
			return false;
		}
	
		if ($("#postComments")[0].value.trim() === "") {
			alert("Please enter some Comments");
			$("#postComments")[0].focus();
			return false;
		}
	
		return true;
	}
	
	
	submitBusinessReview = function() {
	
		if ($("#businessReview")[0].value.trim() === "") {
			alert("Please enter your review");
			$("#businessReview")[0].focus();
			return false;
		}
	
		if ($("#businessReviewName")[0].value.trim() === "") {
			alert("Please enter your Name");
			$("#businessReviewName")[0].focus();
			return false;
		}
	
		if ($("#businessReviewEmailAddress")[0].value.match(/^[a-zA-Z0-9._%\-+]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,6}$/) === null ||
			$("#businessReviewEmailAddress")[0].value.match(/^[a-zA-Z0-9._%\-+]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,6}$/)[0] !== $("#businessReviewEmailAddress")[0].value) {
			alert("Please enter your Email Address");
			$("#businessReviewEmailAddress")[0].focus();
			return false;
		}
	
		if ($("#businessReviewApproveRules")[0].checked !== true) {
			alert("Please agree to the rules for posting reviews");
			$("#businessReviewApproveRules")[0].focus();
			return false;
		}
	
		return true;
	}
	
	submitContribute = function() {
		if ($("#contributeName")[0].value.trim() === "") {
			alert("Please enter your Name");
			$("#contributeName")[0].focus();
			return false;
		}
		if ($("#contributeEmailAddress")[0].value.match(/^[a-zA-Z0-9._%\-+]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,6}$/) === null ||
			$("#contributeEmailAddress")[0].value.match(/^[a-zA-Z0-9._%\-+]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,6}$/)[0] !== $("#contributeEmailAddress")[0].value) {
			alert("Please enter your Email Address");
			$("#contributeEmailAddress")[0].focus();
			return false;
		}
		return true;
	}
	submitContact_us = function() {
		if ($("#contact_usMessage")[0].value.trim() === "") {
			alert("Please enter your message");
			$("#contact_usMessage")[0].focus();
			return false;
		}
		if ($("#contact_usName")[0].value.trim() === "") {
			alert("Please enter your Name");
			$("#contact_usName")[0].focus();
			return false;
		}
		if ($("#contact_usEmailAddress")[0].value.match(/^[a-zA-Z0-9._%\-+]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,6}$/) === null ||
			$("#contact_usEmailAddress")[0].value.match(/^[a-zA-Z0-9._%\-+]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,6}$/)[0] !== $("#contact_usEmailAddress")[0].value) {
			alert("Please enter your Email Address");
			$("#contact_usEmailAddress")[0].focus();
			return false;
		}	
	
		return true;
	}
	
	clearField = function(thisField) {
		 if (thisField.value == thisField.defaultValue) {
			 thisField.value = "";
		 }
	}
	
	
		
//	getLatLong = function(address) {
//		$.ajax({
//		   type: "POST",
//		   url: "/ajax/getLatLong",
//		   data: "address=" . address,
//		   success: function(msg){
//			 alert( "Data Saved: " + msg );
//		   }
//		 });
//	}
	
	validateSponsoredContentForm = function(_type) {
		if (_type === "event") {
			//if ($("#sponsoredContentType")[0].value.trim() === "") {
			//	alert("Please select Business Event or Charity Event");
			//	$("#sponsoredContentType")[0].focus();
			//	return false;
			//}
			if ($("#postTitle")[0].value.trim() === "") {
				alert("Please enter the Event Name");
				$("#postTitle")[0].focus();
				return false;
			}
			if ($("#postLocation")[0].value.trim() === "") {
				alert("Please enter the Venue Name");
				$("#postLocation")[0].focus();
				return false;
			}
			if ($("#postEventDate")[0].value.trim() === "") {
				alert("Please enter the Event Date and Time");
				$("#postEventDate")[0].focus();
				return false;
			}
			//if ($("#postContent")[0].value.trim() === "") {
			//	alert("Please enter the Description");
			//	$("#postContent")[0].focus();
			//	return false;
			//}
			if ($("#sponsoredContentName")[0].value.trim() === "") {
				alert("Please enter Your Name");
				$("#sponsoredContentName")[0].focus();
				return false;
			}
			if ($("#sponsoredContentEmailAddress")[0].value.trim() === "") {
				alert("Please enter Your Email Address");
				$("#sponsoredContentEmailAddress")[0].focus();
				return false;
			}
		} else if (_type === "news") {
			if ($("#sponsoredContentType")[0].value.trim() === "") {
				alert("Please select Paid News Item or Charity News Item");
				$("#sponsoredContentType")[0].focus();
				return false;
			}
			if ($("#postTitle")[0].value.trim() === "") {
				alert("Please enter the Title");
				$("#postTitle")[0].focus();
				return false;
			}
			//if ($("#postContent")[0].value.trim() === "") {
			//	alert("Please enter the Description");
			//	$("#postContent")[0].focus();
			//	return false;
			//}
			if ($("#postOrgName")[0].value.trim() === "") {
				alert("Please enter the Business/Organization Name");
				$("#postOrgName")[0].focus();
				return false;
			}
			if ($("#sponsoredContentName")[0].value.trim() === "") {
				alert("Please enter Your Name");
				$("#sponsoredContentName")[0].focus();
				return false;
			}
			if ($("#sponsoredContentEmailAddress")[0].value.trim() === "") {
				alert("Please enter Your Email Address");
				$("#sponsoredContentEmailAddress")[0].focus();
				return false;
			}
		}
	
		return true;
	}

	
	
});


confirmRedirect = function(_question,_url) {
	if (confirm(_question) === true) {
		window.location.href = _url;
		return true;
	}
	return false;
}
