﻿/*
 * brokenImage: a jQuery plugin
 *
 * brokenImage is a jQuery plugin that is able to detect and replace images
 * that are either broken or are taking a long time to load.  The default
 * replacement is a transparent GIF (no extra image file required).  The
 * replacement image and the timeout for slow-loading images are configurable.
 *
 * For usage and examples, visit:
 * http://github.com/alexrabarts/jquery-brokenimage/tree/master
 *
 * Licensed under the MIT:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright (c) 2008 Stateless Systems (http://statelesssystems.com)
 *
 * @author   Alex Rabarts (alexrabarts -at- gmail -dawt- com)
 * @requires jQuery v1.2 or later
 * @version  0.2
 */

(function ($) {
  $.extend($.fn, {
    brokenImage: function (options) {
      var defaults = {
        timeout: 5000
      };

      options = $.extend(defaults, options);

      return this.each(function () {
        // Replace the image with a placeholder if:
        // a. loading fails with an error event or;
        // b. loading takes longer than timeout
        var image = this;

        $(image).bind('error', function () {
          insertPlaceholder();
        });

        setTimeout(function () {
          var test = new Image(); // Virgin image with no styles to affect dimensions
          test.src = image.src;

          if (test.height === 0) {
            insertPlaceholder();
          }
        }, options.timeout);

        function insertPlaceholder() {
          options.replacement ? image.src = options.replacement : $(image).css({visibility: 'hidden'});
        }
      });
    }
  });
})(jQuery);


var Blueprint = {};
Blueprint.go = function (e) {
    var destination = e.options[e.selectedIndex].value;
    if (destination && destination != 0) location.href = destination;
};
Blueprint.formCheck = function () {
    var forms = $("#node-form>div>div>#edit-submit,#comment-form>div>#edit-submit,#user-register>div>#edit-submit");
    $('<div id="saving"><p class="saving">Saving&hellip;</p></div>').insertAfter(forms);
    forms.click(function () {
        $(this).siblings("input:submit").hide();
        $(this).hide();
        $("#saving").show();
        var notice = function () {
            $('<p id="saving-notice">Not saving? Wait a few seconds, reload this page, and try again. Every now and then the internet hiccups too :-)</p>').appendTo("#saving").fadeIn();
        };
        setTimeout(notice, 24000);
    });
};
if (Drupal.jsEnabled) {
    $(document).ready(Blueprint.formCheck);
}(function ($) {
    $.fn.hoverIntent = function (f, g) {
        var cfg = {
            sensitivity: 7,
            interval: 100,
            timeout: 0
        };
        cfg = $.extend(cfg, g ? {
            over: f,
            out: g
        } : f);
        var cX, cY, pX, pY;
        var track = function (ev) {
            cX = ev.pageX;
            cY = ev.pageY;
        };
        var compare = function (ev, ob) {
            ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
            if ((Math.abs(pX - cX) + Math.abs(pY - cY)) < cfg.sensitivity) {
                $(ob).unbind("mousemove", track);
                ob.hoverIntent_s = 1;
                return cfg.over.apply(ob, [ev]);
            } else {
                pX = cX;
                pY = cY;
                ob.hoverIntent_t = setTimeout(function () {
                    compare(ev, ob);
                }, cfg.interval);
            }
        };
        var delay = function (ev, ob) {
            ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
            ob.hoverIntent_s = 0;
            return cfg.out.apply(ob, [ev]);
        };
        var handleHover = function (e) {
            var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
            while (p && p != this) {
                try {
                    p = p.parentNode;
                } catch (e) {
                    p = this;
                }
            }
            if (p == this) {
                return false;
            }
            var ev = jQuery.extend({}, e);
            var ob = this;
            if (ob.hoverIntent_t) {
                ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
            }
            if (e.type == "mouseover") {
                pX = ev.pageX;
                pY = ev.pageY;
                $(ob).bind("mousemove", track);
                if (ob.hoverIntent_s != 1) {
                    ob.hoverIntent_t = setTimeout(function () {
                        compare(ev, ob);
                    }, cfg.interval);
                }
            } else {
                $(ob).unbind("mousemove", track);
                if (ob.hoverIntent_s == 1) {
                    ob.hoverIntent_t = setTimeout(function () {
                        delay(ev, ob);
                    }, cfg.timeout);
                }
            }
        };
        return this.mouseover(handleHover).mouseout(handleHover);
    };
})(jQuery);
(function($) {
    $.fn.listmenu = function(options) {
        var opts = $.extend({}, $.fn.listmenu.defaults, options);
        var alph = ['_', 'ا', 'ب', 'پ', 'ت', 'ث', 'ج', 'چ', 'ح', 'خ', 'د', 'ذ', 'ر', 'ز', 'ژ', 'س', 'ش', 'ص', 'ض', 'ط', 'ظ', 'ع', 'غ', 'ف', 'ق', 'ک', 'گ', 'ل', 'م' , 'ن' , 'و' , 'ه' , 'ی' , '-'];
        return this.each(function() {
            var $wrapper, list, $list, $letters, letters = {},
                $letterCount, id, $menu, colOpts = $.extend({}, $.fn.listmenu.defaults.cols, opts.cols),
                onNav = false,
                onMenu = false,
                currentLetter = '';
            id = this.id;
            $list = $(this);

            function init() {
                $list.css('visibility', 'hidden');
                setTimeout(function() {
                    $list.before(createWrapperHtml());
                    $wrapper = $('#' + id + '-menu');
                    $wrapper.append(createLettersHtml());
                    $letters = $('.lm-letters', $wrapper).slice(0, 1);
                    if (opts.showCounts) $letterCount = $('.lm-letter-count', $wrapper).slice(0, 1);
                    $wrapper.append(createMenuHtml());
                    $menu = $('.lm-menu', $wrapper);
                    populateMenu();
                    if (opts.flagDisabled) addDisabledClass();
                    bindHandlers();
                    if (!opts.includeNums) $('._', $letters).remove();
                    if (!opts.includeOther) $('.-', $letters).remove();
                    $(':last', $letters).addClass('lm-last');
                    $wrapper.show();
                }, 50);
            }

            function setLetterCountTop() {
                $letterCount.css({
                    top: $('.a', $letters).slice(0, 1).offset({
                        margin: false,
                        border: true
                    }).top - $letterCount.outerHeight({
                        margin: true
                    })
                });
            }

            function addDisabledClass() {
                for (var i = 0; i < alph.length; i++) {
                    if (letters[alph[i]] == undefined) $('.' + alph[i], $letters).addClass('lm-disabled');
                }
            }

            function populateMenu() {
                var gutter = colOpts.gutter,
                    cols = colOpts.count,
                    menuWidth, colWidth;
                if (opts.menuWidth) menuWidth = opts.menuWidth;
                else menuWidth = $('.lm-letters', $wrapper).width() - ($menu.outerWidth() - $menu.width());
                colWidth = (cols == 1) ? menuWidth : Math.floor((menuWidth - (gutter * (cols - 1))) / cols);
                $menu.width(menuWidth);
                $list.width(colWidth);
                var letter, outerHeight;
                $list.children().each(function() {
                    str = $(this).text().replace(/\s+/g, '');
                    if (str != '') {
                        firstChar = str.slice(0, 1).toLowerCase();
                        //if (/\W/.test(firstChar)) firstChar = '-';
                        if (!isNaN(firstChar)) firstChar = '_';
                    }
                    outerHeight = $(this).outerHeight();
                    if (letters[firstChar] == undefined) {
                        letters[firstChar] = {
                            totHeight: 0,
                            count: 0,
                            colHeight: 0,
                            hasMenu: false
                        };
                    }
                    letter = letters[firstChar];
                    letter.totHeight += outerHeight;
                    letter.count++;
                    $.data($(this)[0], id, {
                        firstChar: firstChar,
                        height: outerHeight
                    });
                });
                $.each(letters, function() {
                    this.colHeight = (this.count > 1) ? Math.ceil(this.totHeight / cols) : this.totHeight;
                });
                var $this, data, iHeight, iLetter, cols = {},
                    c;
                var tagName = $list[0].tagName.toLowerCase();
                $list.children().each(function() {
                    $this = $(this);
                    data = $.data($this.get(0), id);
                    iLetter = data.firstChar;
                    iHeight = data.height;
                    if (!letters[l = iLetter].hasMenu) {
                        $menu.append('<div class="lm-submenu ' + iLetter + '" style="display:none;"></div>');
                        letters[iLetter].hasMenu = true;
                    }
                    if (cols[iLetter] == undefined) cols[iLetter] = {
                        height: 0,
                        colNum: 0,
                        itemCount: 0,
                        $colRoot: null
                    };
                    c = cols[iLetter];
                    c.itemCount++;
                    if (c.height == 0) {
                        c.colNum++;
                        $('.lm-submenu.' + iLetter, $menu).append('<div class="lm-col c' + c.colNum + '"><' + tagName + ((tagName == 'ol') ? ' start="' + c.itemCount + '"' : '') + ' id="lm-' + id + '-' + iLetter + '-' + c.colNum + '" class="lm-col-root"></' + tagName + '></div>');
                    }
                    $('#lm-' + id + '-' + iLetter + '-' + c.colNum).append($(this));
                    c.height += iHeight;
                    if (c.height >= letters[iLetter].colHeight) c.height = 0;
                });
                $.each(letters, function(idx) {
                    if (this.hasMenu) {
                        $('.lm-submenu.' + idx + ' .lm-col', $menu).css({
                            'width': colWidth,
                            'float': 'right'
                        });
                        $('.lm-submenu.' + idx + ' .lm-col:not(:last)', $menu).css({
                            'marginRight': gutter
                        });
                    }
                });
                $menu.append('<div class="lm-no-match" style="display:none">' + opts.noMatchText + '</div>');
                $list.remove();
            }

            function getLetterCount(el) {
                var letter = letters[$(el).attr('class').split(' ')[0]];
                return (letter != undefined) ? letter.count : 0;
            }

            function hideCurrentSubmenu() {
                if (currentLetter != '') $('.lm-submenu.' + currentLetter, $menu).hide();
                $('.lm-no-match', $menu).hide();
            }

            function bindHandlers() {
                if (opts.showCounts) {
                    $wrapper.mouseover(function() {
                        setLetterCountTop();
                    });
                }
                $('a', $letters).click(function() {
                    $(this).blur();
                    return false;
                });
                $letters.hover(function() {
                    onNav = true;
                }, function() {
                    onNav = false;
                    setTimeout(function() {
                        if (!onMenu) {
                            $('a.lm-selected', $letters).removeClass('lm-selected');
                            $('.lm-menu', $wrapper).hide();
                            hideCurrentSubmenu();
                            currentLetter = '';
                        }
                    }, 10);
                });
                $('a', $letters).mouseover(function() {
                    var count = getLetterCount(this);
                    var $this = $(this);
                    if (opts.showCounts) {
                        var left = $this.position().left;
                        var width = $this.outerWidth({
                            margin: true
                        });
                        if (opts.showCounts) $letterCount.css({
                            left: left,
                            width: width + 'px'
                        }).text(count).show();
                    }
                    var newLetter = $this.attr('class').split(' ')[0];
                    if (newLetter != currentLetter) {
                        if (currentLetter != '') {
                            hideCurrentSubmenu();
                            $('a.lm-selected', $letters).removeClass('lm-selected');
                        }
                        $this.addClass('lm-selected');
                        if (count > 0) $('.lm-submenu.' + newLetter, $wrapper).show();
                        else $('.lm-no-match', $wrapper).show();
                        if (currentLetter == '') $('.lm-menu', $wrapper).show();
                        currentLetter = newLetter;
                    }
                });
                $('a', $letters).mouseout(function() {
                    if (opts.showCounts) $letterCount.hide();
                });
                $menu.hover(function() {
                    onMenu = true;
                }, function() {
                    onMenu = false;
                    setTimeout(function() {
                        if (!onNav) {
                            $('a.lm-selected', $letters).removeClass('lm-selected');
                            $('.lm-menu', $wrapper).hide();
                            hideCurrentSubmenu();
                            currentLetter = '';
                        }
                    }, 10);
                });
                if (opts.onClick != null) {
                    $menu.click(function(e) {
                        var $target = $(e.target);
                        opts.onClick($target);
                        return false;
                    });
                }
            }

            function createLettersHtml() {
                var html = [];
                for (var i = 1; i < alph.length; i++) {
                    if (html.length == 0) html.push('<a class="_" href="#">0-9</a>');
                    html.push('<a class="' + alph[i] + '" href="#">' + ((alph[i] == '-') ? '...' : alph[i].toUpperCase()) + '</a>');
                }
                return '<div class="lm-letters">' + html.join('') + '</div>' + ((opts.showCounts) ? '<div class="lm-letter-count" style="display:none; position:absolute; top:0; left:0; width:20px;">0</div>' : '');
            }

            function createMenuHtml() {
                return '<span style="clear:both;"></span><div class="lm-menu"></div>';
            }

            function createWrapperHtml() {
                return '<div id="' + id + '-menu" class="lm-wrapper"></div>';
            }
            init();
        });
    };
    $.fn.listmenu.defaults = {
        includeNums: true,
        includeOther: false,
        flagDisabled: true,
        noMatchText: 'دوره ای یافت نشد .',
        showCounts: true,
        menuWidth: null,
        cols: {
            count: 4,
            gutter: 40
        },
        onClick: null
    };
})(jQuery);
$(document).ready(function () {
	
	
	
	
	
    $('body').addClass('wait4js');
    var searchField = $('#searchritntid');
    searchField.focus(function () {
        var div = $('div.searchritntid').fadeIn();
    });
    var searchField2 = $('#google-cse-searchbox-form #edit-query');
    searchField2.focus(function () {
        var div = $('div.searchritntid').fadeIn('fast');
    });
    $('.sf-menu a').removeAttr('title');
    $('.col-left a').removeAttr('title');
    $('a.top').mouseenter(function () {
        $('#submenucontainer').slideDown();
        $(this).addClass('submenuitemhover');
        $('.top').not(this).removeClass('submenuitemhover');
        $('.selected').css({
            'background-color': 'transparent',
            'color': '#fff'
        });
        var menuId = $(this).attr('id');
        $('div.submenuitem').hide();
        $('div.' + menuId).show()
    });
	
	$('.directoryphoto').brokenImage({replacement: '/sites/default/files/images/dirnophoto.jpg'});
	
	/*
	$('#messages').slideDown("slow"); 
	$('#messages').click(function () {
        $(this).slideUp();
    });
	*/
    $('#s_or_fs-s').attr('checked', true);
    $('#s_or_fs-s').click(function () {
        $('#deptdirform').attr('action', 'https://www.ntid.rit.edu/directory/stdirkeyw.cfm');
    });
	
    $('#s_or_fs-fs').click(function () {
        $('#deptdirform').attr('action', 'https://www.ntid.rit.edu/directory/fsdirkeyw.cfm');
    });
    $('#peoplesearch_skeyword').focus(function () {
        $('div.peoplesearch_notice').fadeIn('fast');
    });
    $('#searchNTIDonly').click(function () {
        $('#ntidsearchform').show();
        $('#metasearchform').hide();
    });
    $('#searchRITsite').click(function () {
        $('#ntidsearchform').hide();
        $('#metasearchform').show();
    });
    $(function () {
        $('#deptDir').listmenu({
            includeNums: false,
            includeOther: false,
            flagDisabled: true,
            noMatchText: 'دوره ای موجود نمی باشد',
            showCounts: false,
            menuWidth: 646,
            cols: {
                count: 5,
                gutter: 20
            }
        })
    })
});
