var Site = {
    cfg: {
        currentBanner: 0,
        bannerPaused: false,
        bannerTimer: null,
        bannerTime: 5000,
        bannerSrc: ['banner07.png', 'banner09.png', 'banner03.png', 'banner05.png', 'banner06.png', 'banner04.png' ],
        bannerFrSrc: ['banner09-fr.png', 'banner03-fr.png', 'banner05-fr.png', 'banner06-fr.png', 'banner04-fr.png'],
        bannerHref: ['/summer-special.php', '/server-packages.php', '/i7-servers.php', 'ddos-protection.php', '/server-stock.php', '/colocation-unmetered.php'],
        bannerFrHref: ['/fr/server-packages.php', '/fr/i7-servers.php', 'ddos-protection.php', '/fr/server-stock.php', '/fr/colocation-unmetered.php'],
		bannerWhite: [0, 0, 0, 1, 0, 0],
        navOpened: false,
        navAutoClose: true
    }, start: function () {
		if ($('#indexBannerImage') != null && $('#indexBannerImage').hasClass("french")) { /* Since french should not have ddos banner... we delete it. */
			Site.cfg.bannerSrc = Site.cfg.bannerFrSrc;
			Site.cfg.bannerHref = Site.cfg.bannerFrHref;
			Site.cfg.bannerWhite = [0, 0, 1, 0, 0, 0];
		}
        Site.initializeMenu();
        Site.initializeBanner();
        Site.initializeFeatured();
        Site.initializeFeaturedLinks();
        Site.initializePartners(); /*Site.initializeAccordion();*/
        Site.setServerEvenRows();
        Site.setServerClickExpand();
        Site.showVPSCycles();
        setTimeout(Site.preloadImages, 3000);
    }, showVPSCycles: function () {
        $('.paidMonthly').each(function () {
            $(this).parent().mouseenter(function () {
                $(this).stop().animate({
                    'width': 560
                });
                $(this).find('.paidQuarterly').css({
                    display: 'inline'
                }).stop().animate({
                    'opacity': 1
                });
            });
            $(this).parent().mouseleave(function () {
                $(this).stop().animate({
                    'width': 277
                });
                $(this).find('.paidQuarterly').stop().animate({
                    'opacity': 0
                });
            });
        });
        $('.paidAnnually').each(function () {
            $(this).parent().mouseenter(function () {
                $(this).stop().animate({
                    'width': 560
                });
                $(this).find('.paidSemiAnnually').stop().animate({
                    'opacity': 1
                });
            });
            $(this).parent().mouseleave(function () {
                $(this).stop().animate({
                    'width': 277
                });
                $(this).find('.paidSemiAnnually').stop().animate({
                    'opacity': 0
                });
            });
        });
    }, initializeMenu: function () {
        var hideAllSubs = function () {
            $('#navDdMain ol li ol').each(function () {
                $(this).stop().animate({
                    opacity: 0
                });
                $(this).css('display', 'none');
            });
        };
        var openNavigation = function () {
            Site.cfg.navOpened = true;
            $('#navDdMain').css({
                height: 0
            }).stop().animate({
                height: 250
            });
            $('#navDdMain ol').css({
                display: 'block',
                opacity: 0
            }).stop().animate({
                opacity: 0.95
            });
            hideAllSubs();
        };
        var closeNavigation = function () {
            Site.cfg.navOpened = false;
            $('#navDdMain').stop().animate({
                height: 0
            });
            $('#navDdMain ol').stop().animate({
                opacity: 0
            });
            setTimeout(function () {
                $('#navDdMain').css({
                    display: 'none'
                });
            }, 500);
        };
        var autoClose = function () {
            if (Site.cfg.navAutoClose && Site.cfg.navOpened) closeNavigation();
        };
        $('#nav_Service').mouseenter(function () {
            if (!Site.cfg.navOpened) openNavigation();
            Site.cfg.navAutoClose = false;
        }).mouseleave(function () {
            Site.cfg.navAutoClose = true;
            setTimeout(autoClose, 1000);
        });
        $('#navDdMain').mouseleave(function () {
            Site.cfg.navAutoClose = true;
            setTimeout(autoClose, 1000);
        }).mouseover(function () {
            Site.cfg.navAutoClose = false;
        });
        $('#navDdMain ol li').each(function () {
            $(this).mouseover(function () {
                hideAllSubs();
                $(this).children('ol').css('display', 'inline').stop().animate({
                    opacity: 0.95
                }, 150);
            });
        });
    }, initializeBanner: function () {
        if ($('#indexBannerSelector') == null) return false;
        i = 0;
        for (i = 0; i < Site.cfg.bannerSrc.length; i++) {
            $('#indexBannerSelector').append($('<img>').attr({
                'src': '/images/circle_off.b.png',
                'width': 15,
                'height': 15,
                'alt': i,
                'id': 'splashCircle' + i
            }).css({
                'margin-right': '2px',
                'margin-left': '2px'
            }).addClass('splashClickable').click(function () {
                Site.cfg.currentBanner = $(this).attr('alt');
                Site.changeImg();
            }));
        } /* Take out the extras which makes IE 6 & 7 have issues rendering the menu properly */
        if (!jQuery.browser.msie || !(jQuery.browser.version <= 7.0)) {
            $('<img />').addClass('overlayBanner').attr({
                src: '/images/' + Site.cfg.bannerSrc[0],
                id: 'indexBannerOverlay'
            }).css('opacity', 1).insertBefore('#indexBannerRotation');
            $('#indexBannerImage').css('position', 'relative');
            $('#indexBannerSelector').css('position', 'relative');
        }
        Site.changeImg();
    }, changeImg: function () {
        if (Site.cfg.bannerPaused) return;
        clearTimeout(Site.cfg.bannerTimer);
        Site.cfg.bannerTimer = setTimeout(function () {
            Site.cfg.currentBanner = (++Site.cfg.currentBanner % Site.cfg.bannerSrc.length);
            Site.changeImg();
        }, Site.cfg.bannerTime);
        $('#indexBannerSelector img').each(function (i, obj) {
            $(obj).attr({
                'src': '/images/circle_off.b.png'
            });
        });
		if (Site.cfg.bannerWhite[Site.cfg.currentBanner] == 1) {
			$('#indexBannerSelector img').each(function (i, obj) {
				$(obj).attr({
					'src': '/images/circle_off.l.png'
				});
			});
		}
		if (Site.cfg.bannerWhite[Site.cfg.currentBanner] == 1) {
			$('#splashCircle' + Site.cfg.currentBanner).attr({
				'src': "/images/circle_on.l.png"
			});
		} else {
			$('#splashCircle' + Site.cfg.currentBanner).attr({
				'src': "/images/circle_on.b.png"
			});
		}
        var url = "";
        var currentBannerSrc;
        if ($('#indexBannerImage').hasClass("french")) { /* use french */
            currentBannerSrc = Site.cfg.bannerFrSrc[Site.cfg.currentBanner];
            url = Site.cfg.bannerFrHref[Site.cfg.currentBanner];
        } else { /* use english */
            currentBannerSrc = Site.cfg.bannerSrc[Site.cfg.currentBanner];
            url = Site.cfg.bannerHref[Site.cfg.currentBanner];
        }
        if (!jQuery.browser.msie || !(jQuery.browser.version <= 7.0)) {
            $('#indexBannerOverlay').attr({
                'src': $('#indexBannerRotation').attr('src')
            }).css('opacity', 1).animate({
                opacity: 0
            }, 250);
            $('#indexBannerRotation').attr({
                'src': "/images/" + currentBannerSrc
            }).css('opacity', 1);
        } else { /* For IE 6 & 7, just update the banner directly */
            $('#indexBannerRotation').attr({
                'src': "/images/" + currentBannerSrc
            });
        }
        $('#indexBannerRotationAnchor').attr({
            'href': url
        });
    }, initializeFeatured: function () {
        if ($('#vpsTab') == null) return false;
        Site.changeFeatureTabs('Dedi');
        $('#vpsTab').click(function (evt) {
            evt.preventDefault();
            Site.changeFeatureTabs('VPS');
        });
        $('#coloTab').click(function (evt) {
            evt.preventDefault();
            Site.changeFeatureTabs('Colo');
        });
        $('#dediTab').click(function (evt) {
            evt.preventDefault();
            Site.changeFeatureTabs('Dedi');
        });
    }, changeFeatureTabs: function (featureTab) {
        $('#featuredTab').attr({
            className: "select" + featureTab
        });
        var urlPrefix = "";
        if ($('#featuredProducts').hasClass("french")) {
            urlPrefix = "/fr";
        }
        $('#VPSPackages').css({
            display: "none"
        });
        $('#DediPackages').css({
            display: "none"
        });
        $('#ColoPackages').css({
            display: "none"
        });
        $('#' + featureTab + 'Packages').css({
            display: "inline"
        });
        switch (featureTab) {
        case "VPS":
            $('#featuredMoreUrl').attr({
                href: urlPrefix + "/vps-packages.php"
            });
            break;
        case "Dedi":
            $('#featuredMoreUrl').attr({
                href: urlPrefix + "/server-packages.php"
            });
            break;
        case "Colo":
            $('#featuredMoreUrl').attr({
                href: urlPrefix + "/colocation-packages.php"
            });
            break;
        }
    }, initializeFeaturedLinks: function () {
        if ($('#vps1') == null) return false;
        var urlPrefix = "";
        if ($('#featuredProducts').hasClass("french")) {
            urlPrefix = "/fr";
        }
        $('#vps1').click(function (e) {
            self.location = urlPrefix + '/vps-packages.php';
        });
        $('#vps2').click(function (e) {
            self.location = urlPrefix + '/vps-ddos-packages.php';
        });
        $('#colo1').click(function (e) {
            self.location = urlPrefix + '/colocation-packages.php';
        });
        $('#colo2').click(function (e) {
            self.location = urlPrefix + '/colocation-ddos-packages.php';
        });
        $('#dedi1').click(function (e) {
            self.location = urlPrefix + '/server-packages.php';
        });
        $('#dedi2').click(function (e) {
            self.location = urlPrefix + '/server-ddos-packages.php';
        });
    }, initializePartners: function () {
        $('img.partner').mouseenter(function () {
            this.src = this.src.replace('off.png', 'on.png');
        });
        $('img.partner').mouseleave(function () {
            this.src = this.src.replace('on.png', 'off.png');
        });
    }, preloadImages: function () {
        var images = ['/images/nav.menubg.png', '/images/featuretab.vps.png', '/images/featuretab.colo.png', '/images/featuretab.dedi.png', '/images/partners/partner.cisco.on.png', '/images/partners/partner.gblx.on.png', '/images/partners/partner.juniper.on.png', '/images/partners/partner.level3.on.png', '/images/partners/partners.med.hibernia.on.png', '/images/partners/partners.med.tinet.on.png', '/images/partners/partner.tata.on.png', '/images/feature_coloweb1.png', '/images/feature_coloprotected1.png', '/images/feature_dualcoree2160.png', '/images/circle_on.l.png', '/images/circle_on.b.png', '/images/circle_off.l.png', '/images/circle_off.b.png', '/images/banner03.png', '/images/banner04.png', '/images/banner05.png', '/images/banner06.png'];
        var requestedFilename = Site.requestedFilename();
        if (requestedFilename == 'index.php' || requestedFilename == '') jQuery.each(images, function (i, img) {
            $.preloadImages(img);
        });
        $('#mapelLeaf').attr({
            src: '/images/canadaleaf_loaded.png'
        });
    }, requestedFilename: function () {
        var myUrl = self.location.toString().split('/');
        return myUrl[myUrl.length - 1];
    }, setServerTab: function (showType) {
        $('.serverTab').attr({
            className: 'serverTab'
        });
        $('.serverTabEnd').attr({
            className: 'serverTabEnd'
        });
        $('#serv_tab_' + showType + 's').attr({
            className: ''
        }).addClass('serverTab').addClass('tabOn');
        $('#serv_tab_' + showType + 'e').attr({
            className: ''
        }).addClass('serverTabEnd').addClass('tabEndOn');
        $('.serverItem').css({
            display: 'none'
        });
        $('.' + showType + ' td').css({
            opacity: 0
        });
        $('.' + showType).css({
            display: ''
        });
        $('.' + showType + ' td').stop().animate({
            opacity: 1
        });
    }, setServerHeading: function (heading) {
        $('.dedicatedServers .tableHeading').text(heading + ' Servers');
    }, setServerEvenRows: function () {
        $('.dedicatedServers tr.serverItem:even').each(function (i) {
            $(this).addClass('alternate');
        });
    }, setServerClickExpand: function () {
        $('.serverItem').each(function (i) {
            if ($(this).attr('id') != '') {
                $(this).click(function () {
                    Site.toggleServerRow($(this).attr('id'));
                }).mouseover(function () {
                    $(this).addClass('hovered');
                    $('#' + $(this).attr('id') + '_expanded').addClass('hovered');
                    Site.tipServerRow($(this).attr('id'));
                }).mouseout(function () {
                    $(this).removeClass('hovered');
                    $('#' + $(this).attr('id') + '_expanded').removeClass('hovered');
                    Site.untipServerRow($(this).attr('id'));
                }); /* Add the tip after every server item */
                newRow = $('<tr>').attr({
                    id: $(this).attr('id') + '_tip'
                }).addClass('notip').append($('<td>').attr({
                    'colSpan': 7
                }));
                if ($(this).hasClass('alternate')) newRow.addClass('alternate');
                newRow.insertAfter('#' + $(this).attr('id'));
            }
        });
    }, toggleServerRow: function (what) {
        if ($('#' + what + '_expanded').hasClass('expanded')) Site.collapseServerRow(what);
        else Site.expandServerRow(what);
    }, expandServerRow: function (what) {
        newRow = $('<tr>').attr({
            id: what + '_expanded'
        }).addClass('serverItem').addClass('expanded').css({
            'background-image': 'url(/images/table.border.bg.png)',
            'background-repeat': 'repeat-y'
        }); /* Alternate rows */
        if ($('#' + what).hasClass('alternate')) {
            newRow.addClass('alternate');
        }
        newRow.addClass('hovered'); /* because the mouse must be over the parent for this event to happen. */
        newRow.mouseover(function () {
            $(this).addClass('hovered');
            $('#' + $(this).attr('id').replace('_expanded', '')).addClass('hovered');
            $('#' + $(this).attr('id').replace('_expanded', '_tip')).addClass('hovered');
        }).mouseout(function () {
            $(this).removeClass('hovered');
            $('#' + $(this).attr('id').replace('_expanded', '')).removeClass('hovered');
            $('#' + $(this).attr('id').replace('_expanded', '_tip')).removeClass('hovered');
        });
        newRow.append($('<td>').attr({
            'colSpan': 7
        }).append($('<div>').css('width', '100%').addClass('serverDescription').html($('#' + what + '_hiddenPrice td:first-child').html())));
        newRow.insertAfter('#' + what);
        $('#' + what + '_arrow').attr({
            'src': '/images/serveritem.expanded.gif'
        });
        $('#' + what + '_tip').removeClass('tip').addClass('untip');
    }, collapseServerRow: function (what) {
        $('#' + what + '_expanded').remove();
        $('#' + what + '_arrow').attr({
            'src': '/images/serveritem.collapsed.gif'
        });
        $('#' + what + '_tip').removeClass('untip').addClass('tip');
    }, tipServerRow: function (what) {
        if ($('#' + what + '_expanded').hasClass('expanded')) {
            $('#' + what + '_tip').removeClass('tip').removeClass('notip').addClass('untip');
        } else {
            $('#' + what + '_tip').removeClass('untip').removeClass('notip').addClass('tip');
        }
    }, untipServerRow: function (what) {
        $('#' + what + '_tip').removeClass('tip').removeClass('untip').addClass('notip');
    }, hideServerRow: function (what) {
        $.remove('#' + what + '_expanded');
    }, initializeAccordion: function () {
        if ($('#backupMethods') == null) return false;
        $(document).ready(function () {
            $('#backupTypes').accordion({
                autoHeight: false
            });
            $('#backupMethods').accordion({
                autoHeight: false
            });
        });
    }
};
$(function () {
    Site.start();
});