// ========= (c)2011 :: html & css & jquery coding :: ЯRIK - www.csska.com  =========


$(document).ready(function() {
    //$("#hmenu li").corner("5px");
});





// ------------------------------------------------------------------- slider

$(document).ready(function() {
    if ($('#slides').length > 0) {
        $('#slides').cycle({
            fx: 'fade',
            speed: 500,
            timeout: 10000,
            randomizeEffects: false,
            random: 0,
            next: '.slideNext',
            prev: '.slidePrev',
            cleartypeNoBg: true,
            autostop: 0,
            autostopCount: 0,
            pause: 1
        });
    }
});




// ------------------------------------------------------------------- newscarusel

function mycarousel_initCallback(carousel) {
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

jQuery(document).ready(function() {
    if ($("#mycarousel li").length > 3) {
        jQuery('#mycarousel').jcarousel({
            auto: 5,
            wrap: 'last',
            animation: 500,
            scroll: 1,
            initCallback: mycarousel_initCallback
        });
    }
});







// ------------------------------------------------------------------- homemailsub

var src_str = "";
var src_str2 = ""
$(document).ready(function() {
    src_str = $("#homemailsub input[name=mail]").val();
    $("#homemailsub input[name=mail]").focus(function() {
        if ($(this).val() == src_str) {
            $(this).val('');
        }
    });
    $("#homemailsub input[name=mail]").blur(function() {
        if ($(this).val() == '') {
            $(this).val(src_str);
        }
    });

    src_str2 = $("#mailsub input[name=mail]").val();
    $("#mailsub input[name=mail]").focus(function() {
        if ($(this).val() == src_str2) {
            $(this).val('');
        }
    });
    $("#mailsub input[name=mail]").blur(function() {
        if ($(this).val() == '') {
            $(this).val(src_str2);
        }
    });
});






// ------------------------------------------------------------------- Product ZOOM

$(document).ready(function() {

    $("#products .product").hover(function() {
        $(this).find('.product_bg').css({ 'z-index': '10' });
        $(this).find('.productimg').css({ 'z-index': '10' });
        $(this).find('h4').css({ 'z-index': '10' });
        $(this).find('.product_bg').stop().animate({
            marginTop: '-114px',
            marginLeft: '-114px',
            top: '50%',
            left: '50%',
            width: '229px',
            height: '229px'
        }, 200);
        $(this).find('.productimg img').stop().animate({
            marginTop: '-20px',
            marginLeft: '-25px',
            top: '50%',
            left: '50%',
            height: '177px'
        }, 200);
        $(this).find('h4').stop().animate({
            bottom: '-20px'
        }, 200);

    }, function() {
        $(this).find('.product_bg').css({ 'z-index': '0' });
        $(this).find('.productimg').css({ 'z-index': '0' });
        $(this).find('.h4').css({ 'z-index': '0' });
        $(this).find('.product_bg').stop().animate({
            marginTop: '0',
            marginLeft: '0',
            top: '0',
            left: '0',
            width: '163px',
            height: '163px'
        }, 400);
        $(this).find('.productimg img').stop().animate({
            marginTop: '0',
            marginLeft: '0',
            top: '0',
            left: '0',
            height: '126px'
        }, 400);
        $(this).find('h4').stop().animate({
            bottom: '8px'
        }, 400);
    });
});

$(document).ready(function() {
    $('#loginwin_tab1').click(function() {
        alert('s');
    });

});

// ------------------------------------------------------------------- LOGIN Window


$(document).ready(function() {

    $('#login_butt').click(function() {
        $.get('/login_form' + (window.location.href.search(/\/eng\//) != -1 ? '_eng' : '') + '.html', function(data) {
            $('body').append(data);
            $('#loginwin').fadeIn();
            $('body').append('<div id="fade"></div>');
            $('#fade').css({ 'filter': 'alpha(opacity=80)' }).fadeIn();

        });
        return false;
    });

    $('a.loginwin_close, #fade').live('click', function() {
        $('#fade , #loginwin').fadeOut(function() {
            $('#fade').remove();
            $('#loginwin').remove();
        });
        return false;
    });


    $('#loginwin_tab1').live('click', function() {
        $('#loginwin_tab2').removeClass('active');
        $(this).addClass('active');
        $('#loginwin .loginform[name=loginform2]').hide();
        $('#loginwin .loginform[name=loginform1]').show();
        return false;
    });

    $('#loginwin_tab2').live('click', function() {
        $('#loginwin_tab1').removeClass('active');
        $(this).addClass('active');
        $('#loginwin .loginform[name=loginform1]').hide();
        $('#loginwin .loginform[name=loginform2]').show();
        return false;
    });


});

function logob1() {
    $("#logo").css("background-position", "left bottom");
    setTimeout("logob2()", 1500);
}
function logob2() {
    $("#logo").css("background", "url(../images/logo.png) no-repeat scroll left top transparent");
    setTimeout("logob1()", 1500);
}

$(function() {
    $("input[name='ur_face']").live("click", function() {
        SetUr($(this).attr("checked"));
    });
    SetUr($("input[name='ur_face']").attr("checked"));

    $("#logout_butt").live("click", function() {
        LogOut();
    });

    setTimeout("logob1()", 1500);
});

function SetUr(_flag) {
    if (_flag)
        $(".ur").show();
    else
        $(".ur").hide();
}

function Login() {
    var login = $("form[name='loginform1'] input[name='login']").val();
    var password = $("form[name='loginform1'] input[name='password']").val();
    $.get(window.location.href, { CallModule: "login", login: login, password: password }, function(_data) {
        if (_data == "0") {
            $("#login_status").html("<i>Не верная комбинация</i>");
        }
        else {            
            $("#hlogin").empty();
            $("#hlogin").html(_data);
            $('#fade').remove();
            $("#loginwin").remove();
        }
    });
}

function LogOut() {
    $.get("/", { CallModule: "login", action: "logout" }, function(_data) {
        $("#hlogin").empty();
        $("#hlogin").html(_data);
        window.location = window.location;
    });
}

function Registration() {
    var fields = $("form[name='loginform2'] input");
    var fields_arr = new Object();
    for (var i = 0; i < fields.length; i++) {
        var name = fields.eq(i).attr("name");
        $(fields_arr).attr(name, fields.eq(i).val());
    }
    fields_arr.CallModule = "a_reg";
    fields_arr.registrationForm = 1;
    $.get("/", fields_arr, function(_data) {
        if (_data == "1") {
           // window.location = window.location;
           Registration_success();
        }
        else {
            $("form[name='loginform2'] p").css("color", "");
            var fields = _data.split(",");
            for (var i = 0; i < fields.length; i++) {
                $("form[name='loginform2'] input[name='" + fields[i] + "']").parent().css("color", "red");
            }
        }
    });
}

function Registration_success(){
   $('.log_win_inner').empty();
   $(".log_win_inner").html('<div class="top">&nbsp;</div>');
   $(".log_win_inner").html('<div class="login_form_content"></div>');  
   $('.login_form_content').html("<br/><center><p><b>Вы успешно зарегистрировались!</b></p> "+
                                 "<br /><a onclick='Success();' class='button2'>ОК</a> </center>");
   $('.button2').css("float", "none");

}

function Success(){
    window.location = window.location;
}

function ShowRememberForm() {
    $("form[name='loginform1']").removeClass("active");
    $("form[name='loginform3']").addClass("active");
}

function Remember() {
    var login = $("form[name='loginform3'] input[name='login']").val();
    $.get("/", { CallModule: "remember_pass", login: login }, function(_data) {
        if (_data == "1") {
            $("form[name='loginform3']").removeClass("active");
            $("form[name='loginform1']").addClass("active");
        }
        else {
            $("#remember_status").html("<i>Логин не найден в базе.</i>");
        }
    });
}


// ------------------------------------------------------------------- LEFTmenu


$(document).ready(function() {
    $(".leftmenu>ul>li>ul").each(function() {
        if ($(this).find("li").length == 0) {
            $(this).remove();
        }
        else {
            $(this).parent().addClass("dynamic");
        }
    });
    $(".leftmenu>ul>li>ul>li>ul").each(function() {
        if ($(this).find("li").length == 0) {
            $(this).remove();
        }
    });
    $(".leftmenu>ul>li.dynamic>ul").css('display', 'none');
    $(".leftmenu>ul>li.active>ul").css('display', 'block');

    $(".leftmenu>ul>li.dynamic>a").click(function() {
        $(this).parent().parent().find('li.active').find('ul').slideUp(500);
        $(this).parent().parent().find('li.active').removeClass('active');
        $(this).parent().addClass('active');

        if ($(this).parent().find('ul').is(":hidden")) {
            $(this).parent().find('ul').slideDown(500);
        } else {
            $(this).parent().find('ul').slideUp(500);
        };
        return false;
    });
});


function ChangePic(_path) {
    $("#prod_picture").attr("src", _path);
    $("#prod_picture").parent().attr("href", _path);
}

function PostReload(_page, _params) {
    $("body").append("<form method='post' action='" + _page + "' id='tmp_form'></form>");
    for (key in _params) {
        $("#tmp_form").append("<input type='hidden' name='" + key + "' value='" + _params[key] + "' />");
    }
    $("#tmp_form").submit();
    return false;
}


$(document).ready(function() {
    $("table.cart_list_sub select[name^='productCover']").each(function() {
        if ($(this).find("option[selected]").text().indexOf("Ассорт") != -1) {
            $(this).parent().parent().find("select[name^='productSize']").attr("disabled", true);
        }
        else {
            $(this).parent().parent().find("select[name^='productSize']").attr("disabled", false);
        }
    });
    $("table.cart_list_sub select[name^='productCover']").change(function() {
        if ($(this).find("option[selected]").text().indexOf("Ассорт") != -1) {
            $(this).parent().parent().find("select[name^='productSize']").attr("disabled", true);
        }
        else {
            $(this).parent().parent().find("select[name^='productSize']").attr("disabled", false);
        }
    });
    $(".add_row").click(function() {
        var new_row = $(this).parent().parent().find("table.cart_list_sub tr:last").clone();
        $(new_row).find("select[name^='productCover']").change(function() {
            if ($(this).find("option[selected]").text().indexOf("Ассорт") != -1) {
                $(this).parent().parent().find("select[name^='productSize']").attr("disabled", true);
            }
            else {
                $(this).parent().parent().find("select[name^='productSize']").attr("disabled", false);
            }
        });
        $(this).parent().parent().find("table.cart_list_sub tr:last").after(new_row);
    })

    $(".del_row").click(function() {
        $(this).parent().parent().remove();
    })

    $("#subscribe_butt").click(function() {
        $.get("/", { CallModule: "subscribe", email: $("#subscribe_mail").val() }, function(_data) {
            if (_data == "1") {
                $("#homemailsub").html("<br><i>Вы успешно подписались на рассылку наших новостей.</i>");
                $("#mailsub").html("<br><i>Вы успешно подписались на рассылку наших новостей.</i>");
            }
        });
    });
});

//----------------------для оптовиков
$(document).ready(function() {
    /*$("#hmenu a:contains('оптом')").attr("href", "javascript:void(0)").click(function(){
    $.post('/opt_form.html', function(data) {
    $('body').append(data);
    $('#loginwin').fadeIn();
    $('body').append('<div id="fade"></div>');
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();
    });
    });*/
    /*$("a.print").attr("href", "javascript:void(0)").attr("target", "_self").click(function(){
    var content=$("#content").html();
    if(content=="" || content==null)
    {
    content=$("#main").html();
    }
    $.post("/index.php", {CallModule:"print", content:content}, function(_data){
    myWin= open("/index.php", "displayWindow", "width=500,height=400,status=yes,toolbar=yes,menubar=yes");
    myWin.document.open();
    myWin.document.write(_data);
    myWin.document.close();
    })
    });*/
});

/*function CatalogOrder()
{
var fields=$("form[name='loginform1'] input");
var fields_arr=new Object();
for(var i=0; i<fields.length; i++)
{
var name=fields.eq(i).attr("name");
$(fields_arr).attr(name, fields.eq(i).val());
}
fields_arr.CallModule="catalog_order";
fields_arr.registrationForm=1;
$.post("/", fields_arr, function(_data)
{
if(_data=="1")
{
$('#fade').remove();
$("#loginwin").remove();
}
else
{
$("form[name='loginform1'] p").css("color", "");
var fields=_data.split(",");
for(var i=0; i<fields.length; i++)
{
$("form[name='loginform1'] input[name='"+fields[i]+"']").parent().css("color", "red");
}
}
});
}*/
