// ???????? ???????? ???????? ??? ?????????????? ????? ??????
var preserveOpenPanels = function(type){
    type = (type != undefined) ? ('.' + type) : '';
    $(type + ' div.project_table_short a').slice(0, 3).each(function(){
        var pane_id = $(this).parents('.project_table_short').attr('id');
        var flag = false;
        for (var i = 0; i < 3; i++) {
            flag = (FIFO[i] + '_short' == pane_id) || flag;
        }
        if (!flag) {
            $(this).click();
        }
    });
}


// ?????????? ????????? ?? ????

function applyFilter(type){
    $('span.portfolio_menu_photo').attr('class', 'portfolio_menu_photo filter_link');
    $('#menu_' + type).attr('class', 'portfolio_menu_photo filter_link_active');
    
    if (type == 'all') {
        $('div.portfolio_photo').show();
        
        // $('div.project_table_short a').slice(0,3).click();
        preserveOpenPanels();
    }
    else {
        $('div.portfolio_photo').hide();
        
        $('div.' + type).show();
        //		$('div.'+type+' div.project_table_short a').slice(0,3).click();
        
        preserveOpenPanels(type);
    }
}

$(window).load(function(){ //trying window.onload to prevent ie6 buggy page loading

    // ???????? ? ??????????
    
    FIFO = new Array();
    var albumPath;
    var albumNum;
    var albumCount;
    
    // FIFO id-independent
    //FIFO.push('portfolio_0');
    //FIFO.push('portfolio_1');
    //FIFO.push('portfolio_2');
    
    //???????? ?????? ? ?????? ?????????
    function expand(in_id){
        $('#' + in_id + '_short').slideToggle("slow");
        $('#' + in_id + '_long').slideToggle("slow");
        FIFO.push(in_id);
        if (FIFO.length > 3) {
            var out_id = FIFO.shift();
            $('#' + out_id + '_short').slideToggle("slow");
            $('#' + out_id + '_long').slideToggle("slow");
        }
        //?????????? ????????
        //???? ? ??? ????? ? ???? ???????? ? ???????? 1?1 ? ???????? ???????
        var toLoad = $('#' + in_id + '_long input').attr('value');
        //??????? ????? ????????: ???? ?? ???????? ?? ?? ????? ?????????????? ???????? ?
        //???????? ?????????? onload, ??????? ????? ?????? src ? ?????? ??????? ? ???????? ?
        $('<img>').load(function(){
            $('#' + in_id + '_long .small_image_to_load').attr('class', 'big_photo_link')            //????????????? ?????? ?? ?????? ?????? ?????
            //.attr('width','319')
            //.attr('height','160')
            //??? ?????? ???????
            .css('display', 'none').attr('src', '/theme/img/portfolio/' + toLoad).insertAfter('#' + in_id + '_long .small_image_to_load').ifixpng()            //? ????? ??????? ???????
            .fadeIn('slow')            //???? ?? ???? ?? ?????? ???????, ?? ??????
            //???? ??????, ?? ????????? ?? (??? ?????????? onclick ???????? ????? tpl)
            .click(function(){
                if (!this.onclick) {
                    showBigPhoto(this.src, this)
                }
            });
            //					$('img.small_image_to_load').ifixpng();
        
        
        }).attr('src', '/theme/img/portfolio/' + toLoad);
    }
    
    function showPhotoDiv(){
    
        document.getElementById('mask').style.display = "block";
        document.getElementById('big_photo').style.display = "block";
        
        // get scroll height and window width - thanks to rhinoceront
        if (window.innerWidth) { // all but IE
            var verticalScroll = window.pageYOffset;
            var horizontalWidth = window.innerWidth;
        }
        else 
            if (document.documentElement && document.documentElement.clientWidth) { // IE 6 & with doctype
                var verticalScroll = document.documentElement.scrollTop;
                var horizontalWidth = document.documentElement.clientWidth;
            }
            else 
                if (document.body.clientWidth) { // IE 4+ || no doctype
                    var verticalScroll = document.body.scrollTop;
                    var horizontalWidth = document.body.clientWidth;
                }
        
        var neededLeftMargin = (horizontalWidth / 2) - ($('#big_photo').width() / 2);
        
        $('#big_photo').css('top', verticalScroll).css('left', neededLeftMargin);
        
        // ie healers
        var bName = navigator.appName;
        if (bName == "Microsoft Internet Explorer") {
            document.getElementById('mask').style.position = "absolute";
            document.getElementById('mask').style.height = document.body.clientHeight + document.body.scrollTop;
            if ($.browser.version == '6.0') {
                $('#mask').css('width', '104%');
            }
        }
    }
    
    function correctBigPhoto(){
    
        if (bName == "Netscape") {
            document.getElementById('big_photo').style.left = eval((window.innerWidth / 2) - (document.getElementById('big_photo').offsetWidth / 2)) + "px";
            
        }
        if (bName == "Opera") {
            document.getElementById('big_photo').style.left = eval((window.innerWidth / 2) - (document.getElementById('big_photo').offsetWidth / 2)) + "px";
        }
        if (bName == "Microsoft Internet Explorer") {
            document.getElementById('big_photo').style.left = eval((document.body.clientWidth / 2) - (document.getElementById('big_photo').offsetWidth / 2)) + "px";
        }
    }
    
    function showBigPhoto(path, element){
        //prevent from 'album' behavior
        document.getElementById('photo').onclick = function(){};
        
        var id = $(element).parents('.project_table').attr('id')
        id = id.replace('_long', '_short');
        document.getElementById('bigphotolink').innerHTML = $('#' + id).find('a').text();
        
        var new_path = path.slice(0, -4) + "_big.jpg";
        document.getElementById('photo').src = new_path;
        showPhotoDiv();
    }
    
    
    window.showAlbum = function(path, count){
        document.getElementById('photo').src = path + "/1.jpg";
        document.getElementById('photo').style.cursor = "pointer";
        document.getElementById('photo').onclick = albumNext;
        document.getElementById('photo').alt = "     ";
        document.getElementById('photo').title = "     ";
        albumPath = path;
        albumNum = 1;
        albumCount = count;
        showPhotoDiv();
        var str = '  ' + albumNum + "  " + albumCount;
        document.getElementById('bigphotolink').innerHTML = str;
        
        
    }
    
    window.albumNext = function(){
    
        albumNum = albumNum + 1;
        if (albumNum > albumCount) {
            document.getElementById('photo').src = albumPath + "/1.jpg";
            albumNum = 1;
        }
        else {
            document.getElementById('photo').src = albumPath + "/" + albumNum + ".jpg";
        }
        var str = ' ' + albumNum + "  " + albumCount;
        document.getElementById('bigphotolink').innerHTML = str;
        
    }
    
    window.albumPrev = function(){
    
        albumNum = albumNum - 1;
        if (albumNum < 1) {
            document.getElementById('photo').src = albumPath + "/" + albumCount + ".jpg";
            albumNum = albumCount;
        }
        else {
            document.getElementById('photo').src = albumPath + "/" + albumNum + ".jpg";
        }
        
    }
    
    function hideBigPhoto(path){
    
        document.getElementById('mask').style.display = 'none';
        document.getElementById('big_photo').style.display = 'none';
        document.getElementById('photo').alt = "";
        document.getElementById('photo').title = "";
        document.getElementById('photo').src = "";
        //document.getElementById('photo').onclick = void(0);
        document.getElementById('photo').style.cursor = "default";
    }
    
    $('a.portfolio_link').click(function(){
        expand(this.id)
    })
    $('#photo_link').click(function(){
        showPhoto()
    })
    $('#fir_link').click(function(){
        showFir()
    })
    $('#sites_link').click(function(){
        showSites()
    })
    $('#project_link').click(function(){
        showProject()
    })
    
    $('img.big_photo_link').click(function(){
        showBigPhoto(this.src, this)
    });
    
    $('img.big_fir_link').click(function(){
        var id = $(this).parents('.project_table').attr('id');
        id = id.replace('_long', '_short');
        document.getElementById('bigphotolink').innerHTML = $('#' + id).find('a').html();
        showBigPhoto(this.src, this)
    });
    
    $('a.hide_big_link').click(function(){
        hideBigPhoto()
    })
    $('#mask').click(function(){
        hideBigPhoto()
    })
    
    $('img[@src$=.png]').ifixpng();
   
secion_name= $('#h_portfolio_id').html();
cur_section=(secion_name=='')?'site':secion_name;
   applyFilter(cur_section);


    
});
