
/**
 * popUpWin
 */
function popUpWin(url,name,w,h) {
	var x=(screen.width/2-w/2);
	var y=(screen.height/2-h/2);
	window.open(url,name,"toolbar=no,location=no,directories=no,status=no,menuvar=no,resizable=yes,scrollbars=yes,width="+w+",height="+h+",screenX="+x+",screenY="+y+",left="+x+",top="+y);
};

function replaceUrls(str) {
	var pattern = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
	return str.replace(pattern,'<a href="$1">$1</a>'); 
}

var periodicalScroller = new Class({
	Implements: [Options],
	
	options: {
		duration: 500,
		delay: 4000,
		transition: Fx.Transitions.Quad.easeInOut
	},
	
	initialize: function(el, options) {
		this.setOptions(options);
		this.el = $(el);
		this.items = this.el.getElements('.item');
		this.itemsCount = this.items.length;
		this.itemHeight = this.items[0].getSize().y;
		this.currentItem = 0;
		this.nextStep = 0;
		this.itemsVisible = Math.round(this.el.getSize().y/this.itemHeight);
		
		this.scroller = new Fx.Scroll(this.el, {
			wait: false,
			duration: this.options.duration,
			transition: this.options.transition
		});
		
		this.scrollToItem = (function() {
			//this.scroller.toElement(this.items[this.currentItem]); //this is buggy
			this.scroller.start(0, this.nextStep)
	        if(this.currentItem == this.itemsCount-this.itemsVisible) {
	    		// go back to top
	        	this.nextStep = 0;
	        	this.currentItem = 0;
	        } else {
	        	this.nextStep = this.nextStep+this.items[this.currentItem].getSize().y;
	        	// hack...
	        	if (this.currentItem%2) {
	        		this.nextStep--;
	        	}
		 		this.currentItem++;
	        }
		}).bind(this);

		this.launch = (function() {
			this.periodicalScroll = this.scrollToItem.periodical(this.options.delay);
		}).bind(this);
		
		this.el.addEvent('mouseover', function() {
			this.el.setStyle('overflow', 'auto');
			clearInterval(this.periodicalScroll);
		}.bind(this));
		
		this.el.addEvent('mouseout', function(){
			this.el.setStyle('overflow', 'hidden');
			this.launch();
		}.bind(this));

		this.launch();
	}

});

window.addEvent('domready',function(){
	
	// get current window size
	var winSize=window.getSize();
	var winHeight = winSize.y;
	var winWidth = winSize.x;
	
	// External links
	$$('a.ext').each(function(el){
		el.addEvent('click',function(e){
			e.stop();
			window.open('/bounce/'+this.get('href'));
		});
	});
	
	// Popup links
	$$('a.popup').each(function(el){
		el.addEvent('click',function(e){
			e.stop();
			popUpWin(this.get('href'));
		});
	});
	
	// Replace urls with a link
	$$('.replace-urls').each(function(el){
		el.set('html', replaceUrls(el.get('html')));
	});
	
	/**
	 * Inputs with disappearing default values
	 */
	$$('input.defaultvalue').each(function(el) {
		var setDefaultValue = function(event){
			el.set('value',el.title);
			el.removeClass('defaultvalue-changed');
		}
		var removeDefaultValue = function(event){
			if (el.retrieve('isChanged') != true) {
				el.set('value','');
				el.addClass('defaultvalue-changed');
				el.store('isChanged',true);
			}
		}
		if (el.value == '') {
			setDefaultValue();
		} else {
			el.store('isChanged',true);
			el.addClass('defaultvalue-changed');
		}
		el.addEvent('focus', removeDefaultValue);
		el.addEvent('blur', function(event){
			if (el.value == '') {
				setDefaultValue();
				el.eliminate('isChanged');
			}
		});
	});
	
	/**
	 * Disappearing values must be ignored if they aren't changed
	 */
	$$('form.defaultvalued').each(function(el) {
		el.addEvent('submit', function(event){
			els = this.getElements('input.defaultvalue');
			for (i=0; i < els.length; i++) {
				if ( els[i].retrieve('isChanged')!=true) {
					els[i].set('value','');
				}
			}
		});
	});
	
	// Small squeezeBox size
	SqueezeBox.assign($$('a.sbox-small'),{
		handler:'iframe',
		size:{
			x:(winWidth*(0.3)).toInt(),
			y:(winHeight*(0.25)).toInt()
		}
	});
	
	// Medium squeezeBox size
	SqueezeBox.assign($$('a.sbox-medium'),{
		handler:'iframe',
		size:{
			x:(winWidth*(0.55)).toInt(),
			y:(winHeight*(0.5)).toInt()
		}
	});
	
	// Large squeezeBox size	
	SqueezeBox.assign($$('a.sbox-large'),{
		handler:'iframe',
		size:{
			x:(winWidth*(0.8)).toInt(),
			y:(winHeight*(0.75)).toInt()
		}
	});
});
