var name = '';
var last_illegal_name = '';
var last_bad_link = '';
var name_error = 'Proszę podać poprawną nazwę postaci';
var char_noexist = 'Ta postać nie istnieje! Sprawdź poprawność podanej nazwy postaci!';
var name_illegalchar = 'W nazwie postaci znajdują się niedozwolone znaki!';
var offer_added = 'Oferta została pomyślnie dodana do bazy!';
var hash_nomatch = 'Wygerenowany kod nie został poprawnie umieszczony w opisie postaci!';
var hash_match = 'Wygenerowany kod jest zgodny z kodem zamieszczonym w opisie postaci!';
var correct = 'OK';
var link_nomatch = 'Proszę podać poprawny link do profilu na forum';
var profile_noexist = 'Podano link do nieistniejącego profilu!';
var offer_exist = 'Taka oferta już istnieje!';
var give_link = 'W celu szybkiego kontaktu podaj link do swojego profilu na forum.toproste.pl!';
var add = 'Dodaj ofertę';
var sho = 'Pokaż oferty';
var svr_path = '/scripts/srv.php';
var anext = 'Dodaj kolejną ofertę';
var tagain = 'Spróbuj jeszcze raz';


var Cookies = {
	init: function () {
		var allCookies = document.cookie.split('; ');
		for (var i=0;i<allCookies.length;i++) {
			var cookiePair = allCookies[i].split('=');
			this[cookiePair[0]] = cookiePair[1];
		}
	},
	create: function (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=/";
		this[name] = value;
	},
	erase: function (name) {
		this.create(name,'',-1);
		this[name] = undefined;
	}
};
Cookies.init();


function Display(div){
	$('#'+div).toggle('slow');	
	return false;	
	}
	
	$(function (){

		$("#facebook").hover(function(){
				$("#facebook").stop(true, false).animate({right:"0"},"medium");
			},function(){
				$("#facebook").stop(true, false).animate({right:"-205"},"medium");
			},500);
		
		return false;
	});

function Sort(what, how)
{
$('#offer_list').load('/scripts/show.php?action=sort&w='+what+'&h='+how);
return false;
}
/*	
	$(function() {		

		$("#offers-table").tablesorter({sortList:[[0,0],[2,1]], widgets: ['zebra']});
			   });


function rewritefor(id){
var tbody_content = '';
  $('#tbody-offers tr').each(function(){  
							  
		if($(this).attr('id') != 'offer-id-'+id)
		{
		
		if($(this).attr('id') != 'info_box_'+id)
		{
		tbody_content = tbody_content+'<tr id="'+ $(this).attr('id') +'" style="'+$(this).attr('style')+'" class="'+$(this).attr('class')+'">'+$(this).html()+'</tr>';	
		}
		
		}else{
			
		tbody_content = tbody_content+'<tr id="'+ $(this).attr('id') +'" style="'+$(this).attr('style')+'" class="'+$(this).attr('class')+'">'+$(this).html()+'</tr>';		
		tbody_content = tbody_content+'<tr id="'+ $('#info_box_'+id).attr('id') +'" style="'+$('#info_box_'+id).attr('style')+'" class="'+$('#info_box_'+id).attr('class')+'">'+$('#info_box_'+id).html()+'</tr>';	
			
		}
							  });
  $('#tbody-offers').html(tbody_content);
}*/


window.onunload = function() {
Cookies.create('last_left', parseInt($('#lefting').css('left')), 1);						  }
						  
					  
$(document).ready(function() {
doDMarquee();
$('#banner').css('visibility', 'visible');	
$('#plmain').tablesorter({ 
        headers: { 
            1: { 
                sorter: false 
            }, 
			2: { 
                sorter: false 
            }, 

            3: { 
                sorter: false 
            } 
        } 
    }); 

if(offer_id > 0)
{
	  $('#offer_post').hide();
	  $('#offer_sdbar').hide();
	  $('#offer').show();	
	  
	  $('#offer-id-'+offer_id).css('border-color','red');
	  $('#info_box_'+offer_id).css('border-color','red');
	  
	  $('#offer-id-'+offer_id).css('border-width', '2.0mm');
	  $('#info_box_'+offer_id).css('border-left-width', '2.0mm');

	  $('#info_box_'+offer_id).css('border-right-width', '2.0mm');

	  $('#info_box_'+offer_id).css('border-bottom-width', '2.0mm');
	  $.scrollTo($('#offer-id-'+offer_id), 800);	
	  	  ShowInfo(offer_id);

}
						   
						   
						   });


function ShowInfo(id){
if($('#info_box_'+id).css("display") == "none")
{
	$('#info_button_'+id).attr('src','/style/images/info_opened.png');
	$('#info_button_'+id).attr('title','Ukryj dodatkowe informacje');
	$('#offer-id-'+id).css('border-bottom-width','0mm');
	$('#info_box_'+id).fadeIn(1000);
	
}else
{
	$('#info_box_'+id).fadeOut(1000,function(){
if(offer_id == id)
{											 
$('#offer-id-'+id).css('border-bottom-width','2.0mm'); 
}
else
{
	$('#offer-id-'+id).css('border-bottom-width','0.5mm'); 
}
$('#info_button_'+id).attr('src','/style/images/info.png'); 
$('#info_button_'+id).attr('title','Pokaż dodatkowe informacje');									 
											 });	

}

}


function nameact(){
	 
var name = $('#name').val();

	if(name.length < 3 || name == last_illegal_name)
	{
		$('#CharNameInfo').html('<span style="color: red;">'+name_error+'<span>');	
		$('#name').css('border-color','red');
		$('#CharNameInfo').css('width','400 px');
		$('#butone').attr("disabled", true);
		
	}else if (!(name.match(/^[a-zA-Z ]+$/))) 
	{
		$('#CharNameInfo').html('<span style="color: red;">'+name_illegalchar+'<span>');	
		$('#name').css('border-color','red');
		$('#CharNameInfo').css('width','400 px');
		$('#butone').attr("disabled", true);  
	}
	else
	{
		$('#CharNameInfo').html('<span style="color: green;">'+correct+'<span>');	
		$('#name').css('border-color','green');
		$('#CharNameInfo').css('width','150 px');
		if($('#name').attr("readonly") == false)
		{
		$('#butone').attr("disabled", false);
		}
	}
								  
}


function contactdataact(){ 

var link = $('#contact_data').val();

		if (!(link.match(/^(http:\/\/)?(www.)?forum\.toproste\.pl\/member\.php\?u\=[0-9]+$/)) || link == last_bad_link) 
	  {
		  $('#ContactInfo').html('<span style="color: red;">'+link_nomatch+'<span>');	
		  $('#contact_data').css('border-color','red');
		  $('#ContactInfo').css('width','400 px');
		  $('#butthree').attr("disabled", true);
	  }
	  else
	  {
		  $('#ContactInfo').html('<span style="color: green;">'+correct+'<span>');	
		  $('#contact_data').css('border-color','green');
		  $('#ContactInfo').css('width','150 px');
		  $('#butthree').attr("disabled", false);
	  }
								  
							


}

$('#show_tg').live('click', function() { 
var cont = $('#show_tg').text();


						if(cont == add){
						clear();
						$('#offer_list').hide();
						$('#refresh').hide();
						$('#offer_form').show();
						$('#reset').show();
						$('#show_tg').text(sho);
						}else if (cont == sho)
						{
						$('#offer_list').show();
						$('#process_info').text('');
						$('#refresh').show();
						$('#reset').hide();
						$('#offer_form').hide();
						$('#show_tg').text(add);
						}
							


								  
								  });

$('#show_df').live('click', function() { 

						$('#offer_post').show();
						$('#offer_sdbar').show();
						$('#offer').hide();

 });

$('#sh_ofc').live('click', function() { 

						$('#offer_post').hide();
						$('#offer_sdbar').hide();
						$('#offer').show();
 });

$('#refresh').live('click', function() { 
						$('#offer_list').load('/scripts/show.php');


 });

function clear(){
$('#offer_form').show();
$('#process_info').text('');
$('#name').val('');
$('#name').css('border-color','black');
$('#CharNameInfo').text('');
$('#butone').attr("disabled", true);
$('#Hash').text('');
$('#HashResult').text('');
$('#Contact').hide();
$('#contact_data').val('');
$('#contact_data').css('border-color','black');
$('#ContactResult').text('');
$('#ContactInfo').text('');
$('#ActionStatus').text('');
$('#Send').hide();
$('input[name=hide]').attr('checked', false);
$('#butthree').attr("disabled", true);
$('#contact_data').attr("readonly", false);
$('#name').attr("readonly", false);
$('#Text').hide();
$('#info').val('');

	
	
return false;	
	
	
}

$('#reset').live('click', function() { 
$('#reset').text('Wyczyść formularz');
clear();

 });



$('#name').live('keyup', function() {nameact(); return false;});
$('#contact_data').live('keyup', function() {contactdataact(); return false;});
$('#name').live('click', function() {nameact(); return false;});
$('#contact_data').live('click', function() {contactdataact(); return false;});
$('#name').live('mouseover', function() {nameact(); return false;});
$('#contact_data').live('mouseover', function() {contactdataact(); return false;});



$('#butone').live('click', function() { 

var name = $('#name').val();
var server = $('input:radio[name=server]:checked').val();
$('#Hash').html('<img src="/style/images/loading.gif" />');


$.ajax({
  type: 'POST',
  url: svr_path,
  data: { 'name' : name, 'server' : server, 'action' : 'get_hash'},
  dataType: 'json',
  success: function(reply) {
	  
	if(reply.process == 'done')
	{
		$('#Hash').html('<span class="offer_hash_info"><br />W celu weryfikacji wklej ponizszy kod do opisu postaci, którą chcesz sprzedac i kliknij w przycisk \'OK\'.</span><br /><br /><span class="offer_hash_info_rd"><input name="name" id="name" type="text" class="in3read" value="[confirm]'+reply.hash+'[/confirm]" readonly="readonly"/><br /><br /><span class="offer_hash_info_but"><input type="button" class="in3" id="buttwo" value="OK" /></span>');	
		$('#butone').attr("disabled", true);
		$('#name').attr("readonly", true);
	}else if(reply.process == 'noexist')
	{
		last_illegal_name = name;
		$('#Hash').html('<span style="color: red;">'+char_noexist+'<span>');
		$('#CharNameInfo').html('<span style="color: red;">'+name_error+'<span>');	
		$('#name').css('border-color','red');
		$('#CharNameInfo').css('width','400 px');
		$('#butone').attr("disabled", true);
	}else if(reply.process == 'offerexist')
	{
		$('#offer_form').hide();
		$('#process_info').html('<span class="process_info" style="color: red; display: block; margin: auto;">'+offer_exist+'</span><br /><br />');
		$('#show_tg').text(sho);
		$('#reset').text(tagain);

	}
	
	
	  }
});

});



$('#buttwo').live('click', function() { 

var name = $('#name').val();
$('#HashResult').html('<img src="/style/images/loading.gif" />');
var server = $('input:radio[name=server]:checked').val();

$.ajax({
  type: 'POST',
  url: svr_path,
  data: { 'name' : name, 'server' : server, 'action' : 'get_char'},
  dataType: 'json',
  success: function(reply) {
	  
	if(reply.process == 'done')
	{
		$('#buttwo').attr("disabled", true);
		$('#HashResult').html('<span style="color: green;">'+hash_match+'</span><br /><br /><br /><span style="color: red;">'+give_link+'</span>');
		$('#Text').show();
		$('#Contact').show();
		$('#Send').show();
	}else if(reply.process == 'nomatch')
	{
		$('#HashResult').html('<span style="color: red;">'+hash_nomatch+'<span>');
	}
	
	
	  }
});

});



$('#butthree').live('click', function() { 

var name = $('#name').val();
var link = $('#contact_data').val();
var hide = $('input:checkbox:checked').val();
var info = $('#info').val();
var server = $('input:radio[name=server]:checked').val();

$('#butthree').attr("disabled", true);
$('#contact_data').attr("readonly", true);
$('#ActionStatus').html('<img src="/style/images/loading.gif" />');

$.ajax({
  type: 'POST',
  url: svr_path,
  data: { 'name' : name, 'forum_profile' : link, 'hide' : hide, 'server' : server, 'info' : info, 'action' : 'add_offer'},
  dataType: 'json',
  success: function(reply) {
	  
	if(reply.process == 'done')
	{

		$('#offer_form').hide();
		$('#process_info').html('<span class="process_info" style="color: green; display: block; margin:auto;">'+offer_added+'</span><br /><br />');
		$('#show_tg').text(sho);
		$('#reset').text(anext);
		$('#offer_list').load('show.php');


	}else if(reply.process == 'profilenoexist')
	{
		$('#ContactResult').html('<span style="color: red;">'+profile_noexist+'<span>');
		$('#butthree').attr("disabled", false);
		$('#contact_data').attr("readonly", false);
		$('#ActionStatus').text('');
		$('#ContactInfo').html('<span style="color: red;">'+link_nomatch+'<span>');	
		$('#contact_data').css('border-color','red');
		$('#butthree').attr("disabled", true);
		last_bad_link = $('#contact_data').val();
		


	}else if(reply.process == 'offerexist')
	{
		$('#offer_form').hide();
		$('#process_info').html('<span class="process_info" style="color: red; display: block; margin: auto;">'+offer_exist+'</span><br /><br />');
		$('#show_tg').text(sho);
		$('#reset').text(tagain);

	}
	
	
	  }
});

});



//$('#name').live('focusout', function() { if($(this).val().replace(/\s+/,'').length == 0)$(this).val(name);	});
