var vilnius = {
    title : "Vilniaus maratonas",
    wWidth: $(window).width(),
    wHeight: $(window).height(),

    topLine: function(){

        this.wWidth = $(window).width();
        this.wHeight = $(window).height();

        _line = $("#header #top #line");
        if (this.wWidth >= 1558){
            $(_line).width(1558);
            $(_line).css({"left": "-279px"});
            return true;
        }
        else if(this.wWidth <= 1000){
            $(_line).width(1000);
            $(_line).css({"left": 0});
            return true;
        }
        else{
            _left = (this.wWidth - 1000) / -2;
            //console.log(left);

            $(_line).width(this.wWidth);
            $(_line).css({"left":  _left + "px"});
        }
    },

     getTimeLeft: function(dateFuture){

            dateNow = new Date(); //grab current date
            amount = dateFuture.getTime() - dateNow.getTime();	//calc milliseconds between dates
            amount = Math.floor(amount/1000); //kill the "milliseconds" so just secs
            delete dateNow;

            days=0;hours=0;mins=0;secs=0;out="";

            // it's time!
            if(amount <= 0){
                return 0;
            }

            // Calculate date and make needed format..
            days=Math.floor(amount/86400);//days
            amount=amount%86400;
            if (days < 10){days = "0" + days;}

            hours=Math.floor(amount/3600);//hours
            amount=amount%3600;
            if (hours < 10){hours = "0" + hours;}

            mins=Math.floor(amount/60);//minutes
            amount=amount%60;
            if (mins < 10){mins = "0" + mins;}

            secs=Math.floor(amount);//seconds
            if (secs < 10){secs = "0" + secs;}

            out += days +"D ";
            out += hours +":";
            out += mins +":";
            out += secs;

            return out;
    },

    setMarathonTimmer: function(){

        if (marathon == "full"){
            dateFuture = new Date(2012, 8, 9);
        }else{
            dateFuture = new Date(2012, 3, 21);
        }

        timeLeft = vilnius.getTimeLeft(dateFuture);
        $("#marathonCounter").html(timeLeft);

        setInterval( function(){
            timeLeft = vilnius.getTimeLeft(dateFuture);
            $("#marathonCounter").html(timeLeft);

        }, 1000);
    },

    checkUserInput: function(){
        _id = $("input#user_input").val();
        _href = location.protocol + "//" + location.hostname + location.pathname + "?id=" + _id;
        location.href = _href;
    },

    registrationTypesClick: function(obj){

    }




};


var valid_form = true;

function checkForm() {


   valid_form = true;

   $("#page .registration_form input").removeClass("error");
   $("#page .registration_form select").removeClass("error");

   checkElement("#page .registration_form input[name=name]", 4);
   checkElement("#page .registration_form input[name=surname]", 4);
   checkElement("#page .registration_form input[name=email]", 5);
   checkElement("#page .registration_form select[name=sex]", 5);
   checkElement("#page .registration_form select[name=birth_year]", 4);
   checkElement("#page .registration_form select[name=birth_month]", 2);
   checkElement("#page .registration_form select[name=birth_day]", 2);
   checkElement("#page .registration_form input[name=city]", 5);
   checkElement("#page .registration_form select[name=shirts]", 1);

   // valid email
   emailReg =  /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
   _email = $("#page .registration_form input[name=email]").val();
   if(!emailReg.test(_email)) {
       $("#page .registration_form input[name=email]").addClass("error");
       return false;
   }

   // rules checked
   if($("#page .registration_form input[name=rules]").attr("checked") != true){
       $("#page .registration_form input[type=submit]").addClass("error");
       return false;
   }

   if (valid_form == true){
       return true;
   }else{
       return false;
   }
}

function checkElement(selector, lenght_){

    _value = $(selector).attr("value");

    if (_value.length < lenght_){
        $(selector).addClass("error");
        valid_form = false;
    }
}



$(document).ready(function(){

    vilnius.topLine();

    vilnius.setMarathonTimmer();


    $(window).resize(function(){
        vilnius.topLine();
    });


    // hide shirts
    if (marathon == "half_marathon"){
        $("#page .registration_form .wear").hide();
    }

    // Registration Form distance choise
    $("#page .registration_form .types a").bind("click", function(){
        $("#page .registration_form .types a").removeClass("selected");
        $(this).addClass("selected");
        _distance = $(this).attr("title");
        $("#page .registration_form #distance_input").val(_distance);

        if (marathon == "half_marathon"){ return null; }

        if (_distance == 1){
            $("#page .registration_form .wear").show();
        }else{
            $("#page .registration_form .wear").hide();
        }

    });




});


