/*
 * jQuery throttle / debounce - v1.1 - 3/7/2010
 * http://benalman.com/projects/jquery-throttle-debounce-plugin/
 * 
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function(b,c){var $=b.jQuery||b.Cowboy||(b.Cowboy={}),a;$.throttle=a=function(e,f,j,i){var h,d=0;if(typeof f!=="boolean"){i=j;j=f;f=c}function g(){var o=this,m=+new Date()-d,n=arguments;function l(){d=+new Date();j.apply(o,n)}function k(){h=c}if(i&&!h){l()}h&&clearTimeout(h);if(i===c&&m>e){l()}else{if(f!==true){h=setTimeout(i?k:l,i===c?e-m:e)}}}if($.guid){g.guid=j.guid=j.guid||$.guid++}return g};$.debounce=function(d,e,f){return f===c?a(d,e,false):a(d,f,e!==false)}})(this);


jQuery(document).ready(function($) {
	
	/* old dropdown code.
	// dropdown in tha nav
	$("#main-nav > li").hover(function() {
		$(this).find("ul").css({'display': 'block'});
		var howmanychildren = $(this).find("ul").children().length;
		var height = howmanychildren * ($(this).find("li").height());
		var time = howmanychildren * 30;
		$(this).find("ul").animate({'height': height + 'px'}, time);
	}, function() {
		var howmanychildren = $(this).find("ul").children().length + 1;
		var time = howmanychildren * 30;
		$(this).find("ul").animate({'height': '0'}, time);
		var thisthis = this;
		setTimeout(function(){ $(thisthis).find("ul").css({'display': 'none'}); }, time + 10);
	});
	*/
	
	$("nav ul").superfish({
		delay:       600,                            // one second delay on mouseout 
		animation:   {height:'show'},  // fade-in and slide-down animation 
		speed:       230,                          // faster animation speed 
		autoArrows:  false,                           // disable generation of arrow mark-up 
		dropShadows: false
	});
	
	// make the nav full-width
	var main_menu_links = $("nav .menu > li").length;
	var linkwidth = 0;

	$("nav .menu > li").each(function(){
		linkwidth = linkwidth + $(this).find('a').width();
	});
	var default_padding = (956 - linkwidth) / (main_menu_links - 1);
	$("nav .menu > li").css({'margin-left': default_padding / 2, 'margin-right': default_padding / 2});
	$("nav .menu > li").first().css({'margin-left': '0'});
	$("nav .menu > li").last().css({'margin-right': '0'});
	
	$(".menu > .current-menu-item, .menu > .current-menu-parent").addClass('selectedLava');
	// lavalamp effect
	$('nav').lavaLamp({ target:'.menu > li', container:'div', returnDelay: 600, autoResize:true });
	$('nav').css({'visibility':'visible'});
	
	// the slider hover
	$('#slider').hover(function(){
		$('.nivo-nextNav, .nivo-prevNav').animate({'bottom': '17px'});
		$('.nivo-caption').animate({'bottom': '0'});
	}, function(){
		$('.nivo-nextNav, .nivo-prevNav').animate({'bottom': '-38px'});
		$('.nivo-caption').animate({'bottom': '-55px'});
	});
	
	// the work preview hover
	$(".work-preview").hover(function() {
		$(this).find("img").animate({'opacity': '0.6'}, 180);
		$(this).parents(".portfolio-post").find(".new-work").animate({'opacity': '0'}, 180);
	}, function(){
		$(this).find("img").animate({'opacity': '1'}, 180);
		$(this).parents(".portfolio-post").find(".new-work").css({'display': 'block'});
		$(this).parents(".portfolio-post").find(".new-work").animate({'opacity': '1'}, 180);
	});
	
	// the wordpress default gallery
	$(".gallery img").hover(function() {
		$(this).animate({'opacity': '0.6'}, 180);
	}, function(){
		$(this).animate({'opacity': '1'}, 180);
	});
	

	$(".scroll").click(function(event){
		//prevent the default action for the click event
		event.preventDefault();

		//get the full url - like mysitecom/index.htm#home
		var full_url = this.href;

		//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
		var parts = full_url.split("#");
		var trgt = parts[1];

		//get the top offset of the target anchor
		var target_offset = $('#work-post-' + trgt).offset();
		var target_top = target_offset.top - 25;

		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 500);
	});
	
	$("#footer-widgets img").hover(function() {
		$(this).animate({'opacity': '1'}, 130);
	}, function() {
		$(this).animate({'opacity': '0.8'}, 130);
	});
	
	function tothetop_scroll() {
  	// what the y position of the scroll is
  	var ybrowse = $("html, body").scrollTop();

		// for chrome fixing
		if(ybrowse == false) {
			var ybrowse = $("body").scrollTop();
		}
		
		if(ybrowse > 50) {
			$("#tothetop").css({'display': 'block'});
			$("#tothetop").animate({'opacity': '1'}, 400);
		} else if(ybrowse <= 50) {
			$("#tothetop").animate({'opacity': '0'}, 400);
			setTimeout(function() { $("#tothetop").css({'display': 'none'}); }, 410)
		}
	};
	
	$(window).scroll($.throttle(450, tothetop_scroll));
	
	
	
	// tothetop
	$("#tothetop").click(function(){
		$("html, body").animate({ scrollTop: 0 }, 600);
	});
	
	// initialize fancybox
	if($().fancybox) {
		$(".gallery a").attr('rel', 'fancybox');
		
		$(".gallery a").fancybox({
				'transitionIn'	:	'elastic',
				'transitionOut'	:	'elastic',
				'easingIn'      : 'easeOutBack', 
				'easingOut'     : 'easeInBack',
				'speedIn'				:	500, 
				'speedOut'			:	200,
				'cyclic'	 			: true
		});
	}
	
	if($().slimbox) {
		$(".gallery a").attr('rel', 'lightbox-slim');
	}
	
	// blog images
	$('.blog a').hover(function(){
		$(this).find('img').animate({'opacity':'0.7'}, 180);
	}, function(){
		$(this).find('img').animate({'opacity':'1'}, 180);
	});
	
});
