/**
 * linkManager 0.1
 * Copryright (c) BunnyStar, Inc.
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * Since:     2011-7-13
 * Modified:  2011-7-13
 *
 * yuga.js 0.7.1 - 優雅なWeb制作のためのJS
 * Copyright (c) 2009 Kyosuke Nakamura (kyosuke.jp)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * Since:     2006-10-30
 * Modified:  2009-01-27
 *
 * jQuery 1.3.1
 * fancybox 1.3.4
 * 
 */

(function(jQuery){

	jQuery.fn.linkManager  = function(options){

		var opts = jQuery.extend({}, jQuery.fn.linkManager.defaults, options);

		var parser = function(path){
			var self = this;
			this.originalPath = path;
			this.absolutePath = (function(){
				var e = document.createElement('span');
				e.innerHTML = '<a href="' + path + '" />';
				return e.firstChild.href;
			})();
			var fields = {'schema' : 2, 'username' : 5, 'password' : 6, 'host' : 7, 'path' : 9, 'query' : 10, 'fragment' : 11};
			var r = /^((\w+):)?(\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/.exec(this.absolutePath);
			for (var field in fields) {
				this[field] = r[fields[field]];
			}
			this.querys = {};
			if(this.query){
				$.each(self.query.split('&'), function(){
					var a = this.split('=');
					if (a.length == 2) self.querys[a[0]] = a[1];
				});
			}
		} // End of parser.

		return this.each(function(){
			var anchor = $(this);
			var href = new parser(this.getAttribute('href'));
			var url = new parser(location.href);
			var changeImg = false;
	
			//　現在のページにリンクした場合
			if ((href.absolutePath == location.href) && !href.fragment) {
				anchor.addClass(opts.selfClass);
				changeImg = opts.changeImgSelf;

			// 親ディレクトリにリンクした場合
			} else if (0 <= location.href.search(href.absolutePath)) {
				anchor.addClass(opts.parentsClass);
				changeImg = opts.changeImgParents;
			}
	
			// img要素が含まれている場合（ファイル名に-crを付加）
			if (changeImg){
				$(this).find('img').each(function(){
					this.originalSrc = $(this).attr('src');
					this.currentSrc = this.originalSrc.replace(new RegExp('(' + opts.postfix + ')?(\.gif|\.jpg|\.png)$'), opts.postfix + "$2");
					$(this).attr('src',this.currentSrc);
				});
			}

			if(opts.windowOpenImg) {
				// 外部サイトにリンクした場合
				anchor.filter('a[href^="http://"]').not('a[href^="' + parser.schema + '://' + parser.host + '/' + '"]').addClass(opts.externalClass).click(function(){
					if(opts.windowOpen) {
						window.open(this.href, '_blank');
						return false;
					}
				});

				// 画像に直接リンクした場合
				anchor.not('a[href^="http://"]').filter('a[href$=".jpg"], a[href$=".JPG"], a[href$=".gif"], a[href$=".GIF"], a[href$=".png"], a[href$=".PNG"], a[href$=".PDF"], a[href$=".p"]').fancybox(opts.fancyboxOpts);
			} else {
				// 外部サイトにリンクした場合
				anchor.filter('a[href^="http://"]').not('a[href^="' + parser.schema + '://' + parser.host + '/' + '"]').addClass(opts.externalClass).not('a[href$=".jpg"], a[href$=".gif"], a[href$=".png"]').click(function(){
					if(opts.windowOpen) {
						window.open(this.href, '_blank');
						return false;
					}
				});
				// 画像に直接リンクした場合
				anchor.filter('a[href$=".jpg"], a[href$=".JPG"], a[href$=".gif"], a[href$=".GIF"], a[href$=".png"], a[href$=".PNG"]').fancybox(opts.fancyboxOpts);
			}

			// ページ内にリンクした場合
			anchor.filter('a[href^=#], area[href^=#]').not('a[href=#], area[href=#]').addClass(opts.fragmentClass).click(function(){
				var href = $(this).attr('href');
				var target = $(href == '#' || href == "" ? 'html' : href);
				if ($(target).size() > 0) {
					var position = target.offset().top;
					$($.browser.safari ? 'body' : 'html').animate({scrollTop : position}, opts.scrollSpeed, 'swing');
					return false;
				}
			});

			// ファイルにリンクした場合
			anchor.filter('a[href$=".pdf"], a[href$=".PDF"]').click(function(){
				if(opts.windowOpen) {
					window.open(this.href, '_blank');
					return false;
				}
			});

		}); // End of return.

	} // End of jQuery.fn.anchor.

    jQuery.fn.linkManager.defaults = {
		selfClass        : 'current',
		parentsClass     : 'parent',
		fragmentClass    : 'fragment',
		externalClass    : 'external',
		postfix          : '-cr',
		changeImgSelf    : false,
		changeImgParents : false,
		windowOpen       : false,
		windowOpenImg    : false,
		fancyboxOpts     : {
			'overlayShow'	 : true,
			'overlayColor'   : '#000',
			'overlayOpacity' : 0.50,
			'transitionIn'	 : 'fade',
			'transitionOut'	 : 'fade',
			'speedIn'        : 500,
			'speedOut'       : 300
		},
		scrollSpeed      : 1000
    }; // End of jQuery.fn.anchor.defaults.

})(jQuery);
