/**
 * Main function to run forms
 * 
 * $Id: VeroFourSeasons.js 16 2008-07-05 00:59:11Z Chris $
 */

(function(){
    
    $E.onDomload(
        function() {
            FS.load();
            $U.includeFlash('animation', '/flash/four-seasons-animation.swf', 259, 194, 7);
        }
    );

})();


FS = {

    load: function() {
        FS.setProductImages();
        FS.setProducts();
        FS.setAddress();
        FS.setTooltip();
        $U.setNewWindowLinks('ckw-new-win');
    },
    
    setProductImages: function() {
        $E.listen('productDetails', 'click', function(e){
            var target = $E.getTarget(e);
            if (target.className == 'imgLink') {
                $E.kill(e);
                if ($D.id('imgWinImg')) {
                    $D.remove('imgWinImg');
                }
                var parts = target.id.split("-");
                var id = parts[1];
                var sizes = parts[2].split('|');
                var width = parseInt(sizes[0]);
                var height = parseInt(sizes[1]) + 30;
                var title = $D.getContent('title-' + id);
                var img = $D.create('img', {'id':'imgWinImg'});
                img.src = '/images/content/product' + id + '-large.jpg';
                $D.id('imgWin').appendChild(img);
                $D.id('imgWin').style.width = width + 'px';
                $D.id('imgWin').style.height = height + 'px';
                $D.setContent('imgWinTitle', title);
                $F.fadeIn('imgWin');
            }
        });
        
        $E.listen('imgWinClose', 'click', function(){
            $D.id('imgWin').style.display = 'none';
        });
    },
    
    setProducts: function(){
        if ($D.id('p1')) {
            
            $E.listen('p1', 'click', function(){
                FS.toggleProduct(1);
            });
            $E.listen('p2', 'click', function(){
                FS.toggleProduct(2);
            });
            $E.listen('p3', 'click', function(){
                FS.toggleProduct(3);
            });
            
        }
    },
    
    toggleProduct: function(num){
        for (var i = 1; i < 4; i++) {
            var elm = $D.id('p' + i);
            if (elm) {
                if (i == num) {
                    var parent = $D.id('p' + i).parentNode.parentNode;
                    $D.appendClass(parent, 'selected');
                } else {
                
                    var parent = $D.id('p' + i).parentNode.parentNode;
                    $D.removeClass(parent, 'selected');
                }
            }
        }
    },

    setAddress: function() {
        if ($D.id('other')) {
            $E.listen('other', 'click', function(){
                if (this.checked) {
                    $D.id('shipAddress').style.display = 'block';
                } else {
                    $D.id('shipAddress').style.display = 'none';
                    $D.id('shipStreet').value = "";
                    $D.id('shipCity').value = "";
                    var state = new $Form.Select('shipState');
                    state.setValue('');
                }
            });
        }
    },
    
    setTooltip: function() {
        var ttEmail = $D.id('ttEmail');
        if (ttEmail != undefined) {
            if (ttEmail.className == 'gift') {
                var message = "This is the email address you used when you registered for the Four Seasons program. If you have not already registered you will need to do so before you proceed here. You can register via the 'Register' tab in the left toolbar.";
            } else if ($D.id('ttEmail').className == 'draw') {
                var message = "This is the email address you used when you registered for the Four Seasons program. If you have not already registered you will need to do so before you proceed here. You can register via the 'Register' tab in the left toolbar.";
            }
            new $W.Tooltip('ttEmail', message, {'left': '-210', 'top': '-100'});
            $E.listen('ttEmail', 'click', function(e){$E.kill(e)});
        }
    }
    
};