(function($)
{
	$(function()
	{
		$(".custom-select-obj").custom_select();
	});
	
})(jQuery);

var map = {};
var stores = [];
var markers = [];

function load() 
{
  map = new GMap2(document.getElementById("gmap"));
  map.setCenter(new GLatLng(45.508867,-73.55424), 3);
  
  for(var i in stores)
  {
	  addmarker(i);
  }
}


function add_store(data,id)
{
	stores[id] = {};
	stores[id].data = data;
	stores[id].data.id = id;
}

	
function addmarker(id) 
{
	//var newIcon = MapIconMaker.createMarkerIcon({width: 32, height: 32, primaryColor: "#5588ff"});
	var newIcon = get_icon();
	var a = stores[id];
	stores[id].glatlng = new GPoint(stores[id].data.lon,stores[id].data.lat);

	stores[id].marker = new GMarker(stores[id].glatlng,{icon:newIcon});
	stores[id].html = "<b style='font-size:12px;'>" + a.data["Retailer name"] + "</b><div style='font-size:11px;padding-top:3px;'>" + a.data["Address formatted"].nl2br()+'<br /><a href="http://maps.google.com/maps?f=d&source=s_q&hl=en&geocode=&q='+a.data["Address Google map"]+'&ie=UTF8&hq=&hnear='+a.data["Address Google map"]+'&z=16&iwloc=A" target="_blank">Get directions</a>';
	
	if(a.data['Website URL'])
	stores[id].html += '<br /><a href="'+a.data['Website URL']+'" target="_blank">View website</a>';
	stores[id].html += '</div>';
	
	if(a.data["Thumbnail (120x60). NO AUTO RESIZE"])
	{
		if(a.data['Website URL'])
			var img = '<a href="'+a.data['Website URL']+'" target="_blank"><img src="'+a.data["Thumbnail (120x60). NO AUTO RESIZE"]+'" /></a>';
		else
			var img = '<img src="'+a.data["Thumbnail (120x60). NO AUTO RESIZE"]+'" />';
			
		stores[id].html = '<table cellspadding=0 cellspacing=0 width=100%><tr><td width=130 valign=top>'+img+'</td><td valign=top>'+stores[id].html+'</td></tr></table>';
	}
	
	stores[id].marker.element = stores[id];
	
	markers[stores[id].data.id] = stores[id].marker;
	GEvent.addListener(stores[id].marker, 'click', function(a,b,c) 
	{
		openwindowinfo(this.element.data.id);
	});
	
	map.addOverlay(stores[id].marker);
}

function get_icon()
{
	var MyIcon = new GIcon(); // create a new Google Icon object
	MyIcon.image = "/assets/images/bruxe-ggmap.png";// image file name
	MyIcon.shadow = "/assets/images/bruxe-ggmap-shadow.png";// image file name
	MyIcon.iconSize = new GSize(32, 37);// width (px) and height (px)
	MyIcon.shadowSize = new GSize(20, 20);// the shadow's size
	MyIcon.iconAnchor = new GPoint(32, 37);// base of the icon
	MyIcon.infoWindowAnchor = new GPoint(20, 0);// info window base 	
	
	return MyIcon;
}

function openwindowinfo(id)
{
	var marker = markers[id];
	if(!marker) return;
	marker.openInfoWindowHtml(marker.element.html);
	jQuery(window).scrollTo(jQuery("#container"),500);
}




String.prototype.nl2br = function(is_xhtml)
{
	var str = this;
	var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
	return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
};

/*scrollto*/
(function(h){var m=h.scrollTo=function(b,c,g){h(window).scrollTo(b,c,g)};m.defaults={axis:'y',duration:1};m.window=function(b){return h(window).scrollable()};h.fn.scrollable=function(){return this.map(function(){var b=this.parentWindow||this.defaultView,c=this.nodeName=='#document'?b.frameElement||b:this,g=c.contentDocument||(c.contentWindow||c).document,i=c.setInterval;return c.nodeName=='IFRAME'||i&&h.browser.safari?g.body:i?g.documentElement:this})};h.fn.scrollTo=function(r,j,a){if(typeof j=='object'){a=j;j=0}if(typeof a=='function')a={onAfter:a};a=h.extend({},m.defaults,a);j=j||a.speed||a.duration;a.queue=a.queue&&a.axis.length>1;if(a.queue)j/=2;a.offset=n(a.offset);a.over=n(a.over);return this.scrollable().each(function(){var k=this,o=h(k),d=r,l,e={},p=o.is('html,body');switch(typeof d){case'number':case'string':if(/^([+-]=)?\d+(px)?$/.test(d)){d=n(d);break}d=h(d,this);case'object':if(d.is||d.style)l=(d=h(d)).offset()}h.each(a.axis.split(''),function(b,c){var g=c=='x'?'Left':'Top',i=g.toLowerCase(),f='scroll'+g,s=k[f],t=c=='x'?'Width':'Height',v=t.toLowerCase();if(l){e[f]=l[i]+(p?0:s-o.offset()[i]);if(a.margin){e[f]-=parseInt(d.css('margin'+g))||0;e[f]-=parseInt(d.css('border'+g+'Width'))||0}e[f]+=a.offset[i]||0;if(a.over[i])e[f]+=d[v]()*a.over[i]}else e[f]=d[i];if(/^\d+$/.test(e[f]))e[f]=e[f]<=0?0:Math.min(e[f],u(t));if(!b&&a.queue){if(s!=e[f])q(a.onAfterFirst);delete e[f]}});q(a.onAfter);function q(b){o.animate(e,j,a.easing,b&&function(){b.call(this,r,a)})};function u(b){var c='scroll'+b,g=k.ownerDocument;return p?Math.max(g.documentElement[c],g.body[c]):k[c]}}).end()};function n(b){return typeof b=='object'?b:{top:b,left:b}}})(jQuery);

function debug(obj)
{
	if(window.console && window.console.log) window.console.log(obj);	
}


(function($){$.fn.clickclear=function(){return this.each(function(){$(this).bind("click focus",$.fn.clickclear.focus).bind("blur",$.fn.clickclear.blur).trigger("blur");});};$.fn.clickclear.focus=function(){if(this.value==$(this).attr('default')){$(this).val("");$(this).css({color:'#000'});return;}};$.fn.clickclear.blur=function(){if(this.value==''){$(this).val($(this).attr('default'));$(this).css({color:'#000'});return;}};})(jQuery);
jQuery(function(){jQuery(".clickclear").clickclear().blur();});

(function($)
{
	$.fn.custom_select=function()
	{
		return this.each(function()
		{
			var self = $(this);
			var opts = self.find("option");
			var id	= Math.ceil(Math.random()*10000000);
			
			self.attr("id","select-"+id)
			.bind("change",function()
			{
				var id = $(this).attr("id");
				$("#c2-"+id).custom_select_value(self.find("option:selected"));
			});
			
			var c1 = $('<div class="custom-select-container" />').attr("id","c1-select-"+id).hide().appendTo("body");
			opts.each(function()
			{
				c1.append('<a href="" rel="'+$(this).attr("value")+'">'+$(this).html()+'</a>');
			});
			
			c1.find("a").bind("click",function(e)
			{
				e.preventDefault();
				var self = $(this);
				var parent = self.closest(".custom-select-container");
				var id = parent.attr("id").replace("c1-","");
				var opt = $("#"+id).trigger("focus");
				$("#c2-"+id).custom_select_value(opt.val(self.attr("rel")).find("option:selected"));
				$("#c1-"+id).hide();
				opt.trigger("change");
			});
			
			c1.hover(function(){},function(){$(this).hide();});
			
			var c2 = $('<div class="custom-select"><div class="custom-select-text" /><div class="custom-select-right" /></div>').attr("id","c2-select-"+id).insertAfter(self);
			c2.custom_select_value(opts.eq(0))
			.bind("click",function()
			{
				var self = $(this);
				var id = self.attr("id").replace("c2-","");
				var c1 = $("#c1-"+id);
				if(c1.is(":visible"))
					c1.hide();
				else
				{
					$(".custom-select-container").hide();
					$.custom_select_onresize();
					c1.show();
				}
			});
			
			$.custom_select_onresize();
		});
		
	};
	
	$.fn.custom_select_value = function(obj)
	{
		var id = $(this).attr("id").replace("c2-","");
		$(this).find(".custom-select-text").html(obj.html());
		$("#c1-"+id).find("a").removeClass("selected").end().find("a[rel='"+obj.attr("value")+"']").addClass("selected");
		return this;
	};
	
	$.custom_select_onresize = function()
	{
		$(".custom-select-container").each(function()
		{
				var self = $(this).show();
				var id = self.attr("id").replace("c1-","");
				
				var c = {w:self.width(),h:self.height()}; self.hide();
				var w = {w:$(window).width(),h:$(window).height()};
				
				var c2 = $("#c2-"+id);
				var o = c2.offset();
				var s = {w:c2.width(),h:c2.height()};
				
				if(w.w-o.left < c.w)
					self.css({right:w.w-(o.left+s.w)+"px",left:'auto'});
				else
					self.css({left:o.left+"px",right:'auto'});
				
				self.css({top:Math.floor(o.top)+Math.floor(s.h)+'px'});
		});
		
	};
	
	
	$(window).bind("resize",$.custom_select_onresize);
	
})(jQuery);

