/* СКРЫТИЕ ОБЪЕКТОВ 
------------------------------*/
if (document.getElementById){
document.write('\n<style type="text/css">\n')
document.write('.none {display:none;}\n')
document.write('</style>\n')
}

var jBoxParam = false;

(function($){ // jQuery Start
$(document).ready(function(){


/* ПОЛЯ И КНОПКИ
------------------------------*/
// Поля
var inputTxt = ':text, :password, :file, textarea, select';

$(inputTxt).addClass('input')
.each(function(){
	if(!this.value){
		$(this).val($(this).attr('title'));
		$('select').change(function(){ 
			$(this).blur(); 
		}).focus(function(){
			if($.browser.msie) $(this).addClass('select_focus');
		});
		/*if($(this).find('[@type=password]')){
			console.log($(this).val());
			$(this).each(function(){$(this).attr('input','text');});
		}*/
	}
})
.hover(
	function(){ $(this).addClass('iHover'); },
	function(){ $(inputTxt).removeClass('iHover'); }
)
.focus(function(){
	if($(this).val() == $(this).attr('title')){
		$(this).val('');
	}
	$(this).addClass('bgcolor_sl');
})
.blur(function(){
	if(!this.value){
		$(this).val($(this).attr('title'));
	}
	$(this).removeClass('bgcolor_sl');
});

// Кнопки
//$('input[@type=submit], input[@type=button]').addClass('button'); 

// Разное

/*var leftHeight = $('#right').height();
$('#leftBox').css({height:leftHeight});*/

$('div#panelInfo').each(function(){
	var t = $(this);
	var b = $(t).find('div.panelBody');
	var io = document.createElement('div');
	$(io).text('+').addClass('panelToggle').prependTo(t).show();
	$(t).hover(function(){
		$(this).click(function(){
			$(io).hide();
			$(b).show();
		});
	},function(){
			$(b).hide();
			//console.log($(b));
			$(io).show();
	});
});

function BoxSize(obj,param){
	//var xywh = document.body.offsetHeight;
	//alert(document.body.offsetHeight);
	//console.log(document.body.offsetHeight);
	var _ow = document.body.offsetWidth;
	var _oh = document.body.offsetHeight;
	//console.log(_ow);
	if('boxWIN'==obj||obj=='all'){
		obj = $('#'+obj);
		$(obj).attr({style:'top:'+((_oh - param[2]) / 2 )+'px; left:'+((_ow - param[1]) /2)+'px; width:'+param[1]+'px; height:'+param[2]+'px'});
		if ($.browser.msie) $('#boxBODY').attr({style:'width:'+param[1]+'px'});
	}
}

/* ВСПЛЫВАЮЩЕЕ ОКНО
------------------------------*/
$.fn.jBOX = function(e)
{
	function box(param){
		var param = param.split(',');
		var id = param[0].toString();
		//console.log($(id));

		if(!$(document).find('#box').is('#box')){
			$('body').append('<div id="box"><div id="boxBG" class="boxBG"></div><div id="boxWIN"><div title="Закрыть" id="boxClose"></div><b class="br10"></b><b class="br7"></b><b class="br5"></b><b class="br4"></b><b class="br3"></b><b class="br2"></b><b class="br2"></b><b class="br1"></b><b class="br1"></b><b class="br1"></b><div id="boxBODY"></div><b class="br1"></b><b class="br1"></b><b class="br1"></b><b class="br2"></b><b class="br2"></b><b class="br3"></b><b class="br4"></b><b class="br5"></b><b class="br7"></b><b class="br10"></b></div></div>');
		}
		$('#boxBODY').append($('#'+id));
		$('#'+id).show();
		$('#box').show();
		BoxSize('boxWIN',param);
		$(window).resize(BoxSize('boxWIN',param));

		$('#boxClose').click(function(){
			$('#box').hide();
		});
		$('#boxBG').click(function(){
			//console.log('YES');
			$('#box').hide();
		});
	}//fn:box

	if(e){ box(e.param); }
	$(this).click(function(){
		var param = $(this).attr('jBOX');
		box(param);
		return false;
	});	
}

if($('.jBOX').length){
	if(jBoxParam)
	$('.jBOX').jBOX(jBoxParam);
	else
	$('.jBOX').jBOX();
}


/* МЕНЮ
------------------------------*/
if($('div#menu').is('.in')){

$('div#menu ul.sub').hide();

$('div#menu li.head span').click(
	function(){
		$(this).toggleClass('open');
		if($(this).hasClass('open')){
			$(this).find('a').removeClass('dashed').addClass('select');
/*			$('div#login').css({height:'5%'});
			$('div#price').css({height:'5%'});
			$('div#search').css({height:'5%'});*/
			$(this).next('ul.sub').show(600);
			
			$('div#loginForm').hide(400);
			$('div#loginLink').show(1100);
		} else {
			$('div#loginLink').click();
		}
});
$('div#loginLink').click(function(){
	$('div#loginLink').hide(40);
	$('div#loginForm').show(400);
/*	$('div#login').css({height:'25%'});
	$('div#price').css({height:'20%'});
	$('div#search').css({height:'10%'});*/
	$('div#menu li.head span').find('a').addClass('dashed').removeClass('select');
	$('div#menu li.head span').next('ul.sub').hide(400);
});
} else {
	$('div#menu li.head span').find('a').removeClass('dashed').addClass('select');
}




/* СЛАЙДШОУ
------------------------------*/
// Захват событий
// http://experiment.net.ru/js2/?f=evnt.htm

$.fn.SlideShow = function(e)
{
	var iBigBox 		= $(e.iBigBoxTD).find('div');
	var iPrevBoxTD 	= this;
	var iPrevBox 		= this.find('ul li');
	var iPrev 			= this.find('img');
	var sTimer;
	var cycle 	= 0;
	var num 		= 0;

	function SlideImages(num)
	{
		///console.log("SI "+cycle);
		var iAct 		= $(iPrevBox[num]).find('img');
		var res 		= $(iAct).attr('res');
		var href 		= $(iAct).parents('a').attr('href');
		var price 	= $(iAct).parents('li').find(e.priceBox);

		if(num >= iPrevBox.length-1) num = 0; else num++;
		$(iPrev).fadeTo(10, 1);
		$(iAct).fadeTo(10, 0.2);
		$(e.iBigBoxTD).find(':animated').each(function(){$(this).stop(true, false);});
		$(e.iPrevBoxTD).find(e.priceBox).each(function(){$(this).stop(false, false).hide();});
		//$(iPrevBox).find(e.priceBox).each(function(){$(this).hide();});

		$(iBigBox).animate({opacity:0}, 200, 'linear', // Скрывает изображение
		function(){
			if(!$(iAct).hasClass('iBigLoad')) $(e.iBigBoxTD).addClass('loader');

	    $(iBigBox).html('<img src='+res+' />').find('img').bind('load',  // Загружает и контролирет загрузку изображения 
	    function(){
	    	var iBig = $(iBigBox).find('img');
	    	$(iBig).click(function(){window.location = href});
	    	if(!$(iAct).hasClass('iBigLoad')){
	    		$(e.iBigBoxTD).removeClass('loader');
	    		$(iAct).addClass('iBigLoad');
	    	}

	    	$(iBigBox).animate({opacity: 1}, 400, 'linear', // Отображает изображение
	    	function(){
	    		window.clearTimeout(sTimer);
	    		if(price) sTimer = window.setTimeout(function PricePos(){ // Цена : Отображение и позиционирование
	    			var iBigPos = iBig.offset();
	    			var iWidth = price.parent().width();
						var iHeight = price.parent().height();
						var iTop = iBigPos.top + (iBig.height() - iHeight - 30);
						var iLeft = iBigPos.left + (iBig.width() - iWidth / 2);
						$(price).stop();
						$(price).hide();
						$(price).css({position:'absolute', top:iTop, left:iLeft, width:iWidth, height:iHeight}).show(400, 
						function(){
							$(this).css({opacity:1});
						});
						$(window).resize(PricePos);
	    		}, 100);
					if(num < iPrevBox.length && cycle < 100){
						sTimer = window.setTimeout(function(){
							cycle++;
							SlideImages(num);
						}, 4000);
					} else {
						$('body').one("mousemove", function(){
							cycle = 0;
							SlideImages(num);
						}); 
					}
	    	})
	    });
	  });
  } SlideImages(num);

	var mOver = function(){ // Навели курсор
		window.clearTimeout(sTimer);
	}
	var mOut = function(){ // Отвели курсор
		sTimer = window.setTimeout(function(){
			if(num >= iPrevBox.length-1) num = 0; else num++;
			SlideImages(num);
		}, 600);
	}
	$('body').mousemove(function(){ cycle = 0; }); // Обнуление цикла
	$(iBigBox).hover(mOver,mOut).mousemove(mOver); // Навели на превью
  $(iPrev).click(function(){ // Нажатие на превью
  	window.clearTimeout(sTimer);
  	num = $(this).parents('li').prevAll('li').size();
  	SlideImages(num);
  	return false;
	});
	// Изменение размеров эскизов
	if(iPrev.width() > 80) iPrev.width(80);
	if(iPrev.height() > 80) iPrev.height(80);
};

if($('table#cHome_gallery').length)
$('td#cHome_iPrev').SlideShow({
	iBigBoxTD:'td#cHome_iBig',
	priceBox:'div.cHome_price'
});



/* КАТАЛОГ
------------------------------*/
/*$('div.catalog_price_brief').each(function(){
	var price_this = this;
	//$(this).text().match(new RegExp(/[0-9\.]+/));
	console.log($(this).text().match(new RegExp(/[0-9\.]+/)));
//$().find(price).addClass('orange'); 
});
*/
$('div.string_product_sort a:first').click(function(){
	$(this).next('span:first').show();
	$(this).hide();
	return false;
});

$('div.catalog_pic_box_middle').hover(
function(){
	$(this).fadeTo(200, 0.7);
},function(){
	$(this).fadeTo(200, 1);
});



});//ready
})(jQuery);
