﻿$(document).ready(function() {
    //
//    $('#item-count').editable( function(value, settings) {
//             alert(value);
//        }, 
//        { 
//        type     : 'input'
//    });
     
     $("#query").bind("keypress", function(e) {
             if (e.keyCode == 13) {
                 search();
                 return false;
            }
      });
      
      $("#reg_user").bind("keypress", function(e) {
             if (e.keyCode == 13) {
                 $("#reg_mail").focus();
                 return false;
            }
      });
      
       $("#reg_mail").bind("keypress", function(e) {
             if (e.keyCode == 13) {
                Registration();
                 return false;
            }
      });
      
      $("#login_user").bind("keypress", function(e) {
             if (e.keyCode == 13) {
                 $("#login_pwd").focus();
                 return false;
            }
      });
      
      $("#login_pwd").bind("keypress", function(e) {
             if (e.keyCode == 13) {
                 LogIn();
                 return false;
            }
      });
      
      $("#lostpass_pwd").bind("keypress", function(e) {
             if (e.keyCode == 13) {
                 GenPasswd();   
                 return false;
            }
      });
            
    //   
    $(".effect").hover(function() {
        $(this).addClass("hover");
    }, function() {
        $(this).removeClass("hover");
    });
    $(function() {
        $(".item").bind("dblclick",
            function() {
                $(this).block({ message: null });
                $(this).addClass("current").effect("transfer",
                { to: "div#personal-bar" }, 1000,
                function() {
                    addToCart($(this).attr("id"), 1);
                    $(this).removeClass("current");
                });
                $(this).unblock()
            });
    });
    $(function() {
        $("[rel^='lightbox']").lightBox({
            imageLoading: "/skins/modellzona.hu/images/lightbox-ico-loading.gif",
            imageBtnClose: "/skins/modellzona.hu/images/lightbox-btn-close.gif",
            imageBtnPrev: "/skins/modellzona.hu/images/lightbox-btn-prev.gif",
            imageBtnNext: "/skins/modellzona.hu/images/lightbox-btn-next.gif"
        });
    });
    $(function() {
        $(".cart").colorbox({width:"80%", height:"95%", iframe:true, close:"[Bezárás] Vissza a kezdőoldalra"});
        $(".order").colorbox({width:"80%", height:"95%", iframe:true, close:"[Bezárás] Vissza a kezdőoldalra"});   
    });
    $(".order-to-login").click(function(){
        alert("A megrendeles feladásához be kell jelentkezni!");
		$("div#panel").slideDown("slow");
	});
    
    
    $(function() {
        var reg_user = $("#reg_user");
        var reg_user_l = $("#reg_user_l");
        reg_user_l.show();
        if (reg_user.val() == "Felhasznó név") {
            reg_user.val('');
        }
        if (reg_user.val() != "") {
            reg_user_l.hide();
        };
        reg_user_l.click(function() {
            reg_user.focus();
        });
        reg_user.focus(function() {
            reg_user_l.hide();
        });
        reg_user.blur(function() {
            if (reg_user.val() == "") {
                reg_user_l.show();
            };
        });

        var reg_mail = $("#reg_mail");
        var reg_mail_l = $("#reg_mail_l");
        reg_mail_l.show();
        if (reg_mail.val() == "Email cím") {
            reg_mail.val('');
        }
        if (reg_mail.val() != "") {
            reg_mail_l.hide();
        };
        reg_mail_l.click(function() {
            reg_mail.focus();
        });
        reg_mail.focus(function() {
            reg_mail_l.hide();
        });
        reg_mail.blur(function() {
            if (reg_mail.val() == "") {
                reg_mail_l.show();
            };
        });

        var login_user = $("#login_user");
        var login_user_l = $("#login_user_l");
        login_user_l.show();
        if (login_user.val() == "Felhasznó név") {
            login_user.val('');
        }
        if (login_user.val() != "") {
            login_user_l.hide();
        };
        login_user_l.click(function() {
            login_user.focus();
        });
        login_user.focus(function() {
            login_user_l.hide();
        });
        login_user.blur(function() {
            if (login_user.val() == "") {
                login_user_l.show();
            };
        });

        var login_pwd = $("#login_pwd");
        var login_pwd_l = $("#login_pwd_l");
        login_pwd_l.show();
        if (login_pwd.val() == "Jelszó") {
            login_pwd.val('');
        }
        if (login_pwd.val() != "") {
            login_pwd_l.hide();
        };
        login_pwd_l.click(function() {
            login_pwd.focus();
        });
        login_pwd.focus(function() {
            login_pwd_l.hide();
        });
        login_pwd.blur(function() {
            if (login_pwd.val() == "") {
                login_pwd_l.show();
            };
        }); 

        var lostpass_pwd = $("#lostpass_pwd");
        var lostpass_pwd_l = $("#lostpass_pwd_l");
        lostpass_pwd_l.show();
        if (lostpass_pwd.val() == "Email") {
            lostpass_pwd.val('');
        }
        if (lostpass_pwd.val() != "") {
            lostpass_pwd_l.hide();
        };
        lostpass_pwd_l.click(function() {
            lostpass_pwd.focus();
        });
        lostpass_pwd.focus(function() {
            lostpass_pwd_l.hide();
        });
        lostpass_pwd.blur(function() {
            if (lostpass_pwd.val() == "") {
                lostpass_pwd_l.show();
            };
        });
    });
});

function search() {
    var q = $("#query")
    if (q.val() != '') {
        var searchTerm = q.val();
//        document.location = '/kereses/' + $.URLEncode(searchTerm);
        document.location = '/kereses/' + searchTerm;
    }
}

function show_lostpass() {
    $("#login-form").hide();
    $("#lostpass-form").show();
    
}

function show_login() {
    $("#lostpass-form").hide();
    $("#login-form").show();
}

function goToPage(selObj) {
    document.location = selObj.options[selObj.selectedIndex].value;
}

function gohome() {
    document.location = "/";

}

function LogIn() {
    //alert($("#login_user").val());
    $.jmsajax({
        type: "POST",
        url: "/default.aspx",
        method: "LogIn",
        dataType: "msjson",
        data: { email: $("#login_user").val(), passwors: $("#login_pwd").val() },
        success: function(data) {
            if (data)
                gohome();
            else
                alert('Hibás felhsználónév vagy jelszó');
        }
    });
}

function Registration() {
    //alert($("#reg_mail").val());
    $.jmsajax({
        type: "POST",
        url: "/default.aspx",
        method: "CreateUser",
        dataType: "msjson",
        data: { name: $("#reg_user").val(), email: $("#reg_mail").val() },
        success: function(data) {
            if (data) {
                gohome();
                alert('A regisztráció megtörtént! A ' + $("#reg_mail").val() + ' címre elküldtük jelszavát.');
            }
            else
                alert('A felhasználónév vagy email cím már létezik');
        }
    });
}

function GenPasswd() {
    //alert($("#lostpass_pwd").val());
    $.jmsajax({
        type: "POST",
        url: "/default.aspx",
        method: "GenPasswd",
        dataType: "msjson",
        data: { email: $("#lostpass_pwd").val() },
        success: function(data) {
            if (data) {
                gohome();
                alert('A ' + $("#lostpass_pwd").val() + ' címre elküldtük új jelszavát.');
            }
            else
                alert('Az email cím nem megfelelő');
        }
    });
}

function ChangePasswd() {
    //alert($("#login_user").val());
    if($("#login_user").val() == $("#login_pwd").val()){
        $.jmsajax({
            type: "POST",
            url: "/default.aspx",
            method: "ChangePasswd",
            dataType: "msjson",
            data: { newPw: $("#login_pwd").val() },
            success: function(data) {
                if (data)
                    gohome();
                else
                    alert('A jelszónal legalább 8 krakternek kell lennie!')
            }
        });
    }
    else
        alert('A megadott ket jelszó eltérő!');
}

function SendOrderMail() {
    $.jmsajax({
        type: "POST",
        url: "/default.aspx",
        dataType: "msjson",
        method: "SendOrderMail",
        data: {},
        success: function() {
                gohome();
        }
    });
}

function OpenChangePasswd(){
    
}

function LogOut() {
    $.jmsajax({
        type: "POST",
        url: "/default.aspx",
        method: "LogOut",
        dataType: "msjson",
        data: {},
        success: function() {
            if (data)
                gohome();
        }
    });
}

function addToCart(productID, count) {
    $("#personal-bar").block({ message: null });
    $.jmsajax({
        type: "POST",
        url: "/default.aspx",
        method: "AddToCart",
        dataType: "msjson",
        data: { productID: productID, count: count },
        success: function(data) {
            reloadPersonal();
            //alert(String(data));
            $("#personal-bar").unblock();
        },
        error: function() {
            $("#personal-bar").unblock();
        }
    });
    
}
function add2Cart(productID) {
    $this = $("#" + productID);
    $($this).block({ message: null });
    $($this).addClass("current").effect("transfer",
    { to: "div#personal-bar" }, 1000,
    function() {
        addToCart($($this).attr("id"), 1);
        $($this).removeClass("current");
    });
    $($this).unblock()
}
function removeFromCart(OrderItemID, ShippingLimit, ShippingAmount) {
    $.jmsajax({
        type: "POST",
        url: "/cart.aspx",
        method: "RemoveFromCart",
        dataType: "msjson",
        data: { orderItemID: OrderItemID, shippingLimit: ShippingLimit, shippingAmount: ShippingAmount },
        success: function(data) {
            $("#" + OrderItemID.toString()).effect("explode", { pieces: 9 });
            $("#sub-total").text(data.SubTotal + " Ft");
            $("#shipping-amount").text(data.ShippingAmount + " Ft");
            $("#total").text(data.Total + " Ft");
            //window.location.reload();
        },
        error: function() {
        }
    });
}

function reloadPersonal() {
    $.ajax({
        url: "/Ajax/Personal.aspx",
        cache: false,
        success: function(html) {
            $("#personal-bar").replaceWith(html);
            //alert(String(html));
        }
    });
}

function ProductToolbar_addToCart(productID, count) {
    $("#ProductToolbar").block({ message: null });
    addToCart(productID, count);
    reloadProductToolbar(productID)
    $("#ProductToolbar").unblock()
}

function ProductToolbar_removeFromCart(orderItemID, productID) {
    $("#ProductToolbar").block({ message: null });
    removeFromCart(orderItemID);
    reloadProductToolbar(productID)
    $("#ProductToolbar").unblock()
}


function reloadProductToolbar(productID) {
    $.ajax({
        url: "/Ajax/ProductToolbar.aspx?pid=" + productID,
        cache: false,
        success: function(html) {
            $("#ProductToolbar").replaceWith(html);
            //alert(String(html));
        },
        error:function(html){
            alert(String(html));
        }
        
    });
}