// apostropheReady is called at domReady
// it hooks into the a_js javascript framework
// it can be used for progressive enhancements at runtime
// such as Cufon text replacement

function apostropheReady()
{

}

function addLoadEvent(func) {
	  var oldonload = window.onload;
	  if (typeof window.onload != 'function') {
	    window.onload = func;
	  } else {
	    window.onload = function() {
	      if (oldonload) {
	        oldonload();
	      }
	      func();
	    }
	  }
}

	addLoadEvent(function() {
        scroll_companies();

	  /* code to run on page load */
		$(".scrollable").scrollable().navigator();
        $(".solutionsscroll").scrollable().navigator();
		//$("#companiesscroll").scrollable({circular: true}).autoscroll({ autoplay: true, interval: 10000});

        $(".a-slot.product").bind('mouseenter', function() {
            $(this).stop();
            $(this).animate({backgroundColor: "#C9EAF7"}, 300);
        });
        $(".a-slot.product").bind('mouseleave', function() {
            $(this).stop();
            $(this).animate({backgroundColor: "#ffffff"}, 300);
        });

        banner_size = $("#banner_thumbs").children().length;
        banner_index = banner_size>1?1:0;
        banner_timer = null;
        $('.banner_thumb:first-child').click();

		//$('#menu a').css({backgroundPosition:"0px 50px"});

		$('#menu a').mouseover(function() {
				if ($(this).parent().hasClass("a-current-page")) return;
                $(this).stop().animate(
					{backgroundPosition:"0px 0px"},
					500
				)
		});

		$('#menu a').mouseout(function() {
                if ($(this).parent().hasClass("a-current-page")) return;
				$(this).stop().animate({backgroundPosition:"0px 50px"}, 500);
		});

	});

    function subnav(){
        $('.a-subnav-wrapper .a-nav-item > ul').parent().addClass("parent");
        $('.a-subnav-wrapper .a-nav-item > a').bind('click', function() {
            if ($(this).next().hasClass("a-nav-subnav")){
                $(this).next().slideToggle();
                $(this).parent().toggleClass("open");
                return false;
            }else{
                return true;
            }
        });
    }

    function companies(){
        $("#companies img")
            .fadeTo(500, 0.3)
            .hover(function () {
                $(this).stop();
                $(this).fadeTo(500, 1);
            }, function () {
                $(this).stop();
                $(this).fadeTo(500, 0.3);
        });
    }


    function bannerAnimate(){
        if (banner_size>2 && !authenticated){
            $(".banner_thumb:nth-child("+banner_index+")").click();
        }
    }

    function showBanner(button, slot){
        if (banner_index<banner_size)
            banner_index++;
        else
            banner_index = 1;
        $('#banner .a-slot.aExImageWithText').fadeOut('normal');
        slot.fadeIn('normal');
        clearTimeout(banner_timer);
        banner_timer=setTimeout("bannerAnimate()",10000);
        $('.banner_thumb').stop();
            $('.banner_thumb').removeClass("selected");
        button.stop();
            button.addClass("selected");
    }

    function getInt(string) {
            if (typeof string == "undefined" || string == "")
                return 0;
            var tempInt = parseInt(string);

            if (!(tempInt <= 0 || tempInt > 0))
                return 0;
            return tempInt;
     }

    function scroll_companies(){
        if ($("body").hasClass("logged-in")) return;
        comp_width = 0;
        comp_height = 0;
        $(".a-area-footer .a-slots").children().each(
                function(i){
                    comp_width += $(this).width() + getInt($(this).css("padding-left")) + getInt($(this).css("padding-left"));
                    item_height = $(this).height() + getInt($(this).css("padding-top")) + getInt($(this).css("padding-bottom"));
                    if (item_height > comp_height) comp_height = item_height;
                }
        );
        if ($("#companies").width() > comp_width) return;

        companies1 = $(".a-area-footer");
        companies1.width(comp_width);
        companies1.height(comp_height);
        companies1.css("position", "absolute");
        companies1.css("top", 0);
        companies1.css("left", 0);
        companies2 = companies1.clone(true);
        $("#companies").append(companies2);
        companies1.css("left", -comp_width+"px");

        var comp_timer = setTimeout("scroll()",2000);
    }

    function scroll(){
        if (companies1.position().left > companies2.position().left){
            companies1.css("left", companies1.position().left+1+"px");
            companies2.css("left", companies1.position().left-comp_width+"px");

        }else{
            companies2.css("left", companies2.position().left+1+"px");
            companies1.css("left", companies2.position().left-comp_width+"px");
        }
        if (companies1.position().left > $("#companies").width()) companies1.css("left", companies2.position().left-comp_width+"px");
        if (companies2.position().left > $("#companies").width()) companies2.css("left", companies1.position().left-comp_width+"px");
        comp_timer = setTimeout("scroll()",30);
    }
