//	if (navigator.userAgent.indexOf("Safari") > -1) {alert("Using safari...");}

function getUserAgentLowerCase(){ return navigator.userAgent.toLowerCase(); }

function isGecko1() { var ua = getUserAgentLowerCase(); return (ua.indexOf("gecko/")!=-1); };

function isWin2k() { var ua = getUserAgentLowerCase(); return (ua.indexOf("windows nt 5.0")!=-1); };
function isWinXP() { var ua = getUserAgentLowerCase(); return (ua.indexOf("windows nt 5.1")!=-1); };
function isWin98() { var ua = getUserAgentLowerCase(); return (ua.indexOf("windows 98") > -1 && ua.indexOf("win 9x") == -1); };
function isWinME() { var ua = getUserAgentLowerCase(); return (ua.indexOf("windows 98") > -1 && ua.indexOf("win 9x") > -1); };

getViewportWidth = function() {
/* 
	These functions need to be tweaked!
*/	
  var width = 0;
  if( document.documentElement && document.documentElement.clientWidth ) {
    width = document.documentElement.clientWidth;
  }
  else if( document.body && document.body.clientWidth ) {
    width = document.body.clientWidth;
  }
  else if( window.innerWidth ) {
    width = window.innerWidth - 18;
  }
  return width;
};

getViewportHeight = function() {
  var height = 0;
  if( document.documentElement && document.documentElement.clientHeight ) {
    height = document.documentElement.clientHeight;
  }
  else if( document.body && document.body.clientHeight ) {
    height = document.body.clientHeight;
  }
  else if( window.innerHeight ) {
    height = window.innerHeight - 18;
  }
  return height;
};

function hasPlatformQueryStr(platformStr) {
	var hasQueryStr = document.URL.indexOf('?p=');
	var pVal;
	if (hasQueryStr != -1) {
		pVal = document.URL.substring(hasQueryStr+3);
	}

	if (pVal == platformStr) {
		return true;
	}
	else {
		return false;
	}
}

// mishmash  of multiple js libraries from mobile...
function yg_addEvt(o,e,f,c) { 
	if(o.addEventListener)o.addEventListener(e,f,c);else if(o.attachEvent)o.attachEvent("on"+e,f);else eval("o.on"+e+"="+f);
}

// function from yg_mail to deal with x-browser objects 
function yg_getObj(id,d) {
  var i,x;  if(!d) d=document; 
  if(!(x=d[id])&&d.all) x=d.all[id]; 
  for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][id];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=yg_getObj(id,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(id); 
  return x;
}

// ygPos from Frontend DHTML library
ygPos=new function()
{this.getPos=function(oEl)
{var ua=navigator.userAgent.toLowerCase();var isIE=false;var isGecko=false;if(ua.indexOf('msie 6')!=-1||ua.indexOf('msie 5.5')!=-1)
isIE=true;else if(ua.indexOf('gecko')!=-1&&ua.indexOf('safari')<0)
isGecko=true;var pos=[oEl.offsetLeft,oEl.offsetTop];var parent=oEl.offsetParent;var tmp={x:null,y:null};if(parent!=oEl)
{while(parent)
{if(isIE)
{if(parent.currentStyle.position=='relative'&&oEl.currentStyle.width=='auto'&&!(oEl.currentStyle.position=='relative'||oEl.currentStyle.position=='absolute'))
{return[oEl.offsetLeft,oEl.offsetTop];}
else if(parent.currentStyle.width!='auto')
{tmp.x=parseInt(parent.currentStyle.borderLeftWidth);tmp.y=parseInt(parent.currentStyle.borderTopWidth);if(!isNaN(tmp.x))pos[0]+=parseInt(tmp.x);if(!isNaN(tmp.y))pos[1]+=parseInt(tmp.y);}}
else if(isGecko)
{var dv=document.defaultView;if(dv.getComputedStyle(parent,'').getPropertyValue('position')=='relative')
{tmp.x=parseInt(dv.getComputedStyle(parent,'').getPropertyValue('border-left-width'));tmp.y=parseInt(dv.getComputedStyle(parent,'').getPropertyValue('border-top-width'));if(!isNaN(tmp.x))pos[0]+=tmp.x;if(!isNaN(tmp.y))pos[1]+=tmp.y;}}
pos[0]+=parent.offsetLeft;pos[1]+=parent.offsetTop;parent=parent.offsetParent;}}
return pos;},this.getX=function(oEl)
{return this.getPos(oEl)[0];}
this.getY=function(oEl)
{return this.getPos(oEl)[1];}
this.setPos=function(oEl,aPos)
{if(oEl.offsetWidth)
{var oOffsetParent=oEl.offsetParent;while(oOffsetParent.tagName!='HTML'&&oOffsetParent.tagName!='BODY')
{if(aPos[0]!==null)aPos[0]-=oOffsetParent.offsetLeft;if(aPos[1]!==null)aPos[1]-=oOffsetParent.offsetTop;oOffsetParent=oOffsetParent.offsetParent;}}
oEl.style.position='absolute';if(aPos[0]!==null)oEl.style.left=aPos[0]+'px';if(aPos[1]!==null)oEl.style.top=aPos[1]+'px';}
this.setX=function(oEl,iX)
{this.setPos(oEl,[iX,null]);}
this.setY=function(oEl,iY)
{this.setPos(oEl,[null,iY]);}}

// function returns event location as array variable iLoc
function getloc(v) {
var ix, iy;
if (!v) var v = window.event;
// pageXY preferred method but only supported by N4 and N6 and, uh...
if (v.pageX || v.pageY) {
    ix = v.pageX; iy = v.pageY;
}
else if (v.clientX || v.clientY) {
    // documentElement required for IE with DOCTYPE declaration, otherwise "body" works fine
    ix = v.clientX+document.documentElement.scrollLeft; iy = v.clientY + document.documentElement.scrollTop;
}
// return ix and iy as array variable iLoc
var iLoc = Array(1);
iLoc[0] = ix;
iLoc[1] = iy; 
return iLoc;
}

// function that moves the popup
function ymobpopup(k) {
	var trapperdiv = document.getElementById("trapper");
	var srcEl = (k.srcElement) ? k.srcElement : this; // ie:gecko
	
	if (srcEl.nodeType == 3) {srcEl = k.target.parentNode;}
		
	var popupVal = srcEl.previousSibling.innerHTML;
	var srcFld = srcEl.parentNode.parentNode.parentNode.firstChild.firstChild.id;
	var destFld = document.getElementById('popmidbox');	

	if ( (typeof trapperdiv.style.opacity != "undefined") || (typeof trapperdiv.style.filter != "undefined") ) {
		trapperdiv.style.display = "block";
		trapperdiv.style.zindex = 100;
			
		trapperdiv.style.height = getViewportHeight();
		trapperdiv.style.width = getViewportWidth();
	
		trapperdiv.style.opacity = 0;
		trapperdiv.style.filter = "alpha(opacity=0)";
	}
	
// define element to be moved
	
	var oBox = yg_getObj('fpopuPpSvcYmob');
	// get coordinates array a
	var a = getloc(k);
	
	var newX=0, newY=0;
	
	if( (a[0]+140) > getViewportWidth()) {newX = a[0]+140;}
	if( (a[1]+140) > getViewportHeight()) {newY = a[1]+140;}
	
	if (newX > 0) {a[0]=a[0]-125-100;} else {a[0]=a[0]-125;}
	
	a[1]=a[1];

	destFld.innerHTML = popupVal;

	oBox.style.display="block";
	oBox.style.position="absolute";
	oBox.style.zindex = 200;

	ygPos.setPos(oBox, a);

	displayDetails(srcFld);
}

//function that closes the popup
function ymobclspop() {
var oBox = yg_getObj('fpopuPpSvcYmob');
oBox.style.display="none";
}

// function to add eventlisteners to every link with a certain class name
// variables: oClaz='A' class name; fFunc=function to attach
function connectevents(oClaz,fFunc) {
	var oA = document.getElementsByTagName('a');
	for(i=0;i<oA.length;i++) {
		if (oA[i].className==oClaz) {
			var a_id = oClaz+i;
			oA[i].setAttribute('id', a_id);
			yg_addEvt(yg_getObj(a_id),'click',fFunc,false);
			}
		}
}

function showitemdetail(content) {
	
	var rtcol = document.getElementById("rightcol");
//	var itemid = document.getElementById("itemdetail");

	var itemid = document.getElementById("fpopuPpSvcYmob");
	var trapperdiv = document.getElementById("trapper");

	document.documentElement.style.height = "100%";
	document.body.style.height = "100%";
	document.documentElement.style.overflow = "hidden";
	document.body.style.overflow = "hidden";

	itemid.innerHTML = content;
	
	trapperdiv.style.zindex = 100;
	trapperdiv.style.height = "100%";
	trapperdiv.style.width = getViewportWidth();
	trapperdiv.style.opacity = 1;
	trapperdiv.style.filter = "alpha(opacity=100)";
	trapperdiv.style.display = "block";
	
	itemid.style.display = "block";
	itemid.style.zindex = 200;
	
	
	return;	
}

function hideitemdetail() {
	var itemid = document.getElementById("fpopuPpSvcYmob");
	var trapperdiv = document.getElementById("trapper");
	itemid.style.display = "none";
	trapperdiv.style.display = "none";
	return;	
}

function roundCorners(obj) {
	if (!obj)
		var obj = document.body;

	for (var c = 0; c < obj.childNodes.length; c++) {
		var o = obj.childNodes[c];
		
		// do leaves before the branch
		if (o.id != 'debug' && o.childNodes)
			roundCorners(o);

		if (o.nodeType == 1) {
			if (o.className.indexOf('rounded') != -1) {
				// round the corners using our css tricks
				o.innerHTML += '<span class="tl"></span><span class="tr"></span><span class="bl"></span><span class="br"></span>';

				// this is to compensate for lazy folks
				// who forget to set this in CSS
				o.style.position = 'relative';
			}
		}
	}
}

function moveBoxes() {
	var topNode = document.getElementById("available");
	var cards = topNode.childNodes;
	var numChildren = cards.length;

	for (x=0; x<numChildren;++x) 
	{
		var s = cards[x].firstChild.firstChild.firstChild;
		var relStateNode = cards[x].childNodes[2].firstChild.childNodes[1];
		
		if (s && s.className == "uptodate") {
			if (relStateNode && relStateNode.className == "relstatenew") {
				relStateNode.style.display = "none";
			}
			var t = topNode.removeChild(cards[x]);
			topNode.appendChild(t);
			
			--x;
			--numChildren;
		}
	}

	var bookmark = null;
	
	for(x = 0; x < numChildren; ++x)
	{
		var s = cards[x].firstChild.firstChild.firstChild;
		if (!s || (s.className != "updateavailable") ) {
			bookmark = cards[x];
			break;
		}
	}
	
	if(bookmark)
	{
		for(x =0 ; x<numChildren; ++x)
		{
			var s = cards[x].firstChild.firstChild.firstChild;
			var relStateNode = cards[x].childNodes[2].firstChild.childNodes[1];
			
			if (s && s.className == "updateavailable") {
				var t = topNode.removeChild(cards[x]);
				topNode.insertBefore(t, bookmark);
				if (relStateNode && relStateNode.className == "relstatenew") {
					relStateNode.style.display = "none";
				}				
			}
			--x;
			--numChildren;						
		}
	}
	
	for (x=0;x<numChildren;++x) 
	{
		var relStateNode = cards[x].childNodes[2].firstChild.childNodes[1];
		if(relStateNode && relStateNode.className == "relstatebeta") {
			var t = topNode.removeChild(cards[x]);
			topNode.appendChild(t);
			
			--x;
			--numChildren;						
		}
	}
			
	var asie = document.getElementById("Yahoo_Search_for_Internet_Explorer");
	if (asie) {
		var asieNode = asie.parentNode.parentNode;
		var t = topNode.removeChild(asieNode);
		topNode.appendChild(asieNode);
	}
	
}

function hideAutoSearch() {
	var asie = document.getElementById("Yahoo_Search_for_Internet_Explorer");
	if (asie) {
		var asieNode = asie.parentNode.parentNode;
		asieNode.style.display = "none";
	}
}

function doResize() {
	var outerbox = document.getElementById("container");
	var right = document.getElementById("rightcol");
	var left = document.getElementById("leftcol");
	var midcol = document.getElementById("midcol");

	var oBox = yg_getObj('fpopuPpSvcYmob');
	
	try {
		
		if(oBox.style.display != "none"){oBox.style.display = "none";}
		midcol.style.display = "none";
		right.style.display = "none";
		midcol.style.height = (outerbox.clientHeight) + "px";
		right.style.width = (outerbox.clientWidth - 310) + "px";
		midcol.style.display = "block";
		right.style.display = "block";
	}    
	catch(e){}            
}

window.onresize = doResize;

function onLoad()
{
	roundCorners();
	doResize();
	connectevents("fcaniSvcYmob",ymobpopup);
	if(document.all && !hasPlatformQueryStr("mac") && !hasPlatformQueryStr("unix") ) { 
		IsControlInstalled();
		moveBoxes(); 
	}
	if (isGecko1()) {
		hideAutoSearch();	
	}
}

