// initModalBox called from gotoCW - to open the contribution wizard as an iFrame in the 
// widget page
// "/userzone/simpleeditor/kshow_id/" + kshow_id, { width: 888, height: 544 }
function kalturaInitModalBox ( url, options, html ){
	var width = 680;
	var height = 360;
	
	if (typeof(options) != "undefined"){
		if (options.width)
			width = options.width;
		if (options.height)
			height = options.height;
	}
	var objBody = document.getElementsByTagName("body").item(0);
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.setAttribute('onclick','kalturaCloseModalBox()');
	objOverlay.setAttribute('title','Click to close');
	objOverlay.style.filter = "alpha(opacity=60)";  // make the overlay opaque in IE only. in firefox useing 50% opaque PNG anyway
	// if (jQuery(window).width() > 900 ) // don't bother to show the Overlay if page size is small
	objBody.appendChild(objOverlay, objBody.firstChild);
	
	// create modalbox div, same note about styles as above
	var objModalbox = document.createElement("div");
	objModalbox.setAttribute('id','modalbox');
	//objModalbox.setAttribute('style', 'margin-left:-' + width / 2 + 'px; width:' + width + 'px');
	if (jQuery(window).width() < 900 )
		objModalbox.className = "locked";
	else{
		objModalbox.style.width = width + 'px';
		objModalbox.style.marginLeft = -(width / 2) -5 + 'px';
		objModalbox.style.height = height + 'px';
		objModalbox.style.marginTop = -(height / 2) -5 + 'px';
		objModalbox.style.top = "50%";
	}
	
	
	// create content div inside objModalbox
	var objModalboxContent = document.createElement("div");
	objModalboxContent.setAttribute('id','mbContent');
	if ( url != null ) {
		objModalboxContent.innerHTML = '<iframe scrolling="no" width="'+width+'" height="'+height+'" frameborder="0" src="' + url + '"/>';
	}
	else if ( html != null ) {
		objModalboxContent.innerHTML = html;
	}

	objModalbox.appendChild(objModalboxContent, objModalbox.firstChild);
	
	objBody.appendChild(objModalbox, objOverlay.prevSibling);
	/*
	if (!isIE6) {
		var newImg = new Image();
		newImg.src = "/images/graphics/shadowBox.png";
		newImg.id = "shadowBox";
		shadeWidth = $("#modalbox").outerWidth() + 80;
		shadeHeight = $("#modalbox").outerHeight() + 80;
		newImg.width = shadeWidth;
		newImg.height = shadeHeight;
		newImg.style.marginTop = -(shadeHeight / 2) + 'px';
		newImg.style.marginLeft = -(shadeWidth / 2) + 'px';
		newImg.onload = function(){
			this.onload = null;
			jQuery("body").append(this);
		}
	}
	*/
	return objModalboxContent;
}


function kalturaCloseModalBox(){
	if ( this != window.top ){
		window.top.kalturaCloseModalBox();
		return false;
	}

	// TODO - have some JS to close the modalBox without refreshing the page if there is no need
	// overlay_obj = document.getElementById("overlay");
	// modalbox_obj = document.getElementById("modalbox");
	//overlay_obj.parentNode.removeChild( overlay_obj );
	//modalbox_obj.parentNode.removeChild( modalbox_obj );
	jQuery("#shadowBox").remove();
	$("#modalbox").fadeOut("normal", function(){ 
		$("#overlay").fadeOut("fast", function(){
			$("#modalbox, #overlay").remove();
		});
	});
	
	
}

function $id(x){ return document.getElementById(x); }


function kalturaRefreshTop ()
{
	if ( this != window.top )
	{
		window.top.kalturaRefreshTop();
		return false;
	}	
	window.location = new String(window.location).replace("&__temp=1", "").replace("/__temp/1", "");
}
