﻿/// <reference path="jquery-1.2.6-vsdoc.js" />
/// <reference path="jqDnR.js" />

$(function() {
    //$('.popinfo').jqDrag();
    $('#graphThumb, #closeButton').bind('click', function(event) {
        $.togglePopup('#servicesust_popup3');
    });

    $('#close_news').bind('click', function(event) {
        $.togglePopup('#news_popup');
        News.start();
    });

    ServicesMenu.init();
    HeaderBanner.init();
    News.init();

    $("html").removeClass("loading");
});

function contentCallback(editor_id, body, doc) {

    $('#' + editor_id + '_tbl').removeClass('displayNone');
}

var ServicesMenu = function() {
    return {
        init: function() {
            var currentNav;
            var currentClass;

            $(".nav_menu li h3 a").click(function() {
                var parentEl = $(this).parents("li");
                if (parentEl.hasClass("solo") || parentEl.hasClass("active")) return;
                ServicesMenu.toggleSection(this);
            });
            
            

            /*.each(function() {
            if ($(this).parents("li").hasClass("solo")) return;
            ServicesMenu.toggleSection(this, true);
            });*/

            currentClass = $("body").attr("class");
            currentNav = $(".nav_menu ." + (currentClass === "" ? "home" : currentClass));
            
            if (currentNav.length == 1) {
                currentNav.parents("li").addClass("active");
            }
        },

        toggleSection: function(link, noAnim) {
            var elParent = $(link).parents("li");
            var elChild = elParent.children("ul");

            if (elParent.hasClass("closed")) {
                noAnim ? elChild.show() : elChild.slideDown("fast");
                elParent.removeClass("closed").addClass("open");
            }
            else {
                noAnim ? elChild.hide() : elChild.slideUp("fast");
                elParent.removeClass("open").addClass("closed");
            }
        }
    }
} ();

var News = function() {
    var _arArticles = [];
    var _summaryItem = null;
    var _summaryTemplate = "<a href=\"javascript:News.showArticle({num})\">{title}</a>";
    var _position = 0;
    var _total = 0;
    var _paused = false;
    
    var createTicker = function() {
        $(".header_content").append("<div class=\"news_ticker\"><h2>4sl Group news:</h2><p>"
            + _summaryTemplate.replace("{num}", "0").replace("{title}", _arArticles[0].title)
            + "</p></div>");
        
        _summaryItem = $(".news_ticker p");
        _summaryItem.hover(
            function() {News.stop();},
            function() {if (!_paused) News.start();}
        );
    }

    return {
        ival: null,
        
        init: function() {
            $("#news .article").each(function(index) {
                var article = {
                    num: index,
                    title: $("h2", this).html(),
                    body: $(".body", this).html()
                }
                
                _arArticles.push(article);
                
                $(this).remove();
            });
            
            
            
            if (_arArticles.length == 0) return;
            
            _total = _arArticles.length;
            
            createTicker();
            
            News.start();
        },
        
        showArticle: function(num) {
            $("#popNews > h2").html(_arArticles[num].title);
            $("#popNews > .popscrollarea").html(_arArticles[num].body);
            $.togglePopup("#news_popup");
            News.stop();
            _paused = true;
        },
        
        start: function() {
            _paused = false;
            News.stop();
            News.ival = setInterval("News.next()", 4000);
        },
        
        stop: function() {
            clearInterval(News.ival);
        },
        
        next: function() {
			News.move(1);
		},

		prev: function() {
			News.move(-1);
		},

		move: function(num) {
			_position += num;

			if (_position < 0) _position = _total - 1;
			if (_position > _total - 1) _position = 0;
			
			_summaryItem.fadeOut(function() {
			    _summaryItem.html(_summaryTemplate.replace("{num}", _position).replace("{title}", _arArticles[_position].title));
                _summaryItem.fadeIn();
            });
		}
    }
}();

var HeaderBanner = function() {
    var _public = null;
    var _img = null;
    var _imgNext = null;
    var _position = 0;
    var _total = 0;
    
    var createBanner = function() {
        if ((_public.images == null) || (_public.images.length == 0)) return;
        
        $(".header_content").append("<div class=\"banner\">"
            + "<img class=\"photo-next\" src=\"{src}\" alt=\"\"/>".replace("{src}", _public.images[0])
            + "<img class=\"photo\" src=\"{src}\" alt=\"\"/>".replace("{src}", _public.images[0])
            + "<div class=\"overlay\"></div></div>"
        );
        
        _img = $(".header_content .banner .photo");
        _imgNext = $(".header_content .banner .photo-next");
    }
    
    return {
        ival: null,
        images: [],
        
        init: function() {
            _public = this;
            _total = this.images.length;
            
            createBanner();
            this.start();
        },
        
        start: function() {
            _paused = false;
            _public.stop();
            _public.ival = setInterval(_public.next, 4000);
        },
        
        stop: function() {
            clearInterval(News.ival);
        },
        
        next: function() {
            _public.move(1);
        },

        prev: function() {
            _public.move(-1);
        },

        move: function(num) {
            _position += num;

            if (_position < 0) _position = _total - 1;
            if (_position > _total - 1) _position = 0;

            _imgNext.attr("src", _public.images[_position]);
            
            _img.fadeOut(function() {
                _img.attr("src", _public.images[_position]);
                _img.show();
            });
        }
    }
}();
