(function (){
     var $$$ = $$$ || {};

     $$$.namespace = function (namespaceString) {
         var parts = namespaceString.split('.'),
         parent = window,
         currentPart = '';    

         for(var i = 0, length = parts.length; i < length; i++) {
             currentPart = parts[i];
             parent[currentPart] = parent[currentPart] || {};
             parent = parent[currentPart];
         }

         return parent;
     };

     var utils = $$$.namespace("$$$.utils");
     utils.disableSale = function () {
         var $link = $('div.top_horiz_nav > div.nav_content.su_category_48').children('a').first();
         $link.addClass('disabled');
         $link.click(function(e) {
                         e.preventDefault();
                     });
     };
     utils.applyStyles = function() {
         var style = '<style type="text/css">';
         var $head = $('head:first');
         if (utils.isCurrentlyViewingProduct()){
             var $breadCrumbs = utils.getBreadCrumbs();
             $breadCrumbs.each(function(i) {
                                   style += utils.getStylesFromBreadCrumbs(i,$(this));
                               });
         }
         else if (!utils.isCurrentlyViewingArticle()){
             style += '#content #main_content #content_area > table > tbody:first-child > tr:first-child > td:first-child > table:first-child > tbody:first-child > tr:first-child > td:first-child > table:first-child {'
                      + 'border-collapse: collapse;'
                      + '}';
         }
         style += '</style>';
         // link product specific css
         $head.append(style);
         // link category specific css
         $head.append(utils.getCustomStylesheet());
         return 0;
     };
     utils.getBreadCrumbs = function() {
         return $('#content_area table tr td').first().find('a');
     };
     utils.getStylesFromBreadCrumbs = function(i,$breadCrumb) {
         var categoryID = utils.getCategoryIDFromURL($breadCrumb.attr('href'));
         var css = '';
         if (categoryID != ''){
             if (i > 1) {
                 // this is not root
                 var tmpCss = '.su_category_%s,.su_category_%s .children {'
                              + 'display: block !important;'
                              + '}';
             }
             else if (i==1) {
                 // this is root
                 var tmpCss = '.su_category_%s,#nav_menu {'
                              + 'display: block !important;'
                              + '}';
             }
             else {
                 // this should never be executed coz the case where i==0 is
                 // discarded since it doesn't correspond to a valid categoryID
                 throw "Unexpected control flow: See utils.js";
             }
             css += tmpCss.replace(/%s/g,categoryID);
             if ($breadCrumb.next()[0].tagName != 'A') {
                 // highlighted category
                 var tmpCss = '.su_category_%s > a,.su_category_%s .children > a{'
                              + 'color: #D5A00C !important;'
                              + '}';
                 css += tmpCss.replace(/%s/g,categoryID);
             }             
         }
         return css;
     };
     utils.getTrailingURL = function (url) {
         var urlSplit = url.split('/');
         return urlSplit[urlSplit.length -1];
     };
     utils.getCustomStylesheet = function() {
         var key = '';
         var dict = {
             'Store_Locations':'article',
             'all':'products',
             'graphic':'products',
             'travel':'products',
             'coastal':'products',
             'botanic':'products',
             'collections':'products',
             'quilts':'products',
             'euro_shams':'products',
             'pillow_cases':'products',
             'runners':'products',
             'placemats':'products',
             'napkins':'products',
             'napkin_rings':'products',
             'sale':'sale'
         };
         var linkTitleTag = utils.getCurrentLinkTitleTag();
         if (dict[linkTitleTag]) {
             key = dict[linkTitleTag];
         }
         else if (utils.isCurrentlyViewingArticle()) {
             key = 'article';
         }
         else {
             key = linkTitleTag;
         }
         if (key != ''){
             var href = '/v/vspfiles/css/%s/style.css'.replace('%s',key);
             return '<link rel="stylesheet" type="text/css" href="'
                    + href
                    + '" />';
         }
         else {
             return '';
         }
     };
     utils.getCurrentLinkTitleTag = function() {
         var urlSplit = window.location.pathname.split('/');
         var linkTitleTag = urlSplit[urlSplit.length -2];
         if (linkTitleTag=='') {
             return linkTitleTag;
         }
         else {
             return linkTitleTag.substr(0,linkTitleTag.length -2);
         }
     };
     utils.getCategoryIDFromURL = function(url) {
         var lastUrl = utils.getTrailingURL(url);
         return lastUrl.substr(0,lastUrl.search('.htm'));         
     };
     utils.getCurrentCategoryID = function() {
         return utils.getCategoryIDFromURL(window.location.pathname);         
     };
     utils.isCurrentlyViewingArticle = function() {
         var dict = {
             'Store_Locations':'article'
         };
         var linkTitleTag = utils.getCurrentLinkTitleTag();
         if (dict[linkTitleTag]) {
             return true;
         }
         var lastUrl = utils.getTrailingURL(window.location.pathname);
         return lastUrl.search('.asp') > -1;
     };
     utils.isCurrentlyViewingProduct = function() {
         var categoryID = utils.getCurrentCategoryID();
         return !isFinite(categoryID);
     };

})();


