$(function(){
	//--------- правила акции
	$("#RuleLink1").mouseup(function(){
		if($("#Rule").css('display') == 'none'){
			$("#RuleLink1").removeClass("RuleOpen");
			$("#RuleLink1").addClass("RuleClose");
			$("#Rule").css('display','block');
		}else{
			$("#RuleLink1").removeClass("RuleClose");
			$("#RuleLink1").addClass("RuleOpen");
			$("#Rule").css('display','none');
		}
	});
	
	//--------- правила акции
	$("#RuleLink2").mouseup(function(){
		if($("#Rule").css('display') == 'none'){
			$("#RuleLink1").removeClass("RuleOpen");
			$("#RuleLink1").addClass("RuleClose");
			$("#Rule").css('display','block');
		}else{
			$("#RuleLink1").removeClass("RuleClose");
			$("#RuleLink1").addClass("RuleOpen");
			$("#Rule").css('display','none');
		}
	});
	
	//--------- переключение закладок
	$('a','.Tabs').mouseup(function(){
		$('td',$(this).parent().parent().parent()).each(function(i){
			$(this).removeClass('act').removeClass('n');
			if(i==0)
				$(this).addClass('n');
		});
		$(this).parent().parent().addClass('act');
		$(this).parent().parent().next().addClass('n');
		obContents = $($('.TabsBlock'),$(this).parent().parent().parent().parent().parent().parent().parent().parent().parent());
		$(obContents).css('display','none');
		$('td','.Tabs').each(function(i){
			if($(this).hasClass('act')){
				$(obContents).each(function(j){
					if(i==j){
						$(this).css('display','block');
					}
				});
			}
		});
	});
	$('.Tabs').each(function(){
		$('td',$(this)).each(function(i){
			if(i==1)
				$(this).addClass('n');
		});
	});
	
	
	
	//--------- листание телефонов на главной
	$('.nextPhone').mouseup(function(){
		var counter = 0;
		var Width = 0;
		var WidthDrive = 0;
		$('li',$(this).parent().parent()).each(function(i){
			if($(this).attr('value') != '1'){
				Width += $(this).width();
			}else{
				WidthDrive += $(this).width();
			}
		});
		if(Width > 308){
			$('li',$(this).parent().parent()).each(function(i){
				if($(this).attr('value') != '1' && counter == 0){
					counter++;
					var L = -$(this).width()*1 - WidthDrive*1;
					$(this).parent().animate({left:L+'px'}, 300);
					$(this).attr('value', '1');
					$('a', $(this).next()).focus();
					$(this).parent().children('li').removeClass('act');
					$(this).next().addClass('act');
					//$(this).css('display', 'none');
				}
			});
		}
	});
	
	//--------- листание телефонов на главной
	$('.backPhone').mouseup(function(){
		var counter = 0;
		var WidthDrive = 0;
		$('li','.listPhone').each(function(i){
			if($(this).attr('value') != '1'){
			}else{
				WidthDrive += $(this).width();
			}
		});

		$('li','.listPhone').each(function(i){
			if($(this).attr('value') != '1' && counter == 0){
				counter++;
				if($(this).prev().html() != null){
				var L = $(this).prev().width()*1 - WidthDrive+'px';
				$(this).parent().animate({left:L}, 300);
				$(this).prev().attr('value', '2');
				$('a', $(this).prev()).focus();
				$(this).parent().children('li').removeClass('act');
				$(this).prev().addClass('act');
				//$(this).prev().css('display', 'block');
				}
			}
		});
		
	});
	
	$('h1').each(function(){
		var str = $(this).html();
		re = /&lt;sup&gt;3&lt;\/sup&gt;/g;
		str = str.replace(re, "<sup>3</sup>");
		$(this).html(str);
	});
	
	MenuWidth = $('.menuTop').children('div').children('div').width();
	MenuLiWidth = 0;
	$('li','.menuTop').each(function(){
		MenuLiWidth += $(this).width();
	});
	$('.menuTop').children('div').children('div').css('padding-left', (MenuWidth-MenuLiWidth)/2)
	
});

//--------- вывод карзины
function AddBasket(id, type, action){
	$('.BannerSmall').css('display','none');
	$.get('/bitrix/templates/Main_new/include_files/basket/basket_small.php',{ID:id,TYPE:type,ACTION:action,r:Math.random()}, function(data){
		$('.BasketSmall').html(data);
	});
	return false;
}


//-------- Загрузка телефона на главной странице
function Phone(SITE_ID, arSectionID, id, ob){
	$('.top','#TabsBlock'+arSectionID).html('<img src="/bitrix/templates/Main_new/images/loading_indicator.gif" /> Loading...');

	$(ob).parent().parent().parent().children('li').removeClass('act');
		$(ob).parent().parent().addClass('act');
	
	$.get('/bitrix/templates/Main_new/include_files/equipment/phone_main_detail.php',{r:Math.random(),SITE_ID:SITE_ID,ID:id},function(data){
		$('.top','#TabsBlock'+arSectionID).html(data);
	});
	return false;
}

//--------- фильтр по терминалам
function filterMain(SITE_ID, ob, ID_SECTION){
	$(".listPhoneLoad",$(ob).parent().parent().parent().parent()).html('<img src="/bitrix/templates/Main_new/images/loading_indicator.gif" /> Loading...');
	$(".PhoneDetailMain",$(ob).parent().parent().parent().parent().parent()).html('<img src="/bitrix/templates/Main_new/images/loading_indicator.gif" /> Loading...');
	var C_BLUETOOTH, C_IRDA, N_EVDO, T_MP3_PLEER, SITE_ID;
	$('input', $(ob).parent().parent()).each(function(i){
		if($(this).attr('name') == "C_BLUETOOTH"){
			if($(this).attr('checked') == true)
			C_BLUETOOTH = "да";
		}
		if($(this).attr('name') == "C_IRDA"){
			if($(this).attr('checked') == true)
			C_IRDA = "да";
		}
		if($(this).attr('name') == "N_EVDO"){
			if($(this).attr('checked') == true)
			N_EVDO = "да";
		}
		if($(this).attr('name') == "T_MP3_PLEER"){
			if($(this).attr('checked') == true)
			T_MP3_PLEER = "да";
		}
	});
	
	//------------- загрузка списка терминалов
	$.get("/bitrix/templates/Main_new/include_files/equipment/phone_main_list.php",{r:Math.random(),SITE_ID:SITE_ID,ID_SECTION:ID_SECTION,C_BLUETOOTH:C_BLUETOOTH, C_IRDA:C_IRDA, N_EVDO:N_EVDO, T_MP3_PLEER:T_MP3_PLEER}, function(data){
		//alert($(ob).parent().parent().parent().parent().html())
		$(".listPhoneLoad",$(ob).parent().parent().parent().parent()).html(data);
	});
	$.get("/bitrix/templates/Main_new/include_files/equipment/phone_main_list_first_id.php",{r:Math.random(),SITE_ID:SITE_ID ,ID_SECTION:ID_SECTION,C_BLUETOOTH:C_BLUETOOTH, C_IRDA:C_IRDA, N_EVDO:N_EVDO, T_MP3_PLEER:T_MP3_PLEER}, function(id){
		Phone(SITE_ID, ID_SECTION, id);
	});
}

//-------создает объект из формы.
function CreateObForm(ob){
	var params = {};
	
	params["r"] = Math.random();
	
	$('input', $(ob)).each(function(){
		if($(this).attr('type') == 'checkbox'){
			if($(this).attr('checked') == true)
				params[$(this).attr('name')] = $(this).attr('checked');
			else
				params[$(this).attr('name')] = false;
		}else
			params[$(this).attr('name')] = $(this).attr('value');
	});
	
	$('textarea', $(ob)).each(function(){
		params[$(this).attr('name')] = $(this).attr('value');
	});
	return params;
}


function WindowUpLoad(url, width, height){
	var scroll = "no";
	var top=0, left=0;
	if(width > screen.width-10 || height > screen.height-28) scroll = "yes";
	if(height < screen.height-28) top = Math.floor((screen.height - height)/2-14);
	if(width < screen.width-10) left = Math.floor((screen.width - width)/2-5);
	width = Math.min(width, screen.width-10);
	height = Math.min(height, screen.height-28);
	var wnd = window.open(url,"popup","width="+width+",height="+height+",scrollbars="+scroll+",resizable=yes,left="+left+",top="+top);
	return false;
}

function closePopup()
{
	$('#PopupBackground').toggleClass('Hidden').removeAttr('style');
	$('#PopupData').toggleClass('Hidden');
	return false;
}

//open pop-up
function showPopup(popup_type){
	if($.browser.msie){
		$('#PopupBackground').height($(document).height()).toggleClass('Hidden');
	}else{
		$('#PopupBackground').height($(document).height()).toggleClass('Hidden').fadeTo('slow', 0.8);
	}

	$('#PopupData')
	.html(popup_type.html())
	.alignCenter()
	.toggleClass('Hidden');
	return false;
}

$(function(){
	$.fn.alignCenter = function() {
		var marginLeft = Math.max(40, parseInt($(window).width()/2 - $(this).width()/2)) + 'px';
		var marginTop = Math.max(40, parseInt($(window).height()/2 - $(this).height()/2)) + 'px';
		return $(this).css({'margin-left':marginLeft, 'margin-top':marginTop});
	};
});