// JavaScript Document - makes nav double click for iPads drop downs

(function($){

var timeout = 500;
var closetimer = 0;
var ddmenuitem = null;

// From https://github.com/Modernizr/Modernizr/blob/master/modernizr.js
var isTouch = ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch;

// jsddm funcs from http://javascript-array.com/scripts/jquery_simple_drop_down_menu/
function jsddm_open($this){
jsddm_canceltimer();
if(!$this.parentsUntil('.mainNav').hasClass("open")) jsddm_close();
ddmenuitem = $this.next('ul').css('display', 'block').addClass("open");
}

function jsddm_close() {
if(ddmenuitem){
ddmenuitem.css('display', 'none').removeClass("open");
ddmenuitem = null;
}
}

function jsddm_timer() {
closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer() {
if(closetimer)
{ window.clearTimeout(closetimer);
closetimer = null;
}
}

function jsddm_toggle($this) {
if (ddmenuitem && $this.next("ul").hasClass("open")){
jsddm_close();
window.location = $this.attr("href");
}
else {
jsddm_open($this);
}
}

$.fn.make_dropdown = function(options){

return this.each(function(){

if (options && options['timeout']){
timeout = options['timeout'];
}

if (!isTouch){
//$(this).mouseover(function(){ jsddm_open($(this)) }).mouseout(jsddm_timer);
}
else{
$(this).click(function(event){
//alert("touch device");
event.preventDefault();
jsddm_toggle($(this));
event.stopPropagation();
});
}
});
}

$(document).click(jsddm_close);

})(jQuery);

Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function(){$('#wrapNav .menu li.parent > a').make_dropdown();});