$(document).ready(function(){

	// man-handle the min-width attribute by setting it manually (Thanks IE! You suck!)
	var loginWidthHack;
	if($("li#login").width() < 100){
		loginWidthHack = 102;
	}else{
		loginWidthHack = $("li#login").width() + 2;
	}
	$("li#login").css({
		'width':loginWidthHack
	});
	$("li#login").hover(
	    function(){
	        $(this).children('ul').width($(this).width()).slideDown('fast');
	    },
	    function(){
	        $(this).children('ul').slideUp('fast');
	    }
	);
	
	$("#login>a").click(function(e){
		e.preventDefault();
		$('.modal').dialog('destroy');		
		$("#modal-login").dialog({
			height: 450,
			width: 770,
			modal: true,
			draggable: false,
			resizable: false,
			dialogClass: 'modal'
		});
	});
	$("a#signup").click(function(e){
		e.preventDefault();
		$('.modal').dialog('destroy');
		$("#modal-signup").dialog({
			height: 450,
			width: 770,
			modal: true,
			draggable: false,
			resizable: false,
			dialogClass: 'modal'
		});
	});
	$("#forgotUserOrPassLink").click(function(e){
		e.preventDefault();
		$('.modal').dialog('destroy');
		$("#modal-forgotusername").dialog({
			height: 450,
			width: 770,
			modal: true,
			draggable: false,
			resizable: false,
			dialogClass: 'modal'
		});
	});
	$("a#change-password").click(function(e){
		e.preventDefault();
		$('.modal').dialog('destroy');
		$("#modal-password-change").dialog({
			height: 450,
			width: 770,
			modal: true,
			draggable: false,
			resizable: false,
			dialogClass: 'modal'
		});
	});
    $("ul.sf-menu").superfish({ 
        delay:       0,                            // one second delay on mouseout 
        speed:       'fast',                          // faster animation speed 
        autoArrows:  false,                           // disable generation of arrow mark-up 
        dropShadows: false,                            // disable drop shadows 
        pathClass:  'current' 
    });	





    
    
    
    
    
    
    
	
      $("#logout a").click(function(e){
          e.preventDefault();
          document.logoutForm.submit();	   
      });
      
      $("#loginBtn").click(function(e){
          e.preventDefault();
          document.loginForm.submit();   	   
      });
    
    $("select").uniform();
    
	var bodyClass = $('body').attr('class').split(' '); // array
	$(bodyClass).each(function(index){
		switch(this.toString()){
			case 'form':
				$('body').addClass('detectedForm');
				break;
			case 'reports':
				$('body').addClass('detectedReports');
					$("#date-start").datepicker({
						showOn: 'both',
						showAnim: 'slideDown',
						buttonImage: 'themes/images/calendar.gif',
						buttonImageOnly: true,
						minDate: "-13M",
						maxDate: -1
					});
					$("#date-end").datepicker({
						showOn: 'both',
						showAnim: 'slideDown',
						buttonImage: 'themes/images/calendar.gif',
						buttonImageOnly: true,
						minDate: "-13M",
						maxDate: -1
					});
				break;
			case 'search':
				$('.tooltip').hide();
				
				
			    var map;
			    var geocoder;

			      function initialize() {
			        geocoder = new google.maps.Geocoder();
			        var latlng = new google.maps.LatLng(-34.397, 150.644);
			        var myOptions = {
			          zoom: 18,
			          center: latlng,
			          mapTypeId: google.maps.MapTypeId.ROADMAP
			        }
			        map = new google.maps.Map(document.getElementById("map"), myOptions);
			        
			        codeAddress(address);
			      }



			    var address = 'Austin, TX';


			      function codeAddress(address) {
			        //var address = document.getElementById("address").value;
			        
			        
			        geocoder.geocode( { 'address': address}, function(results, status) {
			          if (status == google.maps.GeocoderStatus.OK) {
			            map.setCenter(results[0].geometry.location);
			            var marker = new google.maps.Marker({
			                map: map, 
			                position: results[0].geometry.location
			            });
			          } else {
			            alert("Geocode was not successful for the following reason: " + status);
			          }
			        });
			      }

			      function initMapTip(div) {
			        geocoder = new google.maps.Geocoder();
			        var latlng = new google.maps.LatLng(-34.397, 150.644);
			        var myOptions = {
			          zoom: 22,
			          center: latlng,
			          mapTypeId: google.maps.MapTypeId.ROADMAP
			        }
			        map = new google.maps.Map(document.getElementById(div), myOptions);
			        
			        codeAddress(address);
			      }

				
				$("#findPartsBtn").click(function(e){
					e.preventDefault();
				    document.partSearchForm.submit(); 
				}); 				
				$('a.contact').click(function(e){
					    e.preventDefault();
					    var that = $(this);
				        $(this).addClass('active').next('.tooltip').fadeIn('fast').children($(this + ' dt a')).click(function(e){ 
				        	e.preventDefault(); 
				        	that.removeClass('active'); 
				        	$(this).parent().hide();
				        	});
				 });
				
				$('a.supersession').click(function(e){
				    e.preventDefault();
				    var that = $(this);
			        $(this).addClass('active').next('.tooltip').fadeIn('fast').children('a.close').click(function(e){
				       	e.preventDefault();
				       	that.removeClass('active'); 
				       	$(this).parent().hide();
				       });
			        // Add 1 to supersession index and get the part row
					var superIdx = $(this).attr("id").replace('supersession','');
					var myRowIdx = parseInt(superIdx) + 1;
					var myRowId = '#partRow' + myRowIdx;
					var myRow = jQuery(myRowId);
					var rowPosition = myRow.position();
					var tooltipVar = that.next('.tooltip-sm');
					tooltipVar.css({
						left : rowPosition.left + 5,
						top : rowPosition.top - 100 - myRowIdx*myRowIdx
					});

				});
				
				$('a.view-map').click(function(e){
				    e.preventDefault();
				    var that = $(this);
				       $(this).addClass('active').next('.tooltip').fadeIn('fast');				
						    address = $(this).attr("href");
						    var div = document.getElementById(this.id.substr(4));
						    geocoder = new google.maps.Geocoder();
						    var latlng = new google.maps.LatLng(-34.397, 150.644);
						    var myOptions = {
						      zoom: 15,
						      center: latlng,
						      mapTypeId: google.maps.MapTypeId.ROADMAP,
					           disableDefaultUI: true,
					           draggable: false
						    }
						    map = new google.maps.Map(div, myOptions);				    
						    codeAddress(address);
				
				      $(this).next('.tooltip').children('a.close').click(function(e){
				       	e.preventDefault();
				       	that.removeClass('active'); 
				       	$(this).parent().hide();
				       });
				   });		
				// initialize google map
			  	initialize();
				break;
			case 'favorites':
				$('.tooltip').hide();
				$('a.contact').click(function(e){
					    e.preventDefault();
					    var that = $(this);
				        $(this).addClass('active').next('.tooltip').fadeIn('fast').children($(this + ' dt a')).click(function(e){ e.preventDefault(); that.removeClass('active'); $(this).parent().hide();});
				 });
				break;
			case 'inventory':
					//$('.found').hide();
					$("#date-start").datepicker({
						showOn: 'both',
						showAnim: 'slideDown',
						buttonImage: 'themes/images/calendar.gif',
						buttonImageOnly: true
					});
					$("#date-end").datepicker({
						showOn: 'both',
						showAnim: 'slideDown',
						buttonImage: 'themes/images/calendar.gif',
						buttonImageOnly: true
					});
				break;
			default:				
				break;
		}
	});
});  
