$(document).ready(function() {
$('[id=product_description] [id^=name]').click(function() {
editProductName(this);
});
$('[id=product_description] [class=prod_descriptive]').click(function() {
editProductDesc(this);
});
$('[id=prod_fabric]').click(function() {
editProductFabric(this);
});
$('[id=prod_cats]').mouseenter(function() {
editProductCats(this);
});
$('img[rel=ia]').mouseover(function() {
editProductImage(this);
});
$('img[rel=ia]:first').parent().before($('img[rel=ia][main=t]').parent());
$('img[rel=ia]:first').trigger("mouseover");
$('img[rel=ia]:first').trigger("mouseleave");
$('[id=add-cats]').click(function() {
addProductCats(this);
});
$('#rel-sortable').sortable();
$('#rel-sortable').disableSelection();
$("#rel-sortable").sortable({
update : function(event, ui) {
sortRelatedProds();
}
});
$('#clear_this_product').click(function() {
if (confirm("Are you sure you want to clear all your changes?")) {
jsapi('clear_product_changes', {
prod_id : $('#prod_main').val()
}, function(r) {
//console.log(r);
window.location.reload();
});
}
});
$('#save_this_product').click(function() {
if (confirm("Are you sure you want to save all your changes?")) {
jsapi('save_this_product', {
prod_id : $('#prod_main').val()
}, function(r) {
//console.log(r);
window.location.reload();
});
}
});
$('#frame3 > ul li[pid] > div').mouseenter(function() {
editRelatedProds(this);
});
$('#add-related-prods').keypress(function(e) {
if (e.which == 13) {
addRelatedProduct(this);
}
});
});
function addRelatedProduct(context) {
var pid = $('#prod_main').val();
jsapi('find_product_by_sku', {
sku : $(context).val()
}, function(r) {
var prod = $.parseJSON(r);
console.log(prod);
if (prod.error == '') {
var alreadyin = false;
$('#frame3 > ul > li').each(function() {
if ($(this).attr('pid') == prod.pid) {
alreadyin = true;
}
});
if (!alreadyin) {
$('#frame3 > ul').append("
" + prod.pbox + "");
respond();
$('#frame3 > ul li[pid] > div').mouseenter(function() {
editRelatedProds(this);
});
change_product_info('related_prods', 'add', pid, prod.pid, function(r) {
//console.log(r);
});
}else{
$('#add-related-prods-response').html("This product is already in related products.");
}
} else {
$('#add-related-prods-response').html(prod.error);
}
});
}
function editRelatedProds(context) {
var pid = $('#prod_main').val();
var prod = $(context);
var pos = $('img', prod).position();
$('[id=relatedPopUp]').remove();
var checked = '';
var time = prod.parent().attr('time');
if (prod.parent().attr('is_pinned') == 'true') {
checked = 'checked';
}
prod
.append("");
$('#relatedPopUp').css({
top : pos.top + $('img', prod).height() - $('#relatedPopUp').height() - 14,
left : pos.left + $('img', prod).width() - $('#relatedPopUp').width() - 14
});
prod.mouseleave(function() {
$('[id=relatedPopUp]').remove();
});
$('#pinned').click(function() {
var pinned = $(this).is(':checked');
change_product_info('related_prods', 'pin', pid, {
pin : pinned,
pid : prod.parent().attr('pid')
}, function(r) {
//console.log(r);
});
if (pinned) {
prod.parent().attr('is_pinned', 'true');
} else {
prod.parent().attr('is_pinned', 'false');
}
});
$('#remove-related').click(function() {
prod.parent().remove();
change_product_info('related_prods', 'remove', pid, prod.parent().attr('pid'), function(r) {
//console.log(r);
});
});
$('#pinned-time').change(function() {
prod.parent().attr('time', $(this).val());
change_product_info('related_prods', 'pin-time', pid, {
time : $(this).val(),
pid : prod.parent().attr('pid')
}, function(r) {
//console.log(r);
});
});
}
function jsapi(mode, data, callback) {
var d = $('#jsapi_domain').val();
$.ajax({
type : 'post',
url : d + '/jsapi.php?mode=' + mode,
data : data,
success : function(r) {
callback(r);
}
});
}
function sortRelatedProds() {
var pid = $('#prod_main').val();
var sort = [];
$("#rel-sortable > li").each(function() {
sort.push($(this).attr('pid'));
});
console.log(sort);
change_product_info('related_prods', 'sort', pid, sort, function(r) {
//console.log(r);
});
}
function addProductCats(context) {
var span = $(context);
span.unbind('click');
var pid = span.attr('product');
span.html($('#all-cats'));
$('#all-cats').show();
$('#all-cats').focusout(function() {
if ($('#all-cats-holder #all-cats').length == 0) {
$('#all-cats-holder').html($('#all-cats'));
$('#all-cats option').attr('selected', false);
$('#all-cats option:first').attr('selected', true);
$('#all-cats').hide();
$('#all-cats').unbind('change');
$('#all-cats').unbind('focusout');
span.html("Categories: ");
span.click(function() {
addProductCats(this);
});
}
});
$('#all-cats').change(
function() {
var cat = $('#all-cats option:selected').val();
if (cat != '0') {
var url = $('#all-cats option:selected').attr('url');
var cat_name = $('#all-cats option:selected').text();
change_product_info('category', 'add', pid, cat, function(r) {
//console.log(r);
$('[id=prod_cats]:last',span.parent().parent()).parent().html(
$('[id=prod_cats]:last',span.parent().parent()).parent().html().replace('and', ','));
$('[id=prod_cats]:last',span.parent().parent()).after(
" and " + cat_name + "");
$('[id=prod_cats]').mouseenter(function() {
editProductCats(this);
});
});
$('#all-cats-holder').html($('#all-cats'));
$('#all-cats option').attr('selected', false);
$('#all-cats option:first').attr('selected', true);
$('#all-cats').hide();
span.html("Categories: ");
span.click(function() {
addProductCats(this);
});
}
});
}
function editProductImage(context) {
var img = $(context);
var pos = img.position();
var main = img.attr('main');
$('[id=imgpopup]').remove();
var mainimg = "";
if (main == 't') {
mainimg = "";
}
img.parent().before(
"");
$('#imgpopup').css({
top : pos.top - 30,
left : pos.left + (img.width() / 2) - 4 - ($('#imgpopup').width() / 2)
});
$('#imgpopup .downarrow').css({
top : $('#imgpopup').height() + 10,
left : ($('#imgpopup').width() / 2) - 4
});
$('#imgpopup,[id=image_container]').mouseleave(function() {
$('[id=imgpopup]').remove();
});
$('#isnt_main').click(function() {
$(this).attr('id', 'is_main');
$(this).css({
cursor : 'default'
});
change_product_info('product_image', 'set_main_img', img.attr('pid'), img.attr('img'), function(r) {
//console.log(r);
});
$(this).attr('src', '/css/images/start_icon.png');
$('img[rel=ia][main=t]').attr('main', 'f');
img.attr('main', 't');
});
$('#remove-img').click(
function() {
change_product_info('product_image', 'remove', img.attr('pid'), img.attr('img'), function(r) {
//console.log(r);
});
img.parent().remove();
if (main == 't') {
$('img[rel=ia]:first').attr('main', 't');
change_product_info('product_image', 'set_main_img', $('img[rel=ia]:first').attr('pid'), $(
'img[rel=ia]:first').attr('img'), function(r) {
//console.log(r);
});
}
});
}
function editProductCats(context) {
var pos = $(context).position();
$('[id=catpopup]').remove();
var width = $(context).width();
if ($(context).height() > 20) {
width = -20;
pos.left = pos.left + $(context).width();
}
$(context)
.before(
"");
$('#catpopup').css({
top : pos.top - 30,
left : pos.left + (width / 2) - 4 - ($('#catpopup').width() / 2)
});
$('#catpopup .downarrow').css({
top : $('#catpopup').height() + 10,
left : ($('#catpopup').width() / 2) - 4
});
$('#catpopup,[id=materialsbrand]').mouseleave(function() {
$('[id=catpopup]').remove();
});
$('#catpopup #remove-cat').click(function() {
var cat = $(context).attr('cat_id');
var pid = $(context).attr('product');
console.log(cat, pid);
change_product_info('category', 'remove', pid, cat, function(r) {
//console.log(r);
$(context).parent().remove();
});
});
}
function editProductFabric(context) {
var span = $(context);
var pid = span.attr('product');
var prod_name = span.html();
span.html("");
$('#editfabric').select();
span.unbind('click');
$('#editfabric').focusout(function() {
var fabric = $(this).val();
span.html(fabric);
change_product_info('product_fabric', 'edit', pid, fabric, function(r) {
//console.log(r);
});
span.click(function() {
editProductFabric(this);
});
});
$('#editfabric').keypress(function(event) {
if (event.which == 13) {
var fabric = $(this).val();
span.html(fabric);
change_product_info('product_fabric', 'edit', pid, fabric, function(r) {
//console.log(r);
});
span.click(function() {
editProductFabric(this);
});
return false;
}
});
}
function editProductName(context) {
var span = $(context);
var pid = span.attr('product');
var prod_name = span.html();
span.html("");
$('#editname').select();
span.unbind('click');
$('#editname').focusout(function() {
var name = $(this).val();
span.html(name);
change_product_info('product_name', 'edit', pid, name, function(r) {
//console.log(r);
});
span.click(function() {
editProductName(this);
});
});
$('#editname').keypress(function(event) {
if (event.which == 13) {
var name = $(this).val();
span.html(name);
change_product_info('product_name', 'edit', pid, name, function(r) {
////console.log(r);
});
span.click(function() {
editProductName(this);
});
return false;
}
});
}
function editProductDesc(context) {
var p = $(context);
var pid = p.attr('product');
var prod_desc = p.html();
p.html("");
$('#editdesc').select();
p.unbind('click');
$('#editdesc').focusout(function() {
var desc = $(this).val();
p.html(desc);
change_product_info('product_desc', 'edit', pid, desc, function(r) {
//console.log(r);
});
p.click(function() {
editProductDesc(this);
});
});
$('#editdesc').keypress(function(event) {
if (event.which == 13) {
var desc = $(this).val();
p.html(desc);
change_product_info('product_desc', 'edit', pid, desc, function(r) {
//console.log(r);
});
p.click(function() {
editProductDesc(this);
});
return false;
}
});
}
function change_product_info(type, action, prod_id, assoc_data, callback) {
var d = $('#jsapi_domain').val();
$('#save_this_product').attr('class', 'changes-made');
$.ajax({
type : 'post',
url : d + '/jsapi.php?mode=change_product_info',
data : {
type : type,
action : action,
prod_id : $('#prod_main').val(),
pid : prod_id,
assoc_data : assoc_data
},
success : function(r) {
callback(r);
}
});
}