/*
 * JTip
 * By Cody Lindley (http://www.codylindley.com)
 * Under an Attribution, Share Alike License
 * JTip is built on top of the very light weight jquery library.
 */

//on page load (as soon as its ready) call JT_init
$(document).ready(JT_init);

function JT_init(){
    $(".jTip").each(function() {
        $(this).hover(
            function(){
                JT_show(this.href, this.id, this.name, "")
            }, 
            function(){ 
                $('#JT').remove();
                /*$('#JT').fadeTo("fast",0, function() {
                    $('#JT').each(function() {$(this).remove();});
                });*/
            }
        );
    
        /*$(this).click(function(){return false});*/
    });
    
    $(".jTipError").each(function() {
        $(this).hover(
            function(){
                JT_show(this.href, this.id, this.name, "jTipError")
            }, 
            function(){ 
                $('#JT').fadeTo("fast",0, function() {
                    $('#JT').each(function() {$(this).remove();});
                });
            }
        );
    });
}

function JT_show(url, linkId, title, sClass){
    
	
	var de              = document.documentElement;
	var w               = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var hasArea         = w - getAbsoluteLeft(linkId);
	var clickElementy   = getAbsoluteTop(linkId) + ($('#' + linkId).height()-25); //set y position
	
	var queryString     = (url !== undefined) ? url.replace(/^[^\?]+\??/,'') : "";
	var params          = parseQuery(queryString);
	
	if(params['width'] === undefined)
	    params['width'] = 75;
	    
	if(params['link'] !== undefined){
	    $('#' + linkId).bind('click',function(){window.location = params['link']});
	    $('#' + linkId).css('cursor','pointer');
	}
	
	if(params['show_Title'] !== undefined) {
	    if(title != false) {
	        title = "<div id='JT_close'>" + title + "</div>";
	    } else {
	        title = "";
        }
	} else {
	    title = ""; 
    }
    
	if ($('#JT')) {
        $('#JT').remove();
	}
	
	var jTipDiv         = document.createElement('div')
	var jTipArrow       = document.createElement('div')

    $(jTipArrow).html('<div class="line20"></div>'
                    + '<div class="line19"></div>'
                    + '<div class="line18"></div>'
                    + '<div class="line17"></div>'
                    + '<div class="line16"></div>'
                    + '<div class="line15"></div>'
                    + '<div class="line14"></div>'
                    + '<div class="line13"></div>'
                    + '<div class="line12"></div>'
                    + '<div class="line11"></div>'
                    + '<div class="line10"></div>'
                    + '<div class="line9"></div>'
                    + '<div class="line8"></div>'
                    + '<div class="line7"></div>'
                    + '<div class="line6"></div>'
                    + '<div class="line5"></div>'
                    + '<div class="line4"></div>'
                    + '<div class="line3"></div>'
                    + '<div class="line2"></div>'
                    + '<div class="line1"></div>');

    $(jTipDiv).attr("id", "JT");
    
    if (sClass != "")
        $(jTipDiv).addClass(sClass);

    //$(jTipDiv).append(jTipArrow);
    $(jTipDiv).append(title
                 + "<div id='JT_copy'>"
                 + "    <div class='JT_loader'><div>"
                 + "</div>");
                 
    $("body").append(jTipDiv);
	
	$(jTipDiv).css({width: (params["width"] * 1) + "px"});

	if(hasArea > ((params['width'] * 1) + 75)){
	    //right side
	    $(jTipArrow).addClass("JT_arrow_left");

		var arrowOffset     = getElementWidth(linkId) + 8;
		var clickElementx   = getAbsoluteLeft(linkId) + 10; //set x position

	} else {
	    
	    //left side
	    $(jTipArrow).addClass("JT_arrow_right");
	    //$(jTipArrow).css({left: ((params['width'] * 1) + 1) + "px"});
		   
		var clickElementx   = getAbsoluteLeft(linkId) - ((params['width'] * 1) + 20);  //set x position
	}
	
	if(params['ajax_content'] !== undefined)
	    $('#JT_copy').load(url);
	else {
	    $('#JT_copy').html($("#" + linkId).attr("jTip_content"));
	}
	
	$('#JT').css({left: clickElementx + "px", top: clickElementy + "px"});
	$('#JT').fadeIn("slow");
}

function getElementWidth(objectId) {
	x = document.getElementById(objectId);
	
	return x.offsetWidth;
}

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
}

function parseQuery ( query ) {
   var Params = new Object ();
   if ( ! query ) return Params; // return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}

function blockEvents(evt) {
              if(evt.target){
              evt.preventDefault();
              }else{
              evt.returnValue = false;
              }
}
