// Обновление контента заданной области через AJAX
function ajax_load (container, ajax_call, params) {
	if (!$.isArray(params)) {
		params = $.queryString(params);
	}
	$.post(
		'/bitrix/tools/ajax.php?ajax_call='+ajax_call,
		params,
		function (data) {
			if (container) {
				$(container).html(data);
			}
		}
	);
	//return false;
}
function additional_load (container, ajax_call, params) {
	if (!$.isArray(params)) {
		params = $.queryString(params);
	}
	$.post(
		'/bitrix/templates/main/components/bexx/catalog.items/additional/additional.php',
		params,
		function (data) {
			if (container) {
				$(container).html(data);
			}
		}
	);
	//return false;
}
// Блокировка содержимого обновляемой области и ее затемнение
function ajax_block (target) {
	$(target).prepend('<div id="ajax_loader_bg" style="position: absolute; background: #000 url(/bitrix/templates/main/images/ajax-loader.gif) center no-repeat; text-align: center;"> </div>');
	$('#ajax_loader_bg').width($(target).width()).height($(target).height()).css('opacity', '0.1');
}
$(function(){

	$('#slider').nivoSlider({
		animSpeed:500,
		pauseTime:8000,
		effect: 'fold,fade,boxRandom,boxRain,boxRainReverse,boxRainGrow,boxRainGrowReverse',
		directionNav:true,
		directionNavHide:false,					
	});		

	$('input').each(function(i,e){
		if (!$(e).val() && $(e).attr('title')) {
			$(e).addClass('default');
			$(e).val($(e).attr('title'));
		}
	});
	$('input').focus(function(){
		if ($(this).val()==$(this).attr('title')){
			$(this).val('');
			$(this).removeClass('default');
		}
	});
	$('input').blur(function(){
		if (!$(this).val()){
			$(this).val($(this).attr('title'));
			$(this).addClass('default');
		}
	});
	/** 
	 * Обработка ссылок для AJAX
	 * 
	 * <a href="#some_text" class="ajax_link" rel="AJAX_CALL_ID" params="IBLOCK_ID=1&TYPE=2">update</a>
	 * href="#some_text" - указатель на контейнер. #some_text = <div id="some_text"></div>
	 * class="ajax_link" - данная ссылка будет обрабатываться как AJAX
	 * rel="AJAX_CALL_ID" - md5-хэш, идентификатор AJAX объекта
	 * params="IBLOCK_ID=1&TYPE=2" - дополнительные и заменяемые параметры для AJAX-объекта
	 */
	$(".ajax_link").click(function(){ // Обработка ссылок AJAX
		ajax_load($(this).attr('href'), $(this).attr('rel'), $(this).attr('params'));
		return false;
	});

	$("#indexbrandcontent").carouFredSel({
		
		prev : {	
			button	: "#brand_prev",
			key		: "left"
		},
		next : { 
			button	: "#brand_next",
			key		: "right"
		},
		pagination	: "#brand_pag"
	});
	
	$("#additionals1").carouFredSel({
		circular : false,
		prev : {	
			button	: "#add_prev1",
			key		: "left"
		},
		next : { 
			button	: "#add_next1",
			key		: "right"
		},
		pagination	: "#brand_pag"
	});	
	
	$("#additionals2").carouFredSel({
		circular : false,
		prev : {	
			button	: "#add_prev2",
			key		: "left"
		},
		next : { 
			button	: "#add_next2",
			key		: "right"
		},
		pagination	: "#brand_pag"
	});	
	
	$("#additionals3").carouFredSel({
		circular : false,
		prev : {	
			button	: "#add_prev3",
			key		: "left"
		},
		next : { 
			button	: "#add_next3",
			key		: "right"
		},
		pagination	: "#brand_pag"
	});		
	
	width = $(document).width();
	height = $(window).height();
	$('#quickcart').css('left', ((width/2)-150));	
	$('#followProductblock').css('left', ((width/2)-150));
	$('#lowprice').css('left', ((width/2)-235)).css('top', ((height/2)-190));
	var buypanelCookie = $.cookie('propechiru_buy_panel');

	if (buypanelCookie == 'open') {
		$('#panelcontent').css('display', 'block');
		$.cookie('propechiru_buy_panel', 'open', { path: '/' });
	} else {
		$('#panelcontent').css('display', 'none');
		$.cookie('propechiru_buy_panel', 'close', { path: '/' });
	}

	$("#lowpriceform").ajaxForm(function() {
		alert("Ваше сообщение отправлено. Мы свяжемся с вами в ближайшее время.");
		$('#lowprice').fadeOut();
	});	
	
	
	$("#panel_oc").click(function(){
		if ($('#panelcontent').css('display')=='none')
		{
			$('#panelcontent').slideDown('slow');
			$.cookie('propechiru_buy_panel', 'open', { path: '/' });
		} else {
			if($('#panelcontent').css('display')=='block') {
				$('#panelcontent').slideUp('slow');
				$.cookie('propechiru_buy_panel', 'close', { path: '/' });
			}
		}
	});
	$("#qc_open").click(function(){ 
		$("#quickcart").fadeIn();
	});	
	$("#qc_close").click(function(){ 
		$("#quickcart").fadeOut();
	});			
	$("#fp_open").click(function(){ 
		$("#followProductblock").fadeIn();
	});	
	$("#fp_close").click(function(){ 
		$("#followProductblock").fadeOut();
	});	
	$("#lowprice_open").click(function(){ 
		$("#lowprice").fadeIn();
	});	
	$("#lp_close").click(function(){ 
		$("#lowprice").fadeOut();
	});		
	$("#fp_open").click(function(){ 
		$("#followProductblock").fadeIn();
	});	
	$("#quickcart").draggable({ containment: 'document' });
	$("#followProductblock").draggable({ containment: 'document' });
	ODKL.init();
	$("a[rel='more_photo']").colorbox();
	$('#webmaster').colorbox({inline: true});
	
});
