window.isTest = true;
jQuery(function() {
	if (window.isTest) jQuery('#checkboxes').hide();
	jQuery('#search a').hover(function() {
		jQuery(this).find('.hover').stop(false,true).fadeIn(300);
	},function() {
		jQuery(this).find('.hover').stop(false,true).fadeOut(300);
	});
	jQuery('#performsearch').click(function() { 

		mapIt();
		return false;	
	});
});

if (!Array.prototype.forEach) {
  Array.prototype.forEach = function(fun /*, thisp*/) {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}


var clickHandler;
var map = null;
var lat;
var lng;
var locations;
var bounds;
var clientHome;
var clientMarker;

var directions = null;
function mapIt() {
	getLatLngByZip(jQuery('#codepostal').val().replace(/[^A-Z0-9]/i,''));
}

function getLatLngByZip(zipCode) {
	var gc = new GClientGeocoder();
	if (zipCode.match(/^[a-z]{1}[0-9]{1}/i)) zipCode += ', Canada';
	gc.getLatLng(zipCode,function(latlng) {
		if (latlng==null) return alert(_invalidzip);
		clientHome = latlng;
		if (map == null) {
			
			map = new GMap2(document.getElementById("map"));
			
			map.addControl(new GLargeMapControl());
			
			map.addControl(new GMapTypeControl());
		}
	    else map.clearOverlays();
	  	map.setCenter(clientHome, 13);
	  	clientMarker = new GMarker(clientHome,{ icon:new GIcon(G_DEFAULT_ICON,"http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/blank.png") });
	  	map.addOverlay(clientMarker);
    	jQuery.get('/stores.php?lat='+clientHome.lat()+'&lng='+clientHome.lng()+'&zip='+jQuery('#codepostal').val().replace(/[^A-Z0-9]/i,'')+'&'+jQuery('form:first').serialize(), processLocations );

	});
}

function getStoreUrl(element) {
	if (element.url) return element.url;
	var url = 'http://canadel.udesign.ws/?lang='+_lan+'&collections=';
	var collections = new Array();
	//if (element.canadel==1) collections.push('CND');
	//if (element.colorshop==1) locations[index].html+=' Colorshop,';
	if (element.symphony==1)  collections.push('SYM');
	if (element.islandharbor==1) collections.push('IHA');
	if (element.highstyle==1) collections.push('HST');
	if (element.champlain==1) collections.push('CHD');
	if (element.bellagio==1) collections.push('BEL');
	if (element.sbc==1) collections.push('SBC');
	if (element.carteblanche==1 && element.alacarte==1) collections.push('CND');
	else if (element.alacarte==1) collections.push('ALC');
	else collections.push('CBL');
	return url + collections.join('+');
}
function processLocations(content) {
		try {
    	eval("locations = "+content+';');
    }
    catch(ee) {
    	alert(_internalerror);
    }
    

    if (locations.length == 0) {
    	alert(_noretailers);
    	 map.addOverlay(clientMarker);
    	 map.panTo(clientHome);
    	return;
    }
    jQuery('#results').html('').show(10);
    locations.forEach(function(element, index, array) {
    	locations[index].latlng = new GLatLng(element.latt, element.long);
        locations[index].marker = new GMarker(locations[index].latlng,{ icon:new GIcon(G_DEFAULT_ICON,"http://gmaps-samples.googlecode.com/svn/trunk/markers/red/marker"+(index+1)+".png") });
        if (window.isTest) locations[index].html = '<a href="'+getStoreUrl(element)+'" target="_blank">'+element.nom+'</a>';
		else locations[index].html = '<a href="#" onclick="moveMapTo('+index+');return false;">'+element.nom+'</a>';
		//if (window.isTest) locations[index].html += '<img style="position:absolute;top:5px;right:5px;cursor:pointer;" src="/images/compas.png" onclick="moveMapTo('+index+')" />';
        locations[index].html += '<br />'+((locations[index].validated=='1') ? element.address : element.address2)+'<br />'+element.city+', '+element.state;
        locations[index].html += '<br />'+_offers+':<span class="collections">';
		if (window.isTest) {
			/*if (element.canadel==1) locations[index].html+=' Canadel,';
			if (element.colorshop==1) locations[index].html+=' Colorshop,';*/
			//if (element.alacarte==1 && element.carteblanche==1) locations[index].html+=' Custom Dining Collections,';
			if (element.alacarte==1) locations[index].html+=' A La Carte,';
			if (element.carteblanche==1) locations[index].html+=' Carte Blanche,';
			if (element.symphony==1) locations[index].html+=' Symphony,';
			if (element.islandharbor==1) locations[index].html+=' Island Harbor,';
			if (element.highstyle==1) locations[index].html+=' High Style,';
			//if (element.kincaid==1) locations[index].html+=' Kincaid,';
			
			if (element.champlain==1) locations[index].html+=' Champlain,';
			if (element.bellagio==1) locations[index].html+=' Bellagio,';
			if (element.sbc==1) locations[index].html+=' Bar Stools,';
		}
		else {
			if (element.canadel==1) locations[index].html+=' Canadel,';
			if (element.colorshop==1) locations[index].html+=' Colorshop,';
			
			if (element.symphony==1) locations[index].html+=' Symphony,';
			if (element.islandharbor==1) locations[index].html+=' Island Harbor,';
			if (element.highstyle==1) locations[index].html+=' High Style,';
			//if (element.kincaid==1) locations[index].html+=' Kincaid,';
			
			if (element.champlain==1) locations[index].html+=' Champlain,';
			if (element.bellagio==1) locations[index].html+=' Bellagio,';
			if (element.sbc==1) locations[index].html+=' Bar Stools,';
		}
		locations[index].html+='</span>';
        map.addOverlay(locations[index].marker);
        GEvent.addListener(locations[index].marker, 'click', function() {
            this.openInfoWindowHtml('<div style="width:175px;">'+locations[index].html+'</div>');       
        });
        if (window.isTest) jQuery('#results').append('<li style="background-image:url(/images/dealer/'+(index+1)+'.gif);position:relative;">'+locations[index].html+'<img style="position:absolute;top:5px;right:5px;cursor:pointer;" src="/images/compas.png" onclick="moveMapTo('+index+')" /></li>');
		else jQuery('#results').append('<li style="background-image:url(/images/dealer/'+(index+1)+'.gif);position:relative;">'+locations[index].html+'</li>');
		
        if (locations[index].validated=='1') locations[index].html += '<br /><a href="#" onclick="showDirections('+index+');return false;">'+_drivedir+'</a>';
        else locations[index].html += '<br /><i>Location is aprox.</i>';
   // }
    });
    zoomShowAll();
}

function moveMapTo(index) {
		try { 
			directions.clear();
			showLocations();
		} catch(E) { }
    map.panTo(new GLatLng(locations[index].latt, locations[index].long));
    element = locations[index];
    element.marker.openInfoWindowHtml(element.html);   
}

function showLocations() {
	 map.addOverlay(clientMarker);
	 locations.forEach(function(element, index, array) {
	 		map.addOverlay(locations[index].marker);		
	 });
}

function zoomShowAll() {
    bounds = new GLatLngBounds();
    map.setCenter(new GLatLng(0,0),0);
    
    locations.forEach(function(elemet, id, array) {
    //for(var i = 0;i <  locations.length;i++) {
	  // 	id = i;
	  //  	elemet = locations[id];

        bounds.extend(new GLatLng(locations[id].latt, locations[id].long));
    });
     bounds.extend(clientHome);
  	//}
    map.setZoom(map.getBoundsZoomLevel(bounds));
    var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
    var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
    map.setCenter(new GLatLng(clat,clng));
}

function showDirections(index) {
	jQuery('#route').show();
	
	//jQuery('#map').css('height','300px');
	if (directions==null) {
		directions = new GDirections(map,document.getElementById('route'));
	}
	else directions.clear();
	map.clearOverlays();
	directions.loadFromWaypoints(new Array(clientHome,locations[index].latlng),{ locale:_maplocal });
	//document.location.href='#bottom';
	jQuery.scrollTo('#route',1000);
	jQuery('#printdir').show();
} 
