﻿///<reference path="jquery-1.4.1-vsdoc.js"/>

function enableLightbox(id) {

    if(id.indexOf("#") > -1){
        id = id.substring(id.indexOf("#") + 1);
    }
    var lightbox = $("#"+ id);
    Cufon.replace($(":header[class!='noCufon']", lightbox));
    lightbox.centerScreen(true).show(200, function () {
        $("#curtains").css("height", getDocHeight()).show().click(function () { killLightbox(id); });
    });
}

function killLightbox(id) {
    $("#curtains").hide();
    $("#" + id).hide()
}

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

jQuery.fn.centerScreen = function (onlyX, loaded) {
    var obj = this;
    if (!loaded) {
        obj.css('top', 60 + $(window).scrollTop());
        obj.css('left', $(window).width() / 2 - this.width() / 2);

        $(window).bind("resize scroll", function () {
            obj.centerScreen(onlyX, !loaded);
        })
    } else {
        obj.stop();
        if (!onlyX) {
            obj.animate({
                top: 60 + $(window).scrollTop(),
                left: $(window).width() / 2 - this.width() / 2
            },
			200, 'linear');
        } else {
            obj.animate({ left: $(window).width() / 2 - this.width() / 2 },
			200, 'linear');
        }
    }

    return obj;
}

var CaseMedia = function () {
    var _this = {
        carouselContainer: null,
        casesContainer: null,
        activeIndex: 0,
        scrollSpeed: 'slow',
        itemCount: 0,
        slideShowDelay: 10000,
        itemWidth: null,
        init: function () {
            _this.carouselContainer = $("#casePage #mainMedia");
            _this.casesContainer = $(".list", _this.carouselContainer);

            var items = $(".item", _this.casesContainer);
            if (items.length <= 1) {
                return; // no need to do anything
            }
            _this.itemCount = items.length;
            _this.itemWidth = items.find(":first").width();

            _this.navigationHandler();
        },
        navigationHandler: function () {
            var navigationItems = $("#casePage .navigation").find(".prev, .next");

            navigationItems.click(function () {
                if ($(this).is(".prev")) { // clicked previous
                    _this.navigatePrev();
                } else if ($(this).is(".next")) { // clicked next
                    _this.navigateNext();
                }
                return false;
            });
        },
        navigatePrev: function () {
            if (_this.activeIndex > 0) {
                _this.navigate(_this.activeIndex - 1);
            } else {
                _this.navigate(_this.itemCount - 1);
            }

            return true;
        },
        navigateNext: function () {
            if (_this.activeIndex < _this.itemCount - 1) {
                _this.navigate(_this.activeIndex + 1);
            } else {
                _this.navigate(0);
            }

            return true;
        },
        navigate: function (newIndex, direction) {
            var currentItem = $(".item.active", _this.casesContainer);
            var newItem = $(".item:eq(" + (newIndex) + ")", _this.casesContainer);

            var currentX = _this.activeIndex * _this.itemWidth;
            var destX = newIndex * _this.itemWidth;

            var properties = {};
            properties.left = -1 * destX;

            _this.casesContainer.animate(properties, _this.scrollSpeed, "swing", function () {
                currentItem.removeClass("active");
                newItem.addClass("active");
                var currentFlash = newItem.find(".flash");
                if (currentFlash.length) {
                    var data = currentFlash.metadata();
                    var html = $.flash.create({
                        swf: data.swf,
                        width: 940,
                        height: 366
                    });
                    currentFlash.replaceWith(html);

                }
            });

            _this.activeIndex = newIndex;

        }
    };

    _this.init();
};
