/* 
Kühlschrank-Poesie  
Author: Marc Hinse
www.siebennull.com
*/
window.addEvent('domready', function() {
var cooler = $('cooler');
var wortpositionen = Array();
$$('#worte div').each(function(div,v){
	var drag = div.getElement('span.drag');
	width = drag.getSize().x;
	newwidth = width;
	if(width > 150) newwidth = width - 48;
	if(width <= 150) newwidth = width - 37;
	if(width <= 130) newwidth = width - 34;
	if(width <= 110) newwidth = width - 31;
	if(width <= 90) newwidth = width - 21;
	if(width <= 80) newwidth = width - 17;
	if(width <= 70) newwidth = width - 14;
	if(width <= 60) newwidth = width - 11;
	if(width <= 50) newwidth = width - 4;
	if(width <= 40) newwidth = width - 1;
	if(width <= 30) newwidth = width;
	if(width <= 20) newwidth = width + 3;	
	if(width <= 10) newwidth = width + 7;	
	div.setStyles({
		'width': newwidth,
		'height': width.y + 8,
		'float': 'left'
	});	
	spanpos = drag.getPosition($(div));
	drag.setPosition(spanpos);
	//clearpos = drag.getPosition(cooler);
	//cooler.appendText('| ' + spanpos.x);
	wortpositionen[v] = spanpos;
})
var worte = $$('.drag');
worte.each(function(wort,i){
	var MyDrag = new Drag.Move(wort, {
	    snap: 0,
		container: $('container'),
		droppables: cooler,
		onDrop: function(element, droppable, event){
	        if (!droppable) {
				//console.log(element, ' dropped on nothing');
				var myRequest = new Request({method: 'post', url: '/1/kuehlschrank/store.php'});
				myRequest.send('remove=' + i);
			}
			else //console.log(element, 'dropped on', droppable, 'event', event);
			{
				var position = element.getPosition(cooler);
				var breite = element.getSize();
				wort = element.get('text');
				//console.log(element.getPosition($('cooler')));
				var myRequest = new Request({
					method: 'post',
					url: '/1/kuehlschrank/store.php'
				});
				myRequest.send('x=' + position.x + '&y=' + position.y + '&breite=' + breite.x + '&wort=' + wort + '&id=' + i);
			}
	    }
	});
})
$('clear').addEvent('click',function(e){
	e.stop();
	$$('.drag').each(function(toclear,w){
		//toclear.setPosition(wortpositionen[w]);
		var parent = toclear.getParent('div'); 
		if (toclear.getPosition(parent).x != 0){
		var morpherei = new Fx.Morph(toclear,{
    //first state the name of the option
    //place a :
    //then define your option
    duration: 1500,
    transition: 'expo:in:out'
	});
		morpherei.start.delay(w*3,morpherei,{
   			'left': wortpositionen[w].x,
    		'top': wortpositionen[w].y
		});
		}
	});
		var reinhauen = new Request({
		method: 'post', 
		url: '/1/kuehlschrank/store.php',
		onSuccess: function(responseText, responseXML){
			url=responseText;
			//$('cooler').appendText('cleared');
		}
		});
	reinhauen.send('remove=all');
})

$('submit').addEvent('click',function(e){
	pageTracker._trackPageview("/1/kuehlschrank/ausgabe.php");
	//console.log('google funktion');
});

SqueezeBox.assign($$('a[rel=boxed]'), {
		size: {x: 460, y: 500},
		ajaxOptions: {
			method: 'get' // we use GET for requesting plain HTML (you can skip it, it is the default value)
		}
	});

});
