// JavaScript Document
jQuery.noConflict();

jQuery(document).ready(function() {

    //
    // preload css images
    jQuery.preloadCssImages();

    //
    // init main navigation with submenus
    jQuery('#main_nav li').each(function() {
        var li = jQuery(this);
        var a = li.find('a:first');
        var aId = a.attr('id');

        var speed = 300;

        // first li
        if (li.index() == 0) {
            li.addClass('first');
        };

        // if link has subitems
        if (jQuery('#sub_' + aId).length) {

            li.addClass('has_sub'); // add arrow

            var subMenu = jQuery('#sub_' + aId);
            var sHeight = subMenu.height();

            a.bind('click', function() {
                if (subMenu.is(':hidden')) { //show sub if it hidden
                    closeSubs(); // close all opened subs
                    li.addClass('opened');
                    subMenu.css({ height: '0px', display: 'block' }).animate({ height: sHeight }, speed);
                } else { //hide if visible
                    subMenu.animate({ height: '0px' }, speed / 2, function() {
                        subMenu.css({ display: 'none' });
                        li.removeClass('opened');
                    });
                }
                return false;
            });

            a.bind({
                
            });

        var closeBtn = subMenu.find('div.close_btn');
        closeBtn.bind('click', function() {
            subMenu.animate({ height: '0px' }, speed / 2, function() {
                subMenu.css({ display: 'none' });
                li.removeClass('opened');
            });
            return false;
        });

    } else { // if no subs
        a.bind('click', function() {
            closeSubs(); // close all opened subs
        });
    }

});

//
//search form
jQuery('#search_form input').focus(function() {
    var v = jQuery(this).val();

    if (v == 'SEARCH SITE') {
        jQuery(this).val('');
    }
}).blur(function() {
    var v = jQuery(this).val();

    if (v == '') {
        jQuery(this).val('SEARCH SITE');
    }
}).keypress(function(e) {
    code = e.keyCode || e.which;
    if (code == 13) {
        var v = jQuery(this).val();
        if (v != '' && v != 'SEARCH SITE')
            jQuery('#search_form button').trigger('click');
        e.preventDefault();
    }
});

//
// footer menu
jQuery('#footer ul li:last').css('border', 'none');

});


//
// functions

function closeSubs(){
	jQuery('div.submenu:visible').css({height: '0px', display: 'none'});
	jQuery('#main_nav li.opened').removeClass('opened');
}

function CheckBoxRequired_ClientValidate(sender, e) {
    var str = jQuery(sender).attr('validatorid');
    e.IsValid = jQuery("span[validatorid='" + str + "'] input:checkbox").is(':checked');
}

function countNewLines(instr) {
    var _newLineCounts = 0;
    var _regPat = new RegExp("\\n|\\r\\n", "g");
    while (_newLines = _regPat.exec(instr)) {
        _newLineCounts += 1;
    }
    return _newLineCounts;
}

function checklimits () {
    var max_chars = 250;
    var text_area_val = jQuery(this).val();
    var _newLineCounts = countNewLines(text_area_val);
    var _lenNew = (text_area_val.length + _newLineCounts);
    var count = max_chars - _lenNew;

    if (_lenNew <= max_chars) {
        jQuery(this).prev().children("span.text-counter").html("&nbsp;&nbsp;(" + count + " character(s) left)");
    } else {
        var valueToTrim = max_chars;
        var trimmed_val = text_area_val.substr(0, valueToTrim);
        while (trimmed_val.length + countNewLines(trimmed_val) > max_chars) {
            valueToTrim--;
            trimmed_val = text_area_val.substr(0, valueToTrim);
        } 
        jQuery(this).val(trimmed_val);
        jQuery(this).prev().children("span.text-counter").html("&nbsp;&nbsp;(0 character(s) left)");
    }
    return false;
}


function checklimitsBig() {
    var max_chars = 4000;
    var text_area_val = jQuery(this).val();
    var _newLineCounts = countNewLines(text_area_val);
    var _lenNew = (text_area_val.length + _newLineCounts);
    var count = max_chars - _lenNew;

    if (_lenNew <= max_chars) {
        jQuery(this).prev().children("span.text-counter").html("&nbsp;&nbsp;(" + count + " character(s) left)");
    } else {
        var valueToTrim = max_chars;
        var trimmed_val = text_area_val.substr(0, valueToTrim);
        while (trimmed_val.length + countNewLines(trimmed_val) > max_chars) {
            valueToTrim--;
            trimmed_val = text_area_val.substr(0, valueToTrim);
        }
        jQuery(this).val(trimmed_val);
        jQuery(this).prev().children("span.text-counter").html("&nbsp;&nbsp;(0 character(s) left)");
    }
    return false;
}


