$(document).ready(function() { $(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, div.layout-3 div.img-gallery').each(function() { $(this).imggallery('left', 50); });
	
	$('div.img-tick').each(function() { $(this).imgtick(2000); });
	$('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'}); }	
	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'); } });     }



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(); });	}
