/**
 * An ajax callback that binds events to new links.
 * TODO: verify this isn't needed anymore.
 */
/*Petflow.petflowBrandsResponse = function(target, response) {
	$('#hidden-brands input.hidden-check').each(function(){
		// check.
		$('div.browse-brands input#brand-' + $(this).val()).attr('checked', true);
	});
	$('div.browse-brands input.brand-check').change(Petflow.addRemoveHiddenBrands);

	if ($('body').attr('class').indexOf('page-brands') == -1) {
		$('li.pager-next a').html('<img src="sites/all/themes/petflow/images/start/brandMore.gif" width="37" height="75" border="0" style="margin-right:12px;">');
		$('li.pager-previous a').html('<img src="sites/all/themes/petflow/images/start/brandBack.gif" width="37" height="75" border="0" style="margin-right:12px;">');
	}
};

Petflow.addRemoveHiddenBrands = function() {
	var hidden = $('#hidden-brands #hidden-brand-' + $(this).val());
	var htmlStr = $('#hidden-brands').html();
	if ($(this).is(':checked')) {
		// see if it's in the list and, if not, add it.
		if (!$(hidden).attr('id')) {
			$('#hidden-brands').html(htmlStr + '<input type="hidden" name="brands[]" id="hidden-brand-' + $(this).val() + '" class="hidden-check" value="' + $(this).val() + '" />');
		}
	} else {
		// see if it's in the list and, if so, remove it.
		if ($(hidden).attr('id')) {
			$(hidden).remove();
		}
	}
}*/

//Probably don't need this anymore.
function loadVideo(){
	var browserName = navigator.appName;
	var vid = document.getElementById('movie-placeholder');
	if(browserName == 'Microsoft Internet Explorer'){
		vid.innerHTML = '<div class="commercial-video-wrapper"><iframe id="commercial-video" src="http://www.youtube.com/embed/MTXTO_XzBnE" frameborder="0" scrolling="no" style="margin: 0px; width: 330px; height: 273px; overflow: hidden" /></div>';
	}else{
		vid.innerHTML = '<object style="height: 273px; width: 330px"><param name="movie" value="http://www.youtube.com/v/MTXTO_XzBnE"><param name="allowFullScreen" value="true"><param name="wmode" value="transparent"><embed src="http://www.youtube.com/v/MTXTO_XzBnE" type="application/x-shockwave-flash" wmode="transparent" allowfullscreen="true" width="330" height="273"></object>';
	}
}


$(document).ready(function(){
	//loadVideo();
	/* New Start Page */

	//TODO:Fix animations here, timers are messing with each other.

	if(navigator.vendor == 'Google Inc.' || navigator.vendor == 'Apple Computer, Inc.'){
		$('.tile-holders').css('margin-top', '0px');
		$('.st-main-content-title-1').css('margin-top', '5px');
		$('.st-main-content-title-2').css('margin-top', '5px');
		$('.st-main-content-title-3').css('margin-top', '5px');
		$('.st-main-content-title-4').css('margin-top', '5px');
		$('.st-main-content-right-2').css('margin-top', '12px');
		$('.st-main-content-right-4').css('margin-top', '12px');
	}else{
		$('.tile-holders').css('margin-top', '-5px');
	}

	var cl = self.setInterval('switchContent()', 3000);

	$('#st-tab-1').click(function(){
		cl = clearInterval(cl);
	});
	$('#st-tab-2').click(function(){
		cl = clearInterval(cl);
	});
	$('#st-tab-3').click(function(){
		cl = clearInterval(cl);
	});
	$('#st-tab-4').click(function(){
		cl = clearInterval(cl);
	});

	$('#st-tab-1-off').mouseenter(function(){
		cl = clearInterval(cl);
		changeContentDisplay(1);
	});
	$('#st-tab-2-off').mouseenter(function(){
		cl = clearInterval(cl);
		changeContentDisplay(2);
	});
	$('#st-tab-3-off').mouseenter(function(){
		cl = clearInterval(cl);
		changeContentDisplay(3);
	});
	$('#st-tab-4-off').mouseenter(function(){
		cl = clearInterval(cl);
		changeContentDisplay(4);
	});

	$('#st-brand-select').click(function(){
		$('#brands-wrapper').removeClass('hide');
	});
	
	$('#splash-start-here-map').click(function(){
		$('#brands-wrapper').removeClass('hide');
	});
	
	$('#main-c-arrow-map').click(function(){
		$('#brands-wrapper').removeClass('hide');
	});

	if(document.location.href.match(/#brand-select/)){
		$('#brands-wrapper').removeClass('hide');
	}

	/* End New Start Page */



	$('.views-exposed-widgets').hover(function(){
		//$('#commercial-video').css('visibility', 'hidden');
		//$('.commercial-video-wrapper').css('background', 'url("' + Petflow.rewriteBase + 'images/start/youtubePlaceholder.png")');
	},function(){
		//$('#commercial-video').css('visibility', 'visible');
		//$('.commercial-video-wrapper').css('background', '#FFFFFF');
	});

	// Start page click events for hiding/showing Quick Select and Browse Brands
	$('.start-quick-select').click(function(){
		$('.skip').addClass('hide');
		$('div#brands-wrapper').hide();
		$('div#quick-wrapper').show();
	});
	$('a.start-special-conditions').click(function(){
		$('a.start-quick-select').click();
		$('div#conditions').slideDown();
		$('div#conditions').toggle();
		//$('div#lifestyles').css('height', '110px');
	});
	//TODO: Make this dynamic, brands will probably be constantly added to the site, so doing this statically is just not the right way to do it.
	$('.start-brand-select').click(function(){
		if (Petflow.page == 'new'){
			//$('.skip').addClass('hide');
			$('div#quick-wrapper').hide();
			$('div#brands-wrapper').show();
			$("#content-wrapper").css("height", "2190px");
		}
		else if (Petflow.page == 'start'){
			$('.skip').addClass('hide');
			$('div#quick-wrapper').hide();
			$('div#brands-wrapper').show();
			//$("#content-wrapper").css("height", "2560px");
			//$("#page-wrapper").css("height", "2650px");
		}
	});
	// Show content with URL fragment
	var anchor = document.location.toString();
	if (anchor.match('#')) {
		//$('a.start-' + anchor.split('#')[1]).click();
	}

	// Enable hidden pet fields if necessary on page load.
	$('div#pet-age div').each(function(){
		togglePetFields($(this).children('input[type=checkbox]:first'));
	});

	$('div#pet-age div input[type=checkbox]').change(function(){
		togglePetFields(this);
	});

	$('div.browse-brands input.brand-check:checked').each(function(){
		// uncheck.
		$(this).attr('checked', false);
	});

	$('div.browse-brands input.brand-check').change(Petflow.addRemoveHiddenBrands);

	if ($('body').attr('class').indexOf('page-brands') == -1) {
		$('li.pager-next a').html('<img src="sites/all/themes/petflow/images/start/brandMore.gif" width="37" height="75" border="0" style="margin-right:12px;">');
		$('li.pager-previous a').html('<img src="sites/all/themes/petflow/images/start/brandBack.gif" width="37" height="75" border="0" style="margin-right:12px;">');
	}

	/*
	$('#wizard-show-all-brands').click(function() {
		var brands = $('.page-start #wizard .view-brands');
		var height = brands.height();
		if (height == 450) {
				brands.animate({height:'150px'}, 500);
		} else if (height == 150) {
				brands.animate({height:'450px'}, 500);
		}
		return false;
	});

	brandsContainer = $('#brands .view-brands .view-content');
	$('#brands #left').click(function() {
		if (!brandsWidth) brandsWidth = $('#brands .view-brands').width();
		if (brandsLeft >= 0) return false;
		brandsLeft += brandsWidth;
		brandsContainer.animate({left:brandsLeft + 'px'}, 500);
		//brandsContainer.css('left', brandsLeft + 'px');
		if (brandsLeft >= 0) $('#brands #left').hide(200);
		$('#brands #right').show(200);
		return false;
	});

	$('#brands #right').click(function() {
		if (!brandsWidth) brandsWidth = $('#brands .view-brands').width();
		if (brandsLeft <= 0 - brandsWidth * 2) return false;
		brandsLeft -= brandsWidth;
		brandsContainer.animate({left:brandsLeft + 'px'}, 500);
		//brandsContainer.css('left', brandsLeft + 'px');
		if (brandsLeft <= 0 - brandsWidth * 2) $('#brands #right').hide(200);
		$('#brands #left').show(200);
		return false;
	});*/

});

function changeContentDisplay(ct){
	var t1 = document.getElementById('st-tab-1');
	var t2 = document.getElementById('st-tab-2');
	var t3 = document.getElementById('st-tab-3');
	var t4 = document.getElementById('st-tab-4');
	var t1Off = document.getElementById('st-tab-1-off');
	var t2Off = document.getElementById('st-tab-2-off');
	var t3Off = document.getElementById('st-tab-3-off');
	var t4Off = document.getElementById('st-tab-4-off');
	if(ct == 1){
		$('div[id^="st-content-"]').addClass('hide');
		$('#st-content-1').removeClass('hide');
		$('img[id^="st-tab-"]').addClass('hide');
		$(t1).removeClass('hide');
		$(t2Off).removeClass('hide');
		$(t3Off).removeClass('hide');
		$(t4Off).removeClass('hide');
	}else if(ct == 2){
		$('div[id^="st-content-"]').addClass('hide');
		$('#st-content-2').removeClass('hide');
		$('img[id^="st-tab-"]').addClass('hide');
		$(t1Off).removeClass('hide');
		$(t2).removeClass('hide');
		$(t3Off).removeClass('hide');
		$(t4Off).removeClass('hide');
	}else if(ct == 3){
		$('div[id^="st-content-"]').addClass('hide');
		$('#st-content-3').removeClass('hide');
		$('img[id^="st-tab-"]').addClass('hide');
		$(t1Off).removeClass('hide');
		$(t2Off).removeClass('hide');
		$(t3).removeClass('hide');
		$(t4Off).removeClass('hide');
	}else if(ct == 4){
		$('div[id^="st-content-"]').addClass('hide');
		$('#st-content-4').removeClass('hide');
		$('img[id^="st-tab-"]').addClass('hide');
		$(t1Off).removeClass('hide');
		$(t2Off).removeClass('hide');
		$(t3Off).removeClass('hide');
		$(t4).removeClass('hide');
	}
}
function togglePetFields(obj) {
	if ($(obj).parent().children('input[type=checkbox]:checked').size() > 0) {
		$(obj).parent().children('input.hidden-pet').removeAttr('disabled');
	} else {
		$(obj).parent().children('input.hidden-pet').attr('disabled', 'disabled');
	}
}
var i = 1;
function switchContent(){
	if(i==5){
		i=1;
	}
	changeContentDisplay(i);
	i++;
}
