$(window).load(function(){
	$('div.combo-plain h2 a, div.combo-tick h2 a, div.combo-slide h2 a').each(function() { $(this).tagclick($(this).parents('div:eq(0)')); });
	
	$('div.layout-1 div.img-gallery').each(function() { $(this).imggallery('bottom', 50); });
	$('div.layout-2 div.img-gallery').each(function() { $(this).imggallery('left', 50); });
	$('div.layout-3 div.img-gallery, div.layout-4 div.img-gallery').each(function() { $(this).imggallery('left', 55); });
	
	$('div.img-tick').each(function() { $(this).imgtick(2000); });
	$('div.img-book').each(function() { $(this).imgbook(); });
	$('div.combo-slide').each(function() { $(this).divslide(); });
	$('div.combo-tick').each(function() { $(this).divtick(); });

	var _box = $('div.text-box, div.combo-plain, div.combo-slide, div.combo-tick, div.img-tick, div.img-gallery');
	_box.css({'background': 'transparent'});
	_box.parents('div.layout-1, div.layout-2, div.layout-3, div.layout-4').each(function() { $(this).divbox('corner'); });     });

//imgtick

jQuery.fn.imgtick = 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').imgsquare();
	_item.matchheight();
	_parent.css({'height': _item.eq(0).heightplus('box')});
	_item.hide();
	_item.eq(0).show();
	if (_item.length > 1) { setTimeout(loop, _time); }
	//loop
	function loop() {
		var _current = _parent.children('div.item:eq(0)');
		var _next = _parent.children('div.item:eq(1)');
		_current.fadeOut('slow');
		_next.fadeIn('slow', function() { _current.appendTo(_parent); setTimeout(loop, _time); });     }      }
		
//imgthumb		

jQuery.fn.imggallery = function (_position, _size) {
	$(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');
	_item.matchheight();
	var _first = _item.eq(0);
	var _img = _item.children('img');
	_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.imgsquare();
	var _height = _first.heightplus('box');
	var _right = _first.widthplus('box');
	var _width = _parent.widthplus('box') - _right;
	_item.hide();
	_first.show();
	_thumb.eq(0).classswitch('here');
//position
	if (_position == 'left') { _parent.css({'height': _height + 'px', 'paddingRight': _right + 'px', 'width': _width + 'px'}); _ul.css({'width': _width + 'px'}); }	
	else if (_position == 'bottom') { _parent.css({'paddingTop': _height + 'px'}); }
//click
	_thumb.bind('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();
			$(this).classswitch('here'); } });     }

//imgbook
	
jQuery.fn.imgbook = 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>');
	_parent.append('<div class="item"><h2>The End - Click here to start again</div>');
	if (_parent.children('div.item').length.isodd) { _parent.append('<div class="item"></div>'); }
	var _item = _parent.children('div.item');
	_item.wrapInner('<div class="js"></div>');
	var _length = _item.length;
	var _itemleft = _parent.children('div.item:nth-child(odd)');
	var _itemright = _parent.children('div.item:nth-child(even)');
	_item.find('img').imgmatch();
	_item.matchsize();
	
	_item.find('h2').centerY(_item);
	_itemleft.css({'right': '50%', 'margin-right': '1px'});
	_itemright.css({'left': '50%', 'margin-left': '1px'});
	var _height = _item.eq(0).heightplus('box');
	var _width = _item.eq(0).width();
	_parent.css({'height': _height + 'px'});
	_item.cssZindex(0);
	_item.csswidth(0);
	_item.eq(0).csswidth(_width);
	_item.eq(1).csswidth(_width);
//click
	_itemright.bind('click', function() {
		var _currentright = _parent.children('div.item').index(this);
		var _currentleft = _currentright - 1;
		if (_currentright >= _length - 1) { var _nextleft = 0; } else { var _nextleft = _currentright + 1; }
		var _nextright = _nextleft + 1;
		_item.cssZindex(0);
		_item.children('div.js').cssopacity(1);
		_item.eq(_currentright).cssZindex(2);	 			 
		_item.eq(_currentleft).cssZindex(2);
		_item.eq(_nextleft).cssZindex(3);
		_item.eq(_nextright).cssZindex(1);
		_item.eq(_nextright).csswidth(_width);
		_item.eq(_nextleft).children('div.js').cssopacity(0);
		_item.eq(_currentright).animate({width: 0}, 400);
		_item.eq(_nextleft).delay(200).animate({marginRight: '-40px', width: '40px'}, 100) .animate({marginRight: '1px', width: _width}, 300);
		_item.eq(_currentright).children('div.js').animate({opacity: 0}, 400);
		_item.eq(_nextleft).children('div.js').delay(200).animate({opacity: 1}, 400, function() { _item.cssZindex(0); _item.eq(_nextleft).cssZindex(2);_item.eq(_nextright).cssZindex(1); _item.eq(_currentleft).csswidth(0); background(_nextleft); });     }); 
//click
	_itemleft.bind('click', function() {
		var _currentleft = _parent.children('div.item').index(this);
		var _currentright = _currentleft + 1;
		if (_currentleft != 0) {
			var _nextright = _currentleft - 1;
			var _nextleft = _nextright - 1;
			_item.cssZindex(0);
			_item.children('div.js').cssopacity(1);
			_item.eq(_currentleft).cssZindex(2);	 			 
			_item.eq(_currentright).cssZindex(2);
			_item.eq(_nextright).cssZindex(3);
			_item.eq(_nextleft).cssZindex(1);
			_item.eq(_nextleft).csswidth(_width);
			_item.eq(_nextright).children('div.js').cssopacity(0);
			_item.eq(_currentleft).animate({width: 0}, 400);
			_item.eq(_nextright).delay(200).animate({marginLeft: '-40px', width: '40px'}, 100) .animate({marginLeft: '1px', width: _width}, 300);
			_item.eq(_currentleft).children('div.js').animate({opacity: 0}, 400);
			_item.eq(_nextright).children('div.js').delay(200).animate({opacity: 1}, 400, function() { _item.cssZindex(0);_item.eq(_nextright).cssZindex(2);_item.eq(_nextleft).cssZindex(1); _item.eq(_currentright).csswidth(0); background(_nextleft); });     }}); 
//background
	_parent.prepend('<div class="background"></div>');
	_background = _parent.children('div.background');
	_background.csswidth((_width * 2) + 22);
	_padding = Math.round((_parent.width() - _background.width()) / 2) - 10;
	_background.css({'right': _padding + 'px'});
	function background(_index) { _background.css({'right': Math.round(_index * (20 / _length)) + _padding + 'px'}); }     }

jQuery.fn.divslide = function () {
	var _offset = parseInt($(this).children('div.item').eq(0).position().top);
	$(this).children('div.item').wrapAll('<div class="js"></div>');
	$(this).append('<div class="control"><span class="next">Next</span><span class="prev">Prev</span><ul class="menu"></ul></div>');
	var _parent = $(this).children('div.js');
	var _control = $(this).children('div.control');
	var _prev = _control.children('span.prev');
	var _next = _control.children('span.next');
	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'});
		_control.children('ul.menu').append('<li><span class="no">' + (_key + 1) + '</span></li>'); });
	var _li = _control.children('ul.menu').children('li');
	_parent.css({'height': _height + 'px'});
	var _index = 0;
	_li.eq(0).addClass('here');
	_prev.hide();
//click
	_control.find('span').bind('click', function() {
		if ($(this).attr('class') == 'prev') { _index --; }
		else if ($(this).attr('class') == 'next') { _index ++; }
		else { _index = parseInt($(this).text()) - 1; }
		_item.each(function(_key, _value) { $(this).animate({'left': ((_width * _key) - (_index * _width)) + 'px'}, 'fast'); });
		if (_index == 0) { _prev.hide(); } else { _prev.show(); }
		if (_index + 1 == _length) { _next.hide(); } else { _next.show(); }
		_li.removeClass();
		_li.eq(_index).addClass('here'); });     }	



jQuery.fn.divtick = function () {
	$(this).children('div.item').wrapAll('<div class="js"></div>');
	var _parent = $(this).children('div.js');
	var _item = _parent.children('div.item');
	_item.children('img').imgsquare();
	_item.matchheight();
	var _height = _item.eq(0).heightplus('box');
	_parent.css({'height': _height + 'px'});
	_item.css({'top': _height + 'px'});	
//loop
	function loop() {
		var _current = _parent.children('div.item:first');
		if (parseInt(_current.css('top')) <= 0) {
			var _time = (_height + parseInt(_current.css('top'))) / 0.125;
			_current.animate({'top': -_height}, _time, function() { _current.appendTo(_parent); _current.css({'top': _height + 'px'}); loop(); }); }
		else { 
			var _time = parseInt(_current.css('top')) / 0.125;
			_current.animate({'top': 0}, _time, function() { _current.animatepause(2500, function() { loop(); })    }); }     }
	loop();
	_parent.mouseenter(function() { _item.stop(); });
	_parent.mouseleave(function() { loop(); });	}
