var popupInitialized = 0;
var overupCounter = 1; // not 0 to avoid showing first item twice!
var overupTimer;
var overupTimerOn = 0;
var glImages;

var overupXoffset = 20;
var overupYoffset = 20;

jQuery(document.body).append(jQuery(document.createElement('div')).attr('id','overUp').css('display','none'));

var overUpObj = jQuery("#overUp");
var overUpSelector = false;

function initializePopup() {
	if (popupInitialized == 0 && document.getElementById("overUp") != undefined) {
		var newContent = '<div id="overUpContent"></div>';
		document.getElementById("overUp").innerHTML = newContent;
		document.getElementById("overUp").style.display = 'block';
		popupInitialized = 1;
	}
}

function overupClear() {
	overupElements = jQuery(".overupTooltip");
	if (overupElements.length > 0) {
		overupElements.removeAttr("title");
		var innerImage = jQuery(overupElements).find("img");
		if (innerImage.length > 0) {
			innerImage.removeAttr("title");
			innerImage.removeAttr("alt");
		}
	}

	return false;
}

function overup(element,class_name,title,text,image,width,xoffset,yoffset) {

	initializePopup();

	element.title = '';
	overUpSelector = jQuery(element);
	overUpSelector.bind("mousemove",function(e){ moveOverup(e); });

	if(title==undefined || title==null) {
		var title = false;
	}
	if(text==undefined || text==null) {
		var text = false;
	}
	if(image==undefined || image==null) {
		var image = false;
	}
	if(class_name==undefined || class_name==null) {
		var class_name = 'overUpDefault';
	}
	if(width==undefined || width==null) {
		var width = '200px';
	}
	if(xoffset==undefined || xoffset==null) {
		var xoffset = 20;
	}
	if(yoffset==undefined || yoffset==null) {
		var yoffset = 20;
	}

	overupXoffset = xoffset;
	overupYoffset = yoffset;

	var overUpElement = document.getElementById("overUp");

	if (overUpElement != undefined) {

		overUpElement.className = class_name;
		overUpElement.style.visibility='visible';

		var content = '';

		if(title!=false && title!="") {

			content += '<div class="title">' + title + '</div>';

		} else {

			overUpElement.style.width = width;

		}

		if(text!=false && text!="") {

			content += '<div class="text">' + text + '</div>';

		}

		if(image!=false && image!="") {

			glImages = image;

			if (jQuery.isArray(image)) {

				content += '<div class="image"><img src="' + imageUrl(glImages[0]) +  '" id="overUpImage" /></div>';

				if (glImages.length > 1) {

					overupLoop();

				}

			} else {

				content += '<div class="image"><img src="' + imageUrl(glImages) +  '" /></div>';

			}
		}

		if (document.getElementById("overUpContent") != undefined) {

			document.getElementById("overUpContent").innerHTML = content;

		}

	}

}

function imageUrl(str) {

	return 'http://www.admeen.com/images/' + str +  '';
}

function nd() {

	if (overUpSelector) {
		overUpSelector.unbind("mousemove");
	}

	if (document.getElementById("overUp") != undefined) {

		document.getElementById("overUp").style.visibility='hidden';

	}

	clearTimeout(overupTimer);
	overupTimerOn = 0;
	overupCounter = 1;

}

function overupAnimate() {

	document.getElementById("overUpImage").src = imageUrl(glImages[overupCounter]);

	if (overupCounter == (glImages.length-1)) {
		overupCounter = 0;
	} else {
		overupCounter = overupCounter+1;
	}

	overupTimer=setTimeout("overupAnimate();",1000);
}

function overupLoop() {
	if (!overupTimerOn) {
		overupTimerOn=1;
		overupTimer=setTimeout("overupAnimate();",1000);
	}
}

function moveOverup(e) {
	if (overUpObj.is(":visible")) {

		var options = {
			maxWidth:300,
			offset:{
				cursor:{
					x:overupXoffset,
					y:overupYoffset
				},
				window:{
					x:5,
					y:30
				}
			}
		}

		if(options.maxWidth && overUpObj.width()>options.maxWidth) {
			overUpObj.width(options.maxWidth);
		}

		var popup = [], browser = [], position = [], scroll = [], cursorOffset = [], wndw = jQuery(window);

		popup['width'] = overUpObj.outerWidth();
		popup['height'] = overUpObj.outerHeight();
		popup['left'] = 0;
		popup['top'] = 0;
		browser['width'] = wndw.width();
		browser['height'] = wndw.height();
		position['x'] = 0;
		position['y'] = 0;
		scroll['top'] = wndw.scrollTop();
		scroll['left'] = wndw.scrollLeft();

		if(document.all) {
			position['x']=event.clientX+options.offset.cursor.x;
			position['y']=event.clientY+options.offset.cursor.y;
		} else {
			position['x']=e.clientX+options.offset.cursor.x;
			position['y']=e.clientY+options.offset.cursor.y;
		}

		position['x'] = position['x']+scroll['left'];
		position['y'] = position['y']+scroll['top'];

		if ( position['x'] < (browser['width'] + scroll['left'] - popup['width']) ) {
			popup['left'] = position['x'];
		} else {
			popup['left'] = browser['width']+scroll['left']-(popup['width']+options.offset.window.x);
		}

		if ( position['y'] < (browser['height'] + scroll['top'] - popup['height']) ) {
			popup['top'] = position['y'];
		} else {
			popup['top'] = position['y']-(popup['height']+options.offset.window.y);
		}

		overUpObj.css({ 'left':popup['left'], 'top':popup['top'] });
	}
}

jQuery(function(){
	overupClear();
});
