﻿$(document).ready(function() {
    if (!pd.ci) getKitItemsCookie();
    $("#CustomTextPersonalMessage").watermark("15 characters max");
});

function setvariant(productID, variantID, isLuxuryFinish, isHighColor, variantSKU, variantName, variantDescription) {
    pd.lf = isLuxuryFinish;
    pd.hc = isHighColor;
    var f;
    var sl;

    var re = document.getElementById('renderProcess');
    if (re) {
        re.style.display = isHighColor ? 'block' : 'none';
        sl = re.getElementsByTagName('select');
        if (sl.length > 0) {
            sl[0].selectedIndex = isHighColor ? 0 : 1;
        }
        f = "Render_" + pd.dpid + " = " + (isHighColor ? "'_COLOR'" : "''");
        eval(f);
    }

    f = "setsku_" + pd.dpid + "('" + variantSKU.substring(1) + "')";
    eval(f);

    var variantElem = document.getElementById('VariantID_' + pd.dpid + '_' + pd.dvid);
    variantElem.value = variantID;

    document.getElementById('variantName').innerHTML = variantName;
    document.getElementById('variantDescription').innerHTML = variantDescription;

    var lfe = document.getElementById('luxuryFinish');
    if (lfe) {
        lfe.style.display = isLuxuryFinish ? 'block' : 'none';
        var lf = 'None';
        if (isLuxuryFinish) {
            sl = lfe.getElementsByTagName('select');
            if (sl.length > 0) {
                lf = sl[0].options[sl[0].selectedIndex].text;
            }
        }
        eval("setluxuryfinish_" + pd.dpid + "('" + lf + "')");
    }
}

function onkitgroupchange(groupname, grouptype, itemid, value) {
    var f;
    if (groupname == "Luxury Finish") {
        f = "setluxuryfinish_" + pd.dpid + "('" + value + "')";
        eval(f);
    } else if (groupname == "Process") {
        $('#daysToShip').html(AdjustDaysToShip(value));
        changeprocess(value);
    } else if (groupname == "Custom Text") {
        $('span.CharCount').html(value.length > 15 ? 15 : 15 - value.length);
    }

    updatePrice(groupname, grouptype, itemid, value);
}

function AddToCart() {
    return AddToCartForm_AddToCart();
}

function setPanel(pnl) {
    setKitItemsCookie();
    var url = pd.upfx + '/custom' + (pd.ver ? '/v-' + pd.ver : '') + '#' + (pd.qa ? '/qa:' + encodeURIComponent(pd.qa) : '') + (pd.qd ? '/qd:' + encodeURIComponent(pd.qd) : '') + '/' + pd.mnm + '/' + pd.dnm + (pd.clr.length > 0 ? (',' + pd.clr) : '') + '/' + pd.scnm + '/' + pd.anm + (pnl != 'art' ? '/p-' + pnl : '');
    window.location.href = url;
}

function changeDesign(cat, art) {
    setKitItemsCookie();
    var url = pd.upfx + '/product' + '/' + pd.mnm + '/' + pd.dnm + (pd.clr.length > 0 ? (',' + pd.clr) : '') + '/' + cat + '/' + art;
    $('input[name=CartRecID]').val('');
    document.forms[0].action = url;
    document.forms[0].submit();
    return false;
    //return navto(url);
}

function setKitItemsCookie() {
    var c = $('#textFont img:first').attr('src');
    var name, value;
    $("input[name^='KitGroupID']:text").each(function() {
        name = $(this).attr('id') + '=';
        value = $(this).val();
        c += '|' + (value == '15 characters max' ? name : name + escape(value));
    });
    document.cookie = 'kititems=' + c + "; path=/";
}

function getKitItemsCookie() {
    var cookieArr;
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf('kititems=');
        if (c_start != -1) {
            c_start = c_start + 9;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1)
                c_end = document.cookie.length;
            cookieArr = document.cookie.substring(c_start, c_end).split('|');
            $('#textFont img:first').attr('src', cookieArr[0]);

            for (var i = 1; i < cookieArr.length; i++) {
                if (cookieArr[i].match(/^(.+)=(.*)$/)) {
                    var id = RegExp.$1, value = RegExp.$2;
                    $('#' + id).val(unescape(value));
                    //need to update price and character count if custom text field has any value in it
                    if (id == 'CustomTextPersonalMessage') {
                        var name = $('#CustomTextPersonalMessage').attr('name');
                        if (name.match(/.+_(\d+)$/)) {
                            onkitgroupchange('Custom Text', 4, RegExp.$1, value);
                        }
                    }
                }
            }
        }
    }
    return "";
}

function doSearch(q, t) {
    if (t == 'a') {
        pd.qa = q;
        pd.scnm = pd.anm = '-';
        setPanel('art');
    } else {
        pd.qd = q;
        pd.mnm = pd.dnm = '-';
        pd.clr = '';
        setPanel('dev');
    }
}

//dynamically change product picture (simplified script from ProductImageGallery.GetImgDHTML)
var picFormula = 'http://files.coveroo.com/variant/large/{0}/{1}{2}{3}_{4}.jpg';
var picIndex = 1;

function zoompic() {
    if (picIndex == 1) {
        picIndex = 3;
        document.getElementById('ImageZoom').innerHTML = 'Zoom (-)';
    }
    else {
        picIndex = 1;
        document.getElementById('ImageZoom').innerHTML = 'Zoom (+)';
    }
    changeproductpic();
}

function changecolorpic(color) {
    document.title = document.title.replace(/in\s\w+$/, 'in ' + color);

    //re-render fb like button with new color in url
    $('div.LikeButton').remove();
    $('div.SocialButtons').prepend('<div class="LikeButton"><fb:like href="' + window.location.href.replace(/,\w+/, ',' + color.toLowerCase()) + '" layout="button_count" show_faces="false" width="90" font="trebuchet ms"></fb:like></div>');
    FB.XFBML.parse();
   
    var devimgsrc = $('[name=MyDevicePic]').attr('src');
    $('[name=MyDevicePic]').attr('src', devimgsrc.replace(pd.clr, color.toLowerCase()));
    pd.clr = color.toLowerCase();
    changeproductpic();
}

function changeprocess(process) {
    pd.prcs = (process.indexOf('HighColor Full') >= 0) ? '_color_fb' : (process.indexOf('HighColor') >= 0) ? '_color' : '';
    changeproductpic();
}

function changeproductpic() {
    $('[name=ProductPic]').attr('src', picFormula.replace('{0}', pd.dnm).replace('{1}', pd.clr != '' ? pd.clr + '/' : '').replace('{2}', pd.anm).replace('{3}', pd.prcs).replace('{4}', picIndex));
}