/*global jQuery, $, $$, addEventListener, alert, blur, clearInterval, clearTimeout, close, closed, confirm, console, Debug, defaultStatus, document, window, event, events, focus, frames, getComputedStyle, history, Image, length, location, moveBy, moveTo, name, navigator, onblur: true, onerror: true, onfocus: true, onload: true, onresize: true, onunload: true, open, opener, opera, Option, parent, print, prompt, resizeBy, resizeTo, screen, scroll, scrollBy, scrollTo, setInterval, setTimeout, status, top, XMLHttpRequest */


/*  DGS : Digitas Javascript Object, version 0.0.0.1
 *--------------------------------------------------------------------------*/


var DGS = {
    ID: "DGS Javascript Object",
    Version: "0.0.0.1",
    Browser: {
        IE: !!(window.attachEvent && navigator.userAgent.indexOf('Opera') === -1),
        Opera: navigator.userAgent.indexOf('Opera') > -1,
        WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
        Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') === -1,
        MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
    },
    Debugg: !!(window.console)
};

(function($) {
    jQuery.fn.miniModal = function(options) {

        // ----------------------------
        // ! DEFAULT SETTINGS
        // ----------------------------
        var defaults = {
            containerAttrs: {
                className: "container_16"
            },
            maskAttrs: {
                id: "modal-mask"
            },
            loaderAttrs: {
                id: "modal-loader"
            },
            windowAttrs: {
                id: "modal-window"
            },
            closeAttrs: {
                id: "modal-close"
            },
            closeText: "close",

            onBeforeStart: function() {},
            onComplete: function() {
            // alert("Content Loaded \n(Ajax Callback)\n(Modal ready for new javascript features)");
            }

        };
        var settings = $.extend(true, {},
            defaults, options);

        return this.each(function() {

            // ----------------------------
            // ! Plugin Start here...
            // ----------------------------

            function showModalDialog(event) {
                event.preventDefault();
                event.stopPropagation();

                if (jQuery.browser.msie && jQuery.browser.version < 7) {
                    $("select").css("visibility","hidden");
                }
				
                // ----------------------------
                // ! references for href and hash values
                // ----------------------------
                var modalHref = jQuery(this).attr("href").split("#")[0];
                //var modalResponse = jQuery(this).attr("hash");
                // ----------------------------
                // ! references for mask, loader window and close button
                // ----------------------------
                var modalMask = jQuery("<div />").attr(settings.maskAttrs);
                var modalLoader = jQuery("<div />").attr(settings.loaderAttrs);
                var modalWindow = jQuery("<div />").attr(settings.windowAttrs);
                //var modalClose= jQuery("<a href='#' />").attr(settings.closeAttrs).text(settings.closeText);
                // ----------------------------
                // ! Append mask, loader and window in first body container
                // ----------------------------
                jQuery("body").prepend(modalMask);
                jQuery(modalMask).after(modalLoader);
                modalLoader.after(modalWindow);

                // ----------------------------
                // ! Trigger Request in callback mask animation
                // ----------------------------
                jQuery(modalMask).animate({
                    opacity: 0.75
                },
                200, "swing",
                function() {

                    jQuery.ajax({
                        type: "GET",
                        url: modalHref,
                        dataType: "html",
                        processData: "false",

                        beforeSend: function(XMLHttpRequest) {
                            // ----------------------------
                            // ! trigger onBeforeStart plugin method
                            // ----------------------------
                            settings.onBeforeStart.call(this);

                        },
                        error: function(XMLHttpRequest, textStatus, errorThrow) {
                        },
                        success: function(XMLHttpRequest, textStatus) {
                        //jQuery.log("success modal XMLHttpRequest");
                        },
                        complete: function(XMLHttpRequest, textStatus) {
                            //jQuery.log("complete modal XMLHttpRequest");
                            // ----------------------------
                            // ! append data response to modal window (hidden)
                            // ----------------------------
                            jQuery(XMLHttpRequest.responseText).find("#modal-response").each(function() {

                                jQuery(modalWindow).append(this);
                            //jQuery(this).prepend(modalClose);
                            /* jQuery(modalClose).click(function(event){
								event.preventDefault();
								event.stopPropagation();
								
								
								jQuery(modalWindow).slideUp("slow",function(){
									jQuery(this).remove()
									
								});
								 jQuery(modalMask).slideUp("normal",function(){
									jQuery(this).remove()
								});
							});*/
                            });


                            // ----------------------------
                            // ! get/set modal position / margin
                            // ----------------------------
                            var modalScope = Math.round(((jQuery(modalWindow).height()) / 2) * -1);
                            jQuery(modalWindow).css("margin-top", modalScope);


                            // ----------------------------
                            // ! hide loader
                            // ----------------------------
                            jQuery(modalLoader).animate({
                                opacity: 0
                            },
                            'fast', "swing",
                            function() {

                                jQuery(this).remove();
                            });


                            // ----------------------------
                            // ! show modal window  (visible)
                            // ----------------------------
                            jQuery(modalWindow).animate({
                                opacity: 1
                            },
                            'fast', "swing",
                            function() {

                                // ----------------------------
                                // ! trigger onComplete plugin method
                                // ----------------------------
                                settings.onComplete.call(this);

                            });



                        //jQuery("body").addClass("modalActiveBody");

                        }
                    });

                });

            }


            /* ! bind click event function */
            jQuery(this).bind("click", showModalDialog);


        });

    };
} (jQuery));



// ----------------------------
// ! ///// AGENDA AJAX START //////////
// ----------------------------
function getEventDetails(event) {
    event.preventDefault();
    event.stopPropagation();

    if (jQuery.browser.msie && jQuery.browser.version < 7) {
        $("select").css("visibility","hidden");
    }

    var eventTarget = jQuery(this).attr("href");
    jQuery(".agenda-remote-days td").removeClass("current");
    jQuery(this).parent().addClass("current");
    //console.log(eventTarget);
    jQuery(".agenda-description").fadeOut("fast",
        function() {
            jQuery(this).empty().load(eventTarget + " .agenda-description h2, .agenda-description h3, .agenda-description p",
                function() {

                    jQuery(this).fadeIn();
                });
        });
};
function getNextAgenda(event) {
    event.preventDefault();
    event.stopPropagation();

    var eventTarget = jQuery(this).attr("href");
    //console.log(eventTarget);
    //jQuery("#section .box").remove();
    jQuery("#section").load(eventTarget + " #modal-response");

};





// ----------------------------
// ! ///// AGENDA AJAX END //////////
// ----------------------------


// ----------------------------
// ! ///// DEBUGG START //////////
// ----------------------------
(function($) {
    /* ! Warning !!! set debugg to false in production mode */
    var debugg = false;

    if ((debugg)) {
        jQuery(document).ready(function() {
            $("body").prepend('<div id="debugg" class="box"><h2><a href="#logger" id="toggle-logger">Logger</a></h2><ol id="logger"></ol></div>');
        });
    }
    jQuery.fn.debug = function() {
        return this.each(function() {
            jQuery.log(this);
        });
    };
    jQuery.log = function(message) {
        if ((window.console) && (debugg)) {
            console.debug(message);
            jQuery("#debugg h2").empty().text("Debugg on");
        }
        else {
            jQuery("#debugg ol").append("<li>" + message + "</li>");
        }
    };
} (jQuery));


// ----------------------------
// ! ///// DEBUGG END //////////
// ----------------------------

// ----------------------------
// ! ///// MINI TOGGLE START //////////
// ----------------------------
((function($) {

    jQuery.fn.miniToggle = function(options) {
        // ----------------------------
        // ! DEFAULT SETTINGS
        // ----------------------------
        var defaults = {
            hiddenClass: "hidden",

            params: {
                opacity: "toggle",
                height: "toggle"
            },
            animations: {
                speed: 200,
                easing: "swing"
            },
            catchThis: function() {
                return this.hash;
            //  (ex : '#article-sdt')
            },
            onBeforeStart: function() {},
            onComplete: function() {}

        };
        var settings = $.extend(true, {},
            defaults, options);

        return this.each(function() {

            // ----------------------------
            // ! Plugin Start here...
            // ----------------------------
            function onToggle(event) {

                event.stopPropagation();
                event.preventDefault();

                jQuery(this).toggleClass(settings.hiddenClass);

                var toggleText = jQuery(this).text();
				

                if (toggleText === LABEL_BT_CLOSE) {
                    toggleText = LABEL_BT_OPEN;
                    jQuery(this).text(toggleText);
                } else if(toggleText === LABEL_BT_OPEN){
                    toggleText = LABEL_BT_CLOSE;
                    jQuery(this).text(toggleText);
                };

                var toggleTarget = settings.catchThis.call(this);

                jQuery(toggleTarget).animate(settings.params, settings.animations.speed, settings.animations.easing,
                    function() {

                        // ----------------------------
                        // ! trigger onComplete plugin method
                        // ----------------------------
                        settings.onComplete.call(this);

                    });

                return false;
            }


            jQuery("a[id*=toggle]").live("click", onToggle);



        });
    };

})(jQuery));

// ----------------------------
// ! ///// MINI TOGGLE END //////////
// ----------------------------

// ----------------------------
// ! ///// POPIN FLASH CLOSE FUNCTION //////////
// ----------------------------
function closeMe(event) {
	
    if (jQuery.browser.msie && jQuery.browser.version < 7) {
        $("select").css("visibility","visible");
    }
	
    jQuery("#modal-window").slideUp("slow",
        function() {
            jQuery(this).remove();

        });
    jQuery("#modal-mask").slideUp("normal",
        function() {
            jQuery(this).remove();
        });
    return false;
}

// ----------------------------
// ! ///// DEBUG MENU IE6 //////////
// ----------------------------
function navigationDisplay() {
    if (typeof document.body.style.maxHeight === "undefined") {
        jQuery("ul.nav > li").hover(function() {
            jQuery(this).addClass("activeMenuItem");
        },
        function() {
            jQuery(this).removeClass("activeMenuItem");
        });
    }
}
function updateHiddenList(event){
    event.preventDefault();
    var sourceText = jQuery(this).text();
    var sourceID = jQuery(this).attr("id");
    jQuery("#hidden-list").val(sourceID);
    jQuery("#domain").val(sourceText);
    jQuery("#close-btn").trigger("click");
}

jQuery(document).ready(function(event) {
    $(".searchResult").click(function(){
        document.location = $(this).find("a").attr("href");
    });
    jQuery("a[id*=toggle]").miniToggle();
	
    jQuery("a[class*=toggle]").trigger("click");

    // AGENDA LOAD  EVENTS DETAILS (AJAX)
    jQuery(".agenda-remote-days a").live("click", getEventDetails);
    // AGENDA LOAD  NEXT/PREVIOUS AGENADA (AJAX)
    jQuery(".agenda-remote-months a").live("click", getNextAgenda);


    jQuery(".modal-media a, a.modal-media, .autopopin").miniModal({
        // ----------------------------
        // ! ///// ON COMPLETE AJAX CALLBACK - START//////////
        // ----------------------------
        onComplete: function() {

        }
    // ----------------------------
    // ! ///// ON COMPLETE AJAX CALLBACK - END//////////
    // ----------------------------
    });

    // launch autopopin by simulating the click
    jQuery(".autopopin").trigger("click");
    //  Live events close button
    jQuery("#close-btn").live("click", closeMe);
    //  Live events update hidden input and disabled input
    jQuery("#modal-window .list a").live("click", updateHiddenList);

});


jQuery(window).load(function(event) {

    // ! IE6 Menu debug
    navigationDisplay();

});

