// Main JS for arearugs.com
jQuery(document).ready(function ($) {

 // Hero Slideshow
 $("#slideshow div")
 .after('<div id="pager">')
 .cycle({
 fx: 'fade',
 speed: 500,
 timeout: 7000,
 pager: '#pager'
 });

 // Product Page Image Thumbs
 $("#product-images").cycle({
 fx: 'fade',
 speed: 500,
 timeout: 0
 });

// Fancy Box
$("a.zoom").fancybox();

// Tabify Product Description & Reviews
$("#product-details").tabs();

// Select Menus
(function selectMenus() {
 var selects = $('#product-selector select');
 selects.selectmenu({
 style: 'dropdown',
 // Add price spans to things that look like dollar amounts
 format: function (f) {
 return f.replace(/(\$[\d|,]+\.\d+)/g, "<span class='price'>\$1</span>");
 },
 width: 298
 });
 // Hide the dropdown for and disable selects with only 1 item
 selects.each(function (index, s) {
 s = $(s);
 if (s.find("option").length === 1) {
 s.selectmenu("disable");
 $("#" + s.next().attr("aria-owns")).hide();
 }
 });
})();

// Send user to selected url on search result change
$(".filter-search-results select").bind("change", function () {
 window.location.href = $(this).val();
});

/**
 * This is a hack to make it so you can use a select to select grouped products
 */
(function productSizeSelect() {
 var select = $("#size"), field = $("#wilburys");
 select.bind("change", function (event) {
 field.attr("name", "super_group[" + $(this).val() + "]");
 });
})();

/**
 * Another hack to reload the page on the correct product when a color is
 * selected. I guess we can use something else once we figure out what we're
 * doing.
 */
$("#color").bind("change", function () {
 $("button.add-to-cart").addClass("ui-state-disabled").
 attr("disabled", "disabled");
 window.location.href = $(this).val();
});

// Carousel
$(".carousel").each(function (i, carousel) {
 var c = $(this),
 liCount = c.find("li").length,
 min = 3, // only do it if there's more than this many
 buttons = [c.prev(), c.next()];

 if (liCount > min) {
 c.carousel({ visible: min }).addClass("ui-carousel-active");
 $.each(buttons, function (i, b) { b.removeClass("ui-state-disabled"); });
 buttons[0].bind("click", function () { c.carousel("prev"); });
 buttons[1].bind("click", function () { c.carousel("next"); });
 }
});

/**
 * Resize the From the Blog container to be equal to the slide show
 */
(function resizeFromTheBlog() {
 var ss = $("#slideshow .module-border"),
 ftb = $("#from-the-blog .module-border"),
 p = parseInt(ftb.css("padding-top"), 10) +
 parseInt(ftb.css("padding-bottom"), 10);
 ftb.height(ss.height() - p);
})();

});

// Magento's Analytics setup forgets this
var _gaq = [];

