$(document).ready(function(){
	
	var isMSIE = $.browser.msie;

	$('.tooltip_show').each(function(val){
		var a = $(this); 
		var img = $('img.imgpcturl:eq('+val+')');

		if (!isMSIE) {
			jQuery.ajax({
			    type: "GET",
			    async: false,
			    url: '/photos/' + a.attr('pictureurl'),
			    success: function(results){
					if (a.attr('pictureurl') != ""){
						a.attr('existPicture', '1');
					} else {
						a.attr('existPicture', '0');
					}
					
		    	},
			    error: function(XMLHttpRequest, textStatus, errorThrown){
		    		a.attr('existPicture', '0');
			    }
			});
		} else {
			jQuery.ajax({
			    type: "GET",
			    async: false,
			    url: '/photos/' + a.attr('pictureurl'),
			    success: function(results){
					if (a.attr('pictureurl') != ""){
						a.attr('existPicture', '1');
					} else {
						a.attr('existPicture', '0');
					}
					
		    	},
			    error: function(XMLHttpRequest, textStatus, errorThrown){
		    		if (XMLHttpRequest.status == 200){
		    			a.attr('existPicture', '1');
		    		} else {
		    			a.attr('existPicture', '0');
		    		}
		    		
			    }
			});
		}
		
		var fit_mode; 
		
		if (a.attr('existPicture') != 0){
			var imag = new Image();
			var imgUrl = '/photos/'+a.attr('pictureurl');
			imag.src = imgUrl;
			var newWidth;
			var newHeight;
			var diff;
			
			if (imag.width > imag.height) {
				/*
				newWidth = 245;
				diff = imag.width / 245;
				newHeight = ParseInt(imag.height / diff);
				fit_mode = 'width="'+newWidth+'" height="'+newHeight+'"';
				*/
				fit_mode = 'width="245"';
			}
			else {
				/*
				newHeight = 245;
				diff = imag.height / 245;
				newWidth = parseInt(imag.width / diff);
				fit_mode = 'width="'+newWidth+'" height="'+newHeight+'"';
				*/
				fit_mode = 'height="245"';
			}
		}
		else {
			fit_mode = 'height="245" width="245"';
		}
		//alert('/photos/'+a.attr('pictureurl')+' '+imag.width +' '+ imag.height+' '+fit_mode);
		a.tooltip({
			bodyHandler: function() {
			return "<table><tr><td id=\"valign\"><img \src=\"" + '/photos/' + (a.attr('existPicture') == '0' ? 'nofoto.jpg' : a.attr('pictureurl')) + "\" "+fit_mode+" /></td></tr></table>";
			}, 
			showURL: false,
			top: 40,
			left: 40
		});
		
		//alert("<div class=\"t_photo\"><img class=\"timg\" \src=\"" + '/photos/' + (a.attr('existPicture') == '0' ? 'nofoto.jpg' : a.attr('pictureurl')) + "\" "+fit_mode+" /></div>");
		//alert($('img.timg:eq('+val+')').attr('width') +' '+ $('img.timg:eq('+val+')').attr('height'));
		/*
		if ($('img.timg:eq('+val+')').attr('width') > $('img.timg:eq('+val+')').attr('height')) {
			$(this).attr('width', '100%');
		}
		else {
			$(this).attr('height', '100%');
		}
		*/
		if (a.attr('existPicture') == 0){
			img.attr('src', '/images/nophoto.gif');
			img.css('cursor', 'default');
		}
		else {
			img.tooltip({
				bodyHandler: function() {
				return "<table><tr><td id=\"valign\"><img \src=\"" + '/photos/' + (a.attr('existPicture') == '0' ? 'nofoto.jpg' : a.attr('pictureurl')) + "\" "+fit_mode+" /></td></tr></table>";
				}, 
				showURL: false,
				top: 40,
				left: 40
			});
			img.click(function(event){
			openPhotoWin(a.attr('pictureurl'));
			event.preventDefault();
			});
		};
		
	});
	
	$('table.catalog-list tr').each(function(index){
		var val = $(this);
		var color = val.css('background-color');
		val.mouseover(function(){
			
			var urlString = window.location.pathname;
			if (urlString == '/tyresbp' ||
					urlString == '/disksbp'){
				val.css({'background-color': '#CCCCCC'});
			}
		});
		val.mouseleave(function(){
			var urlString = window.location.pathname;
			if (urlString == '/tyresbp' ||
					urlString == '/disksbp'){
				val.css({'background-color': color});
			}
		});
	});
	


	/*
	$('.tooltip_show').each(function(val){
		var a = $(this);
		a.tooltip({
			bodyHandler: function() {
				return '<img src="/photos/loading.gif" />';
			}, 
			showURL: false,
			top: 40,
			left: 40
			
		});		
		var img = $('img.imgpcturl:eq('+val+')');
		var ext = a.attr('src');
		if (a.attr('existPicture') == 0 && ext != '/photos/loading.gif'){
			img.attr('src', '/images/nophoto.gif');
			img.css('cursor', 'default');
		}
		else {
			img.tooltip({
				bodyHandler: function() {
					return " <img src=\"" + '/photos/' + (a.attr('existPicture') == '0' ? 'nofoto.jpg' : a.attr('pictureurl')) + "\" width=\"100%\" height=\"100%\"/>";
				}, 
				showURL: false,
				top: 40,
				left: 40
			});
			img.click(function(event){
			openPhotoWin(a.attr('pictureurl'));
			event.preventDefault();
			});
		};		
	});
	*/
	
});
