var newItemDuration = 1000; //время для прокрутки фотографии
var photoDivWidth = 191;
var minResolution = 1150;
var defaultNewItemsWidth = 764;
var currPhoto = 0;
var photoCount = 0;

jQuery(document).ready(function() {
	photoCount = jQuery('#new_items td').size() - Math.ceil(jQuery('#products td.center').width() / photoDivWidth);
	//image effect
	jQuery('#products a.prev:hover img, #products a.next:hover img').css('visibility', 'visible');
	jQuery('#products .prev, #products .next').hover(
		function(){
			className = jQuery(this).attr('class');
			if((currPhoto != 0 || className != 'prev') && (currPhoto != photoCount || className != 'next'))
				jQuery(this).children('img').stop().fadeTo(200, 0.01);
		},
		function(){jQuery(this).children('img').stop().fadeTo(200, 0.99);}
	);
	
	//presets new items count
	function resizeNewItems()
	{
		prevWidth = jQuery('#products td.center').width();
		if(jQuery('body').width() > minResolution)
		{
			k = Math.ceil((jQuery('body').width() - minResolution) / photoDivWidth);
			newWidth = Math.min(defaultNewItemsWidth + k * photoDivWidth, jQuery('#new_items td').size() * photoDivWidth);
			jQuery('#products td.center').css('width', newWidth);
		}
		else 
			jQuery('#products td.center').css('width', defaultNewItemsWidth);
		photoCount = jQuery('#new_items td').size() - Math.ceil(jQuery('#products td.center').width() / photoDivWidth);
	}	
	resizeNewItems();
	//scroll new items
	var tableWidth = jQuery('#new_items table').width();
	jQuery(window).resize(function() { resizeNewItems(); });

	
	jQuery('a.prev').click(
		function()
		{
			if(currPhoto > 0)
			{
				--currPhoto;
				jQuery('#new_items table').stop().animate({
					'left': (-photoDivWidth * currPhoto) + 'px'
				}, newItemDuration);
			}
		}
	);
	jQuery('a.next').click(
		function()
		{
			divWidth = jQuery('#new_items').width();
			if(tableWidth - currPhoto * photoDivWidth > divWidth)
			{
				++currPhoto;
				jQuery('#new_items table').stop().animate({
					'left': (-photoDivWidth * currPhoto) + 'px'
				}, newItemDuration);
			}
		}
	);
	//types
	jQuery('#types a').hover(
		function(){ jQuery(this).stop().animate({paddingBottom: '5px', paddingTop: '150px'}, 50); },
		function(){ jQuery(this).stop().animate({paddingBottom: '0px', paddingTop: '155px'}, 50); }
	);
	//menu
	jQuery('#menu a').hover(
		function(){ jQuery(this).parent('td').stop().animate({paddingTop: '12px'}, 50); },
		function(){ jQuery(this).parent('td').stop().animate({paddingTop: '16px'}, 50); }
	);
	//catalog
	jQuery('#catalog a').hover(
		function(){ jQuery(this).parent('td').stop().animate({paddingBottom: '5px', paddingTop: '0px'}, 50); },
		function(){ jQuery(this).parent('td').stop().animate({paddingBottom: '0px', paddingTop: '5px'}, 50); }
	);
	//inner catalog
	jQuery('.cat a').hover(
		function(){ jQuery(this).stop().animate({paddingBottom: '5px', paddingTop: '0px'}, 50); },
		function(){ jQuery(this).stop().animate({paddingBottom: '0px', paddingTop: '5px'}, 50); }
	);
	//inner catalog menu
	jQuery('#right_cont a').hover(
		function(){ jQuery(this).stop().animate({paddingLeft: '8px'}, 50); },
		function(){ jQuery(this).stop().animate({paddingLeft: '0px'}, 100); }
	);
});

function OpenPhoto(img, w, h, title){
  if (w && h) {
    if (w > screen.availWidth || h > screen.availHeight) {
      if (w / screen.availWidth > h / screen.availHeight) {
        h *= screen.availWidth / w;
        w = screen.availWidth;
      }
      else {
        w *= screen.availHeight / h;
        h = screen.availHeight;
      }
    }
    if (!title) title = "Просмотр фотографии";
    var win = window.open("", "_blank", "width="+w+", height="+h);
    win.document.write("<html><head>\n");
    win.document.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n");
    win.document.write("<title>"+title+"</title></head>\n");
    win.document.write("<body style=\"padding: 0; margin: 0; cursor: hand;\">\n");
    win.document.write("<img src=\""+img+"\" width=\""+w+"\" height=\""+h+"\" alt=\""+title+"\" title=\""+title+"\" onclick=\"window.close()\">\n");
    win.document.write("</body></html>");
    win.focus();
  }
}
