// JavaScript Document
var arSelectors=["S50","NGROUP_TV2","NGROUP_TV3","NGROUP_TV4","NGROUP_TV5"];

$(document).ready(function() 
{
	//alert($(".right_sidebar").css("width"));
	onLoadDropdown();
	//onLoadDropdownSort();	
	onLoadFilterNew();
	//onLoadCatalogList();
	
	if (document.getElementById("carousel_compare"))
	{
		$("#id_carousel_compare_items_count").val($("#carousel_compare > li").length);
		jQuery('#carousel_compare').jcarousel(
		{
			scroll: 1,
			//wrap: 'circular',
			itemVisibleInCallback: {
				onAfterAnimation:  ccCallback
			}
		});
	}
	
	$(".basket_products_count").keydown(function(event)
	{
	   switch (event.keyCode)
	   {
		   case 40:
			var num=$(this).attr("rel");
			num=num.substr(3);
			num=num*1+1;
			$(".basket_products_count[rel=num"+num+"]").focus();
		   break;
		   
		   case 38:
			var num=$(this).attr("rel");
			num=num.substr(3);
			num=num*1-1;
			$(".basket_products_count[rel=num"+num+"]").focus();
		   break;
	   }
	});
	
});

function ccCallback(carousel, item, idx, state) {
	var pos=0;
	if (state=="next")
	{
		pos=idx-2;
	}
	else if (state=="prev")
	{
		pos=idx;
	}
	if (pos!=0)
	{
		var count=$("#id_carousel_compare_items_count").val();
		//alert((pos-1)+" "+(count-3)+" "+(pos-1)*(726/(count-3)));
		$(".jcarousel-cursor").css("margin-left",(pos-1)*(726/(count-3)));
		$(".compare_div").removeClass("compare_border");
		$("#compare_item"+pos).addClass("compare_border");
		$("#compare_item"+(pos+1)).addClass("compare_border");
	}
};

/*
function onLoadDropdownSort()
{
	$(".dropdown_sort").change(function () 
	{
		val=$(this).val();
		if (val!=null)
		{
			for (var key in arSelectors) 
			{
				var n=arSelectors[key];
				if (this.name!="arrFilter_sort["+n+"]")
				{
					$("#PROP_SORT_"+n).val("");
				}
			}
			AjaxProductsShow(1);
		}
	});
}
*/

function onLoadDropdown()
{
   $(".dropdown").dropdownchecklist(
   {
	   icon:
	   {
		   placement: 'right', 
		   toOpen: 'ui-icon-arrowthick-1-s', 
		   toClose: 'ui-icon-arrowthick-1-n' 
	   },
	   firstItemChecksAll: true,
	   maxDropHeight: 350, 
	   width: 132,
	   filterButton: 'Y',
	   onComplete: function(selector) 
	   {
		   $("#id_selectorname").val(selector.name);
		   if ($("#id_makefilter").val()==1)
		   {
			   GetFilterSelect();
		   }
	   }
   });
   
}

function onLoadCatalogList()
{
   ajaxGetPage();
   basketRefresh();
   
   $(".products_count").keydown(function(event)
   {
	   switch (event.keyCode)
	   {
		   case 13:
		   case 40:
			var num=$(this).attr("rel");
			num=num.substr(3);
			num=num*1+1;
			$(".products_count[rel=num"+num+"]").focus();
		   break;
		   
		   case 38:
			var num=$(this).attr("rel");
			num=num.substr(3);
			num=num*1-1;
			$(".products_count[rel=num"+num+"]").focus();
		   break;
	   }
   });
   
   $(".catalog_pic_plus").mousemove(function(e)
   {
	   var id=$(this).attr("id");
	   var imgNew='<img width="100" id="PreviewImg" name="PreviewImg" src="/images/copco/catalog/'+id+'.jpg" >';
	   var lMouse=e.pageX+5;            
	   var TMouse=e.pageY+5;
	   
	   $('#ImageBox').html(imgNew);
	   $('#ImageBox').css('left',lMouse);
	   $('#ImageBox').css('top',TMouse);
	   $('#ImageBox').css('visibility','visible');
   }).mouseout(function()
   {
	   $('#ImageBox').css('visibility','hidden');
   });
	   
   
   //alert($("#content").outerHeight());
   var h=$("#content").outerHeight();
   $("#sideLeft").css("height",h);
   $("#sideRight").css("height",h);
}

function setMainFilter()
{
	setTimeout(GetFilterSelect,10);
}

function setMakeFilter()
{
	$("#id_makefilter").val(1);
	//addProductsToBasket();
}

function GetFilterSelect()
{
	//alert(selectorname);
	//var arSelectors=["S50","NGROUP_TV2","NGROUP_TV3","NGROUP_TV4","NGROUP_TV5"];
	var arData=new Array();
	//alert($("#id_selectorname").val());
	for (var key in arSelectors) 
	{
		var val=arSelectors[key];
		arData[val]=$("#PROP_"+val).val();
		//alert(arData[val]);
		if (arData[val]==null)
		{
			arData[val]="";
		}
		else if (arData[val][0]=="%")
		{
			arData[val]="";
		}
		arData['SORT_'+val]=$("#PROP_SORT_"+val).val();
	}
	jsAjaxUtil.ShowLocalWaitWindow('waitCatalogFilter', 'id_table_filters');
	jsAjaxUtil.ShowLocalWaitWindow('waitProductsShow', 'catalog_tree1');
	$.ajax(
	{
		url: '/ajax/index.php', 
		type: "POST", 
		timeout: 15000, 
		cache: false,
		data: 
		{
			cmd: 'GetFilterSelect',
			selectorname: $("#id_selectorname").val(),
			S50: arData["S50"],
			NGROUP_TV2: arData["NGROUP_TV2"],
			NGROUP_TV3: arData["NGROUP_TV3"],
			NGROUP_TV4: arData["NGROUP_TV4"],
			NGROUP_TV5: arData["NGROUP_TV5"]
			//SORT_S50: arData["SORT_S50"],
			//SORT_NGROUP_TV2: arData["SORT_NGROUP_TV2"],
			//SORT_NGROUP_TV3: arData["SORT_NGROUP_TV3"],
			//SORT_NGROUP_TV4: arData["SORT_NGROUP_TV4"],
			//SORT_NGROUP_TV5: arData["SORT_NGROUP_TV5"]
		},
		dataType: 'text', 
		success: function(data)
		{
			$("#catalog_filter").html(data);
			AjaxProductsShow();
			//jsAjaxUtil.CloseLocalWaitWindow('waitCatalogFilter', 'catalog_filter');
		}, error: function(event) {}
	});
} 

function ClearFilter()
{
	//addProductsToBasket();
	jsAjaxUtil.ShowLocalWaitWindow('waitCatalogFilter', 'id_table_filters');
	jsAjaxUtil.ShowLocalWaitWindow('waitProductsShow', 'catalog_tree1');
	$.ajax(
	{
		url: '/ajax/index.php', 
		type: "POST", 
		timeout: 15000, 
		cache: false,
		data: 
		{
			cmd: 'ClearFilter'
		},
		dataType: 'text', 
		success: function(data)
		{
			$("#catalog_filter").html(data);
			AjaxProductsShow();
		}, error: function(event) {}
	});
}

function ClearFilterPiter()
{
	//addProductsToBasket();
	jsAjaxUtil.ShowLocalWaitWindow('waitCatalogFilter', 'id_table_filters');
	jsAjaxUtil.ShowLocalWaitWindow('waitProductsShow', 'catalog_tree1');
	$.ajax(
	{
		url: '/ajax/index.php',
		type: "POST",
		timeout: 15000,
		cache: false,
		data:
		{
			cmd: 'ClearFilterPiter'
		},
		dataType: 'text',
		success: function(data)
		{
			$("#catalog_filter").html(data);
			AjaxProductsShow();
		}, error: function(event) {}
	});
}

function AjaxProductsShow(shadow)
{
	addProductsToBasket();
	//var arSelectors=["S50","NGROUP_TV2","NGROUP_TV3","NGROUP_TV4","NGROUP_TV5"];
	var arData=new Array();
	for (var key in arSelectors) 
	{
		var val=arSelectors[key];
		arData['SORT_'+val]=$("#PROP_SORT_"+val).val();
	}
	if (shadow!=null) jsAjaxUtil.ShowLocalWaitWindow('waitProductsShow', 'catalog_tree1');
	var countOnPage=$("#id_catalog_num_rows").val();
	$.ajax(
	{
		url: '/catalog/index.php', 
		type: "POST", 
		timeout: 15000, 
		cache: false,
		data: 
		{
			cmd: 'GetProducts',
			//SORT_S50: arData["SORT_S50"],
			//SORT_NGROUP_TV2: arData["SORT_NGROUP_TV2"],
			//SORT_NGROUP_TV3: arData["SORT_NGROUP_TV3"],
			//SORT_NGROUP_TV4: arData["SORT_NGROUP_TV4"],
			//SORT_NGROUP_TV5: arData["SORT_NGROUP_TV5"],
			countOnPage: countOnPage,
			AJAX: 'Y'
		},
		dataType: 'text', 
		success: function(data)
		{
			$("#catalog_tree1").html(data);
			jsAjaxUtil.CloseLocalWaitWindow('waitProductsShow', 'catalog_tree1');
			if (shadow==null) jsAjaxUtil.CloseLocalWaitWindow('waitCatalogFilter', 'id_table_filters');
		}, error: function(event) {}
	});
}

function Select_PR()
{
	var action=$("#id_sel_actions").val();
	switch (action)
	{
		case "delbasket":
		  var arElements=new Array();
		  $(".products_checkbox:checked").each(function () 
		  {
			  var id=$(this).attr("id");
			  id=id.substr(7);
			  arElements.push(id);
		  });
		  if (arElements.length>0)
		  {
			  jsAjaxUtil.ShowLocalWaitWindow('waitProductsShow', 'catalog_tree1');
			  jsAjaxUtil.ShowLocalWaitWindow('waitGetBasket', 'div_basket');
			  $.ajax(
			  {
				  url: '/ajax/index.php', 
				  type: "POST", 
				  timeout: 15000, 
				  cache: false,
				  data: 
				  {
					  cmd: 'DelBasket',
					  arElements: arElements
				  },
				  dataType: 'text', 
				  success: function(data)
				  {
					  $("#div_basket").html(data);
					  jsAjaxUtil.CloseLocalWaitWindow('waitGetBasket', 'div_basket');
					  jsAjaxUtil.CloseLocalWaitWindow('waitProductsShow', 'catalog_tree1');
				  }, error: function(event) {}
			  });
		  }
		  else
		  {
			  alert("Не выбраны товары для удаления из корзины");
		  }
		break;
		
		case "compare":
		  var arElements=new Array();
		  $(".products_checkbox:checked").each(function () 
		  {
			  var id=$(this).attr("id");
			  id=id.substr(7);
			  arElements.push(id);
		  });
		  if (arElements.length>0)
		  {
			  jsAjaxUtil.ShowLocalWaitWindow('waitProductsShow', 'catalog_tree1');
			  //jsAjaxUtil.ShowLocalWaitWindow('waitGetCompare', 'div_compare');
			  $.ajax(
			  {
				  url: '/ajax/index.php', 
				  type: "POST", 
				  timeout: 15000, 
				  cache: false,
				  data: 
				  {
					  cmd: 'AddToCompare',
					  arElements: arElements
				  },
				  dataType: 'text', 
				  success: function(data)
				  {
					  $(".products_checkbox").attr("checked","");
					  $(".tr_chk").css("background-color","");
					  //$("#div_compare").html(data);
					  //jsAjaxUtil.CloseLocalWaitWindow('waitGetCompare', 'div_compare');
					  jsAjaxUtil.CloseLocalWaitWindow('waitProductsShow', 'catalog_tree1');
					  window.open("/catalog/compare.php");
				  }, error: function(event) {}
			  });
		  } 
		  else
		  {
			  alert("Не выбраны товары для сравнения");
		  }
		break;
		
		case "addbasket":
		  var arPrice=new Array();
		  var arPriceCount=new Array();
		  $(".products_count").each(function () 
		  {
			  var val=$(this).val();
			  if (val>0)
			  {
				  var id=$(this).attr("id");
				  id=id.substr(5);
				  arPrice.push(id);
				  arPriceCount.push(val);
			  }
		  });
		  $(".products_count").val("");
		  if (arPrice.length>0)
		  {
			  jsAjaxUtil.ShowLocalWaitWindow('waitProductsShow', 'catalog_tree1');
			  jsAjaxUtil.ShowLocalWaitWindow('waitGetBasket', 'div_basket');
			  $.ajax(
			  {
				  url: '/ajax/index.php', 
				  type: "POST", 
				  timeout: 15000, 
				  cache: false,
				  data: 
				  {
					  cmd: 'AddProductToBasket',
					  arPrice: arPrice,
					  arPriceCount: arPriceCount
				  },
				  dataType: 'text', 
				  success: function(data)
				  {
					  $("#div_basket").html(data);
					  jsAjaxUtil.CloseLocalWaitWindow('waitGetBasket', 'div_basket');
					  jsAjaxUtil.CloseLocalWaitWindow('waitProductsShow', 'catalog_tree1');
					  alert("Товары добавлены в корзину");
				  }, error: function(event) {}
			  });
		  }
		  else
		  {
			  alert("Не выбраны товары для добавления в корзину\nЧтобы добавить товар в корзину необходимо ввести его количество");
		  }
		 break;
	}
}

function onLoadFilterNew()
{
	$("#f_all").click(function () 
	{
		$(".f_links").removeClass("active");
		$(this).addClass("active");
		catalogAddFilter("prop","NEW","");
		return false;
	});
	$("#f_new").click(function () 
	{
		$(".f_links").removeClass("active");
		$(this).addClass("active");
		catalogAddFilter("prop","NEW","1");
		return false;
	});
}

function catalogAddFilter(type,code,val)
{
	if ((type!=null) && (code!=null))
	{
		jsAjaxUtil.ShowLocalWaitWindow('waitProductsShow', 'catalog_tree1');
		if (val==null) val="";
		$.ajax(
		{
			url: '/ajax/index.php', 
			type: "POST", 
			timeout: 15000, 
			cache: false,
			data: 
			{
				cmd: 'AddFilter',
				type: type,
				code: code,
				val: val
			},
			dataType: 'text', 
			success: function(data)
			{
				AjaxProductsShow();
			}, error: function(event) {}
		});
	}
}


function clearCompareList()
{
	$.ajax(
	{
		url: '/ajax/index.php', 
		type: "POST", 
		timeout: 15000, 
		cache: false,
		data: 
		{
			cmd: 'ClearCompareList'
		},
		dataType: 'text', 
		success: function(data)
		{
		}, error: function(event) {}
	});
	if (window.opener)
	{
		//var div_compare=window.opener.document.getElementById("div_compare");
		//div_compare.innerHTML="Сравниваемых товаров: 0";
	}
}

function clearWindowCloseEvent()
{
	window.onbeforeunload = null;
}

var print_r = function(variable, deep, index) {
		if (variable===undefined) {alert('undefined'); return false;}
		if (deep===undefined) {var deep = 0;}
		if (index===undefined) {var index = '';} else {index+=': ';}
		var mes = '';
		var i = 0;
		var pre = '<br>';
		while (i<deep) {pre+='\t'; i++;}
		if (typeof(variable)=='object') {
			mes+=pre+index+' {';
			for (index in variable) {
				mes+=print_r(variable[index], (deep+1), index);
			}
			mes+=pre+'}';
		} else {
			mes+=pre+index+variable;
		}
		if (deep) {return mes;} else {document.write('<pre>'+mes+'</pre>');}
	}
	
function addProductsToBasket()
{
	var arPrice=new Array();
	var arPriceCount=new Array();
	$(".products_count").each(function () 
	{
		var val=$(this).val();
		if (val>0)
		{
			var id=$(this).attr("id");
			id=id.substr(5);
			arPrice.push(id);
			arPriceCount.push(val);
		}
	});
	$(".products_count").val("");
	if (arPrice.length>0)
	{
		$.ajax(
		{
			url: '/ajax/index.php', 
			type: "POST", 
			timeout: 15000, 
			cache: false,
			data: 
			{
				cmd: 'AddProductToBasket',
				arPrice: arPrice,
				arPriceCount: arPriceCount
			},
			dataType: 'text', 
			success: function(data)
			{
			}, error: function(event) {}
		});
	}
	//alert("addProductsToBasket");
}

function basketRefresh()
{
	$.ajax(
	{
		url: '/ajax/index.php', 
		type: "POST", 
		timeout: 15000, 
		cache: false,
		data: 
		{
			cmd: 'AddProductToBasket'
		},
		dataType: 'text', 
		success: function(data)
		{
			//alert("basketRefresh");
			$("#div_basket").html(data);
		}, error: function(event) {}
	});
}

function ajaxGetPage()
{
	$(".ajaxgetpage").click(function () 
	{
		addProductsToBasket()
		var href=$(this).attr("href");
		jsAjaxUtil.ShowLocalWaitWindow('waitProductsShow', 'catalog_tree1');
		$.ajax(
		{
			url: href, 
			type: "POST", 
			timeout: 15000, 
			cache: false,
			data: 
			{
				AJAX: 'Y'
			},
			dataType: 'text', 
			success: function(data)
			{
				$("#catalog_tree1").html(data);
				jsAjaxUtil.CloseLocalWaitWindow('waitProductsShow', 'catalog_tree1');
			}, error: function(event) {}
		});
		return false;
	});
	return false;
}
