var satchmo = satchmo || {};

// Get the current selected product price.  If taxed is true, then
// return the price inclusive of taxes.
satchmo.get_current_price = function(detail, qty, taxed) {
    var k = taxed ? "TAXED" : "PRICE";
    var prices = detail[k];
    if (prices) {
        var best = prices['1'];
        if (qty > 1) {
            for (var pricekey in prices) {
                var priceqty = parseInt(pricekey);
                if (priceqty > qty) {
                    break;
                }
                best = prices[pricekey];
            }
        }
        return best;        
    }
    else {
        return ""
    }
};
        
// Update the product name
satchmo.set_name = function(name) {
    $("#productname").attr('value', name);
};

// Update the product price
satchmo.set_price = function(price) {
    alert(price);
    $("#price").text(price);
};

satchmo.show_error = function(msg) {
    var section = $('#js_error');
    if (section.length == 0) {
        if (msg != "") {
            $('form#options').before("<div id='js_error' class='error'>" + msg + "</div>");
        }
    }
    else {
        section.text(msg);
        if (msg == "") {
            section.hide();
        }
    }
    var disabled = (msg != "");
    $('#addcart').attr('disabled', disabled);
};
    
// update name and price based on the current selections
satchmo.update_price = function() {
    var key = satchmo.make_optionkey();
    var detail = satchmo.variations[key];
    var msg = "";
    alert(detail);
    if (detail) {
        var qty = parseInt($('#quantity').fieldValue()[0]);
        satchmo.set_name(detail['SLUG']);
        var price = satchmo.get_current_price(detail, qty, satchmo.default_view_tax);
        satchmo.set_price(price);
        if (qty && qty > detail['QTY']) {
            if (detail['QTY'] == -1) {
                msg = "Sorry, we are out of stock on that combination.";
             }
             else {
                 msg = "Sorry, we only have " + detail['QTY'] + " available in that combination.";
             }
        }
    }
    else {
        msg = "Sorry, we don't have any of that combination available.";
    }
    satchmo.show_error(msg);
};

show_subcategories =  function() {
    parent_cat_slug = this.id;
    url = '/subcategory/'+ parent_cat_slug.toString() +'/ajax/';
    $.getJSON(url, function(res){ 
      length = res.length;
      html = '';
      if (res) {
         for(i=0; i< length; i++) {
            html = html + '<ul class=\"content-list\"><li class=\"content-list-title\"><a href=\"'+ 
                           res[i][1] +'\">' + res[i][0] +'</a></li></ul>';
            html = html + '<ul class=\"content-list2\" id=\"ul_bebek1\">';
            for(j=0;j < res[i][2].length; j++) {
              html = html + '<li class=\"content-list-level1\" style=\"list-style: none none;\">' +
                                  '<ul class=\"content-list-level1\">' +
                                    '<li><a href=\"'+ res[i][2][j][1] +'\">' + res[i][2][j][0] +'</a></li>' +
                                    '<li style=\"list-style: none none;\">' +
                                      '<span class=\"content-list-link\">'+ res[i][2][j][2] +'</span>' +
                                    '</li>' +
                                  '</ul>' +
                                '</li>';
            }
            html = html + '<li class=\"content-list-last\">' +
                                 '<ul class="content-list-last-level1">'+
                                   '<li style=\"list-style: none none;\"><a href=\"'+ res[i][1] +'\">' +
                                   'Daha Fazlası...</a></li>'+
                                  '</ul>' +
                                '</li></ul>';
            $("#cheaper_products_"+ i.toString()).html(html);
            html = '';
         }
      }
    }); 
};


// add product to cart and show cart popup page
post_additem = function() {
    $.ajax( {
        url : '/cart/add/ajax/',
        type : 'POST',
        data : 'productname=' + $("#productname").val() + '&quantity=' + $("#quantity").val(),
        dataType : 'json',
        success : function(json) {
            jQuery.facebox({ajax: '/cart/popup'});
        },
    } );
    return false;
}

$(document).ready(function() {
    $('#id_addcart').bind("click", {}, post_additem);
    $("#other_brands").hide(); 
    $('#other_brands_text').bind("click", function() {
        if ($("#other_brands").is(':visible')) {
          $("#other_brands").hide();
        } else {
          $("#other_brands").show();
        }  
      }
    ); 
  }  
);
