var homepageSlideshowTO = null;
var homepageSlideshow = false;

$(document).ready( function() {

	dynamItScript({
		xout: {
			image: 'img/xout.gif',
			width: 17,
			height: 18,
			right: 10,
			top: 4
		},
		dialogTop: '<div class="formcontainer"><div class="formouter"><div class="formcap"></div> <div class="formbody">',
		dialogBottom: '</div><div class="formcup"></div></div></div>',
		dialogWidth: 486
	});



	// FIX PNGs on IE 6
	if( IE6 ) {
		
		document.execCommand("BackgroundImageCache",false,true);
		
		$("img[src$='.png']").each(function() {
			var src = $(this).attr('src');
			if(src.substring(0,1) != '/' && src.substring(0,7) != 'http://') { src = '/' + src; }
			$(this).attr("src", "http://www.dynamit.us/img/spacer.gif").attr("style", "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "');");
		});
	}


	// CAROUSEL -- homepage
	$('#carousel ul').dynamItCarousel({

		next: '.arrowright',
		prev: '.arrowleft',
		indexnav: 'ul#hungry',
		width: 996,
		wraparound: false,
		slideshow: false,
		slideshowtimeout: 7000,
		onafter: function(obj, i) {
			//resetSlideshow();
			if(obj.useraction.indexOf('indexnav') != -1) {
				obj.slideshow = false;
				clearTimeout(obj.sstimeout);
			}
		}
	});
	homepageSlideshowTO = setTimeout( startSlideshow, 7000 ); // start slideshow

	// CAROUSEL -- about :: brothrely love
	$('#brotherly .carousel ul').dynamItCarousel({

		next: '.arrowright',
		prev: '.arrowleft',
		width: 936,
		inview: 3

	});

	// CAROUSEL -- franchising :: testimonials
	$('#testimonials ul').dynamItCarousel({

		next: '.testarrowright',
		prev: '.testarrowleft',
		wraparound: true,
		width: 696

	});

	// CAROUSEL -- Menu
	$('#menucarousel ul').dynamItCarousel({

		next: '.arrowright',
		prev: '.arrowleft',
		indexnav: 'ul#hungry',
		wraparound: true,
		width: 996,
		onbefore: function(obj, i) {
			$('#menucarousel ul').children('li').find('.menucontent').fadeOut('slow');
		},
		onafter: function(obj, i) {
			setTimeout( function() {
				var $me = $('#menucarousel ul').children('li').eq(i);
				$me.find('.menucontent').fadeIn('slow');
			}, 1500);
		},
		oninit: function(obj, i) {
			$('#menucarousel ul').children('li').find('.menucontent').hide();
			setTimeout( function() {
				var $me = $('#menucarousel ul').children('li').eq(i);
				$me.find('.menucontent').fadeIn('slow');
			}, 2000);
		}

	});



	$("input[name*='phone']").live( 'blur', function() { fixPhone(this); } );

	$("#emp_none").click(function() {
		if(this.checked) {
			$("input.emp").attr('verify', 'none');
		} else {
			$("input.emp").attr('verify', 'nonempty');
		}
	});

	$("#feedbackbtnlink").click( openFeedback );
	$("#eclubform").submit( openEclub );

	$("#findmorelink").click( function() {
		mapLoadMore(findMoreIndex);
	});

	$("#historyvideo").click( function() {
		dynamItLoad('/pages/about/history/video.php', null, 486, 'auto', null);
		return false;
	});
});

function openFeedback() {
		dynamItLoad('forms/frm_feedback.php?captcha=off', null, 486, 'auto', function() {

			Recaptcha.create(RECAPTCHA_PUBLIC_KEY, "recaptcha_div", {
				theme: "red",
				callback: Recaptcha.focus_response_field
			});

		});
		return false;
}

function openEclub() {

		var email = '';
		if( this && this.email && this.email.value && this.email.value != 'enter your e-mail' ) { email = this.email.value; }

		dynamItLoad('forms/frm_eclub.php?captcha=off&email=' + email, null, 486, 'auto', function() {

			Recaptcha.create(RECAPTCHA_PUBLIC_KEY, "recaptcha_div", {
				theme: "red",
				callback: Recaptcha.focus_response_field
			});

		});
		return false;
}

function fixPhone(o) {

	var phone = o.value;
         var newphone;

	phone = phone.replace(/[^0-9]+/g, '');

	if(phone.length >= 7) {

		newphone = phone.substring(0,3) + '-' + phone.substring(3,6) + '-' + phone.substring(6);
		o.value = newphone;
	} else {

		o.value = '';
		if(phone.length > 1) {
			o.value = 'xxx-xxx-xxxx';
		}
	}

}


/** special homepage slideshow controls **/
function startSlideshow() {
//	homepageSlideshow = true;
//	$('.arrowright').click();
//	homepageSlideshowTO = setTimeout( startSlideshow, 7000 );
}

function stopSlideshow() {
//	homepageSlideshow = false;
//	clearTimeout(homepageSlideshowTO);
}

function resetSlideshow() {
//	if( homepageSlideshow ) {
//		clearTimeout(homepageSlideshowTO);
//		homepageSlideshowTO = setTimeout( startSlideshow, 7000 );
//	}
}


