var fullPageDecos = [];

jQuery(document).ready(function(){
    
        
		if (window.IE != 6)
        {
            // add page-wide backgrounds to some content blocks
            jQuery('.withFullPageDeco').each(function(){
                fullPageDecos[fullPageDecos.length] = new fullPageDeco( this );
            });


            // replace content of marked elements with generated texts
            var headings = jQuery('.richtextDecoHeadings h2').add('.richtextDecoHeadings h3');
            imageText.init(headings, { type : 'contentHeadings', lineHeight: false } );

        }
		
		
        if ( jQuery.browser.msie ) {
            var ieHeigt = jQuery('.headerImagesBox').innerHeight();
            jQuery('.headerImagesOverlay').height(ieHeigt);
        }
        
        if( jQuery('.headerImagesBox').length > 0 )
        {
            initHeaderImages();
            updateImageOverlayHeight();
        }
        
});


var fullPageDeco = function( box )
{
    this.ok = false;

    box = jQuery(box);
    
    if (box.size() != 1)
    {
        return;
    }
    this.sourceBox = box;
    
    this.decoBox = jQuery('<div class="fullPageDeco"><!-- --></div>');


    this.decoBox.appendTo(document.body);

    this.ok = true;

    this.update();
};


fullPageDeco.prototype.update = function()
{
    if (!this.ok)
    {
        return;
    }


    var css = {
        top: parseInt( this.sourceBox.offset().top ),
        height: parseInt( this.sourceBox.outerHeight() )
    };

    this.decoBox.css(css).show();

}


function updateFullPageDecos()
{
    for (var i=0; i<fullPageDecos.length; i++)
    {
        fullPageDecos[i].update();
    }
    return;
}


var imageText = new function()
{
    var script = this;

    this.init = function( nodes, moreParams )
    {
        script.baseUrl = jQuery('#imageTextUrl').val();
        if (!script.baseUrl)
        {
            return;
        }
        if (!moreParams)
        {
            moreParams = {};
        }

        nodes = jQuery(nodes);
        nodes.each(function(){

            var node = jQuery(this);

            var params =
            {
                tag: this.tagName,
                text: node.text(),
                maxWidth: node.width(),
                fontSize: parseInt(node.css('fontSize')),
                lineHeight: parseInt(node.css('lineHeight'))
            };


            var textClass = getImageTextClass( node );
            if (textClass)
            {
                params.textClass = textClass;
            }

            for (key in params)
            {
                if (!params[key])
                {
                    return; // if any of the params is empty, skip this one
                }
            }

            if (moreParams)
            {
                for (key in moreParams)
                {
                    if (moreParams[key] === false)
                    {
                        delete params[key];
                    }
                    else
                    {
                        params[key] = moreParams[key];
                    }
                }

            }

            replaceText( node, params );

            // alert(textClass);
        });
    }

    var getImageTextClass = function ( node )
    {
        var textClass = node.data('imageTextClass');
        if (!textClass)
        {
            var classes = node.attr('class').split(' ');
            for (var i=0; i<classes.length; i++)
            {
                var cl = classes[i];
                if (!cl.match(/^imageText.+$/))
                {
                    continue;
                }
                textClass = cl.replace(/^imageText/, '');
                // lcfirst
                textClass = textClass.substring(0,1).toLowerCase().concat(textClass.substring(1));
            }
            node.data('imageTextClass', textClass);
        }
        return textClass;
    }

    var replaceText = function( node, params )
    {
        node = jQuery(node);
        params.block = 'getImageText';

        var queryParamString = jQuery.param( params );
        node.load( script.baseUrl, queryParamString, function( resp )
        {
            node.css('visibility', 'visible');
            // node.css();
            // alert(resp);
        });
    }

};


function updateImageOverlayHeight()
{
    
	var height = jQuery('.headerImagesBox').height();
    var offset = jQuery('.headerImagesBox').offset();
    
    jQuery('.headerImages').height(height);
    jQuery('.headerImagesOverlay').offset({ top: offset.top });
    height = jQuery('.headerImagesBox').height();
    jQuery('.headerImagesOverlay').css('height',height);
    jQuery('.headerImagesOverlay').height(height);
	
	
}

function initHeaderImages()
{
    var thumbs = jQuery('#thumbs');
    
    jQuery('#headerImages').cycle({
        fx: 'fade',
        speed: 1000,
        timeout: 5000,
        pager: '#nav',
        pagerAnchorBuilder: function(idx, slide)
        {
            var alt = jQuery(slide).find('img').attr('class');
            var img = thumbs.find('.thumb-' + (alt.split('-')[1]));
            return '<a href="#"><img src="'+ img.text() +'" alt=""/></a>';
        }
    });
}
