//load
$('html').addClass('js');

//cookie ($days: -1:delete, null:none)
jQuery.cookieWrite = function($name, $value, $days) { $value.toString(); if ($days) { var $date = new Date(); $date.setTime($date.getTime()+($days*24*60*60*1000)); var $expires = '; expires='+$date.toGMTString(); } else { var $expires = ''; } document.cookie = $name + '=' + $value + $expires + '; path=/'; }	
jQuery.cookieRead = function($name) { var $array = document.cookie.split(';'); for (var $item in $array) { var $this = $.trim($array[$item]); if ($this.substring(0, $name.length + 1) == ($name + '=')) { return decodeURIComponent($this.substring($name.length + 1)); } } }		
jQuery.cookieArray = function($name) {	if ($.cookieRead($name)) { var $array = $.cookieRead($name).split(','); } else { $array = new Array; } return $array; }
jQuery.cookieUpdate = function($cookie, $add, $delete) { var $current = $.cookieArray($cookie); if ($delete) { $.arrayDelete($current, $delete); } if ($add) { $.arrayAdd($current, $add); } $.cookieWrite($cookie, $current); }

//data
jQuery.locationPath = function () { var $path = document.location.pathname.split('.'); $dir = $path[0].split('/'); return $dir.pop(); }
jQuery.dateFormat = function($date) {
	var $array = $date.split('-');
	var $month = new Array ('', 'January', 'Februray', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	return $array[2] + ' ' + $month[$array[1]] + ' ' + $array[0];     }

//array
jQuery.arrayForce = function($var) { if ($var instanceof Array == false) { return new Array($var); } else { return $var; } }
jQuery.arrayDelete = function($haystack, $needle) { $.each($.arrayForce($needle), function($key, $value) { $index = $.inArray($value, $haystack); if ($index != -1) { $haystack.splice($index, 1); } }); return $haystack; }
jQuery.arrayAdd = function($haystack, $needle) { $.each($.arrayForce($needle), function($key, $value) { $index = $.inArray($value, $haystack); if ($index == -1) { $haystack.push($value); } }); return $haystack; }
jQuery.idArray = function($selector) { var $array = new Array; $.each($selector, function($key, $value) { $array[$key] = $selector.eq($key).attr('id'); }); return $array; }

//style
jQuery.fn.classSwitch = function($class) { $(this).removeClass(); $(this).addClass($class); }
jQuery.fn.cssMargin = function() { return $(this).css('margin-top') + ' ' + $(this).css('margin-right') + ' ' + $(this).css('margin-bottom') + ' ' + $(this).css('margin-left'); }
jQuery.fn.cssPadding = function() { return $(this).css('padding-top') + ' ' + $(this).css('padding-right') + ' ' + $(this).css('padding-bottom') + ' ' + $(this).css('padding-left'); }
jQuery.fn.cssInt = function($attribute) { var $value = parseInt($(this).css($attribute)); if (isNaN($value)) { $value = 0; } return $value; }
jQuery.fn.cssOpacity = function($value) { $(this).css({opacity: $value}); }
jQuery.fn.cssHeight = function($value) { $(this).css({height: $value + 'px'}); }
jQuery.fn.cssWidth = function($value) { $(this).css({width: $value + 'px'}); }
jQuery.fn.centerY = function ($parent) { 
	var $childheight = $(this).heightPlus('BOX') / 2; 
	if ($parent == 'body') { var $parentheight = document.documentElement.clientHeight / 2; } 
	else { var $parentheight = $parent.heightPlus('BOX') / 2; } 
	$(this).css({'margin-top': $parentheight - $childheight + 'px'}); }

//animate
jQuery.fn.showZindex = function($value) { if (!$value) { var $value = 1; } $(this).css({zIndex: $value}); $(this).show(); }
jQuery.fn.hideZindex = function() { $(this).css({zIndex: 0}); $(this).hide(); }
jQuery.fn.showOpacity = function($speed, $callback) { $(this).animate({opacity: 'show'}, $speed, $callback); }
jQuery.fn.hideOpacity = function($speed, $callback) { $(this).animate({opacity: 'hide'}, $speed, $callback); }
jQuery.fn.showWidth = function($speed, $callback) { $(this).animate({paddingLeft: 'show', paddingRight: 'show', width: 'show'}, $speed, $callback); }
jQuery.fn.hideWidth = function($speed, $callback) { $(this).animate({paddingLeft: 'hide', paddingRight: 'hide', width: 'hide'}, $speed, $callback); }

//height & width
jQuery.fn.pageHeight = function ($offset) { var $height = document.documentElement.clientHeight - $offset; $(this).css({'height': $height  + 'px'}); }
jQuery.fn.matchHeight = function() { var $height = 0; $(this).each(function() { if ($(this).height() > $height) { $height = $(this).height(); } }); $(this).css({'height': $height + 'px'}); }
jQuery.fn.matchWidth = function() { var $width = 0; $(this).each(function() { if ($(this).width() > $width) { $width = $(this).width(); } }); $(this).css({'width': $width + 'px'}); }
jQuery.fn.matchSize = function() { $(this).matchHeight(); $(this).matchWidth(); }

jQuery.fn.heightPlus = function($attribute) { 
	if ($(this).css('display') == 'none') { var $thishidden = true; $(this).show(); }
	var $parenthidden = $(this).parents().filter(function() { return $(this).css('display') == 'none'; });
	$parenthidden.show();
	var $height = 0;
	if ($attribute == "BOX") { $attribute = 'bhp'; }
	else if ($attribute == "INNER") { $attribute = 'hp'; }
	else if ($attribute == "OUTER") { $attribute = 'bhmpy'; }
	else { $attribute = 'h'; }
	var $array = $attribute.split('');
	for ($item in $array) {
		if ($array[$item] == 'b') { $height += ($(this).cssInt('border-bottom-width') + $(this).cssInt('border-top-width')); }
		else if ($array[$item] == 'h') { $height += $(this).height(); }
		else if ($array[$item] == 'm') { $height += ($(this).cssInt('margin-bottom') + $(this).cssInt('margin-top')); }
		else if ($array[$item] == 'p') { $height += ($(this).cssInt('padding-bottom') + $(this).cssInt('padding-top')); }
		else if ($array[$item] == 'y') { if ($(this).cssInt('bottom') > 0) { $height += $(this).cssInt('bottom'); } else if ($(this).cssInt('top') > 0) { $height += $(this).cssInt('top'); } }     }
	if ($parenthidden) { $parenthidden.hide(); }
	if ($thishidden) { $(this).hide(); }
	return $height;     } 
jQuery.fn.widthPlus = function($attribute) { 
	if ($(this).css('display') == 'none') { var $thishidden = true; $(this).show(); }
	var $parenthidden = $(this).parents().filter(function() { return $(this).css('display') == 'none'; });
	$parenthidden.show();
	var $width = 0;
	if ($attribute == "BOX") { $attribute = 'bpw'; }
	else if ($attribute == "INNER") { $attribute = 'pw'; }
	else if ($attribute == "OUTER") { $attribute = 'bmpwx'; }
	else { $attribute = 'w'; }
	var $array = $attribute.split('');
	for (var $item in $array) {
		if ($array[$item] == 'b') { $width += ($(this).cssInt('border-left-width') + $(this).cssInt('border-right-width')); }
		else if ($array[$item] == 'm') { $width += ($(this).cssInt('margin-left') + $(this).cssInt('margin-right')); }
		else if ($array[$item] == 'p') { $width += ($(this).cssInt('padding-left') + $(this).cssInt('padding-right')); }
		else if ($array[$item] == 'w') { $width += $(this).width(); }
		else if ($array[$item] == 'x') { if ($(this).cssInt('left') > 0) { $width += $(this).cssInt('left'); } else if ($(this).cssInt('right') > 0) { $width += $(this).cssInt('right'); } }    }
	if ($parenthidden) { $parenthidden.hide(); }
	if ($thishidden) { $(this).hide(); }
	return $width;     }

//img
jQuery.fn.imgLoad = function() { $(this).find('img').each(function() { if ($(this)[0].complete == false) { $(this).load(); } }); }
jQuery.fn.imgSquare = function() {
	$(this).each(function() {
		var $padding = parseInt($(this).css('padding-top')); var $width = $(this).attr('width'); var $height = $(this).attr('height');
		if ($width > $height) { $buffer = (($width - $height) / 2) + $padding; $(this).css({'padding': Math.floor($buffer) + 'px ' + $padding + 'px ' + Math.ceil($buffer) + 'px ' + $padding + 'px'}); }
		else if ( $width < $height) { $buffer = (($height - $width) / 2) + $padding; $(this).css({'padding': $padding + 'px ' + Math.floor($buffer) + 'px ' + $padding + 'px ' + Math.ceil($buffer) + 'px'}); }     })     }
jQuery.fn.imgMatch = function() {
	var $height = 0; $(this).each(function() { if ($(this).attr('height') > $height) { $height = $(this).attr('height'); } });
	var $width = 0; $(this).each(function() { if ($(this).attr('width') > $width) { $width = $(this).attr('width'); } });
	$(this).each(function() {
		var $padding = parseInt($(this).css('padding-top'));
		var $paddingY = (($height - $(this).attr('height')) / 2) + $padding;
		var $paddingX = (($width - $(this).attr('width')) / 2) + $padding;
		$(this).css({'padding': Math.floor($paddingY) + 'px ' + Math.floor($paddingX) + 'px ' + Math.ceil($paddingY) + 'px ' + Math.ceil($paddingX) + 'px '});     })     }
jQuery.fn.imgResize = function($maxwidth, $maxheight) {
	$(this).each(function() {
		var $thiswidth = $(this).attr('width'); $ratiowidth = $maxwidth / $thiswidth;
		var $thisheight = $(this).attr('height'); $ratioheight = $maxheight / $thisheight;
		if ($ratiowidth < $ratioheight) { var $width = $maxwidth; var $height = Math.floor($ratiowidth * $thisheight); }
		else { var $height = $maxheight; var $width = Math.floor($ratioheight * $thiswidth); }
		$(this).css({'height': $height + 'px', 'width': $width + 'px'});     })     }	

//div
jQuery.fn.divBox = function($type, $dir, $ext) { 
	function background($name) { return 'background-image: url(' + $dir + $name + '.' + $ext + ');'; }
	$(this).find('div.box').empty().remove();
	if ($type == "corner") { 
		$(this).prepend('<div class="box" style="height: ' + $(this).heightPlus('OUTER') + 'px;">' +
			'<div class="box0" style="' + background('0') + ' height: ' + ($(this).heightPlus('OUTER') - 60) + 'px"></div>' +
			'<div class="box1" style="' + background('1') + ' height: ' + ($(this).heightPlus('OUTER') - 60) + 'px"></div>' +
			'<div class="box00" style="' + background('00') + '"></div>' +
			'<div class="box01" style="' + background('01') + '"></div>' +
			'<div class="box10" style="' + background('10') + '"></div>' +
			'<div class="box11" style="' + background('11') + '"></div></div>'); }
	else if ($type == "side") { $(this).prepend('<div class="box" style="height: ' + $(this).heightPlus('OUTER') + 'px"><div class="boxX0"></div><div class="boxX1"></div><div class="boxY0"></div><div class="boxY1"></div></div>'); }     }
jQuery.fn.divClick = function($parent) { var $child = $(this); $parent.css({'cursor': 'pointer'}); $parent.bind('click', function() { window.location = $child.attr('href'); }); }
