$(document).ready(function(){
    
    //addClass to change css on rollovers
	$(".buttonContainer").addClass('js');
	
	$(".accordionContainer").addClass('js');
	
    $('.buttonContainer a').each(function(){    
        var linkHasFocus = false;

        $(this).hover(function(){
			var $buttonElements = $(this).closest(".buttonContainer").children();			
			jQuery.each($buttonElements, function() {
				if($.browser.msie)
				{
					$(this).css("background-position-y","-34px");
				}
				else
				{
					var position = $(this).css("background-position").replace("px 0px","px -34px");
					$(this).css("background-position",position);
				}
			});
        }, 
        function() {
            if (!linkHasFocus) {
			    var $buttonElements = $(this).closest(".buttonContainer").children();			
			    jQuery.each($buttonElements, function() {
				    if($.browser.msie)
				    {
					    $(this).css("background-position-y","0px");
				    }
				    else
				    {
					    var position = $(this).css("background-position").replace("px -34px","px 0px");
					    $(this).css("background-position",position);
				    }
			    });
            }
        });

	    $(this).focus(function()
		    {
			    var $buttonElements = $(this).closest(".buttonContainer").children();			
			    jQuery.each($buttonElements, function() {
				    if($.browser.msie)
				    {
					    $(this).css("background-position-y","-68px");
				    }
				    else
				    {
				        var position = $(this).css("background-position").replace("px 0px","px -68px");
					    position = position.replace("px -34px","px -68px");
					    $(this).css("background-position",position);
				    }
			    });
			    linkHasFocus = true;
			    return false;
		    }
	    );
    	
	    $(this).blur(function()
		    {
			    var $buttonElements = $(this).closest(".buttonContainer").children();			
			    jQuery.each($buttonElements, function() {
				    if($.browser.msie)
				    {
					    $(this).css("background-position-y","0px");
				    }
				    else
				    {
					    var position = $(this).css("background-position").replace("px -68px","px 0px");
					    position = position.replace("px -34px","px 0px");
					    $(this).css("background-position",position);
				    }
			    });
			    linkHasFocus = false;
		    }
	    );
	    
	    $(this).click(function()
	        {
		        var $buttonElements = $(this).closest(".buttonContainer").children();			
		        jQuery.each($buttonElements, function() {
			        if($.browser.msie)
			        {
				        $(this).css("background-position-y","-68px");
			        }
			        else
			        {
				        var position = $(this).css("background-position").replace("px -34px","px -68px");
				        $(this).css("background-position",position);
			        }
		        });
		        $(this).blur();
	        }
	    );
	    
	
    });
    
    $("input.rollover").hover(
		function(){
			var $imageName = $(this).attr("src");
			$(this).attr("src",$imageName.replace("_click",""));
			$(this).attr("src",$imageName.replace(".jpg","_roll.jpg"));
		},
		function(){
			var $imageName = $(this).attr("src");
			$(this).attr("src",$imageName.replace("_click",""));
			$(this).attr("src",$imageName.replace("_roll",""));
		}
	);
	$("input.rollover").focus(
		function(){
			var $imageName = $(this).attr("src");
			$imageName = $imageName.replace("_roll","");
			$(this).attr("src",$imageName.replace(".jpg","_click.jpg"));
			$(this).blur();
		}
	);
	// for Safari only
	$("input.rollover").click(
		function(){
		    if($.browser.safari)
		    {
			var $imageName = $(this).attr("src");
			$imageName = $imageName.replace("_roll","");
			$(this).attr("src",$imageName.replace(".jpg","_click.jpg"));
			}
		}
	);
	
	$("input.rollover").blur(
		function(){
			var $imageName = $(this).attr("src");
			$(this).attr("src",$imageName.replace("_click",""));
		}
	);
    
    //addClass to change css on rollovers
	$(".panelbuttonContainer").addClass('js');
    
    $('.panelbuttonContainer a').each(function(){    
        var linkHasFocus = false;

        $(this).hover(function(){
			var $buttonElements = $(this).closest(".panelbuttonContainer").children();			
			jQuery.each($buttonElements, function() {
				if($.browser.msie)
				{
					$(this).css("background-position-y","-28px");
				}
				else
				{
					var position = $(this).css("background-position").replace("px 0px","px -28px");
					$(this).css("background-position",position);
				}
			});
        }, 
        function() {
            if (!linkHasFocus) {
			    var $buttonElements = $(this).closest(".panelbuttonContainer").children();			
			    jQuery.each($buttonElements, function() {
				    if($.browser.msie)
				    {
					    $(this).css("background-position-y","0px");
				    }
				    else
				    {
					    var position = $(this).css("background-position").replace("px -28px","px 0px");
					    $(this).css("background-position",position);
				    }
			    });
            }
        });

	    $(this).focus(function()
		    {
			    var $buttonElements = $(this).closest(".panelbuttonContainer").children();			
			    jQuery.each($buttonElements, function() {
				    if($.browser.msie)
				    {
					    $(this).css("background-position-y","-56px");
				    }
				    else
				    {
				        var position = $(this).css("background-position").replace("px 0px","px -56px");
					    position = position.replace("px -28px","px -56px");
					    $(this).css("background-position",position);
				    }
			    });
			    linkHasFocus = true;
			    return false;
		    }
	    );
    	
	    $(this).blur(function()
		    {
			    var $buttonElements = $(this).closest(".panelbuttonContainer").children();			
			    jQuery.each($buttonElements, function() {
				    if($.browser.msie)
				    {
					    $(this).css("background-position-y","0px");
				    }
				    else
				    {
					    var position = $(this).css("background-position").replace("px -56px","px 0px");
					    position = position.replace("px -28px","px 0px");
					    $(this).css("background-position",position);
				    }
			    });
			    linkHasFocus = false;
		    }
	    );
	    
	    $(this).click(function()
	        {
		        var $buttonElements = $(this).closest(".panelbuttonContainer").children();			
		        jQuery.each($buttonElements, function() {
			        if($.browser.msie)
			        {
				        $(this).css("background-position-y","-56px");
			        }
			        else
			        {
				        var position = $(this).css("background-position").replace("px -28px","px -56px");
				        $(this).css("background-position",position);
			        }
		        });
		        $(this).blur();
	        }
	    );
	    
	
    });
    
    
    $('.testmyline').each(function(){    
    
	    $(this).focus(function()
		    {
			    $(this).addClass('testmylineClick');				 
		    }
	    );
    	
	    $(this).blur(function()
		    {			    
			    $(this).removeClass('testmylineClick');
		    }
	    );	    
	    
	    $(this).click(function()
	        {
			    $(this).addClass('testmylineClick');
	        }
	    );	
    });
    
    $('#BBChomepageCarousel').before('<div class="BBCheaderContainer"></div>');	
	
	$('#BBChomepageCarousel .header').each(function()
	    {
		    $(this).appendTo('.BBCheaderContainer');
		}
	);
	
	$('#homepageCarouselFrames').css('borderTop','none');

	$('#BBChomepageCarousel #homepageCarouselFrames').cycle({ 
		timeout: 0,
		speed: 0,
		pause:true,	
		// next:'#homepageCarouselNext',
		// prev:'#homepageCarouselPrev',
		pager: '.BBCheaderContainer',
		pagerAnchorBuilder: function(idx, slide) { 
			// return selector string for existing anchor 
			return '.BBCheaderContainer h2:eq(' + idx + ') a'; 
		}

	});	
	
	$('.noJS').removeClass('noJS');
	
	$('.homeDoubleColumn').each(function(){
        var $biggerSim = 0;
        $('.dealContent',this).each(function(){
            if( $(this).height() > $biggerSim )
            {
                $biggerSim = $(this).height()
            }
        });
        $('.dealContent',this).height($biggerSim);
    });
    
    /* $('.accordionContainer').each(function()
        {
            $(this).find('h2 a').removeClass('close').addClass('open');
            $(this).find('.accordionContent').hide();
        }
    ); */
    
    $('.accordionContainer h2 a').click(function()
        {
            if($(this).hasClass('open'))
            {
                $(this).removeClass('open').addClass('close');
                $(this).addClass('clickOpen');
                setTimeout("blurAccordion()",200);
            }
            else
            {
                $(this).removeClass('close').addClass('open');
                $(this).addClass('clickClose');
                setTimeout("blurAccordion()",200);
            }
            $(this).closest('.accordionContainer').find('.accordionContent').slideToggle();
            return false;
        }
    );
    
    $('.accordionContainer').each(function()
        {
            $(this).find('h3 a').removeClass('close').addClass('open');
            $(this).find('.accordionInnerContent').hide();
        }
    );
    
    $('.accordionContainer h3 a').click(function()
        {
            if($(this).hasClass('open'))
            {
                $(this).removeClass('open').addClass('close');
                $(this).addClass('clickOpen');
                setTimeout("blurAccordion()",200);
            }
            else
            {
                $(this).removeClass('close').addClass('open');
                $(this).addClass('clickClose');
                setTimeout("blurAccordion()",200);
            }
            $(this).closest('h3').next('.accordionInnerContent').slideToggle();
            return false;
        }
    );
    
    $('.dealRow').each(function(){
        var $biggerSim = 0;
        $('.dealContent',this).each(function(){
            if( $(this).height() > $biggerSim )
            {
                $biggerSim = $(this).height()
            }
        });
        $('.dealContent',this).height($biggerSim);
    });

    
});

function blurAccordion()
{
    $('.accordionContainer h2 a').removeClass('clickOpen').removeClass('clickClose');
    $('.accordionContainer h3 a').removeClass('clickOpen').removeClass('clickClose');
}

