/*	jquery imagepos plugin for TST		*/
/*	version: 0.1						*/
/*	author: bjorn.wendeler@burnabit.com	*/

jQuery.noConflict();

jQuery(document).ready(function() {  
		jQuery(window).setimagepos();
		jQuery(window).resize(
			function () {
				jQuery(window).setimagepos();
			}
		);
	});  

(function(jQuery){

	jQuery.fn.extend({ 
	
		//pass the options variable to the function
 		setimagepos: function(opt) {
			
			//Set the default values, use comma to separate the settings, example:
			var defaults = {
				imgx: 0,
				imgy: 279,
				imgsrc: "http://www.thestrangestthing.com/templates/tst/images/tst/tst-motiondesign-57x150.png",
				imgalt: "The Strangest Thing - MOTION DESIGN",
				imgw: 57,
				imgh: 150
			};
				
			var options = jQuery.extend(defaults, opt);
			
			check_sizes(options);

    		function check_sizes(o) {
    			var dims = get_dimensions();
    			var pagew = jQuery("#page").width();
    			var reqw = pagew + 24 + o.imgw;
    			if ( dims.width > reqw ) {
//    				alert("dims.width: "+dims.width+" > reqw:"+reqw);
    				set_image(o); 
    			} else {
    				remove_image();
    			}
    		}
    		
    		function create_image(o) {
    			var img = '<div id="tstmotiondesign"><img src="'+o.imgsrc+'" width="'+o.imgw+'" height="'+o.imgh+'" alt="'+o.imgalt+'" border="0" /></div>';
    			jQuery("body").append(img);
    			jQuery("#tstmotiondesign").css("position", "absolute");
    			jQuery("#tstmotiondesign").css("display", "none");
    		}
    		
    		function remove_image() {
    			if ( jQuery("#tstmotiondesign").size() !== 0 ) jQuery("#tstmotiondesign").remove();
    		}
    		
    		function set_image(o) {
    			if ( jQuery("#tstmotiondesign").size() === 0 ) create_image(o);
    			var pageoff = jQuery("#page").offset();
    			var imgx = parseInt(Math.round(pageoff.left) - o.imgw);
    			jQuery("#tstmotiondesign").css("top", o.imgy+"px");
    			jQuery("#tstmotiondesign").css("left", imgx+"px");
    			jQuery("#tstmotiondesign:hidden").fadeIn("slow");
    		}
    		
    		function get_dimensions(){
    			var dimensions = {width: 0, height: 0};
    			if (document.documentElement) {
    				dimensions.width = document.documentElement.offsetWidth;
    				dimensions.height = document.documentElement.offsetHeight;
    			} else if (window.innerWidth && window.innerHeight) {
    				dimensions.width = window.innerWidth;
    				dimensions.height = window.innerHeight;
    			}
    			return dimensions;
    		}
    	}	//	EO setimagepos
	});
})(jQuery); 