/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var Page = (new function(){
    this.updateCart = function(){
        $.post('/update-winkelwagen.html',function(dataReturn){            
            $('#shoppingcart span.price').html(dataReturn.quantityTotal +' items &euro; ' + dataReturn.total);
        },'json');
    }

    this.deleteItem = function(){
        $('#different_address').live('click',function(){
            if ($(this).attr('checked') == true){
                $('#shipping_address').show();
            }
            else{
                $('#shipping_address').hide();
            }
            var indentificationHeight = $("#indentificationOrder .indentification").height();
           $("#indentificationOrder .payments").css('height',indentificationHeight*1 +'px');
        });

        $("a.delete").live('click',function(){            
            var id = $(this).parents('.product').attr('id').split('_');           
            var product_id;
            if (id.length == 3) {
                product_id = id[1]+ '_' + id[2];
            } else {
                product_id = id[1];
            }          
            $.post('/verwijderen-winkelwagen-item.html', {id: product_id}, function(data){
                if (data.success){                   
                    $('#product_' + product_id).hide();
                    $('#product_' + product_id).remove();
                    $('.subtotal td.subTotalPrice').html('&euro; ' + data.total);
                    $('.subtotal td.totalBTW').html('&euro; ' + data.totalBTW);
                    $('.subtotal td.btw').html('&euro; ' + data.btw);
                    $('.subtotal td.totalPrice').html('&euro; ' + data.total);                    
                    Page.updateCart();
                }
            },'json');
            return false;
        });
                
        $("input[name='quantityCart']").live('change',function (event){
            var id = $(this).parents('.product').attr('id').split('_');
            var product_id;
            if (id.length == 3) {
                product_id = id[1]+ '_' + id[2];
            } else {
                product_id = id[1];
            }
            var valQuantity = $(this).val();           
            var tr = $(this).parents('.product');
            $.post('/update-hoeveelheid.html', {id: product_id, quantity:valQuantity}, function(data){                
                if (data.success){
                    if (data.quantityTotal == null){
                       $('#product_' + product_id).hide();
                        $('#product_' + product_id).remove();
                        $('.subtotal td.subTotalPrice').html('&euro; ' + data.total);
                        $('.subtotal td.totalBTW').html('&euro; ' + data.totalBTW);
                        $('.subtotal td.btw').html('&euro; ' + data.btw);
                        $('.subtotal td.totalPrice').html('&euro; ' + data.total);
                        Page.updateCart();

                    }else {
                        var productPrice = tr.find('.productPrice p').text().split('€');
                        var subTotal = valQuantity * productPrice[1];
                        tr.find('.subTotal p').html('&euro;' + subTotal);
                        $('.subtotal td.subTotalPrice').html('&euro; ' + data.total);
                        $('.subtotal td.totalBTW').html('&euro; ' + data.totalBTW);
                        $('.subtotal td.btw').html('&euro; ' + data.btw);
                        $('.subtotal td.totalPrice').html('&euro; ' + data.total);
                        Page.updateCart();
                    }
                }
            },'json');
        });

       $('a.continueShopping').click(function(){
           window.location.reload();
       });
       $('a.continueOrder').live('click',function(){  
           $(".tabs ul li a.indentificationOrder").parent().addClass('active');
           $(".tabs ul li a.product").parent().removeClass('active');
           $(".tabs ul li a.confirmOrder").parent().removeClass('active');
           $(".tabs ul li a.payOrder").parent().removeClass('active');
           $("#indentificationOrder").removeClass('hide');
           $("#productOrder").addClass('hide');
           $('#confirmOrder').addClass('hide');
           $('#payOrder').addClass('hide');
           var indentificationHeight = $("#indentificationOrder .indentification").height();
           $("#indentificationOrder .payments").css('height',indentificationHeight*1 +'px');
           return false;
       });
       $('.tabs ul li a.product').live('click',function(){
           $(this).parent().parent().find('.active').removeClass('active');
           $(this).parent().addClass('active');
           $('#productOrder').removeClass('hide');
           $('#confirmOrder').addClass('hide');
           $('#payOrder').addClass('hide');
           $('#indentificationOrder').addClass('hide');           
           return false;
       });
       $('.tabs ul li a.indentificationOrder').live('click',function(){           
           $(this).parent().parent().find('.active').removeClass('active');
           $(this).parent().addClass('active');
           $('#productOrder').addClass('hide');
           $('#confirmOrder').addClass('hide');
           $('#payOrder').addClass('hide');
           $('#indentificationOrder').removeClass('hide');
           var indentificationHeight = $("#indentificationOrder .indentification").height();
           $("#indentificationOrder .payments").css('height',indentificationHeight*1 +'px');
           return false;
       });

       $('.idealPayment').live('click',function(){
           var payment_method = $(this).attr('id').split('_')[1];           
           var data = $('.page_affreken').find(':input').serializeArray();           
           data[data.length]={name:'payment_method',value:payment_method};           
           var urlParams = window.location.href.split("/");
           $.post('/'+ urlParams[3]+'/afrekenen.html',data, function(resp){
                if (resp.success){
                    $.post('/'+ urlParams[3]+'/bevestigen.html', {} , function(data) {
                        $('#confirmOrder').html(data);
                        $(".tabs ul li a.indentificationOrder").parent().removeClass('active');
                        $(".tabs ul li a.product").parent().removeClass('active');
                        $(".tabs ul li a.confirmOrder").parent().addClass('active');
                        $("#indentificationOrder").addClass('hide');
                        $("#productOrder").addClass('hide');                        
                        $('#confirmOrder').removeClass('hide');
                        var indentificationHeight = $("#confirmOrder .indentification").height();
                        $("#confirmOrder .payments").css('height',indentificationHeight*1 +'px');
                    },'text');
                } else {                  
                     $.each(resp.errors,function(k,value){
                        $("input[name='" +k +"']").parent().parent().find('em').html(value);
                        $("select[name='"+ k + "']").parent().parent().find('em').html(value);
                        $("input[name='" +k +"']").addClass('error');
                        $("select[name='"+ k + "']").addClass('error');                       
                    });
                    var indentificationHeight = $("#indentificationOrder .indentification").height();
                    $("#indentificationOrder .payments").css('height',indentificationHeight*1 +'px');
                }
            },'json');            
            return false;
       });
       $('.bankTransfer').live('click',function(){
           var payment_method = $(this).attr('id').split('_')[1];
           var data = $('.page_affreken').find(':input').serializeArray();           
           data[data.length]={name:'payment_method',value:payment_method};
           var urlParams = window.location.href.split("/");
           $.post('/'+ urlParams[3]+'/afrekenen.html',data, function(resp){
                if (resp.success){
                    $.post('/'+ urlParams[3]+'/bevestigen.html', {} , function(data) {
                        $('#confirmOrder').html(data);
                        $(".tabs ul li a.indentificationOrder").parent().removeClass('active');
                        $(".tabs ul li a.product").parent().removeClass('active');
                        $(".tabs ul li a.confirmOrder").parent().addClass('active');
                        $("#indentificationOrder").addClass('hide');
                        $("#productOrder").addClass('hide');
                        $('#confirmOrder').removeClass('hide');
                        var indentificationHeight = $("#confirmOrder .indentification").height();
                        $("#confirmOrder .payments").css('height',indentificationHeight*1 +'px');
                    },'text');
                } else {
                     $.each(resp.errors,function(k,value){
                        $("input[name='" +k +"']").parent().parent().find('em').html(value);
                        $("select[name='"+ k + "']").parent().parent().find('em').html(value);
                        $("input[name='" +k +"']").addClass('error');
                        $("select[name='"+ k + "']").addClass('error');
                    });
                    var indentificationHeight = $("#indentificationOrder .indentification").height();
                    $("#indentificationOrder .payments").css('height',indentificationHeight*1 +'px');
                }
            },'json');            
            return false;
       });

       $('#paymentMethodConfirm').live('click',function(){
           var urlParams = window.location.href.split("/");
           $.post('/'+ urlParams[3]+'/bevestigen.html', {'submit' : "submit_order"} , function(data) {
               console.log(data);
                $('#payOrder').html(data);
                $(".tabs ul li a.indentificationOrder").parent().removeClass('active');
                $(".tabs ul li a.product").parent().removeClass('active');
                $(".tabs ul li a.confirmOrder").parent().removeClass('active');
                $(".tabs ul li a.payOrder").parent().addClass('active');
                $("#indentificationOrder").addClass('hide');
                $("#productOrder").addClass('hide');
                $('#confirmOrder').addClass('hide');
                $('#payOrder').removeClass('hide');                
                setTimeout(function(){
                    $('#pay_iDEAL').click();
                },3000);                
           });
       });
    }    
});

