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

//cookie ($days: -1:delete, null:none)
jQuery.cookieWrite = function($name, $value, $days) { $value.toString(); if ($days == -1) { var $expires = '; expires=Thu, 01-Jan-1970 00:00:01 GMT'; } else 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($add, $remove) { $(this).removeClass($remove); $(this).addClass($add); }
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'}); }
jQuery.fn.centerX = function ($parent) { 
	var $childwidth = $(this).widthPlus('BOX') / 2; 
	if ($parent == 'body') { var $parentwidth = document.documentElement.clientWidth / 2; } 
	else { var $parentwidth = $parent.widthPlus('BOX') / 2; } 
	$(this).css({'margin-left': $parentwidth - $childwidth + '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).heightPlus() > $height) { $height = $(this).heightPlus(); } }); $(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 = 'bhmp'; }
	else if ($attribute == 'POS') { $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 = 'bmpw'; }
	else if ($attribute == 'POS') { $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).complete == false) { $(this).load(); } }); }

jQuery.fn.imgMax = function($src, $class) { 
	$(this).each(function() {
		$(this).prepend('<img alt="Loading" class="blob blobload" src="img/blobmax.png" height="430" />');
		$(this).prepend('<img class="' + $class + '" src="' + $src + '" />');
		var $img = $(this).children('img.' + $class + ':eq(0)');
		$img.hide();
		$img.load(function() { $(this).next('img.blobload').hide(); $(this).show(); $(this).imgSquare(); });     });     }
jQuery.fn.imgSquare = function() {
	$(this).each(function() {
		var $padding = parseInt($(this).css('padding-top')); var $width = $(this).widthPlus(); var $height = $(this).heightPlus();
		if ($width > $height) { var $buffer = (($width - $height) / 2) + $padding; $(this).css({'padding': Math.floor($buffer) + 'px ' + $padding + 'px ' + Math.ceil($buffer) + 'px ' + $padding + 'px'}); }
		else if ( $width < $height) { var $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).heightPlus() > $height) { $height = $(this).heightPlus(); } });
	var $width = 0; $(this).each(function() { if ($(this).widthPlus() > $width) { $width = $(this).widthPlus(); } });
	$(this).each(function() {
		var $padding = parseInt($(this).css('padding-top'));
		var $paddingY = (($height - $(this).heightPlus()) / 2) + $padding;
		var $paddingX = (($width - $(this).widthPlus()) / 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).widthPlus(); var $ratiowidth = $maxwidth / $thiswidth;
		var $thisheight = $(this).heightPlus(); var $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'});
		$width = $(this).widthPlus('BOX');
		$(this).next('p.label').css({'width': $width + 'px'});     })     }	

//div
jQuery.fn.divBox = function($style, $ext) { 
	function background($name) { return 'background-image: url(asset/' + $style + $name + '.' + $ext + ');'; }
	$(this).children('div.box').empty().remove();
	$(this).prepend('<div class="box ' + $style + '" style="height: ' + $(this).heightPlus('OUTER') + 'px;">' +
		'<div class="box0" style="' + background('0') + ' height: ' + ($(this).heightPlus('OUTER') - 60) + 'px; top: 30px;"></div>' +
		'<div class="box1" style="' + background('1') + ' height: ' + ($(this).heightPlus('OUTER') - 60) + 'px; top: 30px;"></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>');    }
		
jQuery.fn.divClick = function($parent) { var $child = $(this); $parent.css({'cursor': 'pointer'}); $parent.bind('click', function() { window.location = $child.attr('href'); }); }

jQuery.fn.headBox = function($style, $ext) { 
	function background($name) { return 'background-image: url(asset/' + $style + 'head' + $name + '.' + $ext + ');'; }
	$(this).find('div.box').empty().remove();
	var $height = $(this).heightPlus('INNER'); 
	$(this).wrapInner('<span class="wrap"></span>');
	$(this).prepend('<div class="box ' + $style + '" style="height: ' + $height + 'px;">' +
		'<div class="box0" style="' + background('0') + ' height: ' + $height + 'px"></div>' +
		'<div class="box1" style="' + background('1') + ' height: ' + $height + 'px"></div>');    }

//flash
jQuery.flashWrite = function($file, $width, $height) { document.write('<div class="cell cellflash"><object type="application/x-shockwave-flash" data="' + $file + '" width="' + $width + '" height="' + $height + '"><param name="movie" value="' + $file + '" /><param name="wmode" value="opaque" /></object></div>'); }
