function PersonalPage_Init( ){
	$(window).bind('resize', function() {
		Homepage_Icon.CloseWindow();
	});

	$('li.Game').each( function(  ){
		new Homepage_Game( this );
	} );

	$('li.Music').each( function(  ){
		new Homepage_Music( this );
	} );

	$('li.XSTag').each( function(  ){
    new Homepage_XSTag( this );
  } );

	$('li.Language').each( function(  ){
    new Homepage_Language( this );
  } );

	$('div#Promotion').each(function(){
		new PromotionalGames( this );
	});
}

function GamesPage_Init(){
	$('li.Game').each( function(  ){
    new Homepage_Game( this );
  } );

	$(window).bind('resize', function() {
    Homepage_Icon.CloseWindow();
  });
}

function GamesPage_OpenGame( game_id ){
	Homepage_Game.Games[ game_id ].Click();
}

function Homepage_Icon(  ){
}

Homepage_Icon.prototype.Init = function( element ){
	this.Element = element;

	this.MouseOn = {};

  this.outTimer = null;

	var self = this;

  this.Element.onclick = function(){ return false; }

	this.InitButtons();

  $(this.Element).children("a").bind( 'mouseover', function(){
    self.MouseOver();
  } ).bind( 'mouseout', function(){
    clearTimeout( this.outTimer ); 
    self.outTimer = setTimeout( function(){
      self.MouseOut();
    }, 50 );
  } ).bind( 'click', function(){

		if ( $(this).hasClass( "Title" ) ){
       self.Click();
    }

		var classes = this.className.split( ' ' );
		for( var i in classes ){
			if ( jQuery.isFunction( self[ 'Click' + classes[ i ] ] ) ){
				self[ 'Click' + classes[ i ] ]();
			}
		}
		
    return false;
  } );

}

Homepage_Icon.prototype.InitButtons = function( ){
	
	$(this.Element).children("a.AlwaysVisible").css( "visibility", "visible" );
	this.jqueryButtonsAnimation = $(this.Element).children( 'a.Action:not(a.AlwaysVisible)' );

}

Homepage_Icon.CurrentOpenWindow = null;

Homepage_Icon.prototype.Click = function(){}

Homepage_Icon.prototype.ClickAsk = function(){}

Homepage_Icon.prototype.ClickSound = function(){}

Homepage_Icon.prototype.ClickPlay = function(){}

Homepage_Icon.prototype.Ask = function( question, callback ){
  var node = document.createElement( 'a' );
  node.appendChild( document.createTextNode( question ) );

  node.href='javascript:void(null)';
  var self = this;
  $(node).bind( 'click', callback );
  this.ShowInfoWindow( node );
}

Homepage_Icon.prototype.MouseOver = function(){
	this.jqueryButtonsAnimation = $(this.Element).children( 'a.Action:not(a.AlwaysVisible)' );

  clearTimeout( this.outTimer );
	this.jqueryButtonsAnimation.stop().css('visibility', 'visible');
	if ($.browser.msie) {
		this.jqueryButtonsAnimation.show();
	} else {
		this.jqueryButtonsAnimation.stop().css( 'opacity', '1' ).show();
	}
}

Homepage_Icon.prototype.MouseOut = function(){
  clearTimeout( this.outTimer );
	this.InitButtons();

	if ($.browser.msie) {
		this.jqueryButtonsAnimation.hide();
	} else {
		this.jqueryButtonsAnimation.animate( { opacity: 0 }, 1000 );
	}
}

Homepage_Icon.prototype.ShowInfoWindow = function( html_or_node ){

	Homepage_Icon.CloseWindow();

	var w = CreateInfoWindow(
		html_or_node, 
		{ 
			markerOffset: $(this.Element).position().left + parseInt( $(this.Element).children('a.Title').width() ) / 2, 
			baseNodeType: 'li' 
		} 
	);


	// Search last node in the current floating row
	var nextNode = $(this.Element).next();
	var lastInRow = $(this.Element);

	while( nextNode.length == 1 && nextNode.position().left != 0 ){
		if ( nextNode.position().left > lastInRow.position().left ){
			lastInRow = nextNode;
		}
		nextNode = nextNode.next();
	}

	if ( lastInRow.next().length == 1 ){
		w.AddToDocument( this.Element.parentNode, lastInRow.next().get( 0 ) );
	} else {
		w.AddToDocument( this.Element.parentNode );
	}

  Homepage_Icon.CurrentOpenWindow = {
    Object: this,
    Element: w,
    html: html_or_node
  };

}

Homepage_Icon.CloseWindow = function(){
	if ( Homepage_Icon.CurrentOpenWindow ){
		RemoveInfoWindow( Homepage_Icon.CurrentOpenWindow.Element );
	}
	Homepage_Icon.CurrentOpenWindow = null;
}

function Homepage_Game( element ){
	this.Init( element );

	this.game_id = this.id = null;

	if ( $(this.Element).parent().get(0).id == 'collection_all_games' ){
   	this.game_id = parseInt( element.id.substring( 5 ) );
  } else {
		this.id = parseInt( element.id.substring( 5 ) );
  }

	Homepage_Game.Games[ this.game_id ] = this;
}

Homepage_Game.Games = [];

Homepage_Game.Get = function( game_id ){
	if ( Homepage_Game.Games[ game_id ] ){
		return Homepage_Game.Games[ game_id ];
	}
	return null;
}

Homepage_Game.prototype = new Homepage_Icon();

Homepage_Game.prototype.Click = function(){
	if ( $(this.Element).children( 'div.Details' ).length > 0 ){
		this.ShowInfoWindow( $(this.Element).children( 'div.Details' ).get( 0 ).innerHTML );
    RatingTool.InitRating( );
	} else {
		var self = this;
		jQuery.get(
  	  '../swinxs/game_info.php',
	    {
    	  game_id : this.game_id,
  	    id : this.id
	    },
    	function( data ){
  	    self.ShowInfoWindow( data );
	      RatingTool.InitRating( );
  	  }
	  );
	}
}

Homepage_Game.prototype.ClickDownload = function(){
  var self = this;
	var node = document.createElement( 'span' );
  var downloadAllowed = $(this.Element).find( 'span.Prohibited' ).length == 0;
 	
	if ( downloadAllowed ) {
		var link = document.createElement( 'a' );
		link.appendChild( document.createTextNode( 'Download this game' ) );
  	link.href='javascript:void(null)';
  	$(link).bind( 'click', function(){
    	Add_Game( self.game_id );
			Homepage_Icon.CloseWindow();
  	} );

		node.appendChild( link );
	}

	if ( $(this.Element).find( 'span.Download' ).length > 0 ) {
		if ( downloadAllowed ) node.appendChild( document.createElement( 'br' ) );
		$(this.Element).find( 'span.Download' ).each( function(){
			var message = this.cloneNode(true);
			node.appendChild( message );
			message.style.display = 'block';
			// Dit werkt niet: $(message).show();
		} );
	}

  this.ShowInfoWindow( node );
}

Homepage_Game.prototype.ClickBuyWithCredits = function(){
	var self = this;
  var node = document.createElement( 'span' );

	var link = document.createElement( 'a' );

  link.appendChild( document.createTextNode( 'Buy this game with credits and then download it to your Swinxs console' ) );
  link.href='javascript:void(null)';
	$(link).bind( 'click', function(){
		Buy_Game( self.game_id );
		Homepage_Icon.CloseWindow();

  } );

	node.appendChild( link );

	this.ShowInfoWindow( node );

	return false;
}

Homepage_Game.prototype.ClickDelete = function(){
	var self = this;
	this.Ask( 'Click to remove', function(){
		self.Delete();
	} );
}

Homepage_Game.prototype.Delete = function(){
	Homepage_Icon.CloseWindow();
	$(this.Element).children( '.Delete' ).remove();

	Delete_Game( this.id, this.game_id );
}

Homepage_Game.prototype.ClickUpdate = function(){
	var self = this;
  this.Ask( 'Click to update this game', function(){
		self.Update();
  } );
}

Homepage_Game.prototype.Update = function(){
  Homepage_Icon.CloseWindow();
  $(this.Element).children( '.Update' ).remove();
	Update_Game( this.id, this.game_id );
}

function Homepage_Music( element ){
  this.Init( element );

  this.music_id = parseInt( element.id.substring( 6 ) );

	Homepage_Music.Music[ this.music_id ] = this;
}

Homepage_Music.Music = [];

Homepage_Music.Get = function( music_id ){
  if ( Homepage_Music.Music[ music_id ] ){
    return Homepage_Music.Music[ music_id ];
  }
  return null;
}
  
Homepage_Music.prototype = new Homepage_Icon();

Homepage_Music.prototype.ClickPlay = function(){
	if ( $(this.Element).children('a.Play').hasClass( 'AlwaysVisible' ) == false ){
		var self = this;
		Play_Music( 
			this.music_id, 
			function(){
				self.MusicStopped();
			} 
		);
		$(this.Element).children('a.Play').addClass('Playing');
		$(this.Element).children('a.Play').addClass('AlwaysVisible');
	} else {
		Stop_Music( this.music_id );
	}
}

Homepage_Music.prototype.MusicStopped = function( ){
	$(this.Element).children('a.Play').removeClass('AlwaysVisible');
	$(this.Element).children('a.Play').removeClass('Playing');
	this.MouseOut();
}

Homepage_Music.prototype.ClickDelete = function(){
	var self = this;
  this.Ask( 'Remove?', function(){
		Delete_Music( self.music_id );
  } );
}

Homepage_Music.prototype.Click = function(){
  var self = this;
  jQuery.get( '../swinxs/music_info.php', { id: this.music_id }, function( data ){
    self.ShowInfoWindow( data );
  } );
}

function Homepage_XSTag( element ){
  this.Init( element );
  
  this.rfid = parseInt( element.id.substring( 6 ) );
} 
  
Homepage_XSTag.prototype = new Homepage_Icon();

Homepage_XSTag.prototype.Click = function(){
	this.ClickAsk();
}

Homepage_XSTag.prototype.ClickAsk = function(){
	if ( $(this.Element).find('textarea').length > 0 ){
		return;
	}

	var input = document.createElement( 'textarea' );
	input.className = 'XSTag_Name';
	input.value = $(this.Element).children(".Title").children('span').text();
	$(this.Element).find(".Ask").stop().text('').addClass( 'AlwaysVisible' ).append( input );
	input.focus();
	input.select();

	var self = this;
	var update = function(){
    self.StoreXSTagName( this.value );
  }

	$(input).bind( 'blur', update );
	$(input).keyup(function(e) {
		if(e.keyCode == 13) {
			update.apply( this );
		}
	});
}

Homepage_XSTag.prototype.StoreXSTagName = function( name ){
	name = jQuery.trim( name );

	var self = this;
	Update_XS( this.rfid, name, function(){
			$(self.Element).children(".Title").children('span').text( name );
			$(self.Element).children(".Ask").text( name ).removeClass( 'AlwaysVisible' ).children('textarea').remove();
			self.MouseOut();
	}	);
}

Homepage_XSTag.prototype.ClickSound = function(){
	Add_XS_Sound( this.rfid );
}

Homepage_XSTag.prototype.ClickPlay = function(){
	if ( $(this.Element).children('a.Play').hasClass( 'AlwaysVisible' ) == false ){
		Play_XS_Sound( this.rfid );
		$(this.Element).children('a.Play').addClass('Playing');
    $(this.Element).children('a.Play').addClass('AlwaysVisible');
		var self = this;
		setTimeout( function(){
			$(self.Element).children('a.Play').removeClass('Playing');
	    $(self.Element).children('a.Play').removeClass('AlwaysVisible');	
			self.MouseOut();
		}, 4000 );
	}
}

Homepage_XSTag.prototype.ClickDelete = function(){
	var self = this;
  this.Ask( 'Are you sure you want to remove this personal sound?', function(){
    self.Delete();
  } );
}

Homepage_XSTag.prototype.Delete = function(){
	var self = this;
	$.get(
    '../home/personal.php',
    {
			deletexs: this.rfid
    }, function(){
			checkQueue();
			Homepage_Icon.CloseWindow();
		}
  );
}

function Homepage_Language( element ){
  this.Init( element );

  this.language_id = parseInt( element.id.substring( 9 ) );
}

Homepage_Language.prototype = new Homepage_Icon();

Homepage_Language.prototype.ClickDelete = function(){
	var self = this;
  this.Ask( 'Click to remove', function(){
    self.Delete();
  } );
}

Homepage_Language.prototype.Delete = function(){
  Homepage_Icon.CloseWindow();
  $(this.Element).hide( 1000 );
	Delete_Language( this.language_id );
}

Homepage_Language.prototype.Click = function(){  
	if ( $(this.Element).find("a.Installed").length == 0 ){
		var self = this;
	  this.Ask( 'Click to download this language', function(){
			Add_Language( self.language_id );
	  } );
	}
}

Homepage_Language.prototype.ClickPlay = function(){
  var self = this;
  this.Ask( 'Click to select this language as your standard language', function(){
		Select_Language( self.language_id );
	} );
}

function RatingTool( element ){
	this.Element = element;
	this.game_id = this.Element.id.substring( 12 );

	this.defaultClassName = this.Element.className;
	
	var self = this;
	$(this.Element).find("a").each(function(){
		self.InitStar( this, this.className.substr( 5 ) );
	})
}

RatingTool.prototype.InitStar = function( element, nr ){
	var self = this;
	$(element).bind( 'mouseover', function(){
		self.SetRating( nr );
	});
	$(element).bind( 'mouseout', function(){
		self.SetDefaultRating();
	});
	$(element).bind( 'click', function(){
		self.Submit( nr );
  });
}

RatingTool.prototype.SetRating = function( nr ){
	this.Element.className = 'Rating Rating_' + nr;
}

RatingTool.prototype.SetDefaultRating = function(  ){
	this.Element.className = this.defaultClassName;
}

RatingTool.prototype.Submit = function( nr ){
	this.defaultClassName = this.Element.className;
	$.post( 
		'../swinxs/game_rating.php', 
		{
			id: this.game_id,
			rating: nr
		},
		function(){
			//todo bedankt boodschap oid
		}
	);
}

RatingTool.InitRating = function( ){
	$('form.Rating').each(function(){
		new RatingTool( this );
	});
}

function PromotionalGames( element ){
	var self = this;
	this.Element = element;

	this.Childs = [];
	this.currentIndex = 0;

	$(this.Element).find( 'li' ).each(function( i ){
		self.Childs.push( this );
		if ( $(this).hasClass( 'Active' ) ){
		 self.currentIndex = i;
		}
	});

	$(this.Element).find( 'a.Previous' ).click(function(){
    self.NavigateTo( self.currentIndex - 1 );
  });
	
	$(this.Element).find( 'a.Next' ).click(function(){
		self.NavigateTo( self.currentIndex + 1 );
	});

	$(this.Element).find( 'a.Download' ).click(function(){
		var node = document.createElement( 'a' );
		node.href = 'javascript:void(0)';
		node.appendChild( document.createTextNode( 'Download?' ) );
		$(node).click( function() {
			
		});

		var w = CreateInfoWindow( node, { markerOffset: 70 } );
		var text = $(this).parent().next(0).get( 0 );
		w.AddToDocument( text.parentNode, text );

	});

}

PromotionalGames.prototype.DownloadGame = function( id ){
	ajaxCall('../swinxs/addgame.php?game_id=' + id,function() {},1,0,0);
	checkQueue();
}

PromotionalGames.prototype.NavigateTo = function( index ){
	if ( index >= this.Childs.length ){
		index = 0;
	} 
	if ( index < 0 ){
		index = this.Childs.length - 1;
	}

	$(this.Childs[this.currentIndex]).hide();
	$(this.Childs[index]).show();
	this.currentIndex = index;
}

