/*
*
* Version: 1.3.1
*
* Requires: jQuery 1.2+
* Requires: jQuery.Dimensions 1.2+
* 
*/

(function($) {

$.hcvPrueba = {
  open:function(){alert('Open');}, 
  close:function(){alert('Close');}

}


/* Extension de funciones */
$.fn.hcvModalShow = function(){
  alert('Funciono!!');
  $.hcvPrueba.open();
  $.hcvPrueba.close();
}

$.fn.hcvScrollY = function(){
    return Math.max(document.body.scrollTop,document.documentElement.scrollTop);
}

$.huecuvu = {
		
  //**************************************
  //	VENTANA MODAL
  //**************************************
  modalOpen:function(){
    $.huecuvu.modalOpen('');
  },
  
  modalClose:function(){
    $.huecuvu.modalClose('');
  },
  
  modalOpen:function(ventana){
    $("body").append("<div id=\"jqHuecuvuModal\" style=\"background-color:#000000; position: absolute; left: 0; top: 0; z-index: 3000; filter: alpha(opacity=50); opacity: 0.5; cursor: wait;\" />");
    $('#jqHuecuvuModal').height( $(document).height() );
    $('#jqHuecuvuModal').width( $(window).width() );
    $('#'+ventana).css({
      display: "",
      position: "absolute",
      top: (($(window).height()-$('#'+ventana).height())/2) + $(window).scrollTop() ,
      left: ($(window).width()-$('#'+ventana).width())/2 
    });
    $('#'+ventana).css('z-index', "3001");
    
    //Agego el evento "Scroll" al window para que posicione el div en el centro cuando hay scroll
    $(window).scroll(function () { 
      //alert($(window).scrollTop());
      $('#'+ventana).css({
        top: (($(window).height()-$('#'+ventana).height())/2) + $(window).scrollTop()
      }); 
    });
    
  },
  
  modalClose:function(ventana){
    //Elimino los eventos "scroll" del window
    $(window).unbind( "scroll" );
    
    $('#'+ventana).css({display:"none"});
    $('#jqHuecuvuModal').remove();
  },

  //**************************************
  //	TABS
  //**************************************
  tabs:function(id){
	$("#"+id).children("ul").children().each(function(index, domElement){
				
		$(domElement).children().click(function() {
			$(domElement).parent().children().each(function(index2, domElement2){
				$(domElement2).children()[0].className="tab-nav";
				$($(domElement2).children()[0].hash).css("display", "none");
			});
				  
			this.className="tab-selected";
			$(this.hash).css("display", "");
		});
	});
  },
  
  //**************************************
  //	TOOLTIP
  //**************************************
  tooltip:function(id){
  
  	$("#" + id + " img").wrap('<a href="#"></a>');
  
  	$("#" + id + " a").mouseover(function(){ $.huecuvu.tooltipShow(id)} );
  	$("#" + id + " a").mouseout(function(){ $.huecuvu.tooltipHidden(id)} );
	
  
  	$("#" + id + " div").css({
	  display: "none",
	  position: "absolute",
	  width: "200px"
	  });	
  },
  
  tooltipShow:function(id){
  
  	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var hasArea = w - getAbsoluteLeft(id);
	
	var tooltip_width = $("#" + id + " div").width();
	var tooltip_top = getAbsoluteTop(id) + 10;
	
	if(hasArea > tooltip_width) {
		var tooltip_left = getAbsoluteLeft(id) + 22;
	
	} else {
		var tooltip_left = $("#" + id)[0].offsetLeft - tooltip_width - 5;
	
	}
  
  	$("#" + id + " div").css({
	  display: "",
      top: tooltip_top,
      left: tooltip_left
	  });
  },
  
  tooltipHidden:function(id){
  	$("#" + id + " div").css({
	  display: "none"
	  });
  },
  
  //**************************************
  //	minipulacion
  //**************************************
  
  elementHidden:function(id){
  	$("#" + id).css({ display: "none" });
  },
  
  elementShow:function(id, x, y){
  	$("#" + id).css({
	  display: "",
      top: y,
      left: x
	  });
  }
}

})(jQuery);


function getAbsoluteLeft(objectId) {
	// Get an object left position from the upper left viewport corner
	o = document.getElementById(objectId)
	oLeft = o.offsetLeft            // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent    // Get parent object reference
		oLeft += oParent.offsetLeft // Add parent left position
		o = oParent
	}
	return oLeft
}

function getAbsoluteTop(objectId) {
	// Get an object top position from the upper left viewport corner
	o = document.getElementById(objectId)
	oTop = o.offsetTop            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oTop += oParent.offsetTop // Add parent top position
		o = oParent
	}
	return oTop
}
