﻿/**
 * @author tfuhlroth maxomedia - agentur für crossmedia-kommunikation bsw
 * @version 1.0 (12.01.2008)
 */

ERAW.map = {
	
	init: function (coordinates) {
		this.coordinates = coordinates;
		this.loadAPI();
	},
	
	loadAPI: function () {
		google.load('maps', '2', {
			callback: function () {
				this.initGoogleMaps();
			}.bind(this)
		});
	},
	
	initGoogleMaps: function () {
		this.map = new google.maps.Map2($('gmaps-container'));
		this.map.addControl(new google.maps.SmallMapControl());
		this.map.setCenter(new google.maps.LatLng(this.coordinates.lat, this.coordinates.lng), 10, G_PHYSICAL_MAP );
		
		this.baseIcon = new GIcon();
		this.baseIcon.image = '/_gfx/icon_map-pin.png';
		this.baseIcon.iconSize = new GSize(32, 34);
		this.baseIcon.iconAnchor = new GPoint(6, 29);
		this.baseIcon.infoWindowAnchor = new GPoint(14, 10);
		this.baseIcon.shadow = '/_gfx/icon_map-pin-shadow.png';
		this.baseIcon.shadowSize = new GSize(32, 34);
		
		this.setOfferPoint();
	},
	
	setOfferPoint: function () {
		var marker = new google.maps.Marker(new google.maps.LatLng(this.coordinates.lat, this.coordinates.lng), this.baseIcon);
		this.map.addOverlay(marker);
	}
	
};




ERAW.Rater = new Class({
	
	Implements: [Options, Events],
	
	options:  {
		delay: 200
	},
	
	initialize: function (button, originalRating, params) {
		this.button = $(button);
		this.originalRating = $(originalRating);
		this.originalRatingSquares = this.originalRating.getElement('.squares_red');
		this.originalRatingDisplay = this.originalRating.getElement('.rating_font');
		this.params = new Hash(params);
		
		this.actions = ['build', 'send'];
		this.button.addEvent('click', this.fireAction.bind(this));
		this.currentRating = 0;
	},
	
	fireAction: function () {
		if (this.actions.length) this[this.actions.shift()]();
	},
	
	build: function () {
		this.container = new Element('span', {'class': 'rating_red r_spacer'});
		this.squares = new Element('span', {'class': 'squares_red square-0', styles: {'text-indent': 0}}).inject(this.container);
		//this.fx = new Fx.Morph(this.squares, {unit: 'em', link: 'cancel', transition: 'sine:out'}).set('.squares_red.square-0');
		(10).times(function (index) {
			var anchor = new Element('a');
			if (index == 9) anchor.setStyle('padding-right', 0);
			var percent = (index + 1) * 10;
			anchor.addEvents({
				'mouseover': this.over.pass(percent, this),
				'mouseout': this.out.bind(this),
				'click': this.set.pass(percent, this)
			});
			anchor.inject(this.squares);
		}, this);
		this.display = new Element('span', {'class': 'rating_font'}).inject(this.container);
		this.container.inject(this.button, 'before');
		
		this.button.set('html', ERAW.dict.get('&raquo; Bewertung senden'));
	},
		
	over: function (percent) {
		//this.fx.start('.squares_red.square-' + percent / 10);
		this.squares.set('class', 'squares_red square-' + percent / 10);
		this.display.set('text', percent + '%');
	},
	
	out: function () {
		//this.fx.start('.squares_red.square-' + this.currentRating / 10);
		this.squares.set('class', 'squares_red square-' + this.currentRating / 10);
		this.display.set('text', this.currentRating + '%');
	},
	
	set: function (percent) {
		this.currentRating = percent;
	},
	
	send: function () {
		this.params.set('rating', this.currentRating);
		new Request({
			url: '/_service/OfferRatingSet.ashx',
			method: 'get',
			data: this.params.toQueryString(),
			onSuccess: function (response) {
				this.container.destroy();
				this.button.destroy();
				// set new rating value
				if (response.toInt() > 0) {
					var percent = Math.floor((response.toInt() / 10));
					this.originalRatingSquares.set('class', 'squares_red square-' + percent);
					this.originalRatingDisplay.set('text', response + '%');
				}
			}.bind(this)
		}).send();
	}
	
});