﻿

/// <reference name="MicrosoftAjax.js"/>
var edl_scripts = new Object();
edl_scripts.init = function() {
    this.keyCodes = new Object();
    this.keyCodes.Down = 40;
    this.keyCodes.Up = 38;
    this.keyCodes.Enter = 13;
    //GoogleMaps
    this.geocoder;
    this.map;

    this.start();
}
edl_scripts.start = function() {
    if (typeof ($) != 'undefined' && $(document) && $(document).ready) {
        $(document).ready(function() {
            edl_scripts.LoadMenuEvents();
            edl_scripts.LoadLinkMouseEvents();
            edl_scripts.PrepareBigImage();
            edl_scripts.ChangeTitleInput();
            edl_scripts.TabsOnRDF();
            edl_scripts.GmapsOnRDF();
            edl_scripts.LoadCFPSelectEvents();
            edl_scripts.LoadRDFSelectEvents();
            //edl_scripts.LoadSlideshowGalleryView();
        });
        return true;
    }
    else {
        //alert("error: jquery not loaded !");
        return false;
    }
}
edl_scripts.LoadLinkMouseEvents = function() {
    if ($(".link_in_cart").size() > 0) {
        $(".link_in_cart").each(function() {
            $(this).mouseover(function(e) {
                $(this).closest(".button_in_cart_image").css("background-image", "url(" + siteBase + "Content/img/add_in_cart_on.jpg)");
                return true;
            });
            $(this).mouseout(function(e) {
                //EDL_FrontOffice
                $(this).closest(".button_in_cart_image").css("background-image", "url(" + siteBase + "Content/img/add_in_cart.jpg)");
                return true;
            });
            $(this).click(function(e) {
                $(this).closest('form').submit();
                return true;
            });
        });
    }
}
edl_scripts.PrepareBigImage = function() {
    var htmlModal = '' +
    '<div id="modal_big_image">' +
        '<div id="modal_popup">' +
            '<div id="modal_header">' +
                '<p class="right_align"><input id="CloseButton" type="button" value="Quitter"/></p>' +
            '</div>' +
            '<div id="modal_content">' +
                '<img class="imageCouverture" src="" alt="Loading..." />' +
            '</div>' +
            '<div id="modal_footer">' +
                '<p></p>' +
            '</div>' +
        '</div>' +
    '</div>';

    if ($("#produit_detail").size() == 1) {
        $(htmlModal).prependTo('body');
        $("#CloseButton").click(function(e) {
            $("#modal_big_image").slideUp("slow");
            return true;
        });
        $("#modal_content img").load(function() {
            $(this).css('display', 'none'); // since .hide() failed in safari
            $(this).fadeIn(1000);
        });
        $("#modal_big_image").hide();
    }
}

edl_scripts.GetBigImage = function(lienImage, titreImage) {
    if ($("#produit_detail").size() == 1) {
        var htmlTitre = "<p>" + titreImage + "</p>";
        $("#modal_footer p").replaceWith(titreImage);
        //Affichage d'une div gris foncé transparent faisant toute la page
        $("#modal_big_image").slideDown();
        $("#modal_footer").hide();
        $("#modal_popup").hide();
        $("#modal_popup").show();
        //loading image + effet
        $("#modal_content img").attr('src', lienImage);

        //Position de l'image
        $("#modal_big_image").height($(document).height());
        $("#modal_popup").css("margin-top", $(window).height() / 10 + "px");
        $("#modal_footer").slideDown("slow");
    }
}
edl_scripts.LoadMenuEvents = function() {
    if ($(".slide_down").size() > 0) {
        //au click
        $(".slide_down").click(function() {
            $(this).next().slideToggle("fast");
            return false;
        });
        $(".slide_down").next().hide();
    }
    //        //Au passage de la souris déploiement
    //        $(".slide_down").mouseenter(function() {
    //            $(this).next().slideDown("fast");
    //        });
    //        //sur sortie on rétracte la liste
    //        $(".slide_down").parent().mouseleave(function() {
    //            $(this).children("div").slideUp("fast");
    //        });
}

edl_scripts.TabsOnRDF = function() {
    if ($("#rdf_client").size() > 0) {
        //Gestion d'Onglet
        $("#tabs").tabs();
        $("#tabs").tabs("option", "fx", { height: 'toggle', opacity: 'toggle' });

    }
}

edl_scripts.ChangeTitleInput = function() {
    $('input[type=text][title!=""]').each(function() {
        if ($.trim($(this).val()) == '') $(this).val($(this).attr('title'));
        if ($(this).val() == $(this).attr('title')) $(this).addClass('exampleText');
    }).focus(switchText).blur(switchText);

    $('form').submit(function() {
        $(this).find('input[type=text][title!=""]').each(function() {
            if ($(this).val() == $(this).attr('title')) $(this).val('');
        });
    });
}

edl_scripts.GmapsOnRDF = function() {
    if ($("#map").size() > 0 && $("#Address").size() > 0) {

        loadMap($("#Address").val());
    }
}

function switchText() {
    if ($(this).val() == $(this).attr('title'))
        $(this).val('').removeClass('exampleText');
    else if ($.trim($(this).val()) == '')
        $(this).addClass('exampleText').val($(this).attr('title'));
}


// On page load, call this function
function loadMap(address) {
    // Create new map object
    this.map = new GMap2(document.getElementById("map"));
    $("#map").hide();
    // Create new geocoding object
    this.geocoder = new GClientGeocoder();

    // Retrieve location information, pass it to addToMap()
    this.geocoder.getLocations(address, addToMap);
}

// This function adds the point to the map
function addToMap(response) {
    // Retrieve the object
    if (response.Placemark != null) {
        place = response.Placemark[0];
        $("#map").show();
        // Retrieve the latitude and longitude
        point = new GLatLng(place.Point.coordinates[1],
                              place.Point.coordinates[0]);

        // Center the map on this point
        this.map.setCenter(point, 13);

        // Select a map type which supports aerial imagery
        this.map.setMapType(G_HYBRID_MAP);

        //Custom display options
        var customUI = map.getDefaultUI();
        customUI.controls.scalecontrol = false;
        this.map.setUI(customUI);

        // the map type collection
        this.map.enableRotation();

        // Create a marker
        marker = new GMarker(point);

        // Add the marker to map
        this.map.addOverlay(marker);
    }
}

edl_scripts.LoadCFPSelectEvents = function() {
    if ($("#GroupeThemesArchivesCFP").size() > 0 && $("#ThemesArchivesCFP").size() > 0) {
        $("#groupTheme").change(function() {
            $("#groupeTheme").val($("#groupTheme").val());
            $("#SubmitUpdateThemesArchivesCFP").click(); //Soumission ajax du formulaire
        });
    }
}
edl_scripts.LoadRDFSelectEvents = function() {
    if ($("#FormUpdateListCatSousRDF").size() > 0 && $("#UpdateListCatSousRDF").size() > 0) {
        $("#theme").change(function() {
            $("#SubmitListCatSousRDF").click(); //Soumission ajax du formulaire
        });
    }
}
edl_scripts.CopyInputBeforPost = function() {
    $("#cat").val($("#theme").val());
    $("#catsous").val($("#sousTheme").val());
    $("#submitFormOrganisme").submit();
}

edl_scripts.LoadSlideshowGalleryView = function() {
    
}

edl_scripts.init();
