(function($){
	//mycustom function
	$.fn.slideLoop= function(options){
		//default values which can be change by the options parameter
		var defaults = {		
			pauseDelay: 1000	
		};

		var options = $.extend(defaults, options);
		var container=this;
		var control=$(container.selector+'_control');
		$('div:eq(0)',control).addClass('bannerImageNumberOn');
		var imgCount=$('img',container).length;
		var imgPos=0;
		var loopInterval=((imgCount*2000));
		
		repeat=setInterval(loop, loopInterval);
		
		//overlay static image on mouseover
		$('div',control).click(function(index){
			var num=$('div',control).index(this);
			$('div',control).removeClass('bannerImageNumberOn');
			$(this).addClass('bannerImageNumberOn');
			$('img:nth-child('+num+')',container).show();
			$('img',container).each(function(index){
				if (index==num){
					$(this).css('z-index','1');
					$(this).show();
				}else{
					$(this).css('z-index','0');
					$(this).hide();
				}
			})

			$('.breadcrumbstextdiv > div').each(function(index){
				if(index==num){
					$(this).show();
				}else{
					$(this).hide();
				}
				
			})

			$(container).queue( [ ] ).stop();

			clearInterval(repeat);
			repeat=setInterval(function(){loop(imgPos);}, 8000);
		})				
		
		// main animation loop
		function loop(num){
				
			$('img:nth-child(1n)',container).each(function(index) {
				imgPos=index;
				var $img=$(this);
				
				container.queue(function(){		
					$img.css('z-index','1');
					
					if (index==(imgCount-1)){
						nextNodeIndex=1;
					}else{
						nextNodeIndex=index+2;
					}
					$('img:nth-child('+nextNodeIndex+')',container).css('z-index','0');
					$('img:nth-child('+nextNodeIndex+')',container).show();
					
					$('div',control).removeClass('bannerImageNumberOn');
					$('.breadcrumbs > div').hide();
					$('div:eq('+(nextNodeIndex-1)+')',control).addClass('bannerImageNumberOn');
					$('.breadcrumbs > div:eq('+(nextNodeIndex-1)+')').show();
					$img.fadeOut(1000,function(){
						
						container.dequeue();
					})
				})
				
				//pause between animation
				container.queue(function(){
					setTimeout(function(){

						container.dequeue();
						//$('img',container).css('z-index','0');
						//$img.show();
						
					},4000)
				})
			})
		};
	}	
})(jQuery);

(function($){
	//mycustom function
	$.fn.imageViewer= function(ilist){
		//default values which can be change by the options parameter
		var defaults = {		
			pauseDelay: 1000	
		};

		var options = $.extend(defaults, options);
		var container=this;
		
		//image viewer controls
		var control=$(container.selector+'Control');
		var moveLeft=$(control.selector+'Left');
		var moveRight=$(control.selector+'Right');
		var main=$(container.selector+'Main');
		
		
		var visibleWidth=$(control.selector).width();
		var totalWidth=$(control.selector+' ul').width();
		var imageWidth=$('#imgViewSlide li:eq(0)').outerWidth(true);
		
		var visibleCount=Math.floor(visibleWidth/imageWidth);
		var totalCount=$('#imgViewSlide li').length;
		var pos=0;
		
		var imagesMedium=Array();
		var imagesLarge=Array();
		
		images=ilist;
		currentImage='';
		
		container.data('images',ilist);
		
		
		moveLeft.click(function(){
			if (pos!=0){
				$('#imgViewSlide').animate({left:'+='+imageWidth},1000);
				pos-=1;
			}
		})
		
		moveRight.click(function(){
			if(pos<(totalCount-visibleCount)){
				$('#imgViewSlide').animate({left:'-='+imageWidth},1000);
				pos+=1;
			}
		})
		
		$('img',control).click(function(){
			var num=$('img',control).index(this);
			img=$(this).attr('src');
			mediumimage=container.data('images')[img][0];
			largeimage=container.data('images')[img][1];
			$('.image',main).attr('src',container.data('images')[img][0]);
		})

		main.click(function(){
			$('#imageDialog').myDialog()
			$('#imageDialog').myDialog.open(container.data('images'),currentImage) //show();
		})
		
		$.fn.imageViewer.imageList = function(ilist){
			container.data('images',ilist);
		};			
	}	
})(jQuery);

(function($){
	//mycustom function
	$.fn.myDialog= function(options){
		//default values which can be change by the options parameter
		var defaults = {};

		var options = $.extend(defaults, options);
		var container=this;
		var close=$('.close',container);
		currentimage='';
		
		close.click(function(){
			// hide the grey overlay
			$('.imageDialogOverlay').fadeOut(1000);
			container.fadeOut(1000);
		})
		
		$.fn.myDialog.open = function open(imagelist,currentimage){
			container.data('images',imagelist);
			$('.image',container.selector).attr('src',container.data('images')[img][1]);
			// show the grey overlay
			$('.imageDialogOverlay').fadeIn(1000);
			$(container).fadeIn(1000);
			var left=($(window).width()/2)-($('#imageDialog').outerWidth()/2);
			var top=($(window).height()/2)-($('#imageDialog').outerHeight()/2);
			$('#imageDialog').css('left',left);
			$('#imageDialog').css('top',top);
			
		}
		
		$('ul li img',container).click(function(){
			var num=$('ul img',container).index(this);
			$('.image',container.selector).attr('src',container.data('images')[$(this).attr('src')][1]);
		})
	}	
})(jQuery);

(function($){
	//mycustom function
	$.fn.netfoxSearch= function(options){
		//default values which can be change by the options parameter
		var defaults = {		
			type: 'simpleSearch'	
		};
		
		var options = $.extend(defaults, options);
		$(this).submit(function(){
			document.location='/products/ search::'+$('input:text',this).val();
			return false;
		})
	}	
})(jQuery);

$(document).ready(function(){	
	$('#search').netfoxSearch();	
	$('#map_canvas').each(function(){	
		initialize();
	})
	$('#imageslide').slideLoop();
	
	$('.contactbar div').toggle(function(){
		$('.contactbar div span').html('collapse');
		$('.contactinfo').slideDown();
		$('.contactbar img').attr('src','/custom/images/whitearrowup.png');
	},function(){	
		$('.contactbar div span').html('expand');
		$('.contactinfo').slideUp();
		$('.contactbar img').attr('src','/custom/images/whitearrowdown.png');
	})
	
	$('.bluezebra tbody tr:even').addClass('bluezebra');
	
	$('.contactTable tbody tr:even').addClass('bluezebra');
	
	$('.tableselect tbody tr').click(function(){
		$('.tableselect tbody tr').removeClass('blueselected');
		
		$('.tableselect tbody tr').children().each(function(){
			$(this).removeClass('yellowline');
		});
		
		var index=$('.tableselect tbody tr').index(this);
		$('#maps img').each(function(i){
			if(i==index){
				$(this).show();
			}else{
				$(this).hide();
			}
		})
		$(this).addClass('blueselected');
		$(this).children().each(function(){
			if($(this).hasClass('blueline')) {
				$(this).addClass('yellowline');
			}
		});
	})
	
	$('.collapse').click(function(){
		if ($(this).data('hidden')==false ){
			$(this).data('hidden',true);
			$(this).parent().animate({bottom:'0'},1000);
			$(this).html('Collapse');
		}else{
			$(this).data('hidden',false);
			$(this).parent().animate({bottom:'-=86'},1000);
			$(this).html('Expand');
		}
	})
	
	//default state
	$('.submenuitems > div').toggle(function(){
		var parent=$(this).parent();
		$(parent).addClass('menuon');
		$(parent).removeClass('menuoff');
		$('img:eq(0)',this).attr('src','/custom/images/menuarrowblackdown.png');
		$('ul',parent).show();
	},function(){
		var parent=$(this).parent();
		$(parent).removeClass('menuon');
		$(parent).addClass('menuoff');
		$('img:eq(0)',this).attr('src','/custom/images/menuarrowwhite.png');
		$('ul',parent).hide();
	});

});

function initialize() {
	var latlng = new google.maps.LatLng(51.415,0.276);
	var markerlatlng = new google.maps.LatLng(51.415,0.276);
	var myOptions = {
		zoom: 12,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
		};
	var map = new google.maps.Map( document.getElementById("map_canvas"), myOptions );
	var infowindow = new google.maps.InfoWindow({
		content: "<div style=\"color:#000;\"><strong>Hawkspare Limited</strong><br />Green Street Green<br />Dartford<br />Kent<br />DA2 8DP</div>"
	});
	var marker = new google.maps.Marker({
		position: markerlatlng, 
		map: map,
		title:"Hawkspare Directions"
	});  
	infowindow.open(map,marker);
}

/* forgotten password email validation */
function checkForm() {
	var validate = new validateForm();
	validate.checkText( 'contact_name', 'Contact Name' );	
	validate.validateEmailAddress( 'contact_email', 'Email Address' );
	validate.checkText( 'contact_enquiry', 'Enquiry' );
	if( validate.numberOfErrors() > 0 ) {
		validate.displayErrors();
		return false;
	}
	return true;
}

function inputBoxFocus( input, defaultText, passwordField ) {
	if( typeof( passwordField ) != 'undefined' ) {
		document.getElementById( passwordField ).style.display = '';
		document.getElementById( passwordField+'_text' ).style.display = 'none';
		document.getElementById( passwordField ).focus();
	} else {
		if( input.value == defaultText ) {
				input.value = '';
		} else {
		}
	}
}

function inputBoxBlur( input, defaultText, passwordField ) {
	if( input.value == '' ) {
		if( typeof( passwordField ) != 'undefined' ) {
			document.getElementById( passwordField ).style.display = 'none';
			document.getElementById( passwordField+'_text' ).style.display = '';
		} else {
			input.value = defaultText;
		}
	}
}

function showLanguage(languageBox) {
	var langContainer = $('#langDialog');
	var close=$('.close',langContainer);

	close.click(function(){
		langContainer.hide();
		// hide the grey overlay
		$('.imageDialogOverlay').hide();
	})
	
	var selectedLangIndex = languageBox.value;
	
	if(selectedLangIndex != '0') {
		$('.langText').hide();
		$('#langText'+selectedLangIndex).show();
		
		// show the grey overlay
		$('.imageDialogOverlay').show();
		
		langContainer.show();
		var left=($(window).width()/2)-(langContainer.outerWidth()/2);
		var top=($(window).height()/2)-(langContainer.outerHeight()/2);
		langContainer.css('left',left);
		langContainer.css('top',top);
	}
}
