// init
var tracker = Array();
tracker['photos_more'] = 0;

var	basket_text = 'товаров: <span class="pr"><span>{count}</span></span><br />на сумму: {summ} руб.';

// refactor it with jquery
// -------------------------------------------------------------------------------
function opensub(sub_obj) {
	sub_obj.style.display = 'block';
	sub_obj.style.visibility = 'visible';
}

function closesub(sub_obj) {
	sub_obj.style.visibility = 'hidden';
	sub_obj.style.display = 'none';
}

function makeRequest(uri,write_obj){
	var httpRequest;
	if (window.XMLHttpRequest) { 
		httpRequest = new XMLHttpRequest(); 
		if (httpRequest.overrideMimeType) {httpRequest.overrideMimeType('text/xml');}
	} else if (window.ActiveXObject) {
		try { httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch(e){ 
			try { httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch(e){}
		}
	}
	if (!httpRequest){
		alert('Не могу создать экземпляр XMLHTTP');
		return false;
	}

	httpRequest.onreadystatechange = function() {alertContents(uri,httpRequest,write_obj)};
	httpRequest.open('GET', uri, true);
	httpRequest.send('');
}

function writetext(tmp,write_obj) {
	write_obj.innerHTML = tmp;
}

/* menu func */
function closeallsub(sub_id){
	// определение области закрытия
	prefix = sub_id.substring(0,1);
	for (key in tracker) {
		cprefix = key.substring(0,1);
		if (tracker[key]==1 && (prefix=='s' || (prefix=='f' && prefix==cprefix))) {
			sub_obj = document.getElementById(key);
			closesub(sub_obj);
			tracker[key]=0;
		} 
	}	
}

function flipsub(sub_id) {
	sub_obj = document.getElementById(sub_id);
	if (tracker[sub_id]==1) {
		//closesub(sub_obj);
		//tracker[sub_id]=0;
	} else {
		closeallsub(sub_id);
		sub_obj = document.getElementById(sub_id);
		opensub(sub_obj);
		tracker[sub_id]=1;
		
		//var content_obj = document.getElementById('content');
		//makeRequest('/ajax.php?goods='+sub_id,content_obj);

	}	
}

function opensubmenu(sub_id) {
	var div_id = 's'+sub_id;
	if (tracker[div_id]!=1) {
		closeallsub(div_id);
		var sub_obj = document.getElementById(div_id);
		var content_obj = document.getElementById('content');
		
		makeRequest('/ajax.php?submenu='+sub_id,sub_obj);
		makeRequest('/ajax.php?goods='+sub_id,content_obj);

		//show div
		opensub(sub_obj);
		tracker[div_id]=1;
	}	
}

function opencategory(sub_id) {
	var div_id = 'f'+sub_id;
	if (tracker[div_id]!=1) {
		closeallsub(div_id);
		var sub_obj = document.getElementById(div_id);
		var content_obj = document.getElementById('content');
		var key = sub_id.substring(sub_id.indexOf('_')+1);
		makeRequest('/ajax/menu_flt/type'+key,sub_obj);
		makeRequest('/ajax/goods/type'+key,content_obj);
		
		//opensub(sub_obj);
		tracker[div_id]=1;
	}	
}

/* basket */
function buy(g_id,wht) {
	var img_obj = document.getElementById('i'+g_id);
	var unbuy = img_obj.src.indexOf('unbuy');
	var fs = g_id.substring(0,1);
	if (isNaN(fs)) g_id = g_id.substring(1);
	var act = 'add';
	if (checksubstring(img_obj.src,'unbuy')) act = 'remove';
	makeRequest('/ajax/basket/'+act+'/'+g_id,null);
}

function buy_over(img_obj,state) {
	if (state==0) img_obj.src='/i/unbuy.gif'; else img_obj.src='/i/buy.gif'
}

function buy_out(img_obj,state) {
	if (state==1) img_obj.src='/i/unbuy.gif'; else img_obj.src='/i/buy.gif'
}

function changeqty(g_id,i_obj) {
	var count = i_obj.value;
	if (count=='') count = 0;
	makeRequest('/ajax/basket/recount/'+g_id+'/'+count,null);
}


/* tth */
function photos_more() {
	sub_id = 'photos_more';
	sub_obj = document.getElementById(sub_id);
	link_obj = document.getElementById('photos_more_link');

	if (tracker[sub_id]==1) {
		sub_obj.style.visibility = 'hidden';
		tracker[sub_id]=0;
		writetext('Показать дополнительные изображения',link_obj);
	} else {
		sub_obj.style.visibility = 'visible';
		tracker[sub_id]=1;
		writetext('Скрыть дополнительные изображения',link_obj);
	}
}

function supertype_over(key) {
	tmp = document.getElementById(key+'_i');
	tmp.src='/i/stt/'+key+'_o.gif';
}

function supertype_out(key) {
	tmp = document.getElementById(key+'_i');
	if (tmp.className == 'sel') tmp.src='/i/stt/'+key+'_s.gif'; else tmp.src='/i/stt/'+key+'.gif';
}

function supertype_sover(key) {
	tmp = document.getElementById(key+'_l');
	tmp.className='lunderline';
}

function supertype_sout(key) {
	tmp = document.getElementById(key+'_l');
	tmp.className='lnone';
}


function alertContents(uri,httpRequest,write_obj) {
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			
			if (checksubstring(uri,'/basket/')) {
				// basket action - parse response 1|count|summ
				var tmp = httpRequest.responseText; 
				var res = shiftpipe(tmp,'|',1);
				var count = shiftpipe(tmp,'|',2);
				var summ = shiftpipe(tmp,'|',3);
				if (count=='') count = 0;
				if (summ=='' || summ==',00' || summ=='0,00') summ = 0;
				var tmp = uri;
				var act = shiftpipe(tmp,'/',4);
				var g_id = shiftpipe(tmp,'/',5);
				
				write_obj = document.getElementById('basketsumm');
				if (summ!=0) {
					var pz = summ.indexOf(',');
					if (pz) summ = '<span class="pr"><span>'+summ.substring(0,pz)+'</span><sup>'+summ.substring(pz+1)+'</sup></span>';
					writetext('товаров: <span class="pr"><span>'+count+'</span></span><br />'+'на сумму: '+summ+' руб.',write_obj);
				} else writetext('пусто',write_obj);

				if (act=='add' || act=='remove') {
					var bp = Array('a','n','s','h','');
					for (k=0;k<bp.length;k++) {
						var img_obj = document.getElementById('i'+bp[k]+g_id);
						var a_obj = document.getElementById('a'+bp[k]+g_id);
						if (img_obj) {
							if (act=='add') img_obj.src='/i/unbuy.gif'; else img_obj.src='/i/buy.gif'
						}		
						if (a_obj) {
							if (act=='add') a_obj.className='unbuy'; else a_obj.className='buy'
						}		
					}				
				}
				if (act=='recount') {
					write_obj = document.getElementById('summ');
					writetext(summ+' руб.',write_obj);
				}
			} else writetext(httpRequest.responseText,write_obj);
			
		} else { alert('Проблема с запросом.'); }
	}
}

// -------------------------------------------------------------------------------

// ------------------------------------------ ?
function supertype_click(link) {
	document.location=link;	
}

function reqGoods (str) {
  nwindow=window.open('/forms/reqgoods','rg','toobar=no,location=no,directories=no,status=yes,scrollbars=no,resizable=no,copyhistory=no,width=683,height=480');
  nwindow.focus();
}

function reqLP (g_id) {
  nwindow=window.open('/forms/lowerprice/'+g_id,'lp','toobar=no,location=no,directories=no,status=yes,scrollbars=no,resizable=no,copyhistory=no,width=683,height=420');
  nwindow.focus();
}
// ------------------------------------------ ?



// internal
function checksubstring(str,needle) { if (str.indexOf(needle)!=-1) return true; else return false;}

function template_this(str) {
	// template '{var1} {var2} переменные обязательно должны существовать
	var matches = str.match(/\{(\w*)\}/g);
	for(var i=0;i<matches.length;i++) {
		w = matches[i];
		v = w.substring(1,w.length-1);
		str = str.replace(w,eval(v));
	}
	return str;
}

function shiftpipe(str,splitter,count) {
	var ppos,result;
	for(j=0;j<count;j++) {
		ppos = str.indexOf(splitter);
		if (ppos==-1) ppos = str.length;
		result = str.substring(0,ppos);
		str = str.substring(ppos+1);
	}
	return result;
}

function zoom(photoname) {
  if (photoname=='/' || photoname=='http://fcshop.ru/' || photoname=='http://nboom.ru/' || photoname=='#') return false;
  nwindow=window.open(photoname,'image','toobar=no,location=no,directories=no,status=yes,scrollbars=no,resizable=no,copyhistory=no,width=820,height=620');
  nwindow.focus();
}

function showwait() {$('#waittable').toggle();}

