/*
 *
 * Custom Actions
 * Author: Nate Hunzaker of Smashing Boxes
 *
 */

jQuery(document).ready(function($) {
    
    // First, let's create an object with all of the actions we want to take:
    var actions = {
        
        manageNav: function() {
            
            var nav_width = $("#nav").width();
            var linkCount = $("#nav").children("li.level0").length;
            var jewel_width = $("#loose_jewels").width();
            var jewelCount = $("#loose_jewels ul li").length;
            
            // Factor in borders on navigation bar:
            nav_width -= (linkCount * 1) - (linkCount / 2);
            
            // Resize Nav Bar
            $("#nav li.level0, #nav li ul").css("min-width", Math.floor(nav_width / linkCount) );

            $("#nav ul li a").css("width", $("#nav li ul").width() );

            
            // Animate the navigation bar
            $("#nav li").hover(function() {
                $(this).children("ul:first").stop().slideUp(0).slideDown(250, function() {
                    $(this).css("height","");
                });
            }, function() {
                $(this).children("ul:first").stop().slideUp(250, function() {
                    $(this).css("height","");
                });
            });

            // Resize the loose jewels to spread even
            $("#loose_jewels li").width( Math.floor(jewel_width / jewelCount) );
            
            // For chaining
            return actions;
        },
        
        upsellHovers: function() {
            
            var hoverIntent,
            windowSize = $(window).width();
            
            // Add the hover event
            $(".products-grid > li > div.product-box").mouseenter(function() {
                
                windowSize = $(window).width();
                
                // Ensures overlays do not bleed over into the abyss
                //( windowSize > 1360 ) ? $(".overlay").addClass('left') : $(".overlay").removeClass('left');
                
                clearTimeout(hoverIntent);
                
                $(".overlay").stop().animate({ opacity: 0}, 500, function() { $(this).removeClass("hover"); });
                
                $(this).siblings(".overlay").addClass("hover").stop().animate({ opacity: 1}, 500);
            });
            
            $(".overlay, .products-grid").mouseleave(function() {
                var that = $(".overlay");
                hoverIntent = setTimeout(function() {
                    that.stop().animate({ opacity: 0 }, 500, function() { $(this).removeClass("hover"); });
                }, 500);
            });
        },
        
        /*
          hideMiniCartOnClick: function() {
          
          $(".wrapper").click(function (e) {
          
          $("#topCartContent").slideUp(400, function() {
          $(".block-title").removeClass("expanded");
          $(this).removeClass("process");
          });

          });
          
          },
        */

        // Runs through all functions within
        initialize: function() {
            var i;
            
            for(i in actions) {
                if (i !== "initialize") {
                    actions[i]();
                }
            }
            
        }
        
    }
    
    // Queue everything up
    actions.initialize();
    
	// Disables clicking of collections
	$('li.level0.nav-6 a.level-top[href$="/collections"]').click(function(e){ e.preventDefault(); return false;});
    
	//add forever brilliant, remove collections
	$("div#link-box li a[href$='/collections']").hide();
	$("ul.level0 li a[href$='/collections/green']").parent().before('<li><a href="/forever">forever brilliant</a></li>');
   
   // Added by Parris Lucas of Charles and Colvard as a quick fix for direct
    // links to Gemstones in Nav Bar
  	$("li.level0.nav-5.level-top li.level1 a").each(function(index, element) { 
        var u = /(http|ftp|https):\/\//;
		var h = /[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?/;
        var o = jQuery(this).attr("href").replace(u,'');
        o = o.replace(document.location.host,'');
        switch(o) {
  			case '/gemstones/round-5':
    				$(this).attr('href', '/round');
    				break;
    		case '/gemstones/gemstone-square':
    				$(this).attr('href', '/square');
    				break;
    		case '/gemstones/gemstone-cushion':
    				$(this).attr('href', '/cushion');
    				break;
    		case '/gemstones/gemstone-radiant':
    				$(this).attr('href', '/radiant');
    				break;
    		case '/gemstones/gemstone-oval':
    				$(this).attr('href', '/oval');
    				break;
    		case '/gemstones/gemstone-marquise':
    				$(this).attr('href', '/marquise');
    				break;
    		case '/gemstones/gemstone-pear':
    				$(this).attr('href', '/pear');
    				break;
    		case '/gemstones/gemstone-trillion':
    				$(this).attr('href', '/trillion');
    				break;
    		case '/gemstones/gemstone-triangle':
    				$(this).attr('href', '/triangle');
    				break;
    		case '/gemstones/gemstone-heart':
    				$(this).attr('href', '/heart');
    				break;
    		case '/gemstones/gemstone-octagon':
    				$(this).attr('href', '/octagon');
    				break;
  			case '/gemstones/gemstone-castle-2':
    				$(this).attr('href', '/castle');
    				break;
  			default:
            break;
  		  }
     });
                           
                           
    // Product Listing Page - Hide Regular Price TODO: Add Logic to \app\design\frontend\enterprise\charlesandcolvard\template\catalog\product\view.phtml
    //$("div.has-minimal span.regular-price span.price").hide();

    // Product Listing Page - Reduce space between regular and special prices TODO: Update StyleSheet
    //$("div.category-products div.price-box p.old-price").css("margin", 0);

    // Product Listing Page & Product Page -- Change special price to red TODO: Update Stylesheet
    //$("div.category-products div.price-box p.special-price span.price").css("color", "tomato");
    //$("div.product-shop.simple div.price-box p.special-price span.price").css("color", "tomato");
    //$("div.product-view div.product-shop.simple div.price-box p.old-price").show().css("margin-bottom", "10px").children("span.price").css("color","#205791");


    // Live Chat Operations
    // -------------------------------------------------- //
    // A simple hack to make live chat work the way we need it to
    // without adding even more needless markup
    var chat_class = $("#top_chat_link").text().toLowerCase().replace(/(\(|\)|live|chat|\s)/ig, "");
    $("#top_chat_link").removeClass('offline').addClass(chat_class);

});

