﻿function check_integrity(parent_node) {
        if (parent_node.attr('data-actual_items') == parent_node.attr('data-default_items')) {
            parent_node.removeClass('partial').removeClass('unchecked');
        } else if(parseInt(parent_node.attr('data-actual_items')) > 0) {
            parent_node.addClass('partial').removeClass('unchecked');
        } else {
            parent_node.removeClass('partial').addClass('unchecked');
        }
 }

 function power_user_account() {
     if ($("#user_account").find('.username').size() > 0) {
         $("#user_account").hide().before('<a id="user_account_trigger" href="#" title="contul tau">contul tau</a>');
     } else {
         $("#user_account").hide().before('<a id="user_account_trigger" href="#" title="autentifica-te">autentifica-te</a>');
     }
     $("#user_account").prepend('<a id="user_account_close" href="#" title="inchide">inchide</a>');
     $("#user_account_trigger").click(function() {
         $(this).hide();
         $("#user_account").show();
         return false;
     });
     $("#user_account_close").click(function() {
         $("#user_account_trigger").show();
         $("#user_account").hide();
         return false;
     });
 }

 function power_serch_box() {
     $("#search input[type=text]").addClass("no_value").val("cauta acte, articole, forum").focus(function() {
         if ($(this).val() == "cauta acte, articole, forum") {
             $(this).val("").removeClass("no_value");
         }
     }).blur(function() {
     if ($(this).val() == "") {
         $(this).val("cauta acte, articole, forum").addClass("no_value");
     }
 });
     $("form").submit(function() {
         //return false;
     })
     $(document).keypress(function(key) {
         if (key.keyCode == "13") {
             if (!$("#search input[type=text]").hasClass("no_value")) {
                 $("#search input[type=submit]").click();
             } else {
                 return false;
             }
         }
     });
 }

function newsletter_options() {
    $('#newsletter_register .your_choice h3').each(function(i) {
        var child_inputs = $(this).parent().find('input');
        child_inputs.attr('data-belongs_to', 'selection' + i);
        var no_checkboxes = child_inputs.size();
        $(this).attr('data-default_items', no_checkboxes).attr('data-actual_items', no_checkboxes);
        $(this).addClass('selection' + i);
    });

    $('#newsletter_register .your_choice input[checked=checked]').attr('data-checked', 1);

    $('#newsletter_register .your_choice input').click(function() {
        parent_node = $("." + $(this).attr('data-belongs_to'));
        if ($(this).attr('data-checked') == '1') {
            $(this).attr('data-checked', 0);
            parent_node.attr('data-actual_items', parseInt(parent_node.attr('data-actual_items')) - 1);
            check_integrity(parent_node);
        } else {
        $(this).attr('data-checked', 1);
            parent_node.attr('data-actual_items', parseInt(parent_node.attr('data-actual_items')) + 1);
            check_integrity(parent_node);
        }
    });
    $('#newsletter_register .your_choice label').click(function() {
        parent_node = $("." + $(this).next().attr('data-belongs_to'));
        if ($(this).attr('data-checked') == '1') {
            $(this).attr('data-checked', 0);
            parent_node.attr('data-actual_items', parseInt(parent_node.attr('data-actual_items')) - 1);
            check_integrity(parent_node);
        } else {
            $(this).attr('data-checked', 1);
            parent_node.attr('data-actual_items', parseInt(parent_node.attr('data-actual_items')) + 1);
            check_integrity(parent_node);
        }
    });

    $('#newsletter_register .your_choice h3').click(function() {
        if (parseInt($(this).attr('data-actual_items')) > 0) {
            $(this).parent().find("input").removeAttr('checked').attr('data-checked', 0);
            $(this).attr('data-actual_items', 0);
            check_integrity($(this));

        } else {
        $(this).parent().find("input").attr('checked', 'checked').attr('data-checked', 1);
            $(this).attr('data-actual_items', $(this).attr('data-default_items'));
            check_integrity($(this));
        }
    });

    
}


function resources_toggler() {
    $('.other_resources ol ol').hide();
    $('.other_resources h2').append('<a href="#" class="toggler">toate</a>')
    $('.other_resources h2').toggle(function() {
        $(this).next().show();
        $(this).children('.toggler').text('ascunde');
    }, function() {
        $(this).next().hide();
        $(this).children('.toggler').text('toate');
    });
}


$(document).ready(function() {
    resources_toggler();
    newsletter_options();
    power_serch_box();
    //power_user_account();
});

