var popupBG, videoPop;

function initExtraLinks(){
	//Unbind every event for those links
	$('#enlacesextras a').unbind('click');

	$('#enlacesextras a.inline').bind('click', openInlineLink);
	$('#enlacesextras a.blank').bind('click', openBlankLink);
	$('#enlacesextras a.url').bind('click', openUrlLink);
}

function openInlineLink(){
	var href = this.href;
	var id = href.substring(href.indexOf('#') + 1);

	var div = document.getElementById(id);
	var obj = div.getElementsByTagName('object')[0];

	var content = getObjectHTML(obj);

	if(!popupBG){
		popupBG = document.createElement('div');
		popupBG.setAttribute('id', 'videoPopupBg');

		videoPop = document.createElement('div');
		videoPop.setAttribute('id', 'videoPopup');

		document.body.appendChild(popupBG);
		document.body.appendChild(videoPop);
	}

	videoPop.innerHTML = content;

	var pageSize = getPageSize();
	var pageScroll = getPageScroll();

	var left = (pageSize[0] - 464) / 2;
	var top = pageScroll[1] + ((pageSize[1] - 360) / 2);

	videoPop.style.left = left+'px';
	videoPop.style.top = top+'px';

	popupBG.style.height = $getDocumentHeight()+'px';

	popupBG.style.display = 'block';
	videoPop.style.display = 'block';

	return false;
}

function openBlankLink(){
	var win = window.open(this.href);
	return false;
}

function openUrlLink(){
	sendLink(this);
	return false;
}

function closeVideoPopup(){
	popupBG.style.display = 'none';
	videoPop.style.display = 'none';

	videoPop.innerHTML = '';
}

function getPageScroll(){
	var yScrolltop;
	var xScrollleft;
	if (self.pageYOffset || self.pageXOffset) {
		yScrolltop = self.pageYOffset;
		xScrollleft = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){	 // Explorer 6 Strict
		yScrolltop = document.documentElement.scrollTop;
		xScrollleft = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScrolltop = document.body.scrollTop;
		xScrollleft = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScrollleft,yScrolltop)
	return arrayPageScroll;
}

function getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
	arrayPageSize = new Array(w,h)
	return arrayPageSize;
}

function $getDocumentHeight(){
	if(document.documentElement.scrollHeight){
		if(document.body.scrollHeight){
			return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)
		}
		else{
			return document.documentElement.scrollHeight;
		}
	}
	else{
		return document.body.scrollHeight;
	}
}

$().ready(initExtraLinks);