// Change these
var rotatetime = 5000; // Time for each image to be displayed in milliseconds
var fadetime = 1000; // Time for cross-fade in milliseconds

// Don't touch these!
var rotateimgs = new Array();
var pos = 0;
var go;
var inprogress = 0;

function doslider(el) {
	var container = $(el);
	var useel = "A";
	for(var i = 0; i < container.childNodes.length; i++) {
		if(container.childNodes[i].nodeName == useel) {
			container.childNodes[i].id = "fadeimg_" + i;
			container.childNodes[i].firstChild.id = "fadeblock_" + i;
			container.childNodes[i].firstChild.childNodes[0].id = "fadehead_" + i;
			container.childNodes[i].firstChild.childNodes[1].id = "fadetext_" + i;
			$('fadeimg_' + i).style.display = "none";
			rotateimgs[rotateimgs.length] = container.childNodes[i];
			if(rotateimgs.length == 1) container.childNodes[i].style.display = "block";
			$('fadeimg_' + i).addEvent('mouseenter', function(ev) {
				doslide(ev, 'up');
			});
			$('fadeimg_' + i).addEvent('mouseleave', function(ev) {
				doslide(ev, 'down');
			});
		}
	}
	if(rotateimgs.length > 1) go = setInterval(rotateimages, rotatetime + fadetime);
}

var mySlider;
var sizes = new Array();
function doslide(ev, dir) {
	var mainbox = ev.target.id;
	if(mainbox.indexOf("_") != -1) {
		var mainbits = mainbox.split("_");
		var useel = $('fadetext_' + mainbits[1]);
		mySlider = new Fx.Tween(useel, {
			duration: 500,
			link: 'cancel'
		});
		if(dir == 'up') {
			var id = mainbits[1];
			if(!sizes[id]) {
				useel.style.display = "block";
				sizes[mainbits[1]] = useel.getSize();
				useel.style.display = "none";
			}
			mySlider.set('height', 0);
			useel.style.display = "block";
			mySlider.start('height', sizes[id].y);
		}
		else mySlider.start('height', 0).chain(function() {
			useel.style.display = "none";
		});
	}
}

function doelslide(el, dir) {
	if($(el)) {
		var mySlide = new Fx.Tween($(el), {
			duration: 500,
			link: 'cancel'
		});
		var useel = $(el);
		if(dir == "in") {
			useel.style.display = "block";
			var useheight = useel.getSize();
			useel.style.display = "none";
			mySlide.set('height', 0);
			useel.style.display = "block";
			mySlide.start('height', useheight.y);
		}
		else {
			mySlide.start('height', 0).chain(function() {
				useel.style.display = "none";
			});
		}
	}
}
function doelhide(el) {
	if($(el)) $(el).style.display = "none";
}

function searchbox(mode) {
	if(mode == 1) { if($('search').value == $('defsearch').value) $('search').value = ""; }
	else { if($('search').value == "") $('search').value = $('defsearch').value; }
}
function locbox(mode) {
	if(mode == 1) { if($('location').value == $('defloc').value) $('location').value = ""; }
	else { if($('location').value == "") $('location').value = $('defloc').value; }
}
function namebox(mode) {
	if(mode == 1) { if($('name').value == $('defname').value) $('name').value = ""; }
	else { if($('name').value == "") $('name').value = $('defname').value; }
}

function hidethreads() {
	for(var i = 20; i <= totthreads; i++) {
		run = 1;
		$('row' + i).style.display = "none";
	}
	if(run == 1) $('morethreads').style.display = "table-row";
}

function loadthreads() {
	for(var i = shownthreads; i < shownthreads + 20; i++) {
		if($('row' + i)) {
			$('row' + i).style.display = "table-row";
			shownthreads++;
		}
	}
	if(shownthreads >= totthreads) $('morethreads').style.display = "none";
}

function forumfocus() {
	if($('threadtitle')) $('threadtitle').focus();
	else if($('threadmessage')) $('threadmessage').focus();
}

function dores() {
	$('fl_loading').style.display = "block";
	$('resourceadd').value = "Please wait ...";
	$('resourceadd').disabled = true;
	
}

function choosedate(chosen) {
	chosen = chosen.split("-");
	chosen[1] = chosen[1] * 1;
	chosen[2] = chosen[2] * 1;
	$('emailmonth').value = chosen[1];
	$('emailday').value = chosen[2];
	$('emailyear').value = chosen[0];
	var myFx = new Fx.Scroll(window).toElement('contactform');
}

function domonth(month) {
	for(var i = 1; i <= 31; i++) {
		if(i < 10) i = "0" + i;
		if($('avail_' + month + '-' + i)) {
			$('avail_' + month + '-' + i).checked = true;
		}
	}
}

/* map stuff */

var map;
var infowindow;
var maploaded = 0;
var mapzoom = 0;
var pointsarray = [];
var areasarray = [];
var shownlayers = [];
var infowindows = [];

function closeinfo() {
	for (var i = 0; i < pointsarray.length; i++) {
		infowindows[i].close(map,pointsarray[i]);
	}
}

var inited = 0;
function initialize() {
	if($('map_canvas')) {
		showbox('map', 'list');
		if(inited != 1) {
			inited = 1;
			var latlng = new google.maps.LatLng(57,-4);
			var myOptions = {
				zoom: 6,
				center: latlng,
				mapTypeId: google.maps.MapTypeId.HYBRID
			};
			map = new google.maps.Map($('map_canvas'), myOptions);
			google.maps.event.addListener(map, 'tilesloaded', function() { maploaded = 1; });
			google.maps.event.addListener(map, 'click', function() { closeinfo(); });
		}
	}
}

function dopoint(title, place, image, text, catid, isaddress) {
	if(isaddress == 1) {
		var geocoder = new google.maps.Geocoder();
		geocoder.geocode( { 'address': place}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				if(text != '') var infowindow = new google.maps.InfoWindow({ content: text, maxWidth: 350 });
				var marker = new google.maps.Marker({
					position: results[0].geometry.location,
					map: map,
					title: title,
					icon: image
				});
				marker.myCat = catid;
				pointsarray.push(marker);
				if(text != '') {
					infowindows.push(infowindow);
					google.maps.event.addListener(marker, 'click', function() { closeinfo(); infowindow.open(map,marker); });
				}
			}
		});
	
	}
	else {
		var infowindow = new google.maps.InfoWindow({ content: text, maxWidth: 350 });
		place = place.split(",");
		var useplace = new google.maps.LatLng(place[0], place[1]);
		var marker = new google.maps.Marker({
			position: useplace,
			map: map,
			title: title,
			icon: image
		});
		marker.myCat = catid;
		pointsarray.push(marker);
		if(text != '') {
			infowindows.push(infowindow);
			google.maps.event.addListener(marker, 'click', function() { closeinfo(); infowindow.open(map,marker); });
		}
	}
}

function dopolypoint(title, place, image, text, catid) {
	var infowindow = new google.maps.InfoWindow({ content: text, maxWidth: 350 });
	var marker = new google.maps.Marker({
		position: place,
		map: map,
		title: title,
		icon: image
	});
	marker.myCat = catid;
	pointsarray.push(marker);
	infowindows.push(infowindow);
	google.maps.event.addListener(marker, 'click', function() { closeinfo(); infowindow.open(map,marker); });
}

function dopolygon(points, title, image, text, catid) {
	var polygon = new google.maps.Polygon({
		paths: points,
		strokeColor: "#7ac142",
		strokeOpacity: 0.8,
		strokeWeight: 2,
		fillColor: "#7ac142",
		fillOpacity: 0.35
	});
	polygon.myCat = catid;
	polygon.setMap(map);
	areasarray.push(polygon);
	var bounds = new google.maps.LatLngBounds();
	for (var i = 0; i < points.length; i++) {
		bounds.extend(points[i]);
	}
	dopolypoint(title, bounds.getCenter(), image, text, catid);
}

function dobounds() {
	var bounds = new google.maps.LatLngBounds();
	for(var i = 0; i < pointsarray.length; i++) {
		bounds.extend(pointsarray[i].position);
	}
	map.fitBounds(bounds);
	if(pointsarray.length == 1) map.setZoom(8);
	setTimeout(function() { mapzoom = 1; }, 500);

}

function checkbounds() {
	if(pointsarray.length == points) dobounds();
	else setTimeout(function() { checkbounds(); }, 100);
}

function checkmap() {
	if(maploaded == 1) {
		domap();
		checkbounds();
	}
	else setTimeout(function() { checkmap(); }, 100);
}


function showbox(opt, notopt) {
	document.cookie="mapview=" + opt;
	$(opt + 'box').style.display = "block";
	$(notopt + 'box').style.display = "none";
	$(opt + 'tab').className = "boxtab tabselect greenborder";
	$(notopt + 'tab').className = "boxtab greenborder";
}

var availtags;
var mode;
function checktag() {
	var curtags = $('tagbox').value.toLowerCase();
	curtags = curtags.replace(/ +(?= )/g,'');
	var lastchar = curtags.slice(-1);
	if(lastchar == " ") {
		$('suggested').style.display = "none";
	}
	else {
		if(curtags.indexOf(" ") == -1) var checktag = curtags;
		else {
			var tagsarr = curtags.split(" ");
			var usepos = tagsarr.length - 1;
			var checktag = tagsarr[usepos];
		}
		$('sugwords').innerHTML = "";
		var usetag, tagbits;
		for(var i = 0; i < availtags.length; i++) {
			usetag = availtags[i].toLowerCase();
			if(usetag.indexOf(checktag) != -1) {
				if(availtags[i].indexOf("--") != -1) {
					tagbits = availtags[i].split("--");
					$('sugwords').innerHTML += "<a href=\"#\" onclick=\"dotag('" + availtags[i] + "'); return false;\">" + tagbits[1] + "<\/a>";
				}
				else $('sugwords').innerHTML += "<a href=\"#\" onclick=\"dotag('" + availtags[i] + "'); return false;\">" + availtags[i] + "<\/a>";
			}
		}
		if($('sugwords').innerHTML == "") $('suggested').style.display = "none";
		else $('suggested').style.display = "block";
	}
}
function dotag(tag) {
	var curtags = $('tagbox').value;
	if(curtags.indexOf(" ") == -1 || mode == 1) {
		if(mode == 1) {
			var tagbits = tag.split("--");			
			$('tagid').value = tagbits[0];
			$('tagbox').value = tagbits[1];
		}
		else $('tagbox').value = tag + " ";
	}
	else {
		var tagsarr = curtags.split(" ");
		$('tagbox').value = "";
		for(var i = 0; i < tagsarr.length - 1; i++) {
			$('tagbox').value += tagsarr[i] + " ";
		}
		$('tagbox').value += tag + " ";
	}
	$('suggested').style.display = "none";
	if(mode != 1) $('tagbox').focus();
}

function checkchange(box) {
	var show;
	if(box == 'titleid' || box == 'titleid2') {
		if($(box).value == -1) show = "inline";
		else show = "none";
		$(box + 'other').style.display = show;
	}
	else if(box == 'showskills') {
		if($(box).checked == true) show = "block";
		else show = "none";
		$('skills').style.display = show;
	}
	else if(box == 'showwedding') {
		if($(box).checked == true) show = "block";
		else show = "none";
		$('wedding').style.display = show;
	}
}
function checkcouple() {
	var curitem = $('typeid').options[$('typeid').selectedIndex].text;
	curitem = curitem.toLowerCase();
	if(curitem.indexOf('couple') == -1) show = 'none';
	else show = 'block';
	$('couple').style.display = show;
}

function dotabs(curtab, init) {
	var i = 1;
	while($('tab_' + i)) {
		if(init == 1) $('tab_' + i).addEvent('click', function() { dotabs(this.id, 0); } );
		if($('tab_' + i).id == curtab) {
			$('tab_' + i).className = "tab curtab greenborder";
			$('box_' + i).className = "tabbox curbox greenborder";
		}
		else {
			$('tab_' + i).className = "tab notcurtab greenborder";
			$('box_' + i).className = "tabbox notcurbox greenborder";
		}
		i++;
	}
}

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}
window.addEvent('domready', function() {
	if($('imgblock')) doslider('imgblock');
	if($('couple')) checkcouple();
	if($('availability')) $('availability').style.display = "none";
	if($('availlink')) $('availlink').style.display = "block";
	if($('prevtags')) availtags = $('prevtags').innerHTML.split("|");
	if($('tab_1')) dotabs('tab_1', 1);
	if($('maptab')) {
		var mapset = getCookie("mapview");
		if(mapset != "list") initialize();
	}
});
