var ns4=document.layers;
var ns6=document.getElementById&&!document.all;
var ie4=document.all;
var olderBowser;
var fan_strict_mode = document.compatMode == 'CSS1Compat'; // as oposite to quircks

if (window.XMLHttpRequest) {
	olderBowser = false;
} else {
	olderBowser = true;
}

var fan_window_css_loaded;
var fan_window_widget_base_url = "http:\/\/fan.yspan.com";
var fan_doc_head_elem = document.getElementsByTagName('head')[0]; 

// load css
if(typeof(fan_window_css_loaded)!='boolean' || fan_window_css_loaded==false){
	var fan_css_elem = document.createElement('link');
	fan_css_elem.rel = 'stylesheet';
	fan_css_elem.type = 'text/css';
	fan_css_elem.href = fan_window_widget_base_url+'/css/window_common.css';
	fan_doc_head_elem.appendChild(fan_css_elem);
	fan_window_css_loaded = true;
}

// load jquery
if(typeof($)!='function'){
	var jquery_script = document.createElement('script');
	jquery_script.type = 'text/javascript';
	jquery_script.src = fan_window_widget_base_url+'/js/jquery.js';
	fan_doc_head_elem.appendChild(jquery_script);
}

var fan_transparent_bg = document.createElement('div');
fan_transparent_bg.id = 'fan_transparent_bg';
fan_transparent_bg.className = 'fan_transparent_bg';

var fan_transparent_bg_appended = false;
var fan_transparent_bg_shown = false;

function fan_show_transparent_bg(){
	if(!fan_transparent_bg_appended){
		document.body.appendChild(fan_transparent_bg);
		fan_transparent_bg_appended = true;
	}
	
	if(!fan_transparent_bg_shown){
		fan_transparent_bg.style.top = 0;
		fan_transparent_bg.style.left = 0;
		fan_transparent_bg.style.zIndex = 99999;
	
		var root_element = fan_strict_mode?document.documentElement:document.body;
		fan_transparent_bg.style.width  = root_element.scrollWidth + "px";
		fan_transparent_bg.style.height = root_element.scrollHeight + "px"; //'100%';
		
		fan_transparent_bg_shown = true;
		$('object').css('display', 'none');
		$('embed').css('display', 'none');
	}
}

function fan_hide_transparent_bg(){
	fan_transparent_bg.style.top = 0;
	fan_transparent_bg.style.left = 0;
	fan_transparent_bg.style.width = "0px";
	fan_transparent_bg.style.height = "0px";
	fan_transparent_bg_shown = false;
	$('object').css('display', '');
	$('embed').css('display', '');
}

function fan_wait_overlay(waiting_element){
	var _self = this;

	this.waiting_element = waiting_element;
	this.transparent_bg = false;
	
	_self.show = function(){
		_self.waiting_element.style.position = 'relative';
		
		_self.transparent_bg = document.createElement('div');
		_self.transparent_bg.className = 'fan_wait_overlay';
		
		_self.waiting_element.appendChild(_self.transparent_bg);
		
		_self.transparent_bg.style.top = 0;
		_self.transparent_bg.style.left = 0;
		_self.transparent_bg.style.zIndex = 99999;
		
		var height = $(_self.waiting_element).height();
		var width = $(_self.waiting_element).width();
	
		_self.transparent_bg.style.width  = ''+ width + "px";
		_self.transparent_bg.style.height = ''+ height + "px";
		
		var image_elem = document.createElement('img');
		image_elem.src = fan_window_widget_base_url+'/images/wait.gif';
		image_elem.style.position = 'absolute';
		image_elem.style.top = ''+((height-48)/2)+'px';
		image_elem.style.left = ''+((width-48)/2)+'px';
		_self.transparent_bg.appendChild(image_elem);
	}
	
	_self.hide = function(){
		_self.waiting_element.removeChild(_self.transparent_bg);
	}
	
	_self.show();
}

function fan_get_highest_zIndex(container_element){
	var child_elements = $(container_element).find('*');
	var max_zIndex = 0;
	
	for (var i in child_elements){
		var temp = child_elements[i];
		if(typeof(temp.style)=='object' && (typeof(temp.style.zIndex)=='string' || typeof(temp.style.zIndex)=='number')){
			max_zIndex = Math.max(max_zIndex,Number(temp.style.zIndex));
		}
	}
	return max_zIndex;
}

function fan_get_window_scroll() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}

var fan_windows = Array();

/*

properties = {
	onclose:function(){},
	width:'120px',
	height:'120px',
	xOffset:10,
	yOffset:10,
	nobar:true, // no title bar displayed
}

*/

function fan_window(container, title, content, properties){
	var cascadeOffset = 30;
	
	var _self = this;
	if(typeof(properties)!='object'){
		properties = {};
	}
	this.properties = properties;
	
	// set an id
	var current_time = new Date();
	_self.id = current_time.getTime()+'-'+Math.random();
	fan_windows[_self.id] = _self;
	
	// window offset
	var no_of_windows = fan_associativeArrayLength(fan_windows);
	if(typeof(properties.xOffset)!='number'){
		properties.xOffset = cascadeOffset * no_of_windows;
	}
	if(typeof(properties.yOffset)!='number'){
		properties.yOffset = cascadeOffset * no_of_windows;
	}
	
	var zIndex = fan_get_highest_zIndex(container)+1;
	var window_main_container = document.createElement('div');
	window_main_container.className = 'fan_window_main_container';
	window_main_container.style.zIndex = zIndex;
	
	var fan_window_decoration_layout = document.createElement('table');
	if(typeof(properties.nobar)=='boolean' && properties.nobar){
		fan_window_decoration_layout.className = 'fan_window_decoration_layout_nobar';
	}
	else{
		fan_window_decoration_layout.className = 'fan_window_decoration_layout';
	}
	fan_window_decoration_layout.cellSpacing = 0;
	fan_window_decoration_layout.cellPadding = 0;
	var decoration_tbody = document.createElement('tbody');
	fan_window_decoration_layout.appendChild(decoration_tbody);
	
	var scroll_top = 0;
	var scroll_left = 0;
	
	if(container.tagName.toLowerCase()=='body'){
		var temp = fan_get_window_scroll();
		scroll_left = Number(temp[0]);
		scroll_top = Number(temp[1]);
	}
	else{
		try{		
			scroll_top = container.scrollTop;
			scroll_left = container.scrollLeft;
		}
		catch(E){}
	}
	
	window_main_container.style.top = ''+(properties.yOffset+scroll_top)+'px';
	window_main_container.style.left = ''+(properties.xOffset+scroll_left)+'px';
	
	window_main_container.onmousedown = function(){
		var zIndex = fan_get_highest_zIndex(container)+1;
		window_main_container.style.zIndex = zIndex;
	}
	
	// top
	var top_line = document.createElement('tr');
	decoration_tbody.appendChild(top_line);
	
	var top_left_corner = document.createElement('td');
	top_left_corner.className = 'fan_window_top_left';
	top_line.appendChild(top_left_corner);
	
	var top_bar = document.createElement('td');
	top_bar.className = 'fan_window_top';
	top_line.appendChild(top_bar);
	
	//window stop move
	this.stop_move = function(){
		document.onmousemove = prevMouseMove;
		window_main_container.MoveWindow=false;
		window_main_container.XmouseStart=false;
		window_main_container.YmouseStart=false;
		if(ie4)top_bar.releaseCapture();
		_self.show_content();
	};
	
	//window move
	var prevMouseMove = null;
	top_bar.onmousedown = function(){
		window_main_container.MoveWindow=true;
		_self.hide_content();
		
		prevMouseMove = document.onmousemove;
		document.onmousemove=function(e){
			fan_GgetMouse(e, window_main_container);
		}
		if(ie4)top_bar.setCapture();
		
		var old_onmouseup = document.onmouseup;
		document.onmouseup = function(){
			_self.stop_move();
			document.onmouseup = old_onmouseup;
		}
	};
	top_bar.onmouseup = _self.stop_move;
	
	var top_right_corner = document.createElement('td');
	top_right_corner.className = 'fan_window_top_right';
	top_line.appendChild(top_right_corner);
	
	// middle
	var content_line = document.createElement('tr');
	decoration_tbody.appendChild(content_line);
	
	var left_column = document.createElement('td');
	left_column.className = 'fan_window_left';
	content_line.appendChild(left_column);
	
	_self.content_plh = document.createElement('td');
	
	// content
	this.set_content = function(temp_content){
		if(typeof(temp_content)=='string' || typeof(temp_content)=='number'){
			var temp = temp_content;
			temp_content = document.createElement('div');
			temp_content.innerHTML = temp;
		}
		
		if(typeof(temp_content)=='object' && temp_content){
			_self.content_plh.appendChild(temp_content);
		}
	}
	this.set_content(content);
	
	
	
	//width
	this.set_width = function(){
		if(typeof(_self.properties.width)=='string'){
			_self.content_plh.style.width = _self.properties.width;
		}
		else if(typeof(_self.properties.width)=='number'){
			_self.content_plh.style.width = _self.properties.width+'px';
		}
		else{
			_self.content_plh.style.width = 'auto';
		}
	}
	
	//height
	this.set_height = function(){
		if(typeof(_self.properties.height)=='string'){
			_self.content_plh.style.height = _self.properties.height;
		}
		else if(typeof(_self.properties.height)=='number'){
			_self.content_plh.style.height = _self.properties.height+'px';
		}
		else{
			_self.content_plh.style.height = 'auto';
		}
	}
	
	/**
	 * centers the window on the screen
	 */
	this.center_window = function(){
		var posy = scroll_top + ($(window).height() - $(window_main_container).height()) / 2;
		window_main_container.style.top = posy+'px';
		
		var posx = scroll_left + ($(window).width() - $(window_main_container).width()) / 2;
		
		window_main_container.style.left = posx+'px';
	}
	
	this.get_window_element = function(){
		return window_main_container;
	}
	
	this.set_width();
	this.set_height();
	
	if(typeof(properties.height)=='string'){
		_self.content_plh.style.height = properties.height;
	}
	else if(typeof(properties.height)=='number'){
		_self.content_plh.style.height = properties.height+'px';
	}
	_self.content_plh.className = 'fan_window_content';
	content_line.appendChild(_self.content_plh);
	
	var right_column = document.createElement('td');
	right_column.className = 'fan_window_right';
	content_line.appendChild(right_column);
	
	// bottom
	var bottom_line = document.createElement('tr');
	decoration_tbody.appendChild(bottom_line);
	
	var bottom_left_corner = document.createElement('td');
	bottom_left_corner.className = 'fan_window_bottom_left';
	bottom_line.appendChild(bottom_left_corner);
	
	var bottom_bar = document.createElement('td');
	bottom_bar.className = 'fan_window_bottom';
	bottom_line.appendChild(bottom_bar);
	
	var bottom_right_corner = document.createElement('td');
	bottom_right_corner.className = 'fan_window_bottom_right';
	bottom_line.appendChild(bottom_right_corner);
	
	// close function
	this.close = function(){
		if(typeof(_self.properties.onclose)=='function'){
			_self.properties.onclose();
		}
		container.removeChild(window_main_container);
		fan_windows = fan_splice(fan_windows, _self.id);
	}
	
	
	//close button
	if(typeof(properties.nobar)!='boolean' || !properties.nobar){
		var close_btn = document.createElement('a');
		close_btn.className = 'fan_close_btn';
		close_btn.title = 'close';
		close_btn.innerHTML = 'close';
		close_btn.onclick = this.close;
		top_bar.appendChild(close_btn);
	}
	
	window_main_container.appendChild(fan_window_decoration_layout);
	container.appendChild(window_main_container);
	
	// title
	if(typeof(properties.nobar)!='boolean' || !properties.nobar){
		var window_title = document.createElement('div');
		window_title.className = 'fan_title';
		window_title.title = title;
		window_title.innerHTML = title;
		window_title.style.width = ''+(top_bar.offsetWidth - 70)+'px';
		top_bar.appendChild(window_title);
	}
	
	
	this.hide_content = function(){
		_self.content_plh.style.width = _self.content_plh.clientWidth+'px';
		_self.content_plh.style.height = _self.content_plh.clientHeight+'px';
		_self.content_plh.firstChild.style.display = 'none';
	}
	
	this.show_content = function(){
		_self.set_width();
		_self.set_height();
		_self.content_plh.firstChild.style.display = '';
	}
}


//////////////////////
// static functions //
//////////////////////

/**
 * displays a confirm window
 * 
 * @param string or htmlElement content - the questions to be asked
 * @param object answers - {val1:'caption 1', val2:'caption 2', ...}
 * @param function callback
 * @return fan_window
 */

fan_window.confirm = function(content, answers, callback){
	var container = document.createElement('div');
	
	var question_container = document.createElement('div');
	question_container.className = 'fan_window_confirm_question_container';
	if(typeof(content)=='string'){
		question_container.innerHTML = content;
	}
	else if(typeof(content)=='object' && content){
		question_container.appendChild(content);
	}
	container.appendChild(question_container);
	
	var callback_proxy = function(callback_value, window_closed){
		fan_hide_transparent_bg();
		if(typeof(window_closed)!='boolean' || !window_closed){
			confirmWindow.properties.onclose = false;
			confirmWindow.close();
		}
		callback(callback_value);
	}
	
	var answer_container = document.createElement('div');
	answer_container.className = 'fan_window_confirm_answers_container';
	if(typeof(answers)!='object' || !answers){
		answers = {1:'OK', 0:'Cancel'};
	}
	for(var value in answers){
		var caption = answers[value];
		var answer_elem = document.createElement('input');
		answer_elem.type = 'button';
		answer_elem.value = caption;
		answer_elem.callback_value = value;
		answer_container.appendChild(answer_elem);
		answer_elem.onclick = function(){
			callback_proxy(this.callback_value);
		}
	}
	container.appendChild(answer_container);
	

	fan_show_transparent_bg();
	var confirmWindow = new fan_window(document.body, '', container, {onclose:function(){callback_proxy(false, true)}});
	confirmWindow.center_window();
	return confirmWindow;
}


function fan_GgetMouse(e, object2move){
	var mouseX = (typeof(e)=='object')?e.pageX:event.clientX;
	var mouseY = (typeof(e)=='object')?e.pageY:event.clientY;
	
	if(typeof(object2move.MoveWindow)=='boolean' && object2move.MoveWindow){
		if(!object2move.XmouseStart || !object2move.YmouseStart){
			object2move.XmouseStart = mouseX;
			object2move.YmouseStart = mouseY;
			object2move.XposStart = object2move.offsetLeft;
			object2move.YposStart = object2move.offsetTop;
		}
		
		object2move.style.left = (mouseX - object2move.XmouseStart + object2move.XposStart)+'px';
		object2move.style.top = (mouseY - object2move.YmouseStart + object2move.YposStart)+'px';
	}
	
//	var debug_mes = 'mouseX: '+mouseX+'<br />mouseY:'+mouseY;
//	fan_set_debug_message(debug_mes);
}

var fan_window_debub_plh;
function fan_set_debug_message(message){
	if(typeof(fan_window_debub_plh)!='object'){
		fan_window_debub_plh = document.createElement('div');
		fan_window_debub_plh.className = 'fan_window_debub_plh';
		document.body.appendChild(fan_window_debub_plh);
	}
	fan_window_debub_plh.innerHTML = message;
}

function fan_associativeArrayLength(array){
	var length = 0;
	for (var object in array){
		length++;
	}
	return length;
}

function fan_splice(array, key){
	var new_array = Array();
	for (var object in array){
		if(object!=key){new_array[object]==array[object];}
	}
	return new_array;
}


//modify styles for IE
function fan_makeScrollingBody(){
	if(ie4 && olderBowser){
		var html = document.getElementsByTagName('html');
		var bodyElem = document.getElementsByTagName('body');
		var picturePlaceholderDiv = document.getElementById('picturePlaceholderDiv').style;
		var wait_message_bg = document.getElementById('wait_message_bg').style;
		
		picturePlaceholderDiv.position='absolute';
		wait_message_bg.position='absolute';
		
		html[0].style.setAttribute('height','100%');
		html[0].style.setAttribute('overflow','auto');
		bodyElem[0].style.setAttribute('height','100%');
		bodyElem[0].style.setAttribute('overflow','auto');
	}
}

/**
 * Will set the html opacity
 * 
 * @param element html_element the target element
 * @param int opacity the opacity percent (0-transparent, 100-full opacity) 
 */
function fan_set_opacity(html_element, opacity){
	if(typeof(html_element)=='string'){
		html_element = document.getElementById(html_element);
	}
	
	var index_opacity = opacity / 100;
	
	try{
		html_element.filters.item("DXImageTransform.Microsoft.Alpha").opacity = opacity;
	}
	catch(E){
		html_element.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+opacity+")";
	}
		
	
 	html_element.style.MozOpacity = index_opacity; 
	html_element.style.opacity = index_opacity;
}

function fan_makeUnScrollingBody(){
	if(ie4 && olderBowser){
		var html = document.getElementsByTagName('html');
		var bodyElem = document.getElementsByTagName('body');
		var picturePlaceholderDiv = document.getElementById('picturePlaceholderDiv').style;
		var wait_message_bg = document.getElementById('wait_message_bg').style;
		
		picturePlaceholderDiv.position='';
		wait_message_bg.position='';
		
		html[0].style.setAttribute('height','');
		html[0].style.setAttribute('overflow','');
		bodyElem[0].style.setAttribute('height','');
		bodyElem[0].style.setAttribute('overflow','');
	}
}