/**
 * Create Boost namespace
 */
var Boost = new (Class.create({
	
	_li: null,
	clickEv: null,
	picPopup: null,
	
	initialize: function() {
		
		this.initSelect();
		
		this.domReady( function() {
			
			this.ratings();
			
			$( document.body ).observe( 'click', function( e ) {
				
				Boost.clickEv = e;
			
			}.bind(this));
						
		}.bind(this));
		
	},
	
	domReady: function( eval ) {
		
		document.observe( 'dom:loaded', eval );
	
	},
	
	include: function( src, nocheck ) {
		
		if( !nocheck ) {
			
			nocheck = false;
			
		}
		
		var ext		= src.split( '.' );
			ext		= ext[ ( ext.length - 1 ) ];
		var insert	= false;
		
		switch( ext.toLowerCase() ) {
			
			default:

				if( $$( 'iframe[src="' + src + '"]' ) == '' || nocheck === true ) {
											
					var position	= document.getElementsByTagName( 'body' )[ 0 ];
					var el			= new Element( 'iframe', { 'src': src, 'width': 0, 'height': 0 } );
					insert			= true;
					
				}
							
				break;
				
			case 'js':

				if( src.match( /http:\/\//gi ) === null ) {
				
					src = URI + '_public/_js/' + src;
				
				}			

				if( $$( 'script[src="' + src + '"]' ) == '' || nocheck === true ) {
											
					var position	= document.getElementsByTagName( 'head' )[ 0 ];
					var el			= new Element( 'script', { 'src': src, 'type': 'text/javascript' } );
					insert			= true;
					
				}
				
				break;

			case 'css':

				if( src.match( /http:\/\//gi ) === null ) {
				
					src = URI + '_public/_css/' + src;
				
				}			
			
				if( $$( 'link[href="' + src + '"]' ) == '' || nocheck === true ) {
											
					var position	= document.getElementsByTagName( 'head' )[ 0 ];
					var el			= new Element( 'link', { 'href': src, 'rel': 'stylesheet', 'rev': 'stylesheet' } );
					insert			= true;
					
				}
				
				break;

		}
		
		if( insert ) {
			
			position.insert({bottom:el});
		
		}
			
	},
	
	initSelect: function( id ) {
		
		this.domReady( function() {
		
			$$( 'div.FORM-select-input' ).each( function( e ) {

			    var els = e.select( 'select' );
			    
			    if( els[ 0 ] ) {
				    
				    els[0].observe( 'change', function( i ) {

				    	e.select( 'div.FORM-select-input-dummy-container' )[0].innerHTML = i.target.options[ i.target.selectedIndex ].innerHTML;
				    
				    }.bind(e));
				
				}
					
			});
		
		});
	
	},
	
	
	initSelects: function() {
	
		this.domReady( function() {
			
			this.customSelects();
		
		}.bind(this));
	
	},
	
	customSelects: function() {
		
		$$( '.FORM-input-select' ).each( function( e ) {
					    
		    var els = e.select( 'select' );
		    
		    if( els > 0 ) {
			    
			    els[0].observe( 'change', function( i ) {
			    	
			    	e.select( 'div.FORM-input-select-dummy-container' )[0].innerHTML = i.target.options[ i.target.selectedIndex ].innerHTML;
			    
			    }.bind(e));
			
			}
					
		});
			
	},
	
	handleDropdown: function( event ) {

		if( event.target.isChildOf( this._li ) !== true ) {
			
			this._li.select('ul.submenu')[0].hide();
			this.SHOWING = false;	
			document.body.stopObserving( 'click', this.handleDropdown.bind(this).bind(this._li));
			
		}
		
	},
	
	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));
	
	},
	
	ratings: function() {

		document.observe( 'starbox:rated', this.saveRating.bind(this) );
	
	},
	
	saveRating: function( event ) {
	
		new Ajax.Request( URI + 'ratings/save', {
		
			parameters: event.memo
		
		});
	
	},
	
	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 );
			
			}
		
		});
	
	},
	
	picturePopup: function() {
	
		this.picPopup = new jsAlert();
		this.picPopup.addTab( 'Change Picture', '<div class="PROFILE-change-picture"><ul class="POPUP-rows"><li class="PROFILE-change-picture-current"><label>Current</label><div class="FORM-input"><img alt="" src="' + USER.profilePicture + '" /></div></li><li class="PROFILE-change-picture-new even"><label for="file">New</label><div class="FORM-input"><iframe src="' + URI + 'settings/picture" frameborder="0"></iframe></div></li></ul></div>' );
		this.picPopup.show();
	
	},
	
	changePics: function( aws, image ) {
	
		var k = USER.profilePicture;
		
		if( k == 'http://boost.tv/images/layout/top/tux.png' ) {
			
			window.location.reload();
		
		} else {
			
			var x = k.split( '/' );
			var y = x[ ( x.length - 1 ) ];
	
			var regex = new RegExp( 'http:\/\/s3\.amazonaws\.com\/boost\.tv\/photos\/(.*)\/' + y );
		
			$$( 'img' ).each( function( el ) {
			
				var src = el.src;
				var x	= regex.exec( src );
				
				if( x ) {
					
					el.src = aws + x[ 1 ] + '/' + image;
					
				}
				
			}.bind(regex).bind(aws).bind(image));
			
			this.picPopup.destroy();
			
			USER.profilePicture = aws + 'full/' + image;
		
		}
			
	}
		
}));