

jQuery(document).ready(function($){




    // WHAT'S NEW SLIDER SECTION
    
    // BEGIN safe to modify
    
    var sliderspeed = 1800;
    var slidereasing = 'easeOutExpo';
    
    // END safe to modify
    
    var sliderfirst = 0;
    var slidershow = 291;
    var slider = $('#whatsnew-slide ul');
    var sliderwidth = $('#whatsnew-slide li').size();
    var sliderpos = sliderfirst;
    
    sliderwidth = sliderwidth * 291;
    sliderlast = sliderwidth - slidershow + 11;
    
    $(slider).css('margin-left', sliderpos + 'px');
    $('#whatsnew-slide').css('width', sliderwidth + 'px');
    
    $('<div class="pngfix slidernav prev prev-disabled">previous</div><div class="pngfix slidernav next">next</div>').insertBefore('#whatsnew-frame');
    
    $('#whatsnew .prev').hover(function(){
        if ($(this).hasClass('next-disabled') == false) {
            $(this).addClass('next-hover');
        }
    }, function(){
        $(this).removeClass('next-hover');
    });
    
    $('#whatsnew .next').hover(function(){
        if ($(this).hasClass('prev-disabled') == false) {
            $(this).addClass('prev-hover');
        }
    }, function(){
        $(this).removeClass('prev-hover');
    });
    
    function sliderrun(sliderpos, button, state){
        $(slider).animate({
            marginLeft: sliderpos + 'px'
        }, sliderspeed, slidereasing, function(){
            $(button).removeClass(state + 'active');
            if (sliderpos == -(sliderlast)) {
                $('#whatsnew .next').addClass(state + 'disabled');
            }
            if (sliderpos == sliderfirst) {
                $('#whatsnew .prev').addClass(state + 'disabled');
            }
        });
    }
    
    $('#whatsnew .next').disableTextSelect();
    $('#whatsnew .next').click(function(){
        if (sliderpos != -(sliderlast)) {
            $(this).blur();
            $('#whatsnew .prev').removeClass('prev-active');
            $('#whatsnew .prev').removeClass('prev-disabled');
            $(slider).stop(true, false);
            $(this).addClass('next-active');
            sliderpos = sliderpos - slidershow;
            if (sliderpos < -(sliderlast)) {
                sliderpos = -(sliderlast)
            }
            sliderrun(sliderpos, this, 'next-');
        }
    });
    
    $('#whatsnew .prev').disableTextSelect();
    $('#whatsnew .prev').click(function(){
        if (sliderpos != sliderfirst) {
            $('#whatsnew .next').removeClass('next-active');
            $('#whatsnew .next').removeClass('next-disabled');
            $(slider).stop(true, false);
            $(this).addClass('prev-active');
            sliderpos = sliderpos + slidershow;
            if (sliderpos > sliderfirst) {
                sliderpos = sliderfirst;
            }
            sliderrun(sliderpos, this, 'prev-');
        }
    });
    
    
    
    
    // BIG CROSSFADING SLIDESHOW
    
    // BEGIN safe to modify
    
    autoSpeed = 2400;
    manualSpeed = 1200;
    autoSpeedBetween = 15000;
    
    // END safe to modify
    
    slideshow = [];
    slideshowImages = [];
    slideLoading = $('<div class="main-slideshow-css"></div>');
    slideButton = $('<div title="Next slide" class="main-slideshow-next pngfix">&nbsp</div>');
    slideshowNumCache = 0;
    
    $('#main-slideshow li').each(function(){
        var slideText = '';
        var slideLink = 'javascript:void(0)';
        var slideObj = $('<a class="main-slideshow-css"></a>').css('opacity', 0);
        if ($(this).find('a').length == 1) {
            slideText = $(this).find('a').eq(0).text();
            slideLink = $(this).find('a').attr('href');
        }
        else 
            if ($(this).text().length > 0) {
                slideText = $(this).text();
            }
        $(slideObj).attr({
            title: slideText,
            href: slideLink
        });
        $(slideObj).css({
            backgroundImage: 'url(' + $(this).attr('title') + ')',
            outline: 'none'
        });
        slideshowImages.push($(this).attr('title'));
        slideshow.push(slideObj);
    });
    
    function loadSlideshow(slideshowNum, effSpeed){
    
        if (slideshowNum >= slideshow.length) {
            slideshowNumCache = 0;
        }
        
        var slideImage = new Image();
        $(slideImage).load(function(){
            $('.main-slideshow-css').css('z-index', 2);
            $('#content').append($(slideshow).get(slideshowNumCache));
            
            $(slideshow).get(slideshowNumCache).animate({
                opacity: 1
            }, effSpeed, 'linear', function(){
                if ($('.main-slideshow-next').length == 0) {
                    $('#content').append(slideButton);
                }
                
                var n = 0;
                $('#content').find('.main-slideshow-css').each(function(){
                    if (n < $('#content .main-slideshow-css').length - 1) {
                        $(this).css('opacity', 0).remove();
                    }
                    n++;
                });
                
                $(slideshow[slideshowNumCache]).oneTime(autoSpeedBetween, 'mainSlideshow', function(){
                    slideshowNumCache++;
                    loadSlideshow(slideshowNumCache, autoSpeed);
                });
            });
        });
        slideImage.src = slideshowImages[slideshowNumCache];
    }
    
    $('#content').append(slideLoading);
    
    $(slideButton).hover(function(){
        $(this).addClass('main-slideshow-next-hover');
    }, function(){
        $(this).removeClass('main-slideshow-next-hover');
        
    });
    
    $(slideButton).disableTextSelect();
    $(slideButton).click(function(){
        $(slideshow).each(function(){
            $(this).stop(true, false);
            $(this).stopTime();
        });
        $('#content').find('.main-slideshow-css').css('opacity', 0).remove();
        slideshowNumCache++
        loadSlideshow(slideshowNumCache, manualSpeed);
    });
    
    
    
    
    // DROP DOWN LIST
    
    $('#top-panel h4').disableTextSelect();
    $('.dropclip').prepend('<div class="scroll-area"><div class="scroller"></div></div>');
    
    $('#top-panel h4').click(function(){
        if ($(this).parent().find('.dropclip:hidden').length > 0) {
            $(this).parent().parent().find('.droplist').each(function(){
                hideDroplist($(this).find('h4').eq(0));
            });
            showDroplist(this);
            
            var docH = ($(this).parent().find('ul').eq(0).height()) + 1;
            var contH = $(this).parent().find('.dropclip').eq(0).height();
            var scrollAreaH = $(this).parent().find('.scroll-area').eq(0).height();
            var scrollH = 10;
            if (scrollH < 10) 
                scrollH = 10;
            scrollDist = Math.round(scrollAreaH - scrollH);
            
            var scroller = $(this).parent().find('.scroller').eq(0)
            
            $(scroller).height(Math.round(scrollH));
            
            $(scroller).draggable({
                axis: 'y',
                containment: 'parent',
                cursor: 'default',
                drag: function(event, ui){
                    var scrollY = parseInt($(scroller).css('top'));
                    var docY = 0 - (scrollY * (docH - contH) / scrollDist)
                    $('.dropclip ul').css('top', docY + "px");
                }
            });
            
            $(this).parent().find('.dropclip').eq(0).mousewheel(function(event, delta){
            
                var scrollerCurrentPos = parseInt($(this).find('.scroller').css('top'));
                
                if (isNaN(scrollerCurrentPos)) {
                    scrollerCurrentPos = 0;
                }
                if (delta > 0) {
                    delta = 0 - delta;
                }
                else 
                    if (delta < 0) {
                        delta = Math.abs(delta);
                    }
                
                delta = delta * 3; /* make the scroll faster */
                
                if (scrollerCurrentPos <= 0 && delta <= 0) {
                    $(this).find('.scroller').css('top', 0);
                }
                else 
                    if (scrollerCurrentPos >= scrollDist && delta >= 0) {
                        $(this).find('.scroller').css('top', scrollDist);
                    }
                    else {
                    
                        $(this).find('.scroller').css('top', (scrollerCurrentPos + delta) + 'px');
                    }
                
                var scrollY = parseInt($(this).find('.scroller').css('top'));
                var docY = 0 - (scrollY * (docH - contH) / scrollDist)
                $('.dropclip ul').css('top', docY + "px");
                
                return false;
            });
            
            
        }
        else {
            hideDroplist(this);
        }
    });
    
    function showDroplist(el){
        $(el).parent().css('z-index', 200);
        $(el).parent().find('.dropclip').show();
        $(el).parent().find('.scroll-area').show();
        $(el).css('backgroundPosition', '-168px 100%');
    }
    
    function hideDroplist(el){
        $(el).parent().find('.dropclip').hide();
        $(el).parent().find('.scroll-area').hide()
        $(el).parent().css('z-index', '');
        $(el).css('backgroundPosition', '0 100%');
    }
    
    
    
    
    // HOT TOPICS
    
    // BEGIN safe to modify
    
    hotTopicInterval = 4800;
    
    // END safe to modify
    
    topicNumCache = 0;
    hotTopics = [];
    
    $('#hot-topics .button').text('More');
    $.get('http://' + location.host + location.pathname + 'includes/scripts/hot-topics-fetcher.php', function(data){
        $(data).find("item").each(function(){
            var topicItem = $('<a>' + $(this).find('title').text() + '</a>').css('opacity', 0);
            $(topicItem).attr('href', $(this).find('itemLink').text());
            hotTopics.push($(topicItem));
        });
        function hotTopicsSwitch(topicNum){
            if (topicNum >= hotTopics.length) {
                topicNumCache = 0;
            }
            var currentTopic = $('#hot-topics p a');
            $('#hot-topics p').prepend($(hotTopics).get(topicNumCache));
            $('#hot-topics .button').attr('href', $(hotTopics).get(topicNumCache).attr('href'));
            $('#hot-topics p').find('a').eq(0).animate({
                opacity: 1
            }, 720, 'linear', function(){
                $(hotTopics).get(topicNumCache).oneTime(hotTopicInterval, 'hotTopicsTimer', function(){
                    topicNumCache++;
                    hotTopicsSwitch(topicNumCache);
                });
            });
            $(currentTopic).animate({
                opacity: 0
            }, 720, 'linear', function(){
                $(currentTopic).remove();
            });
        }
        if ($('#hot-topics').length > 0) {
            hotTopicsSwitch(topicNumCache);
        }
        
        $('#hot-topics .next').disableTextSelect();
        $('#hot-topics .next').click(function(){
            $('#hot-topics p').find('a').each(function(){
                $(this).stop(true, false);
                $(this).stopTime();
            });
            $('#hot-topics p').find('a').css('opacity', 0).remove();
            topicNumCache++
            hotTopicsSwitch(topicNumCache);
        });
    });
    
    $('#hot-topics p').after('<div class="next pngfix">next</a>');
    
    $('#hot-topics .next').hover(function(){
        $(this).addClass('next-hover');
    }, function(){
        $(this).removeClass('next-hover');
    });
    
    
    
    
    // NICEFY BUTTONS 
    
    $('.button').each(function(){
        $(this).wrap('<span class="button-wrapper"></span>');
        if ($(this).hasClass('button-fixed')) {
            $(this).parent().css('width', $(this).css('width'));
            if ($(this).css('float') != 'none') {
                $(this).parent().css('float', $(this).css('float'));
            }
        }
    });
    
    $('.button').hover(function(){
        $(this).parent().attr('class', 'button-wrapper-hover');
        $(this).addClass('button-hover');
    }, function(){
        $(this).parent().attr('class', 'button-wrapper');
        $(this).removeClass('button-hover');
    });
    
    
    
    
    // NICEFY EMAIL SUBSCRIBE
    
    $('#subscribe-email').wrap('<span class="subscribe-email-wrap"></span>');
    var subscribeEmailText = 'Enter your email address';
    $('#subscribe-email').attr('value', subscribeEmailText);
    
    $('#subscribe-email').focus(function(){
        var handle = $(this).attr('value');
        if (handle == subscribeEmailText) {
            $(this).attr('value', '');
        }
    });
    $('#subscribe-email').blur(function(){
        var handle = $(this).attr('value');
        if (handle == '') {
            $(this).attr('value', subscribeEmailText);
        }
    });
    
    
    
    
    // NEWSLETTER SUBMITTED
    
    $('#newsletter-subscribe').submit(function(){
        var email = $('#subscribe-email').attr('value');
        if (email.match(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/)) {
        
            var ifr = $('<iframe name="target_frame"></iframe>').hide();
            $('body').prepend(ifr);
            
            $('#newsletter-subscribe').attr('target', 'target_frame');
            
            var overlay = $('<div id="newsletter-confirm"></div>').css({
                opacity: .75,
                height: $(document).height() + 'px'
            });
            $('body').prepend(overlay);
            
            var boxTop = $(window).scrollTop() + ($(window).height() / 2);
            
            var box = $('<div id="newsletter-confirm-box"><div class="text"><strong>Thank you for your subscription!</strong>Please check your email for the confirmation message</div><div class="close"><img src="includes/images/close_x.png" alt="Close" title="close" class="pngfix" /></div></div>').css('top', boxTop);
            
            $('body').prepend(box);
            $(box).oneTime(3000, 'confirmPause', function(){
                $('#newsletter-confirm-box').hide().remove();
                $('#newsletter-confirm').hide().remove();
            });
            
            $('#newsletter-confirm').click(function(){
                $('#newsletter-confirm-box').hide().remove();
                $('#newsletter-confirm').hide().remove();
            });
            $('#newsletter-confirm-box .close img').click(function(){
                $('#newsletter-confirm-box').hide().remove();
                $('#newsletter-confirm').hide().remove();
            });
            
            $('#mailError').slideUp('slow');
            return true;
        }
        else {
            var emailError = $('<span id="mailError" style="display: block; position: relative; left: -2px; padding: 3px 0 0 0; color: #ffff00;">please enter a valid email address</span>').hide();
            $('#mailError').slideUp('slow');
            $('#subscribe-email').after(emailError);
            $(emailError).slideDown('slow');
            
            return false;
        }
    });
    
    
    
    
    // FOOTER
    
    $("#footer").positionFooter(false);
    
    
    
    
    // ON WINDOW LOAD
    
    $(window).load(function(){
    
    
    
    
        // PRELOAD GALLERY IMAGES & RUN
        
        if ($('#main-slideshow').length > 0) {
            function preloadImages(n){
                if (n != slideshow.length) {
                    var image = new Image();
                    $(image).load(function(){
                        n++;
                        preloadImages(n);
                    });
                    image.src = slideshowImages[n];
                }
            }
            preloadImages(0);
            loadSlideshow(slideshowNumCache, autoSpeed);
        }
        
        
        
        
    });
});

