var _RESIZE_DURATION = 400;
var _BPOPUP = bPopup;

function openPopUp(sId)
{
	var objPopup = $(sId);
	var objNav = $('vk-main-nav').getElements('li');
	var fx = new Fx.Styles($(sId), {duration:_RESIZE_DURATION, transition:Fx.Transitions.Sine.easeOut});
	
	//code to close
	if (objPopup.getStyle('opacity') == 1){
		
		if(!_BPOPUP) fx.stop().start({'opacity' : 0});
		else objPopup.setStyle('opacity', 0);
		
		objNav.each(function(el){
			el.setStyle('visibility', 'visible');
		});
	}
	//code to open
	else {
		
		if(!_BPOPUP){
			fx.stop().start({'opacity' : 1});
		}
		else{
			objPopup.setStyle('opacity', 1);
			_BPOPUP = false;
			//alert('open ' + _BPOPUP);
		}
		
		objPopup.setStyle('display', 'block');
		
		objNav.each(function(el){
			el.setStyle('visibility', 'hidden');
		});

		scroll(0,0);
	}
}

function closePopup(sId)
{
	var fx = new Fx.Styles($(sId), {duration:_RESIZE_DURATION, transition:Fx.Transitions.Sine.easeOut});
	
	objNav = $('vk-main-nav').getElements('li')
	objPopup = $(sId);

	if (objPopup){
		objNav.each(function(el){
			el.setStyle('visibility', 'visible');
		});	
		fx.stop().start({'opacity' : 0});
	}
}

function resizePopup(sId){
	var objWrapper = $(sId)
	var intWrapperHeight = objWrapper.getStyle('height');
	var intWindowHeight = window.getHeight();

	if(intWrapperHeight < intWindowHeight){
		objWrapper.setStyle('height', intWindowHeight - 25);
	}	
}

function initPopups() {
	var x = $("vk-copy-wrapper").getPosition()["x"] - 17;

	$(document.body).getElements('.popup').each(function (el) {

		el.setStyles({
			"z-index" : 999,
			"opacity": 0,
			"position": 'absolute',
			"top": -3,
			"left": x
		});
		
		el.getElement(".popup-close").addEvent("click", function() {
			var sPopupId = this.getParent().getParent().getProperty("id");
			closePopup(sPopupId);
		});

		resizePopup(el.getProperty("id"));
	});
}

function setCaseReferenceEvents() {
	//set events
	$('vk-case-reference').addEvent('click', function(element){
		
			if(this.getProperty("ref") != "false"){
				
				window.location = this.getProperty("ref");	
			}
			if(this.getProperty("ref2") != "false"){

				window.location = this.getProperty("ref2");
			}
			else {
			
			openPopUp('vk-caseref-wrapper');

			}

			// If more ref id's are needed, set up a "case" function.
		
	});

}

function flashPopUp(url)
{
	newwindow=window.open(url,'name','height=700,width=700,toolbar=no,scrollbar=no');
	if (window.focus) {newwindow.focus()}
}

function initPage(){
	initPopups();
	setCaseReferenceEvents();
	sIFRReplacement();

	if (bOpenCaseReference) openPopUp('vk-caseref-wrapper');
	//refInfo();
	//resizePopup();
}

var intTop = 25;

window.addEvent('load', initPage);
