/*jQuery(document).ready(function(){
	checkFormField('signup','email');
	preloadImages();
 });
*/
//preload the Brand Images for Carousel
function preloadImages() {
	for (var i = 1; i < carousel_itemList.length + 1; i++) {
		var theContainer = document.createElement('div');
		
		document.body.appendChild(theContainer);

		var whichBrand = 'brand_' + i;
		
		theContainer.style.backgroundImage = 'url("images/'+ whichBrand +'.jpg")';
		theContainer.style.position = 'absolute';
		theContainer.style.top = '-9999px';
		theContainer.style.left = '-9999px';
	}
}

// carousel var has been moved outside as .js files are not compiled for rendring in PHP //
// please view page.head.html to see the carousel

function carousel_itemVisibleInCallback(carousel, item, i, state, evt) {
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, carousel_itemList.length);
    carousel.add(i, carousel_getItemHTML(carousel_itemList[idx - 1]));
};

// This removes 'invisible' items from DOM; firefox 2 has nasty flicker -- setTimeout() does trick
function carousel_itemVisibleOutCallback(carousel, item, i, state, evt) {
    setTimeout(function() { carousel.remove(i); }, 20);
};

//Item html creation
function carousel_getItemHTML(item) {
    return ''+ item.html +'';
};

//build carousel external controls
function carousel_buildControls(carousel) {
	$('.jcarousel-control li a:first').attr('class', 'active');
	
	var carouselItems = carousel_itemList;
	$(carouselItems).each(function(index) {
		$('.jcarousel-control').append('<li><a href="">'+ (index + 1) +'</a></li>');
	});

	$('.carousel-next').bind('click', function() {
  	carousel.next();
  	return false;
  });

	$('.carousel-prev').bind('click', function() {
  	carousel.prev();
  	return false;
  });

  $('.jcarousel-control li a').bind('click', function() {
		$('.jcarousel-control li a.active').removeClass('active');
		$(this).attr('class', 'active');
		carousel.scroll($.jcarousel.intval($(this).text()));
	  return false;
	});
};

function activeItem(carousel, item, i) {
	var idx = carousel.index(i, carousel_itemList.length);
	$('.jcarousel-control li a.active').removeClass('active');
	var activeItem = $('.jcarousel-control li a').get(idx - 1);
	$(activeItem).attr('class', 'active');
};

//COUNTDOWN SALES TIMER
function inMS(secs, num1, num2) {
	s = ((Math.floor(secs/num1))%num2).toString();
  		if (s.length < 2) {
    		s = "0" + s;
  		}
  		return s;
}

function countDown(secs, id) {

	if (document.getElementById(id)) {
	
		saleEnd = "This sale has ended!";
  
		if (secs < 0) {
	    	document.getElementById(id).innerHTML = saleEnd;
    	
	    	return;
 		}
 
		DisplayFormat = "%%H%% hrs : %%M%% min : %%S%% sec";
	
		DisplayStr = DisplayFormat.replace(/%%H%%/g, inMS(secs,3600,3600));
		DisplayStr = DisplayStr.replace(/%%M%%/g, inMS(secs,60,60));
		DisplayStr = DisplayStr.replace(/%%S%%/g, inMS(secs,1,60));
	
		document.getElementById(id).innerHTML = DisplayStr;
	
		setTimeout("countDown(" + (secs-1) + ",'"+ id +"')", 1000);
	}
}
////
///
//


//form field clearing/validation
function clearInput(fieldId) {
	// Pass this function the ID of a form field to have its contents erased on focus.
	if (!document.getElementById || !document.getElementById(fieldId)) {
		return;
	}
	document.getElementById(fieldId).onfocus = function() {
		if (this.value == this.defaultValue) {
			this.value = '';
		}
	};
	document.getElementById(fieldId).onblur = function() {
		if (this.value == '') {
			this.value = this.defaultValue;
		}
	};
}

function checkFormField(formId,fieldId) {
	if (!document.getElementById || !document.getElementById(formId) || !document.getElementById(fieldId)) {
		return;
	}
	clearInput(fieldId);
	document.getElementById(formId).checkField = fieldId;
	document.getElementById(formId).onsubmit = function() {
		if (document.getElementById(this.checkField).value == document.getElementById(this.checkField).defaultValue) {
			return false;
		}
	};
}