overlayVis = false;

function getVideoId(url){
    var match_start = url.indexOf('=');
    var match_end = url.indexOf('&');
	var match_length = match_end - match_start;
    if (match_length) {
		id = url.substr(match_start+1, match_length-1);
      return id;
    }
return -1;
}

function prepareVideoEle(link, title) {
	var videoId = getVideoId(link);
	//alert(link);
	var html  = "<br /><object width='408' height='339'><param name='movie' value='http://www.youtube.com/v/"
		+videoId+"'></param><param name='autoplay' value='1'><param name='wmode' value='transparent'></param>"
		+"<embed src='http://www.youtube.com/v/"
		+videoId+"&autoplay=1' type='application/x-shockwave-flash' wmode='transparent' width='408' height='339'></embed>"
		+"</object><br /><br /><a href='"+link+"' >"+title+"</a>";
	return html;
}

function resizeImage(ele, width, height)
{
	//alert(width +","+ height);
	//alert(document.body.clientHeight +","+ document.body.clientWidth);
	//alert(window.innerHeight +","+ window.innerWidth);
	var window_height;
	var window_width;
	if (window.innerWidth != 'undefined') {
		window_height = window.innerHeight;
		window_width = window.innerWidth;
	} else {//IE6
		window_height = document.body.clientHeight;
		window_width = document.body.clientWidth;
	}
	var image_width = width;
	var image_height = height;
	var height_ratio = image_height / window_height;
	var width_ratio = image_width / window_width;
	if (height_ratio > width_ratio) {
		//alert("height");
		ele.style.width = "auto";
		ele.style.height = "90%";
	}
	else {
		//alert("width");
		ele.style.width = "90%";
		ele.style.height = "auto";
	}
}

function popUpVideo(id,link,title) {
	var html = prepareVideoEle(link,title);
	var closeLink = "<a class='popup_close' alt='close' href = '#"
		+ id +"' onclick = 'popUpVideo(\""+ id +"\",\""+ link +"\",\""+ title + "\")'></a><br />";
	if(overlayVis == false) {
		var eleOuter = document.getElementById("overlay");
		var eleInner = document.getElementById("inner_video");
		eleOuter.style.display = "block";
		eleInner.style.display = "block";
		eleInner.innerHTML=closeLink+html;
		overlayVis = true;
	} else {
		var eleOuter = document.getElementById("overlay");
		var eleInner = document.getElementById("inner_video");
		eleOuter.style.display = "none";
		eleInner.style.display = "none";
		eleInner.innerHTML = "";
		overlayVis = false;
	}
}


function popUpImage(id,link,fulllink,title,width,height) {
	var html = "<a href=" + fulllink + " target='_blank' >" 
		+ title + "(Click to view full image)</a><br/><a href="
		+ fulllink +" target='_blank'><img onload='resizeImage(this,\""
		+ width + "\",\"" + height +"\");' class='image_popup' src='" + link + "' /></a></br>";

	var closeLink = "<a class='popup_close' alt='close' href = '#"
		+id+"' onclick = 'popUpImage(\""+ id +"\",\""
		+ link +"\",\""
		+ fulllink +"\",\""
		+ title + "\",\"" 
		+ width + "\",\"" 
		+ height + "\")'></a><br />";
	if(overlayVis == false) {
		var eleOuter = document.getElementById("overlay");
		var eleInner = document.getElementById("inner_image");
		eleOuter.style.display = "block";
		eleInner.style.display = "block";
		eleInner.innerHTML=closeLink+html;
		overlayVis = true;
	} else {
		var eleOuter = document.getElementById("overlay");
		var eleInner = document.getElementById("inner_image");
		eleOuter.style.display = "none";
		eleInner.style.display = "none";
		eleInner.innerHTML = "";
		overlayVis = false;
	}
}