/** 
  * ESOL Dragging popup windows 
  * @author Ivars Veksins 
  */ 
  
// allowed buttons in header 
BN_CLOSE = 1; 
BN_PLUS = 2; 
BN_MINUS = 3; 

// settings 
WND_CORNER_TOP = 1;
WND_CORNER_BOT = 2;  
WND_SIMPLE = 1; 
WND_SHADOW = 2; 
WND_NOFOOTER = 4;


JsWindow.POS_INIT = '-99999px'; 
VideoActivated=false;

 /** 
  * JavaScript popup window class 
  * 
  */
 function JsWindow(type, reference, width, height)
{
	this.$type = type; 
	this.$reference = reference; 
	this.$visible = false; 
	
	
	if (!width) 
		width = 300; 
		
	this.$width = width; 
	
	if (height) 
		this.$height = height; 
	

	if (this.$type & WND_SIMPLE) {
		// create a simple window 
		this.$Wnd = JsWindow.methods.create(this.$reference, this.$type & WND_NOFOOTER); 
	} else if (this.$type & WND_SHADOW) { 
		// create a shadowed window 
		this.$Wnd = JsWindow.methods.create_shadowed(this.$reference, this.$type & WND_NOFOOTER); 
	}
	
	this.$Wnd.style.width = this.$width + 'px'; 
	
	if (height) { 
		this.get_content().style.height = this.$height + 'px'; 
	
	}
	
	// if window has to be placed outside the screen 
	// required by flash controllers
	document.body.appendChild( this.get_window() ); 
	this.get_window().style.top = JsWindow.POS_INIT; 

	// initialise dragging 
	this.init_drag(); 
}

//IMPLEMENT_GARBAGE(JsWindow); 

JsWindow.dispose = function() 
{
	JsWindow.ACTIVE = null; 
	JsWindow.DRAGING_HALT = null; 
	JsWindow.MOUSE_OFFSET = null; 
	
	BN_CLOSE = null; 
	BN_PLUS = null; 
	BN_MINUS = null; 
	
	// settings 
	WND_CORNER_TOP = null;
	WND_CORNER_BOT = null;  
	WND_SIMPLE = null; 
	WND_SHADOW = null; 
	WND_NOFOOTER = null;
	
	JsWindow.POS_INIT = null;

}

// settings for dragging 
JsWindow.ACTIVE; 
JsWindow.DRAGING_HALT = false; 
JsWindow.MOUSE_OFFSET = 0; 

// dynamic 
JsWindow.prototype = 
{

	add_button:  function(href, title, html, className) { 
		var caption = this.get_caption(); 
		
		var button =  this.create_button(href, title, html, className);
		caption.appendChild(button); 
	}, 
	
	// function for manual appending 
	append: function() 
	{
		document.body.appendChild( this.get_window() ); 
		this.get_window().style.top = JsWindow.POS_INIT; 
	
		// initialise dragging 
		this.init_drag(); 		
	},
	playVideo:function(centered)
	{
		this.show(centered);
		if(!VideoActivated)
		{
			// nesol_video 
			var pv = new FlashObject('nesol_video');
			pv.playCurrentVideo();
			VideoActivated=true;
		}

	},
	show:function(centered)
	{
	
		if (!this.$visible) { 
			//document.body.appendChild( this.get_window() ); 
			
			this.get_window().style.visibility = 'visible'; 
			
			this.update_shadow(); 
			this.$visible = true; 
		}
		
		pt = this.get_position(this.get_window()); 

		if (centered || (pt[1] < 0	&& !this.$last_pos)) {
			size = JsWindow.methods.get_window_size(); 
			
			wnd_left=  (size[0] / 2 - this.get_shadow().clientWidth / 2) + document.body.scrollLeft; 
			wnd_top = (size[1] / 2 - this.get_shadow().clientHeight / 2) + document.body.scrollTop; 

			this.$Wnd.style.left = wnd_left + 'px'; 
			this.$Wnd.style.top = wnd_top + 'px'; 		
		}
		
		if (this.$last_pos) 
		{ 
			this.$Wnd.style.left = this.$last_pos[0] + 'px'; 
			this.$Wnd.style.top = this.$last_pos[1] + 'px'; 				
		}
		
		this.get_window().style.visibility = 'visible'; 
      if (this.$visible) 
        focus_ID('button_close');		
	},
	
	hide:function() {
		//document.body.removeChild( this.get_window() ); 
		this.get_window().style.visibility = 'hidden'; 
		
		/*this.$last_pos = this.get_position(this.get_window()); 
		
		var wnd = this.get_window(); 
		
		wnd.style.top = JsWindow.POS_INIT; 
		wnd.style.left = JsWindow.POS_INIT; 
		
		*/
		this.$visible = false; 
		//set focus to check button
		focus_ID('templateArea');
	},
	
	create_button:function(href, title, html, className) { 

		var tmp = document.createElement("a");
		
		tmp.setAttribute("href",href); 
		tmp.setAttribute("title",title); 
		tmp.setAttribute("id",className); 
		tmp.innerHTML = html; 
		tmp.className = className; 
		
		// comunication with drag container 
		tmp.onmousedown = new Function("JsWindow.DRAGING_HALT = true;"); 
		tmp.onmouseup = new Function("JsWindow.DRAGING_HALT = false;"); 
		
		return tmp; 
	},

	
	
	
	get_window: function() { 
		return this.$Wnd; 
	}, 
	// sets window text
	set_text: function (text, padding) 
	{ 
		var content = this.get_content(); 
		//get window height
		var winHeight=document.body.offsetHeight;
		if(window.innerHeight) winHeight=winHeight-20;
		// padding still has to be passed
		if (!padding) {
			content.childNodes[0].innerHTML = text; 
		} else { 
			content.innerHTML = text; 
		}
		//work out content height
		var contentHeight=content.scrollHeight;
		if(!padding)contentHeight=content.childNodes[0].scrollHeight;
		//compare
		if(contentHeight>parseInt(0.8*winHeight)){
			//resize
			if(!padding){
			content.childNodes[0].style.height='auto';
			content.childNodes[0].childNodes[0].style.height=parseInt(0.8*winHeight)+'px';
			content.childNodes[0].childNodes[0].style.overflow='auto';
			} else {
			content.style.height=(0.8*winHeight)+'px';
			content.style.overflow='auto';
			}
		 }
		 else 
			content.childNodes[0].style.height='100%';
		
		if (this.$type & WND_SHADOW) 
			this.update_shadow(); 		
	},
	
	init_drag: function() 
	{ 
		var caption = this.get_caption(); 
		
		if (!caption) 
			return; 
		
		caption.container = this; 
		
		event_install(caption, "onmousedown", JsWindow.methods.start_drag );
		event_install(caption, "onmouseup", JsWindow.methods.end_drag );
		
		event_install(document, "onmousemove",JsWindow.methods.update_position );
		
		if (typeof caption.onselectstart != "undefined")
			caption.onselectstart = new Function("return false;");  	

		caption.onmousedown = new Function("return false;"); 
		
		// install document 	
		event_install(document,"onmouseup",function() { 
				JsWindow.DRAGING = false; JsWindow.DRAGING_HALT = false; 
			} 
		); 
		
	}, 
	
	// gets dragable caption 
	get_caption: function() 
	{ 
	
		if (this.$type & WND_SHADOW) {
		
			var el = this.$Wnd.childNodes[1].childNodes[0].childNodes[0]; 
			
			if (el.className == "dragable_caption"); 
				return el; 
				
		} else { 
			
			var el = this.$Wnd.childNodes[0].childNodes[0]; 
			if (el.className == "dragable_caption"); 
				return el; 
				
		}	
		
		return null; 
	},
	
	// updates shadow window
	update_shadow: function() 
	{ 
		if (this.$type & WND_SHADOW == 0)
			return; 
			
		var content = this.get_content(); 
		
		this.get_shadow().style.width = content.clientWidth + 'px'; 
		this.get_shadow().style.height = content.clientHeight + 'px'; 

	},
	
	get_position: function(element) 
	{ 
		var x = 0, y = 0; 
		do 
		{ 
			
			x += element.offsetLeft || 0; 
			y += element.offsetTop || 0; 
			
			element = element.offsetParent;
		} while (element) 
		
		return [x,y]; 
	},
	
	
	// gets shadow window 
	get_shadow: function() { 

		if (this.$type & WND_SHADOW == 0)
			return; 
			
		return this.$Wnd.childNodes[0].childNodes[1]; 
		
	},
	
	// gets content window 
	get_content: function() {
		
		if (this.$type & WND_SHADOW) {
			return this.$Wnd.childNodes[1].childNodes[1]
		} else { 
			return this.$Wnd.childNodes[1]; 
		}
	}, 
	
	// reset width
	set_width: function(width) { 
		
		this.$width = width; 
		
		this.$Wnd.style.width = this.$width + 'px'; 
		this.update_shadow(); 
	}, 
	
	// reset height
	set_height: function(height) { 
		
		this.$height = height; 
		
		this.get_content().style.height = this.$height + 'px'; 
		this.update_shadow(); 
	},
	
	// reset width AND height (2 functions merged as updating shadow twice caused problems in IE 6) - delete 2 old functions above
	resize: function(width,height) {
	
		this.$width = width;
		this.$height = height;
		
		this.$Wnd.style.width = this.$width + 'px';
		
		this.get_content().style.height = this.$height + 'px'; 

		this.update_shadow(); 
	
	}
	
};

	//reset height



// static 
JsWindow.methods = 
{
	/** 
	 * The same as create except has a shadow
	 */ 
	create_shadowed: function(reference, footer) 
	{ 

		if (!reference) 
			alert("create_shadowed(): Windows have to have a reference. Unique ID to be recognized."); 
		
	
		var shadowed = document.createElement("DIV"); 
		shadowed.className = 'shadowed_window';
		
		shadowed.id = reference; 
		
		var window_1 = JsWindow.methods.create(reference + "_shadow", footer); 
		// make window as a shadow window
		window_1.className = window_1.className + ' shadow'; 
		
		var window_2 = JsWindow.methods.create(reference + "_main", footer); 
		
		shadowed.appendChild(window_1); 
		shadowed.appendChild(window_2); 
		
		return shadowed;
	},

	/** 
	 * Uses DOM to create HTML tree 
	 * which would look like a simple window 
	 * with header or footer
	 */ 
	create : function(reference, has_footer) 
	{


		if (!reference) 
			alert("create(): Windows have to have a reference. Unique ID to be recognized."); 

		// create main dom element 
		var new_window = document.createElement("DIV"); 
		new_window.className = 'window'; 
		
		new_window.id = reference; 
		
		// create header and footer 
		var header = JsWindow.methods.create_corner(WND_CORNER_TOP, true); 
		
		if (has_footer == 0) 
			var footer = JsWindow.methods.create_corner(WND_CORNER_BOT); 
		

		
		// create content area 
		var content = document.createElement("DIV"); 
		content.id = reference + "_content"; 
		

		content.className = 'content'; 
		
		var padding = document.createElement("DIV"); 
		padding.className = 'padding'; 
		
		content.appendChild(padding); 
		
		new_window.appendChild(header); 
		new_window.appendChild(content); 
		
		if (footer) 
			new_window.appendChild(footer); 
		
		return new_window; 
	}, 
	
	create_corner: function(type, dragging) 
	{ 
		if (!dragging) 
			dragging = false; 
		
		
		var class_type = 't'; 
		if (type == WND_CORNER_BOT) 
			class_type = 'b'; 
			
		
		// create main div element 
		var corner = document.createElement("DIV"); 
		corner.className = 'corner'; 
		
		if (dragging) {
			var dragging = document.createElement("DIV"); 
			dragging.className = 'dragable_caption'; 
			
			// append dragable caption 
			corner.appendChild(dragging); 
		}
			
		// cornets itself 
		var left_corner = document.createElement("DIV"); 
		left_corner.className = 'corner-' + class_type + 'l'; 
		
		var right_corner = document.createElement("DIV"); 
		right_corner.className = 'corner-' + class_type + 'r'; 
		
		var caption = document.createElement("DIV"); 
		caption.className = 'caption'; 
		
		corner.appendChild(left_corner); 
		corner.appendChild(caption); 
		corner.appendChild(right_corner); 
		
		return corner; 
	}, 
	
	start_drag: function() {
		if(!CTAD_SETTINGS["capture_mouse_movement"]){
			ctad_error("Cannot drag because capture_mouse_movement is false in settings");
			return false;
		}
		var caption = this.container.get_caption(); 
		
		// get position 
		var pos = this.container.get_position(caption); 
		
		// make it more realistic 
		// better than having the cursor always in centre
		if (pos) 	
			JsWindow.MOUSE_OFFSET = [pos, pos[0] - MouseCapture.XPOS]
			
		JsWindow.ACTIVE = this.container; 
		JsWindow.DRAGING = true; 	
	}, 
	
	end_drag: function() { 
		JsWindow.DRAGING = false; 
		this.style.cursor = 'default'; 
		
		JsWindow.MOUSE_OFFSET = 0; 
		JsWindow.DRAGING_HALT = false; 
	}, 
	
	update_position: function() { 
		if (!JsWindow.DRAGING || !JsWindow.ACTIVE || JsWindow.DRAGING_HALT ) 
			return; 
		
		var container = JsWindow.ACTIVE;
		
		//window.style.position = 'absolute'; 
		container.get_caption().style.cursor = 'pointer'; 
		
		
		// top position 
		var top = 0;
		if (document.documentElement.scrollTop && typeof document.onselectstart != "undefined" ) 
			top = document.documentElement.scrollTop;
		
		// show exactly where picked 
		container.get_window().style.left =(MouseCapture.XPOS +  JsWindow.MOUSE_OFFSET[1] ) + 'px';
		
		window.status =  document.documentElement.scrollTop; 
		
		
		container.get_window().style.top = (MouseCapture.YPOS - 20 + top) + 'px'; 
	},
	
	get_window_size:function(){
		if (window.innerHeight) {
			return Array(window.innerWidth,window.innerHeight);
		}else if (document.documentElement && document.documentElement.clientHeight){
			return Array(document.documentElement.clientWidth,document.documentElement.clientHeight);
		}else if (document.body) {
			return Array(document.body.clientWidth,document.body.clientHeight);
		}
	}

}




/** 
  * Video Window custom methods for resizing window and 
  * closing it. 
  */
function VJsWindow() 
{
}

VJsWindow = 
{
	hide : function(wnd) 
	{ 
		// nesol_video 
		var pv = new FlashObject('nesol_video')
		pv.stopAllSounds();
		
		wnd.hide();
	}
}





// function for resizing window. 
function size_up(window) 
{
	//var width = w2.$width;
	var width = window.$width;
	
	if (width >= 400) 
		return ;
	
	
	if (width == 230) {
		window.resize(300,235);
		return ;
	}  else {
		window.resize(400,300);
		return; 
	}
}

function size_down(window) 
{ 
	//var width = w2.$width;
	var width = window.$width;
	
	if (width == 230) 
		return; 
		
	if (width == 400) { 
		window.resize(300,235);
		return; 
	} else {
		window.resize(230,186);
		return; 
	}
}
function focus_ID(elemID)
{
  var element = document.getElementById(elemID); 
    
	//set focus to close button
   try 
   {
     if(element)
        element.focus();
    } catch (excetion) { 
 
    }
}

function focus_submit()
{
	//focus submit button
	inputs = document.getElementsByTagName("input"); 

	// using image 
	for (var x = 0; x < inputs.length; x++) 
	{
		if (inputs[x].name == "submit") 
		{
			//alert("submit");
			inputs[x].focus(); 	
			return;
		}
	}
}
function timeout() 
{
	//set focus to template area to make the menu tabbing the last thing
	focus_ID('video_thumbnail');
}
