$(document).ready(function() {
    /* search form */
    $("#searchForm input").each(function() {
            if($(this).attr('type') == 'submit') {
                    ;
            }
            else {
                    $(this).attr("value", $(this).attr('title'));
            }
    })
    $("#searchForm input, textarea").click(function(){
    if ($(this).attr("value") == $(this).attr("title"))
      $(this).attr("value", "")
    });

    $("#searchForm input, textarea").blur(function(){
    if ($(this).attr("value") == "")
      $(this).attr("value", $(this).attr("title"))
    });

    /* opacity effects */
    $(".imgLink, .categoryItem").mouseenter(function() {
            $(this).css('opacity', '0.9');
    });
    $(".imgLink, .categoryItem").mouseleave(function() {
            $(this).css('opacity', '1');
    });

    $(".lowOpacity").mouseenter(function() {
            $(this).css('opacity', '1');
    });
    $(".lowOpacity").mouseleave(function() {
            $(this).css('opacity', '0.8');
    });

    /* brand description */
    $(".showInfo").click(function() {
            $("#brandDescription .hidden").slideDown();
            return false;
    });
    $("#brandDescription .hidden a").click(function() {
            $("#brandDescription .hidden").slideUp();
    });

    /* item photo effect*/
    $("#itemPhoto > div > a").mouseenter(function() {
            $(this).css('opacity', '0.8');
    });
    $("#itemPhoto > div > a").mouseleave(function() {
            $(this).css('opacity', '1');
    });


    /* add to cart */
    $("#addToCart").click(function() {

            switch(navigator.appName) {
                      case 'Microsoft Internet Explorer':
                            var myregex = /MSIE 7\.0/i;
                            var myArray = navigator.appVersion.match(myregex);
                            if(myArray.length > 0) scrollY = document.documentElement.scrollTop;
                            else scrollY = document.body.scrollTop;
                      break;
                      default:
                            scrollY = window.pageYOffset;
            }
            $("#messageBox").css('top', scrollY + 30);

    /* processing */
    $(".container").animate({opacity:0.3}, 1000 , function() {
            $("#messageBox > div > span ").html('Добавление товара в корзину...');
            $("#messageBox").fadeIn(2000, function() {
                    /* preparing data */
                    var itemId = $('#itemId').attr('value');
                    var color = $('.selectedColor').attr('id');
                    var size = $('#size').attr('value');

                    var link = "/ajax.php?mod=addToCart&item=" + itemId + "&size=" + size + "&color=" + color;
                    //alert(link);
                    $("#messageBox > div > span").load(link, function() {
                        $("#messageBox").fadeOut(4000, function() {
                                $(".container").animate({opacity:1}, 500);
                        });
                    });

                    /* count in top menu */
                    $("#cartCount").empty();
                    $("#cartCount").load('/ajax.php?mod=cartCount');

            });
    });
    return false;
    });

    /* recommended opacity */
    $(".recommended").css('opacity', 0.5);

    /* itemColor */
    $(".itemColor").click(function() {
       $(".itemColor").css('width', '20px');
       $(".itemColor").css('height', '20px');
       $(".itemColor").css('margin-top', '0px');
       $(".itemColor").removeClass('selectedColor');
       $(this).css('width', '50px');
       $(this).css('height', '30px');
       $(this).css('margin-top', '-5px');
       $(this).addClass('selectedColor');
    });

   /* popup ajax effect */
   $("[rel='#popup']").click(function() {
        switch(navigator.appName) {
                  case 'Microsoft Internet Explorer':
                        var myregex = /MSIE 7\.0/i;
                        var myArray = navigator.appVersion.match(myregex);
                        if(myArray.length > 0) scrollY = document.documentElement.scrollTop;
                        else scrollY = document.body.scrollTop;
                  break;
                  default:
                        scrollY = window.pageYOffset;
        }

        $("#popup").css('top', scrollY + 30);
        $("#popup > div").html("<img src='/img/shop/ajax-loader.gif' />");
        $(".container").animate({opacity:0.3}, 1000);
        $("#popup").fadeIn();

        $("#popup > div").load($(this).attr('href'));

        return false;
   });

   /* popup close effect*/
   $("#popup > img").click(function() {
       $("#popup").fadeOut();
       $(".container").animate({opacity:1}, 1000);
   });

   /* popup form sending */
   $("form").submit(function() {
       //alert(1);
   });


   /*delete item from cart*/
   $(".cartItem  .delete").click(function() {
       $('#messageBox').load($(this).attr('href'));
       
       var parent = $(this).parent();
       $(parent).css('background-color', 'red');
       $(parent).fadeOut().fadeIn().fadeOut().fadeIn().fadeOut(function() {
            $(parent).remove();

           $("#totalPriceValue").empty();
            var totalPrice = new Number;
            totalPrice = 0;
            $.each($('.cartItem input'), function(i, item) {
             totalPrice += parseInt($(item).attr('value'));
            });
            //alert(totalPrice);
            $("#totalPriceValue").html(totalPrice);
       });

       return false;
   });

    /* item colors */
    $.each($(".itemColor"), function(key, value) {
            $(this).css('background-color', $(this).attr('rel'));
    });

    /* login form */
    $("#loginForm").submit(function() {
       var loginInfo = $("#loginForm").serialize();
       $.post("/ajax.php?mod=login&ordering=yes", loginInfo, function(data) {
           $("#loginResponse").html(data);
       });
       return false;
    });

    /* ordering effects */
    $("[name='delivery']").change(function() {
       var deliveryId = $(this).attr('value');
       var infoLink = $('#deliveryInfo').attr('href');
       $('#deliveryInfo').attr('href', infoLink + deliveryId)
    });
    $("[name='payment']").change(function() {
       var deliveryId = $(this).attr('value');
       var infoLink = $('#paymentInfo').attr('href');
       $('#paymentInfo').attr('href', infoLink + deliveryId)
    });

    /*  submenu effect */
    $("#submenu").css('opacity', '0.8');

    /* index page template effect*/
    $("#mm").click(function() {
        if($("#menuM").css('display') == 'none') {
            $("#menuF").slideUp(function() {
                $("#submenu").css('display', 'block');
                $("#menuM").slideDown();
            });
        }
        else {
            $("#menuM").slideUp(function() {
                $("#submenu").css('display', 'none');
            });
        }
    });

    $("#fm").click(function() {
        if($("#menuF").css('display') == 'none') {
            $("#menuM").slideUp(function() {
                $("#submenu").css('display', 'block');
                $("#menuF").slideDown();
            });
        }
        else {
            $("#menuF").slideUp(function() {
                $("#submenu").css('display', 'none');
            });
        }
    });

    $("#mm2").click(function() {
        if($("#menuM").css('display') == 'none') {
            $("#menuF").slideUp(function() {
                $("#submenu").css('display', 'block');
                $("#menuM").slideDown();
            });
        }
        else {
            $("#menuM").slideUp(function() {
                $("#submenu").css('display', 'none');
            });
        }
    });

    $("#fm2").click(function() {
        if($("#menuF").css('display') == 'none') {
            $("#menuM").slideUp(function() {
                $("#submenu").css('display', 'block');
                $("#menuF").slideDown();
            });
        }
        else {
            $("#menuF").slideUp(function() {
                $("#submenu").css('display', 'none');
            });
        }
    });

    /* add to wishlist */
    $("#addToWishlist").click(function() {

            switch(navigator.appName) {
                      case 'Microsoft Internet Explorer':
                            var myregex = /MSIE 7\.0/i;
                            var myArray = navigator.appVersion.match(myregex);
                            if(myArray.length > 0) scrollY = document.documentElement.scrollTop;
                            else scrollY = document.body.scrollTop;
                      break;
                      default:
                            scrollY = window.pageYOffset;
            }
            $("#messageBox").css('top', scrollY + 30);

    /* processing */
    $(".container").animate({opacity:0.3}, 1000 , function() {
            $("#messageBox > div > span ").html('Добавление товара в вишлист...');
            $("#messageBox").fadeIn(1000, function() {
                    /* preparing data */
                    var itemId = $('#itemId').attr('value');

                    var link = "/ajax.php?mod=addToWishlist&item=" + itemId;
                    //alert(link);
                    $("#messageBox > div > span").load(link, function() {
                        $("#messageBox").fadeOut(4000, function() {
                                $(".container").animate({opacity:1}, 500);
                        });
                    });

                    /* count in top menu */
                    $("#wishlistCount").empty();
                    $("#wishlistCount").load('/ajax.php?mod=wishlistCount');

            });
    });
    return false;
    });

   /*delete from wishlist */
   $(".cartItem  .deleteWish").click(function() {
       $('#messageBox').load($(this).attr('href'));

       var parent = $(this).parent();
       $(parent).css('background-color', 'red');
       $(parent).fadeOut().fadeIn().fadeOut().fadeIn().fadeOut(function() {
            $(parent).remove();
       });

       return false;
   });

   /* search form effect*/
   $("#genderSearch").change(function() {
       $("#catalogSearch .invisible").fadeOut();
       $("#catalogSearch .invisible").attr('name', '')
       if($(this).attr('value') == 'm') {
           $("#mCats").fadeIn();
           $("#mCats").attr('name', 'group')
       }
       if($(this).attr('value') == 'f') {
           $("#fCats").fadeIn();
           $("#fCats").attr('name', 'group')
       }
   });
});


