
//Code to do blending
function opacity(id, opacStart, opacEnd, millisec) { 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    if(opacStart > opacEnd) { 
        for(var i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(var i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
} 

function blendimage(divid, imageid, imagefile, millisec) { 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 
     
    document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")"; 
     
    changeOpac(0, imageid); 
     
    document.getElementById(imageid).src = imagefile; 

    for(var i = 0; i <= 100; i++) { 
        setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed)); 
        timer++; 
    } 
}

//Code for dynamically loading adds
function createRequestObject(){
	var request_o;
	var browser = navigator.appName; 
	if(browser == "Microsoft Internet Explorer"){
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		request_o = new XMLHttpRequest();
	}
	return request_o;
}

var http = createRequestObject();

function getAd(currAd)
{
	
	thisPage = pageNum;
	http.open('get', 'content_request.php?action=get_page&page=' + pageNum);
	http.onreadystatechange = handleContent;
	http.send(null);
}

function handleContent()
{
	if(http.readyState == 4)
	{
		var response = http.responseText;
		var responseArray = 
		document.getElementById('image').innerHTML = response;
	}
}

function Left(str, n)
{
	if (n <= 0)
	return "";
	else if (n > String(str).length)
	return str;
	else
	return "http://" + (String(str).substring(4,n)).replace(";","/");
}
	if(!Array.indexOf){
		    Array.prototype.indexOf = function(obj)
		    {
		        for(var i=0; i<this.length; i++)
		        {
		            if(this[i]==obj)
		            {
		                return i;
		            }
		        }
		        return -1;
		    }
		}
		
function newAdd()
{
	var rand = Math.floor(Math.random() * images.length);
	while(displayed.indexOf(rand) >= 0)
	{
		rand = Math.floor(Math.random() * images.length);
	}
	displayed[lastAdd] = rand;
	var tempVar = images[rand];
	switch(lastAdd)
	{
	case 0:
		blendimage('containerImg1', 'hi1img', images[rand], 1000);
		document.getElementById("link1").href=Left(tempVar, images[rand].length-4);
		break;
	case 1:
		blendimage('containerImg2', 'hi2img', images[rand], 1000);
		document.getElementById("link2").href=Left(tempVar, images[rand].length-4);
		break;
	case 2:
		blendimage('containerImg3', 'hi3img', images[rand], 1000);
		document.getElementById("link3").href=Left(tempVar, images[rand].length-4);
		break;
	case 3:
		blendimage('containerImg4', 'hi4img', images[rand], 1000);
		document.getElementById("link4").href=Left(tempVar, images[rand].length-4);
		break;
	}
	lastAdd++;
	lastAdd %= 4;
}
