// For toggling input boxes
jQuery.fn.toggleVal=function(focusClass){this.each(function(){$(this).focus(function()
{if($(this).val()==this.defaultValue){$(this).val("")}if(focusClass)
{$(this).addClass(focusClass)}}).blur(function(){if($(this).val()=="")
{$(this).val(this.defaultValue)}if(focusClass){$(this).removeClass(focusClass)}})})}


//returns true if numeric
function numerics(event) {
	if(document.all) { // Internet Explorer
	  var tecla = event.keyCode;
	}
	else {
	  var tecla = (event.charCode)? event.charCode: event.which; // Firefox e afins
	}

	if(tecla == ''){
	  return true;
	}

	if((tecla > 47 && tecla < 58)) { // numbers of 0 to 9 and .
	  return true;
	}
	else {
	  if ( tecla != 8  ) { // backspace
	  return false;
	  }
	  else
	   {
		return true;
	  }
	}
}



function formatItem(row) {
	return row[0] + ',' + row[1];
}


	
	
$(function() {

	$('.toggle_container').hide(); //Hide/close all containers
	
	if(!$('h1.trigger:first').hasClass('startCollapse')){
		$('h1.trigger:first').addClass('active').next().show(); //Add "active" class to first trigger, then show/open the immediate next container
	}

	//On Click
	$('h1.trigger').click(function(){
		if( $(this).next().is(':hidden') ) { //If immediate next container is closed...
			$('h1.trigger').removeClass('active').next().slideUp(); //Remove all "active" state and slide up the immediate next container
			$(this).toggleClass('active').next().slideDown(); //Add "active" state to clicked trigger and slide down the immediate next container
		}
		return false; //Prevent the browser jump to the link anchor
	});





$('a[href*=#]').live("click", function(e) {

	//prevent the "normal" behaviour which would be a "hard" jump
   e.preventDefault();
   //Get the target
   var target = $(this).attr("href").substr(1);
	//perform animated scrolling
	
	if(target =''){
	   $('html,body').animate(
	   {
			//get top-position of target-element and set it as scroll target
			scrollTop: $("a[name = '"+target+"']").offset().top
			//scrolldelay: 2 seconds
			
	   },1000,function()
	   {
			//attach the hash (#jumptarget) to the pageurl
		   location.hash = '#'+target;
			
	   });
	   
   }
   
   
});



$('input').live('keypress', function(e) {

		var element = $(this);
		var clas_s = element.attr('class');
		
		if(clas_s=='qty'){
	
			return numerics(e);
	
		}
		
	});
	
	
	$('a[href*=#]').live("click", function(e){

	//prevent the "normal" behaviour which would be a "hard" jump
		e.preventDefault();
		//Get the target
		var target = $(this).attr("href").substr(1);
		//perform animated scrolling
		
		if(target =''){
		
			$('html,body').animate(
			{
			//get top-position of target-element and set it as scroll target
			scrollTop: $("a[name = '"+target+"']").offset().top
			//scrolldelay: 2 seconds
			},1000,function()
			{
			//attach the hash (#jumptarget) to the pageurl
			location.hash = '#'+target;
			});
		}
	});

	// Toggle input box default text eg. Search this site...
	$(".toggle").toggleVal("active");

	// Fancybox Image Lightbox - for product photos
	/*
	$("a.fancybox").fancybox({
		'titleShow'		: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});
	*/
	
	//Selects this on the page load
	$('.selected_first').focus();

			
	//Fix the #hash anchor problem with Zend
	$('a[href*=#]').live("click", function(e){

		//prevent the "normal" behaviour which would be a "hard" jump
		e.preventDefault();
		//Get the target
		var target = $(this).attr("href").substr(1);
		
		//perform animated scrolling
		
		if(target =''){
			$('html,body').animate(
			{
			//get top-position of target-element and set it as scroll target
			scrollTop: $("a[name = '"+target+"']").offset().top
			//scrolldelay: 2 seconds
			},1000,function()
			{
			//attach the hash (#jumptarget) to the pageurl
			location.hash = '#'+target;
			});
		}
	});

	// Stripe table rows
	$("tbody>tr:odd").addClass('alt');

	//Initialise the slider by hiding all the child elements
	$('.slide_content').hide();

	//Toggle the divs
	$('.slide_header').toggle (function() {
		$(this).next().slideDown('fast');
	}, function() {
		$(this).next().slideUp('fast');
	});

	// Expands the form fields when clicked. Not crucial but nice effect
	$('textarea.expand').css({'height':'30px'});
	$('textarea.expand').click(function() {
		$(this).css({'height':'150px'});
	});

	$('textarea.expand').blur(function() {
		$(this).css({'height':'30px'});
	});
	

	
	
	$('.accordion .head .a').click(function() {
		$(this).next().toggle('slow');
		return false;
	}).next().hide();

});
