/*
侧边栏，静态树
*/
if(jQuery) (function($){
	
	$.extend($.fn, {
		nc_STree: function(o, h) {
			// Defaults
			
			if( !o ) var o = {};
			if( o.root == undefined ) o.root = '/';
			if( o.folderEvent == undefined ) o.folderEvent = 'click';
			if( o.expandSpeed == undefined ) o.expandSpeed= 500;
			if( o.collapseSpeed == undefined ) o.collapseSpeed= 500;
			if( o.expandEasing == undefined ) o.expandEasing = null;
			if( o.collapseEasing == undefined ) o.collapseEasing = null;
			if( o.multiFolder == undefined ) o.multiFolder = false;
			
			$(this).each( function() {
				
				function showTree(c, t) {
					//if( o.root == t ) $(c).find('UL:hidden').show(); else $(c).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
					bindTree(c);
				}
				
				function bindTree(t) {
					$(t).find('B A').bind(o.folderEvent, function() {
						
						var li = $(this).parent().parent().parent();
						//alert(li.html());
						if(!li.hasClass('empty') ) {
							if( li.hasClass('collapsed') ) {
								// Expand
								if( !o.multiFolder ) {
									li.parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
									li.parent().find('LI.expanded').removeClass('expanded').addClass('collapsed');
								}
								//$(this).parent().find('UL').remove(); // cleanup
								li.find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
								li.removeClass('collapsed').addClass('expanded');
							} else {
								// Collapse
								li.find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
								li.removeClass('expanded').addClass('collapsed');
							}
						} else {
							if(typeof(h)=='function'){h($(this).attr('rel'));}
						}
						return false;
					});
					// Prevent A from triggering the # on non-click events
					//if( o.folderEvent.toLowerCase != 'click' ) $(t).find('LI A').bind('click', function() { return false; });
				}
				// Loading message
				//$(this).html('<ul class="jqueryFileTree start"><li class="wait">' + o.loadMessage + '<li></ul>');
				// Get the initial file list
				showTree( $(this), escape(o.root) );
			});
		}
	});
	
})(jQuery);


if(jQuery) (function($){
	
	$.extend($.fn, {
		nc_Tab: function(o, h) {
			// Defaults
			
			if( !o ) var o = {};
			if( o.root == undefined ) o.root = '/';
			if( o.folderEvent == undefined ) o.folderEvent = 'click';
			if( o.expandSpeed == undefined ) o.expandSpeed= 300;
			if( o.collapseSpeed == undefined ) o.collapseSpeed= 300;
			if( o.expandEasing == undefined ) o.expandEasing = null;
			if( o.collapseEasing == undefined ) o.collapseEasing = null;
			if( o.multiFolder == undefined ) o.multiFolder = false;
			
			$(this).each( function() {
				
				function showTree(c, t) {
					//if( o.root == t ) $(c).find('UL:hidden').show(); else $(c).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
					bindTree(c);
				}
				
				function bindTree(t) {
					$(t).find('h3').bind(o.folderEvent, function() {
						var li = $(this).parent();
						
						if(!li.hasClass('empty') ) {
							if( li.hasClass('normal') ) {
								// Expand
								if( !o.multiFolder ) {
									li.parent().find('h1').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
									li.parent().find('LI.selected').removeClass('selected').addClass('normal');
								}
								//$(this).parent().find('UL').remove(); // cleanup
								li.find('h1:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
								li.removeClass('normal').addClass('selected');
							} else {
								// Collapse
								li.find('h1').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
								li.removeClass('selected').addClass('normal');
							}
						} else {
							if(typeof(h)=='function'){h($(this).attr('rel'));}
						}
						return false;
					});
					// Prevent A from triggering the # on non-click events
					//if( o.folderEvent.toLowerCase != 'click' ) $(t).find('LI A').bind('click', function() { return false; });
				}
				// Loading message
				//$(this).html('<ul class="jqueryFileTree start"><li class="wait">' + o.loadMessage + '<li></ul>');
				// Get the initial file list
				showTree( $(this), escape(o.root) );
			});
		}
	});
	
})(jQuery);


function _showAllBricks()
{
	if($.browser.msie){
		$(document).ready(function(){doShowAllBricks();});
	}
	else{
		addOnloadEvent(function(){doShowAllBricks();});
	}	
}

function doShowAllBricks()
{
	if(modelFrame==null){
		if($.browser.msie){
			modelFrame = $(window.frames[modelFrameID].document);
		}
		else{
			modelFrame = $(document.getElementById(modelFrameID).contentDocument);
		}
	}	
	modelFrame.find("div").each(function(){
		var _h =' '+$(this).html()+' ';	
		if(/brick|picbox/.test(_h)){			
			$(this).after('<div id="'+$(this).attr("id")+'_s">测试:'+$(this).attr("id")+'</div>');
		}
	});
	modelFrame.find("div").each(function(){										 
		var brickTpl = $(this).html()+'';
		var testDomID = $(this).attr("id")+'_s';
		if(/(brick)/.test(brickTpl)){			
			eval(brickTpl.replace(/\)/gi,',"'+$(this).attr("id")+'")'));
			//$(this).css('display','none');
		}
		else if(/picbox/.test(brickTpl)){
			eval(brickTpl.replace(/\[id\]/gi,$(this).attr("id")+'_s'));
			modelHTML = '';			
			//$(this).css('display','none');
		}
		else if(/_s/.test($(this).attr("id"))){
			if(modelHTML&&modelHTML!='')
			{
				modelHTML = modelHTML.replace(/\[html\]/gi,$(this).html());
				modelHTML = modelHTML.replace(/\[id\]/gi,$(this).attr("id"));
				$(this).html(modelHTML);
				$(this).attr("className",$(this).attr("id").replace('_s',''));
				modelHTML = '';
			}
		}
		else{
			//$(this).css('display','none');
		}
	});
}
var modelFrameID = "model";

if(parent.window == window ){
	//小部件调试模式 开始
	//document.writeln('<iframe src="model.html" id="'+modelFrameID+'" style="width:100%; height:300px"></iframe>');
	//_showAllBricks();
	//调试模式 结束

	//小部件运行模式
	document.writeln('<iframe src="/js/model.html" id="'+modelFrameID+'" style="display:none"></iframe>');
	//document.writeln('<iframe src="js/model_r.html" id="'+modelFrameID+'_r" style="display:none"></iframe>');
}

function addOnloadEvent(_event)
{
	if(window.attachEvent)
	{
		window.attachEvent("onload",  _event);
	}
	else if(window.addEventListener)
	{
		window.addEventListener('load',  _event,  false);
	}
}

//小部件 引擎

var modelHTML='';
var modelFrame=null;

addOnloadEvent(
function(){setTimeout(hackInputFocus,100);});

function hackInputFocus()
{
	if($.browser.msie){
		$("input[@type='text'], input[@type='password'], textarea").focus(function() {$(this).css({background:"#fff",border:"#a0c6d2 1px solid",color:"#408ea8"});})
		$("input[@type='text'], input[@type='password'], textarea").blur(function() {$(this).css({background:"#fefefe",border:"#dddddd 1px solid",color:"#9c9b9b"});})
	}
}

function inputfocus(dom)
{
	if(typeof(dom._v)=='undefined')
	{
		dom._v = dom.value;		
		dom.value = '';
	}
	else{
		if(dom.value=='')
		{
			dom.value = dom._v;
		}
		else if(dom.value==dom._v)
		{
			dom.value = '';
		}
	}
}



function brick(baseDomID,brickDomID)
{
	var reg = new RegExp(baseDomID, "gi")
	modelHTML = modelFrame.find("#"+baseDomID).html().replace(reg,brickDomID);	
}

function hack(toid,fromid)
{
	if($.browser.msie){
		//$(document).ready(function(){$(toid).addClass(fromid);return doHackOneFromIFrame(modelFrameID,toid,fromid);});
		addOnloadEvent(function(){$(toid).addClass(fromid);return doHackOneFromIFrame(modelFrameID,toid,fromid);});
	}
	else{
		addOnloadEvent(function(){$(toid).addClass(fromid);return doHackOneFromIFrame(modelFrameID,toid,fromid);});
	}
}

function hackagain(toid,fromid)
{
	$(toid).addClass(fromid);return doHackOneFromIFrame(modelFrameID,toid,fromid);
}



function picbox(boxid,bgColor,corner_pic,vborder_pic, hborcer_pic,corner_size,padding){
	var box = $(boxid);
	if(modelFrame==null){
		initModelFrame(modelFrameID);
	}
	if(box.length<=0){
		box = modelFrame.find(boxid)
	}
	if(box.length>0){
		box.each(function(){
		var l ='';
		l += '<div style="background:url('+corner_pic+') no-repeat top right;height:'+corner_size+'px; overflow:hidden">';
    	l += '<dl style="margin:0px;padding:0px;background:url('+corner_pic+') no-repeat top left; height:'+corner_size+'px; overflow:hidden">';                  
        l += '<dt style="background:url('+vborder_pic+') top repeat-x;height:'+corner_size+'px; margin-left:'+corner_size+'px; margin-right:'+corner_size+'px;"></dt>';
    	l += '</dl><div class="clear"></div>';
		l += '</div>';
		l += '<div style="background:url('+hborcer_pic+') left repeat-y; width:100%">';
    	l += '<dl style="margin:0px;background:url('+hborcer_pic+') right repeat-y;">';
        l += '<dt style="margin:0px '+corner_size+'px 0px '+corner_size+'px; padding:'+padding+'px;background: '+bgColor+'"><div>';
		
		var r ='';
		r += '<div class="clear"></div></div></dt> ';
    	r += '</dl> ';
		r += '</div>';
		r += '<div style="background:url('+corner_pic+') no-repeat bottom right;_background:url('+corner_pic+') no-repeat -'+corner_size+'px right;height:'+corner_size+'px; overflow:hidden">';
    	r += '<dl style="margin:0px;background:url('+corner_pic+') no-repeat bottom left;_background:url('+corner_pic+') no-repeat -'+corner_size+'px left; height:'+corner_size+'px;">';             
        r += '<dt style="background:url('+vborder_pic+') bottom repeat-x;_background:url('+vborder_pic+') 0px -'+corner_size+'px repeat-x;height:'+corner_size+'px; margin-left:'+corner_size+'px; margin-right:'+corner_size+'px"></dt>';
    	r += '</dl><div class="clear"></div>';
		r += '</div>';
		//alert($(this).html());
		$(this).html(l+$(this).html()+r);
		
		//document.write(l+box.innerHTML+r);
		});
	}	
	else{
		//alert('找不到'+boxid);
		//setTimeout(function(){return tc_box(boxid,bgColor,corner_pic,vborder_pic, hborcer_pic,corner_size,padding)},200);
	}
}

function hackimg()
{
	$('.img').html('');
	//addOnloadEvent(function(){$(toid).addClass(fromid);return doHackOneFromIFrame(modelFrameID,toid,fromid);});
}

function hacknav(navid)
{
	var nav = $('#'+navid);
	nav.html('<table cellspacing=0 style="margin:0 auto;" align="center"><tr><td>'+nav.html().replace(/&nbsp;/gi,'')+'</td></tr></table>');
	nav.find("a").each(function(){
		switch($(this).html())
		{
			case '&lt;&lt;':
				$(this).css('display','none');
				//if($(this).attr("disabled")=='disabled'){
//					$(this).attr('className','')
//				}
//				else{
//					$(this).html('&lt;&lt;ooo')
//				}
				break;
			case '&lt;':
				$(this).html('<b></b><div></div><b></b>');
				$(this).attr('className','prev');
				if($(this).attr('disabled')){
					$(this).addClass('navdisable');
				}
				$(this).attr('disabled','');
				break;
			case '&gt;&gt;':
				$(this).css('display','none');
				break;
			case '&gt;':
				$(this).html('<b></b><div></div><b></b>');
				$(this).attr('className','next');
				if($(this).attr('disabled')){
					$(this).addClass('navdisable');
				}
				$(this).attr('disabled','');
				break;
			default:
				$(this).attr('className','page');
				if($(this).attr('disabled')){
					$(this).addClass('navdisable');
				}
				break;
		}		
	});
	nav.find("span").each(function(){
		if($(this).find("a").length>0){}
		else{
			$(this).attr('className','curpage');
			if($(this).attr('disabled')){
				$(this).addClass('navdisable');
			}
		}
	});
}

function dialog_Ex(dialogid, title, msgtype,btnnum) {
	$('#dialog_'+dialogid).modal({
		close:false, 
		overlayId:'confirmOverlay',
		containerId:'confirmContainer', 
		onShow: function (dialog) {			
			dialog.data.find('.title').html(title);
			dialog.data.find('h1 div b:eq(0)').addClass(msgtype);
			if(btnnum<2){
				dialog.data.find('.dialog_btn li:eq(1)').css('display','none');
				dialog.data.find('.dialog_btn').removeClass('twobtn').addClass('onebtn');
			}
			else{
				dialog.data.find('.dialog_btn li:eq(1)').css('display','');
				dialog.data.find('.dialog_btn').removeClass('onebtn').addClass('twobtn');
			}
			// if the user clicks "yes"
			dialog.data.find('.yes').click(function () {
				// call the callback
				var callback = eval('DialogCallBack_'+dialogid);
				if ($.isFunction(callback)) {
					callback.apply();
				}
				// close the dialog
				$.modal.close();
			});
		}
	});
	$('#confirmContainer').easydrag();
}

function confirm(title, message, msgtype,btnnum, callback) {
	$('#confirm').modal({
		close:false, 
		overlayId:'confirmOverlay',
		containerId:'confirmContainer', 
		onShow: function (dialog) {
			dialog.data.find('.msg').html(message);
			dialog.data.find('.title').html(title);
			dialog.data.find('h1 div b:eq(0)').addClass(msgtype);
			if(btnnum<2){
				dialog.data.find('.dialog_btn li:eq(1)').css('display','none');
				dialog.data.find('.dialog_btn').removeClass('twobtn').addClass('onebtn');
			}
			else{
				dialog.data.find('.dialog_btn li:eq(1)').css('display','');
				dialog.data.find('.dialog_btn').removeClass('onebtn').addClass('twobtn');
			}
			// if the user clicks "yes"
			dialog.data.find('.yes').click(function () {
				// call the callback
				if ($.isFunction(callback)) {
					callback.apply();
				}
				// close the dialog
				$.modal.close();
			});
		}
	});
	$('#confirmContainer').easydrag();
}

function doHackOneFromIFrame(frameid,toid,fromid)
{
	if(modelFrame==null){
		initModelFrame(frameid);
	}	
	
	var brickTpl = modelFrame.find("#"+fromid).html().replace(/\);/gi,',"'+fromid+'");');
		
	$(toid).each(function(){
		eval(brickTpl.replace(/\[id\]/g,$(this).attr("id")));
		//alert(brickTpl.replace(/\[id\]/gi,$(this).attr("id")));
		if(/brick/.test(brickTpl))
		{
			
			modelHTML = modelHTML.replace(/\[html\]/gi,$(this).html());
			modelHTML = modelHTML.replace(/\[id\]/gi,$(this).attr("id"));
			$(this).html(modelHTML);
			$(this).addClass(fromid+' '+/brick\(['|"]([\s\S]*?)['|"]/.exec(brickTpl)[1]);
		}
	});
}

function initModelFrame(frameid)
{
    if($.browser.msie){
		try{
			modelFrame = $(window.frames[frameid].document);
		}
		catch(e)
		{
			modelFrame = $(window.frames[frameid+'_r'].document);
		}
	}
	else{
		try{
			modelFrame = $(document.getElementById(frameid).contentDocument);
		}
		catch(e)
		{
			modelFrame = $(document.getElementById(frameid+'_r').contentDocument);
		}
	}
}

function checkform()
{
    $('input').each(function(){
		alert($(this)._v);
	});
}