$(document).ready(
	function()
	{

///////////////////////////////
// funciones manejo pantalla
///////////////////////////////

function overlaySize(){
  if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {
    yScroll = window.innerHeight + window.scrollMaxY;
    xScroll = window.innerWidth + window.scrollMaxX;
    var deff = document.documentElement;
    var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
    var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
    xScroll -= (window.innerWidth - wff);
    yScroll -= (window.innerHeight - hff);
  } else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
    yScroll = document.body.scrollHeight;
    xScroll = document.body.scrollWidth;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    yScroll = document.body.offsetHeight;
    xScroll = document.body.offsetWidth;
    }
};

function getPageScrollTop(){
  var yScrolltop;
  var xScrollleft;
  if (self.pageYOffset || self.pageXOffset) {
    yScrolltop = self.pageYOffset;
    xScrollleft = self.pageXOffset;
  } else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){   // Explorer 6 Strict
    yScrolltop = document.documentElement.scrollTop;
    xScrollleft = document.documentElement.scrollLeft;
  } else if (document.body) {// all other Explorers
    yScrolltop = document.body.scrollTop;
    xScrollleft = document.body.scrollLeft;
  }
  arrayPageScroll = new Array(xScrollleft,yScrolltop)
  return arrayPageScroll;
};

function getPageSize(){
  var de = document.documentElement;
  var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
  arrayPageSize = new Array(w,h)
  return arrayPageSize;
};

///////////////////////////////

		jQuery.fn.newPopup = function(settings){
			var id;
			id=jQuery.fn.newPopup.init(settings);
			jQuery.fn.newPopup.show(id);
			//alert(id);
			if (!settings.close)
				window.timeout_id=setTimeout("$('#"+id+"').fadeOut('slow')",15000);
		};


		jQuery.fn.newPopup.init = function (settings)
		{
				var id,x,y,w,h,m,l,r,c;

				id=settings.id? true : false;				//popup id
				x=settings.x? true : false;					//x position
				y=settings.y? true : false;					//y position
				w=settings.width? true : false;			//width
				h=settings.height? true : false;		//height
				m=settings.margin? true : false;		//margin: initial margin from screen
				l=settings.label? true : false;			//label: title of new popup
				r=settings.resize? true : false;		//1: is resizable, 0: not resizable
				t=settings.text? true : false;			//text: content to display in popup
				c=settings.close? true: false;			//automatically close
				ifra=settings.iframe? true: false;			//iframe

				//id selected from user or dynamically created
				if (id) id=settings.id;
				else {
					var currentTime = new Date();
					id = 'popup'+currentTime.getTime();
				}

				if (w)
					w=settings.width;
				else w=200; //default

				if (h)
					h=settings.height;
				else h=200; //default

				if (m)
					m=settings.margin;
				else m=10;	//default


				if (l)
					l=settings.label;
				else l="Popup - defecto";

				if (r==false)
				  r=true;

				if (t)
					t=settings.text;
				else t="";

				if (ifra)
					ifra=settings.iframe;
				else ifra="";
				
				if (c==false)
					c=true;


				arrayPageScroll=getPageScrollTop();
				pagesize=getPageSize();

				////create popup object

				var buildsettings = {'id':id,'label':l,'resize':r,'text':t,'iframe':ifra};
				jQuery.fn.newPopup.build(buildsettings);



				////set current position

				if (x) {
					switch (settings.x){
						case 'left': 		$('#'+id).css("left", 0 + m);
												 		break;

						case 'center': 	$('#'+id).css("left", (arrayPageScroll[0] + (pagesize[0] - w)/2)+"px");
													 	break;

						case 'right': 	$('#'+id).css("left", (arrayPageScroll[0] + (pagesize[0] - w) - m)+"px");
														break;

						default: 				$('#'+id).css("left",settings.x);
										 				break;
					}
				}
				else { //default settings
					$('#'+id).css("left", (arrayPageScroll[0] + (pagesize[0] - w)/2)+"px");
				}

				if (y) {
					switch (settings.y){
						case 'top': 		$('#'+id).css("top", 0 + m);
												 		break;

						case 'middle': 	$('#'+id).css("top", (arrayPageScroll[1] + (pagesize[1]-h)/2)+"px");
													 	break;

						case 'bottom': 	$('#'+id).css("top", (arrayPageScroll[1] + (pagesize[1]-h) - m)+"px");
														break;

						default: 				$('#'+id).css("top",settings.y);
										 				break;
					}
				}
				else { //default settings
					$('#'+id).css("top", (arrayPageScroll[1] + (pagesize[1] - h)/2)+"px");
				}



				var offsetX,offsetY;

				$('#'+id).attr("defaultwidth",w);
				$('#'+id).attr("defaultheight",h);
				$('#'+id).attr("minwidth",w);
				$('#'+id).attr("minheight",h);
				$('#'+id).attr("dragging",0); // 0 == false
				$('#'+id).attr("draggingRes",0); // 0 == false
				$('#'+id).attr("offsetX",0);
				$('#'+id).attr("offsetY",0);




				$('#'+id).css('width',w+"px");
				$('#'+id).css('height',h+"px");


				$('#'+id+'Content').css('width',(w-20)+"px");
				$('#'+id+'Content').css('height',(h-50)+"px");


				$('#'+id+'Close').bind(
					'click',
					function()
					{
						clearTimeout(window.timeout_id);
						$('#'+id).fadeOut("slow");
					}
				);


				$('#'+id+'Max').bind(
					'click',
					function()
					{
						$('#'+id).css("height",$('#'+id).attr("defaultheight")+"px");
						$('#'+id+'Max').hide();
						$('#'+id+'Min').show();
					}
				);


				$('#'+id+'Min').bind(
					'click',
					function()
					{
						//alert(jQuery.fn.popup.defaults['id']);
						$('#'+id).css("height","30px");
						$('#'+id+'Min').hide();
						$('#'+id+'Max').show();
					}

				);


				function pressed(event) {
			    // Capture mouse
			    if (0==$('#'+id).attr('dragging')) {

			      $(document).bind('mousemove', move).bind('mouseup', unpressed);
			      $('#'+id).attr('dragging',1);
			    }

			    //alert($('#'+id).attr('dragging'));
			    left=$('#'+id).css("left").split("px")[0];
					top_1=$('#'+id).css("top").split("px")[0];

					offsetX=$('#'+id).attr('offsetX');
					offsetY=$('#'+id).attr('offsetY');

					offsetX = event.pageX - left;
					offsetY = event.pageY - top_1;

					$('#'+id).attr('offsetX',offsetX);
					$('#'+id).attr('offsetY',offsetY);
			    return false;
			  }

				function unpressed (event) {
			    // Uncapture mouse
			    $(document).unbind('mousemove', move);
			    $(document).unbind('mouseup', pressed);
			    $('#'+id).attr('dragging',0);
			  }


				function move (event){
					var offsetX,offsetY,calcX,calcY;

					offsetX=$('#'+id).attr('offsetX');
					offsetY=$('#'+id).attr('offsetY');

					calcX=event.pageX-offsetX;
					calcY=event.pageY-offsetY;

					$('#'+id).css({
						left: calcX + 'px',
						top: calcY + 'px'
					})
				}


				$('#'+id+'Top').mousedown(pressed);


			//////////////////////////////
			//         Resize
			//////////////////////////////

				function pressedRes (event) {
			    // Capture mouse
			    if (0==$('#'+id).attr('draggingRes')) {
			      $(document).bind('mousemove', moveRes).bind('mouseup', unpressedRes);
			      $('#'+id).attr('draggingRes',1);
			    }

			    width=$('#'+id).css("width").split("px")[0];
					height=$('#'+id).css("height").split("px")[0];

					offsetW = event.pageX - width;
					offsetH = event.pageY - height;

			    return false;
			  }


				function unpressedRes (event) {
			    // Uncapture mouse
			    $(document).unbind('mousemove', moveRes);
			    $(document).unbind('mouseup', pressedRes);
			    $('#'+id).attr('draggingRes',0);
			  }


				function moveRes(event){
					var left,top_1,calcW,calcH;

					calcW=event.pageX-offsetW;
					calcH=event.pageY-offsetH;

					if (calcW>$('#'+id).attr('minwidth')){
						$('#'+id).css({
							width: calcW
						})

						$('#'+id+'Content').css({
							width: (calcW-20) + 'px'
						})
					}


					if(calcH>$('#'+id).attr('minheight')){
						$('#'+id).css({
							height: calcH + 'px'
						})

						$('#'+id).attr('defaultheight',calcH);

						$('#'+id+'Content').css({
							height: (calcH-50) + 'px'
						})
					}
				}

				$('#'+id+'Res').mousedown(pressedRes);


				return id;
		}; //init


		jQuery.fn.newPopup.build = function (settings){
		    ///vamos a aņadir la logica para eliminar la ventana que ya existe si tiene el mismo id
			$("#"+settings.id).remove();
			///////////////////////////////////////////////////////////////////////////////////////
			/// aņadimos gestion de iframes
			if (settings.iframe!=""){
			//con iframe

			var result='<div style="z-index:100000;display: none;" id="'+settings.id+'" class="ventana">'
		  	+'<div id="'+settings.id+'Top" class="ventanaTop">'
		  	+'<div id="'+settings.id+'Protect"></div>'
		  	+'<div id="'+settings.id+'TopContent" class="ventanaTopContent">'+settings.label+'</div>'
		  	+'<img src="/images/window_min.gif" id="'+settings.id+'Min" class="ventanaMin">'
		  	+'<img src="/images/window_max.gif" id="'+settings.id+'Max" class="ventanaMax">'
		  	+'<img src="/images/window_close.gif" id="'+settings.id+'Close" class="ventanaClose">'
		  	+'</div>'
			+'<iframe id="'+settings.id+'Content" class="ventanaContent" src="'+settings.iframe+'" width="'+settings.width+'" scrolling="auto" frameborder="0"></iframe>';
		  	if (settings.resize==true)
		  		result+='<img src="/images/window_resize.gif" id="'+settings.id+'Res" class="ventanaRes">';
		  	result+='</div>';


			}else{
			//sin iframe
			var result='<div style="z-index:100000;display: none;" id="'+settings.id+'" class="ventana">'
		  	+'<div id="'+settings.id+'Top" class="ventanaTop">'
		  	+'<div id="'+settings.id+'Protect"></div>'
		  	+'<div id="'+settings.id+'TopContent" class="ventanaTopContent">'+settings.label+'</div>'
		  	+'<img src="/images/window_min.gif" id="'+settings.id+'Min" class="ventanaMin">'
		  	+'<img src="/images/window_max.gif" id="'+settings.id+'Max" class="ventanaMax">'
		  	+'<img src="/images/window_close.gif" id="'+settings.id+'Close" class="ventanaClose">'
		  	+'</div>'
		  	+'<div id="'+settings.id+'Content" class="ventanaContent">'
		  	+settings.text
		  	+'</div>';
		  	if (settings.resize==true)
		  		result+='<img src="/images/window_resize.gif" id="'+settings.id+'Res" class="ventanaRes">';
		  	result+='</div>';
            }
		  	$(document.body).append(result);
		}


		jQuery.fn.newPopup.show = function (id){
				$('#'+id).fadeIn("slow");
		};

		jQuery.fn.newPopup.hide = function (id){
				$('#'+id).fadeOut("slow");
		};


	}

);

