//check for ready state
$(document).ready(function() {
	init();
});

//var Brand, Slideshow, GuardCalculator, VoiceController;

function init()
{	
	/*@cc_on
	@if (@_jscript_version <= 5.6)
		// js code for IE6 or down
		browserIE6();
	@else @*/
		// js code for IE7 and non-IE browsers
		$('div.col1 div.node:last').addClass('last');							
	/*@end @*/	
	
	if($('#container_header').length){
		this.Brand = new brand();
	}
	
	if($('#slideshow').length){
		this.Slideshow = new slideshow();		
	}
	
	if($('#calculator').length){
		this.GuardCalculator = new guardCalculator();
	}
	
	var tA = $('.voicecommands');
	if(tA.length){
		this.VoiceController = new voiceController();
	}
	
	if($('#search').length){
		this.Search = new searchController();
	}
	
}

function searchController()
{
	//add event listener yo
	var obj = this;
	
	$('#search').bind('keypress',function(e) { 

		if(e.which == 13){
			obj.submitSearch();
			e.stopPropagation();
			return false;
		}else{
			//kick the interval for live search
			clearInterval(obj.searchInterval);
			obj.searchInterval = setInterval(function(){ obj.submitSearch.apply(obj) },250);
			
		}
	});
	
	$('#form_search .submit').bind('click',function(e) { 
		obj.submitSearch();
		e.stopPropagation();
		return false;
	});
	
	$('#form_search .reset').bind('click',function(e) { 
		obj.resetSearch();
		e.stopPropagation();
		return false;
	});
	
	$('#search').focus();
	if($('#search').val() != ''){
		this.submitSearch();
	}
	
	//change type = submit to type = button
}

searchController.prototype.resetSearch = function()
{
	$('#search').val('');
	$('#search').focus();
	this.submitSearch();
}

searchController.prototype.handleSearch = function(data,textStatus)
{
	if(textStatus == "success"){
		$('#search_results').replaceWith('<div id="search_results">'+data+'</div>');
	}
}

searchController.prototype.submitSearch = function()
{	
	clearInterval(this.searchInterval);
	var f = $('#form_search');
	/*
	var v = f.validate({
		errorElement: 'span',
		errorPlacement: function(error, element) {
			error.appendTo( element.parent().prev().children() );
		}
	});
	*/
	
	//if(f.valid()){
		$.post(f.attr('action'),f.serialize(),this.handleSearch,"html");
		//var obj = $('#submit');
		//obj.attr('value','Sending');
		//obj.attr('disabled','disabled');
	//}
	
}


function voiceController()
{
	this.soundA = [];
	//find items, add em up yo
	var tA = $('.voicecommands');
	for(var i=0;i<tA.length;i++)
	{
		this.registerClip($(tA[i]).attr('rel'));
	}
}

function playMedia()
{
	this.Brand.stopShow();
}
function stopMedia()
{
	this.Brand.playShow();
}

function playClip(id)
{
	VoiceController.playClip(id);
}

voiceController.prototype.registerClip = function(name)
{
	this.soundA.push(name);
}

function kill(name)
{
	var obj = swfobject.getObjectById(name);
	obj.kill();
}

voiceController.prototype.playClip = function(name)
{
	for(var i=0;i<this.soundA.length;i++){
		if(this.soundA[i] != name){
			kill(this.soundA[i]);
		}
	}
}

function slideshow()
{
	this.chapterID = 0;
	this.slideID = 0;
	var obj = this;
	var chapterA = $('#slideshow .nav > li');
	//go through chapters
	$.each(chapterA,function(index){
		
		//do top item
		$(chapterA[index]).children().bind('click',function(e) { 
			obj.viewChapter.apply(obj,[index]); 
			e.stopPropagation();
			return false;
		});
		if(index > 0){
			$(chapterA[index]).find('ol').hide();
		}		
		//do slide nav
		var slideA = $(chapterA[index]).find('ol a');		
		$.each(slideA,function(index2){
			$(slideA[index2]).bind('click',function(e) { 
				obj.viewSlide.apply(obj,[index,index2]); 
				e.stopPropagation();
				return false;
			});
		});
		
	});
	//fade in nav
	var nav = $('#slideshow ul.nav');
	$($(nav).children()[0]).addClass('active');
	$($($(nav).children()[0]).find('ol a')[0]).addClass('active');
	$(nav).animate({opacity:1.0},1500,'');
	
	$('#slideshow .content .learnmore').bind('click',function(e) { 
		obj.learnMore.apply(obj); 
		e.stopPropagation();
		return false;
	});
}

slideshow.prototype.learnMore = function()
{
	var slide = $($($('#slideshow ul.slides').children()[this.chapterID]).find('li')[this.slideID]);
	window.location = $($(slide).children()[0]).attr('href');	
}

slideshow.prototype.viewChapter = function(id)
{
	this.chapterID = id;
	$('#slideshow ul.slides').animate({'left': -(id*464),'top':0},500,'');
	//create subnav
	var nav = $('#slideshow ul.nav').children()[id];

	var navA = $('#slideshow ul.nav li');
	for(var i=0;i<navA.length;i++){
		$(navA[i]).removeClass('active');
		$(navA[i]).find('ol').hide('fast');
	}
	
	$(nav).addClass('active');
	$(nav).find('ol').show('def');
	
	//animate her open yo
	this.trackSlideNav(id,0);
}

slideshow.prototype.viewSlide = function(chap,id)
{
	$('#slideshow ul.slides').animate({'left': -(chap * 464),'top': -(id*600)},500,'');
	this.trackSlideNav(chap,id);
}

slideshow.prototype.trackSlideNav = function(chap,slide)
{
	this.slideID = slide;
	var chapter = $('#slideshow ul.nav').children()[chap];
	var navA = $(chapter).find('ol a');
	for(var i=0;i<navA.length;i++){
		
		if(i == slide){
			$(navA[i]).addClass('active');
		}else{
			$(navA[i]).removeClass('active');
		}
	}
	
}

function browserIE6()
{
	$('ul.nav_sub li:first').addClass('first');	
}

/* Brand object, controls header*/
function brand()
{
	this.playing = true;
	this.swapInterval = '';
	this.imgA = ['b1','b2','b3','b4','b5','b6','b7','b8','n1','n2','n3','n4','n6','n7','n8','n9','n10','n11','n12','n13','n14','n15','n16','n17','n18','n19','n20','n21','n22'];
	this.imgIndex = 0;
	this.init();
}

brand.prototype.stopShow = function()
{
	this.playing = false;
	clearInterval(this.swapInterval);
}

brand.prototype.playShow = function()
{
	this.playing = true;
	var obj = this;
	clearInterval(this.swapInterval);
	this.swapInterval = setInterval(function(){ obj.swapSet.apply(obj) },14000);
}

brand.prototype.kickImage = function(elem)
{
	var obj = this;
	var e = elem;
	setTimeout(function() { obj.fadeItemOnce.apply(obj,[e]); },Math.random() * 500);
}

brand.prototype.kickToggle = function(elem,index)
{
	var obj = this;
	var e = elem;
	var i = index;
	setTimeout(function() { obj.toggleItem.apply(obj,[e,i]); },Math.random() * 500);
}

brand.prototype.init = function()
{
	this.imgA.sort(function() {return 0.5 - Math.random()});
	
	var doFade = false;
	if($('#body.controller_index.action_index').length){
		doFade = true;
	}
	
	for(var i=0;i<6;i++){
		var elem = $('#b'+(i+1));		
		if(doFade == true){
			elem.css('opacity','0.0');
		}
		elem.css('background-image','url(/files/brand_images/image/' + String(this.imgA[this.imgIndex] + '.jpg') +')');		
		if(doFade == true){
			this.kickImage(elem);
		}	
		this.imgIndex++;
	}	
	
	//this.playShow();
	var obj = this;
	this.swapInterval = setInterval(function(){ obj.swapSet.apply(obj) },14000);
}

brand.prototype.fadeItemOnce = function(elem)
{
	elem.animate({opacity: 1.0},500);
}

brand.prototype.toggleItem = function(elem,index)
{
	var obj = this;
	elem.fadeOut(500,function() { elem.css('background-image','url(/files/brand_images/image/' + String(obj.imgA[index] + '.jpg') +')'); elem.fadeIn(500); });
}

brand.prototype.swapSet = function()
{
	if(this.playing == true){
		//clearInterval(swapInterval);
		if((this.imgA.length - this.imgIndex) <= 6){
			//shuffle
			this.imgA.sort(function() {return 0.5 - Math.random()});
			this.imgIndex = 0;
		}
		//go through and set intervals for a shuffled set - fadeItem
		for(var i=0;i<6;i++){
			var elem = $('#b'+(i+1));
			this.kickToggle(elem,this.imgIndex);		
			this.imgIndex++;
		}
	}
	
}

function handleAjax(data,textStatus)
{
	if(textStatus == "success"){
		$('#form_wrapper').slideUp();
		$('#contact_message').append(data);
		//$pageTracker._trackPageview("/water-chestnuts.php");
	}
}

function submitContact()
{
	var f = $('form.validate');
	var v = f.validate({
		errorElement: 'span',
		errorPlacement: function(error, element) {
			error.appendTo( element.parent().prev().children() );
		}
	});
	
	if(f.valid()){
		$.post(f.attr('action'),f.serialize(),handleAjax,"html");
		var obj = $('#submit');
		obj.attr('value','Sending');
		//obj.attr('disabled','disabled');
	}
	
}