var map = null;
var geocoder = null;

function addMarker(address, content) {
	if (geocoder) {
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					//alert(address + " not found");
				} else {
					var marker = new GMarker(point);
					map.addOverlay(marker);
					GEvent.addListener(marker, "click", function() {
						this.openInfoWindowHtml(content, {maxWidth: 320});
					});
				}
			}
		);
	}
}

function generateMap(address) {
	if (geocoder) {
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					alert(address + " not found");
				} else {
					var marker = new GMarker(point);
					map.addOverlay(marker);
					map.setCenter(point, 12);
					map.addControl(new GSmallMapControl());
					map.addControl(new GMapTypeControl());
					map.enableScrollWheelZoom();
				}
			}
		);
	}
}


function findACopyNavigation() {
   var zip = document.getElementById("zip").value;
   document.getElementById("errorMessage").style.display="none";
   var validZips = new Array("46311","46321","46322","46375","46394","60002","60004","60005","60007","60008","60010","60013","60014","60015","60016","60018","60020","60021","60022","60025","60026","60030","60031","60033","60035","60040","60041","60044","60045","60046","60047","60048","60050","60053","60056","60060","60061","60062","60064","60067","60068","60069","60070","60071","60073","60074","60076","60077","60081","60083","60084","60085","60087","60088","60089","60090","60091","60093","60098","60099","60101","60102","60103","60104","60106","60107","60108","60110","60118","60120","60123","60124","60126","60130","60131","60133","60134","60137","60139","60141","60142","60143","60144","60148","60153","60154","60155","60156","60160","60162","60163","60164","60165","60169","60171","60172","60173","60174","60176","60177","60179","60181","60185","60187","60188","60189","60190","60191","60192","60193","60194","60195","60196","60201","60202","60208","60301","60302","60304","60305","60402","60404","60406","60408","60409","60410","60411","60415","60417","60419","60422","60423","60425","60426","60429","60430","60431","60432","60433","60435","60436","60438","60439","60440","60441","60442","60443","60445","60446","60448","60449","60451","60452","60453","60455","60457","60458","60459","60461","60462","60463","60464","60465","60466","60467","60471","60472","60473","60475","60477","60478","60480","60482","60491","60501","60502","60504","60505","60506","60510","60513","60514","60515","60516","60517","60521","60523","60525","60526","60527","60532","60534","60540","60542","60543","60544","60546","60555","60558","60559","60561","60563","60564","60565","60585","60586","60601","60602","60603","60604","60605","60606","60607","60608","60609","60610","60611","60612","60613","60614","60615","60616","60617","60618","60619","60620","60621","60622","60623","60624","60625","60626","60627","60628","60629","60630","60631","60632","60633","60634","60636","60637","60638","60639","60640","60641","60642","60643","60644","60645","60646","60647","60648","60649","60651","60652","60653","60654","60655","60656","60657","60659","60660","60661","60667","60706","60707","60712","60714","60803","60804","60805","60827","60914");
	if(jQuery.inArray(zip, validZips) > -1)
		{		
		window.location = "http://www.chicagoparent.com/about-chicago-parent/find-a-copy" + "#" + zip;
		}
        else
		{
		document.getElementById("errorMessage").style.display="block";
		document.getElementById("searchZip").innerHTML = zip;	
		document.getElementById("zip").value = '';
		}
}

$(document).ready(function() {

	//Home Rotator
//	if ($("#home-rotator").length > 0) {
//		$("#home-rotator .slides").cycle({
//			prev: '#ss-home-prev',
//			next: '#ss-home-next',
//			timeout: 7000
//		});
//		$("#ss-home-stop").click(function(e) {
//			$("#home-rotator .slides").cycle('pause');
//		});
//	} 
	
	//Tab Controls
	$("#home-tabber-featured").tabs();
	$("#tabber-community").tabs();
	$("#tabber-popular").tabs();
	$("#tabber-related").tabs();
	$("#media-widget").tabs();
	$("#tabber-resources").tabs();
	
	
	//Event Calendar
	if ($("#event-calendar").length > 0) {
		$("#event-calendar div.prev:first").html('');
		$("#event-calendar div.next:last").html('');
		$("#event-calendar ul.week li:first").toggleClass('active');
		$("#event-calendar div.events:first").show();
		$("#event-calendar ul.week li").click(function(e) {
			e.preventDefault;
	
		});
		$("#event-calendar ul.week li").each(function(i) {
			$(this).click(function(e) {
				e.preventDefault();
				$("#event-calendar div.events").hide();
				$("#event-calendar div.events:eq("+i+")").show();
				$("#event-calendar ul.week li").removeClass('active');
				$(this).addClass('active');
			});
		});
		$("#event-calendar div.week:first").show();
		$("#event-calendar .next").click(function(e) {
			if ($(this).html() != "") {
				$(this).parent().hide();
				$(this).parent().nextAll("div.week").show();
			}
		});
		$("#event-calendar .prev").click(function(e) {
			if ($(this).html() != "") {
				$(this).parent().hide();
				$(this).parent().prevAll("div.week").show();
			}
		});
	}
	
	//Star Ratings
	if ($(".auto-submit-star").length > 0) {
		var nodeID = $("input[name=contentNodeId]").val()
	
		$(".auto-submit-star").rating({
			callback: function(value, link){
				if (value != "") {
					$.ajax({
		                type: "POST",
		                url: "/ws/ratings.asmx/addrating",
		                data: {
		                    contentNodeID: nodeID,
		                    selectRating: value
		                }
		            });
				}
			}
		});
		
		$.post("/ws/ratings.asmx/getrating", { contentNodeID: nodeID },
			function(data, textStatus) {
				$(".auto-submit-star").rating('select', $("int", data).text());
			},
			"xml"
		);
	}
	
	//Email a Friend
	$("#email-friend-box").dialog({
		autoOpen: false,
		draggable: false,
		resizable: false,
		width: 350,
		height: 450
	});
	
	$("#email-friend-confirm").dialog({
		autoOpen: false,
		draggable: false,
		resizable: false
	});
	
	$("a.email-friend").bind("click", function(e) {
	    e.preventDefault();
		$("#email-friend-box").dialog("open");
	});
	
	$("div.email-friend").bind("click", function(e) {
	    e.preventDefault();
		$("#email-friend-box").dialog("open");
	});
	
	$(".submit-email-friend").click(function(e) {
		e.preventDefault();
		//alert($("#emailFriendNodeID").val());
		var isValid = true;
		errorText = "";
		
		if ($("#senderName").val() == "") {
			errorText += "<li>Please enter your name.</li>";
			isValid = false;
		}
		if ($("#senderEmail").val() == "") {
			errorText += "<li>Please enter your email.</li>";
			isValid = false;
		}
		if ($("#friendName").val() == "") {
			errorText += "<li>Please enter your friend's name.</li>";
			isValid = false;
		}
		if ($("#friendEmail").val() == "") {
			errorText += "<li>Please enter your friend's email.</li>";
			isValid = false;
		}
		
		if (isValid) {
			$.ajax({
	               type: "POST",
	               url: "/ws/email.asmx/emailfriend",
	               data: {
	                   senderName: $("#senderName").val(),
	                   senderEmail: $("#senderEmail").val(),
					   friendName: $("#friendName").val(),
					   friendEmail: $("#friendEmail").val(),
					   comments: $("#comments").val(),
					   linkBack: $("#emailFriendNodeID").val()
	               }
	         });
			 
			 $("#senderName").val("");
			 $("#senderEmail").val("");
			 $("#friendName").val("");
			 $("#friendEmail").val("");
			 $("#comments").val("");
			 
			 $("#email-friend-box").dialog("close");
			 $("#email-friend-confirm").dialog("open");
		} else {
			 $("#email-friend-box .error").html("<ul>" + errorText +"</ul>")
		}
	});
	
	
	//Contact Resource
	$("#contact-resource-box").dialog({
		autoOpen: false,
		draggable: false,
		resizable: false,
		width: 350,
		height: 450
	});
	
	$("#contact-resource-confirm").dialog({
		autoOpen: false,
		draggable: false,
		resizable: false
	});
	
	$("a.contact-resource").bind("click", function(e) {
	    e.preventDefault();
		$("#contact-resource-box").dialog("open");
	});
	
	$(".submit-contact-resource").click(function(e) {
		e.preventDefault();
		//alert($("#resourceID").val());
		var isValid = true;
		errorText = "";
		
		if ($("#contactName").val() == "") {
			errorText += "<li>Please enter your name.</li>";
			isValid = false;
		}
		if ($("#contactEmail").val() == "") {
			errorText += "<li>Please enter your email.</li>";
			isValid = false;
		}
		
		if (isValid) {
			$.ajax({
	               type: "POST",
	               url: "/ws/email.asmx/contactresource",
	               data: {
	                   contactName: $("#contactName").val(),
	                   contactEmail: $("#contactEmail").val(),
					   contactComments: $("#contactComments").val(),
					   resourceID: $("#resourceID").val()
	               }
	         });
			 
			 //clear the vars
			 $("#contactName").val("");
			 $("#contactEmail").val("");
			 $("#contactComments").val("");
			 $("#resourceID").val("");
			 
			 $("#contact-resource-box").dialog("close");
			 $("#contact-resource-confirm").dialog("open");
		} else {
			 $("#contact-resource-box .error").html("<ul>" + errorText +"</ul>")
		}
	});
	
	
	//carousel
	if ($("#photo-carousel").length > 0) {
		$('#large-photos  li:eq(1)').show();
	   	$('#photo-carousel').jcarousel();
		
		$("#photo-carousel li").each(function(i) {
			$(this).click(function(e) {
				e.preventDefault();
				$("#large-photos li").hide();
				$("#large-photos li:eq("+i+")").show();
			});
		});
	}
	
	// Calendar control for calendar page
	if ($("#mini-calendar").length > 0) {
		var dateLink = $("#mini-calendar a");
		var calLink = dateLink.attr("href");
		var curDate = dateLink.text();

		$("#mini-calendar").html('');
		
		var dateArr = curDate.split("/");
		var objDate = new Date();
		objDate.setYear(dateArr[2]);
		objDate.setMonth(dateArr[0]-1);
		objDate.setDate(dateArr[1]);
		
		$("#mini-calendar").datepicker({
			defaultDate: objDate,
			minDate: 0,
			onSelect: function(dateText, inst) {
				document.location.href = calLink + "&dt=" + dateText;
			}
		});
	}
	
	if ($("#calendar-map-results").length > 0) {
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("calendar-map-results"));
			map.setCenter(new GLatLng(41.875185,-87.626953), 12);
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.enableScrollWheelZoom();
						
			geocoder = new GClientGeocoder();
			
			$("#xml-calendar-results .event-item").each(function(i) {
				addMarker($(this).find(".meta-location").text(), $(this).html());
			});
		}
	}
	
	if ($("#search-map-results").length > 0) {
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("search-map-results"));
			map.setCenter(new GLatLng(41.875185,-87.726953), 10);
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.enableScrollWheelZoom();
						
			geocoder = new GClientGeocoder();
			
			$("#xml-search-results .search-result").each(function(i) {
				addMarker($(this).find(".meta-location").text(), $(this).html());
			});
		}
	}
	
	if ($("#media-widget-map").length > 0) {
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("media-widget-map"));
			geocoder = new GClientGeocoder();
			generateMap($(".meta-location").text());
		}
	}
	
	//div show/hide toggle
	$('a#slick-toggle').click(function() {
    $('#slickbox').toggle(200);
    	return false;
  	});
});


