$(document).ready(function(){

	var _ua = (function(){
	return {
	ltIE6:typeof window.addEventListener == "undefined" && typeof document.documentElement.style.maxHeight == "undefined",
	ltIE7:typeof window.addEventListener == "undefined" && typeof document.querySelectorAll == "undefined",
	ltIE8:typeof window.addEventListener == "undefined" && typeof document.getElementsByClassName == "undefined",
	ie:document.uniqueID,
	firefox:window.globalStorage,
	opera:window.opera,
	webkit:!document.uniqueID && !window.opera && !window.globalStorage && window.localStorage,
	mobile:/android|iphone|ipad|ipod/i.test(navigator.userAgent.toLowerCase())
	}
	})();	

	if( _ua.webkit || _ua.firefox || _ua.opera){
		$("#main").preloader();
		$("#topics").preloader();
		$("#postitem").preloader();
		$("#sidecontents").preloader();
	}

	$('#pickupslider').aviaSlider({
		autorotationSpeed: 6,
		showText: false,
	});

	$("#mytweet").innerfade({
		animationtype: 'slide',
		speed: 'slow',
		timeout: 9000,
		type: 'sequence',
		containerheight: '240px'
	});	

	$('#primary-navigation li.m_works').each(function(){
		var distance = 12;
		var time = 250;
		var hideDelay = 100;
		var hideDelayTimer = null;
		var beingShown = false;
		var shown = false;
		var trigger = $('a', this);
		var info = $('#sub-primary-navigation').css('opacity', 0);
		$([trigger.get(0), info.get(0)]).mouseover(function(){
			if(!jQuery.support.opacity){
				$('#sub-primary-navigation').css('opacity', 1);
			}
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			if (beingShown || shown) {
				// don't trigger the animation again
				return;
			} else {
				// reset position of info box
				beingShown = true;
				info.css({
					top: -30,
					display: 'block'
				}).animate({
					top: '-=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					beingShown = false;
					shown = true;
				});
				$('#primary-navigation li.m_works a:first').addClass("sfHover");
			}
			return false;
		}).mouseout(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				info.animate({
					top: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					shown = false;
					info.css('display', 'none');
				});
				$('#primary-navigation li.m_works a:first').removeClass("sfHover");
			}, hideDelay);
			return false;
		});
	});

	$('#primary-navigation li.m_profile').each(function(){
		var distance = 12;
		var time = 250;
		var hideDelay = 100;
		var hideDelayTimer = null;
		var beingShown = false;
		var shown = false;
		var trigger = $('a', this);
		var info = $('#prof-primary-navigation').css('opacity', 0);
		$([trigger.get(0), info.get(0)]).mouseover(function(){
			if(!jQuery.support.opacity){
				$('#prof-primary-navigation').css('opacity', 1);
			}
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			if (beingShown || shown) {
				// don't trigger the animation again
				return;
			} else {
				// reset position of info box
				beingShown = true;
				info.css({
					top: -30,
					display: 'block'
				}).animate({
					top: '-=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					beingShown = false;
					shown = true;
				});
				$('#primary-navigation li.m_profile a:first').addClass("sfHover");
			}
			return false;
		}).mouseout(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				info.animate({
					top: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					shown = false;
					info.css('display', 'none');
				});
				$('#primary-navigation li.m_profile a:first').removeClass("sfHover");
			}, hideDelay);
			return false;
		});
	});

	$('#primary-navigation li.m_goods').each(function(){
		var distance = 12;
		var time = 250;
		var hideDelay = 100;
		var hideDelayTimer = null;
		var beingShown = false;
		var shown = false;
		var trigger = $('a', this);
		var info = $('#goods-primary-navigation').css('opacity', 0);
		$([trigger.get(0), info.get(0)]).mouseover(function(){
			if(!jQuery.support.opacity){
				$('#goods-primary-navigation').css('opacity', 1);
			}
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			if (beingShown || shown) {
				// don't trigger the animation again
				return;
			} else {
				// reset position of info box
				beingShown = true;
				info.css({
					top: -30,
					display: 'block'
				}).animate({
					top: '-=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					beingShown = false;
					shown = true;
				});
				$('#primary-navigation li.m_goods a:first').addClass("sfHover");
			}
			return false;
		}).mouseout(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				info.animate({
					top: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					shown = false;
					info.css('display', 'none');
				});
				$('#primary-navigation li.m_goods a:first').removeClass("sfHover");
			}, hideDelay);
			return false;
		});
	});

	$('.page_navi li.previous a').hover(
		function() {
			$(this).stop().animate({"opacity":0});
		},
		function() {
			$(this).stop().animate({"opacity":1});
		}
	);

	$('.page_navi li.next a').hover(
		function() {
			$(this).stop().animate({"opacity":0});
		},
		function() {
			$(this).stop().animate({"opacity":1});
		}
	);

	$('.prevnext a').hover(
		function() {
			$(this).stop().animate({"opacity":0});
		},
		function() {
			$(this).stop().animate({"opacity":1});
		}
	);

	$('#footer-info li.onoff a').hover(
		function() {
			$(this).stop().animate({"opacity":0});
		},
		function() {
			$(this).stop().animate({"opacity":1});
		}
	);


	$('.grid-inner').hover(
		function() {
			//$(this).children("h3:first").stop(false,true).animate({top: 0},{duration: 100, easing: "easeOutQuart"});
			$(this).children().children(".post-header").fadeIn("normal");//.stop() / .css("display","block");
			$(this).parent().stop().animate({backgroundColor:'#F06'},500);
		},
		function() {
			//$(this).children("h3:first").stop(false,true).animate({top: -100},{duration: 100, easing: "easeOutQuart"});
			$(this).children().children(".post-header").fadeOut("normal");
			$(this).parent().stop().animate({backgroundColor:'#FFF'},500);
		}
	);

	$('a[href^=#]').click(function() {
		var speed = 1000;
		var href= $(this).attr("href");
		var target = $(href == "#" || href == "" ? 'html' : href);
		var position = target.offset().top;
		$($.browser.safari ? 'body' : 'html').animate({scrollTop:position}, speed, 'swing');
		return false;
	});

	$('a').filter(function(){
		return (new RegExp('http:\/\/(?!'+location.hostname +')')).test(this.href);
	}).attr('target','_blank').addClass("external");
/*
	$('#main').isotope({
		// options
		itemSelector : '.grid',
		masonry : {
			columnWidth : 160
		}
	});

	$('.box-sub-grid').isotope({
		// options
		itemSelector : '.grid',
		masonry : {
			columnWidth : 160
		}
	});


	$('#main').masonry({
		// options
		itemSelector : '.grid',
		columnWidth : 160
	});

	$('.box-sub-grid').masonry({
		// options
		itemSelector : '.grid',
		columnWidth : 160
	});

*/
	$("#main").vgrid({
		easeing: "easeOutBounce",
		useLoadImageEvent: true,
		time: 400,
		delay: 20,
		fadeIn: {
			time: 500,
			delay: 50
		}
	});

	$(".box-sub-grid").vgrid({
		easeing: "easeOutBounce",
		useLoadImageEvent: true,
		time: 400,
		delay: 20,
		fadeIn: {
			time: 500,
			delay: 50
		}
	});

});

