function initialize() {
	if (GBrowserIsCompatible() && typeof merchants != 'undefined') {
		var map = new GMap2(document.getElementById("merchant_map"));
		map.setCenter(new GLatLng(merchants[0].lat,merchants[0].lng), 13);
		map.addControl(new GLargeMapControl());
		
		function createMarker(latlng, opts) {
			var marker = new LabeledMarker(latlng, opts);
			//var html = "<strong>"+merchant.name+"</strong>";
			// GEvent.addListener(marker,"click", function() {
				//map.openInfoWindowHtml(latlng, html);
			//});
			return marker;
		};
		
		var icon = new GIcon();
		icon.image = '../images/my_marker.png';
		icon.iconSize = new GSize(24, 32);
		icon.iconAnchor = new GPoint(8, 28);
		icon.infoWindowAnchor = new GPoint(25, 7);
		
		var bounds = new GLatLngBounds;
		for (var i = 0; i < merchants.length; i++) {
			var latlng = new GLatLng(merchants[i].lat, merchants[i].lng)
			bounds.extend(latlng);
			map.addOverlay(createMarker(latlng, opts = {"labelText":i+1, "labelOffset": new GSize(0, -28), "icon": icon}));
		};
		var center = bounds.getCenter();
		var zoom = map.getBoundsZoomLevel(bounds);
		map.setCenter(center,zoom);
	};
};
window.onload=initialize;

function changeCategory(element) {
	window.location.href = "/products" + "?category=" + $F(element)
}

function start_slideshow(start_frame, end_frame, delay) {
	setTimeout(switch_slides(start_frame,start_frame,end_frame, delay), delay);
}
                            
function switch_slides(frame, start_frame, end_frame, delay) {
   	return (function() {
	       Effect.Fade('slide_' + frame);
	       if (frame == end_frame) { frame = start_frame; } else { frame = frame + 1; }
	       setTimeout("Effect.Appear('slide_" + frame + "');", 950);
	       setTimeout(switch_slides(frame, start_frame, end_frame, delay), delay + 950);
	 });
};

document.observe("dom:loaded", function() {
	
	if ($("search")) {
		/$("search").writeAttribute({value:"search our products"});/
		$("search").setStyle({color:"#c1c1c1"});
		$("search").observe("focus", function() {
			$(this).clear();
			$(this).setStyle({color:"#000000"});
		});
	};
	
	//if ($("product_list")) {
	//	var category = $("product_list").readAttribute('class');
	//	if (category != "") {
	//		$("product_link_" + category).down().addClassName("current_category");
	//	};
	//};
	
	if($("search_postcode")) {
		$("search_postcode").writeAttribute({value:"postcode"});
		$("search_postcode").setStyle({color:"#a1a1a1"});
		$("search_postcode").observe("focus", function() {
			$(this).clear();
			$(this).setStyle({color:"#000000"});
		});
	};
	
	$$(".more_images").each(function(i) {
		i.observe("click", function() {
			src = $(this).readAttribute('src');
			newSrc = src.gsub('_thumb','');
			$('main_image').writeAttribute({src:newSrc});
		});
	});
	
	$$(".premier_detail").invoke('hide');
	
	$$(".premier_info").each(function(i) {
		i.observe("click", function() {
			$(this).up().next(3).appear();
		});
	});
	
	// Slideshow scripts
	
	if ($('slideshow_container')) {
		var height = 380;
		images = $('slideshow_container').childElements();
		images.each(function(i) {
			var dimensions = i.getDimensions;
			var imageHeight = dimensions.height;
			var imageWidth = dimensions.width;
			if (imageHeight > height) {
				var scale = height/imageHeight;
				var newHeight = imageHeight*scale;
				var newWidth = imageWidth*scale;
				i.setStyle({height:newHeight,width:newWidth});
			};
		});
	};
	
	if ($('slideshow_container')) {
		images = $('slideshow_container').childElements();
		first_image = images.first();
		new_images = images.without(first_image);
		new_images.each(function(i) {
			i.setStyle({display:"none"});
		});
		
		last_slide = images.size() - 1
		
		start_slideshow(0, last_slide, 5000)
	};
	
	newRegex = new RegExp("^http://www.intatec.co.uk/")
	referrer = document.referrer
	
	if (newRegex.test(referrer)) {
		return;
	} else {
		string = "<div id='phone-update'><p>"
		string += "Following our recent move to Hixon, out contact details have changed. "
		string += "Our new telephone number is <strong>01889 272180</strong>. "
		string += "You can find <a href='/about/'>our other contact information here</a>."
		string += "</p></div>"
		$('content').insert({before:string});
	};
	
})