jQuery.fn.extend({
	/*Obtener la posicion top y lef de un objeto*/
	_getXY:function(dir,obj) {
	  	var cur = 0;
		var objN=(obj!=null)? obj:document.getElementById($(this).attr("id"))
		if (objN.offsetParent){
			while (objN.offsetParent){
				if(dir=="x") cur += objN.offsetLeft
				else if(dir=="y")cur += objN.offsetTop
				objN = objN.offsetParent;
			}
		}else if (objN.x || objN.y){
			if(dir=="x")  cur += objN.x;
			if(dir=="y")  cur += objN.y;
		}
		return cur;  
	},
	getX:function() {return $(this)._getXY("x")},
	getY:function() {return $(this)._getXY("y")},
	exists:function(){ return $(this).size()>0},
	del:function(){if ($(this).parents().exists()){return $(this).empty().remove()}}, 
	urlParams:function(){
		var url=$(this).attr("href")
		return (url.indexOf("?")!=-1)? {dir:url,host:url.split("?")[0], parameters:url.split("?")[1]} : {dir:url,host:url, parameters:""}
	},
	createElem:function(tag,properties,styles,text){
		var el=document.createElement(tag)
        if(properties!=null)for (var i in properties){var v=eval("properties."+i);$(el).attr(i,v)}
		if(styles!=null)for (var x in styles){
			var w=eval("styles."+x);
			$(el).css(x,w)
		}
		if(text!=null){el.appendChild(document.createTextNode(text))}
	
		return el;   
	},
	appendElement:function(tag,properties,styles,text){$(this).append($(this).createElem(tag,properties,styles,text))},
	getCommonStyles:function(){
		//$(this)
	},
	ajaxShowPreloader:function(ids,styles,funct){
		if($("#"+ids).exists()){$("#"+ids).show()}
		else{$("#wrapper").appendElement("div",{id:ids},styles)}
		$("#"+ids).fadeTo(100, 0.25);
		if(funct)funct()
	},
	ajaxHidePreloader:function(ids){
		$("#"+ids).fadeTo(300,0,function(){$(this).del()})
	},
	ajaxGetContent:function(URL,theid,styles,funct,rewrite){
		var rewriteLay=(rewrite==null ||rewrite==true )? true:false;
		var ajaxLay=$(this)
		if(styles==null){
			ajaxLay.styles={
				position:"absolute",
				width:$(this).width()+ parseInt($(this).css("paddingRight"))+ parseInt($(this).css("paddingLeft")),
				height:$(this).height()+ parseInt($(this).css("paddingTop"))+ parseInt($(this).css("paddingBottom")),
				left:$(this).getX(),
				top:$(this).getY(),
				border:"1px solid #ccc",
				background:"#fff url(img/ico_loading.gif) no-repeat center center"
			}
		}
		//$(this).css("visibility","hidden")
		var aj=$.ajax({
	        url: URL,
	        async:true,
	        beforeSend: function(obj){ajaxLay.ajaxShowPreloader("load"+theid,ajaxLay.styles)},
	        complete: function(obj, go){
				
	            if(go=="success"){
					if(rewriteLay){
						ajaxLay.html(obj.responseText)
						ajaxLay.ajaxHidePreloader("load"+theid)
						ajaxLay.show()
						ajaxLay.css("visibility","visible")
					}else {}
					if(funct)funct()
	            }
	        },
	        error: function(objeto, quepaso, otroobj){
	            alert("Error en la peticon ajax: "+quepaso);
	        },			
	        dataType: "html",
	        type: "get"
		});
	}
	
});	

(function($){
	$.createElem2 = function(tag,properties,styles,text){
		
		var el=document.createElement(tag)
        if(properties!=null)for (var i in properties){var v=eval("properties."+i);$(el).attr(i,v)}
		if(styles!=null)for (var x in styles){
			var w=eval("styles."+x);
			$(el).css(x,w)
		}
		if(text!=null){el.appendChild(document.createTextNode(text))}
		return el;   
	}
	
})(jQuery);

(function($){

	// to track if the mouse button is pressed
	var isMouseDown    = false;
	var limits=[];

	// to track the current element being dragged
	var currentElement = null;

	// callback holders
	var dropCallbacks = {};
	var dragCallbacks = {};

	// global position records
	var lastMouseX;
	var lastMouseY;
	var lastElemTop;
	var lastElemLeft;
	
	var limits=[];

	// returns the mouse (cursor) current position
	$.getMousePosition = function(e){
		var posx = 0;
		var posy = 0;

		if (!e) var e = window.event;

		if (e.pageX || e.pageY) {
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) {
			posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop  + document.documentElement.scrollTop;
		}

		return { 'x': posx, 'y': posy };
	}

	// updates the position of the current element being dragged
	$.updatePosition = function(e) {
		var pos = $.getMousePosition(e);

		var spanX = (pos.x - lastMouseX);
		var spanY = (pos.y - lastMouseY);
		
		var xxPos=(lastElemLeft + spanX);
		var yyPos=(lastElemLeft + spanX);
		//limits
		
		
		//if(xxPos>80 && xxPos<937){
			$(currentElement).css("left", (lastElemLeft + spanX));
		//}
	}

	// when the mouse is moved while the mouse button is pressed
	$(document).mousemove(function(e){
		if(isMouseDown){
			// update the position and call the registered function
			$.updatePosition(e);
			
			if(dragCallbacks[currentElement.id] != undefined){
				dragCallbacks[currentElement.id](e);
				
			}
			
			return false;
		}
	});

	// when the mouse button is released
	$(document).mouseup(function(e){
		if(isMouseDown){
			isMouseDown = false;
			if(dropCallbacks[currentElement.id] != undefined){
				dropCallbacks[currentElement.id](e);
			}

			return false;
		}
	});

	// register the function to be called while an element is being dragged
	$.fn.ondrag = function(callback){
		return this.each(function(){
			dragCallbacks[this.id] = callback;
		});
	}

	// register the function to be called when an element is dropped
	$.fn.ondrop = function(callback){
		return this.each(function(){
			dropCallbacks[this.id] = callback;
		});
	}

	// set an element as draggable - allowBubbling enables/disables event bubbling
	$.fn.easydrag = function(allowBubbling){ //left,right//,top,bottom

		return this.each(function(){
			// if no id is defined assign a unique one
			if(undefined == this.id) this.id = 'easydrag'+time();
			
	
			// change the mouse pointer
			$(this).css("cursor", "move");

			// when an element receives a mouse press
			$(this).mousedown(function(e){			

				// set it as absolute positioned
				$(this).css("position", "absolute");

				// set z-index
				$(this).css("z-index", "10000");

				// update track variables
				isMouseDown    = true;
				currentElement = this;

				// retrieve positioning properties
				var pos    = $.getMousePosition(e);
				lastMouseX = pos.x;
				lastMouseY = pos.y;

				lastElemTop  = this.offsetTop;
				lastElemLeft = this.offsetLeft;
			
		

				$.updatePosition(e);
		
				
				return allowBubbling ? true : false;
			});
		});
	}

})(jQuery);


var Ticker={
	init:null,
	cont:1,
	tot:0,
	load:function(){
	
	
		$("#ticker").css("position","absolute")
		$("#ticker").css("left",0)
		$("#ticker").css("top",$("#ticker").getY()-10)
		$("#ticker").css("zIndex","3330")
		$("#ticker").css("width",760)
		
		$("#cticker").css("position","absolute")
		$("#cticker").css("left",0)
		$("#cticker").css("top",0)

		Ticker.tot=$("#cticker>ul>li").size()
		Ticker.init=setInterval("Ticker.move()",5000) 	
		
	
	},
	move:function(){
		
		$("#cticker").animate({top:-1*Ticker.cont*25},1000)
		Ticker.cont++
		if(Ticker.tot==Ticker.cont){
			Ticker.cont=0
			$("#cticker").animate({top:-1*Ticker.cont*25+25},10)
		}
	}
}
var popUp={
    windowWidth:0,
   	windowHeight:0,
   	bodyWidth:0,
   	bodyHeight:0,
   	xWin:0,
   	yWin:0,
   	xWin_min:0,
   	yWin_min:0,
	load:function(){
		$(".viewvideox").each(function(i){
			$(this).bind("click", function(){
				
				popUp.xWin = popUp.xWin_min + document.documentElement.scrollTop;
                popUp.yWin = popUp.yWin_min + document.documentElement.scrollLeft;
                
				popUp.open($(this).attr("href"));
				return false;
			})
		})
	},
	getSizes:function(){
		if(window.innerHeight){
			popUp.windowWidth=window.innerWidth;
			popUp.windowHeight=window.innerHeight;
		}else if(document.documentElement && document.documentElement.clientHeight){
			popUp.windowWidth=document.documentElement.clientWidth;
			popUp.windowHeight=document.documentElement.clientHeight;
		}else if (document.body){
			popUp.windowWidth=document.body.clientWidth;
			popUp.windowHeight=document.body.clientHeight;
		}
		popUp.widthBody=$("body").width()
		popUp.heightBody=$("body").height()
	},
	open:function(url){
		if(!$("#pop1").exists()){
			popUp.create(url)			
		}
	},
	close:function(){
		$("#pop1").del();
		$("#wrappop1").del();
		$("#wrappop2").del();
	}, 
	create:function(_url){
		popUp.getSizes()
		var widthWin=780;
		var heightWin=600;
		var topWin=((popUp.windowHeight-heightWin)/2)+document.documentElement.scrollTop;
		var leftWin=((popUp.windowWidth-widthWin)/2)+document.documentElement.scrollLeft;
		var borderWin=10;
		var borderColWin="#000";
		var opacityWin=0.8;
		
		var wrappop=$.createElem2("div",
			{id:"wrappop1"},
			{width:"100%",height:popUp.heightBody+"px",backgroundColor:borderColWin,position:"absolute",left:"0px",top:"0px",zIndex:3000,opacity:0.5}
		)
		var wrappop2=$.createElem2("div",
			{id:"wrappop2"},
			{width:"0px",height:"0px",backgroundColor:"#333",position:"absolute",left:leftWin+"px",top:topWin+"px",zIndex:3001}
		)
		var pop=$.createElem2("div",
			{id:"pop1"},
			{width:widthWin+ "px",height:heightWin+"px",backgroundColor:"#999",position:"absolute",left:parseInt(leftWin+borderWin)+"px" ,top:parseInt(topWin+borderWin)+"px",zIndex:3002}
		)
		var ifrPop=$.createElem2("iframe",
			{id:"ifrPop1",marginwidth:"0",marginheight:"0",frameBorder:"0",scrolling:"no",width:widthWin,height:heightWin},
			{padding:"0",margin:"0"}
		)

		ifrPop.src="iframe.html?id="+new Date()
		
		$("body").append(wrappop)
		$("body").append(wrappop2)
		$(pop).append(ifrPop)
		$("body").append(pop)
		
		$("#pop1").fadeTo(1000, 1);
		//$("#wrappop1").fadeTo(1000, 0.5);
		$("#wrappop2").fadeTo(1000, 1);
		ifrPop.src=_url
		
	}
	
}
var TinyPopUp={
		xWin_min:15,
    yWin_min:70,
    xWin:500,
    yWin:585,
    widthWin:488,
    heightWin:85,
    borderWin:10,
		opacityWin:0.8,
    borderColWin:'#000',
		load:function(automatic){
			$(".viewvideox").each(function(i){
				$(this).bind("click", function(){
        	/* NOTA de Santi a Santi: Esto se podría sacar en un método redim.*/
          TinyPopUp.xWin = TinyPopUp.xWin_min + document.documentElement.scrollTop;
          TinyPopUp.yWin = TinyPopUp.yWin_min + document.documentElement.scrollLeft;
					TinyPopUp.open($(this).attr("href"));
         	return false;
				})
			})
		},
		setValues:function(xWin,yWin,widthWin,heightWin,borderWin,opacityWin,borderColWin){
			if(xWin!=null){TinyPopUp.xWin=xWin}
			if(yWin!=null){TinyPopUp.yWin=yWin}
			if(widthWin!=null){TinyPopUp.widthWin=widthWin}
			if(heightWin!=null){TinyPopUp.heightWin=heightWin}
			if(borderWin!=null){TinyPopUp.borderWin=borderWin}
			if(opacityWin!=null){TinyPopUp.opacityWin=opacityWin}
			if(borderColWin!=null){TinyPopUp.borderColWin=borderColWin}
		},
		open:function(url){
    	if(!$("#pop1").exists()){
				TinyPopUp.create(url);
        return false;
			}
		},
		close:function(){
    	$("#pop1").del();
			$("#wrappop1").del();
		},
		create:function(_url){
			var topWin=TinyPopUp.xWin;
			var leftWin=TinyPopUp.yWin;
			var wrappop=$.createElem2("div",
				{id:"wrappop1"},
				{width:parseInt(TinyPopUp.widthWin+TinyPopUp.borderWin*2)+ "px",height:parseInt(TinyPopUp.heightWin+TinyPopUp.borderWin*2) + "px",backgroundColor:TinyPopUp.borderColWin,position:"absolute",left:leftWin+"px",top:topWin+"px",zIndex:100}
			)
			var pop=$.createElem2("div",
				{id:"pop1"},
				{width:TinyPopUp.widthWin+ "px",height:TinyPopUp.heightWin+"px",backgroundColor:"#999",position:"absolute",left:parseInt(leftWin+TinyPopUp.borderWin)+"px" ,top:parseInt(topWin+TinyPopUp.borderWin)+"px",zIndex:102}
			)
			var ifrPop=$.createElem2("iframe",
				{id:"ifrPop1",marginwidth:"0",marginheight:"0",frameBorder:"0",scrolling:"no",width:TinyPopUp.widthWin,height:TinyPopUp.heightWin},
				{padding:"0",margin:"0"}
			)
			ifrPop.src="iframe.html"
			$("body").append(wrappop)
			$(pop).append(ifrPop)
			$("body").append(pop)
			$("#pop1").fadeTo(1000, 1);
			$("#wrappop1").fadeTo(1000, TinyPopUp.opacityWin);
			ifrPop.src=_url
		}
}