$(function () {
    stylelists()// adds things to list items
    searchText()// search box text replace
    searchDropDown(); // Search dropdown box
    //homeSlider('#heroHome', 5000); //home hero slider
    homepageslider();
    contactMap(); // map on contact page   
});



function showDisclaimer() {
    $(function () {
        if ($.cookie('ivc_disclaimer') == null) {
            $.colorbox({
                href: "/disclaimer-lightbox.aspx",
                scrolling: true,
                width: 700,
				innerHeight: '500px',
				opacity: 1.0,
				overlayClose: false,
				escKey: false
            });
        }
    });

    $('#acceptDisclaimer').live('click', function () {
        //$.cookie('ivc_disclaimer', 'true', { expires: 7 });
        $.cookie('ivc_disclaimer', 'true');
        $.colorbox.close();
    });
}

function stylelists() {
    $('#breadcrumb li').not(':last').append('&nbsp; / &nbsp;');
};

function searchText() {
    $('#searchBox').addClass("idleField");
	$('input#searchBox').focus(function() {
		$('#searchBox').removeClass("idleField").addClass("focusField");
		if (this.value == this.defaultValue){ 
			this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	});
	$('input#searchBox').blur(function() {
		$('#searchBox').removeClass("focusField").addClass("idleField");
		if ($.trim(this.value) == ''){
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});
};

function searchDropDown() {
	
	var searchConfig = {
        interval: 50,
        sensitivity: 3,
        over: showPanel,
        timeout: 200,
        out: hidePanel
    };

    $('#search').hoverIntent(searchConfig);
	
	function showPanel() {
		$('#searchDropdown').slideDown(100);
	}
	
	function hidePanel() {
		$('#searchDropdown').slideUp(100);
	}
}

function contactMap() {
    $('div.office').not(':first').hide();

    $('#hero.map ul li span').append('<div></div>');
	$('#hero.map ul li span:first').show();
	
	/*
    var mapoverConfig = {
        interval: 80,
        sensitivity: 10,
        over: showSpan,
        timeout: 100,
        out: hideSpan
    };

    $('#hero.map ul li, #subNav ul#officesMenu li').hoverIntent(mapoverConfig);
    //$('#hero.map ul li a, #subNav ul#officesMenu li a').click(function () { return false; });

    function showSpan() {
        var thisClass = $(this).attr('class');
        $('#hero.map ul li.' + thisClass).find('span').fadeIn('slow');
        $('.office').hide();
        $('#' + thisClass + '.office').fadeIn('slow');
        $//('#subNav ul#officesMenu li.' + thisClass).addClass('active');
    };

    function hideSpan() {
        //$('#subNav ul#officesMenu li').removeClass('active');
        var thisClass = $(this).attr('class');
        $('#hero.map ul li.' + thisClass).find('span').fadeOut('slow');
    };
    */
	$('#hero.map ul li a.dot').click(function() {
		 $('#hero.map ul li span').hide();
		 var thisClass = $(this).parents('li').attr('class'); //var thisClass = $(this).attr('class');
		$('#hero.map ul li.' + thisClass).find('span').fadeIn('slow');
        $('.office').hide();
        $('#' + thisClass + '.office').fadeIn('slow');
		return false;
    });

};

/************************************************************************/
/*NOT USED*/
// HERO SLIDER   -   Requires hoverIntent  -  homeSlider(id of outer, speed)
/************************************************************************/
function homeSlider($sliderId, $cycle) {
    var $liwidth = $($sliderId + ' .slides_container div').width();
    var $noof = $($sliderId + ' .slides_container div').length;
    var $leftpos = $liwidth * (-1);
    var $visible;
    var $open = false;
    var $firstOver = true;
    var $nextbtn;
    var timer = false;

    //resize outer
    $($sliderId + ' .slides_container').width($liwidth * $noof + $liwidth);

    // adds classes and numbers to the slides and pagination items
    $($sliderId + ' #heroHolder .slides_container, ' + $sliderId + ' #heroControl').children().each(function () {
        $number = $(this).index();
        $(this).addClass("item" + $number)
    });
    $($sliderId + " #heroControl li:first").addClass('activeControl');
    $($sliderId + " #heroControl li:first a").addClass('hover');

    //copies initial ul order to object
    var $startOrder = $('.slides_container').clone();

    // adds the next prev, moves last item before first and moves to first
    $($sliderId).append('<div id="heroButtons"><a href="#" class="previous">previous</a><a href="#" class="next" style="float:right;">next</a></div>');
    $($sliderId + ' .slides_container div:first').before($($sliderId + ' .slides_container > div:last'));
    slideover_click(0);

    // home hover slide events
    var homeHeroConfig = {
        interval: 80,
        sensitivity: 4,
        over: slideover_hover,
        timeout: 0,
        out: hover_out
    };

    $($sliderId + ' #heroControl li').hoverIntent(homeHeroConfig);

    function slideover_hover() {
        if ($(this).attr('class') == 'activeControl') {
            //nutin appens if roll on current item
        }
        else {
            if ($firstOver) {
                $($sliderId + ' .slides_container').remove(); //remove set
                $($sliderId + ' #heroHolder').append($startOrder); //append original
                //Cufon.replace('.slides_container h1, .slides_container h2');
                $firstOver = false;
            };
            var $activeitem = $($sliderId + " #heroControl li.activeControl").index(); //finds active item
            $leftpos = -$liwidth * $activeitem; //calc where to move to
            slideover_click(0); //moves to active
            $nextbtn = $($sliderId + ' #heroButtons').detach(); //remove next btn, thus stopping auto slide
            var $over_item = $(this).index();
            $($sliderId + " .slides_container").animate({ left: -$liwidth * $over_item }, { duration: 600 }, { queue: false });
            $($sliderId + " #heroControl li").removeClass("activeControl");
            $(this).addClass("activeControl");
        }
    };
    function hover_out() {
    };

    // click next slide
    $($sliderId + ' a.next').click(function () {
        $($sliderId + ' .slides_container > div:last').after($($sliderId + ' .slides_container > div:first'));
        $leftpos = $leftpos + $liwidth;
        slideover_click(0);
        $leftpos = $leftpos - $liwidth;
        slideover_click(600);
        classPagination();
        return false;
    });
    function slideover_click($speed) {
        $($sliderId + " .slides_container").animate({ left: $leftpos }, { queue: true, duration: $speed });
    };
    function classPagination() {
        $visible = 'item' + Math.round($($sliderId + ' .slides_container > div:eq(1)').attr('class').split('item')[1]);
        $($sliderId + ' #heroControl li').removeClass('activeControl');
        $($sliderId + ' #heroControl li a').removeClass('hover');
        $($sliderId + ' #heroControl li.' + $visible).addClass('activeControl');
    };

    //cycle, timer animate
    function cycle() {
        $($sliderId + ' .next').click();
        timer = setTimeout(cycle, $cycle);
    };
    if ($cycle) {
        timer = setTimeout(cycle, $cycle);
    }
}
// HERO SLIDER END
/************************************************************************/

/************************************************************************/
/*replacement for hero slider - this version doesn't use any plugins*/
/************************************************************************/
var carouseltimer;

function homepageslider() {
    //add corresponding class names to the headings
    if ($('#heroHolder .slides_container div').length == $('#heroControl li').length) {
        $('#heroHolder .slides_container div').each(function () {
            $('#heroControl li:eq(' + $(this).index() + ')').addClass($(this).attr('id'));
        });
    }

    //set first slide to active
    $('#heroHolder .slides_container div:first').addClass('activeSlide');
    $('#heroControl li:first').addClass('activeControl');

    //set a timer to move to next slide
    carouseltimer = setTimeout(animateslide, 5000);

    //hover function
    $('#heroControl li').hover(function () {
        //stop timer and any unfinished animation
        clearTimeout(carouseltimer);
        $('#heroHolder .slides_container').clearQueue();
        //set header and slide to image
        $('#heroControl li').removeClass('activeControl');
        var theimage = $('#' + $(this).attr('class').split(' ')[0]);
        $(this).addClass('activeControl');
        $('#heroHolder .slides_container div').removeClass('activeSlide');
        theimage.addClass('activeSlide');
        $('#heroHolder .slides_container').animate({ left: -theimage.position().left }, { queue: false, duration: 600 });
        //timer stops completely
    }, function () {
        //nothing to do on hover out
    });
}

//move to the next slide
function animateslide() {
    //slide image
    $('#heroHolder .activeSlide').removeClass('activeSlide').addClass('prevactive');
    if ($('#heroHolder .prevactive').next().html() == null)
        $('#heroHolder .slides_container div:first').addClass('activeSlide');
    else
        $('#heroHolder .prevactive').next().addClass('activeSlide');
    $('#heroHolder .prevactive').removeClass('prevactive');
    //slide heading
    $('#heroControl li').removeClass('activeControl');
    $('#heroControl li.' + $('#heroHolder .activeSlide').attr('id')).addClass('activeControl');
    //clear unfinished animation
    $('#heroHolder .slides_container').clearQueue();
    //get left margin
    var left = $('#heroHolder .activeSlide').position.left;
    $('#heroHolder .slides_container').animate({ left: -1 * left }, { queue: false, duration: 600,
        complete: function () {
            //$('#heroHolder .slides_container').append($('#heroHolder .slides_container div:first'));
            //$('#heroHolder .slides_container').css('left', '0px');
            carouseltimer = setTimeout(animateslide, 5000);
        }
    });
}
/************************************************************************/
