var currentId = 0;
var maxProducts;
var hover_button = false;
var mouse_enter = false;
var menu_open_name = '';


$(document).ready( function(){
    
	//overide the non javascript defaults
	$('#main').css('overflow','');
	$('#main').css('height','100%');
	$('#footer').css('display','block');
	
	var docHeight =  $.getDocHeight();
	var mainDivHeight = $('#main').height();
	
	
		if((copyDivHeight =  $('#copy_content').height()) == null){
			copyDivHeight =  $('#copy_content_incont').height()
		}
	
	//allows for menu expansion when javascript turned off
	if($.browser.msie && $.browser.version=="6.0"){
		
		$('#footer').css('top','-60px');
		
	if(copyDivHeight >= 670){
				
				$('#container').css('height', (copyDivHeight + 100) +'px');
				$('#sidenav').css('height', (copyDivHeight + 50) +'px');
				
			}
			else{
				
				$('#container').addClass('forceHeight');
				$('#sidenav').css('height',  '511px');
				
			}
		
	}
	else{
		
			if(copyDivHeight >= 670){
				
				$('#container').css('height', (copyDivHeight + 200) +'px');
				$('#sidenav').css('height', (copyDivHeight + 50) +'px');
				
			}
			else{
				
				$('#container').css('height', '670px');
				$('#sidenav').css('height',  '511px');
				
			}
		
	
	
	}
	

    //init stuff here        
   // $(document).pngFix();
   
    $('.sublinks').hide();
    
	$('#navlist li').each(function () {
			
			if ( $(this).children().size() > 1 ) {
			
				$(this).hover(function (event) {
							
							
							if(!hover_button){
							  //$('.sublinks').hide();
								
								hover_button = true;
								 
								 if ( $(this).children().size() > 1 ) {
								 var ul_temp_id = $(this).find('ul').attr('id');
								 menu_open_name = ul_temp_id; //track the name of the open menu
									
									$('#' + ul_temp_id).slideDown('fast', function() {
									//return false;
									
									
								  });
									
								 }
									
								event.stopPropagation();
			
								
								}
								
								return false;
								
							}, 
						   function () {
							
							if(hover_button == true && menu_open_name == $(this).find('ul').attr('id')){
								
								hover_button = false;
							
							if ( $(this).children().size() > 1 ) {
								 var ul_temp_id = $(this).find('ul').attr('id');
								 //$('#' + ul_temp_id).hide();
								   $('#' + ul_temp_id).slideUp('fast', function() {
									//return false;
									
									
								  });
								 }
								 
								event.stopPropagation();
								 
								//return false;
							}
							return false;
					   });
			}	
	});
	
	$('#navlist li').bind("mouseenter",function(event){
      
		
		
		if(!mouse_enter){
			mouse_enter = true;
			//alert('here');
			
		}
	  return false;
    });
	
	$('#navlist li ul').bind("mouseleave",function(event){
      
	  if(mouse_enter){
			mouse_enter = false;
			hover_button = false;
			menu_open_name = '';
			
			$('#navlist li ul').slideUp('fast', function() {
									//return false;
									
									
								  });
			
		}
		return false;
    });

	

    
    //Contact form code//
    
    $('#contact_link img').hover(function(){
		
		$(this).attr('src', 'images/Contact_arrow_hover.gif');
		
		}, function(){
			
			$(this).attr('src', 'images/Contact_arrow_normal.gif');
			
			});
	


	
	
    $('#uro_btn').click( function(){//submit button
        //alert('Logged In');
        var ftp_bool = false;
		var required_fields  = [];
		
		if($(this).parent().attr('name') == 'order'){
			
			required_fields = ['name', 'email', 'country'];
			//alert(required_fields);
		}
		else if($(this).parent().attr('name') == 'contact'){
			
			required_fields = ['name', 'email', 'query'];
			//alert(required_fields);
		}
		
        if(validate_form($('#form1 :input'), required_fields)){
          
          
          ftp_bool = true;
          
        }
        else{
         ftp_bool = false;
        
		

         
          
        } 
       
       //cancel the submit button default behaviours  
       return ftp_bool; 
    });
    
      $('#uro_res').click( function(){//reset button
        
        $(':text, textarea').each(function(){
         
         $(this).val('');
         $(this).removeClass('val_error');
         
         
         });
        
       $(':checkbox').removeAttr('checked');

	   
       //cancel the submit button default behaviours  
       return false; 
    });
    
	//helper for clearing error msg from form input
	
	$('input, textarea').focus(function(){
			if($(this).hasClass('val_error')){
			$(this).val('');
			$(this).removeClass('val_error');
			}
		});
	
    
	//cycle trhough images on front page
	
	if($('#img_changer').length){
        maxProducts = $('#img_changer img').length - 1;
        setInterval("productNext(true)", 6000);
    }
  
    //for scrolling in the faq's
	
	$(window).bind('scroll', function(){return false;}); 
	
	if(($.browser.msie && $.browser.version=="6.0") || ($.browser.msie && $.browser.version=="7.0")){
	
		$('.nav_scroll').click(function(event){
			
			
			
			$('#main').scrollTo($(this).attr('href') , 200);
			
			
			event.stopPropagation();
		});
		
	}else{
		
		$('.nav_scroll').localScroll({
			target:'#main'
		});	
		
	}

	//pretty photo for loading media
	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	

	
            
});

var changingProduct = false;
function productNext(forward){
    //alert(currentId);
    
    if(!changingProduct){
        changingProduct = true;
        if($.browser.msie){
            
            //if(!$('#p_' + currentId).hasClass('hide')){
            //   $('#p_' + currentId).addClass('hide');   
            //}
            
            $('#p_' + currentId).hide();
			$('#p_' + currentId).hide();
            idControl(forward);
            $('#p_' + currentId).show();
            changingProduct = false;
        }else{
            $('#p_' + currentId).fadeOut('600', function(){
                idControl(forward);
                $('#p_' + currentId).fadeIn('600', function() { changingProduct = false; });    
            });
        }
    }
}

function idControl(dir){
    //Keeps it so we don't loop outside the array
    (dir) ? currentId++ : currentId--;
            
    currentId = (currentId  < 0) ? maxProducts : currentId;
    currentId = (currentId  > maxProducts) ? 0 : currentId;        
}



function validate_form(form_data, req_flds){
   
   var val_data_bool = true;
   var multi_count = 0;
   
   form_data.each(function(){   //see if there are multiple emtpy fields
      if($(this).val() == ''){
               
                  multi_count++;
                                
               }
      
      });
   
   form_data.each(function(){
      
            
				if(in_array($(this).attr('name'), req_flds)){
				//if($(this).attr('name') == 'name' || $(this).attr('name') == 'email' || $(this).attr('name') == 'query'){
				
				
				
				   if($(this).val() == ''){
				   
						 
						 
						 $(this).addClass('val_error');
						 $(this).val('Please fill in the '+ $(this).attr('name') +' field');
						 
						 val_data_bool = false;         
				   }
				   else{
					  
					  $(this).removeClass('val_error');
				   }
				
				
				
				}
			
      
      });
   
   
  return val_data_bool;
}

function in_array( term, arr ){
	var a=false;
	for(var i=0;i<arr.length;i++){
	  if(term == arr[i]){
	    a=true;
        break;
	  }
	}
	return a;
}

$.getDocHeight = function(){
     var D = document;
     return Math.max(Math.max(D.body.scrollHeight,    D.documentElement.scrollHeight), Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), Math.max(D.body.clientHeight, D.documentElement.clientHeight));
};




