var AdTop = {
	init: function() {
		if($("#bannerTop").get(0)) {
			
			$("#header").before("<div id='btnExpand'></div>");
			$("#bannerTop").after("<div id='bannerTopFull'></div>");
			var bannerTopUrl = $("#bannerTop img").attr("src");
			$("#bannerTopFull").css("backgroundImage","url(" + bannerTopUrl +")");
			$("#btnExpand").click(function() {
				 if ($("#bannerTopFull").is(":hidden")) {
					$("#bannerTopFull").slideDown("slow", function() {
						$("#btnExpand").addClass("close");
					});
				} else {
					$("#bannerTopFull").slideUp("slow", function() {
						$("#btnExpand").removeClass("close");
					});
				}
			});
		};
	}
}
function openPdf(pdfAddress) {
	window.open(pdfAddress,"mywindow","status=1,resizable=yes,width=800,height=600");
	tb_remove();
	return false;
}
function isIE6() {
	if($("body").get(0).style.maxHeight==undefined) { return true; } else { return false; }
}
function formSubmit(formID){
	document.getElementById(formID).submit();
}

/* RESTAURANTS */
var Restaurants = { 
	chosen: "",
	check: function() {
		Restaurants.chosen = "";
		$("#restaurantServicesInputs input").each(function() {
			var e = this;
			if(e.checked) {
				Restaurants.chosen += '<li>' + $(e).next().html() + '</li>';
			};
		});
		if(Restaurants.chosen!="") {
			Restaurants.chosen = '<ul>' + Restaurants.chosen + '</ul>';
			$("#restaurantsServicesChosen").show();
		}
		$("#restaurantsServicesChosen").html(Restaurants.chosen);
				$("#restaurantServicesInputs").hide();
	}, 
	filter: function() {
		Restaurants.check();
		$("#restaurantServicesInputs").prepend('<span id="btnDone">Done</span>')
		$("#restaurantsServices h3").after('<span id="btnSelect">Select</span>');
		$("#btnSelect").click(function() {
			$("#restaurantServicesInputs").show();
		});
		$("#btnDone").click(function() {
			Restaurants.check();
			$("#btnSelect").show();
		});
	}, 
	setSearch: function() {
		var sStr = "";
		var food = $("#fs option:selected").text();
		var service = $("#ss option:selected").text();
		var town = $("#ts option:selected").text();
		if($("#ts option:selected").val()!= ""){ sStr += town}
		if($("#fs option:selected").val()!= ""){ if(sStr=="") {sStr += food} else {sStr += ', ' + food} }
		if($("#ss option:selected").val()!= ""){ if(sStr=="") {sStr += service} else {sStr += ', ' + service} }
		if(sStr!="") {
			$("#searchString").html('Search for:<br/> <strong>' + sStr + '</strong>');
		} else {
			$("#searchString").html('');
		}
	}, 
	map: function() {
		$("#ts").hide();
		$("#restaurantsFilterBtn").append('<div id="searchString"></div>');
		$("#fs").change(function() {
			Restaurants.setSearch();
		});
		$("#ss").change(function(e) {
			Restaurants.setSearch();
		});
		jQuery.each($("#map area"), function(i) {
			var ord = i + 1;
			var e = $(this);
			e.mouseover(function() {
				if($("#ts").attr("value") == "") {
					var src = '../_graphics/restaurants_map_' + ord + '.jpg';
					$("#imagemap").attr("src", src);
				}
			});
			e.mouseout(function() {
				if($("#ts").attr("value") == "") {
					$("#imagemap").attr("src", "../_graphics/restaurants_map.jpg");
				}
			});
			e.click(function(){
				var src;
				if($("#ts").attr("value") == ord) {
					$("#ts").attr("value", '');
				} else {
					$("#ts").attr("value", ord);
				}
				if($("#ts").attr("value") == "") {
					src = '../_graphics/restaurants_map.jpg';
					Restaurants.setSearch();
				} else {
					src = '../_graphics/restaurants_map_' + ord + 'a.jpg';
					Restaurants.setSearch();
				}
				$("#imagemap").attr("src", src);

				return false;
			});
		});	
	}, 
	featured: function() {
		var original = '';
		$(".restLogo").each(function() {
			var e = $(this);
			e.data("active", "0");
			var href = e.find('a').attr("href").split("/");
			e.data("id", href[3]);
			e.click(function() {
				$("#pickedRestaurant").remove();
				if(e==original) {
					if (e.data("active") == "1") {
						e.data("active", "0");
						e.removeClass('restActive');
					} else {
						Restaurants.featuredOpen(e);
					}
				} else {
					if(original!='') {
						original.removeClass('restActive');
					}
					Restaurants.featuredOpen(e);
				}
				
				original = e;
				
				return false;
			});
		});
	}, 
	featuredOpen: function(active) {
			active.append('<div id="loadingI">Loading...</div>');
			active.parent().after('<div id="pickedRestaurant"></div>');	
			var params = 'id=' + active.data("id");
			$.ajax({
				type: "GET",
				url: "/dining/ajax/restaurant/",
				data: params,
				success: function(data){
					$("#pickedRestaurant").html(data);
					tb_init('#featuredRestaurants a.thickbox');
					active.addClass('restActive');
					active.data("active", "1");
					$("#pickedRestaurant").slideDown("slow", function() {
						$("#loadingI").fadeOut(1000, function() {
							$(this).remove();
						}) ;
					});
				}, 
				error: function() { 
					alert('An error occured. Please contact us via our feedback form.');
				}
			});

		
	}
}
/* PHOTOGALLERY */
var Photogallery = {
	setImage: function(image, ord) {
		$("#imageHolder").addClass("loading");
		$("#image").fadeOut("fast", function() {
			$("#imageHolder").html('<div id="image"></div>');
			$("<img />").bind('load', function(){
				$("#image").fadeIn("slow");
				$("#imageHolder").removeClass("loading");
			}).appendTo('#image').attr({
					src: image.href,
					height: image.h,
					width: image.w
				});
		});
		$("p#imageTitle").html(image.title);
		$('#actualImgOrd').html(ord);
		$(actualImg).removeClass("actualImage");
		actualImg = image;
		$(actualImg).addClass("actualImage");
	},
	init: function() {
		var $pane = $('#pane');
		$pane.jScrollPane({animateTo:true, showArrows:true});
		var actualOrd = 1;
		var allImgs = $('#pane a');
		jQuery.each(allImgs, function(i) {
			this.ord = i + 1;
			var a = this.href.split("=");
			var b = a[1].split("&");
			this.w = a[2];
			this.h = b[0];
		});	
		$("#imageHolder").addClass("loading");
		var allImgsLength = allImgs.length;
		actualImg = allImgs[0];
		$('#allImgLength').html(allImgsLength);
		Photogallery.setImage(allImgs[0], actualOrd);
		$('#previous').append('<span></span>').find('span').attr("class", "inactive").click(function() {
			var previous = actualOrd - 1;
			if(previous==0) {
				return false;
			} else {
				if(previous<allImgsLength) { $("#next").find('span').removeClass("inactive");}
				if(previous==1) { $("#previous").find('span').addClass("inactive");}
				var prevIndex = previous - 1;
				var prevImage = allImgs[prevIndex];
				actualOrd = actualOrd - 1;
				Photogallery.setImage(prevImage, actualOrd); 
				var target = "#img" + (actualOrd - 4);
				if(allImgs.length > 8) {
					$pane[0].scrollTo(target);
				}
			}
		});
		$('#next').append('<span></span>').find('span').click(function() {
			var next = actualOrd + 1;
			if(next>allImgsLength) {
				return false;
			} else {
				if(next>1) { $("#previous").find('span').removeClass("inactive");}
				if(next==allImgsLength) { $("#next").find('span').addClass("inactive");}
				var nextImage = allImgs[actualOrd];
				actualOrd = actualOrd + 1;		
				Photogallery.setImage(nextImage, actualOrd);	
				var target = "#img" + (actualOrd-4);
				if(allImgs.length > 8) {
					$pane[0].scrollTo(target);
				}
			}
		});
		allImgs.click(function() {
			if(this.ord != actualOrd) {
				actualOrd = this.ord;
				Photogallery.setImage(this, actualOrd); 
				var target = "#img" + (actualOrd-4);
				if(allImgs.length > 8) {
					$pane[0].scrollTo(target);
				}
				if((actualOrd>1)&&(actualOrd<allImgsLength)) { 
					$("#previous").find('span').removeClass("inactive");
					$("#next").find('span').removeClass("inactive");
				} else {
					if(actualOrd==1) {
						$("#previous").find('span').addClass("inactive");
						$("#next").find('span').removeClass("inactive");				
					} else {
						$("#previous").find('span').removeClass("inactive");
						$("#next").find('span').addClass("inactive");
					}
				}
				if(actualOrd==allImgsLength) { $("#next").find('span').addClass("inactive");}
				actualImg = this;
			} 
			return false;
		});
		if(allImgsLength==1) {$("#next").find('span').addClass("inactive")};
	}
}

/* GOOGLE MAPS */
var GM = {
	map: {}, 
	directions: {}, 
	actualSpot: "",
	actualHtml: "",
	actualAddress: "",
	createMarker: function(point,html) {
		var marker = new GMarker(point);
			GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html);
			actualSpot = marker;
			actualHtml = html;
		});
		markers.push(marker);
		return marker;
	},
	init: function(){
		markers = [];
		if (GBrowserIsCompatible()) {
			GM.map = new GMap2(document.getElementById("map_canvas"));
			GM.map.setCenter(new GLatLng(mLatitude, mLongitude), zoomLevel);
			GM.map.addControl(new GSmallMapControl());
			GM.directions = new GDirections(GM.map, document.getElementById("directions"));
			GEvent.addListener(GM.directions, "load", GM.onGDirectionsLoad);
			GEvent.addListener(GM.directions, "error", GM.handleErrors);
			if(xml!="") {
				$.ajax({
					type: "GET",
					url: xml,
					dataType: "xml",
					error: function() {
						alert('if IE fails at XML parsing but Firefox is okay, be sure to add this line in your php file: header(Content-type: text/xml');
					}, 
					success: function(xml) {
						$(xml).find('location').each(function(){
							var title = $(this).attr('name');
							var address = $(this).attr('address');
							var latitude = $(this).attr('latitude');
							var longitude = $(this).attr('longitude');
							var active = $(this).attr('active');
							var img = $(this).attr('img');
							var point = new GLatLng(latitude, longitude);
							var html = '<div id="bubble"><h2>' + title + '</h2><p>' + address + '</p>';
							html += '<form action="#" onsubmit="GM.setDirections(this.userAddress.value, this.mapAddress.value, this.dirType.value); return false;"><div style="width: 250px;">Get directions:&nbsp;&nbsp;<span onclick="GM.setFromAndTo(\'to\')" id="toHere">To here</span> - <span onclick="GM.setFromAndTo(\'from\')" id="fromHere">From here</span></div><div id="dirsForm" style="display: none;"><input type="text" name="userAddress" id="userAddress" value="" style="width: 220px;height: 18px;margin-bottom: 10px;" onclick="GM.setInput(this);" /><input type="hidden" name="mapAddress" value="' + address + '" /><input type="hidden" name="dirType" id="dirType" value="to" /><input type="submit" value="GO" /><br />Example: 10 Strawberry Road, Falmouth, MA</div></form></div>';
							var marker = GM.createMarker(point, html);
							GM.map.addOverlay(marker);
						});
					}
				 });
			} else {
				var point = new GLatLng(latitude, longitude);
				var html = '<div id="bubble"><h2>' + title + '</h2><p>' + address + '</p>';
				html += '<form action="#" onsubmit="GM.setDirections(this.userAddress.value, this.mapAddress.value, this.dirType.value); return false;"><div style="width: 250px;">Get directions:&nbsp;&nbsp;<span onclick="GM.setFromAndTo(\'to\')" id="toHere">To here</span> - <span onclick="GM.setFromAndTo(\'from\')" id="fromHere">From here</span></div><div id="dirsForm" style="display: none;"><input type="text" name="userAddress" id="userAddress" value="" style="width: 220px;height: 18px;" onclick="GM.setInput(this);" /><input type="hidden" name="mapAddress" value="' + address + '" /><input type="hidden" name="dirType" id="dirType" value="to" /><input type="submit" value="GO" /><br /><span class="example">Example: 10 Strawberry Road, Falmouth, MA</span></div></form></div>';
				var newMarker = GM.createMarker(point, html, active);
				GM.map.addOverlay(newMarker);
				newMarker.openInfoWindowHtml(html);
				actualSpot = newMarker;
				actualHtml = html;
			}
		}
	},
	setInput: function(link) {
		link.value = "";
	}, 
	setFromAndTo: function(type){
		$("#dirType").attr("value", type);
		$("#userAddress").attr("value", ((type=="to")?"Type your start address":"Type your end address"));
		if(type=="to") {
		 	$("#toHere").addClass("bold");
		 	$("#fromHere").removeClass("bold");
		} else {
			$("#fromHere").addClass("bold");
			$("#toHere").removeClass("bold");
		}
		$("#dirsForm").show();
		GM.map.updateInfoWindow();
		return false;
	},
	setDirections: function(userAddress, mapAddress, type){
		GM.directions.load("from: " + ((type=="to")?userAddress:mapAddress) + " to: " + ((type=="to")?mapAddress:userAddress), { "locale": "en" });
		return false;
	}, 
	handleErrors: function(){
		if (GM.directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
			alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + GM.directions.getStatus().code);
		else if (GM.directions.getStatus().code == G_GEO_SERVER_ERROR)
			alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + GM.directions.getStatus().code);
		else if (GM.directions.getStatus().code == G_GEO_MISSING_QUERY)
			alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + GM.directions.getStatus().code);
		else if (GM.directions.getStatus().code == G_GEO_BAD_KEY)
			alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + GM.directions.getStatus().code);
		else if (GM.directions.getStatus().code == G_GEO_BAD_REQUEST)
			alert("A directions request could not be successfully parsed.\n Error code: " + GM.directions.getStatus().code);
		else alert("An unknown error occurred.");
	}, 
	onGDirectionsLoad: function(){
		GM.map.clearOverlays();
		var dirElm = document.getElementById("directions");
		dirElm.style.width = "380px";
		dirElm.style.display = "";
		GM.map.getContainer().style.width="400px";
		GM.map.checkResize();
		$("#TB_closeAjaxWindow").append('<span id="dirPrint">Print directions</span>');
		$("#TB_closeAjaxWindow").append('<span id="dirReset">&#171; Back to map</span>');
		$("#TB_ajaxWindowTitle").html('Directions');
		
		$("#dirReset").click(function() {
			GM.map.clearOverlays();
			//$("#directions").html('');
			
			document.getElementById("directions").style.display = "none";
			document.getElementById("directions").style.width = "0px";
			GM.map.getContainer().style.width="795px";
			$("#TB_ajaxWindowTitle").html('Map');
			$("#dirPrint").remove();
			$("#dirReset").remove();
			GM.map.checkResize();
			jQuery.each(markers, function() {
				GM.map.addOverlay(this);
				actualSpot.openInfoWindowHtml(actualHtml);
			});
		});
		$("#dirPrint").click(function() {
			var dirPrintWindow = window.open('','directions','width=500,height=600,scrollbars=yes,resizable=yes');
			var dirPrintWindowHtml = '<html><head><title>Directions</title><style>#dirPrint, #dirReset {display: none;} \n div, a { cursor: default !important; } a {color: #1C67A8 !important;} </style></head><body><h1 style="font-family: arial;color: #144A8F; font-size: 20px; margin-bottom: 10px;">Directions</h1>' + $("#directions").html() + '</body></html>';
  			dirPrintWindow.document.write(dirPrintWindowHtml);
			dirPrintWindow.document.close();
			dirPrintWindow.print();
			return true;
		});
	}
}

var Accordion = {
	init: function() {
		if($(".expHid").length > 0 ) {
		
		/*
			$("#expList").prepend('<div id="expLinkAll">View all details</div>').find("#expLinkAll").data("opened", "0").click(function() {
				var e = $(this);
				if(e.data("opened")=="0") {
					e.html("Hide all details");
					e.data("opened","1");
					$(".expHid").slideDown("slow");
					$(".expLink").data("opened", "1").html("Hide details");
				} else {
					e.html("View all details");
					e.data("opened","0");
					$(".expHid").slideUp("slow");
					$(".expLink").data("opened", "0").html("View details");
				}
			});
		*/
			$(".exp").each(function() {
				$(this).append('<div class="expLink">View details</div>');
			});
			$(".expLink").each(function() {
				var e = $(this);
				e.data("opened","0");
				e.click(function(){
					if(e.data("opened")=="0") {
						e.html("Hide details");
						e.data("opened","1");
						e.prev(".expHid").slideDown("slow");
					} else {
						e.html("View details");
						e.data("opened","0");
						e.prev(".expHid").slideUp("slow");
					}
				});
			});
		}
	}
}

var AccordionSimple = {
	init: function() {
		if($(".accordion .acc").length > 0 ) {
			$(".acc .accTitle").each(function() {
				var e = $(this);
				e.data("opened","0");
				e.click(function(){
					if(e.data("opened")=="0") {
						e.data("opened","1");
						e.next(".accCon").slideDown("slow");
					} else {
						e.data("opened","0");
						e.next(".accCon").slideUp("slow");
					}
				});
			});
		}
	}
}
/* Image preview script 
 * written by Alen Grakalic (http://cssglobe.com)
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 */
this.ttip = function(){	
	xOffset = -10;
	yOffset = 10;
	$(".ttip").hover(function(e){
		this.t = this.title;
		this.title = "";	
		
		var c = (this.t != "") ? this.t : "";
		$("body").append("<p id='ttip'>"+ c +"</p>");
		$("#ttip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
	},
	function(){
		this.title = this.t;
		$("#ttip").remove();
	});	
	$(".ttip").mousemove(function(e){
		$("#ttip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
};

var FormsHints = {
	init: function() {
		$("form.hints input.text").each(function() {
			var e = $(this);
			e.focus(function() {
				FormsHints.showHint(e.attr("id") + '-Hint');
			});
			e.blur(function() {
				FormsHints.hideHint(e.attr("id") + '-Hint');
			});
		});
		$("p.formHint").each(function() {
			var e = $(this);
			e.mouseover(function() {
				FormsHints.showHint(e.attr("id"));
			});
			e.mouseout(function() {
				FormsHints.hideHint(e.attr("id"));
			});
		});
	}, 
	showHint: function(id) {
		$("#" + id).addClass('hintActive');
	}, 
	hideHint: function(id) {
		$("#" + id).removeClass('hintActive');
	}
}

var Previews = {
	init: function() {
		$("table.oEdition tbody tr").each(function() {
			var e = $(this);
			e.mouseover(function() {
				Previews.showPreview(e.attr("id") + '-Preview');
				e.addClass('highlight');
			});
			e.mouseout(function() {
				Previews.hidePreview(e.attr("id") + '-Preview');
				e.removeClass('highlight');
			});
		});
		/*
		$("p.preview").each(function() {
			var e = $(this);
			e.mouseover(function() {
				Previews.showPreview(e.attr("id"));
			});
			e.mouseout(function() {
				Previews.hidePreview(e.attr("id"));
			});
		});
		*/
	}, 
	showPreview: function(id) {
		$("#" + id).addClass('previewActive');
	}, 
	hidePreview: function(id) {
		$("#" + id).removeClass('previewActive');
	}
}



var CheckForm = {
	init: function() {
			$('input').each(function() {
				var e = $(this);
				e.blur(function() {
					if ( (e.is('.required'))&&($.trim(e.val()).length==0)&&(e.data('set')!=1)) {
						e.after('<div class="error">Please fill out this field which is required.</div>');
						e.addClass('error');
						e.data('set', 1);
					}
				});
				$(this).keyup(function() {
					if(e.data('set')==1) {
						if ( (e.is('.required'))&&($.trim(e.val()).length!=0)) {
							e.removeClass('error');
							e.next('div.error').remove();
							e.data('set', 0)
						}
						
					}
				});				
			});
	}
	
	
}


