if (typeof DDC === 'undefined') {
    self.DDC = {
    
        controller : {
        
            carousel : {
                
                addImages : function (theContainer, theArray, length) {
                    var i, theImage;
                    for (i = 0; i < length - 1; i = i + 1) {
                        theImage = $(DDC.model.carousel.images[i]);
                        theContainer.prepend(theImage);
                        theArray.push(theImage);
                    }
                    return theArray;
                },
                
                init : function () {
                    var carouselContainer, carouselArray, length, i;
                    carouselArray = [];
                    carouselContainer = $('#JS_carousel');
                    length = DDC.model.carousel.images.length + 1;
                    carouselArray.push($('img:first', carouselContainer));
                    carouselArray = DDC.controller.carousel.addImages(carouselContainer, carouselArray, length);
                    setInterval(function () {
                        for (var b = 0; b < carouselArray.length; b++) {
                            $(carouselArray[b]).css({
                                'display' : 'block'
                            });
                        }
                        $(carouselArray[1]).css({
                            opacity : 1
                        });
                        $(carouselArray[0]).fadeTo(2500, 0, function () {
                            $(carouselArray[1]).addClass('topImage');
                            $(carouselArray[0]).removeClass('topImage');
                            DDC.controller.carousel.rotate(carouselArray, length);
                        });
                    }, 5000);
                },
                
                // rotates items in an array
                rotate : function (theArray, length) {
                    var first = theArray[0], // first item in the array;
                        i; // array counter
                    
                    for (i = 0; i < (length - 1); i++) {
                        theArray[i] = theArray[i + 1];
                    }
                    theArray[length - 1] = first;
                    
                //    theArray[length - 1].fadeTo('slow', 0);
                  //  theArray.unshift(theArray[length - 1]);
                    //theArray.push(theArray[length]);
                    //theArray.show();
                }   
            },
            
            designList : {
                
                init : function () {
                    var theList = $('#JS_designList li'),
                        largeImageContainer = $('<div class="extra"></div>'),
                        length = theList.length,
                        i,
                        listItem,
                        imgSrc,
                        id,
                        largeImgSrc,
                        wrapLink;
                    
                    if (length > 0) {
                        $(theList.parent()[0]).after(largeImageContainer);
                        for (i = 0; i < length; i++) {
                            listItem = $(theList[i]);
                            imgSrc = $('img:first', listItem).attr('src');
                            id = imgSrc.substring(imgSrc.indexOf('small'), imgSrc.lastIndexOf('.'));
                            largeImgSrc = imgSrc.replace('small', 'large');
                            wrapLink = $('<a class="wrapLink" href="#' + id + '"></a>');
                            wrapLink.html(listItem.html());
                            largeImageContainer.append('<div class="largeChocolate" id="' + id + '"><h2>\'' + $('span', listItem).html() + '\'</h2><p><img alt="Photo of the designer\'s chocolate" src="' + largeImgSrc + '" /></p></div>');                          
                            listItem.empty();
                            listItem.append(wrapLink);
                            wrapLink.bind('click', function () {
                                var href = $(this).attr('href'),
                                	hrefArray = href.split('#'),
                                	showMe = $('#' + hrefArray[1]);
                                
                        
                                $('.largeChocolate').hide();
                                showMe.fadeIn(500);
                                //showMe.focus();
                                return false;
                            });
                        }
                    }
                }
            },
            
            signUp : {
                
                checkAndReplace : function (theInput, checkMe, replaceWithMe) {
                    if (theInput.attr('value') === checkMe) {
                        theInput.attr('value', replaceWithMe);
                    }
                },
                
                clearLabel : function (theInput) {
                    theInput.bind('click', function () {
                        DDC.controller.signUp.checkAndReplace(theInput, DDC.model.signUp.originalValue, '');
                    });
                    theInput.bind('focus', function () { 
                        DDC.controller.signUp.checkAndReplace(theInput, DDC.model.signUp.originalValue, '');
                    });
                    theInput.bind('blur', function () {
                        DDC.controller.signUp.checkAndReplace(theInput, '', DDC.model.signUp.originalValue);
                    });
                },
                
                init : function () {
                    var theInput = $('#newsletter'), // the input field
                        theSubmit = theInput.next('button'); // the submit button
                        
                    theInput.attr('value', DDC.model.signUp.originalValue);
                    
                    DDC.controller.signUp.clearLabel(theInput);
                    theSubmit.bind('click', function () {
                        DDC.controller.signUp.submit(theInput, theSubmit);
                        return false;
                    });
                },
                
                submit : function (theInput, theSubmit) {
                    if (DDC.controller.signUp.validate(theInput.attr('value'))) {
                        DDC.view.signup.validate.success();
                        theInput.parent('form').submit();
                    }
                    
                    // a valid email address has not been entered
                    else {
                        DDC.view.signup.validate.error();
                    }
                    return false;
                },
                
                validate : function (theValue) {
                    if ((theValue.indexOf('@') > '-1') || (theValue.indexOf('.') > '-1')) {
                        return true;
                    }
                    
                    // the value is not valid
                    else {
                        return false;
                    }
                }
            },
            
            tabs : {
                
                init : function () {
                    var theTabs = $('#JS_tabs li'),
                        theTabContents = $('#JS_tabs div'),
                        length = theTabs.length,
                        i,
                        theTabContent;
                    
                        
                    for (i = 0; i < length; i ++) {
                        theTabContent = $(theTabContents[i]);
                        if (i !== 0) {
                            theTabContent.hide();
                        }
                        $(theTabs[i]).click(function (a) {
                            return function () {
                                theTabContents.hide();
                                $(theTabContents[a]).show();
                            };
                        }(i));
                    }
                    
                }
            },
            
            map : {
                
                init : function () {
                    var theTowns = $('#JS_towns li'),
                        theTownContents = $('#JS_townsContent .cont'),
                        contentsLength = theTownContents.length,
                        townsLength = theTowns.length,
                        i,
                        theTownContent;
                    
                        
                    for (i = 0; i < contentsLength; i++) {
                        theTownContent = $(theTownContents[i]);
                        if (i !== 0) {
                            theTownContent.hide();
                        }
                    }
                    
                    for (i = 0; i < townsLength; i++) {                 
                        $(theTowns[i]).click(function (a) {
                            return function () {
                                theTownContents.hide();
                                $(theTownContents[Math.floor(a / 2)]).show();
                            };
                        }(i));
                    }
                }
            }
        },
        
        init : function () {
            DDC.controller.carousel.init();
            DDC.controller.signUp.init();
            DDC.controller.tabs.init();
            DDC.controller.map.init();
            DDC.controller.designList.init();
            DDC.view.bling.xmas.init();
        },
        
        model : {
            
            carousel : {
                
                images : [
                 
                    '<img alt="" src="assets/img/home-02.jpg" style="display: none;"/>',
                    
                    '<img alt="" src="assets/img/home-03.jpg" style="display: none;" />',
                    
                    '<img alt="" src="assets/img/home-04.jpg" style="display: none;" />'
                ]
            },
            
            signUp : {
                
                error : 'Please enter a valid email address',
                
                originalValue : 'Your email...',
                
                success : 'Thanks for registering'
            }
        },
        
        view : {
            
            bling : {
                
                xmas : {
                
                    init : function () {
                        if (!($.browser.msie && $.browser.version == 6)) { 
                            $('.christmas .assortment h1').append('<span class="blingReindeer"></span>');
                            $($('.christmas .assortment .boxesList li')[2]).append('<span class="blingCat"></span>');
                            $($('.christmas .assortment .boxesList li')[3]).append('<span class="blingHumming"></span>');
                            $($('.christmas .assortment .boxesList li')[4]).append('<span class="blingEagle"></span>');
                            $('.christmas .assortment #information').append('<span class="blingReindeerSmall"></span>');
                            $('.christmas .assortment #information').append('<span class="blingReindeerLots"></span>');
                        }
                    }
                }
            },
            
            signup : {
                
                validate : {
                
                    error : function () {
                        alert(DDC.model.signUp.error);
                    },
                    
                    success : function () {
                        alert(DDC.model.signUp.success);
                    }
                }
            },
            
            tabs : function () {
                $('#JS_tabs > ul').tabs();
            }
        }
    };
}
    
$(document).ready(function () {
    DDC.init();
});
