$(document).ready(function() { 
   $('.history_back').each(function(){
        var aBackLink = this;
        $(aBackLink).bind('click', function(){
             history.back();
             
             //else
             window.location = aBackLink.href;
             
             return false;
        });         
   });
   
   var scrollTo = function(){
       
		var full_url = window.location.href;
		var parts = full_url.split("#");
		var trgt = parts[1];
       
        
		if(trgt){
		    var target_offset = $("#"+trgt).offset();
		    var target_top = target_offset.top;
		
		    $('html, body').animate({scrollTop:target_top}, 500);
		}

   }();
   
   $(".scroll").click(function(event){
		//prevent the default action for the click event
		event.preventDefault();

		//get the full url - like mysitecom/index.htm#home
		var full_url = this.href;

		//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
		var parts = full_url.split("#");
		var trgt = parts[1];

		//get the top offset of the target anchor
		var target_offset = $("#"+trgt).offset();
		var target_top = target_offset.top;
		
		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 500);
	});
  
  
   
   $('#contact_form').bind('submit', function(){
            var error=new Array();
            
            var full_name=$('#contact_form input[name="name"]').val();         
            var email_address=$('#contact_form input[name="email"]').val();
            var subject=$('#contact_form select[name="subject"]').val();
            var msg=$('#contact_form textarea[name="message"]').val();            
           
            
            //check for errors
            if(full_name==''){
                error.push(" - Please enter your name");
            }     
            
                  
           
            if(email_address==''){
                error.push(" - Please enter your email address");
            } 
            
            if(subject==''){
                error.push(" - Please enter the subject");
            }  
            
            if(msg==''){
                error.push(" - Please enter your message");
            } 
           
                           
            if(error.length>0){             
                $('#error_msg').css("display","block");                
                $('#error_msg').html('The following error(s) occured:<br />'+error.join("<br />"));                
                //$('html,body').animate({scrollTop: 150}, 1000);
                return false;
            }
          
            return true;
       }); 
       
       
       $('.slideshow').each(function(){
		
			var slideShowRef=this;
			var currentImg = $(slideShowRef).find('img.current-img').filter(':first');
			var loadingImg = $(slideShowRef).find('img.loading-img').filter(':first');
			
			var linksRef=$(slideShowRef).find('ul li a').filter(
				//don't add the prev, next links
				function(index) {
  					return $(this).attr('class').indexOf('prev_next') == -1;
				});
					
			//console.log(linksRef);		
					
			//slide navigation
			$(slideShowRef).find('a').click(function () {
				var aRef=this;//number or prev/next
				var aNumRef=aRef;
				
				//show loading gif
				loadingImg.css({display:'block'});
				currentImg.css({display:'none'});
				
				//browse through images only if link clicked is not current image
				if (aRef.className.indexOf('current') != -1){
					return false;
				}
					
				//next/prev was clicked, get aNumRef
				if(aRef.hash){	
									
					linksRef.each(function(index){		
						var nextIndex=(parseInt(index)+1>parseInt(linksRef.length)-1)?0:parseInt(index)+1;//next or first
						var prevIndex=(parseInt(index)-1<0)?parseInt(linksRef.length)-1:parseInt(index)-1;//prev or last
						var selectedIndex=(aRef.hash=='#next')?nextIndex:prevIndex;
													
						//set aNumRef current data						
						if($(linksRef[index]).attr('class').indexOf('current')!=-1){
							aNumRef=linksRef[selectedIndex];//is a number link
						}
					});
					
					
				}
				
				//set current img data					
				currentImg.attr('alt', aNumRef.title);
				currentImg.attr('src', aNumRef.href).load(function() {  
					//hide loading gif
					loadingImg.css({display:'none'});
					currentImg.fadeIn(500);	

					//mark current image in nav			
					$(slideShowRef).find('a').removeClass('current');
					$(aNumRef).addClass('current');	


					//add img description
					$(slideShowRef).find('dt').filter(':first').html(aNumRef.title);
				});
					
				
				return false;
			});
		});
       
       
       
});



