var flashes = new Array();

var url_to_names = new Array();

var iframe_id_to_names = {};

var flash_counter = 0;

var id_prefix = "flash_";

var clip_rect = {left: 0, top: 0, right: 800, bottom: 600};

if(typeof console==='undefined'){
	console = {log: function(x){}};
}

function dquote(str)
{
	var dq = '"';
	return dq + str + dq;
}

function add_iframe(iframe_name, iframe_left, iframe_top, iframe_width, iframe_height, iframe_url, iframe_allowMove)
{
	try {
		//console.log(iframe_name+"\n"+iframe_left+"\n"+iframe_top+"\n"+iframe_width+"\n"+iframe_height+"\n"+iframe_url+"\n"+iframe_allowMove);
		if(typeof(flashes[iframe_name])!=='undefined' && typeof(flashes[iframe_name].id)!=='undefined'){
			return flashes[iframe_name].id;
		}
		var counter = flash_counter++;
		var id = id_prefix + counter;
		
		var cr = intersect(
			parseInt(iframe_left),
			parseInt(iframe_top),
			parseInt(iframe_width),
			parseInt(iframe_height)
		);
		$('body').append(
				'<iframe id=' 	+ dquote(id)
				+ 'src=' 	+ dquote(iframe_url.substring(0,iframe_url.lastIndexOf('/'))+'/eflash.html?url='+iframe_url+'&name='+iframe_name)
				+ 'scrolling="no" '
				+ 'style="position:absolute; '
					+ 'left: '	+ iframe_left 	 + 'px; ' 
					+ 'top:'	+ iframe_top 	 + 'px; ' 
					+ 'height:' 	+ iframe_height + 'px; '
					+ 'width:'	+ iframe_width  + 'px; '
					+ 'overflow: hidden; background: transparent; background-color:transparent; z-index:4;'
					+ 'clip: rect('+cr.top+'px,'+cr.right+'px,'+cr.bottom+'px,'+cr.left+'px);'
					+ 'border: none; Body{background: transparent; overflow: hidden;}"'
				+ 'allowtransparency="true"'
				+ 'allowTransparency="true"'
				+ 'frameborder="0"'
				+ '>'
				+ '</iframe>');
		

		flashes[iframe_name] = {};
		flashes[iframe_name].id = id;
		flashes[iframe_name].visible = true;
		flashes[iframe_name].lastCoords = {};
		flashes[iframe_name].functions = new Array();
		flashes[iframe_name].allowMove = iframe_allowMove;
		flashes[iframe_name].width = iframe_width;
		flashes[iframe_name].height = iframe_height;
		url_to_names[iframe_name] = iframe_name;
		iframe_id_to_names[id]=iframe_name;
		
		flashes[iframe_name].lastCoords["x"] = iframe_left;	
		flashes[iframe_name].lastCoords["y"] = iframe_top;
		
		return id;
	} catch(error){
	}
	return null;
}

function getIframeId(iframe_name){
	try {
		if(typeof(flashes[iframe_name])!=='undefined' && typeof(flashes[iframe_name].id)!=='undefined'){
			return flashes[iframe_name].id;
		}
	} catch(error){
	}
	return null;
}

function remove_iframe(iframe_name)
{
	try {
		var id = getIframeId(iframe_name);
		if (id!=null)
		{
			$('#' + id).remove();
			delete flashes[iframe_name];
			delete iframe_id_to_names[id];
			delete url_to_names[iframe_name];
		}
	} catch(error){
	}
	return;
}

function thisMovie(movieName) {
	try {
		if (navigator.appName.indexOf("Microsoft") != -1) {
			return window[movieName];
		} else {
			return document[movieName];
		}
	} catch(error){
	}
	return null;
}
 
function register_function(frame_url, func_name, func) {
	try {
		var frame_name = url_to_names[frame_url];
		if (typeof(frame_name)!=='undefined' && typeof(flashes[frame_name].functions)!=='undefined')
		{
			flashes[frame_name].functions[func_name] = func;
		}
	} catch(error){
	}
	return;
}

function forward_message_to_flash(iframe_name, func_name, func_args){
	try {
		if(typeof(flashes[iframe_name])!=='undefined' && typeof(flashes[iframe_name].functions)!=='undefined'){
			var func_to_call = flashes[iframe_name].functions["sendMessageToFlash"];
			if(typeof(func_to_call)!=='undefined' && typeof(func_to_call)==='function'){
				func_to_call(func_name, func_args);
			}
		}
	} catch(error){
	}
	return;
}

function forward_message_to_flex(frame_url, func, params){
	try {
		if (typeof(url_to_names[frame_url])!=='undefined'){
			var frame_name = url_to_names[frame_url];
			var tm = thisMovie("NotebookExpress");
			if(tm!=null){
				tm.sendMessageToActionScript(frame_name,func,params);
			}
		}
	} catch(error){
	}
	return;
}

function hide_all_iframes(){
	try {
		$("iframe").each(function(idx, el) {
			try {
				var id = $(el).attr("id");
				if(id.indexOf(id_prefix)==0){
					$("#" + id).css("width", "1");
					$("#" + id).css("height", "1");
				}
			} catch(error){
			}
			return;
		});
	} catch(error){
	}
	return;
}

function restore_all_iframes(){
	try {
		$("iframe").each(function(idx, el) {
			try {
				var id = $(el).attr("id");
				if(id.indexOf(id_prefix)==0 && flashes[iframe_id_to_names[id]].visible===true){
					$("#" + id).css("width", ""+flashes[iframe_id_to_names[id]].width);
					$("#" + id).css("height", ""+flashes[iframe_id_to_names[id]].height);
				}
			} catch(error){
			}
			return;
		});
	} catch(error){
	}
	return;
}

function hide_iframe(iframe_name){
	try {
		var id = getIframeId(iframe_name);
		if (id!==null)
		{
			flashes[iframe_name].visible = false;
			$("#" + id).css("width", "1");
			$("#" + id).css("height", "1");
		}
	} catch(error){
	}
	return;
}

function show_iframe(iframe_name){
	try {
		var id = getIframeId(iframe_name);
		if (id!=null)
		{
			flashes[iframe_name].visible = true;
			$("#" + id).css("width", ""+flashes[iframe_name].width);
			$("#" + id).css("height", ""+flashes[iframe_name].height);
		}
	} catch(error){
	}
	return;
}


function remove_all_iframes(){
	try {
		var to_delete = [];
		$("iframe").each(function(idx, el) {
			try {
				var id = $(el).attr("id");
				if(id.indexOf(id_prefix)==0){
					to_delete.push(id);
				}
			} catch(error){
			}
			return;
		});
		for(var i=0;i<to_delete.length;i++){
			remove_iframe(iframe_id_to_names[to_delete[i]]);
		}
	} catch(error){
	}
	return;
}

function sendTopLeft(iframe_name)
{
	try{
		var id = getIframeId(iframe_name);
		if(id!=null){
			var curX = parseInt($("#" + id).css("left"));
			var curY = parseInt($("#" + id).css("top"));	
			forward_message_to_flex(iframe_name,'setStartingCoords',curX+','+curY);	
		}
	} catch(error){
	}
	return;
}

function move(iframe_name, mouseX, mouseY)
{
	try {
		if(typeof(flashes[iframe_name])!=='undefined' && flashes[iframe_name].allowMove===true){
			var lastX = flashes[iframe_name].lastCoords["x"];
			var lastY = flashes[iframe_name].lastCoords["y"];
			
			var deltaX = Math.abs(lastX - mouseX);
			var deltaY = Math.abs(lastY - mouseY);
			if ((deltaX >= 5) || (deltaY >=5))
			{				
				var id = getIframeId(iframe_name);
				if(id!=null){
					var curX = parseInt($("#" + id).css("left"));
					var curY = parseInt($("#" + id).css("top"));								
					var width = parseInt($("#" + id).css("width"));	
					var height = parseInt($("#" + id).css("height"));	

					var winScreenX;
					var winScreenY;
					
					if (navigator.appName.indexOf("Microsoft") != -1) 
					{
						winScreenX = window.screenLeft;
						winScreenY = window.screenTop;
					}
					
					else
					{
						winScreenX = window.screenX;
						winScreenY = window.screenY;
					}
							
					var newX = (mouseX - winScreenX);
					var newY = (mouseY - winScreenY)-getHeightOffset();	
											
					var realX = newX - (width/2);
					var realY = newY - (height/2);
					move_iframe(iframe_name, realX, realY);
					
					var curX2 = parseInt($("#" + id).css("left"));
					var curY2 = parseInt($("#" + id).css("top"));				
					forward_message_to_flex(iframe_name,'moveFlashAnnotation',curX2+','+curY2);	
				}
			}
		}
	} catch(error){
	}
	return;
}

function move_iframe(iframe_name, new_left, new_top)
{
	try {
		var id = getIframeId(iframe_name);
		if (id!=null)
		{
			$("#" + id).css("left", new_left);
			$("#" + id).css("top", new_top);
			var iframe = document.getElementById(id);
			var cr = intersect(
				parseInt(iframe.style.left),
				parseInt(iframe.style.top),
				parseInt(iframe.style.width),
				parseInt(iframe.style.height)
			);
			iframe.style.clip = "rect("+cr.top+"px,"+cr.right+"px,"+cr.bottom+"px,"+cr.left+"px)"
		}
	} catch(error){
	}
	return;
}

function set_clip_rectangle(rx,ry,rw,rh){
	try {
		clip_rect.left = parseInt(rx);
		clip_rect.top = parseInt(ry);
		clip_rect.right = clip_rect.left+parseInt(rw);
		clip_rect.bottom = clip_rect.top+parseInt(rh);
		var iframes = document.getElementsByTagName('iframe');
		for(var i=0;i<iframes.length;i++){
			var id = iframes[i].id;
			if(id.indexOf(id_prefix)==0){
				var cr = intersect(
					parseInt(iframes[i].style.left),
					parseInt(iframes[i].style.top),
					parseInt(iframes[i].style.width),
					parseInt(iframes[i].style.height)
				);
				iframes[i].style.clip = "rect("+cr.top+"px,"+cr.right+"px,"+cr.bottom+"px,"+cr.left+"px)";
			}
		}
	} catch(error){
	}
	return;
}

function intersect(l,t,r,b){
	try {
	r+=l;
	b+=t;
		if(l>clip_rect.right || t>clip_rect.bottom || b<clip_rect.top || r<clip_rect.left){
			return {left: 0, top: 0, right: 0, bottom: 0};
		} else {
			var cr = {};
			cr['left'] = Math.max(l,clip_rect.left);
			cr['top'] = Math.max(t,clip_rect.top);
			cr['right'] = Math.min(r,clip_rect.right);
			cr['bottom'] = Math.min(b,clip_rect.bottom);
			if((cr.top-t)>0) cr.top-=t; else cr.top=0;
			if((cr.left-l)>0) cr.left-=l; else cr.left=0;
			if((cr.right-r)<0) cr.right = ((r+(cr.right-r))-l); else cr.right = r-l;
			if((cr.bottom-b)<0) cr.bottom = ((b+(cr.bottom-b))-t); else cr.bottom = b-t;
			return cr;
		}
	} catch(error){
	}
	return;
}

function resetScrolls(){
	try{
		if(window.pageYOffset!=0) window.pageYOffset = 0;
		if(window.pageXOffset!=0) window.pageXOffset = 0;
	} catch (error){}
	try{
		if(document.body.scrollTop!=0) document.body.scrollTop = 0;
		if(document.body.scrollLeft!=0) document.body.scrollLeft = 0;
	} catch (error){}
}

function getHeightOffset(){
	try {
		var viewportwidth;
		var viewportheight;
		var outerHeight = $(window).height();
		if (typeof window.innerWidth != 'undefined')
		{
			viewportwidth = window.innerWidth,
			viewportheight = window.innerHeight
		}
		 else if (typeof document.documentElement != 'undefined'
			 && typeof document.documentElement.clientWidth !=
			 'undefined' && document.documentElement.clientWidth != 0)
		 {
			   viewportwidth  = document.documentElement.clientWidth,
			   viewportheight = document.documentElement.clientHeight
		 }
		 else
		 {
			viewportwidth  = document.getElementsByTagName('body')[0].clientWidth;
			viewportheight = document.getElementsByTagName('body')[0].clientHeight;
		 }
		return (outerHeight - viewportheight);
	} catch(error){
	}
	return 0;
}

// setInterval("resetScrolls();",100);

// var ids = ['1288036260830-1550493567',
// '12880361451511083564157',
// '12880351365361337425099',
// '1288036379390-1195169474',
// '1288035130598990751016',
// '1288035120946-1055743708',
// '1288035062544326627918',
// '1288035054072-610250978',
// '1288034323604-1474856733',
// '12880343019071023135630',
// '12880342326001352957382'];


// var cn = 0;
// var cids = 0;
// function tester(){
	// if(cids<ids.length){
		// if(cids>0){
			// remove_iframe(("if"+(cn-1)));
		// }
		// //remove_all_iframes();
		// clip_rect.left = 0;
		// clip_rect.top = 0;
		// clip_rect.right = 1024;
		// clip_rect.bottom = 768;
		// add_iframe(("if"+cn),0,0,760,355,'http://d002017:8080/NotebookDocuments/'+ids[cids]+'/assets/flash/flash111/TitlePage.swf',true);
		// cids++;
		// cn++;
	// } else {
		// cids=0;
	// }
// }
// function hideall(){
	// hide_all_iframes();
	// setTimeout("showall();",5000);
	// console.log("hide all");
// }
// function showall(){
	// restore_all_iframes();
	// setTimeout("hideall();",5000);
	// console.log("show all");
// }
// setInterval("tester();",1000);

// setTimeout("hideall();",5000);
