$(document).ready(function() { 
	$('div.objectbook').each(function() { $(this).objectLoad(); $(this).objectBook(); $(this).objectBox('corner', 'boxmid'); });
	$('div.objectbox').each(function() { $(this).objectLoad(); $(this).objectClick(); $(this).objectBox('corner', 'boxmid'); });
	$('div.objectchannel').each(function() { $(this).objectLoad(); $(this).objectChannel(); $(this).objectClick(); $(this).objectBox('corner', 'boxmid'); });
	$('div.objectdefault').each(function() { $(this).objectLoad(); $(this).objectClick(); $(this).objectBox('corner', 'boxmid'); });
	$('div.objectgallery').each(function() { $(this).objectLoad(); $(this).objectGallery(55); $(this).objectBox('corner', 'boxmid'); });
	$('div.objectslideshow').each(function() { $(this).objectLoad(); $(this).objectSlideshow(2000); $(this).objectBox('corner', 'boxmid'); });
	$('div.objecttab').each(function() { $(this).objectLoad(); $(this).objectTab(); $(this).objectBox('corner', 'boxmid'); });
	$('div.objectticker').each(function() { $(this).objectLoad(); $(this).objectTicker(0.125); $(this).objectClick(); $(this).objectBox('corner', 'boxmid'); });
});	

jQuery.fn.objectLoad = function() { $(this).imgLoad(); $(this).parents('div.col').eq(0).fadeIn('fast'); }
jQuery.fn.objectClick = function() { $(this).find('h2 a').each(function() { $(this).divClick($(this).parents('div.item').eq(0)); }); }
jQuery.fn.objectBox = function($type, $style) { $(this).css({'background': 'transparent'}); $(this).parents('div.col').eq(0).divBox($type, 'img/' + $style, 'png'); }

//book	
jQuery.fn.objectBook = function () {
	$(this).children('div.item').wrapAll('<div class="js"></div>');
	var $parent = $(this).children('div.js');
	$parent.prepend('<div class="item"><h2>Click the right-hand page to turn</h2></div>');
	if ($parent.children('div.item').length % 2) { $parent.append('<div class="item"><h1>The End</h1><h2>Click here to start again</h2></div>'); }
	var $item = $parent.children('div.item');
	$item.eq(0).prepend($parent.siblings('h1'));
	$item.wrapInner('<div class="js"></div>');
	var $length = $item.length;
	var $width = ($parent.widthPlus('BOX')) / 2 - 30;
	var $inner = $item.children('div.js');
	$inner.children('img').imgResize($width - 10, $width - 10);
	$item.css({'width': $width + 'px'});
	$item.matchSize();
	$inner.css({'width': $width + 'px'});
	$inner.matchHeight();
	$inner.each(function($key, $value) { $(this).append('<span class="min">' + $key + '</span>'); });
	var $itemleft = $parent.children('div.item:nth-child(odd)');
	var $itemright = $parent.children('div.item:nth-child(even)');
	$itemleft.css({'right': '50%'}); $itemleft.children('div.js').css({'float': 'right'});
	$itemright.css({'left': '50%'}); $itemright.children('div.js').css({'float': 'left'});
	var $height = $item.eq(0).heightPlus('BOX');
	$parent.css({'height': $height + 'px'});
	$item.hideZindex();
	$inner.hide();
	$item.eq(0).showZindex(2);
	$item.eq(0).children('div.js').show();
	$item.eq(1).showZindex(2);
	$item.eq(1).children('div.js').show();
//action
	function action($pageclose, $pagehide, $pageopen, $pageshow) {
		$item.hide();
		$pageclose.showZindex(2);
		$pageshow.showZindex(1);
		$pageshow.children('div.js').show();
		$pageopen.css({zIndex: 2});
		$pagehide.showZindex(1);
		$pageclose.hideWidth(300);
		$pageclose.children('div.js').hideOpacity(300);
		$pageopen.delay(300).showWidth(300);
		$pageopen.children('div.js').delay(300).showOpacity(300, function() { $pageclose.hideZindex(); $pageclose.children('div.js').hide(); $pagehide.hideZindex(); $pagehide.children('div.js').hide(); });     }
//click
	$itemright.click(function() {
		var $this = $item.index(this);
		var $pageclose = $item.eq($this);
		var $pagehide = $item.eq($this - 1);
		if ($this >= $length - 1) { var $next = 0; } else { var $next = $this + 1; }
		var $pageopen = $item.eq($next);
		var $pageshow = $item.eq($next + 1);
		action($pageclose, $pagehide, $pageopen, $pageshow);     }); 
//click
	$itemleft.click(function() {
		var $this = $item.index(this);
		var $pageclose = $item.eq($this);
		var $pagehide = $item.eq($this + 1);
		var $pageopen = $item.eq($this - 1);
		var $pageshow = $item.eq($this - 2);
		if ($this != 0) { action($pageclose, $pagehide, $pageopen, $pageshow); }      });     }

//channel
jQuery.fn.objectChannel = function () {
	$(this).children('div.item').wrapAll('<div class="js"></div>');
	var $parent = $(this).children('div.js');
	$parent.append('<ul class="menu menuleaf"><li class="other"><span class="button buttonnext">Next</span></li><li class="other"><span class="button buttonnone"></span></li><li class="other"><span class="button buttonprev">Prev</span></li></ul>');
	var $prev = $parent.find('span.buttonprev');
	var $none = $parent.find('span.buttonnone');
	var $next = $parent.find('span.buttonnext');
	var $ul = $parent.children('ul.menu');
	var $item = $parent.children('div.item');
	$item.matchHeight();
	var $length = $item.length;
	var $height = $item.eq(0).heightPlus('BOX');
	var $width = $item.eq(0).widthPlus('BOX');
	$item.each(function($key, $value) { 
		$(this).css({'left': ($width * $key) + 'px'});
		$ul.append('<li><span class="button buttonnum">' + ($key + 1) + '</span></li>'); });
	var $num = $ul.children('li').children('span.buttonnum');
	$num.eq(0).addClass('here');
	$ul.css({'margin-top': $height + 'px'});
	var $index = 0;
	$prev.click(function() { $index --; action(); });
	$next.click(function() { $index ++; action(); });
	$prev.hide();
	$num.click(function() { $index = parseInt($(this).text()) - 1; action(); });
	function action() {
		$item.each(function($key, $value) { $(this).animate({'left': (($width * $key) - ($index * $width)) + 'px'}, 'fast'); });
		if ($index == 0 || $index + 1 == $length) { $none.show(); } else { $none.hide(); }
		if ($index == 0) { $prev.hide(); } else { $prev.show(); }
		if ($index + 1 == $length) { $next.hide(); } else { $next.show(); }
		$num.removeClass('here');
		$num.eq($index).addClass('here');     }     }

//gallery
jQuery.fn.objectGallery = function($size) {
	if ($(this).parents('div.col1').length) { var $position = 'bottom'; $size -= 5; } 
	else if ($(this).parents('div.col2').length) { var $position = 'bottom'; }
	else { var $position = 'left'; }
	$(this).children('div.item').wrapAll('<div class="js"></div>');
	var $parent = $(this).children('div.js');
	$parent.append('<ul class="menu"></ul>');
	var $ul = $parent.children('ul.menu');
	var $item = $parent.children('div.item');
	var $first = $item.eq(0);
	var $img = $item.children('img.default');
	$img.each(function($key, $value) { $ul.append('<li></li>'); $(this).clone().appendTo($ul.children('li').eq($key)); });
	var $thumb = $ul.find('img');
	$thumb.imgResize($size, $size);
	$img.imgMatch();
	$item.matchHeight();
	var $height = $first.heightPlus('BOX');
	var $right = $first.widthPlus('BOX');
	var $width = $parent.widthPlus('BOX') - $right;
	$item.hide();
	$first.show();
	$thumb.eq(0).addClass('here');
//position
	if ($position == 'left') { $parent.css({'height': $height + 'px', 'padding-right': $right + 'px', 'width': $width + 'px'}); $ul.css({'width': _width + 'px'}); }	
	else if ($position == 'bottom') { $parent.css({'padding-top': $height + 'px'}); }
//click
	$thumb.click(function() {
		var $index = $ul.find('img').index(this);
		var $item = $parent.children('div.item');
		if ($item.eq($index).is(':hidden')) {
			$item.fadeOut('fast');
			$item.eq($index).fadeIn('fast');
			$thumb.removeClass('here');
			$(this).addClass('here'); } });     }
	
//slideshow
jQuery.fn.objectSlideshow = function($time) {
	$(this).children('div.item').wrapAll('<div class="js"></div>');
	var $parent = $(this).children('div.js');
	var $item = $parent.children('div.item');
	$item.children('img.default').imgMatch();
	$item.matchHeight();
	$parent.css({'height': $item.eq(0).heightPlus('BOX')});
	$item.hideZindex();
	$item.eq(0).showZindex(1);
	if ($item.length > 1) { setTimeout(action, $time); }
	//loop
	function action() {
		var $current = $parent.children('div.item:eq(0)');
		var $next = $parent.children('div.item:eq(1)');
		$current.fadeOut('slow');
		$next.fadeIn('slow', function() { $next.showZindex(1); $current.hideZindex(); $current.appendTo($parent); setTimeout(action, $time); });     }      }

//tab	
jQuery.fn.objectTab = function () {
	if ($(this).parents('div.win')) { $(this).parents('div.win').show(); }
	$(this).children('div.item').wrapAll('<dl class="menu menudefault"></dl>');
	$(this).find('div.item').each(function($key, $value) { 
		var $h1 = $(this).find('h1:eq(0)');
		if ($h1.length > 0) { var $title = $h1.html(); $h1.remove(); }
		else { var $title = 'Page ' + ($key + 1); }
		$(this).after('<dt>' + $title + '</dt><dd><div class="item">' + $(this).html() + '</div></dd>');
		$(this).empty().remove(); });
	var $dl = $(this).children('dl');
	var $dt = $dl.children('dt');
	var $dd = $dl.children('dd');
	$dt.eq(0).addClass('here');
	$dd.matchHeight();
	$dl.height($dd.eq(0).heightPlus('OUTER'));
	$dd.hide();
	$dd.eq(0).show();
	$(this).show();
	if ($(this).parents('div.win')) { $(this).parents('div.win').hide(); }
	$dt.click(function() {
		if ($(this).attr('class') != 'here') { 
			$(this).siblings('dd').fadeOut('fast');
			$(this).siblings('dt').removeClass('here');
			$(this).next('dd').fadeIn('fast');
			$(this).addClass('here'); }     });     }
			
//ticker
jQuery.fn.objectTicker = function($time) {
	$(this).children('div.item').wrapAll('<div class="js"></div>');
	var $parent = $(this).children('div.js');
	var $item = $parent.children('div.item');
	$item.matchSize();
	var $height = $item.eq(0).heightPlus('BOX');
	$parent.css({'height': $height + 'px'});
	$item.css({'top': $height + 'px'});	
	$item.mouseover(function() { $item.stop(true, false); });
	$item.mouseout(function() { action(); });
	action();
	function action() {
		var $current = $parent.children('div.item').eq(0);
		if (parseInt($current.css('top')) <= 0) { var $speed = ($height + parseInt($current.css('top'))) / $time; $current.delay(2000).animate({'top': -$height}, $speed, function() { $current.appendTo($parent); $current.css({'top': $height + 'px'}); action(); }); }
		else { var $speed = parseInt($current.css('top')) / $time; $current.animate({'top': 0}, $speed, function() { action(); }); }     }     }
