/*
 * ENABLE JS-ONLY STYLES
 */
$('html').addClass('js-enabled');
if(typeof(swfobject) !== 'undefined' && swfobject.hasFlashPlayerVersion("9.0.0")) {
	$('html').addClass('flash-enabled');
}

$(function() {
  // Miscellaneous onload functionality
	$('.nav-header').hover(function() {
	  $(this).addClass('nav-selected');
	}, function() {
	  $(this).removeClass('nav-selected');
	});
});


/*
 * ONLOAD EVENTS
 */
/*$(document).ready(function() {});

	$(function () {
	$('.primary-nav-item').append('<a class="tabover"><span class="tabspan">&nbsp;</span></a>').each(function () {
	  var $span = $('a.tabover', this).hide();
 $(this).hover(function () {
  		$('a.tabover', this).fadeIn(100);
 		 $('span.tabspan', this).fadeIn(100);
	  }, function () {
   	$('a.tabover', this).fadeOut(100);
	   	$('span.tabspan', this).fadeOut(100);
	  });
	});
});*/

/*
 * UTILITY FUNCTIONS
 * 
 * Little functions that make it all happen.
 */
jQuery.fn.center_in_container = function (container) {
	this.each(function () {
		container_width = $(container).width();
		container_height = $(container).height();
		$(this).css("left", 0.5 * (container_width - $(this).width()) + "px");
		$(this).css("top", 0.5 * (container_height - $(this).height()) + "px");
	});
	return this;
}

jQuery.fn.horiz_center_in_container = function (container) {
	this.each(function () {
		container_width = $(container).width();
		$(this).css("left", 0.5 * (container_width - $(this).width()) + "px");
	});
	return this;
}

/*
 * Slideshow setup
 * 
 * This function sets up the slideshow functionality on all pages.
 * For the artist landing page it sets up all slideshows normally,
 * but then specifically sets up the artists slideshow, which has
 * a callback function attached to it.
 */
$(function () {
//  if($("body").attr("id")!=="productlistingall") {
//		$(".slideshow").slideshow();
//	}
	
$(".slideshow").slideshow();
  
  if($("body#artistlanding").length > 0) {
    $("#artists-hero").slideshow(function (i, j) {
      $("#artists-hero .artists-text .artists-lede")
      .eq(i)
      .fadeOut('fast', function () {
        $("#artists-hero .artists-text .artists-lede")
        .eq(j)
        .fadeIn('fast');
      });
    });
    $("div.artists-lede").hide();
    $("div.artists-lede").eq(0).show();
    
    $(".video").center_in_container("#artists-hero");
		
		$(".watch-video a").click(function () {
			$("#artists-hero").block({ 
				message: $("#" + $(this).attr("id").replace(/^watch-/, '')),
				css: {
					top: 0,
					left: 0
				},
				overlayCSS: {
					opacity: 0.75
				},
				centerY: false,
				centerX: false
			});
			return false;
		});
		
		$(".close-overlay").click(function () {
			$(this).parent('.video').fadeOut(100, function () {
				$("#artists-hero").unblock();
			});
			return false;
		});
  }
  
		$("#product-slideshow").slideshow(function (i, j) {
  		$("#product-slideshow .product-slideshow-text .product-lede")
  		.eq(i)
  		.fadeOut('fast', function () {
    		$("#product-slideshow .product-slideshow-text .product-lede")
    		.eq(j)
    		.fadeIn('fast');
  		});
		});
		$("div.product-lede").hide();
		$("div.product-lede").eq(0).show();
  
});

$(function () {
	$("#buyers-guide .watch-video").click(function () {
		$.blockUI({
			message: $("#" + $(this).attr("rel").replace(/^watch-/, '')).parents("div.video"),
			overlayCSS: {
				opacity: 0.75
			},
			centerY: true,
			centerX: true
		});
		
		return false;
	});
	
	$("#buyers-guide .close-overlay").click(function () {
		$.unblockUI();
		return false;
	});
});	

$(function () {
  /*
   * Region selector animated entrance/exit
   */
    $("#region-selector-link").click(function () {
      $("#region-selector").fadeIn(300);
    });
    $("div.region-selector-close a, div.region-americas h2").click(function () {
      $("#region-selector").fadeOut(300);
    });

/*  if($.browser.msie && parseInt($.browser.version) < 8) {
    $("#region-selector-link").click(function () {
      $("#region-selector").fadeIn(300);
    });
    $("div.region-selector-close a, div.region-americas h2").click(function () {
      $("#region-selector").fadeOut(300);
    });
  }
  else {
    $("#region-selector")
    .css("width", "0px")
    .css("height", "0px");
    $("#region-selector *").css("display", "none");

    $("#region-selector-link").click(function () {
      $("#region-selector")
      .animate({
        height: "156px",
        display: "block"
      }, 200, function () {
        $("#region-selector")
        .animate({
          width: "955px"
        }, 200, function () {
          $("#region-selector *").fadeIn(100);
        });
      });
    });
    
    $("div.region-selector-close a, div.region-americas h2").click(function () {
      $("#region-selector")
      .fadeOut(300, function () {
        $("#region-selector *").css("display", "none");
        $("#region-selector")
        .css("width", "0px")
        .css("height", "0px");
      });
    });
    
  }*/
});

/*
 * EXPANDER
 */
$(function() {
	//hide divs with expander class
  $('div.expander > div').hide();
  $('div.expander > .expander-header').addClass('expanding_');
	//toggle first tab on and switch header class.
  $('div.expander > .expander-header').click(function() {
    $(this).next().slideToggle('fast');
    //If an expanding class is on, turn it off, if it's off turn it on.
    $(this).toggleClass('expanding_');
    $(this).toggleClass('expanded_');
  });

  //activate keyboard tabbing and keyboard 'enter/return' navigation on expander
  $('.expander-header').attr('tabindex',0).keyup(function(evt){
  	if (evt.keyCode == 13) $(this).click(); //if key = enter/return fire click, see code above
  });
});

$(function() {
	$('.product-list dt a, dd.qualities dt a, .capabilities dl dt a, .disabled_').click(function(){
		return false;
	});
});

/*
 * Browser-specific hacks
 */
$(function () {
	if($.browser.msie && parseInt($.browser.version) < 8) {
		// MSIE browsers less than version 7 do not support the :hover pseudoclass for any
		// elements other than links.
		$("#wirelessdetail dl.component").hover(
			function () {
				$(this).addClass("component-hover");
			},
			function () {
				$(this).removeClass("component-hover");
			}
		);
		
		// Set up the nav items, since IE6 chokes on our CSS
		$("li.nav-header")
		.hover(
			function () {
				// In
				$(this).children(".nav-content").show();
			},
			function () {
				// Out
				$(this).children(".nav-content").hide();
			}
		);
		
    // Set up hoverboxen on the buyer's guide
    $("li.guide-result").hover(function () {
      $(this).addClass('selected-result');
    }, function () {
      $(this).removeClass('selected-result');
    });

  	/*
     * Fix for news.html navigational items.
     */

    $("li.filter-header").hover(function () {
      $(this).children("ol").show();
    },
    function () {
      $(this).children("ol").hide();
    });
    
    
    // A sundry assortment of hover states that IE refuses to support.
//    $("#breadcrumb li").hover(function () { $(this).addClass("breadcrumb-hover"); }, function () { $(this).removeClass("breadcrumb-hover"); });
//    $("#breadcrumb li.current_").hover(function () { $(this).addClass("current_-hover"); }, function () { $(this).removeClass("current_-hover"); });
    $("li.guide-result").hover(function () { $(this).addClass("guide-result-hover"); }, function () { $(this).removeClass("guide-result-hover"); });
    $("ul.equipment-profile li dl dd.budget-option").hover(function () { $(this).addClass("budget-option-hover"); }, function () { $(this).removeClass("budget-option-hover"); });
    $(".page-content .tabs li.tab").hover(function () { $(this).addClass("tab-hover"); }, function () { $(this).removeClass("tab-hover"); });
    $(".artist-slide .panel-link").hover(function () { $(this).addClass("panel-link-hover"); }, function () { $(this).removeClass("panel-link-hover"); });
    $(".promo-viewer .panel-link").hover(function () { $(this).addClass("panel-link-hover"); }, function () { $(this).removeClass("panel-link-hover"); });
		$(".service-content .panel-link").hover(function () { $(this).addClass("panel-link-hover"); }, function () { $(this).removeClass("panel-link-hover"); });
    $("li.filter-header").hover(function () { $(this).addClass("filter-header-hover"); }, function () { $(this).removeClass("filter-header-hover"); });
    $(".filter-tabs ol.filter-list li").hover(function () { $(this).addClass("li-hover"); }, function () { $(this).removeClass("li-hover"); });
	}
	
	// Set up DT/DL fix
	$("dl.available-colors dt").each(function () {
		$(this).text($.trim($(this).text()) + ":");
	});
	$("dl.available-colors dd:not(:last)").each(function () {
		$(this).text($.trim($(this).text()) + ",");
	});
});

/*
 * Tooltip support 
 */
function qtipSetup() {
  $("dt.mic-cardioid, dt.mic-adjustable, dt.mic-tailored, dt.mic-flat, .mic-type dt, dt.mic-halfcardioid, dt.mic-hypercardioid, dt.mic-lobar, dt.mic-supercardioid, dt.mic-omnidirectional, dt.mic-bidirectional")
  .qtip({
    text: false,
    content: false,
    position: {
      corner: {
        target: 'topMiddle',
        tooltip: 'bottomMiddle'
      }
    },
    adjust: {
      mouse: false
    },
    style: {
      background: '#333',
      tip: {
        corner: 'bottomMiddle',
        style: {
          right: -30
        }
      },
      name: 'dark'
    },
    api: {
      beforeRender: function () {
        this.options.content.text      = "<p><strong>" + $(this.elements.target).text() + "</strong></p>\n"
                                       + "<p>" + $(this.elements.target).next("dd").html() + "</p>\n";
        // this.elements.tip.css("right", "20px");
      }
    }
  });
  
  $('#productlistingall a.family-tip[title]')
  .qtip({
    style: {
      background: '#333', 
	  tip: true,
      name: 'dark',
      width: 200
    },
    position: {
    	target: 'mouse',
    	corner: {
    		tooltip: 'bottomMiddle'
    	},
    	adjust: {
    		screen: true
    	}
    },
    show: {
    	delay: 0
    }
  });  
}
  
$(function () {  
  qtipSetup();
  $(".language-selector li.language-tip a")
  .qtip({
    text: false,
    content: false,
    position: {
      corner: {
        target: 'bottomRight',
        tooltip: 'topLeft'
      }
    },
    adjust: {
      mouse: false
    },
    style: {
      background: '#333',
      tip: {
        corner: 'topLeft',
        style: {
          right: -30
        }
      },
      name: 'dark'
    },
    api: {
      beforeRender: function () {
        this.options.content.text      = "<p>" + $(this.elements.target).next("p.tip_").html() + "</p>\n";
        // this.elements.tip.css("right", "20px");
      }
    }
  });
});

$(function () {
  if($(document.body).attr('id') == 'artist-detail') {
    $.fn.fadeToggle = function (q) {
      $(this).each(function () {
        if($(this).css('display') == 'none') {
          $(this).fadeIn(q);
        }
        else {
          $(this).fadeOut(q);
        }
      });
    }
  }
});

//SCROLLING
;(function($) {
// Animated Scrolling for Same-Page Links
// @see http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links
 
$.fn.smoothScroll = function(options) {
  var opts = $.extend({}, $.fn.smoothScroll.defaults, options),
      locationPath = filterPath(location.pathname),
      scrollElem = scrollableElement('html', 'body');
      
  this.each(function() {
    var link = this,
        $link = $(this),
        hostMatch = ((location.hostname === link.hostname) || !link.hostname),
        pathMatch = (filterPath(link.pathname) || locationPath) === locationPath,
        thisHash = link.hash && link.hash.replace('#',''),
        scrollTargetExists = thisHash && !!$('#' + thisHash).length;
 
    if (hostMatch && pathMatch && scrollTargetExists) {
      var include = true,
 
          exclude = opts.exclude,
          elCounter = 0,
          el = exclude.length,
 
          excludeWithin = opts.excludeWithin,
          ewlCounter = 0,
          ewl = excludeWithin.length;
       
      while (include && elCounter < el) {
        if ($link.is(exclude[elCounter++])) {
          include = false;
        }
      }
      while (include && ewlCounter < ewl) {
        if ($link.parents(excludeWithin[ewlCounter++] + ':first').length) {
          include = false;
        }
      }
 
      if (include) {
        $link.data('scrollTarget', '#' + thisHash);
      }
    }
 
  });
 
  
  this.die('click.smoothscroll').live('click.smoothscroll', function(event) {
    var scrollTargetId = $(this).data('scrollTarget');
    if (scrollTargetId) {
      event.preventDefault();
       
      var scrollTargetOffset = $(scrollTargetId).offset().top;
 
      $(scrollElem).animate({scrollTop: scrollTargetOffset + opts.offset}, 400, function() {
        location.hash = scrollTargetId;
      });
    }
  });
  return this;
  
  // private functions
  
  // don't pass window or document
  function scrollableElement(els) {
    for (var i = 0, argLength = arguments.length; i < argLength; i++) {
      var el = arguments[i],
          $scrollElement = $(el);
      if ($scrollElement.scrollTop() > 0) {
        return el;
      } else {
        $scrollElement.scrollTop(1);
        var isScrollable = $scrollElement.scrollTop() > 0;
        $scrollElement.scrollTop(0);
        if (isScrollable) {
          return el;
        }
      }
    }
    return [];
  }
  
  function filterPath(string) {
    return string
      .replace(/^\//,'')
      .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
      .replace(/\/$/,'');
  }
  
  function debug($obj) {
    if (window.console && window.console.log) {
      window.console.log($obj);
    }
  }
};
 
// default options
$.fn.smoothScroll.defaults = {
  exclude: [],
  excludeWithin:[],
  offset: 0
};
 
})(jQuery);

// FCC ALERT TEST BEHAVIORS

jQuery.fn.fadeToggle = function(speed, callback) {
	return this.animate({opacity: 'toggle'},speed,callback);
};

$(function() {
	$('a.alert-link').click(function(){
		$('div.alert-link-text').fadeToggle('normal');
		return false;
	});
});
