jQuery(document).ready( function() {
    // - needs cleaning
    jQuery('#product_reviews_section').click(function() {
        var cur_div = 'product_reviews_section_loader';
        var product_id=jQuery('#product_id').val();
        jQuery('#' + cur_div).dialog({
            zIndex: 10000,
            modal: true,
            autoOpen: true,
            height: 'auto',
            width: 955,
            title: 'Write a Product Review',
            close: function() {
            },
            open: function() {
                jQuery('#' + cur_div).load('ajax/review.php?review=1&product_id='+product_id, function() {
                    jQuery('#' + cur_div).dialog("option", "height", "auto");
                    jQuery('#' + cur_div).dialog("option", "position", "center");
                    //jQuery('#' + cur_div).dialog( "moveToTop" );
                });
            }
        });
        return false;
    });

    // - needs cleaning
    jQuery('#product_review_rating_section').click(function() {
        var cur_div = 'product_reviews_section_loader';
        var product_id=jQuery('#product_id').val();
        jQuery('#' + cur_div).dialog({
            zIndex: 10000,
            modal: true,
            autoOpen: true,
            height: 'auto',
            width: 955,
            title: 'Write a Product Review',
            close: function() {
            },
            open: function() {
                jQuery('#' + cur_div).load('ajax/review.php?review=1&product_id='+product_id, function() {
                    jQuery('#' + cur_div).dialog("option", "height", "auto");
                    jQuery('#' + cur_div).dialog("option", "position", "center");
                   // jQuery('#' + cur_div).dialog( "moveToTop" );
                });
            }
        });
        return false;
    });

    // - needs cleaning
    jQuery("#reviewform").validate({
        rules: {
            your_headline:{
            required: true
            },
            your_comments:{
            required: true,
            minlength: 50,
            maxlength: 2000
            },
            your_name: {
            required: true,
            minlength: 3,
            maxlength: 25
            },
            your_location: {
            required: true
            },
            your_state: {
            required: true
            },
            captcha: {
            required: true
            }
        },
        messages: {
            your_headline: {
                required: '<br>Please enter a title for this review.'
            },
            your_comments: {
                required: '<br>Please enter some commentary.',
                minlength: '<br>Reviews should be at least 50 characters.',
                maxlength: '<br>Reviews should be less than 2,000 characters.'
            },
            your_name:{
                required: '<br>Please enter your Name',
                minlength: '<br>Please enter at least 3 characters',
                maxlength: '<br>Please enter no more than 25 characters'
            },
            your_location: {
                required: '<br>Please enter a city.'
            },
            captcha: {
                required: '<br><br><br>Please enter the correct code.'
            },
            your_state: {
                required: '<br>Please select a state.'
            }
        }
    });

    // - needs cleaning
    jQuery("#generate").click(function(){
        var your_rating=jQuery("#your_rating").val();
        var your_headline=jQuery("#your_headline").val();
        var your_name=jQuery("#your_name").val();
        var your_location=jQuery("#your_location").val();
        var your_state=jQuery("#your_state").val();
        var your_comments=jQuery("#your_comments").val();
        var products_id=jQuery("#products_id").val();
        var captcha=jQuery("#captcha").val();
        var siteUrl=jQuery("#siteUrl").val();
        var dataString = jQuery('#reviewform').serialize() + '&product_id=' + products_id;
      //  var dataString='your_rating='+your_rating+'&your_headline='+your_headline+'&your_name='+your_name+'&your_location='+your_location+'&your_comments='+your_comments+'&product_id='+products_id+'&captcha='+captcha;
        var format=jQuery("#reviewform").valid();
        if (format) {
            jQuery("#processing").show();
            jQuery.ajax({
                type: "POST",
                url: "ajax/review.php?review=1&review_submit=1",
                data: dataString,
                success: function(responseText) {
                    if (jQuery.trim(responseText)=='true') {
                        window.location.href=siteUrl;
                    }
                    if (jQuery.trim(responseText)=='false') {
                        jQuery("#processing").hide();
                        alert('Image verification Code Is Invalid.');
                    }
                }
            });
        }
        return false;
    });

    // - needs cleaning
    // I have a question link
    jQuery('#product_question_section').click(function() {
        var cur_div    = 'product_reviews_section_loader';
        var product_id = jQuery('#product_id').val();
        jQuery('#' + cur_div).dialog({
            zIndex: 10000,
            modal: true,
            autoOpen: true,
            height: 'auto',
            width: 880,
            title: 'I Have a Question about this Product',
            open: function() {
                jQuery('#' + cur_div).load('ajax/question.php?question=1&product_id='+product_id, function() {
                    jQuery('#' + cur_div).dialog("option", "height", "auto");
                    jQuery('#' + cur_div).dialog("option", "position", "center");

                    jQuery("#submit_question").click(function(){
                        var your_email=jQuery("#your_email").val();
                        var your_name=jQuery("#your_name").val();
                        var your_comments=jQuery("#your_comments").val();
                        var products_id=jQuery("#products_id").val();
                        var captcha=jQuery("#captcha").val();
                        var siteUrl=jQuery("#siteUrl").val();
                        var dataString='your_email='+your_email+'&your_name='+your_name+'&your_comments='+your_comments+'&product_id='+products_id+'&captcha='+captcha;
                        var format=jQuery("#questionform").valid();
                        if (format) {
                            jQuery("#processing").show();
                            jQuery.ajax({
                                type: "POST",
                                url: "ajax/question.php?question=1&question_submit=1",
                                data: dataString,
                                success: function(responseText) {
                                    if(jQuery.trim(responseText)=='true'){
                                        window.location.href=siteUrl;
                                    }
                                    if(jQuery.trim(responseText)=='false'){
                                        jQuery("#processing").hide();
                                        alert('Image verification Code Is Invalid.');
                                    }
                                }
                            });
                        }
                        return false;
                    });
                });
            }
        });
        return false;
    });

    // - needs cleaning
    jQuery("#questionform").validate({
        rules: {
            your_email:{
            required: true,
            email: true
            },
            your_comments:{
            required: true,
            minlength: 50,
            maxlength: 200
            },
            your_name: {
            required: true,
            minlength: 4,
            maxlength: 25
            },
            captcha: {
            required: true
            },
            your_phone_number: {
            required: false,
            number: true
            }
        },
        messages: {
            your_email: {
            required: '<br>Please Enter Valid Email Address'
            },
            your_comments: {
            required: '<br>You must enter a Comment.',
            minlength: '<br>Questions should be at least 50 character',
            maxlength: '<br>Questions should be at less then 200 character'
            },
            your_name:{
            required: '<br>You must enter a name.'
            },captcha: {
            required: '<br>Please enter a correct verification code.'
            }
        }
    });

    // - needs cleaning
    /*video js*/
    jQuery('#product_video_section').click(function() {
        var cur_div = 'product_video_section_loader';
        var product_id=jQuery('#product_id').val();
        jQuery('#' + cur_div).load('ajax/get_videos.php?product_id='+product_id, function() {
            showvideo();
        });
    });






    // Email Friend link
    jQuery('.email_friend_link').click(function() {
        var cur_div    = 'dialog_section_loader';
        var product_id = jQuery('#product_id').val();
        jQuery('#' + cur_div).dialog({
            zIndex: 10000,
            modal: true,
            autoOpen: true,
            height: 'auto',
            width: 680,
            title: 'Email a Friend',
            close: function() {
                jQuery('#' + cur_div).dialog('destroy');
                jQuery('#' + cur_div).html('');
            },
            open: function() {
                jQuery('#' + cur_div).load('index.php?p=view_product&product&product_id='+product_id+'&email_friend=1', function(data) {
                    jQuery('#' + cur_div).dialog("option", "height", "auto");
                    jQuery('#' + cur_div).dialog("option", "position", "center");
                    jQuery('#emailform').submit(function() {
                        jQuery.post(jQuery("#siteUrl").val() + 'index.php?p=view_product&product&product_id='+product_id, jQuery('#emailform').serialize(), function(responseText) {
                            if (responseText == 'captcha_error') {
                                alert('Please verify the image verification code');
                            }
                            else if (responseText == "email_error") {
                                document.emailform.your_email.focus();
                                alert("Please enter a valid email address");
                            }
                            else if (responseText == "friend_email_error"){
                                document.emailform.friend_email.focus();
                                alert("Please enter a valid friend's email address");
                            }
                            else{
                                location.href = jQuery("#siteUrl").val() + 'index.php?p=view_product&product&product_id='+product_id + '&msg=01';
                            }
                        });
                        return false;
                    });
                });
            }
        });
        return false;
    });

    // Quick View hover
    jQuery('.quick_view_wrapper').mouseover(function() {
        jQuery('.quick_view_open_link', this).show();
    });
    jQuery('.quick_view_wrapper').mouseout(function() {
        jQuery('.quick_view_open_link', this).hide();
    });

    // Quick View links on products
    jQuery('.quick_view_open_link').click(function() {
        // initialize variables
        var product_title  = jQuery(this).attr('dialog_title');
        var dialog_domain  = jQuery(this).attr('dialog_domain');
        var product_id     = jQuery(this).attr('product_id');
        var cart_item_id   = jQuery(this).attr('cart_item_id');
        var qty            = jQuery(this).attr('qty');
        var optionsnum     = jQuery(this).attr('optionsnum');

        // remove any existing price blocks that will conflict with the price difference code
        jQuery('#quickview_dialog').data('price_block', jQuery('.price_small_' + product_id).html());
        jQuery('.price_small_' + product_id).html('');

        // open dialog
        jQuery('#quickview_dialog').dialog({
            zIndex: 10000,
            modal: true,
            autoOpen: true,
            dialogClass: 'quickview_dialog',
            title: product_title,
            width: 700,
            height: 700,
            close: function() {
                jQuery('.price_small_' + product_id).html(jQuery('#quickview_dialog').data('price_block'));
                jQuery('#quickview_dialog').dialog('destroy');
                jQuery('#quickview_dialog').html('');
            },
            open: function() {
                var url = dialog_domain + '/ajax/get_product.php?product_id=' + product_id;
                if (cart_item_id) {
                    url += '&cart_item_id=' + cart_item_id + '&qty=' + qty + '&optionsnum=' + optionsnum;
                }

                jQuery('#quickview_dialog').load(url, function() {
                    jQuery('#quickview_dialog').dialog("option", "height", "auto");
                    jQuery('#quickview_dialog').dialog("option", "position", "center");

                    jQuery('.quickview_close').click(function() {
                        jQuery('#quickview_dialog').dialog('close');
                        return false;
                    });
                });
            }
        });
        return false;
    });

});

