$.ajaxSetup({
cache: false
});
var frontend = {
    labels_ids: 0,
    refresh: function() {
        $("a").each(function() {
           if ($(this).attr('href') == document.location.href) {
               $(this).addClass('a_selected');
               if ($(this).parent().is('li')) {
                   $(this).parent().addClass('li_selected');
               }
           }
        });
        $("label").each(function() {
            if (!$(this).attr('for')) {
                id = 'label_'+frontend.labels_ids++;

                $(this).parent().children('input[type="checkbox"]:first').each(function(){
                    if ($(this).attr('id')) {
                        id = $(this).attr('id');
                    } else {
                        $(this).attr('id',id);
                    }
                });
                $(this).attr('for',id);
            }
        });
        $("input").each(function() {
            $(this)
                   .addClass('input')
                   .addClass('input_'+$(this).attr('type'))
                   .addClass('input_name_'+$(this).attr('name'));
        });
        $("textarea").each(function() {
           $(this)
                  .addClass('input')
                  .addClass('input_textarea')
                  .addClass('input_name_'+$(this).attr('name'));
        });
        $("select").each(function() {
           $(this)
                  .addClass('input')
                  .addClass('input_select')
                  .addClass('input_name_'+$(this).attr('name'));
        });
        $(".click_first_anchor").click(function() {
           var href = $(this).find('a:first').attr('href');
           if (href) {
               document.location.href = href;
           }
           return false
        });
        $(".anchor").click(function() {
           var href = $(this).attr('href');
           if (href) {
               document.location.href = href;
           }
           return false
        });
        $(".click_last_anchor").click(function() {
           var href = $(this).find('a:last').attr('href');
           if (href) {
               document.location.href = href;
           }
           return false
        });

        $(".convert_to_img").each(function() {
           var src = $(this).find('img:first').attr('src');
           if (src != undefined) {
           if (src.split('../').length > 1) {

           if (src) {
               src  = src.replace('../','');
               src  = src.replace('../','');
               src  = src.replace('../','');
               src  = src.replace('../','');
           }

              $(this).replaceWith('<img src="'+BASE_URL+'/'+src+'"/>');
           } else {
               $(this).replaceWith('<img src="'+$(this).find('img:first').attr('src')+'"/>');
           }
           } else {
               $(this).detach();
           }

        });
        $(".no_imgs").each(function() {
           $(this).find('img').detach();
        });

        /*
         * @todo: radio
         */
    },
    awake: function () {
        this.refresh();

        $("a.ajax_alert").live('click',function() {
           $.get($(this).attr('href'),function(data){
               alert(data);
           });
           return false
   });
   $("form.ajax_alert").live('submit',function(){
       $.post($(this).attr('action'),$(this).serialize(),function(data){
          alert(data);
       });
       return false
   });


   $("a.ajax_colorbox,a.ajax_lightbox,a.fancybox").live('click',function() {
       $.colorbox({href: $(this).attr('href')});
       return false
   });
   $("form.ajax_colorbox,form.ajax_lightbox,form.ajax_fancybox").live('submit',function() {
       $.colorbox({href: $(this).attr('href')});
       return false
   });
    }
}

$(document).ready(function(){
    frontend.awake();
});


