try{SC.addVersion('0112', '1.0.000', 'Recently Viewed Items');}catch(err){};
function scRecentlyViewed( cName, cNum ) {
        var cookieName = cName;
        this.setName = function ( cName ) {
                /*
                        setName - Public - Sets the name of the cookie
                        PARAMETERS:     cName - the name of the cookie as a string
                        RETURN: none
                */
                cookieName = cName;
        };
        
        var howMany = SC.forceInt(cNum);
        this.setHowMany = function (showHowMany) {
                /*
                        setName - Public - Sets how many (item) cookies will be set/displayed
                        PARAMETERS:     showHowMany - the number to display as an integer
                        RETURN: none
                */
                howMany = SC.forceInt(showHowMany);
        };
        
        var currPage = null;
        this.display = function (cols, format){
                /*
                        display - Public - Displays the set of items which were recently viewed
                        PARAMETERS:     cols - the number of columns for the recently-viewed items table as an integer
                                                format - the format for the recently-viewed items table ("ell" or "vertical")
                        RETURN: none
                */
                var tmpCook,
                ctr = 0,
                cookieArr = [];
                while ((ctr <= howMany) && (tmpCook = SC.fromJSON(SC.getCookie(cookieName + ctr)))) {
                        for (var i in tmpCook){
                                if(SC.typeOf(tmpCook[i]) == "string") tmpCook[i] = unescape(tmpCook[i]);
                        }
                        if (tmpCook.i != currPage) cookieArr.push(tmpCook);
                        ctr++;
                }
                
                displayConts("recentVitems", cookieArr, cols, format);
                
                function drcImg( iObj ){
                        var tContent = "";
                        if (iObj.im) {
                                tContent += "<a href=\"" + iObj.i + ".html\"><img alt=\"" + iObj.n + "\" src=\"" + iObj.im + "\" /></a>";
                        }
                        return tContent;
                }
                
                function drcInfo( iObj ){
                        var tContent = "<div class=\"name\"><a href=\"" + iObj.i + ".html\">" + iObj.n + "</a></div>";
                        if (iObj.p) {
                                tContent += "<div class=\"price" + (!iObj.sp ? "-bold" : "") + "\">" + iObj.p + "</div>";
                        }
                        if (iObj.sp) {
                                tContent += "<div class=\"sale-price-bold\">" + iObj.sp + "</div>";
                        }
                        return tContent;
                }
                
                function displayConts( elmId, iArr, cols, format ){
                        var elm = document.getElementById(elmId);
                        if (elm && (iArr.length > 0)) {
                                elm.innerHTML = "";
                                format = format.toString().toLowerCase();
                                var ell = (format == "ell")? true : false;
                                howMany = (iArr.length < howMany)? iArr.length : howMany;
                                cols = (cols > howMany)? howMany : cols;
                                rows = Math.ceil(howMany / cols);
                                var tContent = "<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" class=\"contents-table\">";
                                for (var i = 0; i < rows; i++) {
                                        var prevCells = i * cols;
                                        tmpCols = ((prevCells + cols) > howMany)? howMany - prevCells : cols;
                                        tContent += "<tr>";
                                        for (var g = 0; g < tmpCols; g++) {
                                                var currItem = (i * cols) + g;
                                                tContent += "<td class=\"imgCell\">";
                                                tContent += drcImg(iArr[currItem]);
                                                tContent += "</td>";
                                                if (ell) {
                                                        tContent += "<td class=\"infoCell\" width=\"" + (100 / cols) + "%\">";
                                                        tContent += drcInfo(iArr[currItem]);
                                                        tContent += "</td>";
                                                }
                                                if (g < (cols - 1)) {
                                                        tContent += "<td class=\"verticalSpacer\" rowspan=\"" + ((ell)? 1 : 2) + "\"></td>";
                                                }
                                        }
                                        if (!ell) {
                                                tContent += "</tr><tr>";
                                                for (var g = 0; g < tmpCols; g++) {
                                                        var currItem = (i * cols) + g;
                                                        tContent += "<td class=\"infoCell\" width=\"" + (100 / cols) + "%\">";
                                                        tContent += drcInfo(iArr[currItem]);
                                                        tContent += "</td>";
                                                }
                                        }
                                        tContent += "</tr>";
                                        if (i < (rows - 1)) {
                                                tContent += "<tr><td class=\"horizontalSpacer\" colspan=\"" + (cols * ((ell)? 3 : 2) - 1) + "\"></td></tr>";
                                        }
                                }
                                tContent += "</table>";
                                elm.innerHTML = tContent;
                        }
                }
        };
        
        this.set = function (name,id,imageurl,price,saleprice){
                /*
                        set - Public - Adds the current item to the set of recently-viewed items; Sets the cookies appropriately
                        PARAMETERS:     name - the name as a string
                                                id - the id as a string
                                                imageurl - the image path as a string
                                                price - the price information as a string
                                                saleprice - the sale price information as a string
                        RETURN: none
                */
                currPage = id;
                var tmpCook,
                ctr = 0,
                cookieObj = {n: name, i: id, im: imageurl, p: price, sp: saleprice},
                cookieArr = [];
                for (var i in cookieObj) cookieObj[i] = escape(cookieObj[i]);
                cookieArr.push(cookieObj);
                while ((ctr < howMany) && (tmpCook = SC.fromJSON(SC.getCookie(cookieName + ctr)))) {
                        if (tmpCook.i != cookieObj.i) cookieArr.push(tmpCook);
                        ctr++;
                }
                for (var i = 0, j = cookieArr.length; i < j; i++) {
                        SC.setCookie(cookieName + i);
                        SC.setCookie(cookieName + i, SC.toJSON(cookieArr[i]), '/');
                }
        };
}
        

