function opendialog(href) {
    $("#region").attr("src", href);
    $('#regiondialog').dialog('open');
    return false;
}
$(document).ready(function () {

    /* JQTransform */

    $("form").jqTransform();

    if ($(".signup-form").length) {


        $("form").validate({
            onsubmit: false
        });

        $(".btn-signin, .btn-submit").live("click", function () {

            var isValid = true;
            $(this).parent().parent().find(':input').each(function (i, item) {
                if (!$(item).valid())
                    isValid = false;
            });

            if (!isValid)
                return false;
        });

    }

    //remove any text from the search button
    $('.btn-search-top > span').text('');

    //hidethis
    $(".step2, .step3, .step3b").hide();

    /* Equalize height */

    $(" .footer-content-col ").equalizeCols();

    $(' .tabs-home-content ').equalizeCols();
    $(" .eqh-th2 ").equalizeCols();
    $(" .eqh-th3 ").equalizeCols();

    /* Overlabel */
    $('label.overlabel').overlabel();

    /* UI functions */
    $(".tabs-home").tabs();

    $(".small-accordion").accordion({ autoHeight: false, active: 1, collapsible: true });
    $(".small-accordion-inside").accordion({ header: "h4", autoHeight: false });

    /* executive Team menu */
    $('.executive-team').hover(
		function () {
		    $(this).addClass('hover');
		},
		function () {
		    $(this).removeClass('hover');
		}
	);
    /*select region*/
    $(".map-selector a.americas").hover(
	  function () {
	      $(".regions-map").css({ backgroundPosition: "0 -270px" });
	  },
	  function () {
	      $(".regions-map").css({ backgroundPosition: "0 0" });
	  }
	);
    $(".map-selector a.europe").hover(
	  function () {
	      $(".regions-map").css({ backgroundPosition: "0 -540px" });
	  },
	  function () {
	      $(".regions-map").css({ backgroundPosition: "0 0" });
	  }
	);
    $(".map-selector a.india").hover(
	  function () {
	      $(".regions-map").css({ backgroundPosition: "0 -810px" });
	  },
	  function () {
	      $(".regions-map").css({ backgroundPosition: "0 0" });
	  }
	);
    $(".map-selector a.asia").hover(
	  function () {
	      $(".regions-map").css({ backgroundPosition: "0 -1080px" });
	  },
	  function () {
	      $(".regions-map").css({ backgroundPosition: "0 0" });
	  }
	);
    /*pass value to selects*/
    $(".btn-go").click(function () {
        var selectURL = $(this).parents("tr").find("select.jqTransformHidden").val();
        location.href = selectURL;
    });
    /* tabs */
    $("#tabs-list-team").tabs();
    $("#tabs-week").tabs();

    /* Slider homes */
    try {
        $('#slider-home').cycle({
            speed: 600,
            fx: 'fade',
            timeout: 8000,
            prev: '.btn-prev-slider',
            next: '.btn-next-slider',
            pager: '.nav-rotator-home'
        });

        $('#slider-home-careers').cycle({
            speed: 600,
            fx: 'fade',
            timeout: 8000,
            prev: '.btn-prev-slider',
            next: '.btn-next-slider',
            pager: '.nav-rotator-home'
        });

        $('#slider-regions').cycle({
            speed: 600,
            fx: 'fade',
            timeout: 8000,
            prev: '.btn-prev-slider',
            next: '.btn-next-slider',
            pager: '.nav-rotator-home'
        });
    } catch (e) { }

    /* News & Events rotator homepage */
    try {
        $(".news-events-rotator .news-events-rotator-wrapper .news-events-rotator-content").jCarouselLite({
            btnNext: ".news-events-rotator .news-events-rotator-wrapper .next-news-event",
            btnPrev: ".news-events-rotator .news-events-rotator-wrapper .prev-news-event",
            visible: 1,
            speed: 800,
            auto: 5000
        });
    } catch (e) { }

    /* H Accordion */
    if ($('ul.h-accordion').length > 0) {
        $('#tab1-home div ul li.last-child div.h-acc-wrapper').addClass('w203');
        $('#tab1-home div ul li.last-child').click(function () { $(this).find('.h-acc-wrapper').removeClass('w203'); });
        $('ul.h-accordion').each(function () {
            var wCont = parseInt($(this).find('.h-acc-content').width()) + 37; // 37 = padd(l/r) + border from content

            var wImg = parseInt($(this).find('.h-acc-main-img').width());
            var maxWidth = String(wCont + wImg);
            var minWidth = String(wImg);

            var lastBlock = $(this).find('li:first-child .h-acc-wrapper');

            $(this).find('li:first-child').addClass('h-acc-active');
            $(lastBlock).css('width', maxWidth + 'px');

            $(this).find(".h-acc-main-img").click(
				function () {
				    $(lastBlock).parent().removeClass('h-acc-active');
				    $(lastBlock).animate({ width: minWidth + "px" }, { queue: false, duration: 400 });
				    $(this).parents('.h-acc-wrapper').animate({ width: maxWidth + "px" }, { queue: false, duration: 400 });
				    lastBlock = $(this).parents('.h-acc-wrapper');
				    $(this).parents('ul.h-accordion li').addClass('h-acc-active');
				}
			);
        });

    }

    $('.h-accordion .h-acc-main-img').hover(
		function () {
		    $(this).addClass('h-acc-hover');
		},
		function () {
		    $(this).removeClass('h-acc-hover');
		}
	);

    /* JTab */
    $('.jtab-control').each(function () {
        var oSlideBox = $(this);
        var oLIs = $('>.jtab-content>div.jtab-content-box', oSlideBox);
        var hJtabUl = $('ul.jtab-items', oSlideBox).height();
        oLIs.css({ 'display': 'none' });
        $('>.jtab-content>div:first-child', oSlideBox).css('display', 'block');
        $('.jtab-items li:first-child a', oSlideBox).addClass('current');
        $('.jtab-items li:last-child', oSlideBox).addClass('last-child');
        $('.jtab-items a', oSlideBox).click(function () {
            if ($(this).hasClass('current')) {
                return false;
            }
            else {
                $('.jtab-items a', oSlideBox).removeClass('current');
                $(this).addClass('current');
                var index = $('.jtab-items a', oSlideBox).index(this);
                index++;

                oLIs.css('z-index', '1');
                $('>.jtab-content>div:nth-child(' + index + ')', oSlideBox).css('z-index', '2');

                //oLIs.customFadeOut('slow');
                oLIs.hide();
                //$('>.jtab-content>div:nth-child(' + index + ')', oSlideBox).customFadeIn(200);
                $('>.jtab-content>div:nth-child(' + index + ')', oSlideBox).show();
                return false;
            }
        });
    });

    /* To Select/Clear all */
    $('.select-all').click(
		function () {
		    var oCont = $(this).parents('.ui-accordion-content');

		    $('input:checkbox', oCont).each(
				function (iIndex, obj) {
				    $(obj).attr('checked', 'checked');
				}
			);
		    $('span.jqTransformCheckboxWrapper>a.jqTransformCheckbox', oCont).each(
				function (iIndex, obj) {
				    $(obj).addClass('jqTransformChecked');
				}
			);

		    return false;

		}
	);

    $('.clear-all').click(
		function () {
		    var oCont = $(this).parents('.ui-accordion-content');

		    $('input:checkbox', oCont).each(
				function (iIndex, obj) {
				    $(obj).removeAttr('checked', 'checked');
				}
			);
		    $('span.jqTransformCheckboxWrapper>a.jqTransformCheckbox', oCont).each(
				function (iIndex, obj) {
				    $(obj).removeClass('jqTransformChecked');
				}
			);

		    return false;

		}
	);

    /* Offices' map */
    //var _id_city_old = "";
    $('#offices-map a, .contact-map-left li a').click(function () {
        _id_city_current = $(this).attr('href');
        $('#offices-map div.contact-info-wrapper').hide();
        $('#offices-map div.contact-info-wrapper' + _id_city_current).show();
        return false;
    });

    $('#offices-map img#office-map-img').click(function () {
        _id_city_old = "";
        $('#offices-map div.contact-info-wrapper').hide();
    });

    /* Main nav  */
    $(".main-nav .nav-list li:last-child").addClass('last-child');

    /*$(".main-nav .nav-list li a").hover(
    function() {
    $(this).parent().prev().addClass('nobg')
    },
    function() {
    $(this).parent().prev().removeClass('nobg')
    }
    );*/

    /**************************/
    /* Flyout menu */
    $(".main-nav .nav-list .display li a").hover(
		function () {
		    $(this).parent().prev().removeClass('nobg')
		}
	);

    /* Solutions menu */
    $('.select-solutions').hover(
		function () {
		    $(this).addClass('hover');
		},
		function () {
		    $(this).removeClass('hover');
		}
	);
    /* Industries menu */
    $('.select-industries').hover(
		function () {
		    $(this).addClass('hover');
		},
		function () {
		    $(this).removeClass('hover');
		}
	);
    /* Publication menu */
    $('.select-publication').hover(
		function () {
		    $(this).addClass('hover');
		},
		function () {
		    $(this).removeClass('hover');
		}
	);
    /* About menu */
    $('.select-about').hover(
		function () {
		    $(this).addClass('hover');
		},
		function () {
		    $(this).removeClass('hover');
		}
	);

    /* Lightboxes */

    try {
        $("#dvdialog").dialog({
            autoOpen: false,
            bgiframe: true,
            width: 444,
            modal: true,
            open: function (event, ui) {
                //alert("this is the open function");
                iframeOpen = true;
            },
            beforeClose: function () {
                //alert("this is before close is it triggering");
                //$("#ifrmProfiles").attr("src", "about:Tabs").hide();
            }
        });
        $("#signupdialog").dialog({
            autoOpen: false,
            bgiframe: true,
            width: 590,
            modal: true,
            open: function (event, ui) {
                iframeOpen = true;
            },
            beforeClose: function () {
            }
        });
        $("#regiondialog").dialog({
            autoOpen: false,
            bgiframe: true,
            width: 600,
            modal: true,
            open: function (event, ui) {
                iframeOpen = true;
                //See if we have are opening a contact dialog
                if ($("#region").attr("src").indexOf('ContactDialog') > 0) {
                    //do some formatting of the contact page
                    $('#region')
						.css('padding-left', '15px')
						.css('padding-top', '20px')
						.css('width', '573px')
						.css('height', '555px');
                    $(this).dialog("option", "height", 585);
                }

                if ($("#region").attr("src").indexOf('videos') > 0) {
                    //do some formatting of the contact page
                    $('#region').css({ 'width': '573px', 'height': '555px' });
                    //$(this).dialog("option", "height", 540);
                }

            },
            beforeClose: function () {
            }
        });


    } catch (e) { }
    // Lightbox Links
    $('#region').addClass('contact-inquiry'); /*delete if other lightboxes have issues*/
    $('.open-dialog.video, .search-title.open-dialog.video').live("click", function (event) {
        event.isDefaultPrevented();
        event.stopPropagation();
        $("#ifrmProfiles").attr("src", this.href);
        $('#dvdialog').dialog('open');
        return false;
    });
    $('#dvdialog .close-dialog').live("click", function () {
        $("#ifrmProfiles").attr("src", "");
        $('#dvdialog').dialog('close');
        return false;
    });
    $('.open-dialog.signup').live("click", function (event) {
        event.isDefaultPrevented();
        event.stopPropagation();
        $("#signup").attr("src", this.href);
        $('#signupdialog').dialog('open');
        return false;
    });
    $('#signupdialog .close-dialog').live("click", function () {
        $("#signup").attr("src", "");
        $('#signupdialog').dialog('close');
        return false;
    });
    $('.open-dialog.region, .open-dialog.contact').live("click", function (event) //behrreich added contact dialog
    {
        event.isDefaultPrevented();
        event.stopPropagation();
        $("#region").attr("src", this.href);
        $('#regiondialog').dialog('open');
        return false;
    });
    $('#regiondialog .close-dialog').live("click", function () {
        $("#region").attr("src", "");
        $('#regiondialog').dialog('close');
        return false;
    });

    $('.map-selector li a').live("click", function () {
        var url = $(this).attr('id').replace("aRegion", "");
        parent.top.location.href = URLsArray[url]; // "/careers/americas.aspx";
        return false;
    });


    /*tooltip-left*/
    var contador = 0;
    $('.button-message>li.tooltip-box').each(function () {
        var _this = $(this);
        var container = $('.right-side');
        var containerWidth = container.width();
        var containerOffsetLeft = container.offset().left;
        var containerCordRight = containerOffsetLeft + containerWidth;

        var liOffsetLeft = _this.offset().left;
        var liWidth = _this.outerWidth(true);
        var liCordRight = liOffsetLeft + liWidth;
        var dif = liCordRight - liOffsetLeft;

        var tooltip = $(this).find('.content');
        var tooltipOffsetLeft = tooltip.offset().left;
        var tooltipWidth = tooltip.width();
        var tooltipCordRight = liOffsetLeft + tooltipWidth;
        contador = contador + 1;
        if (tooltipCordRight > containerCordRight) {
            var diference = ((tooltipCordRight - containerCordRight) + 30) * -1;
            tooltip.css('left', diference);

        }
        else { }
    });


    $('.button-message li.tooltip-box').mouseover(function () {
        $(this).find('span.arrow').css('display', 'block');
    }).mouseout(function () {
        $(this).find('span.arrow').css('display', 'none');
    });
    /**************************/

    /* Zebra effect */
//    $('.all-events-box .all-events-box-item:odd').addClass('odd');


    /* Content */
    $('.top-options .select-regions').hover(
		function () {
		    $(this).addClass('hover');
		},
		function () {
		    $(this).removeClass('hover');
		}
	);

    /* Footer */
    $('.footer .footer-content .footer-content-col:last-child').addClass('last-child');

    /*Search results page - Bill Ehrreich, behrreich@bgtpartners.com*/
    if (typeof (serviceUrl) != "undefined") {
        var order = $('#search-order').val();
        $('#secondary-search').val(srch);
        searchSite(srch, order);


        //additional search box on page
        $('#submit_btn').click
		(
			function () {
			    //searchSite($('#secondary-search').val());
			    var newsrch = $('#secondary-search').val();
			    if (newsrch != '') {
			        srch = newsrch;
			        searchSite(srch, $('#search-order').val());
			    }
			}
		);

        $('#a_submit_btn').click
		(
			function () {
			    //searchSite($('#secondary-search').val());
			    var newsrch = $('#secondary-search').val();
			    if (newsrch != '') {
			        srch = newsrch;
			        searchSite(srch, $('#search-order').val());
			    }
			}
		);

        //sorting box.  We need to fudge this because jqTransform doesn't allow us to
        //directly add a change event to the select
        $('div.top-results-options-right div.jqTransformSelectWrapper ul li a').click
		(
			function () {
			    var text = $("div.top-results-options-right div.jqTransformSelectWrapper span").text()
			    var value = $("#search-order option:contains('" + text + "')").val();
			    searchSite(srch, value);
			}
		);

        $('#secondary-search').keypress
		(
			function (e) {
			    if (e.keyCode == 13) {
			        $('#submit_btn').click();
			        e.preventDefault();
			    }
			}
		);

    }

    //Contact form (WFM)
    if (typeof (contentprefix) != "undefined") {
        //New WFM contact form submit code from behrreich
        $('.btn-submit').click
		(
			function () {
			    //validate the form
			    //var offers = $('#offers');
			    var firstname = $('#fName');
			    var lastname = $('#lName');
			    var email = $('#email');
			    var phone = $('#phone');
			    var companyname = $('#cName');
			    var jobtitle = $('#jTitle');
			    var country = $('#' + contentprefix + 'country');
			    var industry = $('#' + contentprefix + 'industry');
			    var topic = $('#topic');
			    var message = $('#message');
			    var canbecontacted = $('#canbecontacted');
			    var wantemails = $('#wantemails');
			    var howheard = $('#howheard');

			    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			    var msg = "";

			    if (firstname.val() == "" || firstname.val() == "First Name*") {
			        msg += "* You must enter a first name.\r\n";
			    }
			    if (lastname.val() == "" || lastname.val() == "Last Name*") {
			        msg += "* You must enter a last name.\r\n";
			    }
			    if (email.val() == "" || email.val() == "Email*") {
			        msg += "* You must enter an e-mail address.\r\n";
			    }
			    if (!filter.test(email.val()) && email.val() != "" && email.val() != "Email") {
			        msg += "* The e-mail address entered is invalid.\r\n";
			    }
			    if (country.val() == "") {
			        msg += "* You must enter a country.\r\n";
			    }
			    if (industry.val() == "") {
			        msg += "* You must enter an industry.\r\n";
			    }
			    if (topic.val() == "") {
			        msg += "* You must enter a topic.\r\n";
			    }
			    if (topic.val() == "Other" && $('#topicother').val() == "") {
			        msg += "* You must enter a topic in the other topic box.\r\n";
			    }
			    if (industry.val() == "Other" && $('#industryother').val() == "") {
			        msg += "* You must enter an industry in the other industry box.\r\n";
			    }
			    if (!canbecontacted.attr('checked')) {
			        msg += "* You must agree to be contacted by ZS Associates.\r\n";
			    }
			    if (msg != "") {
			        alert(msg);
			    }

			    else {

			        //Copy form values to the wfm form and post
			        $('#' + contentprefix + 'wfmc_field_35C47F66553C4F35AE9631E7539B4435').val(firstname.val()); //firstname
			        $('#' + contentprefix + 'wfmc_field_95EC598949B64F91B6F07FAE0E06C03A').val(lastname.val()); //lastname
			        $('#' + contentprefix + 'wfmc_field_2FD614E487944CA7A47D535ED11D1C18').val(email.val()); //email
			        $('#' + contentprefix + 'wfmc_field_C0B4E4BDB6AA4E35AFB0F6B1F045CA9B').val(phone.val()); //phone
			        $('#' + contentprefix + 'wfmc_field_7E3C538C779049C0B1F989F9795B4300').val(companyname.val()); //companyname
			        $('#' + contentprefix + 'wfmc_field_4A4865FDDF894B178B9A87F2E7DA28C5').val(jobtitle.val()); //jobtitle
			        $('#' + contentprefix + 'wfmc_field_81346BE7A89E44C993898F20259005E6').val(country.val()); //country

			        $('#' + contentprefix + 'wfmc_field_1725415B2F70430C97F39026380AC5E6').val(message.val()); //message

			        if ($('#otherheard').is(":visible")) {
			            $('#' + contentprefix + 'wfmc_field_300494E7ABA54697B40FC9238574EAD6').val(howheard.val() + ': ' + $('#howheardother').val());
			        }
			        else {
			            $('#' + contentprefix + 'wfmc_field_300494E7ABA54697B40FC9238574EAD6').val(howheard.val());
			        }

			        if ($('#othertopic').is(":visible")) {
			            $('#' + contentprefix + 'wfmc_field_4B524CA6360E4F55919F683232146DB6').val(topic.val() + ': ' + $('#topicother').val()); //topic
			        }
			        else {
			            $('#' + contentprefix + 'wfmc_field_4B524CA6360E4F55919F683232146DB6').val(topic.val()); //topic
			        }

			        if ($('#otherindustry').is(":visible")) {
			            $('#' + contentprefix + 'wfmc_field_62D47884EC7148CDB2F3042EE38FD3F5').val(industry.val() + ': ' + $('#industryother').val()); //industry
			        }
			        else {
			            $('#' + contentprefix + 'wfmc_field_62D47884EC7148CDB2F3042EE38FD3F5').val(industry.val()); //industry
			        }

			        $('#' + contentprefix + 'wfmc_field_DA4DEC1CF3AF46A6AD23DA6347B20295').val(contentpath); //source
			        $('#' + contentprefix + 'wfmc_field_6B4225D0C13A4527BE0B9708F4C19DA1').attr('checked', canbecontacted.attr('checked')); //canbecontacted
			        $('#' + contentprefix + 'wfmc_field_A4C87E3237EC4B5595539EC9A7DD5BBA').attr('checked', wantemails.attr('checked')); //wantemails
			        $('#' + contentprefix + 'wfmc_form_3400B3A6EB1C48029EBF4AD7FA261C66_submit').click(); //click submit button

			    }

			}
		);

        //Show the form message for the newsletter form
        if ($('#' + contentprefix + 'wfmc_form_3400B3A6EB1C48029EBF4AD7FA261C66_submit').length == 0) {
            $('.message').text($('#wfmc').text());
            $('.message').css({ color: 'black' });

            //Hide the form
            $('.contact-table').hide();

            //Scroll to the bottom
            $('html, body').animate
			({
			    scrollTop: $(document).height()
			},
                10
			);
        }
        else {
            //Show the error message if there is one
            $('.message').text($('#' + contentprefix + 'wfmc_form_3400B3A6EB1C48029EBF4AD7FA261C66_submitSummary > span').text());
            $('.message').css({ color: 'red' });
        }

        //Subject box.  We need to fudge this because jqTransform doesn't allow us to directly add a change event to the select
        $('#divtopic div.jqTransformSelectWrapper ul li a').click
		(
			function () {
			    var text = $("#divtopic div.jqTransformSelectWrapper span").text()

			    if (text == "Other") {
			        $('#othertopic').show();
			    }
			    else {
			        $('#othertopic').hide();
			    }
			}
		);

        //Industry box.  We need to fudge this because jqTransform doesn't allow us to directly add a change event to the select
        $('#divindustry div.jqTransformSelectWrapper ul li a').click
		(
			function () {
			    var text = $("#divindustry div.jqTransformSelectWrapper span").text()

			    if (text == "Other") {
			        $('#otherindustry').show();
			    }
			    else {
			        $('#otherindustry').hide();
			    }
			}
		);

        //Source box.  We need to fudge this because jqTransform doesn't allow us to directly add a change event to the select
        $('div.contact-tr-last-right div.jqTransformSelectWrapper ul li a').click
		(
			function () {
			    var text = $("div.contact-tr-last-right div.jqTransformSelectWrapper span").text()
			    if (text == "Other") {
			        $('#otherheard').show();
			    }
			    else {
			        $('#otherheard').hide();
			    }
			}
		);



    }

});

//Search filter
function getFilter() {
    //put together a comma-delimited list of filters
    var filter = "";

    $('#tblfilter input.jqTransformHidden').each
	(
		function () {
		    filter += ($(this).attr('checked')) ? "," + $(this)[0].id.replace("srch", "") : "";
		}
	);

    if (filter != "" && filter.indexOf(",") > -1) {
        filter = filter.substr(1, filter.length - 1);
    }
    return filter;
}

//Search function
function searchSite(searchtext, order) {
    var filter = getFilter();

    var Url = serviceUrl + '?s=' + searchtext + "&f=" + filter;
    $.ajax
		(
			{ type: "get", url: Url,
			    success: function (data) {
			        $('#lblSearchString').text(searchtext);

			        var results = $.parseJSON(data);
			        //Get the results div template
			        var template = $('div.search-result-item-template');

			        //clear current entries
			        var existing = $('div.search-result-item');

			        var parent = $('div.search-result-box-right');
			        var nav = $('div.search-result-nav');

			        //clear all previous entries
			        existing.remove();

			        if (results.length >= 1 && results[0].title != "no records found") {
			            $('.search-result-nav').show('fast');

			            //Sort results by matches field
			            results = results.sort(sortMatchesDesc);

			            //Add new entries
			            $.each(results,
						function (p, r) {
						    var newentry = template.clone();
						    newentry.find('a.search-title span').text(this.title);
						    newentry.find('a.search-title').attr('href', this.url).addClass(this.styleclass);
						    var newteaser = this.teaser;

						    //find the search string or its components and highlight in the text
						    //newentry.find('p span').html(this.teaser.replace(searchtext, '<span class=\"search-item-found\">' + searchtext + '</span>'));

						    var searches = searchtext.split(' ');
						    $.each
							(searches,

								function (i, obj) {
								    var re = new RegExp('(' + obj + ')', 'gi');
								    newteaser = newteaser.replace(re, '<span class=\"search-item-found\">$1</span>');
								}
							);

						    newentry.find('p span').html(newteaser);
						    newentry.find('p > a').attr('class', this.styleclass);
						    newentry.find('p > a').text(this.url).attr('href', this.url);
						    if (this.image == "" || this.image == undefined)
						        newentry.find('a > img').remove();
						    else
						        newentry.find('a > img').attr('src', '/' + this.image);
						    newentry.css('display', 'block');
						    newentry.removeClass('search-result-item-template');
						    newentry.addClass('search-result-item');
						    parent.append(newentry);
						}

					);

			            //move the nav to the end
			            parent.append(nav);

			            TABLE.paginate(resultsperpage);
			        }
			        else {
			            $('.search-result-nav').hide('fast');

			            var newentry = template.clone();
			            newentry.find('a').remove();
			            newentry.find('p span').html(results[0].teaser);
			            newentry.css('display', 'block');
			            newentry.removeClass('search-result-item-template');
			            newentry.addClass('search-result-item');
			            parent.append(newentry);

			            $('#srchresultscount > span').text('0');
			        }

			        $('.search-wait').hide('fast');
			    },
			    beforeSend: function () {
			        $('.search-wait').show('fast');
			    }
			}
		);
}

//Table pagination object used for search results.  - Bill Ehrreich, behrreich@bgtpartners.com
var TABLE = {};
TABLE.setPagers = function (current, pageLength, rows) {
    var last = ((current * pageLength + pageLength) > rows.length) ? rows.length.toString() : (current * pageLength + pageLength).toString()
    var $nav = $('div.search-result-nav');
    $nav.find('span.display-text').text('Displaying '
		+ (current * pageLength + 1).toString() + ' - '
		+ last
		+ ' of ' + rows.length.toString());
},
TABLE.paginate = function (pageLength) {
    // 1. Set up paging information
    var $table = $('div.search-result-box-right');
    var $rows = $('div.search-result-item').not(':hidden'); //remove template item
    var numPages = Math.ceil($rows.length / pageLength) - 1;
    var current = 0;

    // 2. Set up the navigation controls
    var $nav = $('div.search-result-nav');
    var $back = $nav.find('li:first-child a');
    var $next = $nav.find('li:last-child a');
    $('#srchresultscount > span').text($rows.length.toString());



    //build a list of pages
    $nav.find('li').not('li:first-child').not('li:last-child').remove();
    for (var i = numPages + 1; i >= 1; i--) {
        var el = $('<li/>');
        var a = $('<a />');
        a.attr('href', 'javascript:void(0);')

        if (i == current + 1) {
            a.addClass('current-page');
        }

        a.text(i.toString())
			.click
			(
				function () {
				    current = parseInt($(this).text()) - 2;
				    pagination('>');
				}
			)
			.css('width', '25px')
			.appendTo(el);


        //place new page indicator after the back link
        $back.parent().after(el);
    }


    TABLE.setPagers(current, pageLength, $rows);

    //Attach previous and next click handlers if they have not already been attached
    $back.addClass('paging-disabled');

    if (typeof ($back.data("events")) == "undefined") {
        $back.click
		(
			function () {
			    pagination('<');
			}
		);
    }

    if (typeof ($next.data("events")) == "undefined") {
        $next.click(function () {
            pagination('>');
        });
    }

    // 3. Show initial rows
    $rows
		.hide()
		.slice(0, pageLength)
		.show();

    pagination = function (direction) {
        reveal = function (current) {

            $back.removeClass("paging-disabled");
            $next.removeClass("paging-disabled");

            $rows
				.hide()
				.slice(current * pageLength, current * pageLength + pageLength)
				.show();

            TABLE.setPagers(current, pageLength, $rows);

            //Reset the hightlighted page button
            $nav.find('li a').removeClass('current-page');
            //var sel = 'li a[text=' + (current + 1).toString() + ']';
            var sel = 'li a:eq(' + (current + 1).toString() + ')';
            $nav.find(sel).addClass('current-page');


        };
        // 4. Move previous and next  
        if (direction == "<") { // previous
            if (current > 1) {
                reveal(current -= 1);
            }
            else if (current == 1) {
                reveal(current -= 1);
                $back.addClass("paging-disabled");
            }
        } else { // next


            if (current < numPages - 1) {

                reveal(current += 1);
            }
            else if (current == numPages - 1) {
                reveal(current += 1);
                $next.addClass("paging-disabled");
            }
        }
    }

};


/*close*/
var iframeOpen = false;

/*function closeDialog(cid){
$('#'+cid).dialog('close');
return false;		
}*/

/* =jQuery Plugins*/
(function ($) {
    /* = plug-in : overlabel */
    $.fn.overlabel = function (options) {
        var opts = $.extend({}, $.fn.overlabel.defaults, options);
        var selection = this.filter('label[for]').map(function () {
            var label = $(this);
            var id = label.attr('for');
            var field = $('#' + id);
            if (!field) return;
            var o = $.meta ? $.extend({}, opts, label.data()) : opts;
            label.addClass(o.label_class);
            var hide_label = function () { label.css(o.hide_css) };
            var show_label = function () { this.value || label.css(o.show_css) };
            $(field)
					.parent().addClass(o.wrapper_class).end()
					.focus(hide_label).blur(show_label).each(hide_label).each(show_label);
            return this;
        });
        return opts.filter ? selection : selection.end();
    };
    $.fn.overlabel.defaults = {
        label_class: 'overlabel-apply',
        wrapper_class: 'overlabel-wrapper',
        hide_css: { 'display': 'none' },
        show_css: { 'display': 'block' },
        filter: false
    };
    /********EQUALIZE************/
    $.fn.equalizeCols = function () {
        var height = 0,
			reset = $.browser.msie ? "1%" : "auto";

        return this
			.css("height", reset)
			.each(function () {
			    height = Math.max(height, $(this).outerHeight(true));
			})
			.css("height", height)
			.each(function () {
			    var h = $(this).outerHeight(true);
			    if (h > height) {
			        $(this).css("height", height - (h - height));
			    };
			});
    };
    /************* Custom Fade ******** start *********/
    $.fn.customFadeIn = function (speed, callback) {
        $(this).fadeIn(speed, function () {
            if (!$.support.opacity)
                $(this).get(0).style.removeAttribute('filter');
            if (callback != undefined)
                callback();
        });
    };
    $.fn.customFadeOut = function (speed, callback) {
        $(this).fadeOut(speed, function () {
            if (!$.support.opacity)
                $(this).get(0).style.removeAttribute('filter');
            if (callback != undefined)
                callback();
        });
    };
    $.fn.customFadeTo = function (speed, to, callback) {
        return this.animate({ opacity: to }, speed, function () {
            if (to == 1 && jQuery.browser.msie)
                this.style.removeAttribute('filter');
            if (jQuery.isFunction(callback))
                callback();
        });
    };
    /********FIX PNG************/
    jQuery.fn.pngFix = function (settings) {

        // Settings
        settings = jQuery.extend({
            blankgif: 'blank.gif'
        }, settings);

        var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
        var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

        if (jQuery.browser.msie && (ie55 || ie6)) {

            //fix images with png-source
            jQuery(this).find("img[src$='.png']").each(function () {

                jQuery(this).attr('width', jQuery(this).width());
                jQuery(this).attr('height', jQuery(this).height());

                var prevStyle = '';
                var strNewHTML = '';
                var imgId = (jQuery(this).attr('id')) ? 'id="' + jQuery(this).attr('id') + '" ' : '';
                var imgClass = (jQuery(this).attr('class')) ? 'class="' + jQuery(this).attr('class') + '" ' : '';
                var imgTitle = (jQuery(this).attr('title')) ? 'title="' + jQuery(this).attr('title') + '" ' : '';
                var imgAlt = (jQuery(this).attr('alt')) ? 'alt="' + jQuery(this).attr('alt') + '" ' : '';
                var imgAlign = (jQuery(this).attr('align')) ? 'float:' + jQuery(this).attr('align') + ';' : '';
                var imgHand = (jQuery(this).parent().attr('href')) ? 'cursor:hand;' : '';
                if (this.style.border) {
                    prevStyle += 'border:' + this.style.border + ';';
                    this.style.border = '';
                }
                if (this.style.padding) {
                    prevStyle += 'padding:' + this.style.padding + ';';
                    this.style.padding = '';
                }
                if (this.style.margin) {
                    prevStyle += 'margin:' + this.style.margin + ';';
                    this.style.margin = '';
                }
                var imgStyle = (this.style.cssText);

                strNewHTML += '<span ' + imgId + imgClass + imgTitle + imgAlt;
                strNewHTML += 'style="position:relative;white-space:pre-line;display:inline-block;background:transparent;' + imgAlign + imgHand;
                strNewHTML += 'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;';
                strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + jQuery(this).attr('src') + '\', sizingMethod=\'scale\');';
                strNewHTML += imgStyle + '"></span>';
                if (prevStyle != '') {
                    strNewHTML = '<span style="position:relative;display:inline-block;' + prevStyle + imgHand + 'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;' + '">' + strNewHTML + '</span>';
                }

                jQuery(this).hide();
                jQuery(this).after(strNewHTML);


            });

            // fix css background pngs
            jQuery(this).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')";
                }
            });

            //fix input with png-source
            jQuery(this).find("input[src$='.png']").each(function () {
                var bgIMG = jQuery(this).attr('src');
                jQuery(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\'scale\');';
                jQuery(this).attr('src', settings.blankgif)
            });

        }

        return jQuery;

    };
    /**************************/
})(jQuery);

//OPEN PDF ON NAMED WINDOW
function openNewWindow(itemUrl, windowName) {
    $.ajax({
        url: "/CaptureUserDownloads.aspx?itemUrl=" + itemUrl,
        success: function () {
            //$(this).addClass("done");
        }
    });

    window.open(itemUrl, windowName);
}

function setSiteNavigationSolutionWidth() {
    var ctrl0 = $("td[id*=siteNavigation_lvSolutions_ctrl0_tdSolution]");
    var ctrl1 = $("td[id*=siteNavigation_lvSolutions_ctrl1_tdSolution]");

    if (ctrl0 != null && ctrl0 != undefined)
        ctrl0.width("125px");

    if (ctrl1 != null && ctrl1 != undefined)
        ctrl1.width("145px");
}

function SetDialogURL() {
    if (document.getElementById('hlnkContactUs') != null || document.getElementById('hlnkContactUs') != undefined)
        document.getElementById('hlnkContactUs').href = "/contactdialog.aspx?l=" + window.location.href;
}

function setDefaultTab(thisTab) {
    $(document).ready(function () {
        var expertise = "tabs1-home";
        var services = "tab2-home";
        var publications = "tab3-home";

        var openTab = expertise;

        if (thisTab == "services")
            openTab = services;
        else if (thisTab == "publications")
            openTab = publications;

        $(".tabs-home").tabs("select", openTab);
    });
}

//Functions used for JSON sorting of search results.  behrreich
jQuery.fn.sort = function () {
    return this.pushStack([].sort.apply(this, arguments), []);
};

//Sort by # matches, updated date asc
function sortMatches(a, b) {
    if (a.matches > b.matches) {
        return 1;
    }
    else if (a.matches < b.matches) {
        return -1;
    }
    else {
        if (a.updated > b.updated) {
            return 1;
        }
        else if (a.updated < b.updated) {
            return -1;
        }
        else {
            return 0;
        }
    }
};

//Reverse of above
function sortMatchesDesc(a, b) {
    return sortMatches(a, b) * -1;
};

