jQuery(function() {
    jQuery("#modal_reset").on('click', function() {
        jQuery('#catdesc').val("");
        jQuery('#catname').val("");
        jQuery('#catinfo').val("");
    });
    jQuery('.wk_manufacturer select').attr('name', 'virtuemart_manufacturer_id[]');
    jQuery('.wk_suggest_category').on('click', function() {
        jQuery('#catdesc').val("");
        jQuery('#catname').val("");
        jQuery('#catinfo').val("");
        jQuery('#wkModal .modal-body').css('display', 'block');
        jQuery('#wkModal .modal-footer').css('display', 'block');
        jQuery('#wkModal p').remove();
    });

    jQuery(window).on('load resize', function() {
        jQuery('.wk_manufacturer select').attr('multiple', 'multiple');

    });
});
if (typeof jQuery == 'undefined') {
    document.write(unescape("%3Cscript src='https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js' type='text/javascript'%3E%3C/script%3E"));
}
jQuery(function() {
    jQuery('.resp-tabs-container .resp-accordion:nth-child(1)').addClass('resp-tab-active');
    jQuery('.resp-tabs-container .contentbar:nth-child(1)').addClass('resp-tab-content-active');
    jQuery('#horizontalTab').easyResponsiveTabs({
        type: 'vertical', //Types: default, vertical, accordion           
        width: 'auto', //auto or any width like 600px
        fit: true, // 100% fit in a container
        closed: 'accordion', // Start closed if in accordion view
        activate: function(event) { // Callback function if tab is switched
            var $tab = jQuery(this);
            var $info = jQuery('#tabInfo');
            var $name = jQuery('span', $info);
            $name.text($tab.text());
            $info.show();
        }
    });
    jQuery('#verticalTab').easyResponsiveTabs({
        type: 'vertical',
        width: 'auto',
        fit: true
    });
});
jQuery(function() {
    jQuery('.Upload_img').on('mouseover', function() {
        jQuery('.upload_options_pro').css('display', 'block');
        jQuery('.upload_options_pro').css('-webkit-display', 'block');

        jQuery('.upload_options_pro').on('mouseover', function() {
            jQuery('.upload_options_pro').css('display', 'block');
            jQuery('.upload_options_pro').css('-webkit-display', 'block');
        });
    });
    jQuery('.Upload_img').on('mouseleave', function() {
        jQuery('.upload_options_pro').css('display', 'none');

        jQuery('.upload_options_pro').on('mouseleave', function() {
            jQuery('.upload_options_pro').css('display', 'none');
        });
    });
});
/******************************************************************/
jQuery(document).ready(function() {
    /*if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
        jQuery('.take_pic_').hide(0);
           jQuery('.nonipad').remove();
    }
    else
    {
      jQuery('.ipad').remove();
      
    }*/
    var resp = 0;
    var pic_snaps = '';
    var pic_size = '';


    jQuery('#result_holder').on('mouseover', '#result', function() {
        jQuery(this).children('#rm').show();
    });
    jQuery('#result_holder').on('mouseleave', '#result', function() {
        jQuery(this).children('#rm').hide();
    });
    jQuery('#camera').hide(0);
    jQuery('#new').hide(0);
    jQuery('#upload_pic').hide(0);

    var videoObj = '';

    jQuery('.take_pic_').on("click", function() {
        jQuery('#camera').css('display', 'block');

        var video = document.getElementById("video"),
            videoObj = { "video": true },
            errBack = function(error) {
                jQuery('.take_pic_').css('display', 'none');
                console.log("Video capture error: ", error.code);
            };

        if (navigator.getUserMedia) { // Standard
            navigator.getUserMedia(videoObj, function(stream) {
                video.src = stream;
                video.play();
            }, errBack);
        } else if (navigator.webkitGetUserMedia) { // WebKit-prefixed
            navigator.webkitGetUserMedia(videoObj, function(stream) {
                video.src = window.webkitURL.createObjectURL(stream);
                video.play();
            }, errBack);
        } else if (navigator.mozGetUserMedia) { // WebKit-prefixed
            navigator.mozGetUserMedia(videoObj, function(stream) {
                video.src = window.URL.createObjectURL(stream);
                video.play();
            }, errBack);
        }

        jQuery('#close_cam').on('click', function(stream) {

            jQuery('#camera').hide();

        });
    });
    // Trigger photo take
    jQuery("#cam_snap").on("click", function(e) {
        var canvas = document.getElementById("canvas"),
            context = canvas.getContext("2d")
        context.drawImage(video, 0, 0, 600, 400);
        jQuery('#video').hide('slow');
        jQuery('#canvas').show('slow');
        jQuery('#cam_snap').hide('slow');
        jQuery('#new').show('slow');
        jQuery('#upload_pic').show('slow');
        e.preventDefault();

    });

    // Capture New Photo
    jQuery("#new").on("click", function(e) {
        jQuery('#video').show('slow');
        jQuery('#canvas').hide('slow');
        jQuery('#cam_snap').show('slow');
        jQuery('#new').hide();
        jQuery('#upload_pic').hide();
        e.preventDefault();
    });
    jQuery(function() {
        jQuery(".add_product_main .product_save").click(function() {
            var productname = jQuery("#productname").val();
            var imgcate = '';
            if (jQuery(window).width() > 768) {
                imgcate = jQuery(".wk_category_tree select:nth-child(1) option:selected").val();
            } else {
                imgcate = jQuery(".wk_category_tree select:nth-child(2) option:selected").val();

            }

            if (typeof imgcate == 'undefined') {
                imgcate = '';
            }
            var stock = jQuery("#product_in_stock").val();
            var weight = jQuery("#product_weight").val();
            var price = jQuery("#price").val();
            var i = 1;
            var j = 1;
            var product_info = new Array('');
            var product_price = new Array('');

            if (!productname.length || !productname.trim().length) {
                product_info[i++] = "Please Eneter Product Name";
            }
            if (!imgcate.length || !imgcate.trim().length) {
                product_info[i++] = "Select atlease one product category";
            }
            if (stock.length && !jQuery.isNumeric(stock)) {
                product_info[i++] = "Stock value should be numeric value";
            }
            if (weight.length && !jQuery.isNumeric(weight)) {
                product_info[i++] = "Weight should be numeric value";
            }
            if (!price.length || !price.trim().length) {
                product_price[j++] = "Please Eneter Price";
            }
            if (!jQuery.isNumeric(price)) {
                product_price[j++] = "Price Should Be Numeric Value";
            }
            if (product_info.length > 1) {

                jQuery('.add_product_main .resp-tabs-list .resp-tab-item:nth-child(1)').trigger('click');
            } else if (product_price.length > 1) {
                jQuery('.add_product_main .resp-tabs-list .resp-tab-item:nth-child(3)').trigger('click');

            }
            var tc_chk = jQuery("#t_and_c").attr("name");
            if (tc_chk === "t_and_c") {
                var tc = jQuery("#t_and_c").attr("checked");
                if (tc !== "checked") {
                    jQuery(".wkRules").css({ 'color': 'red', 'font-weight': 'bold' });
                    jQuery("#t_and_c").focus();
                    jQuery('.add_product_main .resp-tabs-list .resp-tab-item:first-child').trigger('click');
                    return false;
                } else {
                    jQuery(".wkRules").css({ 'color': 'black', 'font-weight': 'unset' });
                }
            }

            var productname = jQuery("#productname").val();
            if (!productname.length || !productname.trim().length) {
                jQuery("#product-name").css({ "display": "block" });
                jQuery("#productname").attr('style', 'border: 2px dotted #FF0000 !important;');
                jQuery("#productname").focus();
                return false;
            } else {
                jQuery("#product-name").css({ "display": "none" });
                jQuery("#productname").css({ "border": "1px solid #AAAAAA" });
            }
            if (jQuery(window).width() > 768) {
                imgcate = jQuery(".wk_category_tree select:nth-child(1) option:selected").val();
            } else {
                imgcate = jQuery(".wk_category_tree select:nth-child(2) option:selected").val();

            }

            if (typeof imgcate == 'undefined') {
                imgcate = '';
            }
            if (!imgcate.length || !imgcate.trim().length) {
                if (jQuery(window).width() > 768) {
                    jQuery("#product_parent_id:nth-child(1)").css({ "border": "2px dotted red" });
                    jQuery("#product_parent_id:nth-child(1)").focus();
                } else {
                    jQuery("#product_parent_id:nth-child(2)").css({ "border": "2px dotted red" });
                    jQuery("#product_parent_id:nth-child(2)").focus();
                }
                return false;
            } else {
                jQuery("#product_parent_id:nth-child(1)").css({ "border": "1px solid #AAAAAA" });
                jQuery("#product_parent_id:nth-child(2)").css({ "border": "1px solid #AAAAAA" });
            }
            var stock = jQuery("#product_in_stock").val();
            if (stock.length && !jQuery.isNumeric(stock)) {
                jQuery("#product-stock").css({ "display": "none" });
                jQuery("#product-numstock").css({ "display": "block" });
                jQuery("#product_in_stock").css({ "border": "2px dotted red" });
                jQuery("#product_in_stock").focus();
                return false;
            } else {
                jQuery("#product-stock").css({ "display": "none" });
                jQuery("#product-numstock").css({ "display": "none" });
                jQuery("#product_in_stock").css({ "border": "1px solid #AAAAAA" });
            }
            var weight = jQuery("#product_weight").val();

            if (weight.length && !jQuery.isNumeric(weight)) {
                jQuery("#product-weight").css({ "display": "none" });
                jQuery("#product-numweight").css({ "display": "block" });
                jQuery("#product_weight").css({ "border": "2px dotted red" });
                jQuery("#product_weight").focus();
                return false;
            } else {
                jQuery("#product-weight").css({ "display": "none" });
                jQuery("#product-numweight").css({ "display": "none" });
                jQuery("#product_weight").css({ "border": "1px solid #AAAAAA" });
            }

            var price = jQuery("#price").val();
            if (price == '') {
                jQuery("#product-price").css({ "display": "block" });
                jQuery("#price").css({ "outline": "2px dotted red" });
                jQuery("#price").focus();
                jQuery(".mnu[rel='#product-price']").trigger("click");
                return false;
            } else if (!jQuery.isNumeric(price)) {
                jQuery("#product-price").css({ "display": "none" });
                jQuery("#product-numprice").css({ "display": "block" });
                jQuery("#price").css({ "outline": "2px dotted red" });
                jQuery("#price").focus();
                jQuery(".mnu[rel='#product-price']").trigger("click");
                return false;
            } else {
                jQuery("#product-price").css({ "display": "none" });
                jQuery("#product-numprice").css({ "display": "none" });
                jQuery("#price").css({ "outline": "none" });
            }


        });
        jQuery('.mnu').on('click', function() {
            var x = jQuery(this).attr('rel');
            jQuery(x).css("display", 'block');
            jQuery(x).addClass('active');
            jQuery(x).nextAll('.tab-pane').css("display", "none");
            jQuery(x).nextAll('.tab-pane').removeClass('active');
            jQuery(x).prevAll('.tab-pane').css("display", "none");
            jQuery(x).prevAll('.tab-pane').removeClass('active');
        });


        jQuery('.small_menu').on('click', function() {
            jQuery('.pls').toggle();
            jQuery('.mns').toggle();
            jQuery('.tab-menu').slideToggle(200);
        });
        jQuery('#product_parent_id option').each(function() {

            if (jQuery(this).text().indexOf('-') != 0) {

                jQuery(this).css('-webkit-text-stroke', '.40px');
            } else {
                jQuery(this).css('font-weight', 'normal');
                jQuery(this).css('-webkit-text-stroke', '0px');
            }
        });
    });


    /**************************start multilang****************************************************************/
    jQuery("body").on("mouseover", "#imagecollection img", function(event) {

        jQuery(event.target).parent("div").find(".image_over").css({ display: "block" });
        jQuery(event.target).parent("div").find("img").css({ opacity: '0.4' });

    });
    jQuery("#imagecollection img").mouseout(function(event) {
        jQuery(event.target).parent("div").find(".image_over").css({ display: "none" });
        jQuery(event.target).parent("div").find("img").css("opacity", "1");
    });

    function handleFileSelect(evt) {
        jQuery('#preview').empty();
        var files = evt.target.files; // FileList object
        var n = jQuery(files).length;
        for (var i = 0, f; f = files[i]; i++) {
            // Only process image files.
            if (!f.type.match('image.*')) {
                continue;
            }
            var reader = new FileReader();
            // Closure to capture the file information.
            reader.onload = (function(theFile) {
                return function(e) {
                    // Render thumbnail.
                    var div = document.createElement('div');
                    div.innerHTML = ['<img id="thumb', escape(theFile.name), '" src="', e.target.result,
                        '" title="', escape(theFile.name), '"/><span class="image_over" style="display: none;"></span><input type="hidden" name ="imagepre[]" value="', escape(theFile.name), '">'
                    ].join('');
                    document.getElementById('preview').insertBefore(div, null);
                    jQuery('#preview div').attr({ id: 'img_id' + escape(theFile.name), class: 'imgdiv' });

                    // Call A function For Css Of Image Div
                    wk_imgview();

                };
            })(f);
            // Read in the image file as a data URL.
            reader.readAsDataURL(f);
        }
    }

    function wk_imgview() {
        jQuery(".imgdiv").css({
            width: '70px',
            position: 'relative',
            border: '2px solid #CCCCCC',
            display: 'block',
            overflow: 'hidden',
            float: 'left',
            margin: '5px'
        });
        jQuery(".imgdiv img").css({
            border: '0 none',
            height: 'auto',
            maxWidth: '100%',
            verticalAlign: 'middle'
        });
        // Show the Cross Icon at Mouse Hover
        jQuery('.imgdiv').mouseover(function(event) {
            jQuery(this).find(".image_over").css({ display: "block" });
            jQuery(this).find("img").css("opacity", "0.4");

            // For Delete the image  Div at Click on Cross Icon
            jQuery(event.target).parent("div").find(".image_over").on('click', function() {
                jQuery(this).parent("div").remove();
                jQuery('#wk_image_label span').text()
            });
        });

        // Hide the cross icon at mouse hover out //
        jQuery(".imgdiv").mouseout(function(event) {
            jQuery(this).find(".image_over").css({ display: "none" });
            jQuery(this).find("img").css("opacity", "1");
        });

    }
    document.getElementById('image').addEventListener('change', handleFileSelect, false);
});