Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

var URI;
var AJAX;
var client;

var doNotDrag = false;

var klydo = Class.create({
	
	twitters: Array(),
	
	initialize: function( uri ) {
	
		this.URI 	= uri;
		this.AJAX	= uri + '_ajax/handle.php';

		URI		= this.URI;
		AJAX	= this.AJAX;
		
		this.loadJs();
		this.initSelects();
		//this.includeEx( 'http://snapabug.appspot.com/snapabug.js', "SnapABug.addButton('07a7bd3e-d864-498a-be04-59e8fffb55f5','0','55%');" );
		
		document.observe( 'dom:loaded', function() {
		
			$( 'popup-holder' ).setStyle({
				position: 'fixed'
			});
			
			setTimeout( function() { this.getNotifications(); }.bind(this), 3000 );
		
		}.bind(this));
						
	},
	
	include: function( src ) {
		
		src = URI + '_public/_js/' + src + '.js';
		
		var head	= document.getElementsByTagName( 'head' )[ 0 ];
		var el		= new Element( 'script', { 'src': src, 'type': 'text/javascript' } );
		
		head.insert({bottom:el});
	
	},

	includeEx: function( src, onload ) {
		
		if( typeof onload == undefined ) {
		
			onload = 'function(){}';
		
		}
		
		var head	= document.getElementsByTagName( 'head' )[ 0 ];
		var el		= new Element( 'script', { 'src': src, 'onload': onload, 'type': 'text/javascript' } );
		
		head.insert({bottom:el});
	
	},	
	
	loadJs: function() {
	
		var js = Array( 'alerts', 'inalerts', 'lightbox', 'autogrow' );
		
		for( var i = 0; i < js.length; i++ ) {
		
			this.include( js[ i ] );
		
		}
		
		/**
		document.observe( 'dom:loaded', function() {
			
			var twits = Array();
			
			for( var i = 0; i < Klydo.twitters.length; i++ ) {
				
				twits[ twits.length ] = Klydo.twitters[ i ].userid;
			
			}
			
			var follow = twits.join( '&' );

			if( follow != '' ) {
								
				var head 	= document.getElementsByTagName( 'head' )[ 0 ];
				
				var el		= document.createElement('script');
					el.src	= 'http://stream.twitter.com/1/statuses/filter.json?follow=' + follow;
					el.type	= 'text/javascript';
					
				head.appendChild( el );
			
			}
		
		});
		**/
						
	},
	
	initSelects: function() {
	
		document.observe( 'dom:loaded', function() {
			
			Klydo.customSelects();
		
		});
	
	},
	
	customSelects: function() {
		
		$$( '.FORM-input-select' ).each( function( e ) {
					    
		    var els = e.select( 'select' );
		    
		    if( els > 0 ) {
			    
			    els[0].observe( 'change', function( i ) {
			    	
			    	//console.log(i.target,e);
			    	e.select( 'div.FORM-input-select-dummy-container' )[0].innerHTML = i.target.options[ i.target.selectedIndex ].innerHTML;
			    	//$( x.target.id + '-container' ).innerHTML = $( x.target.id ).options[ x.target.selectedIndex ].innerHTML;
			    
			    }.bind(e));
			
			}
					
		});
			
	},
	
	initSelect: function( id ) {
		
		var e = $( id );
		
		e.select( 'select' )[0].observe( 'change', function( i ) {
	    	
	    	//console.log(i.target,e);
	    	e.select( 'div.FORM-input-select-dummy-container' )[0].innerHTML = i.target.options[ i.target.selectedIndex ].innerHTML;
	    	//$( x.target.id + '-container' ).innerHTML = $( x.target.id ).options[ x.target.selectedIndex ].innerHTML;
	    
	    }.bind(e));
	
	},
	
	watch: function() {
	
		Event.observe( 'HEADER-search-submit', 'click', function( e ) {
		
			Klydo.search( $( 'HEADER-search-keyword' ).value );
		
		});
	
	},
	
	getFriends: function( friendsTerm, json ) {
	
		var params = {};
		
		params.action	= 'friends.getFriends';	
		params.term		= friendsTerm;
		
		if( json.callback != '' ) {
		
			params.callback	= json.callback;
		
		}
		
		if( json.exclude != '' ) {
		
			params.exclude	= json.exclude;
		
		}
		
		new Ajax.Request( AJAX, {
		
			parameters: params,
			onSuccess: function( e ) {
			
				eval( e.responseText );
			
			}
		
		});
	
	},
	
	search: function( term ) {
	
		window.location.href = URI + 'search/all/' + encodeURIComponent( term );
	
	},
			
	getRelatedServices: function( id ) {
	
		new Ajax.Request( AJAX, {
			
			parameters: { 'action': 'stream.getAvailServices', 'streamId': id },
			onSuccess: function( e ) {
			
				var data = eval( '(' + e.responseText + ')' );
				
				if( data.status == 'ok' ) {
					
					//console.log(data.services);
				
				}
			
			}
		
		});
	
	},
	
	getNotifications: function() {
		
		new Ajax.Request( AJAX, {
		
			parameters: { 'action': 'notifications.get' },
			onSuccess: function( e ) {
				
				var data = e.responseText.evalJSON();
				
				if( data.notifications.length > 0 ) {
					
					var holder = $$( '#notifications ul' );
					
					if( holder.length > 0 ) {
						
						holder = holder[ 0 ];
					
					} else {
						
						holder = false;
					
					}
					
					for( var i = 0; i < data.notifications.length; i++ ) {
					
						if( holder ) {
							
							holder.insert({
								top: '<li class="NOTICE NOTICE-blue growl-quick" style="display: none;"><p>' + data.notifications[ i ] + '</p></li>'
							});
													
						} else {
						
							new growl( '', data.notifications[ i ], 'blue' );
						
						}
												
					}
					
					this.getNotifications();
				
				} else {
					
					this.getNotifications();
				
				}
			
			}.bind(this)
		
		});
	
	},
	
	checkAll: function( checkbox, dom, className ) {
	
		if( typeof( checkbox ) == 'string' ) {
			
			checkbox = $$( checkbox )[ 0 ];
		
		}
		
		if( typeof( checkbox ) == 'object' ) {
		
			checkbox = checkbox[ 0 ];
		
		}
		
		checkbox.observe( 'click', function( x ) {
	
			if( x.target.checked ) {
			
				var selector = 'checked';
			
			} else {
				
				var selector = 'unchecked'; 
			
			}
	
			if( dom && dom !== false ) {
				
				var checkboxes = dom;
			
			} else {
			
				var checkboxes = $$( 'checkbox.' + className );
			
			}
			
			checkboxes.each( function( element ) {
			
				if( selector == 'checked' ) {
				
					if( element.checked != true ) {
					
						element.checked = true;
					
					}
				
				} else {
				
					if( element.checked != false ) {
					
						element.checked = false;
					
					}
				
				}
			
			});
			
		}.bind(dom).bind(className));
	
	}
	
});

var Klydo = new klydo( 'http://klydo.com/' );