/*
 * Coffee engine js script for simple tasks that automates the processing in web development.
 *
 * @author:		Łukasz M. Borzęcki
 * @email:		lukasz.borzecki@gmail.com
 * @copyright: MIT Style License
 * @based on:	Moo-Tools 1.2
 */

function StrToBoolean(value) {
	return value == 'false' ? false : true;
}

function DegToRad(value) {
	return value * (Math.PI / 180);
}

var CoffeePageControl = new Class( {

	Implements: [ Options ],

	options: {
		adopter: [ ],
		pages: [ ],	/* Page name that are created */
		astyle: "", /* The greyed style of a href */
		hide: false,
		grey: false
	},

	initialize: function( obj, objp, pages, adopter, options ) {
		this.obj = $(obj);
		this.objp = $(objp);
		
		if (!this.obj || !this.objp)
			return false;

		this.pages = pages;
		this.adopter = adopter;
		
		this.setOptions(options);

		this.ul = new Element('ol');
		this.obj.adopt(this.ul);
		
		this.count = 0;
		this.selected = -1;

		this.liclasses = []; /* Container for a elements */
		this.diclasses = []; /* Container for divs */
		
		if (this.obj)
			this.initpages();
	},
	
	adopter: [],
	
	/* @desc: Creates all necessary elements
	*/
	initpages: function() {
		var ulElement = this.ul;
		var duElement = this.objp;
		var liClasses = this.liclasses;
		var diClasses = this.diclasses;
		var hideSelf = this.options.hide;
		var adopter = this.adopter;
				
		this.pages.each(
			function(el, pos)
			{
				var liElement = new Element('li');
				var diElement;
				var aElement = new Element('a');

				if ($chk(adopter[pos]))
				{
					diElement = $(adopter[pos]);
				}
				else
					diElement = new Element('div').appendText('');

				diElement.setProperties( { 
					'vis' : false
				} );

				aElement.setProperties(
				{
					'href': '#',
					'html': el,
					'index': pos,
					'id': 'tab-links-' + (pos + 1)
				}
				);

				aElement.addEvent('click', function()
				{
					var href = this;
					var indexClicked = href.getProperty('index');

					diClasses.each(function(di, pos)
					{
		
						if (pos != indexClicked) {
							di.fade(0);
							new Fx.Slide(di).hide();
						} else
						{
							if (!hideSelf) {
								di.fade(1);
								new Fx.Slide(di).show();
							} else
							{

								if (StrToBoolean(di.getProperty('vis'))) {
									di.fade(0);
									new Fx.Slide(di).hide();
								} else
								{
									di.fade(1);
									new Fx.Slide(di).show();

								}
							}
						}

						if (!hideSelf)
							di.setProperty('vis', pos != indexClicked);
						else
						if (pos != indexClicked)
							di.setProperty('vis', false);
						else
						{
							var bo = !StrToBoolean(di.getProperty('vis'));
							di.setProperty('vis', bo);
						}
					}
					);

				}
				);

				liElement.adopt(aElement);
				ulElement.adopt(liElement);
				duElement.adopt(diElement);

				new Fx.Slide(diElement).hide();

				liClasses.include(aElement);
				diClasses.include(diElement);
			}

			);

		if ($chk(adopter[0])) {
			new Fx.Slide($(adopter[0])).show();
			$(adopter[0]).fade(1);
		}

	},
	
	showPage: function(index) {
		var ulElement = this.ul;
		var duElement = this.objp;
		var liClasses = this.liclasses;
		var diClasses = this.diclasses;
		var hideSelf = this.options.hide;
		var divClasses = this.options.adopt;
		
		this.pages.each(
			function(el, pos)
			{
				if (pos == index)
				{
					
				}
				else
				{
					
			}
			
			}
			);
	},

	getPageContainer: function(index) {
		if ($chk(this.diclasses))
			return this.diclasses[index];
		else
			return null;
	}
});

var CoffeeSin = new Class({
	Implements: [ Options ],

	options: {
		imgdir: "",
		links: [],
		linkstags: [],
		linkshtml: [],
		linksdiv: [ ]	/* Page name that are created */
	},
	
	transitions: [],

	initialize: function( divContainer, options ) {
		this.divContainer = $(divContainer);
		
		this.setOptions(options);

	},

	addevents: function() {
		var instance = this;
		var options = this.options;

		options.linksdiv.each ( function( item, pos ) {

			item.addEvents( {
				mouseenter: function(event) {
					//options.linksdiv.each( function(item, pos) {
					//	item.setProperty('ishover', 'false');
					//});
					this.setProperty('ishover', 'true');
					this.setProperty('changed', 'true');

					instance.stopanim();
		//			instance.startanim(this);
					instance.updatepositions();

					options.linksdiv.each( function(xitem, xpos) {
							if (xpos == pos) {
								instance.centerdiv.set({
									html: options.linkshtml[xpos] + "<br /><a href=" + options.linkstags[xpos] + ">Otworz</a>"
								})
							}

					});
					
				},
				mouseleave: function(event) {

					this.setProperty('ishover', 'false');
					this.setProperty('changed', 'true');
					
					instance.stopanim();
	//				instance.startanim(this);
					instance.updatepositions();


				}
			});
		});

	},
	
	removeevents: function() {
		var options = this.options;
		options.linksdiv.each ( function( item, pos ) {
			item.removeEvents();
		});
	},
	
	createdivs: function() {
		var container = this.divContainer;
		var options = this.options;
		var instance = this;
		
		this.options.links.each(function(item, pos){
			var newDiv = new Element('div');
			var newImg = new Element('img');

			if (newDiv) {
				newDiv.setProperty('ishover', 'false');
				newDiv.setProperty('changed', 'true');
				newDiv.setStyle('display', 'block');
				newImg.setProperty('src', options.imgdir + item);

				newDiv.adopt( newImg );

				container.adopt(newDiv);
				options.linksdiv.include(newDiv);
				instance.startanim(newDiv);
			}

		});

		this.centerdiv = new Element('div');
		this.centerdiv.addClass('infobox');
	
		container.adopt(this.centerdiv);
		
		this.addevents();
		
	},

	startanim: function(element) {
		var morp = new Fx.Morph(element, {
			});
		this.transitions.include( morp );

	},

	stopanim: function() {
		this.transitions.each( function(morphine, pos) {
			morphine.cancel();
		});
		//this.transitions.empty();
	},
	
	updatepositions: function() {
		var left = 565;
		var top = 380;
		var radiusAway;
		var p = this.options.linksdiv.length;
		var x; var y;
		var rad = DegToRad(360 / p);
		var hovered = false;
		var instance = this;
		
		/* Check if some items are hovered */
		this.options.linksdiv.each( function(item, pos ) {
			if ( StrToBoolean(item.getProperty('ishover')) == true ) {
				if (!hovered)	hovered = true;
			}
		});

		this.options.linksdiv.each( function(item, pos ) {

			if ( StrToBoolean(item.getProperty('ishover')) == true ) {
				radiusAway = 250;

				item.fade(1);
			}
			else {				
				if (hovered) {
					item.fade(0.1);
					radiusAway = 340;
				}
				else {
					item.fade(0.2);
					radiusAway = 250;
				}
			}

			x = Math.cos(rad * pos) * radiusAway + left;
			y = Math.sin(rad * pos) * radiusAway + top;

			item.setProperty( 'leftNew', Math.round(x) );
			item.setProperty( 'topNew', Math.round(y) );

			
		});

		x = Math.cos(rad) + left - (50);
		y = Math.sin(rad) + top - (50);

		instance.centerdiv.setStyle('left', x + 'px');
		instance.centerdiv.setStyle('top', y + 'px');

		if (hovered) {
			instance.centerdiv.fade(1);
		}
		else {
			instance.centerdiv.fade(0.4);
		}
		
		instance.transitions.each( function(morphine, pos) {

			morphine.start({
				'left': 
					[instance.options.linksdiv[pos].getStyle('left'),
					instance.options.linksdiv[pos].getProperty('leftNew')],
				'top': 
					[instance.options.linksdiv[pos].getStyle('top'),
					instance.options.linksdiv[pos].getProperty('topNew')]
			});
		});
				
	}
});

function CoffeeInit() {

	return true;
}
