/*------------------------
[custom JS styles]

Project: eventIMPACTS
Last change: 21.01.2010
Assigned to: Felicity Evans, Lance Cooper
-------------------------*/

$(document).ready(function() {
	
	externalLinks();
	
    //detect for ie6 and target function for this browser
    var ie6 = document.getElementById && document.all && (navigator.appVersion.indexOf("MSIE 6.") >= 0);
    if (ie6) {
        DD_belatedPNG.fix('#wrapper .extraColInner,#wrapper .extraCol,p.more,p.more a');
    }
    //$('body').addClass('js');
	
	// add print link
	if($(".breadcrumb").size()){
		$(".breadcrumb").append('<p class="print"><a href="javascript:window.print();">Print page</a></p>');
	}
	   
   
    // Glossary and Form tooltips
    function addToolTip(toolTipItems) {
        if (jQuery('html').attr('dir') == 'rtl') { yOffset = -0; }
        else { yOffset = -189; };
        xOffset = -21;
        toolTipItems.hover(function(e) {
            if ($(this).attr('class') == "form-tip") {
                yOffset = 10;
            }
            this.t = this.title;
            this.title = "";
            jQuery("body").append("<span class='toolTip'><strong>Definition:</strong> " + this.t + "</span>");
            jQuery(".toolTip").css("top", (e.pageY - xOffset) + "px").css("left", (e.pageX + yOffset) + "px").fadeIn("fast");
        }, function() {
            this.title = this.t;
            jQuery(".toolTip").remove();
        });
        toolTipItems.click(function(e) {
            this.title = this.t;
            jQuery(".toolTip").remove();
        });
        toolTipItems.mousemove(function(e) {
            jQuery(".toolTip").css("top", (e.pageY - xOffset) + "px").css("left", (e.pageX + yOffset) + "px");
        });
    };
    toolTipItems = jQuery(".glossary");
    addToolTip(toolTipItems);

    jQuery(".form-tip").stickyTooltip();

    // Floating login form
    $('#download').show();
    var slideOutDownload = function() {
        if (ie6) { $("#download").css('width', '252px').css('right', '-252px'); }
        else {
            $("#download .download-contentInner").css('background', 'transparent url(/images/interface/download-tab-bg.png) no-repeat left top');
            $("#download .download-content").css('background', 'transparent url(/images/interface/download-tab-bg-btm.png) bottom left no-repeat');
        }
        $("#download").animate({ right: '0' }, { duration: 300 }).addClass('expanded');
    }
    var slideInDownload = function() {
        $("#download").animate({ right: '-210px' }, 300, slideInComplete, false).removeClass('expanded');
    }
    var slideInComplete = function() {
        if (ie6) { $('#download').css('right', '0').css('width', '40px').css('overflow', 'hidden'); }
        else { $("#download .download-content,#download .download-contentInner").css('background', 'none'); }
    }
    if (ie6) { } else {
        if ($("#download").hasClass("expanded")) { } else {
            $("#download .download-content,#download .download-contentInner").css('background', 'transparent none');
        }
    }
    $('#download .download-title,a[rel=login]').click(function() {
        if ($('#download').hasClass('expanded')) { slideInDownload(); }
        else { slideOutDownload(); }
        return false;
    });
    $(".internal #wrapper").click(function() {
        if ($('#download').hasClass('expanded')) { slideInDownload(); }
    });
    if (ie6) {
        $(window).scroll(function() {
            $("#download").css("top", $(window).scrollTop() + "px");
        });
    }

    //Colorbox
    $("a.colorbox").colorbox();
    $("a.colorbox").append('<span>Enlarge image</span>');

});

/* functions + plugins */

//stickyTooltip
(function($) {
    $.fn.stickyTooltip = function(options) {
        var opts = $.extend({}, $.fn.stickyTooltip.defaults, options);
        var $els = this, ref, urrentTitle = "", hoverTimeout, ignoreHover = false;

        return this.each(function(e) {
            // access defaults/options via: opts.customValue
            $(this).hover(function(self) {
                if (!ignoreHover) {
                    hoverTimeout = setTimeout(function() { createNewTooltip(e, self.currentTarget); }, 200);
                }
            }, function() {
                clearTimeout(hoverTimeout);
                ignoreHover = false;
            })
            $(this).click(function() { return false; })
        });
        function setRef() { ref = $("#toolTip").attr("class").split("tt")[1]; }
        function createNewTooltip(e, self) {
            closeTooltip();
            ref = e;
            currentTitle = self.title;
            self.title = "";
            $("body").append("<span id='toolTip' class='tt" + e + "'><span><a href='#' class='closeBtn'>Close</a><strong>Definition:</strong> " + currentTitle + "</span></span>");
            $("#toolTip").css("top", Math.round($(self).offset().top - ($("#toolTip").height() / 2) + 10) + "px").css("left", Math.round($(self).offset().left + 30) + "px").fadeIn("fast");
            $("#toolTip .closeBtn").click(function() {
                closeTooltip();
                ignoreHover = false;
                return false;
            });
            function addContent() {
            }
            function removeContent() {
                closeTooltip();
            }
            var toolTipConfig = {
                interval: 0,
                sensitivity: 4,
                over: addContent,
                timeout: 200,
                out: removeContent
            };
            $("#toolTip").hoverIntent(toolTipConfig);
        }       
        function closeTooltip() {
            if ($("#toolTip").size()) {
                setRef();
                $els.filter(":eq(" + ref + ")").attr("title", currentTitle);
                currentTitle = "";
                $("#toolTip").remove();
                ref = null;
                ignoreHover = false;
            }
        }
    }
    $.fn.stickyTooltip.defaults = {}

})(jQuery);


/* ---- START OPEN EXTERNAL LINKS IN A NEW WINDOW ---- */

function externalLinks() {
    if (!document.getElementsByTagName) {
        return;
    }
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") &&
            anchor.getAttribute("rel") == "external" || anchor.getAttribute("rel") == "pdf") {
			anchor.target = "_blank";
			anchor.title = "This link will open in a new window";
		}
    }
}
/* ---- END OPEN EXTERNAL LINKS IN A NEW WINDOW ---- */
