/**
modal_content => sm_content
modal_overlay => sm_olay
modal_close => sm_close
modal_title => sm_title
fn.modal* => fn.smart_modal*
modal_count => sm_count
**/
jQuery(document).ready(function(){

	(function($) {
		//base function to call and setup everything
		
		$.fn.smart_modal = function(options)
		{
			return this.each(function()
			{
				if(this.id!='')
				{
					var load_popup_image = new Image();
						load_popup_image.src = this.id;
					
					if(this._sm) return; //if already a modal return
					
					if(typeof(options) != "undefined")var params = $.extend(true, {}, $.fn.smart_modal.defaults, options); //if some options are passed in merge them
					else var params = $.fn.smart_modal.defaults;
					
					if(typeof(sm_count) == "undefined") sm_count=0; //set the counter to 0
					
					sm_count++;
					this._sm=sm_count; //set what modal number this is
					H[sm_count] = {config:params,target_sm:this}; //add to hash var
					$(this).smart_modal_add_show(this); //add show & hide triggers
				}
			});
		};
		
		$.fn.smart_modal_add_show=function(ele){ return $.smart_modal.show(ele); };
		
		//extra function so show & hide can be called
		$.fn.smart_modal_show = function()
		{
			return this.each(function()
			{
				$.smart_modal.open(this);
			}); 
		};
		
		$.fn.smart_modal_hide = function()
		{
			return this.each(function()
			{
				$.smart_modal.hide(this, true);
			}); 
		};
		
		//the default config vars
		$.fn.smart_modal.defaults = 
		{
			show:false, 
			hide:false, 
			modal_styles:
			{
				display:"block", 
				zIndex:1001
			},
			resize:true, 
			hide_on_overlay_click:true
		};
		
		//the over riden stuff
		$.smart_modal = 
		{
			hash:{}, //the hash used to store all the configs & targets
			show:function(ele)
			{
				var pos = ele._sm, h = H[pos];
				jQ(h.target_sm).mouseover(function()
				{
					if($(h.target_sm).attr('id')!='') $.smart_modal.open(ele);
					return false;
				});
				
				return false;
			},
			hide:function(ele, force)
			{
				var pos = ele._sm, h = H[pos];
				if(h.config.hide_on_overlay_click) var idstr = "#sm_olay, .sm_close";
				else var idstr = ".sm_close";
				
				if(force) $.smart_modal.remove(ele);
				
				jQ(h.target_sm).mouseout(function()
				{
					$.smart_modal.remove(ele);
					return false;
				});
			},
			remove:function(ele)
			{
				var pos = ele._sm, h = H[pos];
				jQ("#sm_content").remove();
				jQ("#sm_olay").remove();
				if(h.config.hide) h.config.hide();
			},
			open:function(ele)
			{
				var pos = ele._sm;
				var h = H[pos]; 
				
				$.smart_modal.insert_content_container();
				var smcontent = $.smart_modal.get_content($(h.target_sm), $(h.target_sm).attr('id'));
				jQ("#sm_content").html(smcontent);
				
				if(h.config.modal_styles) jQ("#sm_content").css(h.config.modal_styles);
				if(h.config.resize) $.smart_modal.resize_container();
				
				$.smart_modal.for_ie(jQ("#sm_olay")); 
				
				if(h.config.show) h.config.show();
				
				$.smart_modal.hide(ele); //add hiding
			},
			resize_container: function()
			{
				jQ(document).mousemove(function(e)
				{
					var pageCoords = new Array(e.pageX, e.pageY);
					var clientCoords = new Array(e.clientX, e.clientY);
					Toper = clientCoords[1];
				});
				
				var max_width = 0, max_height=0;
				
				jQ('#sm_content').css({
						top:	'-1000px',
						left:	'-1000px'
					});
				
				jQ('#sm_content *').load(function()
				{
					jQ('#sm_content *').each(function()
					{
						var tw = jQ(this).outerWidth(), th = jQ(this).outerHeight();
						if(tw > max_width) max_width = tw;
						max_height += th;
					});
					
					var page_size = getPageSize();
					var scr_hight = page_size[3];
					
					(Toper - 100) < 10 ? Toper = 10 : Toper -= 100;
					
					if (Toper + max_height > scr_hight) Toper = scr_hight - max_height - 40;
					
					var main_wrapper = document.getElementById('header').offsetLeft;
					
					jQ('#sm_content').css({
						top:	(Toper)+'px',
						left:	(main_wrapper+120)+'px'
					});
				});
			},
			insert_content_container:function()
			{
				if(!jQ('#sm_content').length) {jQ("body").prepend('<div id="sm_content"></div>');}
			},
			get_content:function(trig, popup_image)
			{
				//var nidle_div = trig.parents().eq(2);
				//var title = jQ(nidle_div).find('h3 a').text();
				//var description = jQ(nidle_div).find('p').text();
				c = "<img src='"+popup_image+"' alt='sm_modal' />";
				//c += "<div class=\"popup_image_title\">"+title+"</div>";
				//c += "<div class=\"popup_image_description\">"+description+"</div>";
				return c;
			},
			for_ie:function(o)
			{
				if(ie6&&$('html,body').css({height:'100%',width:'100%'})&&o)
				{
					$('html,body').css({height:'100%',width:'100%'});
					i=$('<iframe src="javascript:false;document.write(\'\');" class="overlay"></iframe>').css({opacity:0});
					o.html('<p style="width:100;height:100%"/>').prepend(i);
					o = o.css({position:'absolute'})[0];
				}
			}
		};
		
		var H=$.smart_modal.hash, jQ = jQuery;
		ie6=$.browser.msie&&($.browser.version == "6.0");
	})(jQuery);

});

function getPageSize()
{
	 var xScroll, yScroll;
	 
	 if (window.innerHeight && window.scrollMaxY)
	 {
		 xScroll = document.body.scrollWidth;
		 yScroll = window.innerHeight + window.scrollMaxY;
	 }
	 else if (document.body.scrollHeight > document.body.offsetHeight)
	 {
		 // all but Explorer Mac
		 xScroll = document.body.scrollWidth;
		 yScroll = document.body.scrollHeight;
	 }
	 else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight)
	 { 
	 	// Explorer 6 strict mode
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	 }
	 else 
	 {
		// Explorer Mac...would also work in Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	 }
	
	 var windowWidth, windowHeight;
	 
	 if (self.innerHeight)
	 {
		 // all except Explorer
		 windowWidth = self.innerWidth;
		 windowHeight = self.innerHeight;
	 }
	 else if (document.documentElement && document.documentElement.clientHeight)
	 {
		 // Explorer 6 Strict Mode
		 windowWidth = document.documentElement.clientWidth;
		 windowHeight = document.documentElement.clientHeight;
	 }
	 else if (document.body)
	 {
		 // other Explorers
		 windowWidth = document.body.clientWidth;
		 windowHeight = document.body.clientHeight;
	 }
	
	 // for small pages with total height less then height of the viewport
	 if(yScroll < windowHeight)
	 {
	 	pageHeight = windowHeight;
	 } 
	 else
	 {
	 	pageHeight = yScroll;
	 }
	
	 // for small pages with total width less then width of the viewport
	 if(xScroll < windowWidth)
	 {
	 	pageWidth = windowWidth;
	 } 
	 else 
	 {
	 	pageWidth = xScroll;
	 }
	
	 return [pageWidth,pageHeight,windowWidth,windowHeight];
}

