		
		function writeFlashOverlay(source){//writes flash content to page. The source should be the URL to the SWF
		// <![CDATA[
			//console.info('writeFlashOverlay'+ source); //trace with firebug
			var so = new SWFObject(source, "overlay", "100%", "100%", "8", "#000000");
			so.addParam("allowScriptAcces","always");
			so.addParam("wmode","transparent");
			so.write("overlayHolder");
		// ]]>
		}
		//---------------------------------
		function showDiv(){//show's the div after the swf is loaded. The javascript call is made from flash.
			//console.info('showDiv'); //trace with firebug
			var holder = document.getElementById("overlayHolder")	
			// set height/width of movie
			var arrayPageSize = getPageSize();
			w = arrayPageSize[0];
			h = arrayPageSize[1];
			holder.style.height = h +"px";
			holder.style.width = w +"px";
			showHideDivs('none');
		}
		//---------------------------------	
		function hideDiv(){//hide the div after the swf is finished. The javascript call is made from flash.
			// console.info('hideDiv'); //trace with firebug
			h = document.getElementById("overlayHolder")
			h.style.height = "1px";
			h.style.width = "1px";
			h.style.top = "-2px";
			showHideDivs('block');
		}
		//---------------------------------
		function showHideDivs(sh){//function to hide div's that may overlay over the banner. Fill out the id's in the divToHide separated by comma.
			//console.info('showHide called with: '+ sh);
			var divToHide = "div1,div2,div3";
			var arr = new Array();
			arr = divToHide.split(",");
			
			var arLen=arr.length;
			for (i=0;i<arLen;i++){
					document.getElementById(arr[i]).style.display = sh;
				}
		}
		//---------------------------------
		function getPageSize(){//generic function to calculate document height.
			
			var xScroll, yScroll;
			
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			
			var windowWidth, windowHeight;
			
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	
			
			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else { 
				pageHeight = yScroll;
			}
		
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth,pageHeight) 
			return arrayPageSize;
		}

        function createCookie(name,value,days) {
            if (days) {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
            }
            else var expires = "";
            document.cookie = name+"="+value+expires+"; path=/";
        }
        
        function readCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
            }
            return null;
        }
        function eraseCookie(name) {
            createCookie(name,"",-1);
        }
        
        function startOverlay(page) {
            var variants = new Array('flash/light.swf', 'flash/wind.swf', 'flash/sound.swf', 'flash/shake.swf');

            // eraseCookie('viralPlayerData');

            var overlayCookiedata = getOverlayCookieData();
            var randomVariant = null;
        
            // have we played on this page something? if so cancel...
            if (overlayCookiedata['localPlayed'][page] == undefined) {
                // any variants left? if not cancel...
                if (overlayCookiedata['globalPlayed'].length < variants.length) {
                    var doneSelecting = false;
                    while(!doneSelecting) {
                        var rnd = Math.floor(Math.random() * 4);
                        var randomVariant = variants[rnd];
        
                        var inArray = false;
                        for (var i = 0; i < overlayCookiedata['globalPlayed'].length; i++)
                            if (overlayCookiedata['globalPlayed'][i] == randomVariant)
                                inArray = true;
                    
                        doneSelecting = !inArray;
                    }
        
                    overlayCookiedata['globalPlayed'].push(randomVariant);
                    overlayCookiedata['localPlayed'][page] = randomVariant;
        
                    // console.info('Playing: ' + randomVariant);
        
                    setOverlayCookieData(overlayCookiedata);
        
                    writeFlashOverlay(randomVariant);
                } // else console.info("not all items have been played, selecting one:");
            } // else console.info("this page has played something earlier: " + overlayCookiedata['localPlayed'][page]);
        }

        function getOverlayCookieData() {
            var data = readCookie('viralPlayerData');

            if (data == undefined) data = new Object();
            else data = data.parseJSON();

            // init property
            if (data['globalPlayed'] == undefined)
                data['globalPlayed'] = new Array();

            // init property
            if (data['localPlayed'] == undefined)
                data['localPlayed'] = new Object();

            return data;
        }

        function setOverlayCookieData(data) {
            createCookie('viralPlayerData', data.toJSONString(), 0);                
        }

function showOverlay(element, div, left, top)//, text)
{
	var e = document.getElementById(element);
	var d = document.getElementById(div);
	//var t = document.getElementById('overlay_text');
	
	d.style.display = '';
	//t.innerHTML = text;
	
	var divheight = 0;
	if(d.offsetHeight) { divheight = d.offsetHeight; }
	else if(d.style.pixelHeight) { divheight = d.style.pixelHeight; }

	//left 18 top 50
	d.style.left = (findPosX(e) - (-left)) + "px";
	d.style.top = (findPosY(e) - (-top) - divheight) + "px";
}

function hideMe(div)
{
	var d = document.getElementById(div);
	d.style.display = 'none';
}

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }