/*
 * jQuery SyntaxHighlighter Plugin
 * version: 1.1
 * @requires jQuery v1.3.2 or later
 *
 * Copyright (c) 2008 AlloVince
 * Examples at: http://allo.ave7.net/JQuery_with_SyntaxHighlighter
 * Modified by millken 2010-1-21
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 */
if(jQuery) (function($){
$.extend($, {
	SyntaxHighlighter: function (option) {
			//原有设置
			var highlighter_name = option.name!= undefined ?  option.name : "code";
			//插件设置
			if(typeof(option) == "string") {
				var dir = option;
			} else if(typeof(option) == "object") {
				var dir = option.dir;
			}
			var autofind = option.autofind != undefined ? option.autofind : true;
			var jspath = option.jspath ? option.jspath : dir + "scripts/";
			var csspath = option.csspath ? option.csspath : dir + "styles/";
			var swfpath = option.swfpath  ? option.swfpath : dir + "scripts/";
			var highlighter = {
				Cpp:{
					alias:"cpp",
					has:false
				},
				CSharp:{
					alias:"c#,c-sharp",
					has:false
				},
				Css:{
					alias:"css",
					has:false
				},
				Delphi:{
					alias:"pascal",
					has:false
				},
				Java:{
					alias:"java",
					has:false
				},
				JScript: {
					alias:"js,javascript",
					has:false
				},
				Php:{
					alias:"php",
					has:false
				},
				perl:{
					alias:"perl",
					has:false
				},
				Ruby:{
					alias:"rails,ror,ruby",
					has:false
				},
				Sql:{
					alias:"sql",
					has:false
				},
				Vb:{
					alias:"vb,net,asp",
					has:false
				},
				Xml:{
					alias:"xml,html,xhtml,xslt",
					has:false
				},
				Bash:{
					alias:"bash,shell",
					has:false
				},
				Scala:{
					alias:"scala",
					has:false
				},
				Groovy:{
					alias:"groovy",
					has:false
				},
				AS3:{
					alias:"as3",
					has:false
				}
			}
			var highlighter_count = [];
			if(autofind == true) {
				//自动寻找条件1:有class
				var finds = "pre[class^=brush]";
			}
			else {
				var finds = "pre[name='" + highlighter_name + "'][class]";
			}		
			//计数
			$(finds).each(function(){
				var code_type = $(this).attr("class");
				//code_type = $.trim(code_type.split(":")[1].replace(';',''));
				code_type = (code_type.split(":")[1]).split(";")[0];
				if($(this).css("display") == "none") {
					$(this).attr("name",highlighter_name + '_lighted');
					//continue;
				}
				
				//自动寻找条件2:未定义name
				if ($(this).attr("name") == undefined || $(this).attr("name") == highlighter_name) {
					for (var types in highlighter) {
						if (types == code_type) {
							if (highlighter[types].has == false) {
							highlighter_count.push(types);
							}
							highlighter[types].has = true;
							$(this).attr("name", highlighter_name);
							break;
						}
						//别名
						else if (highlighter[types].alias) {
							var alias = highlighter[types].alias.split(",");
							for (var i = 0; i < alias.length; i++) {
								if (code_type == alias[i]) {
									if (highlighter[types].has == false) {
										highlighter_count.push(types);
									}
									highlighter[types].has = true;
									$(this).attr("name", highlighter_name);
									break;
								}
							}
						}
					}
				}
			});
		
		if(highlighter_count.length > 0) {			
			$("head").append("<script type=\"text/javascript\" src=\""+jspath+"shCore.js\" /></script>");
			$("head").append("<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"" + csspath + "shCore.css\" />");
			$("head").append("<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"" + csspath + "shThemeDefault.css\" />");
			i = 0;
				for(var types in highlighter_count) {
					var jsfile = jspath + "shBrush" + highlighter_count[types] + ".js";
					$("head").append("<script type=\"text/javascript\" src=\""+jsfile+"\" /></script>");
						i++;
						if(i == highlighter_count.length){
							SyntaxHighlighter.config.strings = {
								expandSource : '展开代码', 
								viewSource : '查看代码', 
								copyToClipboard : '复制代码', 
								copyToClipboardConfirmation : '代码已成功复制到您的剪切版', 
								print : '打印', 
								help : '?', 
								alert: '『温馨提示』', 
								noBrush : '不能找到刷子: ', 
								brushNotHtmlScript : '刷子没有配置html-script选项', 
								aboutDialog : 'emlog for SyntaxHighlighter'	
							}
							SyntaxHighlighter.config.clipboardSwf = jspath + 'clipboard.swf';
							SyntaxHighlighter.all();
						}
				}
		}

	}
})
})(jQuery);
