var isMSIE = /*@cc_on!@*/false;
if (typeof document.documentElement.style.maxHeight != "undefined") {
	ie6 = false;
}else {
	ie6 = true;
}
Array.prototype.max = function () {
    var max = this[0];
    var len = this.length;
    for (var i = 1; i < len; i++) if (this[i] > max) max = this[i];
    return max;
}
Array.prototype.min = function () {
    var min = this[0];
    var len = this.length;
    for (var i = 1; i < len; i++) if (this[i] < min) min = this[i];
    return min;
}
var URLArray = location.href.split("#");
var URL = URLArray[0];
jQuery.fn.extend({
    imgTrim: function (trimW, trimH, fitSize, wrapTag) {
		//fitSize = true or false : default = false
		//wrapTag = parent tag string : default = "div"
		var fitSize = fitSize || false;
        var wrapTag = wrapTag || "div";
        this.each(function () {
			if(fitSize) {
				$(this).fitSizeImg(trimW,trimH);
			}
            var parentTagName = $(this).parent().get(0).tagName;
            if (parentTagName == "A") {
                var parentTag = $(this).parent("a");
            } else if (parentTagName == wrapTag.toUpperCase()) {
                var parentTag = $(this).parent(wrapTag);
            } else {
                $(this).wrap("<" + wrapTag + "></" + wrapTag + ">");
                var parentTag = $(this).parent(wrapTag);
            }
            parentTag.css({
                width: trimW,
                height: trimH,
                overflow: "hidden",
                display: "block",
                position: "relative",
                zoom: "1"
            });
            distW = Math.floor(($(this).attr("width") - trimW) / 2);
            distH = Math.floor(($(this).attr("height") - trimH) / 2);
            $(this).css({
                position: "absolute",
                top: "0",
                left: "0",
                marginTop: -distH,
                marginLeft: -distW
            });
        });
        return this;
    },
    idtBaseLine: function () {
        this.each(function () {
            var w = $(this).width();
            $(this).parent("*").css({
				textIndent: -w,
				marginLeft: w
			});
        });
        return this;
    },
	listIdtBaseLine: function() {
		var wList = [];
		this.each(function(){
			wList.push($(this).width());	   
		});
		var maxW = wList.max();
		this.each(function () {
			var gap = maxW - $(this).width();
			$(this).css("margin-right",gap);
			$(this).parent("*").css({
				textIndent: -maxW,
				marginLeft: maxW
			});
        });
        return this;
	},
    afterSep: function (tagStr, addClassStr) {
		var tagStr = tagStr || "div";
		var addClassStr = addClassStr || "last";
        this.each(function () {
            $(this).addClass(addClassStr).after("<" + tagStr + " class='separate'>&nbsp;</" + tagStr + ">").next().css({
                clear: "both",
                height: "1px",
                overflow: "hidden",
                display: "block",
                width: "auto",
                border: "none",
                background: "none",
				float: "none",
				lineHeight: "1px",
				margin: "0",
				padding: "0",
                fontSize: "1px"
            });
        });
        return this;
    },
    unbindHover: function () {
        this.each(function () {
            $(this).unbind("mouseenter").unbind("mouseleave")
        });
        return this;
    },
    flatheightsSet: function (countNum) {
        var num = countNum;
        var sets = [],
            temp = [];
        this.each(function (i) {
            temp.push(this);
            if (i % num == num - 1) {
                sets.push(temp);
                temp = [];
            }
        });
        if (temp.length) sets.push(temp);
        $.each(sets, function () {
            $(this).flatHeights();
        });
        return this;
    },
    flatheightsSetEach: function (selectorStr, countNum) {
        this.each(function () {
            $(this).find(selectorStr).flatheightsSet(countNum);
        });
        return this;
    },
    reSizeChildImg: function (maxW,addLink) {
		//maxW = number or null
		//addLink = true or false : default = true
		if(addLink == undefined) {
			addLink = true;
		}
		var maxW = maxW || $(this).width();
        this.each(function () {
            $(this).find("img").filter(function () {
                return $(this).attr("width") > maxW;
            }).removeAttr("height").attr("width", maxW).each(function () {
				if(addLink) {
                	$(this).wrap("<a href='" + $(this).attr('src') + "'></a>");
				}
            });
        });
        return this;
    },
	fitSizeImg: function (fitW,fitH,fitType) {
		//fitType = large or small : default = "large"
		var fitType = fitType || "large";
		this.each(function() {
			var baseW = $(this).attr("width");
			var baseH = $(this).attr("height");
			var baseRate = baseW / baseH;
			var fitRate = fitW / fitH;
			if(fitType == "large") {
				if(baseRate > fitRate) {
					$(this).attr("height",fitH).attr("width",fitH * baseRate);
				}else {
					$(this).attr("width",fitW).attr("height",fitW / baseRate);
				}
			}else if(fitType == "small") {
				if(baseRate > fitRate) {
					$(this).attr("width",fitW).attr("height",fitW / baseRate);
				}else {
					$(this).attr("height",fitH).attr("width",fitH * baseRate);
				}
			}
			
		});
	},
    activeNavi: function (dellink) {
        this.each(function () {
            var tagName = $(this).get(0).tagName;
            if (tagName == "IMG") {
                var pass = $(this).attr("src");
                pass = pass.replace(/(\.[a-z]{3})/g, '_o$1');
                $(this).unbindHover().attr("src", pass);
                if (dellink) {
                    $(this).parent("a").replaceWith($(this));
                }
            }
        });
		return this;
    },
    imgCenterParent: function (parentTag) {
		//parentTag = parentTag string
        var parentTag = parentTag || "*";
        this.each(function () {
            var $target = $(this);
            if (parentTag == "*") {
                var $parent = $target.parent("*");
            } else {
                var $parent = $target.parents(parentTag);
            }
            if ($target.attr("width") > $target.attr("height")) {
                $target.css({
                    marginTop: ($parent.height() - $(this).attr("height")) / 2
                });
            } else {
                $target.css({
                    marginLeft: ($parent.width() - $(this).attr("width")) / 2
                });
            }
        });
		return this;
    },
	inputTitleValue: function () {
		this.each(function () {
			$(this).find(":text[title]").each(function () {
				$(this).focus(function () {
					if ($(this).attr("title") == $(this).attr("value")) {
						$(this).attr("value", "");
					}
				}).blur(function () {
					if ($(this).attr("value") == "") {
						$(this).attr("value", $(this).attr("title"));
					}
				});
			}).end().submit(function () {
				$(this).find(":text[title]").each(function () {
					if ($(this).attr("title") == $(this).attr("value")) {
						$(this).attr("value", "");
					}
				});
			});
		});
		return this;
	}
});
