function initTabs() {
    var sets = document.getElementsByTagName("ul");
    for (var i = 0; i < sets.length; i++) {
        if (sets[i].className.indexOf("tabset") != -1) {
            var tabs = [];
            var links = sets[i].getElementsByTagName("a");
            for (var j = 0; j < links.length; j++) {
                if (links[j].className.indexOf("tab") != -1) {
                    tabs.push(links[j]);
                    links[j].tabs = tabs;
                    var c = document.getElementById(links[j].href.substr(links[j].href.indexOf("#") + 1));

                    if (c) if (links[j].className.indexOf("active") != -1) c.style.display = "block";
                    else c.style.display = "none";

                    links[j].onclick = function () {
                        var c = document.getElementById(this.href.substr(this.href.indexOf("#") + 1));
                        if (c) {
                            for (var i = 0; i < this.tabs.length; i++) {
                                var tab = document.getElementById(this.tabs[i].href.substr(this.tabs[i].href.indexOf("#") + 1));
                                if (tab) {
                                    tab.style.display = "none";
                                }
                                this.tabs[i].className = this.tabs[i].className.replace("active", "");
                            }
                            this.className += " active";
                            c.style.display = "block";
                            return false;
                        }
                    }
                }
            }
        }
    }
}
if (window.addEventListener)
    window.addEventListener("load", initTabs, false);
else if (window.attachEvent && !window.opera)
    window.attachEvent("onload", initTabs);

// Fancy Box

jQuery(document).ready(function () {

    jQuery("a.sharelinksoverlay").fancybox({
        'transitionIn': 'fade',
        'transitionOut': 'fade',
        'hideOnContentClick': false,
        'overlayShow': true,
        'padding': 0,
        'width': 200,
        'height': 30,
        'type': 'iframe',
        'scrolling': 'no'
    });
});

// TODO: Add PDF Aloqua scripts

jQuery(document).ready(function () {
    $("a[href$='.pdf']").each(function () {
        $(this).attr('href', '/elqNow/elqRedir.htm?ref=http://' + window.location.hostname + $(this).attr('href'));
        $(this).attr('target', '_Blank');
    });

});

jQuery(document).ready(function () {
    $("a.product-documentation-file").each(function () {
        $(this).attr('href', '/elqNow/elqRedir.htm?ref=' + $(this).attr('href'));
        $(this).attr('target', '_Blank');
    });

});


// Visuals slideshow
jQuery(document).ready(function () {
    jQuery('.slideshow').cycle({
        fx: 'fade',
        timeout: 15000,
        pager: '.slideshow-pager',
        cleartypeNoBg: true, pause: 1
    });
});



$(function () {


    // Contact BizForm

    //add newsletter copy above the newsletter checkboxes
    $("table[id$='newsletters'] tbody").prepend('<tr><td><p>Please sign me up for the iCommuniqu&eacute; eNewsletter and send me email updates on the latest news, information, and events in my industry.</p></td></tr>');

    //display state/provinces based on country
    var countryValue = $("#bizform select[id$='country'] option:selected").val();
    whichState(countryValue, 1);
    $("#bizform select[id$='country']").change(function () {
        countryValue = $("#bizform select[id$='country'] option:selected").val();
        whichState(countryValue, 0);
    });
    $("#bizform select[id$='us_states_list'], #bizform select[id$='au_states_list'], #bizform select[id$='ca_provinces_list']").change(function () {
        popState($("option:selected", this).val());
    });

    $("#bizform select[id$='Primary_Industry']").change(function () {
        whichSector($(this).val().replace(/[^\w]/ig, ''));
    });

    $("#bizform div#sectors>div select").change(function () {
        popSector($(this).val());
    });


    //modify utility nav
    $('#header .header-holder #menuElem li:last-child').addClass('last');



    //sales chat popup

    $("a[id$='HyperLink_Chat']").click(function () {
        //window.open('http://www.plugoo.com/chat/run.php?go=TC3L59XHSX4&sk', 'plugoo', 'width=169, height=309');
        window.open('http://server.iad.liveperson.net/hc/58043447/?cmd=file&file=visitorWantsToChat&site=58043447&imageUrl=http://server.iad.liveperson.nethttp://images.liveperson.com/lp/58043447/&referrer='+escape(document.location), 'chat58043447', 'width=475,height=400,resizable=yes');
        return false;
    });
});

function whichState(c, s) {

    if (s == 1) {
        $("#bizform #us_states, #bizform #au_states, #bizform #ca_provinces").hide();
        if (c == 'US') {
            $("#bizform #us_states").show();
        }
        if (c == 'CA') {
            $("#bizform #ca_provinces").show();
        }
        if (c == 'AU') {
            $("#bizform #au_states").show();
        }
    }

    if (s == 0) {
        popState('');
        if (c != 'US' && c != 'CA' && c != 'AU') {
            $("#bizform #us_states, #bizform #au_states, #bizform #ca_provinces").fadeOut();
        } else {
            $("#bizform #us_states, #bizform #au_states, #bizform #ca_provinces").hide();
        }
        if (c == 'US') {
            $("#bizform #us_states").fadeIn();
        }
        if (c == 'CA') {
            $("#bizform #ca_provinces").fadeIn();
        }
        if (c == 'AU') {
            $("#bizform #au_states").fadeIn();
        }
    }
}

function popState(x) {
    $("#bizform div[id='state'] input[id$='state']").val(x);
    if (x == '') {
        $("#bizform select[id$='us_states_list'], #bizform select[id$='au_states_list'], #bizform select[id$='ca_provinces_list']").val('');
    }
}

function whichSector(x) {
    popSector('');
    $('#bizform div#sectors>div select').each(function (e) { $(this).val(''); });
    if (x == '') {
        $('#bizform div#sectors').fadeOut();
    } else {
        $('#bizform div#sectors div select').hide();
        $('#bizform div#sectors div select#' + x + '_sectors').show();
        $('#bizform div#sectors').fadeIn();
    }
}

function popSector(x) {
    $("#bizform div[id='Sector'] input[id$='Sector']").val(x);
    if (x == '') {
        $('#bizform div#sectors>div select').each(function (e) { $(this).val(''); });
    }
}


jQuery(document).ready(function () {

    var links = jQuery('#sidebar .sidebar-holder .subnav > li > .sub_CMSListMenuUL > li > a');

	
	links.each(function () {
        var link = jQuery(this);

        if (link.siblings().length > 0){
            link.append("<span class='expandable'>+</span>");
		}
    });

    links = jQuery('#sidebar .sidebar-holder #menuElem .expandable');

    links.click(function(E) {
		// prevent the default link click behavior
		E.preventDefault();
		
		var method = 'slideDown';
		var matches = null;
		
		var $this = $(this);
		
		// If the link is already expanded, slide it up and remove the class
		if( $this.parent().hasClass('sub_CMSListMenuLinkHighlighted') ) {
			method = 'slideUp';
			$this.parent().removeClass('sub_CMSListMenuLinkHighlighted');
			$this.addClass('expandable');
		} else { //  if the link is not expanded, do so
			$this.parent().addClass('sub_CMSListMenuLinkHighlighted');
			$this.removeClass('expandable').addClass('expand-btn');
		}
		
		matches = $this.parent().siblings().first(); // siblings because the trigger is on the same level as the ul that slides.
		matches[method]();
		
		return false;
	});

	
	/*
    links.each(function () {
        var link = jQuery(this);

        if (link.siblings().length > 0)
            link.addClass('expandable');
    });

    links = jQuery('#sidebar .sidebar-holder #menuElem .expandable');

    links.click(function () {
        var clickedLink = jQuery(this);

        if (!clickedLink.hasClass('sub_CMSListMenuLinkHighlighted')) {
            links.filter('.sub_CMSListMenuLinkHighlighted').removeClass('sub_CMSListMenuLinkHighlighted').addClass('expandable').siblings().first().slideUp();
            clickedLink.removeClass('expandable').addClass('sub_CMSListMenuLinkHighlighted').siblings().first().slideDown();
        }

        return false;
    });
	
	*/
});


