﻿var dropDownWidth = 223;

$(document).ready(function () {

    //GLOBAL--------------------------------------------------------------
    //CENTER DROPDOWN TO RESPECTIVE NAVIGATION
    $('#mainNav > ul').children('li').each(function (indx) {
        newLeft = ($(this).width() - dropDownWidth) / 2;
        $(this).children('ul').css({ left: newLeft + 'px' });

        //REMOVE LAST BACKGROUND DIVIDER
        $(this).children('ul').children('li:last').css({ background: 'none' });

    });


    //CTA BUG FIX
    $('.bugs ul li').each(function () {
        imgWidth = $(this).children('img').outerWidth();
        $(this).children('p').css({ paddingLeft: imgWidth + 'px' });
        //$(this).children('.btnRibbon').css({ paddingLeft: imgWidth + 'px' });
    });


    //FORCE H2 TAGS TO INDENT WHEN NEEDED
    $('.cRight .indentH2').each(function () {
        imgArr = $(this).prevAll('img');
        addMargin = $(imgArr[0]).outerWidth();
        $(this).css({ marginLeft: addMargin + 'px' });
    });


    //PRELOAD IMAGES
    $.fn.preload = function () {
        this.each(function () {
            $('<img/>')[0].src = this;
        });
    }

    $(['../App_Themes/Piacci/global/images/dropdown_bg.png', './App_Themes/Piacci/global/images/dropdown_topShadow.png', '../App_Themes/Piacci/global/images/dropdown_selected.png']).preload();

    //STORE SAMPLING------------------------------------------------------
    //SHOW/HIDE SAMPLING INFO
    $('.btn_storeSamples').click(function () {
        $(this).next('.sampleItems').stop(true, true).toggle(400);

        return false;
    });

    //set flags (reused by code behind)
    errorSetFlags();

    $('.formFields .btnHolder .btnRibbon a').click(function () {
        $('.formFields input').each(function () {
            errorControl($(this), "hide");
        });

        $('.formFields select').each(function () {
            errorControl($(this), "hide");
        });

    });


  
});


function errorControl(elem, action) {
    if ($(elem).parent('span')) {
        if (action == "show") {
            $(elem).parent('span').children('span.errorNote').each(function (indx) {
                theMsg = $(this).children('.errorMsg');
                if ($(theMsg).css('display') != 'block')
                    $(theMsg).stop(true, true).show("slide", { direction: "right" }, 400);
            });
        } else {
            $(elem).parent('span').children('span').each(function (indx) {
                theMsg = $(this).children('.errorMsg');
                if ($(theMsg).css('display') == 'block')
                    $(this).children('.errorMsg').stop(true, true).hide("slide", { direction: "right" }, 400);
            });
        }
    }
}

function errorSetFlags() {
    //CONTACT ------------------------------------
    $('.formFields input[type=text], .formFields select').bind({
        mouseenter: function () {
            errorControl($(this), "show");
        },
        mouseleave: function () {
            //errorControl($(this), "hide");
        }
    });
}
