$(document).ready(function () {
	ed=new Array();
	block_element=null;
	persent=10;
	var tm;
	var json_data={};
	
	$('#error_dialog_div').dialog({
		autoOpen:false,
		buttons: {
			'確定':function(c,e){
				$(this).dialog('close');
			}
		},
		draggable:false,
		resizable:false,
		title:'系統發生錯誤',
		modal: true,
		close:function(event, ui){
			if(json_data.data.error_code=='0001'){
				window.location.replace(json_data.data.url);
			}
			if(json_data.data.error_code=='0002'){
				window.location.reload();
			}
		}
	});

	$.ajaxSetup({
		timeout:10000,
 		beforeSend: function(){
 			if(block_element!=null){
 				$("#progressbar_div").progressbar({'value':persent});
				block_element.block({
				showOverlay: false, 
				centerY: false, 
				centerX: false, 
            	message: $('#progressbar_div'),
            	overlayCSS:  {  
        			backgroundColor:'#fff',  
		        	opacity:'0.3'  
    			}, 
            	css:{
	            	border:'none',
    	        	backgroundColor: '#fff', 
        	    	'-webkit-border-radius': '10px', 
            		'-moz-border-radius': '10px' 
            	}
        	});
			tm=$.timer(1000, function (timer) {
				persent+=10;
				if(persent>100){
					persent=100;
					timer.stop();
				}
				$("#progressbar_div").progressbar({'value':persent});
		  	});
		  }	
 		},
 		complete: function(){
 			if(block_element!=null){
 				block_element.unblock();
 				$("#progress_dialog_div").hide();
				persent=10;
				tm.stop();
			}
 		},
 		error:function(e){
 			json_data={data:{msg:'錯誤:'+e.status,error_code:e.status}};
	 		error_dialog();
 		},
	 	ajaxError:function(e){
 				
 		}
	});

	error_dialog=function(src_data){
	
		if(src_data!=null){
			json_data=src_data;
		}
		$('#error_dialog_div').html(json_data.data.msg);
		$('#error_dialog_div').dialog('open');
	};


tinymce_simple_cfg={
	language:'tw',
	mode : "textareas",
	theme : "advanced",
	convert_urls : false,
	apply_source_formatting : true,
	verify_html : true,
	theme_advanced_resizing : false,
	relative_urls : false,
	remove_script_host : false,
	elements:'comment',
	flash_wmode : "transparent",
	flash_quality : "high",
	flash_menu : "false",
	cleanup : true,
	plugins : "paste,safari,contextmenu",
	theme_advanced_toolbar_location : "top",
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,separator,bullist,numlist,undo,redo",
	theme_advanced_buttons2 : "",
	theme_advanced_buttons3 : "",
	valid_elements : "strong,b,i,u,ul,ol,li,p,br,strike"
};
tinymce_admin_cfg={
	language:'tw',
	mode : "textareas",
	theme : "advanced",
	convert_urls : false,
	apply_source_formatting : true,
	verify_html : true,
	theme_advanced_resizing : false,
	relative_urls : false,
	remove_script_host : false,
	elements:'comment',
	flash_wmode : "transparent",
	flash_quality : "high",
	flash_menu : "false",
	cleanup : true,
	plugins : "paste,safari,contextmenu",
	theme_advanced_toolbar_location : "top",
	theme_advanced_buttons1 : "bold,italic,underline,separator,bullist,numlist,undo,redo,link,unlink",
	theme_advanced_buttons2 : "",
	theme_advanced_buttons3 : "",
	valid_elements : "a[name|href|target|title|onclick],font[color],strong,b,i,u,ul,ol,li,p,br,strike"
};


	
	
	debug1=function(obj){
		try{
			console.log(obj);
		}
		catch(e){
			alert(obj);
		}
	}
	pager=function(target_div,now_page,total_pages,start,end,QueryString){
		var content='';
		QueryString=remove_parameter(QueryString,['page']);
		QueryString=cut_tail(QueryString,'&');
		
		if(now_page>1){
			content+='<a href="?'+QueryString+'&page='+1+'">&nbsp;最前頁&nbsp;</a>';
			content+='<a href="?'+QueryString+'&page='+(now_page-1)+'">&nbsp;上一頁&nbsp;</a>';
		}
		if(now_page<total_pages){
			content+='<a href="?'+QueryString+'&page='+(now_page+1)+'">&nbsp;下一頁&nbsp;</a>';
			content+='<a href="?'+QueryString+'&page='+total_pages+'">&nbsp;最末頁&nbsp;</a>';
		}
		content+='&nbsp;頁數:';
		for(i=start;i<=end;i++){
			if(i==now_page){
				content+='<span>&nbsp;'+i+'&nbsp;</span>';
			}
			else{
				content+='<a href="?'+QueryString+'&page='+i+'">&nbsp;'+i+'&nbsp;</a>';
			}
		}
		$('#'+target_div).html(content);
	}

	cut_tail=function(str,tail){
		str=$.trim(str);
		if(str.length>0){
			if(str.substr(str.length-1,str.length)==tail){
				str=str.substr(0,str.length-1);
			}
		}
		return str;
	}
	
	//計算關鍵字出現的次數
	count_word=function(str,keyWord){
		var index = 0;
		var count = 0;
		while (index != -1){
			index = str.indexOf(keyWord);
			if (index != -1){
				index = index + keyWord.length;
				str=str.substr(index,str.length);
				count++;
			}
		}
		return count;
	};
	
	remove_parameter=function(queryString,parameters){
		var result='';
		var each_parameter=queryString.split("&");
		var duplicated=false;
		for (var i=0 ; i<each_parameter.length; i++){
			var each_value=each_parameter[i].split("=");
			//each_value[0]=key,each_value=value
			for(var j=0;j<parameters.length;j++){
				if(each_value[0]==parameters[j]){
					duplicated=true;
					break;
				}
			}
			if(!duplicated){
				result+=(result!='')?'&':'';
				result+=each_parameter[i];
			}
			duplicated=false;
		}
		result=result.replace(/&&/g,"")
		return result;
	};
	open_win=function(url,params){
		return false;
		var the_win=Ext.getCmp('the_win');
		if(the_win){

			Ext.getCmp('the_win').close();
		}
		the_win = new Ext.Window({
			id:'the_win',
			layout:'fit',
			xtype:'window',
			modal: true,
			resizable:false,
			stateful:false,
	        closeAction:'close',
	        keys: {
				key: Ext.EventObject.ESC, // Esc key
				handler: function(){ the_win.close(); },
				scope: this
			}
		});
		the_win.setSize(400,400);
		the_win.show(the_win);
		if(!the_win.hasListener('close')){
			the_win.on('close',function(){
				event_remove();
			});
		}
		var mgr = the_win.getUpdater();
		mgr.update({
			url: url,
			scripts:true,
			nocache:true,
			params: params
		});
		the_win.syncSize();
		
	};
	event_remove=function(){
		try{
			while(ed.length>0){
				var aed=ed.pop();
				aed.remove();
			}
			if(runner!=null){
				try{
					 runner.stopAll();
				}catch(ex){
				}
			}
		}
		catch(e){
			//debug1(e.toString());
		}
	};
	menu_zap=function(parent_obj,target_id){
		
		var parent_open_class_name="archive_parent_open";
		var parent_close_class_name="archive_parent_close";
		if($(parent_obj).hasClass(parent_close_class_name)){
			$('#'+target_id).fadeIn('fast');
			$(parent_obj).removeClass(parent_close_class_name);
			$(parent_obj).addClass(parent_open_class_name);
		}
		else{
			$('#'+target_id).fadeOut('fast');
			$(parent_obj).removeClass(parent_open_class_name);
			$(parent_obj).addClass(parent_close_class_name);
		}
	}
	refresh_valid_code=function(src_img_id){
		$('#'+src_img_id).attr('src','/valid_code.jsp?timestamp='+new Date().getTime());
	}
	get_ip=function(prompt_title,ip){
		prompt(prompt_title,ip);
		return;
	}
	widget_rss=function(widget_div_id){
		
		$.each($('img[rel=\'widget_tool_bar_toggle_box\']'),function(i,n){
			$('#'+$(this).attr('target_id')).fadeOut('fast');
			$(this).click(widget_toggle);
		});
	};
	widget_toggle=function(event,dom,obj){
		
		if($('#'+$(this).attr('target_id')).css('display')!='none' ){
			//Es visible.
			$(this).attr('src','images/zippy2.gif');
			$(this).attr('title','開啟');
			$('#'+$(this).attr('target_id')).fadeOut('fast');
		}
		else{
			//No es Visible.
			$(this).attr('src','images/zippy.gif');
			$(this).attr('title','折合');
			$('#'+$(this).attr('target_id')).fadeIn('fast');
		}
	};
});
