﻿/// <reference path="jquery/jquery-1.4.1-vsdoc.js" />
var headimages;
$(function() {
    var partners = $('div.partnersGalleryBody ul');
    if (partners.length) {
        var left = $('a.scrollLeft');
        var right = $('a.scrollRight');
        var li = $('li', partners);
        var count = li.length - 4;
        var i = 0;
        var shift = function(to) {
            if (to < 0 || to > count) return;
            i = to;
            if (i == 0) left.addClass("no-left"); else left.removeClass("no-left");
            if (i == count) right.addClass("no-right"); else right.removeClass("no-right");
            partners.animate({ 'margin-left': -155 * i }, 'slow');
        };
        left.click(function() { shift(i - 1); });
        right.click(function() { shift(i + 1); });
        shift(0);
    }
    var carousels = $('.gallery_carousel');
    if (carousels.length)
        carousels.replaceWith(function() {
            var self = $(this);
            var images = $('img', self);
            var carousel = $(
                '<div><div class="carousel"><div class="carouselL"><a href="javascript:void(0);" class="scrollLeft"></a></div><div class="carouselC"><ul>' +
                '</ul></div><div class="carouselR"><a href="javascript:void(0);" class="scrollRight"></a></div></div></div>'
            );
            var ul = $('ul', carousel);
            images.each(function() {
                ul.append('<li><div class="carouselLeftcorner"><img src="im/PhotoHover-left.png"></div><div class="carouselRightcorner"><img src="im/PhotoHover-right.png"></div>' +
                    '<div class="carouselItem"><a class="lightbox" href="' + this.src + '"><img width="267" height="178" src="' + (this.src.replace('/ArticleImages_', '/thumb_ArticleImages_')) + '"/></a></div></li>');
            });
            var left = $('div.carouselL a', carousel);
            var right = $('div.carouselR a', carousel);
            var count = $('li', carousel).length - 3;
            var i = 0;
            var shift = function(to) {
                if (to < 0 || to > count) return;
                i = to;
                if (i == 0) left.addClass("no-left"); else left.removeClass("no-left");
                if (i == count) right.addClass("no-right"); else right.removeClass("no-right");
                ul.animate({ 'margin-left': -281 * i }, 'slow');
            };
            left.click(function() { shift(i - 1); });
            right.click(function() { shift(i + 1); });
            shift(0);
            carousel.append('<div class="signBlock"><div class="poinTop"/>' + self.text() + '</div>');
            return carousel;
        });
    $('a.lightbox').lightBox({
        imageLoading: 'scripts/jquery.lightbox/lightbox-ico-loading.gif',
        imageBtnPrev: 'scripts/jquery.lightbox/lightbox-btn-prev.gif',
        imageBtnNext: 'scripts/jquery.lightbox/lightbox-btn-next.gif',
        imageBtnClose: 'scripts/jquery.lightbox/lightbox-btn-close.gif',
        imageBlank: 'scripts/jquery.lightbox/lightbox-blank.gif',
        txtImage: '',
        txtOf: '/'
    });
    $('div.titleddescription').each(function() {
        var description = $('>.description', this);
        $('>.title', this).click(function() { description.toggle('slow'); });
    });
    $('#top-menu li:has(ul)').addClass('has-submenu');
    $('#top-menu li').hover(function() {
        $('>ul', this).stop(true).css('width', '').css('height', '').css('opacity', '').slideDown(200, function() { $(this).css('overflow', 'visible') });
    }, function() {
        $('>ul', this).stop(true).css('width', '').css('height', '').css('opacity', '').slideUp(200);
    });
    if (headimages) {
        setTimeout(function() {
            var banner = $('#bannerPlace');
            var imgs = "";
            for (var i in headimages)
                imgs += "<img src='" + headimages[i] + "' alt='' style='position:absolute;visibility:hidden;z-index:" + (i - 19) + ";left:0;top:0;'/>";
            imgs = $(imgs);
            imgs.css('opacity', 0).css('visibility', 'visible');
            banner.append(imgs);
            banner.ready(function() {
                $('#bannerPlace img').rotate(5000);
            });
        }, 1000);
    }
});
$.fn.rotate = function(interval) {
    var images = this;
    images.filter('not(:first)').css('opacity', 0);
    var rotateTo = function(i) {
        var _Op;
        var _I;
        if (i > 0) {
            _Op = 1;
            _I = i;
        } else {
            _Op = 0;
            _I = images.length - 1;
        }
        $(images[_I]).animate({ 'opacity': _Op }, 'slow', 'linear', function() {
            if (i > 1)
                $(images[(i + images.length - 1) % images.length]).css('opacity', 0);
            setTimeout(function() {
                rotateTo((i + 1) % images.length);
            }, interval);
        });
    };
    setTimeout(function() { rotateTo(1); }, interval);

};
