﻿function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


var addEvent = function(o, t, f, l)
{
    var d = 'addEventListener';
    if (o[d]  && !l) return o[d](t, f, false);
    if (!o._evts) o._evts = {};
    if (!o._evts[t])
    {
    var n = 'on' + t;
    o._evts[t]  = o[n] ? { b: o[n] } : {};
    o[n] = new  Function('e', 'return addEvent._c(e||window.event, this, this._evts["' + t + '"])');
    }
    if (!f._i) f._i = addEvent._i++;
    o._evts[t][f._i] = f;
    if (t != 'unload')  addEvent(window, 'unload', function() {removeEvent(o, t, f, l)});
    }

    addEvent._i  = 1;
    addEvent._c = function(e, o, a)
    {
    var r = true, i;
    for (i in a) { if (a[i]._i) {o._f  = a[i]; r = o._f(e) != false &&  r; o._f = null;} }
    return  r;
} 
    
        
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 cancelEvent(e)
{
  if(!e)
    e = window.event;
  if(e.stopPropagation)
    e.stopPropagation();
  if(e.preventDefault)
    e.preventDefault();
  e.cancelBubble = true;
  e.cancel = true;
  e.returnValue = false;
  return false;
}


var langEventInitialized = false;

function showMenu(ev,id)
{
    document.getElementById(id).style.visibility='visible';     
    cancelEvent(ev);
}

function hideMenu(ev,id)
{
    document.getElementById(id).style.visibility='hidden';     
}

function chooseLanguage(ev)
{       

    cancelEvent(ev);
    var box = document.getElementById("LangSelect");          

    if (box.style.visibility != "hidden")
        box.style.visibility = "hidden";
    else
        box.style.visibility = "";

    if (!langEventInitialized)
    {
        langEventInitialized = true;
        addEvent(document,'click',function(e){
        
		    var target = document.getElementById("LangSelect");                      					
		    pos = findPos(target);			   			    
		    
		    var isInVer = pos[1] <= e.clientY && pos[1] + target.offsetHeight >= e.clientY;
		    var isInHor = pos[0] <= e.clientX && pos[0] + target.offsetWidth >= e.clientX;
		    if(!isInHor || !isInVer)
		    {
		       //alert("cursor: " + e.clientX + "," + e.clientY + "   obj: " + pos[0] + "," + pos[1] );
		        box.style.visibility = "hidden";
			    
		    }
        });
    }
    return false;
}    


function downloading(id, inviteFriends)
{
    document.getElementById('img_dl_' + id).src='http://www.tracktesting.com/images/skin_white3/shop_download_free_downloaded.gif';
    if (inviteFriends)
    document.getElementById('div_invitefriends').style.display='block';    
}







var cbb = {
	init : function() {
	// Check that the browser supports the DOM methods used
		if (!document.getElementById || !document.createElement || !document.appendChild) return false;
		var oElement, oOuter, oI1, oI2, tempId;
	// Find all elements with a class name of cbb
		var arrElements = document.getElementsByTagName('*');
		var oRegExp = new RegExp("(^|\\s)cbb(\\s|$)");
		for (var i=0; i<arrElements.length; i++) {
	// Save the original outer element for later
			oElement = arrElements[i];
			if (oRegExp.test(oElement.className)) {
	// 	Create a new element and give it the original element's class name(s) while replacing 'cbb' with 'cb'
				oOuter = document.createElement('div');
				oOuter.className = oElement.className.replace(oRegExp, '$1cb$2');
	// Give the new div the original element's id if it has one
				if (oElement.getAttribute("id")) {
					tempId = oElement.id;
					oElement.removeAttribute('id');
					oOuter.setAttribute('id', '');
					oOuter.id = tempId;
				}
	// Change the original element's class name and replace it with the new div
				oElement.className = 'i3';
				oElement.parentNode.replaceChild(oOuter, oElement);
	// Create two new div elements and insert them into the outermost div
				oI1 = document.createElement('div');
				oI1.className = 'i1';
				oOuter.appendChild(oI1);
				oI2 = document.createElement('div');
				oI2.className = 'i2';
				oI1.appendChild(oI2);
	// Insert the original element
				oI2.appendChild(oElement);
	// Insert the top and bottom divs
				cbb.insertTop(oOuter);
				cbb.insertBottom(oOuter);
			}
		}
	},
	insertTop : function(obj) {
		var oOuter, oInner;
	// Create the two div elements needed for the top of the box
		oOuter=document.createElement("div");
		oOuter.className="bt"; // The outer div needs a class name
	    oInner=document.createElement("div");
	    oOuter.appendChild(oInner);
		obj.insertBefore(oOuter,obj.firstChild);
	},
	insertBottom : function(obj) {
		var oOuter, oInner;
	// Create the two div elements needed for the bottom of the box
		oOuter=document.createElement("div");
		oOuter.className="bb"; // The outer div needs a class name
	    oInner=document.createElement("div");
	    oOuter.appendChild(oInner);
		obj.appendChild(oOuter);
	},
	// addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
	addEvent : function(obj, type, fn) {
		if (obj.addEventListener)
			obj.addEventListener(type, fn, false);
		else if (obj.attachEvent) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
			obj.attachEvent("on"+type, obj[type+fn]);
		}
	}
};

cbb.addEvent(window, 'load', cbb.init);