var menuHoverTimeout;

jQuery.noConflict();
jQuery().ready(function() {

    // search box initialize
    var searchFieldColor = jQuery("input.search-field").css("color");
    jQuery("input.search-field").css("color", "#aaa").val("Search").bind("focus", function() {
        jQuery(this).css("color", searchFieldColor).val("").unbind("focus");
    });

    // 3rd level subnav, prepend arrows
    jQuery("li.nav3entry a").prepend("&gt; ");
    if (!jQuery("ul#nav2ul").hasClass("subnav-home") && jQuery("ul#nav2ul a.clicked").length == 0) {
        jQuery("li.nav2entry:first").addClass("clicked").find("a:first").addClass("clicked");
    }

    // menu drop-downs
    if (jQuery("ul.subnav").length > 0) {
        jQuery("ul#topnav > li:not(#search)").hover(function() {
            clearTimeout(menuHoverTimeout);
            if (jQuery(this).find("ul.subnav").css("display") != "block") {
                jQuery("ul#topnav > li:not(#search)").find("a.a1").removeClass("hover");
                jQuery("ul#topnav ul.subnav").hide().css("display", "none");
                jQuery(this).find("a.a1").addClass("hover");
                jQuery("ul#topnav ul.subnav-" + jQuery(this).attr("id")).fadeTo(0, 0).css("display", "block").fadeTo("fast", 0.95);
            }
        }, function() {
            clearTimeout(menuHoverTimeout);
            if (jQuery("ul#nav2ul").hasClass("subnav-" + jQuery(this).attr("id"))) {
                rewrapDropDown();
            } else {
                menuHoverTimeout = setTimeout("rewrapDropDown()", 300);
            }
        });
    }

    jQuery('ul#portfolio').innerfade({
        speed: 750,
        timeout: 9000,
        type: 'sequence',
        containerheight: '200px'
    });

    // testimonials
    jQuery('ul#portfolio2').innerfade({
        speed: 750,
        timeout: 15000,
        type: 'random',
        containerheight: '250px'
    });

    jQuery('ul#portfolio3').innerfade({
        speed: 750,
        timeout: 6000,
        type: 'sequence',
        containerheight: '330px'
    });

    // news line on home
    jQuery('ul#portfolio4').innerfade({
        animationtype: 'slide',
        speed: 600,
        timeout: 4000,
        type: 'sequence',
        containerheight: '13px'
    });

    // services/consulting
    jQuery('ul#portfolio5').innerfade({
        speed: 750,
        timeout: 3000,
        type: 'sequence',
        containerheight: '103px'
    });

    // products/overview
    jQuery('ul#portfolio6').innerfade({
        speed: 750,
        timeout: 3000,
        type: 'sequence',
        containerheight: '50px'
    });

    jQuery("ul.tabs").tabs("div.panes > div", {
        effect: 'fade',
        fadeInSpeed: 300},
                          function(i) {
                              // get the pane to be opened
                              var pane = this.getPanes().eq(i);
                              // if it is empty ..
                              if (pane.is(":empty")) {
                                  // load it with a page specified in the tab's href attribute
                                  pane.load(this.getTabs().eq(i).attr("href"));
                              }
                          }).history();


    jQuery("ul.tabs-p").tabs("div.panes-p > div", {
        effect: 'fade',
        fadeInSpeed: 400},
                            function(i) {
                                // get the pane to be opened
                                var pane = this.getPanes().eq(i);
                                // if it is empty ..
                                if (pane.is(":empty")) {
                                    // load it with a page specified in the tab's href attribute
                                    pane.load(this.getTabs().eq(i).attr("href"));
                                }
                            }).history();


    // if the function argument is given to overlay, it is assumed to be the onBeforeLoad event listener
    jQuery("a[rel]").overlay(function() {
        // grab wrapper element inside content
        var wrap = this.getContent().find("div.wrap");
        // load only for the first time it is opened
        {
            wrap.load(this.getTrigger().attr("href"));
        }
    });

    // initialize scrollable 
    jQuery("div.scrollable").scrollable({
        size: 3,
        items: '#thumbs',
        hoverClass: 'hover',
        clickable: false
    });

    jQuery("#jqtree").treeview({
        animated: "fast",
        collapsed: true
    });

    jQuery("#jqtree1").treeview({
        animated: "fast",
        collapsed: true
    });

    jQuery("#jqtree2").treeview({
        animated: "fast",
        collapsed: true
    });

    fixPng();
});

function fixPng() {
    var arVersion = navigator.appVersion.split("MSIE");
    var version = parseFloat(arVersion[1]);

    if ((version >= 5.5) && (version < 7.0) && (document.body.filters)) {
        for (var i = 0; i < document.images.length; i++) {
            var img = document.images[i];
            var imgName = img.src.toUpperCase();
            if (imgName.substring(imgName.length - 3, imgName.length) == "PNG") {
                var imgID = (img.id) ? "id='" + img.id + "' " : "";
                var imgClass = (img.className) ? "class='" + img.className + "' " : "";
                var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
                var imgStyle = "display:inline-block;" + img.style.cssText;
                if (img.align == "left") imgStyle = "float:left;" + imgStyle;
                if (img.align == "right") imgStyle = "float:right;" + imgStyle;
                if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
                img.outerHTML = "<span " + imgID + imgClass + imgTitle
                        + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
                        + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                        + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
                i = i - 1
            }
        }

        // fix css background pngs
        jQuery("h1#logo").find("*").each(function() {
            var bgIMG = jQuery(this).css('background-image');
            if (bgIMG.indexOf(".png") != -1) {
                var iebg = bgIMG.split('url("')[1].split('")')[0];
                jQuery(this).css('background-image', 'none');
                jQuery(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
            }
        });

    }
}

function rewrapDropDown() {
    clearTimeout(menuHoverTimeout);
    jQuery("ul#topnav ul.subnav").fadeOut(200, function() {
        jQuery("ul#topnav > li:not(#search)").find("a.a1").removeClass("hover");
        jQuery(this).css("display", "none");
    });
}

// anchorId has to be unique for the entire document, rec holds the recipient part of
// the address before the @
function setLnkAddr(anchorId, rec) {
a = document.getElementById(anchorId);
a.firstChild.nodeValue = addr(rec);
a.href=lnk(rec);
}


function addr(rec) {
var a = "canoo"
var b = "com"
return rec + "@" + a + "." + b
}

function lnk(rec) {
var d = "mai"
var f = "lto"
var g = ":"
return  d + f + g + addr(rec);
}


