/*
---
script: RegioLike.js
description: adds a Like Funktion for elements with the class regio_like
copyright: Copyright (c) 2011 REGIOCAST
authors: [Marcel Domke]


lifetime: [daily, weekly, monthly, yearly, infinite]
text_sum:  $ is the placeholder for the number,

requires: 
  core:1.3.0: 
  - Array
browsers: FF,IE
...
*/

var RegioLike = new Class({
	
	Extends: Regio,

	client: null,

	options: {
		user_ip: null,
		file_prefix: 'Default',
		save_url: 'php/RegioLike_Save.php',
		state_url: 'php/RegioLike_State.php',
		text: {
			hasVoted: 'Du magst schon diesen Schnack'
		},
		lifetime: 'daily',
		text_sum: '($)',
		fade: {
			enabled: true,
			value: 0.5
		}
	},
	
	initialize: function(elements, options) {
		/**
		 * Merge global class specified options.
		 * The global data are set in the file: js/%client%/frontent.js
		 */
		if( $type(document.blankoweb_client_settings) !== false ) {
			if( $type(document.blankoweb_client_settings.RegioLike) !== false ) {
				this.options = $merge(this.options,document.blankoweb_client_settings.RegioLike);
			}
		}

		this.setOptions(options);
		this.LikeElements = elements;

		if( $type(elements) == 'array' && elements.length > 0 ){
			this.states = this._getStates();
			this._buildLikeContainer();
			this._bindMooTip();
		}
	},
	
	_buildLikeContainer: function () {

		this.LikeElements.each( function (elLike) {
			if( $chk(elLike) && $type(elLike.getAttribute('id')) !== false ) {
				var uniqueId = elLike.getAttribute('id');
				var RetButton = this._buildButtonLike(uniqueId);
				RetButton.inject(elLike);

				new Element('div', {
					'class': 'clear'
				}).inject(elLike);

			}
		}.bind(this));
		
	},
	
	_buildButtonLike: function ( uniqueId ) {
		
		
		
		if(! $chk(this.states[uniqueId])) {
			return new Element('div', {'class': 'empty'});
		}

		var state = this.states[uniqueId];
		
		var ButtonLike = new Element('div', {
			'class':'regio_like_button btn_like',
			'events': {
				click: function () {
					if( !ButtonLike.hasClass('active') ) {
						this._save(uniqueId, 'like');
						ButtonLike.addClass('active').fade('in');
						var SumButtonLike = ButtonLike.getElement('.sum');
						
						this.mooTips.attach('.regio_like_button.active');
						
						var Sum = SumButtonLike.getElement('span').get('html').toInt();
						SumButtonLike.set('html',this.options.text_sum.replace('$','<span>'+(Sum+1)+'</span>'));
					}
				}.bind(this),
				mouseenter: function () {
					if( !this.hasClass('active') ) {
						this.fade(1);
					} else {
						this.setStyle('cursor','default');
					}
				},
				mouseleave: function () {
					if( !ButtonLike.hasClass('active') ) {
						if( this.options.fade.enabled == true) ButtonLike.fade(this.options.fade.value);
					} else {
						ButtonLike.setStyle('cursor','default');
					}
				}.bind(this)
			}
		});

		if( this.options.fade.enabled == true) ButtonLike.fade(this.options.fade.value);
		
		if( state !== null ) {
			if( state['like'].hasVoted === true ) {
				ButtonLike.addClass('active').fade('in');
				
				this._bindMooTip();
				
			}
			var ButtonLikeState = new Element('div', {
				'html': this.options.text_sum.replace('$','<span>'+state.likes+'</span>'),
				'class': 'sum'
			}).inject(ButtonLike);
		}
		
		return ButtonLike;
	},
	
	_save: function (uniqueId,button_mode) {
		
		var SaveLike = new Request({
			'url': this.options.save_url,
			'async':true,
			'method': 'post',
			onComplete: function () {
				// send google analytics event
				gaLabel = this.options.file_prefix + ' - ' + uniqueId;
				_gaq.push(['_trackEvent', 'RegioLike', 'Send', gaLabel]);
			}
		}).send(
			'id=' + uniqueId +
			'&button=' + button_mode + 
			'&ip=' + this.options.user_ip +
			'&prefix=' + this.options.file_prefix +
			'&client=' + document.blankoweb_client +
			'&lifetime=' + this.options.lifetime
		);

	},
	
	_bindMooTip: function () {
		
		this.mooTips = new Tips('.regio_like_button.active',{
			fixed:false,
			title: function () {
				return this.options.text.hasVoted;
			}.bind(this),
			offset: {'x': 0, 'y': 20}
		});
		
	},
	
	_getStates: function () {
		
		var uniqueIds = [];
		
		this.LikeElements.each( function (elLike,idx) {
			
			if( $type(elLike.getProperty('id')) !== false ) {
				uniqueIds.push(elLike.getProperty('id'));
			}
		}.bind(this));
		
		var StateLike = new Request.JSON({
			'url': this.options.state_url,
			'async':false,
			'method': 'post',
			onComplete: function () {}
		}).send(
			'ids=' + uniqueIds.join('-|-') +
			'&ip=' + this.options.user_ip +
			'&prefix=' + this.options.file_prefix +
			'&client=' + document.blankoweb_client +
			'&lifetime=' + this.options.lifetime
		);
		
		if( $type(StateLike.response.json)) {
			return StateLike.response.json;
		}
		
		return null;
		
	}
	
});
