//prikid.com.ua

var FULL_BASE_URL = '';
var IMAGES_URL = FULL_BASE_URL + 'images/';

var back_array = new Array();
var historyback = true;
var addtohistory = true;

/*
30.01.2007 
Javascript navigation 
save for search engine
*/

function navigation(id,all,by,now)
{
var obj		= document.getElementById(id);

this.all	= all;
this.now	= now;
this.by		= by;

if(now < by/2){
	startPos = 1;
}else{
	if(now >= all - by/2){
		startPos=Math.floor(all - by + 1);
	}else{
		startPos=Math.floor(now - by/2 + 0.5);
	}
}


this.list			= document.createElement('div');
this.list.className = "list";
this.list.innerHTML = obj.innerHTML;
obj.innerHTML		= '';

this.line			= document.createElement('div');
this.line.className = "line";
this.toch			= document.createElement('div');
this.toch.className = "toch";

var toch_in			= document.createElement('div');
toch_in.className	= "toch_in";

this.toch.appendChild(toch_in);

this.line.appendChild(this.toch);
obj.appendChild(this.list);
obj.appendChild(this.line);

var _self			= this;
var moveoffset		= -1;
var save_handlers	= new Array();
this.toch.onmousedown = function(){
	save_handlers['onmouseup'] = document.onmouseup;
	save_handlers['onmousemove'] = document.onmousemove;
	document.body.onmouseup = this.onmouseup;
	document.body.onmousemove = this.onmousemove;
	moveoffset = xMousePos-this.offsetLeft;
};

this.toch.onmouseup = function(){
	moveoffset = -1;
	document.onmouseup = save_handlers['onmouseup'];
	document.onmousemove = save_handlers['onmousemove'];
};

this.toch.onmousemove=function(){
	if(moveoffset!=-1){
		_self.toch.style.left=xMousePos-moveoffset;
		if(xMousePos-moveoffset<0)
		_self.toch.style.left=0;
		if(xMousePos-moveoffset+_self.toch.offsetWidth>_self.line.offsetWidth)
		_self.toch.style.left=_self.line.offsetWidth-_self.toch.offsetWidth;
		_self.renderList(_self.getPos());
	}
};

_self.renderList(startPos);

width= this.list.firstChild.offsetWidth - (this.list.firstChild.firstChild.firstChild.firstChild.offsetWidth*2);
this.line.style.width=width
this.toch.style.width=this.by*width/all;
this.setPos(startPos);

}

navigation.prototype.setPos=function (now){
	this.toch.style.left=(this.line.offsetWidth-this.toch.offsetWidth)*(now-1)/(this.all-this.by);
	this.pos=now;
};



navigation.prototype.getPos=function (){
	return Math.floor((this.toch.offsetLeft*(this.all-this.by)/(this.line.offsetWidth-this.toch.offsetWidth)))+1;
}

navigation.prototype.renderList=function (startPos){
	var rez='<table id="nav_table"><tr>';
	var next_url = '';
	if(startPos > 1){
		rez += "<td>...</td>";
	}else{
		rez += "<td> </td>";
	}
	for(i = startPos; i < startPos + this.by; i++)
	if(i == this.now){
		rez+="<td><span>" + i + "</span></td>";
	}else{
		/***/
		next_url = FULL_BASE_URL + "newsn" + (i - 1)*5 + ".html";
		/***/
		rez+="<td><a href=\""+ next_url +"\">" + i + "</a></td>";
	}
	if(i <= this.all){
		rez += "<td>...</td>";
	}else{
		rez += "<td> </td>";
	}
	rez += "</tr></table>";
	this.list.innerHTML = rez;
};


//---------------------------------------
var IE = document.all?true:false

if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;

var xMousePos = 0
var yMousePos = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE && !window.opera) {
//    xMousePos = event.offsetX + document.body.scrollLeft
//    yMousePos = event.offsetY + document.body.scrollTop
    xMousePos = event.clientX + document.body.scrollLeft
    yMousePos = event.clientY + document.body.scrollTop

  } else {
    xMousePos = e.pageX
    yMousePos = e.pageY
  }

  if (xMousePos < 0){xMousePos = 0}
  if (yMousePos < 0){yMousePos = 0}

  return true
}
  /*Check if year is selected: if ok submit button disabled off*/
   function check_submit(id){
	 document.getElementById(id).disabled = document.getElementById('years_list').value != '--' ? false : true;
	 generateDaysList();
  }
  function generateDaysList() {
	var year = document.getElementById('years_list').value;
	var month = document.getElementById('monthes_list').value;
	if (month != '--' && year != '--'){
		var req = new JsHttpRequest();
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				document.getElementById('d_list').innerHTML = req.responseJS.days_list;
			}
		}
		req.open(null, FULL_BASE_URL + 'gendayslist_backend.php', true);
		req.send( { y: year, m:month } );
	}
}
 /*Checking submit form: if ok submit button disabled off*/
function chack_FB_form(){
	var ok_ = false;
	if (document.getElementById('FB_comments_id').value != '' && document.getElementById('FB_captcha_id').value != ''){	
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (filter.test(document.getElementById('FB_email_id').value)){
			ok_ = true;
		}
	}
	document.getElementById('FB_submit_id').disabled = ok_ ? false : true;
	document.getElementById('FB_submit_id').style.color = 'green';
}
/*
function viewImage (img){
	var obj		= document.getElementById('divforviewimage');
	img = img == 'N/A' ? IMAGES_URL + 'big_blank.jpg' : img;
	obj.innerHTML = '<a href="javascript:hideImage()">Закрыть</a><br /><img src="'+ img +'" />';
	obj.style.display = 'block';
	obj.style.top = getScrollXY()[1];
	obj.style.left = getScrollXY()[0];
}

function hideImage (){
	var obj		= document.getElementById('divforviewimage');
	obj.innerHTML = '';
	obj.style.display = 'none';
}
*/
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

$(document).ready(function(){
	$('#check_login').click(function(){
		$('#login_div').load('user_backend.php', {'operation': 'check_login', 'login': $('#login').attr('value')});
		return false;
	});
	
	$('#subscribe_to_msg_delivery').click(function(){
		$.ajax({
			type: "POST",
			url: "subscr_bknd.php",
			data: "cmd=usersubscribe&sub_subscriber_email=" + $('#subscriber_email').val(),
			success: function(msg){
				alert(msg);
				$('#subscriber_email').val('');
			}
		});
		return false;
	});
	
	addToBasket = function(id){
		$('#basket_div').load('user_backend.php', {'operation': 'add_good', 'id': id});
		return false;
	}
	
	removeFromBasket = function(id){
		$('#basket_div').load('user_backend.php', {'operation': 'remove_good', 'id': id}, function(){
			$('#orders_big_div').load('user_backend.php', {'operation': 'update_orders'});
		});
		return false;
	}
	
	removeOrder = function(id){
		$('#orders_div').load('user_backend.php', {'operation': 'remove_order', 'id': id});
		$('#numorders_div').load('user_backend.php', {'operation': 'update_numorders'});
		return false;
	}
	
	showCommentForm = function(){
		if ($('#comment_form_div').css('display') == 'block') {
			$('#comment_form_div').css('display', 'none');
			$('#add_comment').attr('innerHTML', 'Добавить комментарий');
		}
		else if ($('#comment_form_div').css('display') == 'none') {
			$('#comment_form_div').css('display', 'block');
			$('#add_comment').attr('innerHTML', 'Скрыть');
		}
		return false;
	}
	
	sort = function(field, order){
		$('#goods_div').load('user_backend.php', {'operation': 'sort', 'field': field});
	}
	
	addFilter = function(type, parameter, value){
		$('#goods_div').load('user_backend.php', {'operation': 'add_filter', 'type': type, 'parameter': parameter, 'value': value});
		return false;
	}
	
	removeFilter = function(type, parameter, value){
		$('#goods_div').load('user_backend.php', {'operation': 'remove_filter', 'type': type, 'parameter': parameter, 'value': value});
		return false;
	}
	
	removeAll = function(type){
		$('#goods_div').load('user_backend.php', {'operation': 'remove_all', 'type': type});
		return false;
	}
});
