
/* ============================= */
/* ===== JavaScript Document === */
/* ============================= */


$(document).ready(function() {

    initInputValue();

    //alert($('#slider-stage li').length);

    if ($('#slider-stage li').length > 3) {
        $('#slider-stage').carousel('#previous', '#next');
    } else {
        $('.highlights #previous').css({ 'background': '#ffeb2d', 'cursor': 'default' });
        $('.highlights #next').css({ 'background': '#ffeb2d', 'cursor': 'default' });
    }

    if ($('#pane1').length) {
        $('#pane1').jScrollPane();
    }
});



function initInputValue() {
    $('input[type="text"]').each(function() {
        $(this).data('defaultValue', $(this).val());
    });
    $('input[type="text"]').focus(function() {
        if ($(this).val() == $(this).data('defaultValue')) {
            $(this).val("");
        }
    });
    $('input[type="text"]').blur(function() {
        if ($(this).val() == "") {
            $(this).val($(this).data('defaultValue'));
        }
    });
}

function OpenLogin(redirect, returnUrl) {

    $(function() {
        tb_show("", "LoginFlash.aspx?TB_iframe=true&height=510&width=960&modal=true&redirect=" + redirect + "&ReturnUrl=" + returnUrl, "");
    });

    return false;
}

function OpenAgeControl() {
    $(function() {
        tb_show("", "/Age_Control.aspx?TB_iframe=true&height=510&width=960&modal=true", "");
    });
    return false;
}

function UpdateFlashUser(userLogin) {
    //Actualizar o SWF
    var windowParent = $(window.parent.document);
    var myFlashObject = windowParent.find('#flashContentPassatempo');
    if (myFlashObject.length) {
        myFlashObject = myFlashObject.find('div').attr('id');

        var idSwf = myFlashObject.replace('flashcontent', 'JbPortugal');

        if (idSwf.length) {
            var swfObjectPassatempo = window.parent.document.getElementById(idSwf);
            swfObjectPassatempo.UpdateUser(userLogin);
            //alert('fim');
        }
    }

    return false;
}


/****************
################# DECLARE GLOBAL VARS, ARRAYS, OBJECTS ################# 
****************/

var countLinkIndex, sentido, xmlPath, imagePath;
countLinkIndex = 0;

/****************
################# WHEN DOM LOADS ################# 
****************/

$(document).ready(function() {

    initGaleriaSlideShow()
    initCreateNumberLinks();

});

/****************
################# RESET THE ANIMATION POSITION AND INITS ARROW NAVIGATION ################# 
****************/

function initGaleriaSlideShow() {
    $('.blockHistoryFrameContent').css('left', '0px');
    $('.setaEsquerda a').addClass('disabled');
    initGaleriaSetas();
}

/****************
################# CREATE ARROW NAVIGATION EVENT HANDLERS ################# 
****************/

function initGaleriaSetas() {
    $('.setaEsquerda a').bind('click', function() {
        animateSlideShow('esq');
        return false;
    });
    $('.setaDireita a').bind('click', function() {
        animateSlideShow('dir');
        return false;
    });
}

/****************
################# CREATE NAVIGATION NUMBER LINKS FOR EACH SENTIDO ################# 
****************/

function initCreateNumberLinks() {

    var param = $('.blockHistoryFrameContent .blockHistorySlide').length;

    $('.blockIndexHistory').html('&nbsp;')
    var html2 = ''
    html2 += '<ul>';
    for (var i = 1; i <= param; i++) {
        html2 += '<li><a href="#">' + i + '</a></li>'
    }
    html2 += '<li class="txt">&nbsp;de&nbsp;' + param + '</ul>';
    $('.blockIndexHistory').append($(html2));
    var liWidth = parseInt($('.blockIndexHistory ul li').width());

    initLayoutNumberLinks();
}

/****************
################# CREATE EVENT HANDLERS FOR NAVIGATION NUMBER LINKS FOR EACH SENTIDO ################# 
****************/

function initLayoutNumberLinks() {
    $('.blockIndexHistory ul li:eq(0) a').addClass('selected');
    $('.blockIndexHistory ul li:eq(1) a').addClass('selectedDir');
    var slideWidth = $('.blockHistorySlide').width();

    $('.blockIndexHistory ul li').each(function(index) {
        $(this).find('a').click(function() {
            $('.blockIndexHistory ul li a').removeClass();
            var linkLi = $(this);
            linkLi.parent().prev('li').children('a').addClass('selectedEsq');
            linkLi.addClass('selected');
            linkLi.parent().next('li').children('a').addClass('selectedDir');
            var positionLeft = index * slideWidth;
            animateSlideShow('numberLink', positionLeft *= -1);
            countLinkIndex = index;
            return false;
        });
    });
}

/****************
################# CREATE CUSTOMIZED CSS LAYOUT FOR EACH LINK AND ITS SIBLINGS ################# 
****************/

function initSetNumberLinks(param) {
    $('.blockIndexHistory ul li a').removeClass();
    $('.blockIndexHistory ul li:eq(' + (param - 1) + ') a').addClass('selectedEsq');
    $('.blockIndexHistory ul li:eq(' + param + ') a').addClass('selected');
    $('.blockIndexHistory ul li:eq(' + (param + 1) + ') a').addClass('selectedDir');
}

$('.setaEsquerda a, .setaDireita a').click(function() {
    return false;
});

/****************
################# CUSTOMIZED ANIMATION OF THE SLIDES ################# 
****************/

function animateSlideShow(param, param2) {

    //UNBINDS EVENT HANLDE WHEN ANIMATION IS NOT COMPLETED

    $('.setaEsquerda a').unbind('click');
    $('.setaDireita a').unbind('click');
    $('.setaEsquerda a, .setaDireita a').click(function() {
        return false;
    });

    var easingType1 = 'easeInQuad';
    var easingType2 = 'easeinout';
    var animDuration = 500;



    var totalRowWidth = $('.blockHistoryFrameContent .blockHistorySlide').length * $('.blockHistoryFrameContent .blockHistorySlide').width();
    var numberSlides = $('.blockHistoryFrameContent .blockHistorySlide').length;
    var slideWidth = $('.blockHistoryFrameContent .blockHistorySlide').width();
    var slidePosition = parseInt($('.blockHistoryFrameContent').css('left'));

    //IF ARROW NAVIGATION IS RIGHT

    if (param == 'dir') {

        if (slidePosition != ((parseInt(totalRowWidth) - parseInt(slideWidth)) * -1)) {
            $('.setaEsquerda a').removeClass('disabled');
            slidePosition -= slideWidth;
            initSetNumberLinks(countLinkIndex += 1);
        }
        if (slidePosition == ((parseInt(totalRowWidth) - (parseInt(slideWidth))) * -1)) {
            $('.setaDireita a').addClass('disabled');

        }
    }

    //IF ARROW NAVIGATION IS LEFT

    if (param == 'esq') {

        if (slidePosition != 0) {
            $('.setaDireita a').removeClass('disabled');
            slidePosition += slideWidth;
            initSetNumberLinks(countLinkIndex -= 1);
        }
        if (slidePosition == 0) {
            $('.setaEsquerda a').addClass('disabled');
        } else {
            $('.setaEsquerda a').removeClass('disabled');
        }
    }

    //IF NUMBERS NAVIGATION

    if (param == 'numberLink') {
        slidePosition = parseInt(param2);
        if (slidePosition == 0) {
            $('.setaEsquerda a').addClass('disabled');
        } else {
            $('.setaEsquerda a').removeClass('disabled');
        }
        var count1 = parseInt(totalRowWidth);
        var count2 = parseInt(slideWidth);
        var count3 = (count1 - count2) * -1;

        //alert(count1 + " : " + count2 + " : " + count3);

        if (slidePosition == count3) {
            $('.setaDireita a').addClass('disabled');
        } else {
            $('.setaDireita a').removeClass('disabled');
        }
    }

    //ANIMATION OF THE GALLERY SLIDESHOW

    $('.blockHistoryFrameContent')
		.animate(
			{ left: slidePosition },
			{
			    duration: animDuration,
			    easing: easingType2,
			    complete: function() {
			        //RESTARTS EVENT HANLDER WHEN ANIMATION IS COMPLETE
			        initGaleriaSetas();
			    }
			}
	);


}
