/* 
   Simple JQuery Collapsing menu.
   HTML structure to use:

   <ul id="menu">
     <li><a href="#">Sub menu heading</a>
     <ul>
       <li><a href="http://site.com/">Link</a></li>
       <li><a href="http://site.com/">Link</a></li>
       <li><a href="http://site.com/">Link</a></li>
       ...
       ...
     </ul>
     <li><a href="#">Sub menu heading</a>
     <ul>
       <li><a href="http://site.com/">Link</a></li>
       <li><a href="http://site.com/">Link</a></li>
       <li><a href="http://site.com/">Link</a></li>
       ...
       ...
     </ul>
     ...
     ...
   </ul>

Copyright 2007 by Marco van Hylckama Vlieg

web: http://www.i-marco.nl/weblog/
email: marco@i-marco.nl

Free for non-commercial use
*/
var sidenavOpenState = false;
var sidenavFlyoutOpenState = false;
var showap;
var showallprodsUl;
var leftnavLi;
var pageId = 'partner';
var ajaxFlyouts = true;
var isMsie6 = false;
var isMsie7 = false;

function setUpSAP() {
    var a = $("#showallprods");
    showallprodsUl = $("#showallprods > ul > li > ul");
    leftnavLi = $("#showallprods ul.firstul > li");
    showap = $("#bigbutton");
    if (pageId == "home") {
        $("#showap").click(function () {
            return false;
        });
    } else {
        if (pageId == "partner") {} else {
            showap.click(function () {
                if (sidenavOpenState) {
                    closeShowAllProducts();
                } else {
                    sidenavOpenState = true;
                    $(this).addClass("showapOn");
                    showallprodsUl.addClass("showul");
                    if (isMsie6) {
                        $("select").removeClass("visible").addClass("invisible");
                    }
                    $(document).bind("click.showap", function (g) {
                        var c = $(g.target);
                        var d = c.parents();
                        var b = true;
                        d.each(function () {
                            if (this.tagName == "DIV" && $(this).attr("id") == "showallprods") {
                                b = false;
                                return false;
                            }
                        });
                        if (b) {
                            closeShowAllProducts();
                        }
                    });
                }
                return false;
            });
        }
    }
}

function closeShowAllProducts() {
    if (!sidenavFlyoutOpenState) {
        sidenavOpenState = false;
        /*if (isMsie6 && !$("#showallprods > ul > li > div.flyout:not(.hide)")[0]) {
            $("select").removeClass("invisible").addClass("visible");
        }*/
        showap.removeClass("showapOn");
        showallprodsUl.removeClass("showul");
        $(document).unbind("click.showap");
    }
}
function closeFlyout(b) {
    b.removeClass("selected");
    var a = $("#" +b.attr("id")+" > div.flyout:not(.flywrap)")
    if (a) {
        a.addClass("hide");
	sidenavFlyoutOpenState = false;
    } else {
        sidenavFlyoutOpenState = false;
    }
}


function hideme(a) {
    $("#" + a).addClass("hide");
}
function showme(a) {
    $("#" + a).removeClass("hide");
}


/*function setUpSidenav() {
    if (typeof(leftnavLi) == "undefined") {
        setUpSAP();
    }
    var a = $("#showallprods");
    showallprodsUl = $("#showallprods > ul > li > ul");
    showap = $("#showap");
    leftnavLi.hoverIntent({
        sensitivity: 1,
        interval: 100,
        over: function(){
            openFlyout($(this));
        },
        timeout: 0,
        out: function(){
		closeFlyout($(this));
        }
    });
}*/

function setUpSidenav(){
	if(typeof(leftnavLi)=="undefined"){
		setUpSAP();
	}
	var a=$("#showallprods");
	showallprodsUl=$("#showallprods > ul");
	showap=$("#showap");
	leftnavLi.hoverIntent({
		sensitivity:1,
		interval:2,
		over:function(){
			openFlyout($(this));
		},
		timeout:1,
		out:function(){
			closeFlyout($(this));
		}
	});
}


/*function openFlyout(d) {
	var a = $(d);
	a.addClass("selected");
	var g = a.data("flyout");
	showFlyOut(a);
}*/

function openFlyout(d){
	var a=$(d);
	a.addClass("selected");
	var g=a.data("flyout");
	if(ajaxFlyouts){
		if(!g){
			var c=(a.attr("id").replace(/(FO)/g,"div"));
			var b=$("#"+c);
			a.data("flyout",b);
			//console.log('a.data("flyout",b)');
			/*a.append(b);
			console.log('a.append(b)');*/
			/*b.remove();
			console.log('b.remove()');*/
		}/*else{
			a.append(g);
			console.log('a.append(g)');
			g.remove();
			console.log('g.remove()');
		}*/
		showFlyOut(a);
	}else{
		if(!g){
			a.data("flyout",a.find("div.flyout"));
		}
		if(!$(g)[0]){}
		else{
			showFlyOut(a);
		}
	}
}

jQuery.fn.totalHeight = function () {
    var a = parseInt(this.css("border-top-width"));
    var b = parseInt(this.css("border-bottom-width"));
    var c = parseInt(this.css("padding-top"));
    var d = parseInt(this.css("padding-bottom"));
    a = a ? a : 0;b = b ? b : 0;c = c ? c : 0;d = d ? d : 0;
    return this.height() + a + b + c + d;
};

function showFlyOut(g) {
	sidenavFlyoutOpenState = true;
	var a = $("#showallprods");
	showallprodsUl = $("#showallprods > ul");
	leftnavLi = $("#showallprods ul.firstul > li");
    
	leftnavLi.find("div.flyout").addClass("hide");
	//$("#" +g.attr("id")+" > div.flyout:not(.flywrap)");
	// var j = g.data("flyout");
	var j = $("#" +g.attr("id")+" > div.flyout:not(.flywrap)");
	var a = {
            offsetTop: g.offset().top,
            positionTop: g.position().top,
            totalHeight: g.totalHeight(),
            flyoutDivTotalHeight: j.totalHeight(),
            divitObj: j.find("span.divit")
        };

	var d = 41;
	var c = -1;
	var h = $(window).scrollTop() - g.offset().top + g.position().top;
	if (h + 10 > c) {
		c = h + 10;
	}

	if (pageId != "home" && d > c) {
		c = d + 5;
	}

	if (g.position().top < c) {
		c = g.position().top - 10;
	}

	if ((c + j.totalHeight()) < (g.totalHeight() + g.position().top + 6)) {
		c = (g.totalHeight() + g.position().top+ 0) - j.totalHeight();
	}
	
	var b = g.position().top - c - 10 + (g.totalHeight() / 2);
	a.divitObj.attr("style", "top:" + b + "px");
	$("#" +g.attr("id")+" > div.flyout:not(.flywrap)").attr("style", "top:" + c + "px");
	g.find('div').first().attr('class', 'flyout');
	if (isMsie6 || isMsie7) {
		g.find("a.closer").attr("style", "right:5px");
	}
}


function initMenu() {
  $('#menu ul.firstul').hide('fast');
  $('#menu li a.selecteditem').each(function(){$(this).parent().parent().show('fast');$(this).next(":not(div)").show('fast');});
  
  $('#menu li a.menu_bb').hoverIntent({
        sensitivity: 1,
        interval: 100,
        over: function() {
        $(this).next().removeClass('firstul');
		$('#menu ul.firstul').hide('fast');
		$(this).next().slideToggle('fast');	
      },
        timeout: 0,
        out: function () {
           $(this).next().addClass('firstul');
        }
    });
  }
$(document).ready(function() {
	initMenu();
	setUpSAP();
	setUpSidenav();
});

