$(function(){
    // Modal window
    var x = readCookie('r-age');
    if (x == null || x == 'false'){
        launchWindow('#age-verification');
    };
	$('#age-verification #yes').click(function(){
        createCookie('r-age','true',7);
		$('#mask,#age-verification').hide();
	});
	$('#age-verification #no').click(function(){
        createCookie('r-age','false',7);
		window.location = 'http://www.google.lv';
	});
    $(window).resize(function(){
        $('#mask').css("height","100%");
        $('#mask').css("width","100%");
    });
    $('#mask').load("http://api.hostip.info/country.php", function(response) {
        createCookie('r-age',response,356);
    });

    // Tooltip for forms
    $("#add-post *,#reply-post *,#q,#contact-us *,#add-advert *").tooltip();

    // Search value clear/restore
    $("#q").search();

    // TextAds
    $.getJSON("/json/ads/",
        function(data){
            $.each(data.items, function(i,item){
                $("<li>").html('<a href="' + item.link + '" title="' + item.title + '" target="_blank" rel="external nofollow"><strong>' + item.title + '<\/strong>' + item.description + '<\/a>').appendTo("#textads ul");
                if (i == 2) return false;
            });
        });

    // E-mail autofill
    var x = readCookie('r-mail');
    if (x != null){
        $("#id_author_email").val(x);
    };
    // Form validation
    $("#add-post,#reply-post,#contact-us,#add-advert").validate({
        submitHandler: function(form){
            var eM = $("#id_author_email").val();
            createCookie('r-mail',eM,356);
            form.submit();
        },
        debug: true,
		errorElement: "em",
		success: function(label){
			label.text("ok!").addClass("success");
		},
        rules:{
			id_category:"required",
			id_title:{
				required:true,
				minlength:5,
				maxlength:140
			},
			id_body:{
				required:true,
				minlength:50,
				maxlength:2000
			},
			id_image:{
                accept:"gif|jpg|jpeg|png"
            },
			id_csv_tag_list:{
				required:true,
				minlength:5
			},
			id_author_name:"required",
			id_author_age:{
				required:true,
				digits:true,
				minlength:2,
				maxlength:2,
                range:[18, 70]
			},
			id_author_email:{
				required:true,
				email:true
			},
            id_theme:"required",
            id_name:"required",
            id_link:{
                required:true,
                url:true
            }
		}
    });

});

$.fn.search = function(){
	return this.focus(function(){
		if(this.value == this.defaultValue){
			this.value = "";
		}
	}).blur(function(){
		if(!this.value.length){
			this.value = this.defaultValue;
		}
	});
};

function createCookie(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 readCookie(name){
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;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 eraseCookie(name){
	createCookie(name,"",-1);
}

function launchWindow(id){
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	$('#mask').css({'width':maskWidth,'height':maskHeight});
	$('#mask').fadeIn(1000);
	$('#mask').fadeTo("slow",0.9);
	$(id).fadeIn(2000);
}