function obj_gallery ( widht, height, link_width, interval )
{
	this.widht = widht;
	this.height = height;
	this.link_width = link_width;
	this.img_id = "image_gallery";
	this.divmain_id = "gdivmain";
	this.obj_name = "gallery";
	this.interval = interval;
	this.number = 1;
	this.backop = false;
	this.lust_number = 0;
	this.images = new Array();
	this.url_images = new Array();
	this.links = new Array();
	this.alttext = new Array();

	this.add_image = add_image;
	this.g_init = g_init;
	this.show_image = show_image;
	this.show_preload_image = show_preload_image;
	this.hideop = hideop;
	this.showimagen = showimagen;
}

function add_image( source, link, alt )
{
	this.url_images[this.url_images.length] = source;
	this.links[this.links.length] = link;
	this.alttext[this.alttext.length] = alt;
}

function g_init()
{
	width_links = this.widht - this.url_images.length * this.link_width;
	html = "<div id='" + this.divmain_id + "' style='width:" + this.widht + "px; height:" + this.height + "px;'><a id='main_glink' href='./'><img width='" + this.widht + "' height='" + this.height + "' id='" + this.img_id + "' src='/images/null.gif' border='0'></a><div id='div_count' style='display: none; position: absolute; margin-top: -25px; margin-left: " + width_links + "px;'>&nbsp;</div></div>";
	document.write( html );
	this.img_element = document.getElementById( this.img_id );
	this.divmain_element = document.getElementById( this.divmain_id );
	this.divcount_element = document.getElementById( 'div_count' );

	links_html = "";
	for( i=0; i <= this.url_images.length - 1; i++ )
	{
		links_html += "<a class='slide' id='" + this.obj_name + "_link_" + i + "' href='#' onclick='" + this.obj_name + ".showimagen(" + i + "); return false;'>" + ( i + 1 ) + "</a> ";
	}
	this.divcount_element.innerHTML = links_html;


	if ( navigator.appName == "Microsoft Internet Explorer" )
	{
		this.divcount_element.style.marginLeft = "-" + this.url_images.length * this.link_width + "px";
		heightie = this.height - this.link_width;
		this.divcount_element.style.marginTop = heightie + "px";
	}
	document.getElementById( this.obj_name + "_link_0" ).className = 'slide2';
	document.getElementById( 'main_glink' ).href = this.links[0];
	this.img_element.alt = this.alttext[0];

	var newimage = new Image();
	newimage.src = this.url_images[0];
	newimage.parent = this;
	this.images[0] = newimage.src;

	this.img_element.src = this.images[0];
	this.maintimer = setTimeout( this.obj_name + ".show_image()", this.interval );
	/*
	newimage.onload = function()
	{
		this.parent.img_element.src = this.parent.images[0];
		this.parent.maintimer = setTimeout( this.parent.obj_name + ".show_image()", this.parent.interval );
	}
	*/
}

function show_image()
{
	if ( this.url_images[this.number] )
	{
		var newimage = new Image();
		newimage.src = this.url_images[this.number];
		newimage.parent = this;
		this.images[this.number] = newimage.src;

		if ( this.lust_number !== false )
		{
			document.getElementById( this.obj_name + "_link_" + this.lust_number ).className = 'slide';
		}
		document.getElementById( this.obj_name+"_link_" + this.number ).className = 'slide2';
		this.img_element.src = this.images[this.number];
		this.img_element.alt = this.alttext[this.number];
		document.getElementById( 'main_glink' ).href = this.links[this.number];
		fadeOpacity( this.img_id, 'oR1' );
		this.maintimer = setTimeout( this.obj_name + ".show_image()", this.interval+400 );
		this.maintimer2 = setTimeout( this.obj_name + ".hideop()", this.interval );
		this.lust_number = this.number;
		this.number += 1;
		/*
		newimage.onload = function()
		{
			if ( this.parent.lust_number !== false )
			{
				document.getElementById( this.parent.obj_name + "_link_" + this.parent.lust_number ).className = 'slide';
			}
			document.getElementById( this.parent.obj_name+"_link_" + this.parent.number ).className = 'slide2';
			this.parent.img_element.src = this.parent.images[this.parent.number];
			this.parent.img_element.alt = this.parent.alttext[this.parent.number];
			document.getElementById( 'main_glink' ).href = this.parent.links[this.parent.number];
			fadeOpacity( this.parent.img_id, 'oR1' );
			this.parent.maintimer = setTimeout( this.parent.obj_name + ".show_image()", this.parent.interval+400 );
			this.parent.maintimer2 = setTimeout( this.parent.obj_name + ".hideop()", this.parent.interval );
			this.parent.lust_number = this.parent.number;
			this.parent.number += 1;
		}
		*/
	}
	else
	{
		if ( this.lust_number !== false )
		{
			document.getElementById( this.obj_name+"_link_" + this.lust_number ).className = 'slide';
		}
		document.getElementById( this.obj_name + "_link_0" ).className = 'slide2';
		this.img_element.src = this.images[0];
		this.img_element.alt = this.alttext[0];
		document.getElementById( 'main_glink' ).href = this.links[0];
		fadeOpacity( this.img_id, 'oR1' );
		this.backop = setTimeout( this.obj_name + ".hideop()", this.interval );

		this.number = 1;
		this.lust_number = 0;
		this.maintimer = setInterval( this.obj_name + ".show_preload_image()", this.interval + 400 );
	}
}

function show_preload_image()
{
	if ( this.images[this.number] )
	{
		if ( this.lust_number !== false )
		{
			document.getElementById( this.obj_name + "_link_" + this.lust_number ).className = 'slide';
		}
		this.lust_number = this.number;
		document.getElementById( this.obj_name + "_link_" + this.number ).className = 'slide2';
		this.img_element.src = this.images[this.number];
		fadeOpacity( this.img_id, 'oR1' );
		this.backop = setTimeout( this.obj_name+".hideop()", this.interval );
		document.getElementById( 'main_glink' ).href = this.links[this.number];
		this.img_element.alt = this.alttext[this.number];
		this.number += 1;
	}
	else
	{
		if ( this.lust_number !== false )
		{
			document.getElementById( this.obj_name + "_link_" + this.lust_number ).className = 'slide';
		}
		document.getElementById( this.obj_name + "_link_0" ).className = 'slide2';
		this.img_element.src = this.images[0];
		document.getElementById( 'main_glink' ).href = this.links[0];
		this.img_element.alt = this.alttext[0];
		fadeOpacity( this.img_id, 'oR1' );
		this.backop = setTimeout( this.obj_name + ".hideop()", this.interval );
		this.number = 1;
		this.lust_number = 0;
	}
}


function hideop()
{
	fadeOpacity.back( this.img_id );
}

function showimagen(num)
{
	if ( this.images[num] )
	{
		clearInterval( this.maintimer );
		if ( this.backop ){ clearTimeout( this.backop ); }
		document.getElementById( this.obj_name + "_link_" + num ).className = 'slide2';
		if ( this.lust_number !== false )
		{
			document.getElementById( this.obj_name + "_link_" + this.lust_number ).className = 'slide';
		}
		this.img_element.src = this.images[num];
		this.lust_number = num;
	}
	else if ( this.url_images[num] )
	{
		clearTimeout( this.maintimer );
		clearTimeout( this.maintimer2 );
		document.getElementById( this.obj_name + "_link_" + num ).className = 'slide2';
		if ( this.lust_number !== false )
		{
			document.getElementById( this.obj_name + "_link_" + this.lust_number ).className = 'slide';
		}
		var newimage = new Image();
		newimage.src = this.url_images[num];
		this.img_element.src = newimage.src;
		this.lust_number = num;
	}
}


function setElementOpacity(oElem, nOpacity)
{
	var p = getOpacityProperty();
	(setElementOpacity = (p=="filter") ? new Function('oElem', 'nOpacity', 'nOpacity *= 100;	var oAlpha = oElem.filters["DXImageTransform.Microsoft.alpha"] || oElem.filters.alpha;	if (oAlpha) oAlpha.opacity = nOpacity; else oElem.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+nOpacity+")";'):p?new Function('oElem', 'nOpacity', 'oElem.style.'+p+' = nOpacity;'):new Function)(oElem, nOpacity);
}

function getOpacityProperty()
{
	var p;
	if (typeof document.body.style.opacity == 'string') p = 'opacity';
	else if (typeof document.body.style.MozOpacity == 'string') p =  'MozOpacity';
	else if (typeof document.body.style.KhtmlOpacity == 'string') p =  'KhtmlOpacity';
	else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5) p =  'filter';

	return (getOpacityProperty = new Function("return '"+p+"';"))();
}

function fadeOpacity(sElemId, sRuleName, bBackward)
{
	var elem = document.getElementById(sElemId);
	if (!elem || !getOpacityProperty() || !fadeOpacity.aRules[sRuleName]) return;

	var rule = fadeOpacity.aRules[sRuleName];
	var nOpacity = rule.nStartOpacity;

	if (fadeOpacity.aProc[sElemId]) {clearInterval(fadeOpacity.aProc[sElemId].tId); nOpacity = fadeOpacity.aProc[sElemId].nOpacity;}
	if ((nOpacity==rule.nStartOpacity && bBackward) || (nOpacity==rule.nFinishOpacity && !bBackward)) return;

	fadeOpacity.aProc[sElemId] = {'nOpacity':nOpacity, 'tId':setInterval('fadeOpacity.run("'+sElemId+'")', fadeOpacity.aRules[sRuleName].nDalay), 'sRuleName':sRuleName, 'bBackward':Boolean(bBackward)};
}

fadeOpacity.addRule = function(sRuleName, nStartOpacity, nFinishOpacity, nDalay){fadeOpacity.aRules[sRuleName]={'nStartOpacity':nStartOpacity, 'nFinishOpacity':nFinishOpacity, 'nDalay':(nDalay || 30),'nDSign':(nFinishOpacity-nStartOpacity > 0?1:-1)};};

fadeOpacity.back = function(sElemId){fadeOpacity(sElemId,fadeOpacity.aProc[sElemId].sRuleName,true);};

fadeOpacity.run = function(sElemId)
{
	var proc = fadeOpacity.aProc[sElemId];
	var rule = fadeOpacity.aRules[proc.sRuleName];

	proc.nOpacity = Math.round(( proc.nOpacity + .1*rule.nDSign*(proc.bBackward?-1:1) )*10)/10;
	setElementOpacity(document.getElementById(sElemId), proc.nOpacity);

	if (proc.nOpacity==rule.nStartOpacity || proc.nOpacity==rule.nFinishOpacity) clearInterval(fadeOpacity.aProc[sElemId].tId);
}
fadeOpacity.aProc = {};
fadeOpacity.aRules = {};

// определяем количество блоков о компании на главной
$(document).ready(function()
{
//	cna = 242; cca = 1; // ширина одного блока с отступами; кол-во всегда открытых блоков
	cna = 320; cca = 1; // ширина одного блока с отступами; кол-во всегда открытых блоков
	countNov(cna, cca, 'about', 'about_block');
	
	// определяем количество спецов на главной
	cns = 449; ccs = 2;
	countNov(cns, ccs, 'spec', 'specs_header');

	// определяем количество новинок на главной
	cn = 242; cc = 4;
	countNov(cn, cc, 'nov', 'nov_header');

	$(window).bind('resize', function () 
	{
		countNov(cna, cca, 'about', 'about_block');
		countNov(cns, ccs, 'spec', 'specs_header');
		countNov(cn, cc, 'nov', 'nov_header');
	});
});

function countNov(cn, cc, cname, cheader) 
{
	var nw = $('.'+cheader).width();
	nov = nw - cn * cc;
	if (nov > (cn*2))
	{
		w = cn - 30;
		$('#'+cname+'_hid1').show();
		$('#'+cname+'_hid2').show();
	}
	else if (nov > cn)
	{
		w = cn - 80 + Math.round(nov / (cc + 1));
		ws = cn - 160 + Math.round(nov / (cc + 1));
		$('#'+cname+'_hid1').show();
		$('#'+cname+'_hid2').hide();
	}
	else
	{
		w = cn - 35 + Math.round(nov / cc);
		ws = cn - 10 + Math.round(nov / cc);
		$('#'+cname+'_hid1').hide();
		$('#'+cname+'_hid2').hide();
	}
	if (cname == 'nov')
	{
		$('#novelties').find('div').width(w);
		var height = 0;
		$('#novelties div').each(function () 
		{
			height = Math.max(height, this.offsetHeight);
		});
		$('#novelties').css("height", height + 80);
	}
	if (cname == 'spec')
	{
//		alert(w);
		$('#specs .frame_specs').width(ws);
	}
}
// определяем количество новинок, спецов и о компании на главной 

