/**
* Fichier : global.js
* Description : fonctions JS génériques
* Auteur : Hamza Iqbal - hiqbal[at]actualys.com
* Copyright: Actualys
******************************************/

/**
* Initialisation
******************************************/
$(document).ready(function() {
    rolloverImg();
    toolTips();
    videInputFocus();

    // Toggle button
    $('.toggle').click(function() {
        $(this).toggleClass('on');
        $(this).parent().find("." + $(this).attr('rel')).toggle();

        return false;
    });
	
	if ($('#product-search').length) {
		$('#webtv-sidebar').css({backgroundPosition: '0px -50px'}); 
	}



    if ($('#funds_list')) expandFundsList();

    // Simple Search + Advanced search
    if ($('#dropdown_product_funds_list')) {
        initFundsSearchArea('dropdown_product_funds_list');
        lipperSearchList();
    }

    if ($('#expertise_dropdown_funds_list')) {
        initExpertiseFundsSearchArea();
    }

    // Advanced search
    if ($('#dropdown_asset_funds_list'))
        initFundsAdvancedSearchArea('#funds');

    // Site Switcher
    $("#site_switcher").change(function() {
        var url = $(this).val();
        if (url != "")
            document.location = url;
    });
});


/**
* Manage cluetip tooltips
******************************************/
var toolTips = function() {

    $(function() {

        $('.helpToolTip').cluetip({
            cluetipClass: 'jtip',
            cluezIndex: 97,
            dropShadow: false,
            hoverIntent: false,
            sticky: false,
            closeText: '',
            mouseOutClose: true,
            local: true
        });
        $('.tooltipInfo').bgiframe();

    });
};

/**
* Rollover
******************************************/
var rolloverImg = function() {
    preloadRolloverImg();

    $(".rollover").hover
	(
		function() { $(this).attr('src', rolloverNewImg($(this).attr('src'))); },
		function() { $(this).attr('src', rolloverOldimage($(this).attr('src'))); }
	);

    $(".rollover").click
	(
    //function() { $('#funds').submit() ; }
    // function() { $(this).attr('src', rolloverNewImg($(this).attr('src'))); }
	);
}

var preloadRolloverImg = function() {
    $(window).bind('load', function() {
        $('.rollover').each(function() {
            $('<img>').attr('src', rolloverNewImg($(this).attr('src')));
        });
    });
}

var rolloverNewImg = function(src) {
    // if (src.indexOf('_on') < 0)
    return src.substring(0, src.search(/(\.[a-z]+)$/)) + '_on' + src.match(/(\.[a-z]+)$/)[0];
    // alert(src);
    //return src;
}

var rolloverOldimage = function(src) {
    return src.replace(/_on\./, '.');
}

/**
* Vide la valeur des champs input au focus
******************************************/
var videInputFocus = function() {
    var input = $('input.input_focus[value!=""]');

    input.each(function() {
        $(this).focus(function() { if (this.value == this.defaultValue) this.value = ''; });
        $(this).blur(function() { if (this.value == '') this.value = this.defaultValue; });
    });
};




/**
* Expand Funds list
******************************************/
var expandFundsList = function() {

    var table = $('#funds_list_table');
    var td_isin = table.find('tr:not(.more) td.isin');

    table.find('tr.more').hide();

    td_isin.click(function() {
        var td = $(this);
        var tr = td.parent();

        tr.toggleClass('open');

        tr.nextAll().each(function() {
            if ($(this).hasClass('more')) $(this).toggle();
            else return false;
        });
    });

};


var initExpertiseFundsSearchArea = function() {

    var dropdownFundsList = document.getElementById('expertise_dropdown_funds_list');
    if (dropdownFundsList == null) {
        return;
    }
    hightlightNatixisFunds('expertise_dropdown_funds_list');

    dropdownFundsList.onchange = function() {
        OpenFancyDetails(dropdownFundsList.options[dropdownFundsList.selectedIndex].value);
    }

};


var hightlightNatixisFunds = function(dropDownId) {

    var dropdownFundsList = document.getElementById(dropDownId);
    if (dropdownFundsList == null) {
        return;
    }

    dropdownFundsList.className = "products-dropdown";

    for (i = 0; i < dropdownFundsList.length; i++) {
        var option = dropdownFundsList.options[i];
        var optionText = dropdownFundsList.options[i].text;
        if (optionText != "nothing" && optionText.indexOf('Natixis') >= 0) {
            option.className = "hightlighted";
        }
        else {
            option.className = "nothightlighted";
        }
    }

    dropdownFundsList.onfocus = function() {
        if (dropdownFundsList.selectedIndex == 0) {

            for (i = 0; i < dropdownFundsList.length; i++) {
                var option = dropdownFundsList.options[i];
                var optionText = dropdownFundsList.options[i].text;

                if (optionText != "nothing" && optionText.indexOf('Natixis') >= 0) {
                    dropdownFundsList.selectedIndex = i;
                    break;
                }
            }
        }
    }
};

/* Search Block
/* Search ComboBox : Choose an OPCVM
/* Hightlight Natixis products
*************************************/
var initFundsSearchArea = function(dropdownFundID) {

    var dropdownFundsList = document.getElementById(dropdownFundID);
    if (dropdownFundsList == null) {
        return;
    }

    hightlightNatixisFunds(dropdownFundID);


    var productName = document.getElementById('Name');
    if (productName != null) {
        productName.onchange = function() {
        cleanMiscSearchCriteria(dropdownFundID);
        }
    }

    var isinCode = document.getElementById('ISINCode');
    if (isinCode != null) {
        isinCode.onchange = function() {
        cleanMiscSearchCriteria(dropdownFundID);
        }
    }

    var submitButton = document.getElementById('prod_submit');
    if (submitButton != null) {
        submitButton.onclick = function() {

            if (dropdownFundsList.selectedIndex > 0) {
                OpenFancyDetails(dropdownFundsList.options[dropdownFundsList.selectedIndex].value);
            }
            else {
                $('#funds').submit();
            }
        }
    }

    var submitButtonPopup = document.getElementById('prod_submit_popup');
    if (submitButtonPopup != null) {
        submitButtonPopup.onclick = function() {

            if (dropdownFundsList.selectedIndex > 0) {
                OpenFancyDetails(dropdownFundsList.options[dropdownFundsList.selectedIndex].value);
            }
            else {
                $('#fundspopup').submit();
            }
        }
    }
};




/* Search Block
/* Search ComboBox : AssetClass
*************************************/
var initFundsAdvancedSearchArea = function(formId) {

    var dropdownAssetList = document.getElementById('dropdown_asset_funds_list');
    var subAssetDropList = document.getElementById('dropdown_subasset_funds_list');
    var specificityDropList = document.getElementById('dropdown_specificity_funds_list');
    var universDropList = document.getElementById('dropdown_univers_funds_list');
    var minimumTermDropList = document.getElementById('dropdown_minterm_funds_list');

    if (dropdownAssetList == null) {
        return;
    }

    if (dropdownAssetList.selectedIndex == 0) {
        subAssetDropList.selectedIndex = 0;
        subAssetDropList.disabled = true;
        specificityDropList.selectedIndex = 0;
        specificityDropList.disabled = true;
    }
    if (subAssetDropList.selectedIndex == 0) {
        specificityDropList.disabled = true;
    }

    dropdownAssetList.onchange = function() {
        cleanMiscSearchCriteria('');
        $(formId).submit();
    }

    subAssetDropList.onchange = function() {
        cleanMiscSearchCriteria('');

        if (subAssetDropList.selectedIndex > 0) {
            // specificityDropList.selectedIndex = 0;
            specificityDropList.disabled = false;
        }
        else {
            // specificityDropList.selectedIndex = 0;
            specificityDropList.disabled = true;
        }
        $(formId).submit();
    }

    specificityDropList.onchange = function() {
        cleanMiscSearchCriteria('');
        $(formId).submit();
    }

    universDropList.onchange = function() {
        cleanMiscSearchCriteria('');
        $(formId).submit();
    }

    minimumTermDropList.onchange = function() {
        cleanMiscSearchCriteria('');
        $(formId).submit();
    }

};

/* Advanced Search Block
*************************************/
var cleanSearchCriteria = function() {

    cleanMiscSearchCriteria('');
    var dropdownAssetList = document.getElementById('dropdown_asset_funds_list');
    if (dropdownAssetList != null)
        dropdownAssetList.selectedIndex = 0;
    var subAssetDropList = document.getElementById('dropdown_subasset_funds_list');
    if (subAssetDropList != null)
        subAssetDropList.selectedIndex = 0;
};


/* Advanced Search Block
*************************************/
var cleanMiscSearchCriteria = function(dropdownFundID) {

    if ( dropdownFundID!= '' && $('#' + dropdownFundID)) {
        var dropdownFunds = document.getElementById(dropdownFundID);
        dropdownFunds.selectedIndex = 0;
    }
    var pageNumber = document.getElementById('Page');
    var sortBy = document.getElementById('SortBy');
   
    if (pageNumber != null)
        pageNumber.value = 0;
    if (sortBy != null)
        sortBy.value = '';
};

/* Advanced Search Block
*************************************/
var lipperSearchList = function() {
    var lipper = document.getElementById('Lipper');
    if (lipper != null && lipper.value > 0) {
        var lipperID = '#lipper' + lipper.value;
        $(lipperID).attr('src', rolloverNewImg($(lipperID).attr('src')));
    }
};

function setLipperRatting(lipperValue, formId) {
    var lipper = document.getElementById('Lipper');
    lipper.value = lipperValue;

    for (i = 0; i <= 4; i++) {
        var lipperID = '#lipper' + i;
        if (i != lipper.value) {
            $(lipperID).attr('src', rolloverOldimage($(lipperID).attr('src')));
        }
    }
    if ($(formId != '' && '#' + formId))
        $('#' + formId).submit();
}



function OpenFancyDetails(ISINCode) {
    /*$.ajax({
    type: "GET",
    url: "http://ppu.am.natixis.com/fr-FR/NAM/FR0010152967",
    data: $(this).serializeArray(),
    success: function(data) {
    $.fancybox(data);
    }
    });*/

    var URL = "http://ppu.am.natixis.com/fr-FR/NAM/" + ISINCode;

    $.fancybox({
        type: 'iframe',
        'width': 910,
        'height': '90%',
        'autoScale': false,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'hideOnOverlayClick': true,
        'overlayColor': '#000',
        href: URL
    });

    return false;

}

/*
* Debug mode
*/
function ViewDetails(fundId, ISINCode) {
    $.get(
                    "/fr-FR/PortFolio/FundPopupView?fundId=" + fundId + "&ISINCode=" + ISINCode,
                    function(htmlResult) {
                        $("#FundDetails").remove(); //In case this is the second time they've requested the dialog.
                        $("#container").append(htmlResult);
                        $("#FundDetails").dialog({
                            width: 700,
                            height: 500,
                            modal: true
                        });
                    }
                );
    return false; //To keep the default behavior of the anchor tag from occuring.
}
