// From typo3conf/globalconf.php
var SEND_FRIEND_PID = 19;
var SEND_FRIEND_SMS_PID = 23;


/* this one talks to slave */
HMapMaster = new Class({
	Implements: [Options],

	initialize : function(options) {
		this.setOptions(options);
	},

	setSlave: function(slave) {
		this.slave = slave;
	},
	
	parseVouchers : function(vouchers_json) {
		var vouchers;
		var expr = 'vouchers = '+vouchers_json;
		eval(expr);				
		this.processVouchers(vouchers);
	},

	processVouchers: function(vouchers) {
		this.vouchers = vouchers;
	},

	getViewport: function() {
		return this.slave.getViewport();
	},
	
	onMapMoved: function() {
		this.updateMarkers();
	},
	
	updateMarkers: function() {
		if (! this.vouchers)
			return;

		var v = this.getViewport();
		var swLat = v.southwest.lat;
		var swLng = v.southwest.lng;
		var neLat = v.northeast.lat;
		var neLng = v.northeast.lng;
		
		var visibleVouchers = [];
		
		for (var i = 0; i < this.vouchers.length; i++) {
			var v = this.vouchers[i];
			v.visible = false;
			var sign = (v.lat - swLat) * (v.lat - neLat);
			if (sign < 0) {
				sign = (v.lng - swLng) * (v.lng - neLng);
				if (sign < 0) {
					v.visible = true;
					visibleVouchers.push(v);
				}
			}	
		}
		this.visibleVouchers = visibleVouchers;
		
		// console.log(visibleCount);
		/*
		for (var i = 0; i < this.vouchers.length; i++) {
			var v = this.vouchers[i];
			this.showMarker(v, v.visible);
		}
		*/
		
		this.renderSideVouchers(1);
	},
	
	renderSideVouchers: function(page) {
		var container = $('side_vouchers');
		container.set('html', '');

		var vpp = this.slave.options.sideVouchersPerPage;
		var pageCount = Math.ceil(this.visibleVouchers.length / vpp);
		
		var startIndex = (page - 1) * vpp;
		var endIndex = startIndex + vpp;
		if (endIndex > this.visibleVouchers.length)
			endIndex = this.visibleVouchers.length;


		var _this = this;
		for (var i = startIndex; i < endIndex; i++) {
			var v = this.visibleVouchers[i];
		
			var box = new Element('div').addClass('voucher-box').set('id', 'voucher-box-'+ v.uid);
			if (v.m) {
				(function(v, box){
					box.addEvent('mouseover', function(){
						_this.highlightMarker(v.m, true);
					});
					box.addEvent('mouseout', function(){
						_this.highlightMarker(v.m, false);
					});
				})(v, box);
			}

			var h = [];
			h.push('<b><a href="'+v.url+'">'+ v.title + '</a></b><br />');
			h.push('<div class="functions">');
			h.push('<a href="'+v.url+'"><img src="/fileadmin/templates/i/icon-details.gif" alt="Mehr Info" /></a>');
			h.push('<a href="'+v.url+'"><img src="/fileadmin/templates/i/icon-delete.gif" alt="Einlösen" /></a>');
			// TODO: send

			var fullUrl = 'http://'+ location.host + v.url;
			send_url = '/index.php?id='+SEND_FRIEND_PID + '&tipUrl='+ encodeURIComponent(fullUrl);
			send_url_sms = '/index.php?id='+SEND_FRIEND_SMS_PID + '&tipUrl='+ encodeURIComponent(fullUrl);

			h.push('<img src="/fileadmin/templates/i/icon-send.gif" onload="fixPNG(this)" onmouseover="showTip(\'hb'+ v.uid +'\');" onmouseout="hideTip(\'hb'+ v.uid +'\', event);" />');
			
			h.push('<div onmouseout="hideTip(\'hb'+v.uid+'\', event);" onmouseover="showTip(\'hb'+v.uid+'\');" id="hb'+v.uid+'" class="hovertip">');
			h.push('  <img src="/fileadmin/templates/i/v/action-email.gif" alt="Senden als E-Mail" />');
			h.push('  <a class="send_friend_link" href="'+ send_url +'" rel="shadowbox;width=720;height=460 nofollow" onclick="return false">Senden als E-Mail</a><br />');
			h.push('  <img src="/fileadmin/templates/i/v/action-sms.gif" alt="Senden als SMS" />');
			h.push('  <a class="send_friend_link" href="'+ send_url_sms +'" rel="shadowbox;width=720;height=460 nofollow" onclick="return false">Senden als SMS</a>');
			h.push('</div>');


			h.push('<a onmouseover="return addthis_open(this, \'\', \''+ v.url +'\', \''+ v.title + '\')" onmouseout="addthis_close()" href="http://addthis.com/bookmark.php?v=250&amp;username=xa-4bf1c95a2a99249c"><img src="/fileadmin/templates/i/icon-share.gif" /></a>');
			// h.push('<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4bf1c95a2a99249c"></script>');
			
			h.push('<a href="'+v.url+'"><img src="/fileadmin/templates/i/icon-checkin.gif" alt="Checkin" /></a>');
			
			h = h.join('\n');
			box.innerHTML = h;
			container.adopt(box);
		}
		var links = $$('a[class=send_friend_link]');
		if (links.length)
			Shadowbox.setup(links, {});
		
		if (pageCount > 1) {
			// rendering pager
			var p = new Element('div').addClass('small-pager');
			
			// browse to the left
			if (page <= 1) {
				var span = new Element('span').addClass("pager-inactive");
				span.set('html', ' &laquo; ');
				p.adopt(span);
			} else {
				var a = new Element('a').addClass('ajax-link');
				a.set('html', ' &laquo; ');
				a.set('href', '#');
				a.addEvent('click', function(ev){
					ev.stop();
					_this.renderSideVouchers(page - 1);
				});
				p.adopt(a);
			}
			
			var xOFy = new Element('span').addClass("page-x-of-y");
			xOFy.set('text', 'Seite '+ page +' von ' + pageCount);
			p.adopt(xOFy);
			
			// browse to the right
			if (page >= pageCount) {
				var span = new Element('span').addClass("pager-inactive");
				span.set('html', ' &raquo; ');
				p.adopt(span);
			} else {
				var a = new Element('a').addClass('ajax-link');
				a.set('html', ' &raquo; ');
				a.set('href', '#');
				a.addEvent('click', function(ev){
					ev.stop();
					_this.renderSideVouchers(page + 1);
				});
				p.adopt(a);
			}
			container.adopt(new Element('br'));
			container.adopt(new Element('br'));
			container.adopt(p);
		}
	}
});


