var minBrowserWidth = 1000;
var minBrowserHeight = 540;
var isIE7 = false;
var isIE = false;
var isMozilla = false;
var resizeFooter = true;


/* Invoke Scrollbar when the page is too small */
function checkWidth() {
	if(isIE7 || isIE) {
		window.scrollTo(0,0);
	}

	var browserWidth = $(window).width();
	var browserHeight = $(window).height();
	
	var currBrowserWidth = $(window).width() < minBrowserWidth ? minBrowserWidth : $(window).width();	
	var currBrowserHeight = $(window).height() < minBrowserHeight ? minBrowserHeight : $(window).height();

	var docWidth = $("body").width();
	var docHeight = $("body").height();
	
	var viewWidth = $(window).scrollLeft() + $(window).width();
	var viewHeight = $(window).scrollTop() + $(window).height();
	
	var clipWidth = currBrowserWidth;
	var clipHeight = currBrowserHeight;
	
	if(browserWidth < minBrowserWidth) {
		$("body").css("width", minBrowserWidth +"px");
		
		$("#find_dealer_widget").css({
			'left': '773px',
			'right': 'auto'	
		});
		$("#search_site_widget").css({
			'left': '773px',
			'right': 'auto'	
		});
		$("html").css({'overflow-x': 'auto'});
		$("#background-image").css("min-width", browserWidth+"px");
		$("#background-image").css("width", minBrowserWidth);
		
	} else {
		$("body").css("width", browserWidth +"px");
		$("#find_dealer_widget").css({
			'right': '20px',
			'left': 'auto'	
		});
		$("#search_site_widget").css({
			'right': '20px',
			'left': 'auto'	
		});
		$("html").css({'overflow-x': 'hidden'});
		$("#background-image img").css("min-width", browserWidth+"px");
		$("#background-image").css("width", currBrowserWidth);
	}
	// set background image height so it matches visible area or content space
	//alert($(document.body).height() +" / "+ $(window).height())
	if($(document.body).height() < $(window).height()) {
		$("#background-image").css({"height":$(window).height()+"px"});
	} else {
		$("#background-image").css({"height":$(document.body).height()+"px"});
	}
	// set footer width
	$("#page_footer").css("width", clipWidth +"px");
	
	// tire selector layout
			
	if(typeof(nav_TabName) != 'undefined'){
		$("body").addClass(nav_TabName);
		
		if (nav_TabName == 'tires-page' || nav_TabName == 'tire-layout tires-page'){
			$("#nav-tires a").addClass("selected");
		}
		
		if (nav_TabName == 'explore-page' || nav_TabName == 'tire-layout explore-page'){
			$("#nav-explore a").addClass("selected");
		}
		
		if (nav_TabName == 'news-page' || nav_TabName == 'tire-layout news-page'){
			$("#nav-news a").addClass("selected");
		}
		
		if (nav_TabName == 'heritage-page'){
			$("#nav-heritage a").addClass("selected");
		}
		
		if (nav_TabName == 'contact-us-page'){
			$("#nav-contact a").addClass("selected");
		}
	}
	$("body.tire-layout #scrollable").css("width", currBrowserWidth +"px");
	$("body.tire-layout #scrollable").css("height", currBrowserHeight +"px");
	$("body.tire-layout #page_wrapper").css({
		'width': '100%',
		'height': currBrowserHeight
	});
	
	if(currBrowserWidth <= 1024){
		$("#page_footer ul.horizontal li").css("margin-right","5px")
	}
}


/* Lock Footer to the bottom of the page if the content is smaller than the browser */
function footerLock() {
	if(resizeFooter) {
       var footerHeight = 0,
           footerTop = 0,
           $footer = $("#page_footer");

	
		var layout = $("body").attr("class");
		
		footerHeight = $('#page_footer').height();
		footerPos = ($(window).scrollTop() + $(window).height() - ($('#page_footer').height()) - ($('#main-menu').height()) );
		footerTop = (footerPos - 6) + "px";

		if ( ($(document.body).height()+footerHeight) < ($(window).height() + 30)) {
		   $footer.css({
				"position": "absolute",
				"top": footerTop,
				"margin-bottom": "0"
		   });
		   $(".swoosh").css({"position": "absolute"});
		   //$("html").css({"overflow-y" : "hidden"});
		} else {
		   $footer.css({
				"position": "relative"
		   });
		   $(".swoosh").css({"position": "absolute", "right": "0", "bottom": "0px", "z-index": "-1"});
		   //$("html").css({"overflow-y" : "auto"});
		}
	}
}


/* Manages page events based on browser size */
 $(document).ready(function() {
 
	try{Typekit.load();}catch(e){};

 	var isIE7 = navigator.userAgent.match("MSIE 7") ? true : false;
	var isChrome = navigator.userAgent.toLowerCase().match("chrome") ? true : false;
	var special = $(".driver-info");
	if(special.length) {
		minBrowserHeight = special.height() + special.offset().top + 10 + $('#page_header').height();

		if(isIE7 || isChrome) {
			minBrowserHeight += 100;
		}
		resizeFooter = false;
	}

	footerLock();
	checkWidth();

	try {
	$("a[rel=lightbox]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'overlayColor'		: '#fff',
		'overlayOpacity'	: 0.7,
		'titlePosition' 	: 'inside',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="lightbox-image-counter">' + (currentIndex + 1) + ' of ' + currentArray.length + '</span><span id="lightbox-image-caption">' + (title.length ? title : ' &nbsp; ') + '</span>';
		}
	});
	} catch(err) {}
	
	//Adding the Fancybox to the large image of the media gallery on the bfg-news-article.html to run a video in lightbox
	try {
		$(".lightboxVideo").click(function() {
			var myVideo = $(this).attr('href');
			var isiDevice = /iPad|iPhone|iPod/i.test(navigator.userAgent);
			var userAgent = navigator.userAgent.toLowerCase();
			var leftPos = document.body.clientWidth/2 - 320
			var topPos = window.innerHeight/2 - 190
			var params  = 'width=640,height=380,location=no,menubar=no,scrollbars=no,titlebar=no,toolbar=no';
				params += ',left='+leftPos;
				params += ',top='+topPos;
			$("#videoPathForIdevices").val(myVideo);
			//Detect for iDevice - true
			if(isiDevice) {
				window.open("/assets/bfg/system/html/idevice_video.html","mywindow",params);
				return false;
			} else {
				$.fancybox({
					'href'		: '#ipad'
				});
				$("#ipad").attr("href", myVideo);
				flowplayer("ipad", "/assets/bfg/system/videos/flowplayer-3.2.5.swf");
				return false;
			}
		});
	} catch(err) {}
	
	$(window).resize(function() {checkWidth(); footerLock();});
	$('#btn_find_dealer').mouseover(function() {$('.navbar_widget').hide(); $('#searches img').removeClass('selected'); $('#find_dealer_widget').show();$("#searches").css("height","40px"); $('#btn_find_dealer').addClass('selected');});
	$('#btn_search_site').mouseover(function() {$('.navbar_widget').hide(); $('#searches img').removeClass('selected'); $('#search_site_widget').show(); $('#btn_search_site').addClass('selected');});
	$('#find_dealer_widget .close_button').click(function(event) {$('#find_dealer_widget').hide(); $('#searches img').removeClass('selected');$("#searches").css("height","30px");});

	$('body').click(function() {$('.navbar_widget').fadeOut(); $('#searches img').removeClass('selected');});
	$('.navbar_widget').click(function(event){
		event.stopPropagation();
		if(window.next_event) {
			clearTimeout (next_event);
		}
	});
	
	var isIE6 = navigator.userAgent.match("MSIE 6") ? true : false;
	if(isIE6){
		DD_belatedPNG.fix('#page_header, img');
	}

});

$(window).load(function(){
	if ($.browser.msie){
		var myFile = document.location.toString();
		if (myFile.match('#')) {
			// the URL contains an anchor
			var myAnchor = myFile.split('#')[1];
			if(myAnchor == "friendsofbfg"){
				$('html, body').animate({scrollTop: $("#friendsofbfg").offset().top}, 100);
			}
		};
	}
	
	var myFile = document.location.toString();	
	var myAnchor = myFile.split('#')[1];
	if(myAnchor == "user_tire_rating_reviews_panel"){
		$('html, body').animate({scrollTop: $("#user_tire_rating_reviews_panel").offset().top}, 100);
	}
	
	footerLock();
	checkWidth();
});



