﻿var home_gal_sel = 1;
$(function() {
    $('div.gall_mitm').each(function(){
        if (!checkHomeGallSel(this)){
            $(this).css('opacity',0.5);
        }
        $(this).hover(function(){
            if (!checkHomeGallSel(this)){ $(this).fadeTo('fast',1.0); }
        },function (){
            if (!checkHomeGallSel(this)){ $(this).fadeTo('fast',0.5); }
        });
        $(this).click(function(){
            var index = getItemIndex(this,'gall_mitm_');
            if (index>=0 && index != home_gal_sel){
                $('div#gall_mitm_'+home_gal_sel).fadeTo('fast',0.5);
                loadItem(index);
            }
        });
        
    });

    function loadItem(index){
        if (index>=0 && index != home_gal_sel){
            var move = (home_gal_sel-index)*250;
            var value = Math.abs(home_gal_sel-index)*250;
            if ((home_gal_sel-index)<0){
                $('div#gall_view_coll').animate({"top": "-=" + value + "px"}, "slow");
            } else if ((home_gal_sel-index)>0){
                $('div#gall_view_coll').animate({"top": "+=" + value + "px"}, "slow");
            } 
            if (Math.abs(home_gal_sel-index)>0) {
                home_gal_sel = index;
                $('div.gall_desc').hide();
                $('div#gall_desc_' + home_gal_sel).show();
            }
            home_gal_sel = index;
        }
    }


});

    function checkHomeGallSel(obj){
        var sel = false;
        var index = getItemIndex(obj,'gall_mitm_');
        if (index >= 0){
            sel = (index == home_gal_sel);
        }
        return sel;
    }

    function getItemIndex(obj,match){
        var itm_id = $(obj).attr('id');
        var ret = -1;
        if (itm_id.indexOf(match)>=0){
            var index = itm_id.substring(String(match).length);
            if (!isNaN(index)) {
                ret = index;
            }
        }
        return ret;
    }
