var $screenWidth = 0; var $tabletLayoutMade = false; var $mobileLayoutMade = false; var $carouselMade = false; var $overrideMade = false;
var $tabletSwitch = 984;
var $mobileSwitch = 740;

var $skipMobile = $.cookie('skipMobileCookie');
	
$(document).ready(function() {
	
	if ( $skipMobile == 'yep' ) { 
		$('meta[name=viewport]').attr('content', 'width=1024');
		$('.addmymobilelink').html('<br><a id="override_mobile" href="#mobile">View on Mobile Device</a>');	
		$overrideMade = true;
	} else if ( $skipMobile == 'tablet' ) {
		$('meta[name=viewport]').attr('content', 'width=768');
		$('.addmymobilelink').html('<br><a id="override_mobile" href="#mobile">View on Mobile Device</a>');	
		$overrideMade = true;
	}
	

	updateWidth();

	if ( $screenWidth > $tabletSwitch && $carouselMade == false ) { makeCarousel(); }
	if ( $screenWidth <= $tabletSwitch && $screenWidth > $mobileSwitch && $tabletLayoutMade == false ) { makeTabletLayout(); }
	if ( $screenWidth <= $mobileSwitch && $mobileLayoutMade == false ) {
		makeMobileLayout();
	}
	
	
	// on resize
	$(window).resize(function() {
		updateWidth();
		if ( $screenWidth > $tabletSwitch && ($tabletLayoutMade == true || $mobileLayoutMade == true) ) {
			unmakeTabletLayout();
			unmakeMobileLayout();
			if ( $carouselMade == true ) { unmakeCarousel(); }
			if ( $carouselMade == false ) { makeCarousel(); } // Updates carousel's size if coming from tablet layout - sidesteps a few cycle plugin bugs
		}
		if ( $screenWidth <= $tabletSwitch && $screenWidth > $mobileSwitch && $tabletLayoutMade == false ) {
			unmakeMobileLayout();
			makeTabletLayout();
		}
		if ( $screenWidth <= $mobileSwitch && $mobileLayoutMade == false ) {
			unmakeTabletLayout();
			makeMobileLayout();
		}
	});

		
	// overrides
	$('#override_default').live('click', function() {
		$('meta[name=viewport]').attr('content', 'width=1024, initial-scale=0.45');
		$('.hidemynav').css({ 'display': 'block' });
		$('.addmymobilelink').html('<br><a id="override_mobile" href="#mobile">View on Mobile Device</a>');	
		$.cookie('skipMobileCookie', 'yep');
		return false;
	});
	$('#override_tablet').live('click', function() {
		$('meta[name=viewport]').attr('content', 'width=768, initial-scale=0.6');
		$('.hidemynav').css({ 'display': 'block' });
		$('.addmymobilelink').html('<br><a id="override_mobile" href="#mobile">View on Mobile Device</a>');	
		$.cookie('skipMobileCookie', 'tablet');
		return false;
	});
	$('#override_mobile').live('click', function() {
		$('meta[name=viewport]').attr('content', 'width=device-width, initial-scale=1');
		$('#show_nav, #hide_nav').remove();
		$('#navigation > ul').hide();
		$('#navigation').prepend('<a id="show_nav" href="#">Show Navigation</a>');
		$('.viewfullsite').html('&nbsp;|&nbsp; <a id="override_default" href="#default">View Full Site</a>');
		$.cookie('skipMobileCookie', 'nope');
		return false;
	});

	// functions -- These had to be redeclared here to fix a problem on BlackBerry 
	
	function updateWidth() {
		$screenWidth = $(window).width();
	}
	
	function makeCarousel() {
		$('#no_carousel').hide();
		$('#carousel').show().css({ 'background-image': 'url("images/ajax-loader.gif")' }).load('includes/carousel.htm .content', function() { 
			if ( $screenWidth <= $tabletSwitch ) {
				$('#carousel .content .slides img, #carousel').css({ width: '495px', height: '270px' });
			} else {
				$('#carousel .content .slides img, #carousel').css({ width: '550px', height: '300px' });
			}
			$('#carousel .content .slides').cycle({ pause: 1, timeout: 8000, speed: 2000, pager: '#carousel .nav' });
		});
		$carouselMade = true;
	}
	function unmakeCarousel() {
		// $('#carousel .content .slides').cycle('stop').cycle('destroy');
		$('#carousel').html(' ').hide();
		$('#no_carousel').show();
		$carouselMade = false;
	}
	
	function makeTabletLayout() {
		$tabletLayoutMade = true;
		if ( $carouselMade == true ) { unmakeCarousel(); }
		makeCarousel(); // updates size if coming from default - sidesteps a few cycle bugs
	}
	function unmakeTabletLayout() {
		$tabletLayoutMade = false;
		$('#content.home .box').height('auto');
		$('.addedTablet').remove();
	}
	
	function makeMobileLayout() {
		$mobileLayoutMade = true;
		if ( $carouselMade == true ) { unmakeCarousel(); }
		if ( $overrideMade == false ) {
			$('.viewfullsite').html('&nbsp;|&nbsp; <a id="override_default" href="#default">View Full Site</a>');
			$overrideMade = true;
		}
	}
	
	
	function unmakeMobileLayout() {
		$mobileLayoutMade = false;
		$skipMobile = $.cookie('skipMobileCookie');
	}
	
});

// functions

function updateWidth() {
	$screenWidth = $(window).width();
}

function makeCarousel() {
	$('#no_carousel').hide();
	$('#carousel').show().css({ 'background-image': 'url("images/ajax-loader.gif")' }).load('includes/carousel.htm .content', function() { 
		if ( $screenWidth <= $tabletSwitch ) {
			$('#carousel .content .slides img, #carousel').css({ width: '495px', height: '270px' });
		} else {
			$('#carousel .content .slides img, #carousel').css({ width: '550px', height: '300px' });
		}
		$('#carousel .content .slides').cycle({ pause: 1, timeout: 8000, speed: 2000, pager: '#carousel .nav' });
	});
	$carouselMade = true;
}
function unmakeCarousel() {
	$('#carousel .content .slides').cycle('stop').cycle('destroy');
	$('#carousel').html(' ').hide();
	$('#no_carousel').show();
	$carouselMade = false;
}

function makeTabletLayout() {
	$tabletLayoutMade = true;
	if ( $carouselMade == true ) { unmakeCarousel(); }
	makeCarousel(); // updates size if coming from default - sidesteps a few cycle bugs
}
function unmakeTabletLayout() {
	$tabletLayoutMade = false;
	$('#content.home .box').height('auto');
	$('.addedTablet').remove();
}

function makeMobileLayout() {
	$mobileLayoutMade = true;
	if ( $carouselMade == true ) { unmakeCarousel(); }
	if ( $overrideMade == false ) {
		$('.viewfullsite').html('&nbsp;|&nbsp; <a id="override_default" href="#default">View Full Site</a>');
		$overrideMade = true;
	}
}


function unmakeMobileLayout() {
	$mobileLayoutMade = false;
	$skipMobile = $.cookie('skipMobileCookie');
}
