/*
 * jquery stonesmart 1.0
 */
(function($) {
	$.fn.extend({

		makeAbsolute: function(rebase) {
			return this.each(function() {
				var el = $(this);
				var pos = el.position();
				
			//	alert(el.outerWidth() + " - " + el.outerHeight());
				
				el.css({ position: "absolute",			
						marginLeft: 0, 
						marginTop: 0,			
						top: pos.top, 
						left: pos.left,			
						width: el.outerWidth(),
						height: el.outerHeight()			
					 });	
					 	
				if (rebase)			
					el.remove().appendTo("body");			
			});			
		},
		
		centerInWindow: function(){
		
		        var el = $(this);
		        	        
		        el.css({
		            left: Math.floor(($(window).width() - el.width()) / 2) + 'px',
		            top: Math.floor(($(window).height() - el.height()) / 3) + 'px',
		            position: 'absolute'
		        });
		       
		},
		
		
		preloadImages: function()
        {
          for(var i = 0; i<arguments.length; i++)
          {
            $("<img>").attr("src", arguments[i]);
          }
        },
		
			
		insertImage: function(destination, options, callback) {			
		
		 				var settings = $.extend({
							 animate: true,
							 fadeIn: false,
							 fadeOut: false,
							 speed: 1800,
							 animation: "swing"
						  }, options);
		
			return this.each(function() {


						var source = $(this);		
						if(settings.animate)
						{
							
							/*
							 * Lägg på animeringsinställningar
							 */
							 settings = $.extend({
									 animateTo: {
										top: destination.position().top,
										left: destination.position().left,
										height: destination.innerHeight(),
										width: destination.innerWidth()
									 }
								  }, settings);
		
							source.makeAbsolute(false).
							animate({ 
								width: settings.animateTo.width,
								height: settings.animateTo.height,
								left: settings.animateTo.left,
								top: settings.animateTo.top
								}, settings.speed, settings.animation, function(){
									source.appendTo($(destination))
									callback();
								});						
						}else if(settings.fadeOut){			
						
							
							/*source.fadeOut(Math.floor(settings.speed / 2), function(){
									source.show()
									.appendTo($(destination))
									.hide()
									.fadeIn(Math.floor(settings.speed / 2), function(){	
											source.css("position", "inherit").appendTo($(destination)); //.css("position", "inherit");										
											callback();
									});		*/		
									
									
									source.fadeOut(Math.floor(settings.speed / 2), function(){
									
										source.show().appendTo($(destination)).hide();
															
										 callback();
									 
									});		
									
									
							//});	 
							
							
							
							
							}else if(settings.fadeIn){		
													
								source.show()
								    .css("position", "static")
									.hide()
									.appendTo($(destination))
									.fadeIn(settings.speed, function(){	
										callback();					 
									});	
							
							
							//show().hide() är för ie ^^
							
							
							}else{
								source.appendTo($(destination))
							}
/*
							
							source.css({
									   left: destination.position().left,
									   top: destination.position().top									   
									   });
							
							"left", $(destination).css("left"))
								  .css("top", $(destination).css("top"));
								  
								  
								  
							try{
							source.css("width", $(destination).innerWidth())
								  .css("top", $(destination).innerHeight())
							}catch(e){
								
							}
*/


							
							
							
							
										
				});
		}
	
	});

})(jQuery);
