/*
**********************************************************************************
*   Common Utils Methods
**********************************************************************************
*/

// init ajax requests
var requests = [];

function Validate() {
    // abort ajax requests
    if (this.requests) {
        for (var i = 0; i < requests.length; i++)
            requests[i].abort();

    }
};


function Select(obj) {
    n = $('.nav ul li a');
    sn = $('.special-nav ul li a');

    if (obj && obj != "") {
        $(obj).attr('class', $(obj).attr('class') + ' current');
    } 

    if (sn.length != 0) {
        sn.click(function(event) {
            opt = $(event.target);
            $('.nav ul li a').add('.special-nav ul li a').each(function(index, item) {
                $(item).attr('class', $(item).attr('class').replace('current', ''));
            });
            
            if (opt.attr('class').indexOf(' current') == -1)
                opt.attr('class', opt.attr('class') + ' current');
        });
    }

    if (n.length != 0) {
        n.click(function(event) {
            opt = $(event.target);

            $('.nav ul li a').add('.special-nav ul li a').each(function(index, item) {
                $(item).attr('class', $(item).attr('class').replace('current', ''));
            });

            if (opt.attr('class').indexOf(' current') == -1)
                opt.attr('class', opt.attr('class') + ' current');
        });
    }
}

function SearchHandler(url) {
    go = $('input', 'div.search .right');
    go.bind('click', function() {
        s = $('input', 'div.search .left')
        sval = s.val();
        if (!sval | sval == s.attr('placeholder')) return false;
        sval = sval.replace(/^\s+/, "");
        if (sval != "") {
            url = url + "?searchterm=" + sval
            location.replace(url);
        }
        return false;
    });

    $('input', 'div.search .left').bind('keyup', function(event) {
        s = $('input', 'div.search .left')
        sval = s.val();
        if (!sval | sval == s.attr('placeholder')) return false;
        sval = sval.replace(/^\s+/, "");
        if (event.which == 13) {
            if (sval != "") go.trigger('click');
        }
    });
}

function removeHtmlTag(html) {
    return html.replace(new RegExp('<\/?[^>]+(>|$)', 'gi'), '');
}

function setItems(item, value) {
    $.each(item, function(index, current) {
        $(current).text = value[index];
    });
}

function getOrdinal(day) {
    var ordinal = "";
    if (day >= 11 && day <= 13) {
        ordinal = "th";
    }
    switch (day % 10) {
        case 1: ordinal = "st";
        case 2: ordinal = "nd";
        case 3: ordinal = "rd";
        default: ordinal = "th";
    }

    return day + ordinal;
}

/*
**********************************************************************************
*   jscript for main navigation system
*   Master Page
**********************************************************************************
*/
$(function() {
    /*Set border bottom for all li less the last li of each menu*/
    $('.sub-commitment li')
        .not(':last').css('border-bottom', '1px solid #ded0bf');
    $('.sub-nutritionist li')
        .not(':last').css('border-bottom', '1px solid #ded0bf');

    $('.sub-weight li a')
        .not(':last').css('border-bottom', '1px solid #efefef');
    $('.sub-fiber li a')
        .not(':last').css('border-bottom', '1px solid #efefef');
    $('.sub-heart li a')
        .not(':last').css('border-bottom', '1px solid #efefef');
    $('.sub-veg li a')
        .not(':last').css('border-bottom', '1px solid #efefef');

    /*Keep active de parent item relative to current submenu*/
    /*left menu*/
    $('.nav ul li ul').mouseover(function() {
        var strIdMenu = '.' + $(this).attr('class') + '-a';
        $(strIdMenu).addClass('activelink');
    });
    $('.nav ul li ul').mouseout(function() {
        //var strIdMenu = '.' + $(this).attr('class') + '-a';
        $('.activelink').removeClass('activelink');
    });

    /*right menu*/
    $('.special-nav ul li ul').mouseover(function() {
        var strIdMenu = '.' + $(this).attr('rel') + '-a';
        $(strIdMenu).addClass('active-section');
    });
    $('.special-nav').mouseout(function() {
        $('.active-section').removeClass('active-section');
    });

    /*Show and hide de rounded corners on parent menu and submenu items*/
    $('.shell .special-nav ul li.first').hover(
        function() {
            $('.border-left').removeClass('b-left').addClass('b-left-on');
        },
        function() {
            $('.border-left').removeClass('b-left-on').addClass('b-left');
        }
    );
    $('.shell .special-nav ul li.last').hover(
        function() {
            $('.border-right').removeClass('b-right').addClass('b-right-on');
        },
        function() {
            $('.border-right').removeClass('b-right-on').addClass('b-right');
        }
    );


    /*Rounded last li nav menu*/
    $('.sub-commitment li:last, .sub-nutritionist li:last').addClass('rounded-bottom');

    /*Empty links  alert*/
    $('a[href*="#"]').parent().not('.navi').not('.list_carousel2, .list_carousel').click(function() {
        //alert('We are still working here');
        return false;
    });

}); //END of jscript for main navigation system



/*Custom radios*/
$(function() { });
//$('.radio1').wrap('<span class="radio-custom rHtml on"/>');
//$('.radio2').wrap('<span class="radio-custom rText off"/>');

/*$('.radio-custom').click(function() {
var currOn = $('.on');
var currOff = $('.off');
currOn.removeClass('on').addClass('off');
currOff.removeClass('off').addClass('on');
var currC = $('input:checked');
var currNC = $('input').not(':checked');
currC.removeAttr('checked');
//currNC.attr('checked', 'checked');
})*/

$(function calloutHover() {
    /*This function changes the font color on mouse over, on home callouts title, like a link*/
    $('.home-callouts ul li a h4').hover(
            function() { $(this).css('color','#BF1328') },
            function() { $(this).css('color', '#BCA05C') }
    );
});


$(function() { /*Rounded corners overlays on IE - It works together Scripts/PIE.JS*/
    if (window.PIE) {
        $('.jqmContent').each(function() {
            PIE.attach(this);
        });
    }
});

function hotKeys() {
    var ua = navigator.userAgent.toLowerCase();
    var str = '';
    var isWebkit = (ua.indexOf('webkit') != -1);
    var isMac = (ua.indexOf('mac') != -1);

    if (ua.indexOf('konqueror') != -1) {
        str = 'CTRL + B'; // Konqueror
    } else if (window.home || isWebkit || isIEmac || isMac) {
        str = (isMac ? 'Command/Cmd' : 'CTRL') + ' + D';
    }
    return ((str) ? 'Press ' + str + ' to bookmark this page.' : str);
}

function addBookmark(title) {
    var isMSIE=/*@cc_on!@*/false;
    var isIEmac = false; /*@cc_on@if(@_jscript&&!(@_win32||@_win16)&& (@_jscript_version<5.5)) isIEmac=true; @end@*/        
    if (window.sidebar) // firefox
        window.sidebar.addPanel(title, location.href.toString(), "");
    else if (window.external && isMSIE && !isIEmac)// ie
        window.external.AddFavorite(location.href.toString(), title);
    else
        alert(hotKeys());
}


/*****Printing functions******/
$(function() {
    $('.print.hep-landing').click(function() {
        $('.callouts, #footer,.product-profiler, .secondarylinks,.share, #header, .breadcrumb-inner, .tools').hide();
        $('body').css('background', 'none');
        $('a.table-plan').removeAttr('href');
        window.print();
        setTimeout('window.location.reload();', 500);
        return false;
    });
});

$(function() {
    $('.print.art').click(function() {
        $('#1, .breadcrumb-inner, .list_carousel, .suggest-topic-holder, .around-web-container, .callouts')
            .remove();
        $('.navigation, .sign-up-link, .upcoming-events-wrapper, .facebook-like, .tweet-like, .google-plus, .twitter-share-button')
            .remove();
        $('.eventCar, .share, .tools, #header, .picture-expert a')
            .remove();
        $('body').css('background', 'none').css('background-color', '#fff');
        $('#area').width(477);
        window.print();
        setTimeout('window.location.reload();', 500);
        return false;
    });
});

/*****Printing functions END******/

$(function() { /*Set special style on validation feedback news. reg.*/$('.validation-summary-errors ul li').addClass('alert').css('font-weight', 'normal'); });

$(function() { $('#html-lb, #text-lb').click(function() { return false; }); });


