var nav      = '';
var imgnum   = 0;
var imgid    = '';
var blocknum = 0;
var blockid  = '';
var txnum    = 0;
var txid     = '';

var animateMaximised  = false;
var animateActive     = false;

var blockTitle = new Array();
var blockAlt   = new Array();
var blockText  = new Array();

jQuery(document).ready(function () {

    generateMiniGal();

    tweakPortfolio();

});

function generateMiniGal ()
{
    jQuery(' .csc-textpic').each(function() {
        var csctextpicObj = jQuery(this);
        blocknum++;
        blockid = 'csc-textpic-' + blocknum;
        nav = '';
        idnum = 0;

        jQuery(this).attr('id', blockid);

        jQuery(this).find('.csc-textpic-imagewrap').each(function() {

            // console.log(jQuery(this).html());

            imgnum = 0;
            jQuery(this).find('.csc-textpic-imagerow').each(function() {
                imgnum++;
                imgid  = 'csc-textpic-imagerow-' + blocknum + '-' + imgnum;
                var link = 'MiniGal_view(\''+blockid+'\', \''+imgid+'\')';
//                var effect = 'jQuery(\'.minigalnav\').removeClass(\'act\');jQuery(this).toggleClass(\'act\')';
                var effect = '';
                jQuery(this).attr('id', imgid).addClass(blockid);
                jQuery(this).attr('onClick', 'MiniGalNext(\'' + blockid + '\', \'' + imgid  + '\')' ).css('cursor','pointer');
                nav += '<a href="javascript:void(0)" class="minigalnav" id="nav_' + imgid  + '" onClick="' + link + ';' + effect + ';return false;">' + imgnum + '</a>';
                if( imgnum > 1)
                {
                    jQuery(this).hide();
                }
            }); // each '.csc-textpic-imagerow'

            imgnum = 0;
            jQuery(this).find('.csc-textpic-caption').each(function() {
                imgnum++;
                imgid  = 'csc-textpic-imagerow-' + blocknum + '-' + imgnum;
                blockText[imgid] = jQuery(this).html();
                jQuery(this).remove();
            }); // each '.csc-textpic-caption'


            imgnum = 0;
            jQuery(this).find('.csc-textpic-image dt img').each(function() {
                imgnum++;
                imgid  = 'csc-textpic-imagerow-' + blocknum + '-' + imgnum;
//                console.log( jQuery(this) );
//                console.log('csc-textpic-image title -> ' + jQuery(this).attr('title'));
//                console.log('csc-textpic-image alt -> ' + jQuery(this).attr('alt'));
                blockTitle[imgid] = jQuery(this).attr('title');
                blockAlt[imgid] = jQuery(this).attr('alt');
            }); // each '.csc-textpic-image


        }); // each '.csc-textpic-imagewrap'
        
        if( nav.length > 0) {
            jQuery('#' + blockid).append('<div class="csc-textpic-dynnav">' + nav + '</div>');
        }


        // http://jscrollpane.kelvinluck.com/

        jQuery(this).find('.csc-textpic-text').each(function() {
            var csctextpictextObj = jQuery(this);
            var csctextpictextText = ''
            jQuery(this).find('p').each(function() {
                csctextpictextText += '<p>' + jQuery(this).html() + '</p>';
                jQuery(this).remove();
            }); // find p
            txnum++;
            txid  = 'dyntx' + txnum;
            csctextpictextObj.append('<div class="dyntx" id="' + txid + '">' + csctextpictextText + '&nbsp;<br/>&nbsp;<br/></div>');
            jQuery('#' + txid).jScrollPane({
        	autoReinitialise: true,
        	showArrows: true
            });

            jQuery(this).click(function() {
            //jQuery('#' + txid).css('height', '100%');
                if( !animateActive ) {
                    if( !animateMaximised )
                    {
                        animateActive = true;
                        jQuery(this).animate({height: "230px", top: "505px"}, 300, function() {
                            animateActive    = false;
                            animateMaximised = true;
                            var txidObj= jQuery('#' + txid);
                            var txid_h= '116';
                            txidObj.css('height', (txid_h + 'px') );

                            var jspPane=jQuery(' .jspPane', txidObj);
                            var jspPane_h=jspPane.css('height').replace(/px/,'');
                            
                            
//                            var dyntx_h=jspPane.css('height').replace(/px/,'');
                            var diff= txid_h - jspPane_h + 5;
                            
                            jspPane.css({'padding-bottom': diff + 'px'});
                            
//                            console.log( 'diff:', txid_h - jspPane_h );
//                                            .animate({'padding-bottom':'200px'});
                        });
                    }
                    else if( animateMaximised )
                    {
                        animateActive = true;
                        jQuery(this).animate({height: "160px", top: "565px"}, 150, function() {
                            animateActive    = false;
                            animateMaximised = false;
                            jQuery('#' + txid).css('height', '46px');
                        });
                    }
                }
                var api = jQuery('#' + txid).jScrollPane().data('jsp');
                api.reinitialise();
            }).css('cursor', 'pointer');
            
        }); // find('.csc-textpic-text')
        

        
    });
}

function MiniGal_view (classname, id)
{
//    console.log('MiniGal_view (' + classname + ',' + id + ')');

    jQuery('.csc-textpic .' + classname).each(function() {
        var vid = jQuery(this).attr('id');
        if( vid != id) {
            jQuery(this).hide();
        } else {
            jQuery(this).show('fast');
        }
    });

    jQuery('.minigalnav').removeClass('act');
    jQuery('#nav_' + id).toggleClass('act');
    
    jQuery('.csc-textpic h1').each(function() {
        jQuery(this).html(blockTitle[id]);
    });
    jQuery('.csc-textpic h3').each(function() {
        jQuery(this).html(blockAlt[id]);
    });

    if( jQuery('#dyntx1').jScrollPane() ) {
        var settings = '{autoReinitialise: true, showArrows: false}';
        var api = jQuery('#dyntx1').jScrollPane().data('jsp');
        api.getContentPane().html('<p>' + blockText[id] + '</p>&nbsp;<br/>&nbsp;<br/>');
        api.reinitialise();
    }
}

function MiniGalNext (blockid, imgid)
{
//    alert('MiniGalNext ' + blockid + ', ' + imgid);
    var firstid = 'unset';
    var locator = 'seek';
    var viewid  = 'unset';

    jQuery('.csc-textpic .' + blockid).each(function()
    {
        var vid = jQuery(this).attr('id');

//        console.log(' ... ' + blockid + ', ' + imgid + ' ?= ' + vid + ' locator=' + locator + ', viewid=' + viewid);

        if( firstid == 'unset' )
        {
            firstid = vid;
        }
        
        if( vid != imgid) 
        {
            if( locator == 'next') 
            {
                locator = 'end';

//                console.log('this is it ...');

//                MiniGal_view(blockid, vid);
                viewid = vid;
            }
            else if( locator == 'seek' )
            {
                //jQuery(this).hide();
            }
        } 
        else if( vid == imgid && locator == 'seek') 
        {

//            console.log('the next is it ...');

            locator = 'next';
        } 

    });

    // Nichts gefunden?
    if( viewid  == 'unset' && firstid != 'unset' )
    {

//      console.log('end: the first is it ... (' + blockid + ',' + firstid + ')');
        viewid = firstid;
    }

    if( viewid  != 'unset' )
    {
        MiniGal_view(blockid, viewid);
    }
    
    // ende
}


function tweakPortfolio ()
{
    jQuery('.portfolio .pokunden').each(function() {
        var Obj = jQuery(this);
        var Text = ''
        Obj.find('p').each(function() {
            Text += '<p>' + jQuery(this).html() + '</p>';
            jQuery(this).remove();
        }); // find p
        Obj.append('<div class="dynpotx" id="dynpotx">' + Text + '&nbsp;<br/>&nbsp;<br/></div>');
        jQuery('.dynpotx').jScrollPane({showArrows: true});
    });
}

