function init()
{
	var n = 0;
	n = parseInt(read_cookie('tab'))

	if ( !isNaN(n) && n >= 0 || n < 3 )
	{
		active_tab(n);
	}
	else
	{
		active_tab(0);
	}

	$('hdr_stats').observe( 'click', function (e){ active_tab(0); } );
	$('hdr_settings').observe( 'click', function (e){ active_tab(1); } );
	$('hdr_offers').observe( 'click', function (e){ active_tab(2); } );
	$('offer_form').style.display='none';
	$('offerSubmit').observe('click', validate_offer);

}

function validate_offer ()
{
	if ( $F('offer_title').length >= 5 )
	{
		if ( $('offer_summary').value.length >= 20 )
		{
			if ( $F('offer_description').length >= 20 )
			{
				if ( $('offer_category').value > 0 )
				{
					if ( $('offer_scategory').value > 0 )
					{
						$('offer_frm').submit();
					}
					else
					{
						alert('Подкатегорята е задължителна');
						$('offer_scategory').focus();
					}
				}
				else
				{
					alert('Категорята е задължителна');
					$('offer_category').focus();
				}
			}
			else
			{
				$('offer_description').focus();
				alert('Това поле не може да бъде с по - малко от 20 символа');
			}
		}
		else
		{
			$('offer_summary').focus();
			alert('Това поле не може да бъде с по - малко от 20 символа ' + document.getElementById('offer_summary').value );
		}
	}
	else
	{
		alert('Това поле неможе да бъде празно или име под 5 символа ');
		$('offer_title').focus();
		return false;
	}
}

function active_tab ( n )
{
	var arr = new Array(new Array('hdr_stats', 'tab_stats'),  new Array('hdr_settings', 'tab_settings'), new Array('hdr_offers', 'tab_offers') );
	create_cookie('tab', n, 2);

	for ( i = 0; i < 3; i++ )
	{
		if ( n == i )
		{
			$(arr[i][0]).addClassName('active');
			$(arr[i][1]).addClassName('active_tab');
			continue;
		}
		else
		{
			$(arr[i][0]).removeClassName('active');
			$(arr[i][1]).removeClassName('active_tab');
		}
	}
}

function show_offer_form ()
{
	Effect.Fade('offers_list', {duration:0.2});
	Effect.Appear('offer_form');
	reset_offer_form();
}

function hide_offer_form ()
{
	Effect.Fade('offer_form', {duration:0.2});
	Effect.Appear('offers_list');
	reset_offer_form ();
}


function reset_offer_form ()
{
	$('oid').value = 0;
	var data = new Array('offer_title', 'offer_summary', 'offer_description', 'offer_price');
	for ( var i = 0, j = data.length; i < j; i++ )
	{
		$(data[i]).value = '';
	}
	$('file_offer_logo').innerHTML = '';
	$('offerSubmit').value='Създай';
	$('offer_category').value = 0;
	$('more_images').style.display='none';
	$R(1,5).each(function(i){ $('img'+i).innerHTML=''; });
}

function create_cookie(name,value,days) 
{
	if (days) 
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function read_cookie(name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for( var i=0, j = ca.length ; i < j ; i++ ) 
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring( 1, c.length );
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}

	return null;
}

function erase_cookie(name) 
{
	createCookie(name,"",-1);
}


function load_offer_subcategories (n)
{
	var url = '/clients/subcats/catid/'+parseInt(n);

	new Ajax.Request(url, 
		{
			method: 'post',
			parameters: 'type=offer',
			onSuccess: function(transport) 
			{
				$('offer_subcats').innerHTML = transport.responseText;
  			}
	});

}

function edit_offer(n)
{
	n = parseInt(n);

	if ( !isNaN(n) && n > 0 )
	{
		show_offer_form ();
		$('offerSubmit').value='Запази';
		var url = '/clients/loadoffer/'
		new Ajax.Request( url, 
			{ 
				method:'post',
				parameters:'offer_id='+n,
				onSuccess: function( resp )
				{

					var data = eval('(' + resp.responseText + ')');

					$('offer_title').value=data.name;
					$('offer_summary').value = data.summary;
					$('offer_description').value = data.description;
					var url = '/clients/subcats/catid/'+parseInt(data.offer_category);

					new Ajax.Request(url, 
					{
						method: 'post',
						parameters: 'type=offer',
						onSuccess: function(transport) 
						{
							$('offer_subcats').innerHTML = transport.responseText;
							$('offer_category').value = data.offer_category;
							$('offer_scategory').value = data.offer_scategory;
							$('offer_price').value = data.price;
							if ( data.active == 1 )
							{
								$('active').checked = 'checked';
							}

							if ( data.logo )
							{
								$('file_offer_logo').innerHTML = data.logo ? '<img src="/uploads/offers/'+data.id+'/'+data.id+'.'+data.logo+'"  alt="'+data.name+'" />' : '&nbsp;';
							}
						}
					});

					$('oid').value = data.id;
					$('more_images').style.display='block';

	for ( var i in data.pictures )
	{
		$('img'+parseInt(parseInt(i)+1) ).innerHTML = '<a href="/uploads/offers/'+data.id+'/'+data.pictures[i]+'.jpg" rel="lightbox[slide]"><img src="/uploads/offers/'+data.id+'/thumb_'+data.pictures[i]+'.jpg"  alt="Image" /></a>';
	}
initLightbox();

				}
			} 
		);

	}
}


function delete_offer(n)
{
	if ( confirm('Изтриване на обявата ?') )
	{
		window.location = '/clients/delete_offer/'+n;
	}
}


function make_vipoffer( n )
{
	n = parseInt(n);
	if ( !isNaN(n) )
	{
		new Ajax.Request('/clients/getvipcode',
		{
			method:'post',
			parameters:'id='+n,
		 	onSuccess: function(msg)
			{ 
				if(msg.responseText != 'error') 
				{
					$('vip_block'+n).innerHTML = msg.responseText;
				}
			}  
		});
	}
}


function hide_vip_form(id)
{
	$('vip_block'+id).innerHTML = '&nbsp;';
}

function check_vip_code(id)
{
	id = parseInt(id);
	if ( !isNaN(id) && id > 0 )
	{
		new Ajax.Request('/clients/checkvipcode',
		{
			method:'post',
			parameters:'id='+id+'&code='+$('response_code'+id).value,
			onSuccess: function(m){ switch( m.responseText ){ case -1: case '-1':alert('Невалиден код!'); break; case 1:case '1': alert('Честито, Вашата обява вече е VIP! '); window.location=window.location;  break;default: alert('Възникна грешка при създаването на VIP обява!'+"\n"+'Свържете се с нас!');  } }
		});
	}
}

