// ==UserScript==
// @name		Vemil info hover
// @namespace		http://www.rulac.net/userjs/vemil-info-hover/
// @description		On mouseover show product info
// @version		1.1
// @include		http://vemil.hr/*
// @include		http://*.vemil.hr/*
// @include		http://ve-mil.hr/*
// @include		http://*.ve-mil.hr/*
// ==/UserScript==

// =============================
// start of config
// =============================

// velicina prozora
opiswidth	= 630; // sirina
opisheight	= 430; // visina

// =============================
// end of config
// =============================


// malo mjenja i ubrzava mutavi ajax tako da redirekta na zgodan url (bez callback=true) http://www.vemil.hr/Cjenik.aspx?callback=true&Grp2_Simbol=cvrstidisk	
window.opera.defineMagicFunction("PrikaziCjenik", function(oldFunction, thisReference, grp2_Simbol, searchString, grp3_Id, prt_Osb_Naziv, cijena, color, sorter, ascending, leaveFilter ){
	var xrSend = "";
	if (grp2_Simbol != ""){
		xrSend = "Grp2_Simbol=" + grp2_Simbol;
	} else if (searchString != ""){
		xrSend = "searchString=" + searchString.replace("+", "%2b");
	}
	xrSend = xrSend + (grp3_Id ? ('&Grp3_Id=' + grp3_Id) : '') + (prt_Osb_Naziv ? ('&Prt_Osb_Naziv=' + prt_Osb_Naziv)  : '') + (cijena ? ('&Cijena=' + cijena)  : '') + (sorter ? ('&sorter=' + sorter) : '') + '&ascending=' + (ascending ? 'true' : 'false') + '&color=' + color;
	window.location	= "/Cjenik.aspx?" + xrSend;
});
	
// tooltips
document.addEventListener("DOMContentLoaded", function(){
	links		= document.getElementsByTagName("a");
	for (i = 0; i < links.length; i++){
		tmp1		= links[i].getAttribute("onclick");
		if (tmp1){
			links[i].tmp1	= tmp1.lastIndexOf("opis.aspx");
		}
		if (links[i].tmp1 > -1){
			links[i].onmouseover	= function(){
				tooltip("<iframe src ='" + this.getAttribute("href") +"' width='" + opiswidth + "' height='" + opisheight + "'></iframe>");	
			}
			links[i].onmouseout	= function(){
				exit();
			}
		}
	}

	// lixlpixel Javascript tooltips
	// http://lixlpixel.org/javascript-tooltips/

	// * part of code is added by Rulac

	// position of the tooltip relative to the mouse in pixel //
	var offsetx	= -30 ;
	var offsety	=  20;

	function newelement(newid){ 
		if(document.createElement){
			var el = document.createElement('div'); 
			el.id = newid;	 
			with(el.style){ 
				display		= 'none';
				position	= 'absolute';
				top		= '-20000px'; // * part of code added by Rulac
				zIndex		= '1000'; // * part of code added by Rulac
			} 
			el.innerHTML = '&nbsp;'; 
			document.body.appendChild(el); 
		} 
	} 
	var ie5		= (document.getElementById && document.all); 
	var ns6		= (document.getElementById && !document.all); 
	var ua		= navigator.userAgent.toLowerCase();
	var isapple	= (ua.indexOf('applewebkit') != -1 ? 1 : 0);
	function getmouseposition(event){
		if(document.getElementById){
			var iebody	= (document.compatMode && document.compatMode != 'BackCompat') ? document.documentElement : document.body;
			pagex		= (isapple == 1 ? 0:(ie5)?iebody.scrollLeft:window.pageXOffset);
			pagey		= (isapple == 1 ? 0:(ie5)?iebody.scrollTop:window.pageYOffset);
			mousex		= (ie5)?event.x:(ns6)?clientX = event.clientX:false;
			mousey		= (ie5)?event.y:(ns6)?clientY = event.clientY:false;
			
			var lixlpixel_tooltip		= document.getElementById('tooltip');
			lixlpixel_tooltip.style.left	= (mousex+pagex+offsetx) + 'px';
			lixlpixel_tooltip.style.top	= (mousey+pagey+offsety) + 'px';

			// * START of part code added by Rulac
			if ((window.innerWidth/2 ) < (mousex)){
				lixlpixel_tooltip.style.left	= (mousex+pagex-offsetx-opiswidth) + 'px';
			}
			if ((window.innerHeight/2) < (mousey)){
				lixlpixel_tooltip.style.top	= (mousey+pagey-offsety-opisheight) + 'px';
			}
			// * END of part code added by Rulac
		}
	}
	function tooltip(tip){
		if(!document.getElementById('tooltip')) newelement('tooltip');
		var lixlpixel_tooltip		= document.getElementById('tooltip');
		lixlpixel_tooltip.innerHTML	= tip;
		lixlpixel_tooltip.style.display	= 'block'
		document.onmousemove		= getmouseposition;
	}
	function exit(){
		document.getElementById('tooltip').style.display	= 'none';
	}

}, false);