var compareIsPopup = false;

var compareInit = function()
{
	var body = $$("body").first();
	new Insertion.Bottom(body,'<script type="text/javascript" src="js/windows/window.js"><\/script>');
	new Insertion.Bottom(body,'<style type="text/css">@import url(js/windows/themes/default.css)<\/style>');
	new Insertion.Bottom(body,'<div id="comparePopupDiv" style="display: none;">1111111111111</div>');	

//<link href="js/windows/themes/spread.css" rel="stylesheet" type="text/css"/> 
}

var comparePopup = function()
{
	popupWin = new Window({
			className: "alphacube", 
			title: "<h3>Сравниваемые товары</h3>", 
			width:200, 
			height:100, 
			destroyOnClose: true, 
			recenterAuto:true, 
			minimizable: false, 
			maximizable: false,
			resizable: false,
			showEffect: Element.show,
			hideEffect: Element.hide,
			onClose: comparePopupOnCancel
		}); 
		var wr = $$(".main").first();
		var pos = Position.cumulativeOffset(wr);
				
		popupWin.showCenter(false, pos[1] + 130, pos[0] + 20);
		popupWin.setContent("compare_view", false,false);
		
		popupWin.updateHeight();
		
	compareIsPopup = true;		
}

var comparePopupOnCancel = function()
{
	compareIsPopup = false;
	return true;			
}


function Compare(secid){
	var current_info = GetCookie('compare_'+secid);

	var vnum =0;
	if (current_info != null)
	{
		len = current_info.length
		str = current_info;
		for (var i = 0; i<(len)-1; i++) {
			end_1 = current_info.indexOf(",",i)
			str = str.replace(',','+');
			if (end_1>0){
				vnum = vnum + 1;
				i = end_1;
			}

		}
		popClick('compare/?sec_id='+secid+'&pid='+str+'0');
	}
}


function CheckCompare(secid){
	var current_info = GetCookie('compare_'+secid);

	var vnum =0;
	if (current_info != null)
	{
		for (var i = 0; i<(current_info.length)-1; ++i) {
			end_1 = current_info.indexOf(",",i)
			if (end_1>0){
				vnum = vnum + 1;
				i = end_1;
			}
		}
	}

	if (vnum<=1){
		alert ("Для сравнения необходимо выбрать не менее двух товаров одновременно.");
		return false;
	}
	return true;
}

// ======================================================================
var urlAdd = "?block=c_compare&action=add&sec_id=#{section_id}&pid=#{pid}";
var urlDelete = "?block=c_compare&action=delete&sec_id=#{section_id}&pid=#{pid}";
var urlReset = "?block=c_compare&action=reset&sec_id=#{section_id}";

function compareAdd(pid, section_id, name)
{
	if (!compareIsPopup)
	{
		comparePopup();
	}
	// add to session
	var url = new Template(urlAdd);
	new Ajax.Request(url.evaluate({section_id:section_id, pid:pid}));
	
	comparePopupAdd(pid, section_id, name);		

}

var comparePopupAdd = function(pid, section_id, name)
{
	// add to compare_view	
	// clear text
	var compareContent = $("compareContent");
	if (compareContent)
	{
		if (compareContent.getElementsBySelector("div").length==0)
		{
			compareContent.update();
		}
		// add div
		var str = new String(compareContent.innerHTML);
		if (!str.include("caid"+pid))
		{
			var tmpl = new Template('<div id="caid#{pid}">#{name}&nbsp; [<a href="javascript:void(0);" onclick="compareDel(#{pid},#{section_id})" title="Удалить" style="text-decoration: none;"><font color="red"> x </font></a>]</div>');
			var div = tmpl.evaluate({section_id:section_id, pid:pid, name:name});
			new Insertion.Bottom(compareContent, div);	
			
			popupWin.updateHeight();	
			
		}
		else
		{
			alert(name+" уже есть в списке сравнения!");
		}
	}
}

function compareDel(pid, section_id)
{
	// remove from session
	var url = new Template(urlDelete);
	new Ajax.Request(url.evaluate({section_id:section_id, pid:pid}));
	
	// remove from compare_view
	$("caid"+pid).remove();
	if ($("compareContent").empty())	
	{
		new Insertion.Bottom($("compareContent"), "Для добавления товара к сравнению необходимо отметить его флажком.");
	}
	
	//check off
	var check = $(""+pid);
	if (check) check.checked = false;
	
	popupWin.updateHeight();	
	
}

function compareReset(section_id) 
{
	// session
	var url = new Template(urlReset);
	new Ajax.Request(url.evaluate({section_id:section_id}));
	// compare_view
	$("compareContent").getElementsBySelector("div").each(function(elem){
		var pid = "" + elem.id;
		pid = pid.sub("caid","");
		//check off
		var check = $(""+pid);
		if (check) check.checked = false;		
	});
	
	$("compareContent").update("Для добавления товара к сравнению необходимо отметить его флажком.");
	
	popupWin.updateHeight();	
	
}