// page init
$(function(){
	initCufon();
	initHomeImage();
	initPicTooltip();
	initPicPreview();
});

// cufon init
function initCufon() {
	Cufon.replace('.my-page p', { fontFamily: 'Gloucester'});
	Cufon.replace('.mainbox-title span', { fontFamily: 'Gloucester'});
	//Cufon.replace('#top_menu ul.top-menu li.first-level.cm-active a', { fontFamily: 'Gloucester', hover: true });
	Cufon.replace('a.topNavItem', { fontFamily: 'Gloucester', hover: true });
	Cufon.replace('div.main-holder h2', { fontFamily: 'Gloucester' });
	Cufon.replace('div#footer h4', { fontFamily: 'Gloucester' });
}

// tabs function
function initHomeImage() {
	var _imageCount = 5;
	var _randomIndex = Math.floor(Math.random()*_imageCount)+1;
	var _classPrefix = 'bg';
	var _holder = $('#home-image');
	_holder.addClass(_classPrefix+_randomIndex);
}


// picture tooltip
function initPicTooltip() {
	var _animSpeed = 3;
	var _activeClass = 'active';
	var _tooltipSelects = $('select.tooltip-select');

	_tooltipSelects.each(function(){
		var _select = $(this);

		var _tooltip = $('#'+_select.attr('title')).hide();

		// tooltip options
		function refreshOptions(_obj) {
			for(var i=0; i<_obj.options.length; i++) {
				if(_obj.options[i] && _obj.options[i].title) {
					if(i == _obj.selectedIndex) $('#'+_obj.options[i].title).show();
					else $('#'+_obj.options[i].title).hide();
				}
			}
			if(!_obj.options[_obj.selectedIndex].title) _tooltip.hide()
			else if(_obj.focused) _tooltip.fadeIn(_animSpeed)
		}

		// tooltip control
		_select.focus(function(){
			if(this.options[this.selectedIndex].title) _tooltip.fadeIn(_animSpeed);

			this.focused = true;
		})
		_select.blur(function(){
			_tooltip.fadeOut(300);
			this.focused = false;
		})
		_select.change(function(){
			refreshOptions(this);
		});
		refreshOptions(this);
	});
}

// picture preview
function initPicPreview() {
	var _animSpeed = 300;
	$('select.resize-select').each(function(){
		var _select = $(this);
		var _box = $('#'+_select.attr('title'));
		function refreshSize() {
			var _size = (_select.get(0).options[_select.get(0).selectedIndex].title).split(';');
			var _width = parseInt(_size[0]);
			var _height = parseInt(_size[1]);

			_box.animate({width:_width,height:_height},{duration:_animSpeed,queue:false});
		}
		_select.change(refreshSize);
		refreshSize();
	});
}

