﻿// на document.ready инициализируем все слайдеры на странице
jQuery(function() {
    jQuery(".slider").each(function() { createSlider(this); });
});

// функция для инициализации слайдера
function createSlider(container) {
    // считываем параметры из hidden полей
    var width         = jQuery(container).find(".hidWidth").val();
    var padding       = jQuery(container).find(".hidPadding").val();
    var img_width     = jQuery(container).find(".hidImgWidth").val();
    var img_padding   = jQuery(container).find(".hidImgPadding").val();    
    var count_on_page = jQuery(container).find(".hidCountImgOnPage").val();
    var count_imgs    = jQuery(container).find(".hidCountImg").val();
    var imgs_folder   = jQuery(container).find(".hidImgFolder").val();
    
    // устанавливаем ширину слайдера и его padding
    jQuery(container).css("width", width).css("padding", padding+"px");
    // устанавливаем padding для каждого из изображений
    jQuery(container).find(".img").css("padding", img_padding + "px").css("left", "0px").css("top", "0px");
    // рассчитываем ширину отображаемой области слайдера
    var window_width = (img_width * 1 + img_padding * 2) * count_on_page;
    // окно в котором прокручивается контент
    var window = jQuery(container).find(".window");
    // область с контентом, которую крутим
    var content_area = jQuery(container).find(".imglist");
    // рассчитываем ширину всего содержимого
    var content_width = (img_width * 1 + img_padding * 2) * count_imgs;
    // если всё умещается на одну страницу
    if (count_imgs * 1 <= count_on_page * 1) {
        window.css("width", content_width + "px");
        return;
    }
    window.css("width", window_width + "px");
    // отображаем кнопку влево и вешаем обработчик
    jQuery(jQuery(container).find(".btn_right")).show().click(function() {
        content_area.stop(true, true);
        var left = parseInt(content_area.css("left"), 10);
        if (((left * -1) + window_width) < content_width)
            content_area.animate({ "left": "-=" + window_width + "" }, 'slow', function() {
                                                                                           //SetBtnImages(container, window_width, content_width, imgs_folder); 
                                                                                          });
                                                                                      });
    
    // устанавливаем обработчики на наведение и уход мыши с изображения кнопки
    jQuery(jQuery(container).find(".btn_left img")).attr("src", imgs_folder + "left.gif")
                    .mouseover(function() { jQuery(this).attr("src", imgs_folder + "left_dis.gif"); })
                    .mouseout(function() { jQuery(this).attr("src", imgs_folder + "left.gif"); });
                    
    // отображаем кнопку вправо и вешаем обработчик
    jQuery(jQuery(container).find(".btn_left")).show().click(function() {
        content_area.stop(true, true);
        var left = parseInt(content_area.css("left"), 10);
        if (left < 0)
            content_area.animate({ "left": "+=" + window_width + "" }, 'slow', function() {
                                                                                            //SetBtnImages(container, window_width, content_width, imgs_folder); 
                                                                                           });
                                                                                       });
    
    // устанавливаем обработчики на наведение и уход мыши с изображения кнопки
    jQuery(jQuery(container).find(".btn_right img")).attr("src", imgs_folder + "right.gif")
                    .mouseover(function() { jQuery(this).attr("src", imgs_folder + "right_dis.gif"); })
                    .mouseout(function() { jQuery(this).attr("src", imgs_folder + "right.gif"); });
    

    // оформляем кнопки
    //SetBtnImages(container, window_width, content_width, imgs_folder);
}

// устанавливаем нужные изображения для кнопок
function SetBtnImages(container, window_width, content_width, imgs_folder) {
    var current_left = parseInt(jQuery(container).find(".imglist").css("left"), 10);
    var btn_left = jQuery(container).find(".btn_left img");
    var btn_right = jQuery(container).find(".btn_right img");

    if (((current_left * -1) + window_width) < content_width)
        btn_right.attr("src",imgs_folder+"right.gif");
    else
        btn_right.attr("src", imgs_folder + "right_dis.gif");

    if (current_left < 0)
        btn_left.attr("src", imgs_folder + "left.gif");
    else
        btn_left.attr("src", imgs_folder + "left_dis.gif");
}

function GetSwfContentFromServer(filename, entity_type, entity_id, width, height) {
    //Shadowbox.open({"player":"swf","width": width,"height":height,"content": <%= "'{0}FileStorage/{1}/{2}/'".FormatWith(Atlantis.Core.WebSite.Url, EntityType, EntityId) %> + filename});
    jQuery.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: "{width: '" + width + "',height: '" + height + "',fileName: '" + filename + "',EntityType: '" + entity_type + "',EntityId: '" + entity_id + "'}",
        url: EntityHintsUrl + "/GetSwf",
        dataType: "json",
        success: function(data) {
        Shadowbox.open({ player: data.d.player, content: data.d.content, width: width, height: height, gallery: 'video' });
        }
        , error: function(e) {
            var t = e;
            //alert(e.responseText);
        }
    });
}
