$(document).ready(function () {
	//newsletter
	$("#newsletter #email").focus(function() {
		$(this).addClass("dark");
		var value = $(this).val()
		if (value == "JOIN OUR NEWSLETTER" || value == "TYPE YOUR EMAIL") {
			$(this).val("");
		}
	});
	$("#newsletter #email").blur(function() {
		$(this).removeClass("dark");
		var value = $(this).val()
		if (value == "") {
			$(this).val("JOIN OUR NEWSLETTER");
		}
	});
	$("#newsletter a").click(function() {
		$("#newsletter a img").attr("src", "/assets/images/newsletter_join_working.gif");
	
		var email_address = $("#newsletter #email").val();
					
		//create query string
		var q = "action=join_newsletter&email_address=" + email_address;
		
		//validate, submit
		$.ajax({type: "POST", url: "/includes/helpers/jquery.newsletter.php", data: q, dataType: "text", 
			success: function (data, textStatus) {			
				if (textStatus == "success") {
					$("#newsletter a img").attr("src", "/assets/images/newsletter_join.gif");
					if (data == "error") {
						$("#newsletter #email").val("TYPE YOUR EMAIL");
					}
					if (data == "complete") {
						$("#newsletter #email").val("THANK YOU");
					}
				}
			}
		});
	});

	//center city, data
	$("#data div.category .view-reports").click(function() {
		var parent = $(this).parent().parent().parent().parent();
		var parent_id = $(parent).attr("id");
		var info = new Array();
		info = parent_id.split("-");
		$(parent).find("img.view-reports").attr("src", "/assets/images/data_reports_on.gif");
		$(parent).find("img.view-stats").attr("src", "/assets/images/data_statistics_off.gif");
		$("#stats-" + info[1]).hide();
		$("#reports-" + info[1]).show();		 		
	});
	$("#data div.category .view-stats").click(function() {
		var parent = $(this).parent().parent().parent().parent();
		var parent_id = $(parent).attr("id");
		var info = new Array();
		info = parent_id.split("-");
		$(parent).find("img.view-reports").attr("src", "/assets/images/data_reports_off.gif");
		$(parent).find("img.view-stats").attr("src", "/assets/images/data_statistics_on.gif");
		$("#reports-" + info[1]).hide();
		$("#stats-" + info[1]).show();
	});
	
	//news, search
	$("#news_search input").keydown(function(event){
		if (event.keyCode == 13) {
			var keyword = $("#news_search input").val();
			if (keyword.length >= 3) window.location.href = "/news/search/" + keyword + "/";
		}
	});
});

/*

PLACES TO WORK

*/

function placesWorkPage(page) {
	$("#places_to_work").html("<img src=\"/assets/images/places_loader.gif\" class=\"places_loader\" />");

	//create query string
	var q = "action=places_work&page=" + page;
	
	//validate, submit
	$.ajax({type: "GET", url: "/includes/helpers/jquery.places_work.php", data: q, dataType: "text", 
		success: function (data, textStatus) {			
			if (textStatus == "success") {
				$("#places_to_work img").fadeOut("fast", function() {
					$("#places_to_work").html(data);
				});
			}
		}
	});
}

/*

PLACES TO LIVE

*/

function placesLivePage(neighborhood, page) {
	$("#places_to_live").html("<img src=\"/assets/images/places_loader.gif\" class=\"places_loader\" />");

	//create query string
	var q = "action=places_live&neighborhood=" + neighborhood + "&page=" + page;
	
	//validate, submit
	$.ajax({type: "GET", url: "/includes/helpers/jquery.places_live.php", data: q, dataType: "text", 
		success: function (data, textStatus) {			
			if (textStatus == "success") {
				$("#places_to_live img").fadeOut("fast", function() {
					$("#places_to_live").html(data);
				});
			}
		}
	});
}

/*

GALLERY

*/

//External Interface

var current_file = 0;
var gallery_init = false;

function loadGalleryFile() {
	var targ = document.getElementById('cccpgallery');
	//typeof unknown, IE
	if (typeof(targ.SetVariable) == "function" || typeof(targ.SetVariable) == "unknown") {
		targ.SetVariable("current_file", gallery.media[current_file].path);
	}
	if (typeof(targ.callLoadFile) == "function") {
		targ.callLoadFile();
	}
	$("#galleryopen div.caption").text(gallery.media[current_file].caption);
	$("#galleryopen div.nav span").text((current_file + 1) + " of " + gallery.media.length);
}
function loadGalleryNext() {
	var i = current_file + 1;
	if (i >= gallery.media.length) {
		i = 0;
	}
	current_file = i;
	loadGalleryFile();
}
function loadGalleryLast() {
	var i = current_file - 1;
	if (i < 0) {
		i = (gallery.media.length - 1);
	}
	current_file = i;
	loadGalleryFile();
}

function launchGallery(ID) {
	$("#gallery").hide();
	$("#galleryopen").show();
	
	//IE sniffer
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	
	if (gallery_init == true) {
		if (!isIE) {
			//if not IE: just load file
			if ($("#cccpgallery")) loadGalleryFile();
			return true;
		}
	}
	
	//if IE: clear flash, callLoadFile() doesn't reinit
	if (isIE) {
		if ($("#galleryopen")) {
			$("#galleryopen").html("");
		}
	}
	
	//create query string
	var q = "action=gallery&ID=" + ID;
	
	//validate, submit
	$.ajax({type: "POST", url: "/includes/helpers/jquery.gallery.php", data: q, dataType: "text", 
		success: function (data, textStatus) {			
			if (textStatus == "success") {
				$("#galleryopen").html(data);
				swfobject.embedSWF("/assets/swf/gallery.swf", "gallerymedia", "400", "300", "9.0.0", null, {}, {menu: "false"}, {id: 'cccpgallery'});
				if ($("#cccpgallery")) loadGalleryFile();
				//if one file, hide nav				
				if (gallery.media.length == 1) {
					$("#galleryopen .nav").hide();
				}
				//IE6 sniffer, load GIF buttons
				var isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;
				if (isIE6) {
					$("#gallery_last").html('<img src="/assets/images/gallery_last.gif" alt="" width="21" height="21" />');
					$("#gallery_next").html('<img src="/assets/images/gallery_next.gif" alt="" width="21" height="21" />');
				}
			}
		}
	});
	
	//preempty External Interface __flash_onUnload(), IE bug
	if (gallery_init == false) {
		window.onbeforeunload = function() {
			window.onunload = null;
		}
	}
	
	//if not IE, init
	gallery_init = true;
}
function closeGallery() {
	$("#galleryopen").hide();
	$("#gallery").show();
	
	//Firefox sniffer, weird selection bug
	var isFF = navigator.userAgent.toLowerCase().indexOf('firefox') != -1;
	if (isFF) {
		$("#gallery").focus();
	}
}



