// JavaScript Document
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-25176416-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();



$(document).ready(function(){
	
	
	init_second_nav();
	init_list_rotator();
	
	init_list_rotator2();
	init_email_spam_catch();
	
	init_product_hider();
	
	$.clearFields();
	
	$('.numonly, .frm_qty input').each(function(){
		
		$(this).keypress(function(e){
			return checkIt(e);
		});
		
	});
	
	
	
});


var hider_height = 80;

function init_product_hider(){
	
	$('.product-item').each(function(){
		
		var theid = $(this).attr('id');
		
		$(this).find('h2').each(function(){
			var thehtml = $(this).html();
			$(this).html('<a href="#" class="p-hider" rel="' + theid + '">' +thehtml +'</a>');
		});
		
		
		$(this).after('<div class="fade-to-white"><div></div></div>');
		
	});
	
	
	//original image width must be stored for resize animation
	$('.p-image').each(function(){
		
			var parentid = $(this).closest('.product-item').attr('id');
			//$(this).insertBefore('');
			$(this).wrap('<a href="#" class="p-hider" rel="'+parentid+'"></a>');
			$(this).attr('rel', $(this).width() ); //rel stores original width
			
	});
	
	
	$('.p-hider').each(function(){
		
		var theid = $(this).attr('rel');
		var oldHeight = $('#' + theid).height();
	
		
		
		
		$(this).click(function(e){
			e.preventDefault();
			
			
			if($('#' + theid).height() < oldHeight){
				
				//resize the height
				
				$('#' + theid).animate({'height': oldHeight},500,function(){product_hider_refresh_viewmore()});
				
				
				
					$('#' + theid).attr('title','shown');
					
					//resize the image
					$('#' + theid).find('.p-image').each(function(){
						
						var aspect = $(this).height() / $(this).width();
						var originalWidth = $(this).attr('rel');
						
						$(this).animate({'margin-left': 0, 'width' : originalWidth , 'height' : originalWidth*aspect},500);
						
				
					});
				
				
			
				
			}else{
				
				
				$('#' + theid).animate({'height': hider_height},500,function(){product_hider_refresh_viewmore()});
				
			
					
					$('#' + theid).attr('title','');
					
					$('#' + theid).find('.p-image').each(function(){
						var aspect = $(this).height() / $(this).width();
						var oldWidth = $(this).width();
						var newWidth =  hider_height/aspect;
						var margin = (oldWidth - newWidth ) / 2;
						
						$(this).animate({'margin-left': margin, 'width' : hider_height/aspect , 'height' : hider_height},500);
					});
			
				
				
				
			}
			
			
		});
		
	});
	
	
	$('.product-item').each(function(){
		
		$(this).height(hider_height);
		$(this).css('overflow','hidden');
		
		$(this).find('.p-image').each(function(){
		
		
			var aspect = $(this).height() / $(this).width();
			var oldWidth = $(this).width();
			
			$(this).css('height',hider_height);
			$(this).css('width',hider_height*aspect);
			
			var margin = (oldWidth - $(this).width()) / 2;
			
			
			$(this).css('margin-left',margin);
			
			
		});
	});
	
}

function product_hider_refresh_viewmore(){
	
	$('.p-viewmore').each(function(){
		
			var hideid = $(this).find(':first-child').attr('rel');

			if($('#' + hideid).attr('title') == 'shown'){
				
				
				$(this).find('a').each(function(){ 
					
					$(this).css('top' , $('#' + hideid).height() - 20);
					$(this).html('&lt; Show Less'); 
				});
				
			}else{
			
				$(this).find('a').each(function(){ 
					$(this).css('top' , hider_height - 20);
					$(this).html('View More &gt;'); 
				});
			
			}
			

	});
	
	
}



function init_email_spam_catch(){
	
	$('.no-email-spam').each(function(){
		
		var rel = $(this).attr('rel');
		var parts = rel.split(',');
	
		if(parts.length == 3){
			
			var addy = parts[0] + '@' + parts[1] + '.' + parts[2];
			$(this).attr('href','mailto:'+addy);
			$(this).html(addy);
			
		}
		
	});
	
}


var listrotator2_current = -1;
var listrotator2_count = 0;
var listrotator2_items = [];
var listrotator2_fadeduration = 300;
var listrotator2_showduration = 5000;
var listrotator2_outerheight = 0;

function init_list_rotator2(){
	
	listrotator2_count = 0;
	$('.listrotator2 li').each(function(){
	
		if(listrotator2_count == 0){
			$(this).show();
		}else{
			$(this).hide();
		}
		var id = 'listrotator2_'+listrotator2_count;
		$(this).attr('id', id );
		listrotator2_items[listrotator2_count] = id;
		
		listrotator2_count ++;

	});
	
	list_rotator2_run();
	
}



function list_rotator2_run(){
	
	list_rotator2_hide_all();
	listrotator2_current = (listrotator2_current + 1) % (listrotator2_count);
	
	
	setTimeout('list_rotator2_show('+listrotator2_current+')',listrotator2_fadeduration);
	setTimeout('list_rotator2_run()',listrotator2_showduration + (listrotator2_fadeduration*2));
	
}

function list_rotator2_show(index){
	$('#'+listrotator2_items[index]).fadeIn(listrotator2_fadeduration);
	

}

function list_rotator2_hide_all(){
	
	if(listrotator2_current >= 0){
		for(var i = 0; i < listrotator2_count; i++){
			$('#'+listrotator2_items[i]).fadeOut(listrotator2_fadeduration);
		}
	}
	
}


var listrotator_current = -1;
var listrotator_count = 0;
var listrotator_items = [];
var listrotator_fadeduration = 300;
var listrotator_showduration = 5000;
var listrotator_outerheight = 0;

function init_list_rotator(){
	
	listrotator_count = 0;
	$('.listrotator li').each(function(){
	
		if(listrotator_count == 0){
			$(this).show();
		}else{
			$(this).hide();
		}
		var id = 'listrotator_'+listrotator_count;
		$(this).attr('id', id );
		listrotator_items[listrotator_count] = id;
		
		listrotator_count ++;

	});
	
	list_rotator_run();
	
}



function list_rotator_run(){
	
	list_rotator_hide_all();
	listrotator_current = (listrotator_current + 1) % (listrotator_count);
	
	
	setTimeout('list_rotator_show('+listrotator_current+')',listrotator_fadeduration);
	setTimeout('list_rotator_run()',listrotator_showduration + (listrotator_fadeduration*2));
	
}

function list_rotator_show(index){
	$('#'+listrotator_items[index]).fadeIn(listrotator_fadeduration);
	

}

function list_rotator_hide_all(){
	
	if(listrotator_current >= 0){
		for(var i = 0; i < listrotator_count; i++){
			$('#'+listrotator_items[i]).fadeOut(listrotator_fadeduration);
		}
	}
	
}

/**
	This sets the actions for when the secondary navigation items are
	hovered over / out. ie. it displays the hidden .note class.
**/
function init_second_nav(){
	

	$('#overlay div:first-child').width($('#content').outerWidth());
	$('#overlay div:first-child').height($('#content').height());
	
	$('#second-nav ul li a').each(function(){
		
		$(this).mouseover(function(){
			$(this).parent().children('a.note').each(function(){
				
				
				
				if($(this).parent().children('a:first-child').hasClass('select') == false){
					
					$(this).show();
					$('#overlay').css('display','block');
					
				}
				
			});
		});
		
		$(this).mouseout(function(){
			$(this).parent().children('a.note').each(function(){
				$(this).hide();
				$('#overlay').css('display','none');
				
			});
		});
		
	});
	
}


function checkIt(evt) {
	//checks that input only accepts number input
	evt = (evt) ? evt : window.event;
	var charCode = (evt.which) ? evt.which : evt.keyCode;
	//46 is full stop
	if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46) {
		status = "This field accepts numbers only.";
		return false;
	}
	
	status = "";
	return true;
}



/**
	Title:			ClearFields.
	Author: 		Andrew Ward
	Version: 		1.2
	Last Modified:	23/09/2011
	Web:			www.scorchsoft.com
	
	
	Copyright Scorchsoft Ltd 2011 All rights reserved.
	
	requires: 	jQuery
	
	This plugin clears the text within a textbox if it has been given one of these classes:
		.clr, .l-input, .clearField
	It reinstates old text if input isn't entered
	
	Usage: simple:
	
	$(document).ready(function () {
		$.clearFields();
	});

	
**/
(function( $ ){


	var $clearFields = $.clearFields = function(){
		
		$clearFields.initialize();
		
	};
	
	$clearFields.vars = {
		clearedBooleans: {},
		oldText: {}
	}
	
	
	/**
		This function replaces the text of an input box with its original text
		if it has been wiped. This is useful to call when focus is lost on
		an input box whilst it is empty
	**/
	$clearFields.setOldText = function(thisItem){
	
		//if the textbox is empty
		if(jQuery.trim($(thisItem).val()) == ""){
			//fill with the original text
			$(thisItem).val($clearFields.vars.oldText[$(thisItem).attr('id')]);
			$clearFields.vars.clearedBooleans[$(thisItem).attr('id')] = false;
			
		}
		
	}
	
	/**
		Clears the text fields of their original text. If new text has been added then
		it doesn't clear that text
	**/
	$clearFields.clearText = function(thisItem) {
		//only clears text once
		if($clearFields.vars.clearedBooleans[$(thisItem).attr('id')] == false){
			
			$clearFields.vars.oldText[$(thisItem).attr('id')] = $(thisItem).val();
			$(thisItem).val("");
			$clearFields.vars.clearedBooleans[$(thisItem).attr('id')] = true;
		}
			
	}
	
	$clearFields.initialize = function(){
		$clearFields.assignActions();
	}
	
	
	$clearFields.assignActions = function(){
		$('.clr, .l-input, .clearField, .frm_qty input').each(function() {
	
			$clearFields.vars.clearedBooleans[$(this).attr('id')] = false;
			$clearFields.vars.oldText[$(this).attr('id')] = $(this).val();

			// Should this function be called more than once we need to make sure that
			// any old bound actions are removed.
			$(this).unbind('focus');
			$(this).unbind('blur');
			
			$(this).focus(function() {$clearFields.clearText(this);});
			$(this).blur(function() {$clearFields.setOldText(this);});
	
		});
	}

})( jQuery );












