﻿var rolikIndex = 0;
var commentIndex = (getCookie("commentIndex") == null ? 0 : getCookie("commentIndex"));
$(function()
{        
    cloud();
    $(".center-right-bot-rolik img").load(function()
    {
        scaleImage(282, 300, ".center-right-bot-rolik img");    
    });          
    rolikInit();   
    $(".form-button a").live("click", function()
    {        
        if($(".form-checkbox input").attr("checked"))
        {
            $("form").submit();    
        }            
    });    
    $(".comments-item-button a").live("click", function()
    {        
        getComment();                  
    });
    $(".portfolio-navi-item a").live("click", function()    
    {
        if(!$(this).parent().hasClass("active"))
        {
            var page;
            if($(this).parent().index(".portfolio-navi-item") == 0)
            {
                var active = $(this).parent().parent().find(".active a").html();
                page = parseInt(active) - 1;
                if(page == 0)
                {
                    return false;
                }                                
            }
            else if($(this).parent().index(".portfolio-navi-item") == $(".portfolio-navi-item").length - 1)
            {
                var active = $(this).parent().parent().find(".active a").html();
                page = parseInt(active) + 1;                
                if(page == $(".portfolio-navi-item").length - 1)
                {
                    return false;
                }                
            }
            else
            {
                page = $(this).html();
            }
            getPortfolioPage(page);
        }
    });
    $(".portfolio-item").live("mouseover", function()
    {
        $(".portfolio-item.active").removeClass("active");
        $(this).addClass("active");    
    });
    $(".portfolio-item").live("mouseout", function()
    {
        $(".portfolio-item.active").removeClass("active");        
    });
    $(".portfolio-item").live("click", function()
    {
        var id = $(this).find(".portfolio-item-active").attr("id");
        location.href = "portfolio-" + id + ".html";    
    });    
}); 
function scaleImage(normalWidth, normalHeight, image)
{    
    setTimeout(function()
    {        
        var width = $(image).width();
        var height = $(image).height();
        var temp;         
        var newHeight = Math.ceil(normalWidth * height / width);        
        $(image).css({"width": normalWidth + "px", "height": newHeight + "px"});
        //height(newHeight).width(normalWidth); 
        /*               
        if(width > height)
        {
            temp = width;
            width = normalWidth;
            height = height / (temp / width);
            if(height > normalHeight)
            {
                temp = height;
                height = normalHeight;
                width = width / (temp / height);                
            }
            $(image).height(height).width(width); 
        }
        else
        {
            temp = height;
            height = normalHeight;
            width = width / (temp / height);            
            if(width > normalWidth)
            {
                temp = width;
                width = normalWidth;
                height = height / (temp / width);                
            }      
            $(image).height(height).width(width);          
        }     */      
    },250);  
}
function preload(images) 
{ 
    if (typeof document.body == "undefined") return; 
    try { 
  
        var div = document.createElement("div"); 
        var s = div.style; 
            s.position = "absolute"; 
        s.top = s.left = 0; 
        s.visibility = "hidden"; 
        document.body.appendChild(div); 
        div.innerHTML = "<img width = '1' src = \"" + images.join("\" /><img width = '1' src = \"") + "\" />"; 
        var lastImg = div.lastChild; 
        lastImg.onload = function() { document.body.removeChild(document.body.lastChild); }; 
     } 
     catch(e) { 
        // Error. Do nothing. 
    } 
}
function request(parameters, callback)
{    
    $.get(                
        "../../ajax.php",
        parameters,
        function(data)
        {            
            callback(data);    
        }            
    );        
}
function rolikInit()
{
    $(".center-right-bot-rolik img:first, .rolik-img img:first").css("position", "absolute"); 
    var id = rolikInterval();
    $(".center-right-bot-button a").die().live("click", function()
    {
        if($(this).html() == "Остановить")
        {
            clearInterval(id);
            $(this).html("Возобновить");
        }
        else
        {
            id = rolikInterval();
            $(this).html("Остановить");
        }        
    });
    var ignoreLeft;
    var ignoreRight;
    $(".rolik-right a").die().live("click", function()
    {
        if(ignoreRight)
        {
            return false;
        }
        ignoreRight = true;
        rolikIndex++;
        if(rolikIndex == $(".big-image").length)
        {
            rolikIndex = 0;
        }
        preloadNear();
        var src = $(".big-image:nth(" + rolikIndex + ") img").attr("src");           
        $(".rolik-img img:nth(1)").attr("src", src);               
        $(".rolik-img img:nth(0)").fadeTo(1000, 0, function()
        {
            $(this).remove();
            $(".rolik-img img").css("position", "absolute")
                .after("<img/>");  
            ignoreRight = false;   
            var description = $(".description:nth(" + rolikIndex + ")").html();
            $(".rolik-text-text").html(description);  
            getPortfolioLink(description, ".rolik-link a");                     
        });
        scaleImage(810, 1000, ".rolik-img img:last"); 
    });
    $(".rolik-left a").die().live("click", function()
    {
        if(ignoreLeft)
        {
            return false;
        }        
        ignoreLeft = true;
        rolikIndex--;
        if(rolikIndex == -1 )
        {
            rolikIndex = $(".big-image").length - 1;
        }
        preloadNear(true);
        var src = $(".big-image:nth(" + rolikIndex + ") img").attr("src");         
        $(".rolik-img img:nth(1)").attr("src", src);               
        $(".rolik-img img:nth(0)").fadeTo(1000, 0, function()
        {
            $(this).remove();
            $(".rolik-img img:nth(0)").css("position", "absolute")
                .after("<img/>");       
            ignoreLeft = false;     
            var description = $(".description:nth(" + rolikIndex + ")").html();
            $(".rolik-text-text").html(description);
            getPortfolioLink(description, ".rolik-link a");              
        });
        scaleImage(810, 2000, ".rolik-img img:last"); 
    });    
    $(".rolik-back, .rolik-close").die().live("click", function()
    {
        $(".rolik-body").fadeTo(500, 0, function()
        {            
            $(".rolik-body, .rolik-back").css("display", "none");       
        });         
    });    
    $(".center-right-bot-rolik").die().live("click", function()
    {
        $(".center-right-bot-button a").html("Остановить").click();
        var src = $(".big-image:nth(" + rolikIndex + ") img").attr("src");          
        var description = $(".description:nth(" + rolikIndex + ")").html();         
        showViewer(src, description);
        getPortfolioLink(description, ".rolik-link a");            
    }); 
    $(".more-images-item a").live("click", function()
    {
        var src = $(this).attr("href");        
        showViewer(src, "Пример работы", true);    
    });   
}
function preloadNear(left)
{
    var index = rolikIndex;
    if(left)
    {
        index--;
        if(index == -1 )
        {
            index = $(".big-image").length - 1;
        }               
    }
    else
    {      
        index++;
        if(index == $(".big-image").length)
        {
            index = 0;
        }                
    }
    var src = $(".big-image:nth(" + index + ") img").attr("src");
    preload([src]);     
}
function rolikInterval()
{
    var id = setInterval(function()
    {        
        rolikIndex++;
        if(rolikIndex == $(".small-image").length)
        {
            rolikIndex = 0;
        }
        var src = $(".small-image:nth(" + rolikIndex + ") img").attr("src");
        var srcBig = $(".big-image:nth(" + rolikIndex + ") img").attr("src");  
        //preload([srcBig]);      
        $(".center-right-bot-rolik img:nth(1)").attr("src", src);               
        $(".center-right-bot-rolik img:nth(0)").fadeTo(1000, 0, function()
        {
            $(this).remove();
            $(".center-right-bot-rolik img").css("position", "absolute")
                .after("<img/>");            
        });
        scaleImage(282, 300, ".center-right-bot-rolik img:last");
    }, 5000);
    return id;
}
function getComment()
{
    commentIndex++;    
    request({"subject": "comment", "number": commentIndex}, function(data)
    {            
        var response = $.parseJSON(data);                   
        $(".comments-item-text-text").html('"' + response.TEXT.replace(/\[cp:replacement\]/ig, "/") + '"');
        $(".comments-item-desc").html(response.DESC.replace(/\[cp:replacement\]/ig, "/"));
        $(".comments-item-img img").attr("src", response.SRC.replace(/\[cp:replacement\]/ig, "/"));
        commentIndex = response.INDEX / 3 + 1;
        setCookie("commentIndex", commentIndex);
        scaleImage(62, 300, ".comments-item-img img");
    });     
}
function setCookie(name, value)
{
    $.cookie(name, value, { expires: 1});
}
function getCookie(name)
{
    return $.cookie(name); 
}
function getPortfolioPage(page)
{        
    $(".center-left-item:lt(" + ($(".center-left-item").length - 1) + ")").remove();
    $(".center-left-item:last").before("<div class='loading-container'></div>");
    loading(0, false, ".loading-container");
    request({"subject": "portfolio", "page": page}, function(data)
    {           
        $(".loading-container").remove();
        $(".center-left-item:last").before(data);
        $(".portfolio-navi-item.active").removeClass("active");
        $(".portfolio-navi-item:nth(" + page+ ")").addClass("active");
        scaleImage(107, 300, ".portfolio-item-img img");                       
    });     
}
function loading(index, stop, place)
{        
    if(place != undefined)
    {
        var loadingTemplate =   '<div class="center-left-item nobg">' +
                                    '<div class="center-left-item-header">' +
                                        '<div class="center-left-item-header-main"></div>' +                                        
                                    '</div>' +
                                    '<div class="center-left-item-body">' +
                                        '<div class="loading">' +
                                            '<div class="loading-item">' +
                                            '</div>' +
                                            '<div class="loading-item">' +
                                            '</div>' +
                                            '<div class="loading-item">' +
                                            '</div>' +
                                            '<div class="loading-item">' +
                                            '</div>' +
                                            '<div class="loading-item">' +
                                            '</div>' +                                                                                                                                                                               
                                        '</div>' +
                                    '</div>' +
                                    '<div class="clear"></div>' +                                     
                                '</div>';         
        $(place).html(loadingTemplate);
    }
    if(stop)
    {
        $(".loading").remove();
        return false;
    }    
    $(".loading-item:nth(" + index + ")").addClass("active").animate({height: "30px", marginTop: "0"},{duration: 500, complete: function()
    {
        $(".loading-item:nth(" + index + ")").removeClass("active").animate({height: "10px", marginTop: "10px"},{duration: 500});
        index++;
        if(index == $(".loading-item").length)
        {
            index = 0;
        }
        loading(index);                    
    }});
}
function getPortfolioLink(name, place)
{
    request({"subject": "portfolioLink", "name": name}, function(data)
    {           
        $(place).attr("href", data);                       
    });       
}
function cloud()
{    
    $(".cloud-item").each(function()
    {      
        var fontSize = parseInt(Math.random() * 6 + 9);
        fontSize = fontSize > 14 ? 14 : fontSize;        
        $(this).css("fontSize", fontSize + "px");                
    });
}
function showViewer(src, description, special)
{
    $(".rolik-body, .rolik-back").css("display", "block");
    $(".rolik-body").fadeTo(500, 1);  
    $(".rolik-img img").attr("src", src);      
    $(".rolik-text-text").html(description);        
    scaleImage(810, 2000, ".rolik-img img"); 
    if(hide)
    {
        $(".rolik-left a, .rolik-right a, .rolik-link a").fadeTo(0, 0);       
    }   
    else
    {
        $(".rolik-left a, .rolik-right a, .rolik-link a").fadeTo(0, 1);
    }  
}
