//
// LinkMyPics - The Hotlink Advantage Maximizer
// Copyright ©2008 Michael VandeMar
// All Rights Reserved
// http://www.funscripts.net/javascript-widgets/
// 
// Freeware
//

var interupt=true;
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}	return [curleft,curtop];
}

function applyEvent(theObj, theEvent, theFunction){
	if(window.addEventListener){ // Mozilla, Netscape, Firefox
		theObj.addEventListener(theEvent, theFunction, false);
	}else{ // IE
		theObj.attachEvent('on' + theEvent, theFunction);
	}
}

function closeIt(){
	try{
		var curr = document.getElementById("curropenhl2l");
	}catch(e){
	}
	if(curr){
		curr.parentNode.removeChild(curr);
	}
	interupt = true;
}

function useHTML(tcode){
	var ta = document.getElementById("hl2lta");
	ta.value = tcode;
	var r1 = document.getElementById("hl2lr1");
	r1.style.backgroundColor = "#ffffff";
	var r2 = document.getElementById("hl2lr2");
	r2.style.backgroundColor = "#86C81A";
	ta.select();
	return true;
}

function useBBCODE(tcode){
	var ta = document.getElementById("hl2lta");
	ta.value = tcode;
	var r2 = document.getElementById("hl2lr2");
	r2.style.backgroundColor = "#ffffff";
	var r1 = document.getElementById("hl2lr1");
	r1.style.backgroundColor = "#86C81A";
	ta.select();
	return true;
}

function hl2l(e){
	var rightclick;
	if (!e) var e = window.event;
	if (e.which) rightclick = (e.which == 3);
	else if (e.button) rightclick = (e.button == 2);
	if(!rightclick) return true;

	closeIt();

	var tw = 360;
	var th = 186;

	var thisele;
	if(!(typeof event == "undefined")){ 	// Internet Explorer
		thisele = event.srcElement;
		th = th - 10;
	}else if(this){ // Netscape and Firefox
		//thisele = this;
		thisele = e.currentTarget;
	}

	var tpage = document.location.href;
	var tsrc = thisele.getAttribute("src");
	if(tsrc.substr(0,4).toLowerCase()!="http"){
		var base;		
		if(tpage.substr(tpage.length - 1,1)=="/"){
			base = tpage;
		}else if(tpage.indexOf("/",9)==-1){
			base = tpage + "/";
		}else{
			base = tpage.substr(0,tpage.lastIndexOf("/")+1);
		}
		var root = base.substr(0,base.indexOf("/",9));
		if(tsrc.substr(0,1)!="/"){
			tsrc = base + tsrc;
		}else{
			tsrc = root + tsrc;
		}
	}
	else
	{
	lastpos=tsrc.indexOf("images/");	
	base=tsrc.substring(0,lastpos);
	}
	var talt = thisele.getAttribute("alt");
	
	//The below code has been added by Karthikeyan R on 10-Dec-08
	if(talt == '') {
		talt = thisele.getAttribute("title");
	}

	if(talt){
		talt = talt.replace("'", "");
	}else{
		talt = "";
	}

//alert(base);

	thtml = "<a href='"+tpage+"'><img src='"+tsrc+"' alt='"+talt+"'></a>";
	tbbcode = "[url="+tpage+"][img]"+tsrc+"[/img][/url]";

	var nd = document.createElement("div");
	nd.style.width = tw+"px";
	nd.style.height = th+"px";
	//nd.style.border = "1px solid #000000";
	nd.style.position = "absolute";
	nd.style.fontSize = "11px";
	nd.style.fontFamily = "Verdana";
	nd.style.padding = "2px";
	nd.style.margin = "2px";
	
	
	
	texy = findPos(thisele);
	nd.style.left = 150 +((texy[0] + (thisele.offsetWidth/2)) - (tw/2))+"px";
	nd.style.top = 20+((texy[1] + (thisele.offsetHeight/2)) - (th/2))+"px";
	//The below code has been added by Karthikeyan R on 10-Dec-08
	if(((texy[1] + (thisele.offsetHeight/2)) - (th/2)) < 0) nd.style.top = "0px";
	nd.style.backgroundColor = "#fff";
	nd.setAttribute("id", "curropenhl2l");

// Added by Murugan for round corners
	
	var leftcorner=document.createElement("div");
	leftcorner.setAttribute("id", "lefttopcornerdiv");
	leftcorner.style.cssFloat = "left";
	leftcorner.style.styleFloat = "left";
	leftcorner.style.backgroundImage="url('"+document.getElementById("ARX_SiteURL_txt").value+"images/left_top_corner_trans.png')";	
	leftcorner.style.backgroundRepeat="no-repeat";
	leftcorner.style.width=25+"px";
	leftcorner.style.height=29+"px";
	leftcorner.style.display="inline";
	leftcorner.style.zIndex=1;
	leftcorner.style.position="absolute";
	leftcorner.style.top=-1+"px";
	leftcorner.style.left=-2+"px";
	nd.appendChild(leftcorner);
	
	var topborderdiv=document.createElement("div");
	topborderdiv.setAttribute("id", "topborder_div");
	topborderdiv.style.width = (tw-27)+"px";	
	topborderdiv.style.height = 10+"px";		
	topborderdiv.style.backgroundImage="url('"+document.getElementById("ARX_SiteURL_txt").value+"images/top_middle.gif')";	
	topborderdiv.style.backgroundRepeat="repeat-x";
	topborderdiv.style.display="inline";
	topborderdiv.style.zIndex=1;
	topborderdiv.style.position="absolute";
	topborderdiv.style.left=15+"px";
	topborderdiv.style.top=-1+"px";
	nd.appendChild(topborderdiv);
	
	var leftbt_corner=document.createElement("div");
	leftbt_corner.setAttribute("id", "leftbottomcornerdiv");
	leftbt_corner.style.cssFloat = "left";
	leftbt_corner.style.styleFloat = "left";
	leftbt_corner.style.backgroundImage="url('"+document.getElementById("ARX_SiteURL_txt").value+"images/left_bottom_corner_trans.png')";	
	leftbt_corner.style.backgroundRepeat="no-repeat";
	leftbt_corner.style.width=25+"px";
	leftbt_corner.style.height=29+"px";
	leftbt_corner.style.display="inline";
	leftbt_corner.style.zIndex=1;
	leftbt_corner.style.position="absolute";
	leftbt_corner.style.bottom=-11+"px";
	leftbt_corner.style.left=-2+"px";
	nd.appendChild(leftbt_corner);
	
	var leftborderdiv=document.createElement("div");
	leftborderdiv.setAttribute("id", "leftborder_div");
	leftborderdiv.style.width = 5+"px";	
	leftborderdiv.style.height = (th-30)+"px";		
	leftborderdiv.style.backgroundImage="url('"+document.getElementById("ARX_SiteURL_txt").value+"images/left_middle.gif')";	
	leftborderdiv.style.backgroundRepeat="repeat-y";
	leftborderdiv.style.display="inline";
	leftborderdiv.style.zIndex=1;
	leftborderdiv.style.position="absolute";
	leftborderdiv.style.left=-1+"px";
	leftborderdiv.style.top=18+"px";	
	leftborderdiv.style.bottom=10+"px";	
	nd.appendChild(leftborderdiv);
	
	
	var right_corner=document.createElement("div");
	right_corner.setAttribute("id", "righttopcornerdiv");
	right_corner.style.cssFloat = "right";
	right_corner.style.styleFloat = "right";
	right_corner.style.backgroundImage="url('"+document.getElementById("ARX_SiteURL_txt").value+"images/top_right_corner_trans.png')";	
	right_corner.style.backgroundRepeat="no-repeat";
	right_corner.style.width=25+"px";
	right_corner.style.height=29+"px";
	right_corner.style.display="inline";
	right_corner.style.zIndex=1;
	right_corner.style.position="absolute";
	right_corner.style.top=-2+"px";
	right_corner.style.right=-7+"px";
	nd.appendChild(right_corner);
	
	
	var rightborderdiv=document.createElement("div");
	rightborderdiv.setAttribute("id", "righborder_div");
	rightborderdiv.style.width = 10+"px";	
	rightborderdiv.style.height = (th-30)+"px";		
	rightborderdiv.style.backgroundImage="url('"+document.getElementById("ARX_SiteURL_txt").value+"images/right_border.gif')";	
	rightborderdiv.style.backgroundRepeat="repeat-y";
	rightborderdiv.style.display="inline";
	rightborderdiv.style.zIndex=1;
	rightborderdiv.style.position="absolute";
	rightborderdiv.style.right=-1+"px";
	rightborderdiv.style.top=18+"px";	
	rightborderdiv.style.bottom=10+"px";	
	nd.appendChild(rightborderdiv);
	
	var rightbottom_corner=document.createElement("div");
	rightbottom_corner.setAttribute("id", "rightbottomcornerdiv");
	rightbottom_corner.style.cssFloat = "right";
	rightbottom_corner.style.styleFloat = "right";
	rightbottom_corner.style.backgroundImage="url('"+document.getElementById("ARX_SiteURL_txt").value+"images/right_bottom_corner_trans.png')";	
	rightbottom_corner.style.backgroundRepeat="no-repeat";
	rightbottom_corner.style.width=25+"px";
	rightbottom_corner.style.height=29+"px";
	rightbottom_corner.style.display="inline";
	rightbottom_corner.style.zIndex=1;
	rightbottom_corner.style.position="absolute";
	rightbottom_corner.style.bottom=-11+"px";
	rightbottom_corner.style.right=-7+"px";
	nd.appendChild(rightbottom_corner);
	
	var bottomborderdiv=document.createElement("div");
	bottomborderdiv.setAttribute("id", "bottomborder_div");
	bottomborderdiv.style.width = (tw-27)+"px";	
	bottomborderdiv.style.height = 10+"px";		
	bottomborderdiv.style.backgroundImage="url('"+document.getElementById("ARX_SiteURL_txt").value+"images/bottom_middle.gif')";	
	bottomborderdiv.style.backgroundRepeat="repeat-x";
	bottomborderdiv.style.display="inline";
	bottomborderdiv.style.zIndex=1;
	bottomborderdiv.style.position="absolute";
	bottomborderdiv.style.left=15+"px";
	bottomborderdiv.style.bottom=-1+"px";
	nd.appendChild(bottomborderdiv);
	
//

	//instructions and "close" button
	var ndic = document.createElement("div");
	ndic.style.width = tw+"px";	
//	alert(navigator.appName);
	if(navigator.appName=="Netscape")
	ndic.style.height = (th-135)+"px";	
	else
	ndic.style.height = (th-125)+"px";	
	
	var ndc = document.createElement("div");
	ndc.style.cssFloat = "right";
	ndc.style.styleFloat = "right";
	ndc.style.zIndex=2;
	ndc.style.position="absolute";
	ndc.style.top=((th+3)-th)+"px";
	ndc.style.right=((tw+5)-tw)+"px";	
	ndc.style.border = "1px solid #444444";
	ndc.style.backgroundColor = "#86C81A";	
	ndc.style.width = 35+"px";	
	ndc.style.cursor = "pointer";
	ndc.innerHTML = "<B>Close</B>";
	ndic.appendChild(ndc);
	
	applyEvent(ndc, "click", closeIt);
	var ndi = document.createElement("div");
	ndi.style.width = (tw - 50)+"px";
	ndi.style.height = (th-135)+"px";	
	ndi.style.zIndex=2;
	ndi.style.position="absolute";
	ndi.style.top=((th+6)-th)+"px";
	ndi.style.left=((tw+5)-tw)+"px";	
	
	ndi.innerHTML = "<font color='#FF8632'><b> If you wish to use this image elsewhere, please select either HTML (for webpages) or BBCODE (for forums) and use the code provided:</b></font><br><br>";
	
	ndic.appendChild(ndi);

	nd.appendChild(ndic);

	var r1 = document.createElement("span");
	r1.style.width = 35+"px";
	r1.style.border = "1px solid black";
	r1.style.padding = "1px";
	r1.style.backgroundColor = "#ffffff";
	r1.style.cursor = "pointer";
	r1.style.marginLeft = "3px";
	r1.innerHTML = "<B>HTML</B>";
	r1.setAttribute("id", "hl2lr1");
	applyEvent(r1, "click", function(){useHTML(thtml)});

	var s1 = document.createElement("span");
	s1.innerHTML = "&nbsp;";
	s1.style.width = 10+"px";

	var b1 = document.createElement("br");
	var b2 = document.createElement("br");
	var r2 = document.createElement("span");
	r2.style.width = 35+"px";
	r2.style.border = "1px solid black";
	r2.style.padding = "1px";
	r2.style.backgroundColor = "#86C81A";
	r2.style.cursor = "pointer";
	r2.innerHTML = "<b>BBCODE</B>";
	
	r2.setAttribute("id", "hl2lr2");
	applyEvent(r2, "click", function(){useBBCODE(tbbcode)});

	//nd.appendChild(b1);
	nd.appendChild(r1);
	nd.appendChild(s1);
	nd.appendChild(r2);
	nd.appendChild(b2);

	var ta = document.createElement("textarea");
	ta.setAttribute("cols", "40");
	ta.setAttribute("rows", "5");
	ta.setAttribute("wrap", "soft");
	ta.setAttribute("id", "hl2lta");
	ta.style.margin = "2px";
	applyEvent(ta, "mousemove", function(){document.getElementById("hl2lta").select()});

	nd.appendChild(ta);

	document.body.appendChild(nd);

	r1.checked = true;
	useHTML(thtml);

	thisele.oncontextmenu = function(){if(interupt){interupt = false;return false;};};
	nd.oncontextmenu = function(){if(interupt){interupt = false;return false;};};
}
