/**
 * utility functions
 * @author Hayato Tomoda
 * @website http://tomodian.com
 */

$().ready(function() {
	megaDropdown();
	replaceKeyvisual();
	clickableBoxes();
	runGallery();
	runFacebox();
	biggerLink();
	showToolTips();
	setCurrentLocation();
});

/**
 * replaceKeyvisual()
 * wrap #keyvisual image and add watermark.
 */
function replaceKeyvisual() {
	$img = $('#keyvisual');
	$img.wrap('<div id="keyvisual-wrapper"></div>');
	$img.after('<div id="watermark">Tokyo Stainless Grinding</div>');
}

/**
 * runGallery()
 * runs gallery script when .ad-gallery exists.
 */
function runGallery() {
	var gallery = $('.ad-gallery');
	if(gallery) {
		var options = {
			loader_image: "/js/external/jquery.ad-gallery/loader.gif"
		};
		gallery.adGallery(options);
	}
}

/**
 * runFacebox()
 */
function runFacebox() {
	$('a[rel*=facebox]').facebox({});	
}

/**
 * addOpaqueOverlay()
 * overlays shadow to each localnavs.
 */
function addOpaqueOverlay() {
}

/**
 * biggerLink()
 * make block element clickable.
 */
function biggerLink() {
	var element = $('.biggerlink');
	if(element) {
		element.find('div').biggerlink();
		element.css('cursor', 'pointer');
	}
}

/**
 * megaDropdown()
 * make drop down list.
 */
function megaDropdown() {
	$('.nav .mega').hover(
		function() {
			$(this).find('ul').addClass('hover').show();
		},
		function() {
			$(this).find('ul').fadeOut().removeClass('hover');
		}
	);
}

/**
 * setCurrentLocation()
 * add .current into header nav.
 */
function setCurrentLocation() {
	var path = window.location.pathname;
	if(path.length > 1) {
		var url = path.split("/");
		var current = "/" + url[1] + "/";
		$('#header > .right > .nav > li > a').each(function(i) {
			if($(this).attr('href') == current) {
				$(this).addClass('current');
			}
		});
	}
}

/**
 * clickableBoxes()
 */
function clickableBoxes() {
	var box = $('.box');
	box.biggerlink();
	box.hover(
		function(){
			$(this).find('a').addClass('hover');
		},
		function(){
			$(this).find('a').removeClass('hover');
		}
	);
}

/**
 * addLastClass()
 */
function addLastClass() {
  var targets = $(".box");
  targets.each(function(i) {
    if((i + 1) % 3 == 0) {
      $(this).addClass('last');
    }
  });
}

/**
 * tipsy() - show tooltips
 */
function showToolTips() {
  $('.tipsy').tipsy({
    gravity: 's'
  });
}


