// ----------------------------------------------------------------------------
// DOM READY
// ----------------------------------------------------------------------------

$(document).ready(function() {

	// BIND EVENTS FOR CLICKABLE ITEMS
	$('.clickable').bind('click', function() {
		var element = $(this).find('a[href]:not(.notclickable):last');
		var url = element.attr('href');
		var target = element.attr('target');
		element.bind('click', function(event) {
			event.preventDefault();
		});
		if(url) {
			if($('base[href]').length > 0) { // IE BUG
				url = url.replace($('base[href]').attr('href'), '');
				if(target !== '_blank') {
					url = $('base[href]').attr('href') + url;
				}
			}
			if($.data(document, 'notclickable') == false) {
				if(target == '_blank') {
					window.open(url);
				} else {
					window.location.href = url;
				}
			}
		}
	});

	// SET VARIABLE FOR NOT CLICKABLE ITEMS
	$('.clickable .notclickable').bind('mouseenter', function() {
		$.data(document, 'notclickable', true);
	}).bind('mouseleave', function() {
		$.data(document, 'notclickable', false);
	});
	$.data(document, 'notclickable', false);

	// BIND EVENTS FOR HOVERABLE ITEMS
	$('.hoverable').bind('mouseenter', function() {
		$(this).addClass('hover');
	}).bind('mouseleave', function() {
		$(this).removeClass('hover');
	});

	// BIND EVENTS FOR FORMS
	$('.formline, .formarea, .formselect').bind('focus blur', function() {
		$(this).toggleClass('focus');
	});
	$('.formbut').bind('mouseenter mouseleave', function() {
		$(this).toggleClass('hover');
	});

	// SET BACKGROUND AND PADDING
	$(window).bind('load scroll resize', function() {
		$('#tpl_body_bg img').each(function() {
			$(this).css({
				width: 'auto',
				height: 'auto',
				display: 'block',
				marginTop: '0px',
				marginLeft: '0px'
			});
			$(this).height($(this).parent().height());
			if($(this).width() < $(this).parent().width()) {
				$(this).css({
					height: 'auto'
				});
				$(this).width($(this).parent().width());
			}
			$(this).css({
				marginTop: (0 - (($(this).height() - $(this).parent().height()) / 2)) + 'px',
				marginLeft: (0 - (($(this).width() - $(this).parent().width()) / 2)) + 'px'
			});
		});
		$('#tpl_body_bg').css({
			left: (0 - $(document).scrollLeft()) + 'px'
		});
		$('#tpl_body_wrapper').each(function() {
			var height = $(window).height();
			var minHeight = $('#tpl_body').css('minHeight');
			minHeight = parseFloat(minHeight.replace('px', ''));
			var padding = (height - minHeight) / 2;
			padding = Math.floor(padding);
			if(padding < 0) {
				padding = 0;	
			}
			$(this).css({
				paddingTop: padding + 'px'
			});
		});
		if($(document).scrollTop() > 100) {
			$('.tpl_top:last').show();
		} else {
			$('.tpl_top:last').hide();
		}
	}).trigger('resize');

	// RESIZE IMAGE ON LOAD
	$('#tpl_body_bg img').bind('load', function() {
		$(window).trigger('resize');
	});

	// HOVER EFFECT SOCIAL MEDIA ICONS
	if(!$.browser.msie || $.browser.msie && $.browser.version >= 9) {
		$('#tpl_social a').bind('mouseenter', function() {
			$(this).find('img').stop(true, true).fadeTo(300, 1);
		}).bind('mouseleave', function() {
			$(this).find('img').stop(true, true).fadeTo(300, 0.6);
		}).trigger('mouseleave');
	}

	// GOTO TOP
	$('.tpl_top').wrapInner('<a href="#"></a>').find('a').bind('click', function(event) {
		event.preventDefault();
		var duration = Math.round(($(document).scrollTop()*2));
		if(duration > 2000) {
			duration = 2000;
		}
		$('html, body').animate({scrollTop: 0}, duration, 'easeInOutExpo');
	});


	// ------------------------------------------------------------------------
	// LOAD EXTERNAL SCRIPTS / PLUGINS
	// ------------------------------------------------------------------------

	$.getScript(jquery_coreurl+'/include/jquery/easing.latest.js', function() {
	$.getScript(jquery_coreurl+'/include/jquery/cycle.latest.all.js', function() {
	$.getScript(jquery_coreurl+'/include/jquery/nivoslider.latest.js', function() {
	$.getScript(jquery_coreurl+'/include/jquery/tweet.latest.js', function() {

		//$('#tpl_slider').before('<div id="tpl_slider_pager"></div>');
		$('#tpl_slider').before('<div id="tpl_slider_prev"><img src="' + jquery_siteurl + '/image/web/cycle/prev.png" alt="" /></div>');
		$('#tpl_slider').before('<div id="tpl_slider_next"><img src="' + jquery_siteurl + '/image/web/cycle/next.png" alt="" /></div>');
		$('#tpl_slider ul').cycle({
			fx: 'scrollHorz',
			sync: true,
			easing: 'easeInOutExpo',
			timeout: 5000,
			speed: 750,
			pause: true,
			pauseOnPagerHover: true,
			prev: '#tpl_slider_prev',
			next: '#tpl_slider_next',
			pager: '#tpl_slider_pager'
		});
		$('#tpl_slider, #tpl_slider_prev, #tpl_slider_next').bind('mouseenter', function() {
			$('#tpl_slider_prev, #tpl_slider_next').show();
		}).bind('mouseleave', function() {
			$('#tpl_slider_prev, #tpl_slider_next').hide();
		});

		$('#tpl_animation').nivoSlider({
			effect: 'boxRain',
			boxCols: 4,
			boxRows: 4,
			animSpeed: 500,
			pauseTime: 5000,
			directionNav: false
		});

		$('#tpl_tweet').tweet({
			count: 1,
			lang_about: '{text}'
		});

	});
	});
	});
	});


	// ------------------------------------------------------------------------
	// ONLOAD / LOAD COMPLETE
	// ------------------------------------------------------------------------

	//$(window).load(function() {

		// PAGE LOADED

	//});

});
