/*
Flash Banners
*/
function initFlashBanner()
{
	var flashName;
	if( $('body').attr('class') == 'home' )
	{
		$('#flashBanner').flash(
			{ src: 'Swfs/homeBanner.swf',
			  width: 950,
			  wmode: 'transparent',
			  height: 436 },
			{ version: 9 }
		);
	}
	else
	{
		$('#flashBanner').flash(
			{ src: 'Swfs/banner.swf',
			  width: 950,
			  wmode: 'transparent',
			  height: 257 },
			{ version: 9 }
		);
	}
}
/*
Top menu
*/
var menuChild = null;
var outsideImage;
var imgNum;
var imgNamePre;
var imageCount;
function initMenu()
{
    $('#topMenu li').hover(function() {
        var posX = $(this).position().left +2;
        var posY = $('#topMenu').position().top + $('#topMenu').height();
        if($.browser.msie)
        {
            posY--;
            if(isIE6())
			{
				posX-=5;
                posY-=1;
			}
        }
        $(this).find('ul').css('left',posX+'px');
        $(this).find('ul').css('top',posY+'px');
        $(this).addClass("open");$(this).removeClass("close");
        $(this).find('ul:hidden').fadeIn('fast', function() {
			$(this).find('li').css('width','auto');
			var maxWidth = 0;
			$(this).find('li').each(function() {
				maxWidth = Math.max(maxWidth,$(this).width());
			});
			if(isIE6())
			{
				maxWidth += 20;
				$(this).find('li').css('padding-right','0px');
			}
			$(this).find('li').css('width',maxWidth+'px');
        });
        
    }, function() {
        menuChild = $(this);
        $(this).addClass("close");$(this).removeClass("open");
        setTimeout("checkHover()",150);
    });
    outsideImage = $('#topMenu li li').css('background-image');

    $('#topMenu li li').hover(function() {
        $(this).addClass("hover");
    }, function() {
        $(this).removeClass("hover");   
    });

    $('#topMenu a.selected').each(function() {
		var tag = $(this);
		while( tag.attr('id') != 'topMenu' )
		{
			tag = tag.parent();
			tag.addClass("selected");
		}
    });
}

function checkHover() 
{
    $('#topMenu li.close').each(function() {
        $(this).find('ul').fadeOut('fast');
    });
}

/*
Identifiers
*/
function addClassToListItems()
{
	$('ul').find('li:first').addClass('first');
	$('ul').find('li:last').addClass('last');
}

function addClassToCurrentPageLinks()
{
	$('li a').each( function()
	{
		if( document.location.href.indexOf($(this).attr('href')) >= 0 )
			$(this).addClass('selected');
	});
}

function closeRealisationListRows()
{
	$('.realisationList li:nth-child(3n)').addClass('closeRow');
}

/*
Popup
*/

function updatePopupContent(imageName, imageIndex)
{
	imageCount = $('.realisationList li').length;
	var targetImg = $('#popupImg');
	var img = new Image();

	targetImg.load(function () {
		if($.browser.msie)
			$('#popupBackground').css('display','block');
		else
			$('#popupBackground').fadeIn(250);
		$('#popupImageHolder').fadeIn(250);
		$('#loader').removeClass('loading').append(this);
		updatePopupPosition();
	}).attr('src', 'Images/'+imageName+'Large.jpg');
	$('#popupImagePager').html('Image '+imageIndex + ' de ' +imageCount);
}

function updatePopupPosition()
{
	/*if($('#popupImg').width() > 920 || $('#popupImg').height() > 470) {
		$('#popupImg').css('width', 700 + 'px');
		$('#popupImg').css('height', 470 + 'px');
	}*/
	var popupWidth = $('#popupImg').width();
    var popupHeight = $('#popupImg').height();
    var windowWidth = $(window).width();
    var windowHeight = $(window).height();
    var offsetHeight = 0;
	if (isIE6())
        offsetHeight = $(window).scrollTop();
	
	if (isIE6())
	{
		$('#popupBackground').css('top', offsetHeight + 'px');
		$('#popupBackground').css('width', windowWidth + 'px');
		$('#popupBackground').css('height', windowHeight + 'px');
	}
	
	$('#popupImageHolder').css('top', ((windowHeight - popupHeight) / 2) + 'px');
	$('#popupImageHolder').css('left', ((windowWidth - popupWidth) / 2) + 'px');
	
	$('#nextButton a').css('width', (popupWidth / 2) + 'px');
	$('#nextButton a').css('height', popupHeight + 'px');
	$('#prevButton a').css('width', (popupWidth / 2) + 'px');
	$('#prevButton a').css('height', popupHeight + 'px');
}

/**
 * Fix jQuery fadeTo() so it can bring back something after hide() or fadeOut()
 */
(function ($) {
    var proxied = $.fn.fadeTo;
    $.fn.fadeTo = function() {
        if ($(this).is(':hidden')) {
            $(this).css('opacity', 0).show();
        }
        
        return proxied.apply(this, arguments);
    }
})(jQuery);

function showControls(Next)
{
	if (Next) {
		$('#nextButtonImg').stop().fadeTo(250, 1);
	}
	else {
		$('#prevButtonImg').stop().fadeTo(250, 1);
	}
}

function hideControls()
{
		$('#nextButtonImg').stop().fadeOut(250); 
		$('#prevButtonImg').stop().fadeOut(250);
}

function cyclePopupImage(backwards) 
{
	var newImgNum = imgNum;
	if (backwards && imgNum > 1) {
		newImgNum--;
	} else if (!backwards && imgNum < imageCount) {
		newImgNum++;
	}
	var imgNamePreNum = newImgNum;
	while (String(imgNamePreNum).length < 2 && imgNum > 0 && imgNum <= imageCount) {imgNamePreNum = '0' + imgNamePreNum;}
	popupImage(imgNamePre + imgNamePreNum, newImgNum);
}

function popupImage( imageName, imageIndex )
{
	imgNum = imageIndex;
	imgNamePre = imageName.replace(/[0-9]/g,'');
	
	$(window).resize(updatePopupPosition);
	$(window).scroll(updatePopupPosition);
	
	updatePopupContent(imageName, imageIndex);
}

function closePopup()
{
	if($.browser.msie)
		$('#popupBackground').css('display','none');
	else
		$('#popupBackground').fadeOut(250);

	$('#popupImageHolder').fadeOut(250);
	
	$(window).resize();
	$(window).scroll();
}

/*
Misc
*/

function isIE6() 
{
    return ($.browser.msie && $.browser.version.substr(0, 1) == "6");
}

function changeLanguage( lang )
{
	location.href = "../"+lang+"/"+pageName(location.href);
}

function pageName( href )
{
	return href.substring((href.lastIndexOf("/")+1),href.length);
}

$(document).ready(function(){
	addClassToCurrentPageLinks();
	initMenu();
    initFlashBanner();
	addClassToListItems();
	closeRealisationListRows();
    $(document).pngFix();
});

