function textCounter(field, cntfield, maxlimit)
{
    if (field.value.length > maxlimit)
    {
        field.value = field.value.substring(0, maxlimit);
    }
    else
    {
        cntfield.value = maxlimit - field.value.length;
    }
}

function popupPhotoSelector(form, field)
{
    window.open("pages/photo_selector.php?external=1&form=" + form + "&field=" + field, "selectphoto", "width=800,height=600,scrollbars=yes,statusbar=no,menubar=no,resizable=yes");
}

function popupDirSelector(form, field)
{
    window.open("pages/photo_selector.php?external=2&form=" + form + "&field=" + field, "selectphoto", "width=800,height=600,scrollbars=yes,statusbar=no,menubar=no,resizable=yes");
}

jQuery(function($)
{
    $(document).ready(function()
    {
        var ltie7 = $.browser.msie && $.browser.version <= 7;
    	
    	if (ltie7)
    	{
            $('img[@src$=.png]').ifixpng();
        }
        
        var iter = 0;
        var again;
        
        do
        {
        	again = false;
        
	        $('img').each(function()
	        {
	        	if ($(this).width() == 0)
	        	{
	        		$(this).attr('src', $(this).attr('src') + '?' + (new Date()).getTime());
	        		if (again == false) iter++;
	        		again = true;
	        	}
	        });
	        
	        if (iter == 10) again = false;
	        
        } while (again == true);
    });
    
    $('#leaver').click(function()
    {
    	$(this).hide();
    	$('#commentform').show();

		Recaptcha.create("6LcFlgIAAAAAAOrbMc6UZDUvAgycrgO-BvwMxR8N", "recaptcha", {
		    theme: "red",
		    tabindex: 0,
		    callback: Recaptcha.focus_response_field
		});
   	
	});
    
    $('#gallery_main_image').load(function()
    {
        $(this).fadeTo(250, 1.0);        
    });

    $('img.gallery_image').each(function(i)
    {
        var o = $(this).offset();
        
        $(this).css('top', o.top + 22);
        $(this).css('left', o.left + i * 34);
    });

    $('a.gallery_clicker').click(function()
    {
        var navLink = $('#gallery_navigator > a:eq(' + (parseInt($(this).attr('index')) + 1) + ')');

        if (navLink.attr('id') == null)
        {
            navLink = $('#gallery_navigator > a:eq(0)');
        }

        $(this).attr('index', navLink.attr('id'));
        navLink.click();
    });   

    if (!$.browser.opera)
    {
        $('#gallery_navigator > a').hover(
        function()
        {
            if ($(this).hasClass('gallery_number_active')) return;
            $(this).next().fadeIn(250);        
        },
        
        function()
        {
            if ($(this).hasClass('gallery_number_active')) return;
            $(this).next().fadeOut(250);
        });
    }
    
    $('#gallery_navigator > a').click(function()
    {
        var img = $('#gallery_main_image');
        var desc = $('#gallery_description');
        var thumb = $(this).next();
        var clicker = $('a.gallery_clicker');

        if ($(this).hasClass('gallery_number_active')) return;
        if (thumb.attr('id') == img.attr('src'))
        {
            $(this).removeClass("gallery_number");
            $(this).addClass("gallery_number_active");

            return;
        }
        
        $('a.gallery_number_active').each(function()
        {
            $(this).removeClass('gallery_number_active');
            $(this).addClass('gallery_number');        
        });
        
        $(this).removeClass("gallery_number");
        $(this).addClass("gallery_number_active");
        
        clicker.attr('index', $(this).attr('id'));

        thumb.hide();
        
        img.fadeTo(250, 0.01, function()
        {
            img.attr('src', thumb.attr('id'));
        });
        
        var d = $(this).attr('alt');
        
        if (d == "")
        {
            desc.hide();
        }
        else
        {
            desc.show();
            desc.text($(this).attr('alt'));
        }
    });

    
    $('a.toggler').each(function()
    {
        $(this).click(function()
        {
            var img = $('img:eq(0)', this);
            var div = $('div.' + $(this).attr('id'));
            div.toggle();
            
            if (div.css("display") == "none")
            {
                img.attr("src", "images/arrowRight.png");            
            }
            else
            {
                img.attr("src", "images/arrowDown.png");
            }
        });
    });

    if ($('#navigation'))
    {
        var nav_resting_width = "24px";
        var nav_hover_width = "32px";
        var nav_resting_color = "#000000";
        var nav_hover_color = "#800000";
        var body_class_name = document.body.className;
        var delay = 400;
        var ltie7 = $.browser.msie && $.browser.version <= 7;
        
        $('#navigation > li').each(function(e)
        {
        	$(this).click(function()
        	{
    			$('a#hoverable', this).attr('id', 'active');
    			$('div#hoverable', this).attr('id', 'active');
    		});
        
            $(this).hover(function()
            {
                if (!ltie7)
                {
    				$('img#hoverable', this).animate({opacity: 1.0}, 200);
    				$('div#hoverable', this).animate({opacity: 0.75}, 200);
    			}
            },
            function()
            {
                if (!ltie7)
                {
                	$('img#hoverable', this).animate({opacity: 0.5}, 200);
                	$('div#hoverable', this).animate({opacity: 0.0}, 200);
                }
            });
        });
    }
});

