// Send to Jeweler file

function SendToJeweler(map_div, form, error, info, more, prev, map_dia, query, cust_input) {
	StoreLocator.call(this, map_div, form, error, info, more, prev, map_dia);
	
	this.retailer_ids = new Array();
	this.query = query;
	this.cust_input = cust_input;
	this.retailer_input;
};

SendToJeweler.prototype._proto_ = StoreLocator.prototype;



// Override functions

SendToJeweler.prototype.init = function(jquery_rform, retailer_input) {
	this.retailer_input = retailer_input;
	StoreLocator.prototype.init.apply(this, arguments);
}


SendToJeweler.prototype.formCheck = function() {
	StoreLocator.prototype.formCheck.apply(this, arguments);
}


SendToJeweler.prototype.mapPan = function(evt, lat, lng) {
	StoreLocator.prototype.mapPan.apply(this, arguments);
}


SendToJeweler.prototype.initSearch = function() {
	StoreLocator.prototype.initSearch.apply(this, arguments);
}


SendToJeweler.prototype.geocode = function(callback) {
	StoreLocator.prototype.geocode.apply(this, arguments);
}


SendToJeweler.prototype.searchDb = function(latlng) {
	var obj = this;
	
	jQuery.ajax({
		url: obj.response_url,
		type: "POST",
		dataType: "json",
		data: ({
			'page' : obj.current_page,
			'lat' : latlng.lat(),
			'lng' : latlng.lng(),
			'country' : obj.country,
			'citystatezip' : obj.citystatezip,
			'query' : obj.query
		}),
		beforeSend: function() {
			obj.disableClicking(obj.prev_bttn);
			obj.disableClicking(obj.more_bttn);
		},
		error: function() {
			obj.renewClicking(obj.prev_bttn);
			obj.renewClicking(obj.more_bttn);
		},
		success: function(t) {
			obj.dbResult(t);
		},
		complete: function() {
			obj.renewClicking(obj.prev_bttn);
			obj.renewClicking(obj.more_bttn);
		}
	});
};


SendToJeweler.prototype.dbResult = function(t) {
	StoreLocator.prototype.dbResult.apply(this, arguments);
}


SendToJeweler.prototype.makeMap = function() {
	StoreLocator.prototype.makeMap.apply(this, arguments);
}


SendToJeweler.prototype.imageCurtain = function() {
	StoreLocator.prototype.imageCurtain.apply(this, arguments);
}


SendToJeweler.prototype.emptyResults = function() {
	StoreLocator.prototype.emptyResults.apply(this, arguments);
}


SendToJeweler.prototype.addMarker = function(lat, lng, num) {
	StoreLocator.prototype.addMarker.apply(this, arguments);
}  


SendToJeweler.prototype.formatStoreOutput = function(point, num) {
	var store_div = new Element('div').addClassName('shop');
	
	store_div.appendChild(new Element('h3').update(point['title']));
	store_div.appendChild(new Element('p').update(point['addr'] + "<br />" + point['city'] + " " + point['state'] + " " + point['zip'] + "<br />T: " + point['phone']));
	//store_div.appendChild(new Element('a').writeAttribute('href', '#').writeAttribute('id', 'gmap_zoom_' + num).writeAttribute('onclick', 'return false').update('Select this Jeweler'));
	store_div.appendChild(new Element('span').writeAttribute('id', 'gmap_zoom_' + num).update('Select this Jeweler'));
	store_div.appendChild(new Element('input').addClassName('retailer_input').writeAttribute('type', 'radio').writeAttribute('name', 'retailer_string').writeAttribute('value', point['id']));
	
	return store_div;
};


SendToJeweler.prototype.displayResults = function() {
	var obj = this;
	this.emptyResults();
	
	if(typeof this.more != 'undefined' && typeof this.prev != 'undefined') {
		this.displayMorePrev();
	}
	
	if(typeof this.points[0] != 'undefined') {
		this.bounds = new google.maps.LatLngBounds();
		
		for(var i=0,len=this.points.length; i<len; i++) {
			var j = i+1;
			var point = this.points[i];
			this.addMarker(point.lat, point.lng, j);
			
			if(typeof point["title"] != 'undefined') {
				var loc_div = new Element('div').update(this.formatStoreOutput(point,j));
				this.retailer_info.insert({ 'bottom' : loc_div });
				$('gmap_zoom_'+j).observe('click', obj.mapPan.bindAsEventListener(this, point.lat, point.lng));
			}
		}
		
		if(this.points.length > 1) {
			this.map.fitBounds(this.bounds);
		} else {
			this.map.setCenter(new google.maps.LatLng(this.points[0].lat, this.points[0].lng));
			this.map.setZoom(15);
		}
		
		//this.addChecks();
		
		if(this.cust_input===true) {
			jQuery('#cust_submit, #cust_input').show();
		}
	} else {
		var loc_div = new Element('div').update("There are no retailers near you within "+this.map_dia+" miles.");
		this.retailer_info.insert({ 'bottom' : loc_div });
		if(this.cust_input===true) {
			jQuery('#cust_submit, #cust_input').hide();
		}
	}
};


SendToJeweler.prototype.displayMorePrev = function() {
	StoreLocator.prototype.displayMorePrev.apply(this, arguments);
};


SendToJeweler.prototype.disableClicking = function(el) {
	StoreLocator.prototype.disableClicking.apply(this, arguments);
};


SendToJeweler.prototype.renewClicking = function(el) {
	StoreLocator.prototype.renewClicking.apply(this, arguments);
};


SendToJeweler.prototype.emptyVars = function() {
	StoreLocator.prototype.emptyVars.apply(this, arguments);
};


// Unique functions

SendToJeweler.prototype.initLightboxes = function(el, id_string, height, width, radios) {
	var obj = this;
	var form_el = jQuery(id_string).find('form.send_to_jeweler_form');
	
	el.colorbox({
		innerHeight: height,
		innerWidth: width,
		inline:true,
		opacity:0.3,
		href: id_string
	}).bind('cbox_complete', function(){
		obj.validateBind(form_el, radios);
	}).bind('cbox_closed', function(){
		form_el.unbind('submit');
	});
};

SendToJeweler.prototype.validateBind = function(el, radios) {
	el.submit(function(){
		var email_reg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		if(el.find('input#cust_name').length > 0 && el.find('input#cust_email').length > 0) {
			var cust_name = el.find('input#cust_name').attr('value'),
				cust_email = el.find('input#cust_email').attr('value');
			
			if(cust_name == '' || cust_email == '') {
				alert('Please enter a name and email.');
				return false;
			}
			
			if(!email_reg.test(cust_email)) {
				alert('Please enter a correct email.');
				return false;
			}
		}
		
		if(radios) {
			if (!el.find("input.retailer_input:checked").val()) {
				alert('You have not selected a retailer.');
				return false;
			}
		}
	});
};


/*
SendToJeweler.prototype.addChecks = function() {
	var obj = this;
	
	jQuery("input.retailer_input").click(function(){
		var inp = jQuery(this);
		if(!inp.attr('checked')) {
			for(var i=0,len=obj.retailer_ids.length;i<len;i++) {
				if(obj.retailer_ids[i] == inp.attr('name')) {
					obj.retailer_ids.splice(i,1);
				}
			}
		} else {
			obj.retailer_ids.push(inp.attr('name'));
		}
	}).each(function() {
		var inp = jQuery(this);
		if(inp.attr('name') in oc(obj.retailer_ids)) {
			inp.attr('checked',true);
		}
	});
};
*/

