function getCookie(c_name) {

	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1) c_end=document.cookie.length;
			return "true";
		}
	}
	return "false";
}
	
var JSON = JSON || {};
// implement JSON.stringify serialization
JSON.stringify = JSON.stringify || function (obj) {
	var t = typeof (obj);
	if (t != "object" || obj === null) {
		// simple data type
		if (t == "string") obj = '"'+obj+'"';
		return String(obj);
	} else {
		// recurse array or object
		var n, v, json = [], arr = (obj && obj.constructor == Array);
		for (n in obj) {
			v = obj[n]; t = typeof(v);
			if (t == "string") v = '"'+v+'"';
			else if (t == "object" && v !== null) v = JSON.stringify(v);
			json.push((arr ? "" : '"' + n + '":') + String(v));
		}
		return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
	}
};

jQuery.fn.equalizeCols = function(){ 
  var height = 0; 
  return this.css("height","auto").each(function(){ 
    height = Math.max( height, jQuery(this).outerHeight() ); 
  }).css("height", height); 

};
				


$(function() {

	$("#social").prepend('<a title="Facebook" class="fb" name="fb_share" type="icon" share_url="' + window.location.href + '" id="fb_share"></a>');
	$.getScript("http://static.ak.fbcdn.net/connect.php/js/FB.Share");
	
	Cufon.replace("div.col-head div div, div.col-item h2 a, div.col-big h2, div.col-big h3, #message h2, div.media-item h2");

	function popup(div) {
		if ($.blockUI) {
			$.blockUI.defaults.applyPlatformOpacityRules = false;
			$.blockUI({
				message: div,
				css: { 
						width:						'auto', 
						top:							'50%', 
						left:							'50%', 
						marginTop:				- div.outerHeight() / 2,
						marginLeft:				- div.outerWidth() / 2,
						textAlign:				'left', 
						color:						'#000', 
						border:						'0', 
						backgroundColor:	'transparent',
						cursor:						'default'
				}
			});
		} else {
			window.console.log("blockUI: " + $.blockUI);
		}
		
		div.find("a.close").click(function(){
			$.unblockUI({
				onUnblock: function(){
					div.remove();
				} 
			});
			return false;
		});
	}
	
	function initForm(){
		$("div.col-item form").submit(function(){
			var f = $(this);
			if (!$(":checked", f).length) {
				alert("Zaznacz co najmniej jedną opcję.");
				return false;
			}
			
			$.ajax({
				url: f.attr("action"),
				data: f.serialize(),
				dataType: "json",
				type: "post",
				success: function(re){
					if (re.result === true) {
						console.log(re);
					}
				}
			});
			return false;
		});
		
		
		$("div.col-item p.submit a").click(function(){
			$.ajax({
				url: "/ankieta-wyniki.html",
				data: "id=" + $("#ask-form input[name=type]").val(),
				dataType: "json",
				type: "post",
				success: function(re){
					alert("mariusz pudzianowski");
				}
			});
			return false;
		});
	}
	
	initForm();
	
	$("td.ask a.more, div.ask a.more, div.ask2 a.ask2-more").click(function(){
		$("body").append('<div id="ask-form"></div>');
		$("#ask-form").load("/zadajPytanie.html", { type: "post" }, function(){
			popup($(this));
			$("#ask-form form").submit(function(){
				var f = $(this);
				$.ajax({
					url: f.attr("action"),
					dataType: "json",
					data: "url=" + window.location.pathname + window.location.search + window.location.hash + "&" + f.serialize(),
					type: "post",
					beforeSend: function(){
						$("span.error", f).remove();
					},
					success: function(re){
						if (re.result === false) {
							$.each(re.errors, function(id, val){
								$("*[name=" + id + "]", f).parent("p").append('<span class="error">' + val + '</span>');
							});
						} else {
							$("#ask-form").height(260);
							$("form, p", "#ask-form").wrapAll("<div></div>");
							$("#ask-form > div").replaceWith("<p id='ajaxed' style='display: none;'>" + re.message + "</p>");
							$("#ajaxed").fadeIn(function(){
								try { firstTracker._trackPageview("/zadaj_pytanie"); } catch(e) {}
							});
						}
					}
				});
				return false;
			});
		});
		return false;
	});
	
	
	var nextPage = 2;
	$("#footer a.foot-right").click(function(){
		var a = $(this);
		$.ajax({
			url: a.attr("href"),
			data: "page=" + nextPage,
			dataType: "json",
			type: "post",
			success: function(re){
				if (re.next_page === false) {
					a.remove();
				}
				if (re.data) {
					$.each(re.data, function(id, val){
						$("div.col:eq(" + (parseInt(id, 10) - 1) + ")").append(val);
					});
					initForm();
					nextPage++;
					Cufon.replace("div.col-head div div, div.col-item h2 a, div.col-big h2, div.col-big h3, #message h2");
				}
			}
		});
		return false;
	});
	
	
	$("#dla_mediow div.small div.msg").equalizeCols();
	
	$("#dla_mediow div.news-text div.slide-content p:last").css("padding-bottom", "0");
	
	$("#dla_mediow div.news-item a.slider").click(function(){
		var a = $(this);
		var div = a.parents("div.news-item:first");
		a.toggleClass("active");
		a.html($(this).hasClass("active") ? "Ukryj" : "Pokaż").prev("div.slide-content").slideToggle("fast");
		div.siblings().find("div.slide-content").slideUp("fast");
		div.siblings().find("a.slider.active").removeClass("active").html("Pokaż");
		return false;
	});
	
	$("#media-carousel").jcarousel({
		vertical: true,
		scroll: 1,
		visible: 2,
		buttonPrevHTML: "<div>ZOBACZ POPRZEDNIE</div>",
		buttonNextHTML: "<div>ZOBACZ NASTĘPNE</div>"
	});

});
