window.onload = function()
{
	wm_init();
}

function wm_init()
{
	 show_fck();					
	 addHintInfo();

}

function copyText(obj) 
{
	ie = (document.all)? true:false
	if (ie)
	{
		var rng = document.body.createTextRange();
		rng.moveToElementText($(obj));
		rng.scrollIntoView();
		rng.select();
		rng.execCommand("Copy");
		rng.collapse(false);
		alert( '已经将内容复制到剪贴板，你可以使用Ctrl+v将内容转贴到其他的编辑器上' );
	}
}

function $() {
  var results = [], element;
  for (var i = 0; i < arguments.length; i++) {
    element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    results.push(Element.extend(element));
  }
  return results.length < 2 ? results[0] : results;
}

Object.extend = function(destination, source) {
  for (var property in source) {
    destination[property] = source[property];
  }
  return destination;
}


function show_fck()
{
	var replaced = Array();
	var ii = 0;
	var in_it = 0;
	var obj = document.getElementsByTagName('textarea');
	try
	{
		if( obj.length < 1 )
		{
			return false;
		} 
		
		for( i=0 ;i < obj.length; i++)
		{
			if( obj[i].type == "textarea" && obj[i].getAttribute("rel") == "fck" )
			{
				in_it = 0;
				for( key in replaced )
				{
					if( replaced[key] == obj[i].id )
					{
						in_it = 1;
					} 
				}
				
				if( in_it == 0 )
				{
					//alert(obj[i].id);
			
					replaced[ii] = obj[i].id;
					ii++;

					
					
					var oFCKeditor = new FCKeditor( obj[i].id ) ;
					oFCKeditor.BasePath = "lib/" ;
					if( obj[i].getAttribute("toolbar")  )
					{
						oFCKeditor.ToolbarSet = obj[i].getAttribute("toolbar");
					}
					else
					{
						oFCKeditor.ToolbarSet = "Basic";
					}
					
					// get the width and height of textarea
					//alert( parseInt( obj[i].getAttribute("height") ) );
					if( parseInt( obj[i].getAttribute("height") ) > 0 )
					{
						oFCKeditor.Height = parseInt( obj[i].getAttribute("height") ) ;
						//alert( oFCKeditor.Height );
					}
					
					if( parseInt( obj[i].getAttribute("width") ) > 0 )
					{
						oFCKeditor.Width = parseInt( obj[i].getAttribute("width") ) ;
					}
					oFCKeditor.ReplaceTextarea() ;
				}

				
			}

		}	
	}
	catch (e)
	{
	
	  
	} 
	
	
}

var rate_array = Array();

function rate_set_value( mid , id , value , def )
{
	name = 'rate_' + mid + '_' + id;
	//alert( rate_array['name'] );
	if ( rate_array[name] == value )
	{
		return false;
	}
	else
	{	
		var url = '?a=ajax_show_star&v=' + value + '&mid=' + mid + '&id=' + id + '&d=' + def;
		var pars = '';
		var myAjax = new Ajax.Updater( name , url, {method: 'post', parameters: pars});
		rate_array[name] = value;
	}
}


function zoomin( name )
{
	
	//$(name).style.width = parseInt( $(name).style.width ) + 40 + 'px';
	$(name).style.height = parseInt( $(name).style.height ) + 40 + 'px';
}

function zoomout( name )
{
	
	if( parseInt( $(name).style.width ) > 80 &&  parseInt( $(name).style.height ) > 80  )
	{
		//$(name).style.width = parseInt( $(name).style.width ) - 40 + 'px';
		$(name).style.height = parseInt( $(name).style.height ) - 40 + 'px';
	} 	
}

function set( name , value )
{
	var objs = document.getElementsByName( name );
	if( objs[0] )
	{
		switch( objs[0].type )
		{
			case 'radio': 
				return set_radio( name , value  );
				break;
			case 'select-one': 
				return set_select( name , value  );
				break;
			case 'checkbox': 
				return set_checkbox( name , value  );
				break;
			case 'textarea':
				set_text( name , value );
				break;
			case 'text':
			default: return set_text( name , value );
		}
	}
	
}

function set_text( name , value )
{
	var el = getElement( name );
	el.value = value;
}

 function getElement( name )
 {
	var el = document.getElementsByName( name );
	if( el[0] == null )
	{
		alert( 'cannot find ' + name + ' ! ' );
	}
	else
	{
		return el[0];
	}
	
 }

function set_checkbox( name , value )
{
	var obj = document.getElementsByName( name );
	for(var i=0;i<obj.length;i++)
	{
		if(obj[i].type=="checkbox")
		{
			if( obj[i].value == value )
			{
				obj[i].checked = true;
			}
		}
	}
}

function checkbox_on( name )
{
	var obj = document.getElementsByName( name );
	for(var i=0;i<obj.length;i++)
	{
		obj[i].checked = true;
	}

}

function checkbox_off( name )
{
	var obj = document.getElementsByName( name );
	for(var i=0;i<obj.length;i++)
	{
		obj[i].checked = false;
	}

}

function set_select_text( name , value )
{
	var sel = getElement( name );
	var ops = sel.options;
	for( var i = 0 ; i < ops.length ; i++ )
	{
		if( ops[i].text == value  )
		{
			try
			{
				if( i != ops.selectedIndex )
				{
					ops.selectedIndex = i;
					ops[i].selected = true;
				}
				
			}
			catch( e ) 
			{
				// alert( e.description );
				// ie对于动态生成的下拉框会抛出一个“不能设置selected属性，未指明的错误”的异常
				// 原因不明，先不做处理
			}
			
			
		}
	}
}

function set_select( name , value )
{
	var sel = getElement( name );
	var ops = sel.options;
	for( var i = 0 ; i < ops.length ; i++ )
	{
		if( ops[i].value == value  )
		{
			try
			{
				if( i != ops.selectedIndex )
				{
					ops.selectedIndex = i;
					ops[i].selected = true;
				}
				
			}
			catch( e ) 
			{
				// alert( e.description );
				// ie对于动态生成的下拉框会抛出一个“不能设置selected属性，未指明的错误”的异常
				// 原因不明，先不做处理
			}
			
			
		}
	}
}


function set_radio( name , value )
{
	var objRadio = document.getElementsByName( name );
	for(var i=0;i<objRadio.length;i++)
	{
		if(objRadio[i].type=="radio")
		{
			if( objRadio[i].value == value )
			{
				objRadio[i].checked = true;
			}
		}
	}
}

function parentTag( obj , tag )
{
	var o = obj; 
	if( o.parentNode.nodeName != tag && isNaN( o.parentNode.nodeName) )
	{
		//alert( o.parentNode.nodeName );
		return parentTag( obj.parentNode , tag );
	}
	else
	{
		//alert( obj.parentNode.innerHTML );
		return obj.parentNode;
	}
}

function removeThisRow( obj )
{
	if( confirm( 'delete this row?' ) )
	{
		var i = parentTag( obj , 'TR' );
		//alert( i.parentNode.rows.length );
		if( i.parentNode.rows.length > 2 )
		i.parentNode.deleteRow( i.rowIndex );
	}
	
}

function insertUnderThisRow( obj )
{
	var i = parentTag( obj , 'TR' );
	var n = i.parentNode.appendChild( i.cloneNode( true ) );
	setSameValue( i , n );
}

function moveThisRowUp( obj )
{
	var i = parentTag( obj , 'TR' );
	if( i.rowIndex <= 1 ) return false;
	i.parentNode.rows[i.rowIndex-1].swapNode( i );
}

function moveThisRowDown( obj )
{
	var i = parentTag( obj , 'TR' );
	if( i.rowIndex+1 >= i.parentNode.rows.length ) return false;
	i.parentNode.rows[i.rowIndex+1].swapNode( i );
}

function setSameValue( from , to )
{
	var i = 0; // do not delete this!
	
	if( from.nodeType == 3 ) return false;
	if( from.nodeName == 'A' ) return false;
	if( from.nodeName == 'SPAN' ) return false;
	if( from.nodeName == '#text' ) return false;
	if( from.nodeName == 'SELECT' )
	{
		setSelectSameValue( from , to );
		return false;
	}
	if( from.nodeName == 'INPUT' )
	{
		if( from.getAttribute('type') == 'checkbox' )
			setCheckBoxSameValue( from , to );

		if( from.getAttribute('type') == 'radio' )
			setRadioSameValue( from , to );

		if( from.getAttribute('type') == 'text' )
			to.value = from.value;

		return false;
	}
	

	if( from.hasChildNodes() )
	{
		var names = navigator.appName;
	
		for( i = 0 ; i < from.childNodes.length ; i++ )
		{
			setSameValue( from.childNodes[i] , to.childNodes[i] );
		}
	}
}

function setSelectSameValue( from , to )
{
	to.options[from.selectedIndex].selected = true;
}

function setCheckBoxSameValue( from , to )
{
	if( from.checked ) to.checked = true;
}

function setRadioSameValue( from , to )
{
	if( from.checked ) to.checked = true;
}

function copyText(obj) 
{
	ie = (document.all)? true:false
	if (ie)
	{
		var rng = document.body.createTextRange();
		rng.moveToElementText($(obj));
		rng.scrollIntoView();
		rng.select();
		rng.execCommand("Copy");
		rng.collapse(false);
		alert( '已经将内容复制到剪贴板，你可以使用Ctrl+v将内容转贴' );
	}
}

function setC( txt )
{
	clipboardData.setData( 'Text' , txt );
	alert( '已经将页面地址 ' + txt + ' 复制到剪贴板，你可以使用Ctrl+v粘贴到需要的地方' );
}

function lock_pic_size()
{
	
	obj = document.getElementsByTagName( 'IMG' );
	if( obj.length < 1 )
	{
		return false;
	} 
	
	for( i=0 ;i < obj.length; i++)
	{
      
		if( obj[i].width > 400 )
		{
			
			obj[i].height = (400 * obj[i].height)/ obj[i].width;
			obj[i].width = 400;
		}
	   	
	 } 
}

function removeThisLi( obj )
{
	if( confirm( '真的要删除这行？' ) )
	{
		var i = parentTag( obj , 'LI' );
		if( i.parentNode.childNodes.length > 1 )
			i.parentNode.removeChild(i);
		else
			alert( '最后一行不能被删除' );
	}

}

function addLine( mylist )
{
	var d = document.createElement("LI");
	d.innerHTML = $( mylist ).childNodes[$( mylist ).childNodes.length-1].innerHTML;
	$( mylist ).appendChild(d , null);

	Sortable.create(mylist,{dropOnEmpty:false,containment:[mylist]});
	wm_init();
}

function fixcheck(obj , name , ontext , offtext )
{
	sCheckedT	= '<input name="' + name + '" type="checkbox" checked value="1">' + ontext ;
	sCheckedF	= '<input type="checkbox" value="0"><input type="hidden" value="0" name="' + name + '">' + offtext;

	if(obj.alt)
	{
		obj.innerHTML = sCheckedF;
		obj.alt	= false;
	}else{
		obj.innerHTML = sCheckedT;
		obj.alt	= true;
	}

}

function addHintInfo()
{
	var infos = document.getElementsByTagName( '*' );
	for( var i=0 ; i < infos.length ; i++ )
	{
		if( infos[i].getAttribute('wm_hint') != null)
		{
			
			if( infos[i].nodeType == 3 )
			{
				// text node
				infos[i].onmouseover = function(){ showHintDiv( this );	}
				infos[i].onmouseout = function() { hideHintDiv( this ); }
				infos[i].autocomplete="off";
			}
			else
			{
				infos[i].onfocus = function(){ showHintDiv( this ); }
				infos[i].onblur = function(){ hideHintDiv( this ); 	}
				infos[i].autocomplete="off";
			}
			

		}
	}
}



function hideDiv( obj , names )
{
	if( obj.className == 'input' )
	{
		obj.style.height = 20;			
	}
	$( names ).style.display = 'none';
}

function showDiv( obj , names , notice )
{
	if( obj.className == 'input' )
	{
		if( obj.scrollHeight < 80 )
		{
			obj.style.height = 80;
		}
		else
		{
			obj.style.height = obj.scrollHeight+10;			
		}
	}
	
	var xy = getXY( obj );
	$( names ).style.display = 'block'; 
	$( names ).style.left = xy.l; 
	$( names ).style.top = (xy.t+xy.h)+3;
	if( xy.w < 30 )
		$( names ).style.width = 80;
	else 
		$( names ).style.width = xy.w;
	if( isNull(notice) )
		$( names ).innerHTML = ' ↑ ' + obj.getAttribute( 'wm_hint' );
	else
		$( names ).innerHTML = ' ↑ ' + notice;
}

function showHintDiv( obj , notice )
{
	showDiv( obj , 'div_hint' , notice );

}

function hideHintDiv(obj)
{
	hideDiv( obj , 'div_hint' );
}


function showNavDiv( obj , notice )
{
	showDiv( obj , 'div_navbox' , notice );

}

function hideNavDiv(obj)
{
	hideDiv( obj , 'div_navbox' );
}


function isNull(_sVal){return (_sVal == "" || _sVal == null || _sVal == "undefined");}
function getXY(s)
{
	var oObj = typeof(s) == 'object' ? s : $(s);
	var iTop = 0;
	var iLeft = 0;
	var iWidth = oObj.offsetWidth;
	var iHeight = oObj.offsetHeight;
	while(oObj != document.body){
		iLeft += oObj.offsetLeft;
		iTop += oObj.offsetTop;
		oObj = oObj.offsetParent;
	}
	return {l:iLeft, t:iTop, w:iWidth, h:iHeight};
}

// some codes from lightbox 
//	by Lokesh Dhakar - http://www.huddletogether.com
// ------------------------------------------------------
//	Credit also due to those who have helped, inspired, and made their code available to the public.
//	Including: Scott Upton(uptonic.com), Peter-Paul Koch(quirksmode.org), Thomas Fuchs(mir.aculo.us), and others.

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

if (!window.Element)
  var Element = new Object();
//
//	Additional methods for Element added by SU, Couloir
//	- further additions by Lokesh Dhakar (huddletogether.com)
//
Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setLeft: function(element,t) {
	   	element = $(element);
    	element.style.left = t +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});

// -----------------------------------------------------------------------------------

//
//	Extending built-in Array object
//	- array.removeDuplicates()
//	- array.empty()
//
Array.prototype.removeDuplicates = function () {
	for(i = 1; i < this.length; i++){
		if(this[i][0] == this[i-1][0]){
			this.splice(i,1);
		}
	}
}

// -----------------------------------------------------------------------------------

Array.prototype.empty = function () {
	for(i = 0; i <= this.length; i++){
		this.shift();
	}
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function showSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

function closeDivWindow()
{
	$( 'wm_div_widow' ).style.display = 'none';
	$( 'wm_div_msg_box' ).style.display = 'none';
	showSelectBoxes();
	void(0);
}

function closeDivInfoBox()
{
	$( 'wm_div_msg_box' ).style.display = 'none';
	showSelectBoxes();
	void(0);
}

function showDivInfoBox( info )
{
	hideSelectBoxes();
	
	if( isNull($( 'wm_div_msg_box' ))  )
	{
		var objBox = document.createElement("div");
		//objBox.setAttribute('id','wm_div_msg_box');
		//objBox.style.display = 'none';
		//objBox.onclick = function() { objOverlay.style.display = 'none';objBox.style.display = 'none'; showSelectBoxes();return false; }
		//objBox.onclick = function() { objOverlay.style.display = 'none'; return false; }
		document.getElementsByTagName("body").item(0).appendChild(objBox);
		
	
	}

	table_html = '<div id="wm_div_msg_box"  style="position: absolute;padding:5px;top:80;left:200;width:400;border:1px solid #e8e8e8;background:#FFF;z-index:100;"><table width="100%"><tr><td width="165px"><img src="res/img/admin/alert.gif"></td><td align="left"><h3>系统提示2</h3>'
	+ info +  '</td></tr><tr><td colspan=2><p align="right" ><a href="javascript:closeDivInfoBox()">close</a> &raquo; &nbsp;&nbsp;</p></td></table></div>';
	objBox.innerHTML = table_html;
	//Element.setInnerHTML( 'wm_div_msg_box' , table_html );
	//Element.setTop('wm_div_msg_box', 80);
	//alert( $('wm_div_msg_box').style. );
	//Element.setLeft('wm_div_msg_box', 200  );
	Element.show('wm_div_msg_box');
}

function showDivWindow( info )
{
	// 创建对象
	if( isNull($( 'wm_div_widow' ))  )
	{
		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','wm_div_widow');
		objOverlay.style.display = 'none';
		objOverlay.onclick = function() { objOverlay.style.display = 'none';objBox.style.display = 'none'; showSelectBoxes();return false; }
		document.getElementsByTagName("body").item(0).appendChild(objOverlay);

		var objBox = document.createElement("div");
		objBox.setAttribute('id','wm_div_msg_box');
		objBox.style.display = 'none';
		//objBox.onclick = function() { objOverlay.style.display = 'none';objBox.style.display = 'none'; showSelectBoxes();return false; }
		//objBox.onclick = function() { objOverlay.style.display = 'none'; return false; }
		document.getElementsByTagName("body").item(0).appendChild(objBox);


	}

	// 显示对象
	hideSelectBoxes();
	var arrayPageSize = getPageSize();
	Element.setHeight('wm_div_widow', arrayPageSize[1]);
	new Effect.Appear('wm_div_widow', { duration: 0.2, from: 0.0, to: 0.8 });

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 15) + 40 ;

	table_html = '<table width="100%"><tr><td width="165px"><img src="res/img/admin/alert.gif"></td><td align="left"><h3>系统提示</h3>'
	+ info +  '</td></tr><tr><td colspan=2><p align="right" ><a href="javascript:closeDivWindow()">close</a> &raquo; &nbsp;&nbsp;</p></td></table>';
	Element.setInnerHTML( 'wm_div_msg_box' , table_html );
	Element.setTop('wm_div_msg_box', lightboxTop);
	//alert( $('wm_div_msg_box').style. );
	Element.setLeft('wm_div_msg_box', arrayPageSize[0]/2 - 200  );
	Element.show('wm_div_msg_box');



}

function is_all_fill( name )
{
	inputs = document.getElementsByName( name );
	if( !isNull( inputs ) )
	{
		for( var i = 0; i < inputs.length ; i++ )
		{
			if( isNull( inputs[i].value ) )
			{
				return false;
			}
		}

	}

	return true;
	
}

function data_field_submit_check()
{
	if( !is_all_fill( 'fields[dy_en_name][]' ) )
	{
		showDivWindow( '字段英文名不能为空!' );
		return false;
	}

	if( !is_all_fill( 'fields[dy_cn_name][]' ) )
	{
		showDivWindow( '字段中文名不能为空!' );
		return false;
	}
	
	if(check_data_field_exists('fields[dy_en_name][]','字段英文名')
		&&check_data_field_exists('fields[dy_cn_name][]','字段中文名'))
	{
		return true;
	}

	return false;
}

function check_data_field_exists( name , text )
{
	if( isNull( text ) ){ text = name; }
	var earr = Array();
	var myii = 0;
	inputs = document.getElementsByName( name );
	if( !isNull( inputs ) )
	{
		for( var i = 0; i < inputs.length ; i++ )
		{
			// 形成列表
			earr[myii] = inputs[i].value;
			myii++;
		}

		for( var i = 0; i < inputs.length ; i++ )
		{
			if( in_arrays( inputs[i].value , earr ) > 1  )
			{
				showDivWindow( '<font color=red>' + text + '在本页面上有重复，值为' + inputs[i].value + '</font>'  );
				$(name).value = '';
				return false;
			}
		}

		return true;
	}
	else
	{
		return false;
	}

}

function in_arrays( value , arr )
{
	var mycount = 0;
	for( var ii = 0 ; ii < arr.length ; ii++ )
	{
		//alert( 'fff' );
		if( arr[ii] == value )
		{
			mycount++;
		}
	}
	return mycount;

}











