/*
 * 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 : "syntaxhighlighter";
			//插件设置
			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:"c,c++",
					has:false
				},
				CSharp:{
					alias:"c#,c-sharp",
					has:false
				},
				Css:{
					alias:"css",
					has:false
				},
				Delphi:{
					alias:"pascal",
					has:false
				},
				Java:{
					has:false
				},
				JScript: {
					alias:"js,javascript",
					has:false
				},
				Php:{
					alias:"php",
					has:false
				},
				Python:{
					alias:"py",
					has:false
				},
				Ruby:{
					alias:"rails,ror",
					has:false
				},
				Sql:{
					alias:"sql",
					has:false
				},
				Vb:{
					alias:"vb,net",
					has:false
				},
				Xml:{
					alias:"html,xhtml,xslt",
					has:false
				},
				Bash:{
					alias:"bash,shell",
					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(';',''));
				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 + "shThemeRDark.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.clipboardSwf = swfpath + 'clipboard.swf';
								SyntaxHighlighter.all();
						}
				}
		}

	}
})

})(jQuery);
