///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//Global state of the pad

padState = 0;
padTab = 0; //0 - nothing; 1 - lastview; 2 - advertise (spec)
pad_pos = -140;

///////////////////////////////////////////////////////////////////////////////
//Sliding parameters

var on_intervalID = 0;
var off_intervalID = 0;
var in_movement = 0;
var on_delay = 10;
var off_delay = 10;
var step = 5;

///////////////////////////////////////////////////////////////////////////////
//Global pads variables

//Last viewed items pad's parameters
var lv_pars = [];
lv_pars['no_data'] = "<div style='text-align:center; width:100%; padding-top: 55px;'>Нет записей</div>";
lv_pars['div_id'] = "lastview_pad_content";
lv_pars['pad'] = document.getElementById('lastview_pad_wrapper');
lv_pars['scriptFile'] = "/storeScript/getLastViewed.php";

//Advertise pad's parameters
var s_pars = [];
s_pars['no_data'] = "<div style='text-align:center; width:100%; padding-top: 55px;'>Нет записей о спецпредложениях</div>";
s_pars['div_id'] = "lastview_pad_spec";
s_pars['pad'] = document.getElementById('lastview_pad_wrapper');
s_pars['scriptFile'] = "/storeScript/getSpec.php";

num_goods = 4;	//Number of goods to store in cookies

///////////////////////////////////////////////////////////////////////////////
//Functions to work with pads

function setState(state)
{
	padState = state;
}

//Open/close panel button
function open_close()
{
	if (in_movement == 0)
	{
		if(padState == 0)
		{
			switch(padTab)
			{
				case 0:
				{
					load_lastview();
					//$('lastview_pad_horizontal_separator').style.backgroundColor = "#FF9B00";
				} break;
				case 1:
				{
					load_lastview();
				} break;
				case 2:
				{
					load_spec();
					//$('lastview_pad_horizontal_separator').style.backgroundColor = "#FF3600";
				} break;
				case 3:
				{
					load_a();
				}
				default:
				{
					load_lastview();
				}
			}
			setState(1);
		}
		else
		{
			off_intervalID = setInterval("slide_OFF()", off_delay);
			in_movement = 1;
			setState(0);
		}
	}
}


//Open or switch tab to last viewed goods
function open_LastViewPad()
{
	if (in_movement == 0)
	{
		if(padState == 0)
		{
			load_lastview();
			setState(1);
			var inact = document.getElementById("spec_");
			inact.className = "inactive_tab_bookmark";
			inact = document.getElementById("a_");
			if (inact) inact.className = "inactive_tab_bookmark";
			var act = document.getElementById("last_");
			act.className = "active_tab_bookmark";
		}
		else
		{
			if(($('lastview_pad_spec').style.display == "block") || ($('lastview_pad_a').style.display == "block"))
			{
				load_lastview();
				var inact = document.getElementById("spec_");
				inact.className = "inactive_tab_bookmark";
				inact = document.getElementById("a_");
				if (inact) inact.className = "inactive_tab_bookmark";
				var act = document.getElementById("last_");
				act.className = "active_tab_bookmark";
			}
			else
			{
				off_intervalID = setInterval("slide_OFF()", off_delay);
				in_movement = 1;
				setState(0);
			}
		}
	}
}

//Load last view items data
function load_lastview()
{
	$('lastview_pad_content').style.display = "block";
	$('lastview_pad_spec').style.display = "none";
	$('lastview_pad_a').style.display = "none";

	on_intervalID = setInterval("slide_ON()", on_delay);
	in_movement = 1;
	padTab = 1;

	if ($('lastview_pad_content').innerHTML == '')
	{
		new Ajax.Updater(lv_pars['div_id'], lv_pars['scriptFile'], {onComplete: function(){if ($('lastview_pad_content').innerHTML == '')	{$('lastview_pad_content').innerHTML = lv_pars['no_data'];} $('lastview_pad_content').style.background = "none";}});
	}
}

//Open or switch tab to advertise
function open_Spec()
{
	if (in_movement == 0)
	{
		if(padState == 0)
		{
			load_spec();
			var inact = document.getElementById("last_");
			inact.className = "inactive_tab_bookmark";
			inact = document.getElementById("a_");
			if (inact) inact.className = "inactive_tab_bookmark";
			var act = document.getElementById("spec_");
			act.className = "active_tab_bookmark";
			setState(1);
		}
		else
		{
			if (($('lastview_pad_content').style.display == "block") || ($('lastview_pad_a').style.display == "block"))
			{
				load_spec();
				var inact = document.getElementById("last_");
				inact.className = "inactive_tab_bookmark";
				inact = document.getElementById("a_");
				if (inact) inact.className = "inactive_tab_bookmark";
				var act = document.getElementById("spec_");
				act.className = "active_tab_bookmark";
			}
			else
			{
				off_intervalID = setInterval("slide_OFF()", off_delay);
				in_movement = 1;
				setState(0);
			}
		}
	}
}

//Loading advertise data
function load_spec()
{
	$('lastview_pad_content').style.display = "none";
	$('lastview_pad_a').style.display = "none";
	$('lastview_pad_spec').style.display = "block";

	on_intervalID = setInterval("slide_ON()", on_delay);
	in_movement = 1;
	padTab = 2;

	if ($('lastview_pad_spec').innerHTML == '')
	{
		new Ajax.Updater(s_pars['div_id'], s_pars['scriptFile'], {onComplete: function(){if ($('lastview_pad_spec').innerHTML == '')	{$('lastview_pad_spec').innerHTML = s_pars['no_data'];} $('lastview_pad_spec').style.background = "none";}});
	}
}

//Removing specified good from list and cookies
function removeLastViewed(cookie_name)
{
	//Hiding section
	var good_section = document.getElementById(cookie_name);
	good_section.style.display = "none";

	//Hiding right separator
	var good_sep = "";
	good_sep = document.getElementById('sep_' + cookie_name);
	if (good_sep != null)
	{
		good_sep.style.display = "none";
	}

	//If this good is last in the list but not first - then hiding previous separator
	var last_c = -2;	//separator # to hide
	var is_last = true;	//this good is last in list
	var c_pos = 0;	//position of cookie_name
	for(i=1; i<num_goods; i++)
	{
		var buf = "goods_viewed_" + i;
		//Find cookie_name
		if (cookie_name == buf)
		{
			c_pos = i;	//Save position of cookie_name
			//Check if there any goods after this cookie_name
			for(j=i+1; j<num_goods; j++)
			{
				if (getCookie('goods_viewed_'+j) != "")
				{
					is_last = false;
				}
			}

			//If this good is last in the shown list...
			if(is_last == true)
			{
				//... then find number of previous shown good's cookie
				for(k=i-1; k>=0; k--)
				{
					if(getCookie('goods_viewed_'+k) != "")
					{
						last_c = k;
						break;
					}
				}
			}
			break;
		}
	}

	//If we have separator to hide...
	if(last_c >= 0)
	{
		var last_n = "sep_goods_viewed_" + last_c;
		var good_sep_l = "";
		good_sep_l = document.getElementById(last_n);
		if (good_sep_l != null)
		{
			//...then hide it!
			good_sep_l.style.display = "none";
		}
	}


	//Shift down all upper cookies down...
	//Cookie will expire after 1 year
/*
	var expiresDate = new Date();
    expiresDate.setTime(expiresDate.getTime() + 365 * 24 * 60 * 60 * 1000); // срок – 1 год, но его можно изменить
    var expires = expiresDate.toGMTString();
	for(s=c_pos; s<(num_goods-1);s++)
	{
		var idd = s+1;
		setTrueCookie('goods_viewed_'+s, getCookie('goods_viewed_'+idd), expires);

		//document.getElementById('goods_viewed_' + s).id = 'goods_viewed_hidden_' + s;
		//document.getElementById('sep_goods_viewed_' + s).id = 'sep_goods_viewed_hidden_' + s;
		//document.getElementById('goods_viewed_' + idd).id = 'goods_viewed_' + s;
		//document.getElementById('sep_goods_viewed_' + idd).id = 'sep_goods_viewed_' + s;

	}
*/
	//... and delete last one
	//var lastid = num_goods - 1;
	//deleteCookie('goods_viewed_' + lastid);

	deleteCookie(cookie_name);
}

function slide_ON()
{
	var dt = new Date();
	var pad = document.getElementById('lastview_pad');
	if (pad.style.bottom != "0px")
	{
		pad_pos+=step;
		pad.style.bottom = pad_pos + 'px';
	}
	else
	{
		clearInterval(on_intervalID);
		in_movement = 0;
		var oc_btn = document.getElementById('open_close_bookmark');
		oc_btn.style.background = "transparent url('/images/close_pad_bookmark.gif') 0 0 no-repeat";
	}
}

function slide_OFF()
{

	var dt = new Date();
	var pad = document.getElementById('lastview_pad');
	if (pad.style.bottom != "-140px")
	{
		pad_pos-=step;
		pad.style.bottom = pad_pos + 'px';
	}
	else
	{
		clearInterval(off_intervalID);
		in_movement = 0;
		var oc_btn = document.getElementById('open_close_bookmark');
		oc_btn.style.background = "transparent url('/images/open_pad_bookmark.gif') 0 0 no-repeat";
	}
}


///////////////////////////////////////////////////////////////////////////////
//Functions to work with cookies
function deleteCookie(name)
{
	var path = "/";
    if (getCookie(name))
    {
    	document.cookie = name + "=" + ((path) ? "; path=" + path : "") +  "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}


function setTrueCookie(c_name,c_value,date)
{
	var cookie = c_name +"=" + c_value + "; ";
	cookie+="expires="+date;
	cookie +="; Path=/";
	document.cookie = cookie;
}


function setLastViewedCookies(page_id)
{
	//Cookie will expire after 1 year
	var expiresDate = new Date();
    expiresDate.setTime(expiresDate.getTime() + 365 * 24 * 60 * 60 * 1000); // срок – 1 год, но его можно изменить
    var expires = expiresDate.toGMTString();

	var last_empty = -1;	//Last empty slot in cookies from num_goods
	var inlist_position = -1;	//Position of current good's cookie in list of last_viewed cookies
	var holes = [];

	var gotHoles = false;

	//Checking last_viewed list cookies
	for(i=0; i<num_goods; i++)
	{
		holes[i] = false;
		//If cookie of this good is already in list then save it's place
		if ((getCookie('goods_viewed_'+i) == page_id) && (inlist_position < 0))
		{
			inlist_position = i;
		}

		//If cookie in last_viewed list is empty then...
		if (getCookie('goods_viewed_'+i) == "")
		{
			//... we should check all next cookies to verify is it end of list or just one empty item
			var next = false;
			for(n=(i+1); n<num_goods; n++)
			{
				if(getCookie('goods_viewed_'+n) != "")
				{
					next=true;
				}
			}

			//if it is end of list then save length of list (or hole position) and finish checking
			if(next == false)
			{
				last_empty = i;
				break;
			}
			else
			{
				holes[i] = true;
				gotHoles = true;
			}
		}
	}

	//If this cookie is NOT IN list
	if(inlist_position < 0)
	{
		//if list is not full then just add cookie
		if(last_empty >= 0)
		{
			setTrueCookie('goods_viewed_'+last_empty, page_id, expires);
		}
		else	//if list is maybe full...
		{
			//...then check if there any holes...
			if (gotHoles == false)
			{
				//...then move all cookies down on 1 position...
				for(j = 0; j < num_goods-1; j++)
				{
					var id = j+1;
					setTrueCookie('goods_viewed_'+j, getCookie('goods_viewed_'+id), expires);
				}

				//...and add current good's cookie to the end of list
				var lastid = num_goods-1;
				setTrueCookie('goods_viewed_'+lastid, page_id, expires);
			}
			else	// we got a hole!...
			{
				//... we need to shift all upper cookies to 1 position down...
				var holes_num = 0;
				for (h=0;h<num_goods;h++)
				{
					if(holes[h] == true)
					{
						for(k=h; k<(num_goods-1);k++)
						{
							var id = k+1;
							setTrueCookie('goods_viewed_'+k, getCookie('goods_viewed_'+id), expires);
						}
						var last_c = num_goods-1;
						deleteCookie('goods_viewed_'+last_c);
						holes_num = holes_num + 1;
						break;
					}
				}

				//...and finally add current good's cookie to the end of list
				var lastid = num_goods - holes_num;
				setTrueCookie('goods_viewed_'+lastid, page_id, expires);
			}
		}
	}
}



///////////////////////////////////////////////////////////////////////////////