/*
Plugin Name: Skinnytip Tooltips
Plugin URI: http://www.ebrueggeman.com/skinnytip
Description: Easy JavaScript tooltip generator for your pages and posts. 
Version: 1.03
Author: Elliott Brueggeman
Author URI: http://www.ebrueggeman.com
Please see readme.txt version for more information.
*/
/*
Note: Above version number pertains directly to the Wordpress plugin.
This file is Skinnytip Core Version: 2.0
*/
/*  Copyright 2008  Elliott Brueggeman  (email : ebrueggeman@gmail.com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3. 

License on the web: http://www.gnu.org/licenses/gpl-3.0.txt

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
//Call mouse capture handler function on page load
captureMouse();

//CUSTOM VARS - Initialized below
var v_divname;
var v_text;
var v_title;
var v_xoffset;
var v_yoffset;
var v_backcolor;
var v_bordercolor;
var v_textcolor;
var v_titletextcolor;
var v_width;
var v_border;
var v_title_padding;
var v_content_padding;
var v_fontface;
var v_fontsize;
var v_titlefontsize;

//INTERNAL VARIABLES
var v_xcoordinate = 0;
var v_ycoordinate = 0;
var v_visible = 0;
var v_havemouse = 0;
var v_layer = null;

function tooltip(displaytext) {
	//Reset variables for this tool tip
	init_tooltip();
	
	//Title and Text
	//v_title=title;
	v_text=displaytext;
	
	//Parse commands if any
	//parseCommands(commands);
	
/*
	if (v_layer) { 
		v_layer=getLayer(v_divname); 
	}
*/	
	if (!(v_layer=createDivContainer())) { 
		return false;
	}
	
	mainMethod();
}

function init_tooltip() {
	
	v_divname = 'tiplayer';
	v_text = 'Default Text';
	v_title = '';
	
	//UI Variables
	v_xoffset = 15;
	v_yoffset = 15;
	//v_backcolor = '#FFFFCC';
	v_backcolor = '#ececec';
	v_bordercolor = '#d8d8d8';
	v_textcolor = '#000000';
	v_titletextcolor = '#000000';
	v_width = 'auto';
	
	v_border = 1;
	v_title_padding = '1px';
	v_content_padding = '1px 3px';
	
	//v_fontface = 'Arial, Helvetica, Sans-Serif';
	v_fontface = 'Times, Verdana, Georgia, serif;';
	v_fontsize = 14;
	v_titlefontsize = 14;
	
	//SYSTEM VARIABLES
	v_visible = 0;
	v_layer = null;
}

function parseCommands(commands) {
	if (commands != null) {
		var comArray = commands.split(',');
		for (var i = 0; i < comArray.length; i++) {
			var args = comArray[i].split(':');
			eval('v_' + trimWhitespace(args[0]) + '="' + trimWhitespace(args[1]) + '"');
		}
	}
}

// Clears popups if appropriate
function hide() {
	if (v_visible == 1) {
		if (v_layer != null) {
			v_layer.style.visibility = 'hidden';
			v_visible = 0;
		}
	}
	return true;
}

function mainMethod() {	
	v_visible = 0;
	
	var html = makeHTML(v_text);	
	createPopup(html);
	
	//if we have mouse coordinates, position layer and make visible
	if (v_havemouse == 1) {	
		positionLayer();
		v_visible = 1;
		v_layer.style.visibility = 'visible';
	}
}

function makeHTML(text) {
	
	//var container_style = 'width:' + v_width + 'px;';
	var container_style = 'width:' + v_width + ';';
	container_style += 'border:' + v_border + 'px solid ' + v_bordercolor + ';';
	container_style += 'background-color:' + v_backcolor + ';';
	container_style += 'font-family:' + v_fontface + ';';
	container_style += 'font-size:' + v_fontsize + 'px;';

	//var content_style = 'padding:' + v_content_padding + ';';
	//content_style += 'color:' + v_textcolor + ';';
	var content_style = 'padding: 15px 15px 15px 15px;';

	var imagepath = 'http://www.veintemundos.com/magazines/wp-content/themes/mytheme/';

	var txt = '<div id="skinnytip_container" style="' + container_style + '">';
	
	text = editText(text);
	
	if(text.length == 0 || text == null){
		text = 'Text missing';
	}

	txt += '<div id="skinnytip_content" style="' + content_style + '">' + text + '</div>';
	txt += '</div>';
	
	return txt;
}


function editText(text){

	var txt = trimWhitespace(text);
	if(txt == null || txt.length == 0){
		return '';
	}

	//split text
	var wordlist = new Array();
	wordlist = improved_split(txt);
	
	var html = '';
	var counter = 0;

	word = wordlist[counter++];
	while(counter < wordlist.length){ //while there are words in array
		if(word.toString() == 'NEWLINE:' || word.toString() == 'newline:'){
			html += '<br />';	//line break
		}else if(word.toString() == 'PARAGRAPH:' || word.toString() == 'paragraph:'){
			html += '<p></p>';	//new paragraph
		}else if(word.toString() == 'BOLD:' || word.toString() == 'bold:'){
			html += '<b>';
		}else if(word.toString() == ':BOLD' || word.toString() == ':bold'){
			html += '</b>';
		}else if(word.toString() == 'ITALIC:' || word.toString() == 'italic:'){
			html += '<i>';
		}else if(word.toString() == ':ITALIC' || word.toString() == ':italic'){
			html += '</i>';
		}else if(word.toString() == 'UNDERLINE:' || word.toString() == 'underline:'){
			html += '<span style="text-decoration:underline;">';
		}else if(word.toString() == ':UNDERLINE' || word.toString() == ':underline'){
			html += '</span>';
		}else if(word.toString() == 'RED:' || word.toString() == 'red:'){ //definition color
			html += '<span style="color:#610B0B;">';
		}else if(word.toString() == ':RED' || word.toString() == ':RED'){ //end color
			html += '</span>';
		}else if(word.toString() == 'BLUE:' || word.toString() == 'BLUE:'){ //word color
			html += '<span style="color:#0B0B61;">';
		}else if(word.toString() == ':BLUE' || word.toString() == ':BLUE'){ //end color
			html += '</span>';
		}else if(word.toString() == 'SMALL:' || word.toString() == 'small:'){
			html += '<span style="font-size: 13px;">';
		}else if(word.toString() == ':SMALL' || word.toString() == ':small'){
			html += '</span>';
		}else if(word.toString() == 'BIG:' || word.toString() == 'big:'){
			html += '<span style="font-size: 17px;">';
		}else if(word.toString() == ':BIG' || word.toString() == ':big'){
			html += '</span>';
		}else{ 
			html += word + ' ';
		}
		word = wordlist[counter++];
	}
	//add last word of html code	
	if(word.toString() == 'NEWLINE:' || word.toString() == 'newline:'){
		html += '<br />';	//line break
	}else if(word.toString() == 'PARAGRAPH:' || word.toString() == 'paragraph:'){
		html += '<p></p>';	//new paragraph
	}else if(word.toString() == 'BOLD:' || word.toString() == 'bold:'){
		html += '<b>';
	}else if(word.toString() == ':BOLD' || word.toString() == ':bold'){
		html += '</b>';
	}else if(word.toString() == 'ITALIC:' || word.toString() == 'italic:'){
		html += '<i>';
	}else if(word.toString() == ':ITALIC' || word.toString() == ':italic'){
		html += '</i>';
	}else if(word.toString() == 'UNDERLINE:' || word.toString() == 'underline:'){
		html += '<span class="style: text-decoration:underline;">';
	}else if(word.toString() == ':UNDERLINE' || word.toString() == ':underline'){
		html += '</span>';
	}else if(word.toString() == 'RED:' || word.toString() == 'RED:'){ //definition color
		html += '<span style="color:#610B0B;">';
	}else if(word.toString() == ':RED' || word.toString() == ':RED'){ //end color
		html += '</span>';
	}else if(word.toString() == 'BLUE:' || word.toString() == 'BLUE:'){ //word color
		html += '<span style="color:#0B0B61;">';
	}else if(word.toString() == ':BLUE' || word.toString() == ':BLUE'){ //end color
		html += '</span>';
	}else if(word.toString() == 'SMALL:' || word.toString() == 'small:'){
		html += '<span style="font-size: 13px;">';
	}else if(word.toString() == ':SMALL' || word.toString() == ':small'){
		html += '</span>';
	}else if(word.toString() == 'BIG:' || word.toString() == 'big:'){
		html += '<span style="font-size: 17px;">';
	}else if(word.toString() == ':BIG' || word.toString() == ':big'){
		html += '</span>';
	}else{ html += word;}

	return html;	
}

function improved_split(text){
	
	var list = new Array();

	var counter = 0, index = 0;
	var c = ''; //char
	var word = '';

	c = text.charAt(counter++);
	word += c;
	while(counter < text.length){

		if(c == ' ' || c == '\t' || c == '\n' ||
			 c == '\r' || c == '\b'){
			//trim word
			word = trimWhitespace(word);
			//store word in array
			if(word != null && word.length > 0){
				list[index++] = word.toString();
				word = ''; //to empty string
			}
		}

		c = text.charAt(counter++);
		word += c;
	}
	//check for last word
	word = trimWhitespace(word);
	if(word != null && word.length > 0){
		list[index++] = word;
	}
	return list;
}

function trimWhitespace(str) {  
	while(str.charAt(0) == (" ") ) {  
		str = str.substring(1);
	}
	while(str.charAt(str.length-1) == " " ) {  
		str = str.substring(0,str.length-1);
	}
	return str;
}

//Positions popup according to mouse input
function positionLayer() {
	
	var placeX = 300;
	var placeY = 300;
	
	//get final placement
	placeX = horizontalPlacement();
	placeY = verticalPlacement();
	
	//Move the object
	v_layer.style.left = placeX + 'px';
	v_layer.style.top = placeY + 'px';
}

//called when the mouse moves
//sets mouse related variables
function mouseMoveHandler(e) {
	if (!e) {
		e = event;
	}
	if (e.clientX) {
	 //if there is an x pos property
	 //GET MOUSE LOCATION
		v_xcoordinate = mouseX(e);
		v_ycoordinate = mouseY(e);	
		v_havemouse = 1;
	}
	if (v_visible == 1) { 
		positionLayer();	
	}
}

//get mouse x coordinate
function mouseX(evt) {
	if (evt.pageX) return evt.pageX;
	else if (evt.clientX) {
	   return evt.clientX + (document.documentElement.scrollLeft ?
	   document.documentElement.scrollLeft :
	   document.body.scrollLeft);
	}
	else {
		return null;
	}
}

//get mouse y coordinate
function mouseY(evt) {
	if (evt.pageY) { 
		return evt.pageY; 
	}
	else if (evt.clientY) {
	   return evt.clientY + (document.documentElement.scrollTop ?
	   document.documentElement.scrollTop :
	   document.body.scrollTop);
	}
	else { 
		return null;
	}
}

//Set mouse handler
function captureMouse() {
	document.onmousemove = mouseMoveHandler;
}

//Creates the popup
function createPopup(input) {

	var popupwidth = v_width;
	var text;
	var zindex;

	text =  createBackLayer(popupwidth,zindex++);
	text += '<div style="position: absolute; top: 0; left: 0; width: ' + popupwidth + 'px; z-index: ' + zindex + ';">' + input + '</div>';
	
	if (typeof v_layer.innerHTML != 'undefined') {
		v_layer.innerHTML = text;
	} 
	
	//After writing html measure height of backlayer to set height of iframe
	var backlayer=self.document.getElementById("backdrop");
	var container=self.document.getElementById("skinnytip_container");
	backlayer.height = container.offsetHeight;
}

//Back layer prevents forms from showing through popups
function createBackLayer(width, Z) {
	//Create backdrop with 0 height
	return '<iframe id="backdrop" frameborder="0" scrolling="no" width="' + width + '" height="0" style="z-index: ' + Z + '; filter: Beta(Style=0,Opacity=0);background-color: transparent !important; opacity:0; filter:alpha(opacity=0);"><p></iframe>';
}

//get horizontal box placement
function horizontalPlacement() {
	placeX = v_xcoordinate + v_xoffset;
	return placeX;
}

//get vertical box placement
function verticalPlacement() {
	return v_ycoordinate + v_yoffset;
}

// create the div container for popup content if it doesn't exist
function createDivContainer() {
	var divContainer = self.document.getElementById(v_divname);
	return divContainer;
}

function trimWhitespace(str) {  
	while(str.charAt(0) == (" ") ) {  
		str = str.substring(1);
	}
	while(str.charAt(str.length-1) == " " ) {  
		str = str.substring(0,str.length-1);
	}
	return str;
}

