var minFrameW = 200;
var focusedW = 0;
var currW = 0;
var state = 'DEFAULT';
var worksImgs = [];
var officeImgs = [];
var newsImgs = [];
var t;
var timer_is_on=0;
var isLoading=false;

$(document).ready(function() {	
    setGlobalVars();
    $(window).resize(onResize);
    $("#worksFrame").css({
        left: '0px',
        width:(currW*0.33)+'px'
    });
    $("#officeFrame").css({
        left: (currW*0.33)+'px',
        width:(currW*0.34)+'px'
        });
    $("#newsFrame").css({
        left: (currW*0.67)+'px',
        width:(currW*0.33)+'px'
        });
    $(".frameContent").css({
        height: ($(window).height()-195)+'px',
        'overflow-y':'hidden'
    });
    $("#worksFrame .frameContent").css({
        height: ($(window).height()-198)+'px',
        'overflow-y':'hidden'
    });
    
    $('#worksLogo').click(function() {
        $('#worksByYearSelectForm option:nth-child(1)').attr('selected', 'selected');
        $('#worksByCategorySelectForm option:nth-child(1)').attr('selected', 'selected');
        $('#worksFrame .big-image').each(function(){ if($(this).is(':visible')){ swapImage(this); } });
        changeWorksSelects();
    });
    
    $('#officeLogo').click(function() {
        $('#officeMainCategorySelectForm option:nth-child(2)').attr('selected', 'selected');
        $('#officeFrame .big-image').each(function(){ if($(this).is(':visible')){ swapImage(this); } });
        showOfficeData();
    });
    
    $('#newsLogo').click(function() {
        $('#newsFrame .fulltext').hide();
        $('#newsFrame .smalltext').show();
        $('#newsFrame .big-image').each(function(){ if($(this).is(':visible')){ swapImage(this); } });
        $('#newsFrameContent').animate({scrollTop : 0},400);
    });
});

window.onload = initialAnimation; 

/*-----MOVE ACTIONS-----*/
function initialAnimation(){
    prepareImageArray();
    onResize();
    $(".imageMask").each(function(){
        var maskedImg =  $(this).parent('.imageWallOver').next('.imageWall').find('img.little-image').get($(this).index());
        $(this).css({
            width:$(maskedImg).width()+'px',
            height:$(maskedImg).height()+'px'
            });
    });
    $(".imageWall").each(function(){
        $(this).parent().css({
            height:$(this).height()+'px'
            });
    });
   addListeners();     //commented this line
    $("#overwhite").remove();
}

function prepareImageArray() {
    pushWorkImages();
    pushOfficeImages();
    pushNewsImages();
}

function pushWorkImages() {
    worksImgs = [];
    $("#worksContent").find("img.little-image").each(function(){
        addImageActions(this);
        worksImgs.push([$(this), $(this).width(), $(this).height()]);
    });
    $("#worksContent img").mousemove(function(e){
        moveTooltip(e.pageX, e.pageY);
    });
        
}

function pushOfficeImages() {	
    officeImgs = [];
    $("#officeDynamicCont").find("img.little-image").each(function(){
        addImageActions(this);
        officeImgs.push([$(this), $(this).width(), $(this).height()]);
    });
    $("#officeDynamicCont img").mousemove(function(e){
        moveTooltip(e.pageX, e.pageY);
    });
}

function pushNewsImages() {
    newsImgs = [];
    $("#newsContent").find("img.little-image").each(function(){
        addImageActions(this);
        newsImgs.push([$(this), $(this).width(), $(this).height()]);
    });
    $("#newsContent img").mousemove(function(e){
        moveTooltip(e.pageX, e.pageY);
    });
}


function addImageActions(el){
    if($(el).attr('alt').length){
        $(el).mouseover(function(){
            $('#tooltip').html($(el).attr('alt'));
            $('#tooltip').show();
        }).mouseout(function(){
            $('#tooltip').html('');
            $('#tooltip').hide();
        });
    }

    $('#newsLogo').mouseover(function(){
        $('#tooltip').html('RESET NEWS');
        $('#tooltip').show();
    }).mouseout(function(){
        $('#tooltip').html('');
        $('#tooltip').hide();
    }).mousemove(function(e){
        moveTooltip(e.pageX, e.pageY);
    });
    $('#worksLogo').mouseover(function(){
        $('#tooltip').html('RESET WORKS');
        $('#tooltip').show();
    }).mouseout(function(){
        $('#tooltip').html('');
        $('#tooltip').hide();
    }).mousemove(function(e){
        moveTooltip(e.pageX, e.pageY);
    });
    $('#officeLogo').mouseover(function(){
        $('#tooltip').html('RESET OFFICE');
        $('#tooltip').show();
    }).mouseout(function(){
        $('#tooltip').html('');
        $('#tooltip').hide();
    }).mousemove(function(e){
        moveTooltip(e.pageX, e.pageY);
    });
}

function addListeners(){
    $("#worksFrame").click(onWorksClick).mouseover(onWorksOver).mouseout(onWorksOut);
    $("#officeFrame").click(onOfficeClick).mouseover(onOfficeOver).mouseout(onOfficeOut);
    $("#newsFrame").click(onNewsClick).mouseover(onNewsOver).mouseout(onNewsOut);
    setDropDownActions();
    //$("#flashcontent").remove(); //added this line
    showAllImages();
    
}

function showAllImages(){
    if (timer_is_on){
        clearTimeout(t);
    }
    timer_is_on=1;
    t = setTimeout("showAllImagesDelayed()",500); 
}

function showAllImagesDelayed(){
    timer_is_on=0;
    $(".imageWallOver").each(function(){
        $(this).fadeOut('slow', function() {
            $(this).next('.imageWall').css({
                position:'static'
            });
            $(this).parent().css({
                height:'auto'
            });
            $(this).remove();
        });
    });
}

function moveTooltip(X, Y){ 
    if(!$('#tooltip').html().length){
        $('#tooltip').hide();
    }
    $('#tooltip').css({
        "left":(X+15)+"px",
        "top":(Y+7)+"px"
        });
}

function onWorksOver(){
    if(state!='WORK'){
        $("#worksMaskOver").show();
        $("#worksLogoOver").show();
        $("#worksLogo").hide();
    }
}
function onWorksOut(){
    if(!isLoading){
        $("#worksMaskOver").hide();
        $("#worksLogoOver").hide();
        $("#worksLogo").show();
    }
}
function onWorksClick(){
    if(state!='WORK' && !timer_is_on){
        $(".frameContent").css({
            'overflow-y':'hidden'
        });
        $("#worksFrame .frameContent").css({
            'overflow-y':'auto'
        });
        state = 'WORK';
        onWorksOut();
//        $("#newsFrame img.big-image").hide();
//        $("#newsFrame img.little-image").css("margin-top", "0");
//        $("#officeFrame img.big-image").hide();
//        $("#officeFrame img.little-image").css("margin-top", "0");
        $('#officeFrame .big-image').each(function(){ if($(this).is(':visible')){ swapImage(this); } });
        $('#newsFrame .big-image').each(function(){ if($(this).is(':visible')){ swapImage(this); } });
        $("#worksFrame").animate({
            width: focusedW+'px',
            left: '0px'
        }, 500);
        $("#officeFrame").animate({
            width: minFrameW+'px',
            left: focusedW+'px'
            }, 500);
        $("#newsFrame").animate({
            width: minFrameW+'px',
            left: (currW-minFrameW)+'px'
            }, 500, function(){
            playFullWork();
        });
        animateImages(focusedW-25, minFrameW-25, minFrameW-25);
    }
}

function onOfficeOver(){
    if(state!='OFFICE'){
        $("#officeMaskOver").show();
        $("#officeLogoOver").show();
        $("#officeLogo").hide();
    }
}
function onOfficeOut(){	
    if(!isLoading){
        $("#officeMaskOver").hide();
        $("#officeLogoOver").hide();
        $("#officeLogo").show();
    }
}
function onOfficeClick(){	
    if(state!='OFFICE' && !timer_is_on){
        $(".frameContent").css({
            'overflow-y':'hidden'
        });
        $("#officeFrame .frameContent").css({
            'overflow-y':'auto'
        });
        state = 'OFFICE';
        onOfficeOut();
//        $("#worksFrame img.big-image").hide();
//        $("#worksFrame img.little-image").css("margin-top", "0");
//        $("#newsFrame img.big-image").hide();
//        $("#newsFrame img.little-image").css("margin-top", "0");
        $('#worksFrame .big-image').each(function(){ if($(this).is(':visible')){ swapImage(this); } });
        $('#newsFrame .big-image').each(function(){ if($(this).is(':visible')){ swapImage(this); } });
        $("#worksFrame").animate({
            width: minFrameW+'px',
            left: '0px'
        }, 500);
        $("#officeFrame").animate({
            width: focusedW+'px',
            left: minFrameW+'px'
            }, 500);
        $("#newsFrame").animate({
            width: minFrameW+'px',
            left: (currW-minFrameW)+'px'
            }, 500, function(){
            playFullOffice();
        });
        animateImages(minFrameW-25, focusedW-25, minFrameW-25);
    }
}

function onNewsOver(){
    if(state!='NEWS'){
        $("#newsMaskOver").show();
        $("#newsLogoOver").show();
        $("#newsLogo").hide();
    }
}
function onNewsOut(){
    $("#newsMaskOver").hide();
    $("#newsLogoOver").hide();
    $("#newsLogo").show();
}
function onNewsClick(){
    if(state!='NEWS' && !timer_is_on){
        $(".frameContent").css({
            'overflow-y':'hidden'
        });
        $("#newsFrame .frameContent").css({
            'overflow-y':'auto'
        });
        state = 'NEWS';
        onNewsOut();
//        $("#worksFrame img.big-image").hide();
//        $("#worksFrame img.little-image").css("margin-top", "0");
//        $("#officeFrame img.big-image").hide();
//        $("#officeFrame img.little-image").css("margin-top", "0");
        $('#worksFrame .big-image').each(function(){ if($(this).is(':visible')){ swapImage(this); } });
        $('#officeFrame .big-image').each(function(){ if($(this).is(':visible')){ swapImage(this); } });
        $("#worksFrame").animate({
            width: minFrameW+'px',
            left: '0px'
        }, 500);
        $("#officeFrame").animate({
            width: minFrameW+'px',
            left: minFrameW+'px'
            }, 500);
        $("#newsFrame").animate({
            width: focusedW+'px',
            left: (minFrameW*2)+'px'
            }, 500, function(){
            playFullNews();
        });
        animateImages(minFrameW-25, minFrameW-25, focusedW-25);
    }
}

function onResize(){		
    setGlobalVars();
    switch(state) {
        case 'WORK':
            $("#worksFrame").css({
                width: focusedW+'px',
                left: '0px'
            });
            $("#officeFrame").css({
                width: minFrameW+'px',
                left: focusedW+'px'
                });
            $("#newsFrame").css({
                width: minFrameW+'px',
                left: (currW-minFrameW)+'px'
                });
            resizeImages(focusedW-25, minFrameW-25, minFrameW-25);
            break;
        case 'OFFICE':
            $("#worksFrame").css({
                width: minFrameW+'px',
                left: '0px'
            });
            $("#officeFrame").css({
                width: focusedW+'px',
                left: minFrameW+'px'
                });
            $("#newsFrame").css({
                width: minFrameW+'px',
                left: (currW-minFrameW)+'px'
                });
            resizeImages(minFrameW-25, focusedW-25, minFrameW-25);
            break;
        case 'NEWS':
            $("#worksFrame").css({
                width: minFrameW+'px',
                left: '0px'
            });
            $("#officeFrame").css({
                width: minFrameW+'px',
                left: minFrameW+'px'
                });
            $("#newsFrame").css({
                width: focusedW+'px',
                left: (minFrameW*2)+'px'
                });
            resizeImages(minFrameW-25, minFrameW-25, focusedW-25);
            break;
        case 'DEFAULT':
            $("#worksFrame").css({
                left: '0px',
                width:(currW*0.33)+'px'
            });
            $("#officeFrame").css({
                left: (currW*0.33)+'px',
                width:(currW*0.34)+'px'
                });
            $("#newsFrame").css({
                left: (currW*0.67)+'px',
                width:(currW*0.33)+'px'
                });
            resizeImages((currW*0.33)-25, (currW*0.34)-25, (currW*0.33)-25);
            break;
    }
    $(".frameContent").css({
        height: ($(window).height()-195)+'px',
        'overflow-y':'hidden'
    });
    $("#worksFrame .frameContent").css({
        height: ($(window).height()-198)+'px',
        'overflow-y':'hidden'
    });
}

function setGlobalVars(){		
    if($(window).width() >= 885){
        currW = $(window).width();
    }else{
        currW = 885;
    }
    focusedW = currW - (minFrameW*2);
}

/*-----DROP DOWN ACTIONS-----*/
function setDropDownActions() {	
    $('#worksByCategorySelectForm option:nth-child(1)').attr('selected', 'selected');
    $('#worksByYearSelectForm option:nth-child(1)').attr('selected', 'selected');
    $('#worksByNameSelectForm option:nth-child(1)').attr('selected', 'selected');
    $('#officeMainCategorySelectForm option:nth-child(1)').attr('selected', 'selected');
    $('#newsMainCategorySelectForm option:nth-child(1)').attr('selected', 'selected');
    $("#worksByCategorySelectForm").click(function(event){
        event.stopPropagation();
    });
    $("#worksByYearSelectForm").click(function(event){
        event.stopPropagation();
    });
    $("#worksByNameSelectForm").click(function(event){
        event.stopPropagation();
    });
    $("#officeMainCategorySelectForm").click(function(event){
        event.stopPropagation();
    });
    $("#newsMainCategorySelectForm").click(function(event){
        event.stopPropagation();
    });
    $("#worksByCategorySelectForm").change(changeWorksSelects);
    $("#worksByYearSelectForm").change(changeWorksSelects);
    $("#worksByNameSelectForm").change(showWorksData);
    $("#officeMainCategorySelectForm").change(showOfficeData);
//$("#newsMainCategorySelectForm").change(showNewsData);
}

function changeWorksSelects(){
    $('#worksByNameSelectForm option:nth-child(1)').attr('selected', 'selected');
    showWorksData('no');
}

function showWorksData(xid){
    var tid = $('#worksByNameSelectForm').val();
    if(xid.length && xid!='no'){
        tid=xid;
        $('#worksByNameSelectForm').val(tid);
    }
    isLoading = true;
    $("#worksMaskOver").show();
    $("#worksLogoOver").show();
    $("#worksLogo").hide();
    $.ajax({
        url: "includes/functions/get_works.php",
        type: "POST",
        data: ({
            category:$('#worksByCategorySelectForm').val(),
            year:$('#worksByYearSelectForm').val(),
            id :tid
        }),
        success: function(html){
            $("#worksContent").html(html);
            pushWorkImages();
            if($("#worksContent .imageWall").length){
                $("#worksContent .imageWall").css({
                    position:'static'
                });
            }
            isLoading = false;
            onWorksOut();
        }
    });
}

function showOfficeData(){
    isLoading = true;
    $("#officeMaskOver").show();
    $("#officeLogoOver").show();
    $("#officeLogo").hide();
    $.ajax({
        url: "includes/functions/get_office.php",
        type: "POST",
        data: ({
            id :$('#officeMainCategorySelectForm').val()
            }),
        success: function(html){
            $("#officeDynamicCont").html(html);
            pushOfficeImages();
            $("#officeDynamicCont .imageWall").css({
                position:'static'
            });
            isLoading = false;
            onOfficeOut();
        }
    });
}

function showNewsData(){}

/*-----IMAGE ACTIONS-----*/

function animateImages(workW, officeW, newsW){
    var contPrevScale = workW / 450;
    if(contPrevScale > 1){
        contPrevScale = 1;
    }
    var newW = 0;
    var newH = 0;
    var i=0;
    var im=0;
    for(i=0, im=worksImgs.length; im>i; i++){
        newW = Math.floor((worksImgs[i][1] * contPrevScale));
        newH = Math.floor((worksImgs[i][2] * contPrevScale));
        if(newW>worksImgs[i][1]){
            newW = worksImgs[i][1];
        }
        if(newH>worksImgs[i][2]){
            newH = worksImgs[i][2];
        }
        worksImgs[i][0].animate({
            width: newW+'px',
            height:newH+'px'
            }, 500);
    }
    contPrevScale = officeW / 450;
    if(contPrevScale > 1){
        contPrevScale = 1;
    }
    for(i=0, im=officeImgs.length; im>i; i++){
        newW = Math.floor((officeImgs[i][1] * contPrevScale));
        newH = Math.floor((officeImgs[i][2] * contPrevScale));
        if(newW>officeImgs[i][1]){
            newW = officeImgs[i][1];
        }
        if(newH>officeImgs[i][2]){
            newH = officeImgs[i][2];
        }
        officeImgs[i][0].animate({
            width: newW+'px',
            height:newH+'px'
            }, 500);
    }
    contPrevScale = newsW / 450;
    if(contPrevScale > 1){
        contPrevScale = 1;
    }
    for(i=0, im=newsImgs.length; im>i; i++){
        newW = Math.floor((newsImgs[i][1] * contPrevScale));
        newH = Math.floor((newsImgs[i][2] * contPrevScale));
        if(newW>newsImgs[i][1]){
            newW = newsImgs[i][1];
        }
        if(newH>newsImgs[i][2]){
            newH = newsImgs[i][2];
        }
        newsImgs[i][0].animate({
            width: newW+'px',
            height:newH+'px'
            }, 500);
    }
}

function resizeImages(workW, officeW, newsW){
    var contPrevScale = workW / 450;
    if(contPrevScale > 1){
        contPrevScale = 1;
    }
    var newW = 0;
    var newH = 0;
    var i=0;
    var im=0;
    for(i=0, im=worksImgs.length; im>i; i++){
        newW = Math.floor((worksImgs[i][1] * contPrevScale));
        newH = Math.floor((worksImgs[i][2] * contPrevScale));
        if(newW>worksImgs[i][1]){
            newW = worksImgs[i][1];
        }
        if(newH>worksImgs[i][2]){
            newH = worksImgs[i][2];
        }
        worksImgs[i][0].css({
            width: newW+'px',
            height:newH+'px'
            });
    }
    contPrevScale = officeW / 450;
    if(contPrevScale > 1){
        contPrevScale = 1;
    }
    for(i=0, im=officeImgs.length; im>i; i++){
        newW = Math.floor((officeImgs[i][1] * contPrevScale));
        newH = Math.floor((officeImgs[i][2] * contPrevScale));
        if(newW>officeImgs[i][1]){
            newW = officeImgs[i][1];
        }
        if(newH>officeImgs[i][2]){
            newH = officeImgs[i][2];
        }
        officeImgs[i][0].css({
            width: newW+'px',
            height:newH+'px'
            });
    }
    contPrevScale = newsW / 450;
    if(contPrevScale > 1){
        contPrevScale = 1;
    }
    for(i=0, im=newsImgs.length; im>i; i++){
        newW = Math.floor((newsImgs[i][1] * contPrevScale));
        newH = Math.floor((newsImgs[i][2] * contPrevScale));
        if(newW>newsImgs[i][1]){
            newW = newsImgs[i][1];
        }
        if(newH>newsImgs[i][2]){
            newH = newsImgs[i][2];
        }
        newsImgs[i][0].css({
            width: newW+'px',
            height:newH+'px'
            });
    }
}

function swapImage(el){
    var $parent = $(el).parents('.sectionColumn');
    $parent.find('.big-image').css({
        "display":"none",
        "max-width":($parent.width()-30)+"px"
        });
    $(el).parent().find(".little-image").css({
        "display":"inline",
        "margin-top":"0"
    });
    if($(el).hasClass('little-image')){
        $(el).css("display", "none");
        var $big = $(el).next(".big-image");
        $big.css("display", "inline");
        $(el).parent().find(".little-image").each(function(){
            if($(this).offset().top == $big.offset().top){
                $(this).css("margin-top", ($big.height()-$(el).height())+"px");
            }
        });
}else{
    $(el).css("display", "none");
    $(el).prev(".little-image").css("display", "inline");
    $(el).parent().find(".little-image").css("margin-top", "0");
}
}

function playFullWork(){
    $('#worksLogo img').attr('src', 'images/site_layout/menu_gifs/WORK_RKITEKTS_once.gif');
}
function playFullOffice(){
    $('#officeLogo img').attr('src', 'images/site_layout/menu_gifs/OFFICE_RKITEKTS_onceOver.gif');
}
function playFullNews(){
    $('#newsLogo img').attr('src', 'images/site_layout/menu_gifs/NEWS_RKITEKTS_onceOver.gif');
}

function selectNewsText(id){	
    $('#newsFrame .fulltext').hide();
    $('#newsFrame .smalltext').show();
    if($('#fulltext'+id).length){
        $('#fulltext'+id).show();
        $('#smalltext'+id).hide();
        $('#newsFrameContent').animate({scrollTop : $('#news_item_'+id).position().top},400);
    }
}

function toggleNewsText(id){	
    if($('#fulltext'+id).is(':hidden')){
        $('#fulltext'+id).show();
        $('#smalltext'+id).hide();
    }else{
        $('#fulltext'+id).hide();
        $('#smalltext'+id).show();
    }
}

