function Jsgalaxy(){
	var ALL_PREVIEW = jQuery.makeArray($("#galaxy .galaxy_preview"));
	var CURRENT_MAX = 0;

	function Model(){

		function _galaxy(){
			var stage_w = $("#galaxy").width();
			var stage_h = $("#galaxy").height();
			var quart = 1;
			$("#galaxy .galaxy_preview").each(function(i,item){
				v.buildGalaxy($(item), quart, stage_w, stage_h);
				quart = quart + 1;
				quart = quart > 5 ? 1 : quart ;
			});
		};
		// _galaxy()
		
		function _showMax(e){
			var elmt = e || false;
			if(elmt){
				
				CURRENT_MAX = ALL_PREVIEW.indexOf($(elmt).get(0));
				var file = elmt.attr('hr_file');
				var title = elmt.attr('hr_title');
				var ref = elmt.attr('hr_ref');
				var body = elmt.attr('hr_body');
				v.buildMax(title, file, body, ref);
			}
		};
		// _showMax()
		
		function _nextMax(){
			CURRENT_MAX = (CURRENT_MAX+1) == ALL_PREVIEW.length ? 0 : CURRENT_MAX+1;
			var ne = ALL_PREVIEW[CURRENT_MAX];
			var file = $(ne).attr('hr_file');
			var title = $(ne).attr('hr_title');
			var ref = $(ne).attr('hr_ref');
			var body = $(ne).attr('hr_body');
			v.changeMax(title, file, body, ref);
		};
		// _nextMax()
		
		function _hideMax(e){
			var elmt = e || false;
			if(elmt){
				v.hideMax(elmt);
			}
		};
		// _hideMax(e)

		// méthodes publiques
		if (typeof Model.initialized == "undefined"){
			//
			
			Model.prototype.init = function(){
				$.log("GALAXY m // init");
				//alert("init");
				_galaxy();
				c.loadEvents();
			};
			// init
			
			
			Model.prototype.showMax = function(e){
				_showMax(e);
			};
			// showMax
			
			Model.prototype.nextMax = function(){
				_nextMax();
			};
			// nextMax
		
			Model.prototype.hideMax = function(e){
				_hideMax();
			};
			// hideMax
			
			Model.initialized = true;
			//
		}
		
	};

	function View(){
		

		if (typeof View.initialized == "undefined"){
			
			View.prototype.buildGalaxy = function($item, quart, stage_w, stage_h){
				// $.log("_galaxy // stage_h = "+stage_h+" stage_w = "+stage_w);
				//$("#block_views_galaxy li").hide();
				
				var scale = Math.random();
				//$.log("_galaxy // scale = "+scale);
				var $img = $item.find("img:first");
				//alert("scale = "+scale);
				if(jQuery.browser.msie){
					$item.show();
					var q = 0.063;
					var h = $img.height();
					//alert("h = "+h);
					var w = $img.width();
					if(h > 0 && w > 0){
						var h_em = h * q;
						var w_em = w * q;
						$img.css({'height':h_em+"em",'width':w_em+"em"});
					}
					$item.hide();
				}
				var img_w = parseFloat($img.css("width"))*scale;
				var img_h = parseFloat($img.css("height"))*scale;
				//$.log("_galaxy // img_h = "+img_h+" img_w = "+img_w);
			
				$img.css({"width":img_w+"em","height":img_h+"em"}).show();
				
				switch(quart){
					case 1:
					var ori_w = 0;
					var ori_h = 0;
					var amp_w = 25;
					var amp_h = 50;
					break;
					case 2:
					var ori_w = 75;
					var ori_h = 0;
					var amp_w = 25;
					var amp_h = 50;
					break;
					case 3:
					var ori_w = 75;
					var ori_h = 50;
					var amp_w = 25;
					var amp_h = 50;
					break;
					case 4:
					var ori_w = 0;
					var ori_h = 50;
					var amp_w = 25;
					var amp_h = 50;
					break;
					case 5:
					var ori_w = 25;
					var amp_w = 50;
					var amp_h = 10;
					if(Math.random() > 0.5){
						var ori_h = 0;
					}else{
						var ori_h = 90;
					}
					break;
				}
				
				var pos_w = Math.ceil(Math.random()*amp_w)+ori_w;
				var pos_h = Math.ceil(Math.random()*amp_h)+ori_h;
				// $.log('pos_w : '+pos_w+' | ')
				var fade_time = (0.1+Math.random())*7000;	

				$item.css({"top":pos_h+"%","left":pos_w+"%"}).fadeIn(fade_time);
				//$(e).css({"top":pos_h+"%","left":pos_w+"%"}).show();
				// $.log('GALAXY $item',$item);
				
			};
			// buildGalaxy();
			
			View.prototype.buildMax = function(t, f, b, r){
				var title = t || false;
				var file = f || false;
				var body = b || false;
				var ref = r || false;
				//console.log(file);
				if (file && title){
					//$.log("buildMax // file = "+file+" title = "+title+" body = "+body);
					//$.log(file);
					jQuery("<img/>").attr("src", file);
				
					var loader = $("#galaxy").attr("loader");
					var closer = $("#galaxy").attr("closer"); 
				
					var html_str = '<div id="galaxy_hr" style="display:none;">';
					html_str += '<div class="fond"></div>';
					html_str += '<div class="hr_image">';
					html_str += '<div class="hr_title">'+title;
					if(ref){
						html_str += '<span>'+ref+'</span>';
					}
					html_str += '</div>';
					if (body) {
						html_str += '<div class="hr_body">'+body+'</div>';
					}
					html_str += '<img class="preloader" src="'+loader+'" />';
					html_str += '<img class="closer" src="'+closer+'" />';
					html_str += '</div></div>';
					//
					var root = $('body');
					root.prepend(html_str);
					//
					var loader = $(".hr_image > .preloader");
					var loader_marg_top = loader.height() * 0.5;
					var loader_marg_left = loader.width() * 0.5;
					loader.css('margin-left', "-"+loader_marg_left+"px");
					loader.css('margin-top', "-"+loader_marg_top+"px");
				
					//$(".hr_image > .preloader").hide();
					//$(".hr_image > .closer").css('position', 'absolute');
					$(".hr_image > .closer").hide();
					$(".hr_image > .hr_title").hide();
					$(".hr_image > .hr_body").hide();
				
					var galaxy_hr = $('#galaxy_hr');
					galaxy_hr.fadeIn(1000, function(){
						var img = new Image();                                                       
						$(img).load(function () {
							$('#galaxy_hr > .hr_image').prepend(this);
							$(this).attr('class', 'image');
						
							$(".hr_image > .preloader").hide();

							var img_h = $(this).height() * 0.063;
							var img_w = $(this).width() * 0.063;
							$(this).css({"width":img_w+"em","height":img_h+"em"});
							
							//var stage_h = $("#galaxy_hr").height() * 0.063;
							//var box_top = (stage_h - img_h)/2;
							//$.log("stage_h = "+stage_h+" box_top = "+box_top+" img_h = "+img_h);
							//$(".hr_image").css({"width":img_w+"em","height":img_h+"em","top":box_top+"em"});
							$(".hr_image").css({"width":img_w+"em","height":img_h+"em"});
							$(this).hide();
							
							$(this).fadeIn(1000, function(){
								
								$(".hr_image > .hr_title").fadeIn(1000);
								$(".hr_image > .hr_body").fadeIn(2000);
								
								$(".hr_image > .closer").fadeIn(1000, function(){
									$(".hr_image > .closer").click(function () {
										v.hideMax($("#galaxy_hr"));
							    });
								});
							
								
								$(".hr_image > .image").click(function () {
									m.nextMax();
							  });
							
							
							});
						}).error(function (msg, url, line) {
							$.log(msg);
							alert("load image failed");
							v.hideMax($("#galaxy_hr"));
						}).attr('src', file);
					});
					$("#galaxy_hr .fond").click(function () {
						v.hideMax($("#galaxy_hr"));
				  });
				}
			};
			// end of buildMax
			
			View.prototype.changeMax = function(t, f, b, r){
				var title = t || false;
				var file = f || false;
				var ref = r || false;
				var body = b || false;
				
				if (file && title){
					jQuery("<img>").attr("src", file);
				
					//var galaxy_hr = $('#galaxy_hr');
					$(".hr_image > .closer").fadeOut(200);
					$(".hr_image > .hr_title").fadeOut(200);
					$(".hr_image > .hr_body").fadeOut(200);
							
					$('#galaxy_hr > .hr_image > .image').fadeOut(500, function(){
						
						$('#galaxy_hr > .hr_image > .image').remove();
						$(".hr_image > .preloader").fadeIn(500, function(){});
						
						var title_html = ref ? title+'<span>'+ref+'</span>' : title;
						$(".hr_image > .hr_title").html(title_html);
						if(body){	$(".hr_image > .hr_body").html(body); } 
						
						var img = new Image();                                                       
						$(img).load(function () {
							
							$('#galaxy_hr > .hr_image').prepend(this);
							var img_h = $(this).height() * 0.063;
							var img_w = $(this).width() * 0.063;
							$(this).css({"width":img_w+"em","height":img_h+"em"});
							
							//var stage_h = $("#galaxy_hr").height() * 0.063;
							//var box_top = (stage_h - img_h)/2
							//$(".hr_image").css({"width":img_w+"em","height":img_h+"em","top":box_top+"em"});
							$(".hr_image").css({"width":img_w+"em","height":img_h+"em"});
						
							$(this).hide();
							
							//$('#galaxy_hr > .hr_image > .image').replaceWith($(this));
							
							$(this).attr('class', 'image');
						
							$(".hr_image > .preloader").hide();

							$(this).fadeIn(1000, function(){
								$(".hr_image > .hr_title").fadeIn(1000);
								$(".hr_image > .hr_body").fadeIn(2000);
								
								$(".hr_image > .closer").fadeIn(1000, function(){
									$(".hr_image > .closer").click(function () {
										m.hideMax($("#galaxy_hr"));
							    });
								});
								
								$(".hr_image > .image").click(function(){
									m.nextMax($("#galaxy_hr"));
								});
							
							});
						}).error(function () {
							alert("load image failed");
							v.hideMax($("#galaxy_hr"));
						}).attr('src', file);
					});
				}
			};
			// end of changeMax
			
			View.prototype.hideMax = function(e){
				var elmt = e || false;
				//console.log(elmt);
				if (elmt){
					elmt.fadeOut(1000, function(){
						$("#galaxy_hr").remove();
					});
				}
			};
			// end of hideMax
			
			View.prototype.flash = function(e){
				$(e).css({opacity:0.3}).animate({opacity:1}, {duration:500});
			};
			//flash

			View.initialized = true;
		}
	};
	//View

	// CONTROLER - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

	function Controler(){
		// variables privées
	
		// methodes publiques
		if (typeof Controler.initialized == "undefined"){
			//
			
			Controler.prototype.loadEvents = function(){
				$("#galaxy .galaxy_preview").mouseup(function(){
					m.showMax($(this));
		    }).mousedown(function(){
					//
		    });
				
				$("#galaxy .galaxy_preview").hover(function() {
					v.flash($(this));
				}, function() {
				});
				//$("#galaxy .galaxy_preview").hover
				
				//
			};
			//
			Controler.initialized = true;
		}
	};
	//Controler

	var m = new Model();
	var v = new View();
	var c = new Controler();
	//
	
	if (typeof Jsgalaxy.initialized == "undefined"){
		/*
		Galaxy.prototype.getMVC = function(mvc){
			switch(mvc){
				case "m":
					return m;
					break;
				case "v":
					return v;
					break;
				case "c":
					return c;
					break;
			}
		};
		*/
		

		Jsgalaxy.initialized = true;
	}
	
	m.init();
};
// Jsgalaxy

