$(document).ready(function(){

	$(function(){
		$("#datepicker").datepicker();
	});
	
	$('.selecta').live('change',function(){
		$('.selectb').remove();
		$('.selectc').remove();
	});

	$('.sender').click(function(){
		alert('fghfgh');
	});

	$('#favorites-remove').live('click',function(){

		var opcje = {domain:'.gieldarolna.ptwp.pl', path: '/', expires: 365};
		var wartosc = $.cookie("__CJ_favorites");

		wartosc = wartosc.replace($(this).val() + ", ",'');
		wartosc = wartosc.replace(', ,','');
		wartosc = wartosc.replace(', ','');
		wartosc = wartosc.replace($(this).val(),'');

		$.cookie('__CJ_favorites', wartosc, opcje);
		write_btn(0, $(this).val());
	});

	$('#favorites-add').live('click',function(){

		var opcje = {domain:'.gieldarolna.ptwp.pl', path: '/', expires: 365};
		var wartosc;

		if($.cookie("__CJ_favorites")){
			wartosc = $.cookie("__CJ_favorites");
			if(clip($.cookie("__CJ_favorites"), $(this).val())){

			}else{
				wartosc = wartosc.replace(/]/g,', ' + $(this).val() + ']');
			}
		}else{
			wartosc = '[' + ($(this).val()) + ']';
		}
		$.cookie('__CJ_favorites', wartosc, opcje);
		write_btn(1, $(this).val());
	});

	$('.selectax').live('change',function(){

		var a = 0;
		var b = 0;
		var c = 0;

		if($('.selecta').length > 0)
			a = $('.selecta').val();

		if($('.selectb').length > 0)
			b = $('.selectb').val();

		if($('.selectc').length > 0)
			c = $('.selectc').val();

		catax(a,b,c);

	});
	
	if($('#favbtn').length > 0){
		var id = $('#favbtn').html();

		if($.cookie("__CJ_favorites") && clip($.cookie("__CJ_favorites"), id)){
			write_btn(1, id);
		}else{
			write_btn(0, id);
		}
	}
	
	$(".askPost").live('submit', function(){
		$.post('/user/mail.html?rand=' + new Date().getTime(), { mail: $('#mail').val(), msg: $('#msg').val(), uid: $('#uid').val()},
			function(data){
				$('.entry').html(data);
		});

		return false;
	});
	
	$('.dynmenu-item-tab').click(function(){
		$($(this).attr('rel')).css('display','block');
		$($('.dynmenu-tabs > .selected').attr('rel')).css('display','none');
		$('.dynmenu-tabs > .selected').removeClass('selected');
		$(this).addClass('selected');
	});
	
});


function write_btn(casebtn, id){
	if (casebtn == 1){
		$('#favbtn').html('<button id="favorites-remove" value="'+ id +'"><span>usuń&nbsp;ze&nbsp;schowka</span></button>');
	}else{
		$('#favbtn').html('<button id="favorites-add" value="'+ id +'"><span>dodaj&nbsp;do&nbsp;schowka</span></button>');
	}
}

function clip(c, k){
	var jest = c.search(k);
	if(jest != -1){
		return true;
	}else{
		return false;
	}
}

function catax(a,b,c){
	$.get('/cat/ax.html?params=' + a + ';' + b + ';' + c, function(data) {
		$('#c-element').html(data);
	});
}
