﻿

jQuery(function () {
    SetClickableBlocks();

    $("div.search-container input").keypress(function (e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $('#btnSearch').focus().click();
            return false;
        } else {
            return true;
        }
    });


    $("div.news-letter-container input").keypress(function (e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $('a.save-newsletter-email').focus().click();
            return false;
        } else {
            return true;
        }
    });




    $("table.form-table input[type=text], table.form-table textarea , table.form-table select").focus(function () {
        $(this).addClass("activeField").removeClass("idle");
        $(this).prevAll("div.Support").show();
    }).blur(function () {
        $(this).removeClass("activeField").addClass("idle");
        $(this).prevAll("div.Support").hide();
    });

    $('p img').each(function (index) {
        if ($(this).css("float") == "left") {
            $(this).addClass('float-left').addClass("picture");
        }
        else if ($(this).css("float") == "right") {
            $(this).addClass('float-right').addClass("picture");
        }
        else if ($(this).css("margin-left") == "auto") {
            $(this).addClass("picture");
        }
    });

    $('ul.category-list li').slice(0, 3).addClass('first-row');
    $('ul.article-list li:nth(0)').addClass('first');
    $('ul.blog-list li:nth(0)').addClass('first');

    $('table.cart tr:nth(0) th:last').addClass('last');
    $('div.footer-block table tr:nth(0) td:last').addClass('last');
    $('div.footer-block table tr:nth(1) td:last').addClass('last');


    $('div.blog-reaction:nth(0)').addClass('blog-reaction-first');
    $('div.blog-reaction:last').addClass('blog-reaction-last');



    SetEqualHeight();


    SetFlowerPosition();
    $(window).resize(function () {
        
        SetFlowerPosition();

    });

});

function SetFlowerPosition() {
    var minW = 1300;
    var windowW = $(window).width();


  //  $('div#flower-container-left').html("<h1>" + "windowW " + windowW + "</h1>");


    if (windowW > minW) {
        $('div#flower-container-left').css('left', '0px');

        $('div#flower-container-right').css('right', '0px');
        $('div#flower-container-right').css('left', "");
    }
    else {
        var corr = (windowW - minW)  ;

        $('div#flower-container-left').css('left', "" + (corr /2) + "px");

        var l = 1030 + corr / 2;
        if (l < 900) {
            l = 900;
        }

        $('div#flower-container-right').css('left', l + "px");
        $('div#flower-container-right').css('right', '');

        //$('div#flower-container-left').html("<h1>" + "CORR " + corr + "</h1>");
       // $('div#flower-container-left').html("<h1>" + "CORR=" + corr + " l=" + l + "</h1>");
    }
}


function SetClickableBlocks() {
    $(".clickable").click(function () {
        // find a hyperlink
        location = $(this).find('a').attr('href');
    });
}

function SetEqualHeight() {
    
    var maxHeight = 100;


    $("div.equal-height").each(function (index) {
        if ($(this).height() > maxHeight) {
            maxHeight = $(this).height();
        }
    });

    $("div.equal-height").height(maxHeight);
        
        // find a hyperlink
        //location = $(this).find('a').attr('href');
    //});
}





function WB_InitFormValidation() {

    var imgOk = "<img src='/pcd/img/icons/ok.png' alt='ok' />";
//    imgOk = "&nbsp; OK!";
    imgOk = "&nbsp;";

    var validator = $("#Form1").validate({
        errorPlacement: function (error, element) {
            if (error.html().length > 0) {
                 element.parents("tr").find("td.feedback").html(error.html()).addClass('error');
            }
            else {
                element.parents("tr").find("td.feedback").html(imgOk).removeClass('error'); ;
            }
            WB_IndicateFormErrors();
        },
        success: function (label) {
            //  label.html(imgOk);
            //  WB_IndicateFormErrors();
        }
    });




    /* var validator = $("#Form1").validate({
    errorPlacement: function (error, element) {
    if (error.html().length > 0) {
    element.parent("div").find("span.form-feedback").html(" * " + error.html());
    }
    else {
    element.parent("div").find("span.form-feedback").html(imgOk);
    }
    WB_IndicateFormErrors();
    },
    success: function (label) {
    label.html(imgOk);
    WB_IndicateFormErrors();
    }
    }); */

    // var validator = $("#Form1").validate();
    //WB_IndicateFormErrors();
}

function WB_IndicateFormErrors() {
    $('table.form-table td.feedback').each(function (index) {

        //var s = "" + $(this).html();
       // s = s.replace(" ", "");

        //alert(s + "***" + s.length);

        if ($(this).hasClass('error')) {
            //$(this).removeClass("error");
            $(this).parent("tr").addClass("error");

        }
        else {
            $(this).parent("tr").removeClass("error");
        }
    });
}


function WB_OnFormSubmit() {
    return $('#Form1').valid();
}

