﻿loaded_scripts['/js/content_display/add_review.js']=true;
/**
 * @var array Used to track which submit button was pressed
 */ 
var submit_type = "";


/**
 * Is called when the form in the first step of reviewing a listing is submited
 * It builds the paramas array to send to server 
 * [0 => title, 1 => comments, 2 => pros, 3 => cons, 4 => rating, 5 => listing_id, 6 => submit_type(finish, add_photos), 7 => popup ID] 
 * It does a required fields check [title,comments]
 * After the Check it will make the json request, or alert that "Mandatory Fields" are needed
 *
 * @example onsubmit="add_review_step_1(this)"
 * @param $element  The rating the user selected
 */
function add_review_step_1($element){
    var element = $element;
    var params = {
        "title":$('#title_review').attr('value'),
        "comments":$('#comments').attr('value'),
        "pros":($('#pros')[0]) ? $('#pros').attr('value') : '',
        "cons":($('#cons')[0]) ? $('#cons').attr('value') : '',
        "rating":$('#rating').attr('value'),
        "listing_id":$('#listing_id').attr('value'),
        "charity":$('#charity').attr('value'),
        "submit_type":submit_type,
        "elementID":getPopUpWrapperId(element),
        "edit_review":$('#edit_review').attr('value')
    }
    
    if(params.title && params.comments && params.rating>0){ // title and comments  
        var json = new JSON;
        json.request('add_review_step_1',params,callback_add_review_step_1,'','/libs/JSON/review_actions.php');
    }else{
        alert("You Must Fill In All Mandatory Fields (*)");
    }
}

/**
 * The Callback actions for add_review_step_1
 * If HTML is returned it meens that they are moving to the next step and so it write it to the element
 * It then embeds the add_videos button as its not in the smarty file (therefor not in the $data.html). This is done becouse one needs javascript for adding videos    
 * If there is no HTML it checks to see if the user is on actions.php or not.
 * If they are not on actions.php it makes the call to update the review, and if the element is not in the inlinePopups array, closeses the popup
 * If they are on actions.php redirects them to a details page for the product they are reviewing  
 *  
 * @param array $data [html, elementID, review_id, listing_id]
 */
function callback_add_review_step_1($data){
    if ($data.err_code) {
        error_handler_by_id($data.err_code, $data.err_str);
    } else {
        if($data.html) {
            $('#popup_content_'+$data.elementID).html($data.html);
            $('#add_videos_holder').html('<input type="submit" name="add_videos" class="big_button_widest" value="ADD VIDEOS &gt;" onClick="var submit_type = this.name;" />');
        } else {
            var url_ar = getCurrentURL();
            if (url_ar['file'] != 'actions.php') {
                popup_close_all();
                if(loggedin) {
                    if ($("#reviews_wrapper")[0]) {
                        request_review_details($data.review_id,null,true, 'listing_details', $data.listing_id);
                    }
                    if ($data.do_facebook) {
                        publish_to_facebook($data.facebook_details);
                    }
                } else {
                    window.document.location = '/login/';
                }
           } else {
               var redirectLocation = '/d/'+$data.listing_id;
               if($('#referer')[0]){ redirectLocation = $('#referer').attr('value'); }
               window.document.location = redirectLocation;
           }
        }
    }
}


/**
 * Is called when the form in the second step of reviewing a listing is submited
 * It builds the paramas array and sends it to server as type add_review_step_2
 * [0 => listing_id, 1 => review_id, 2 => submit type(finish, add_vidoes), 3 => popup ID]
 *
 * @example onsubmit="add_review_step_2(this)"
 * @param $element  The rating the user selected
 */
function add_review_step_2($element){
    var params = new Array();
    params[0] = $('#listing_id').attr('value');
    params[1] = $('#review_id').attr('value');
    params[2] = submit_type;
    params[3] = getPopUpWrapperId($element);
    params[4] = $('#edit_review').attr('value');
    var json = new JSON;
    json.request('add_review_step_2',params,add_review_step_2_callback,'','/libs/JSON/review_actions.php');
}

/**
 * The Callback actions for add_review_step_2
 * If HTML is returned it meens that they are moving to the next step and so it write it to the element
 * Loads the Javascript for the video upload element 
 * It then embeds flash video uploader    
 * If there is no HTML it checks to see if the user is on actions.php or not.
 * If they are not on actions.php it makes the call to update the review, and if the element is not in the inlinePopups array, closeses the popup
 * If they are on actions.php redirects them to a details page for the product they are reviewing  
 *  
 * @param array $data [html, member_id, review_id, listing_id]
 */
function add_review_step_2_callback(data){
    if (data.err_code) {
        error_handler_by_id(data.err_code, data.err_str);
    }else{
        if(data.html){
            loadScript('/js/content_display/videos.js');
            $('#popup_content_'+data.elementID).html(data.html);
            
            var params = { wmode: "transparent", quality: "high" }
            var flashvars = {};
            flashvars.user_id = data.member_id;
            flashvars.id = data.review_id;
            flashvars.type = "review";
            flashvars.loggedin = getLoginString();
            
            swfobject.embedSWF("/swf/video_uploader.swf", "videoUploads", "520", "180", "8", '', flashvars, params);
        }else{
           var url_ar = getCurrentURL();
           if(url_ar['file'] != 'actions.php'){
               if(!inlinePopups[data.elementID]){
                   popup_close_all();
               }else{
                   remove_element($("#"+data.elementID)[0]);
               }
               if (data.do_facebook) publish_to_facebook(data.facebook_details);
               request_review_details(data.review_id,null,true, 'listing_details', data.listing_id);
           }else{
               var redirectLocation = '/dir/n49/d'+data.listing_id+'/'+$('#listing_name').html();
               if($('#referer')[0]){ redirectLocation = $('#referer').attr('value'); }
               window.document.location = redirectLocation;
           }
        }
    }
}

/**
 * Is called when the form in the third step of reviewing a listing is submited
 * If they are not on actions.php it makes the call to update the review, and if the element is not in the inlinePopups array, closeses the popup
 * If they are on actions.php redirects them to a details page for the product they are reviewing  
 *
 * @example onsubmit="add_review_step_3(this)"
 * @param $element The rating the user selected
 */
 var add_video_upload_status = false;
function add_review_step_3($element) {
    if (!add_video_upload_status) {
        var url_ar = getCurrentURL();
        if (url_ar['file'] != 'actions.php') {
            request_review_details($('#review_id').attr('value'),null,true, 'listing_details', data.listing_id);
            
            params[0] = $('#listing_id').attr('value');
            params[1] = $('#review_id').attr('value');
            params[2] = $('#edit_review').attr('value');
            var json = new JSON;
            json.request('add_review_step_3',params,add_review_step_3_callback,'','/libs/JSON/review_actions.php');
            
            var elementHolder = getPopUpWrapperId($element);
            if (!inlinePopups[elementHolder]) {
                popup_close_all();
            } else {
                remove_element($("#"+elementHolder)[0]);
            }
        } else {
           var redirectLocation = '/d/'+$('#listing_id').attr('value');
           if ($('#referer')[0]) { redirectLocation = $('#referer').attr('value'); }
           window.document.location = redirectLocation;
        }
   } else {
        var answer = confirm("You Must Wait For The Upload To Finish And Submit Your Details Before You Can Continue. \nWould You Like To Cancel Your Video Upload?")
        if (answer) {
            add_video_upload_finished();
            add_video_finish_listing($form);
        }
   }
}
function add_review_step_3_callback(data){
    if (data.err_code) {
        error_handler_by_id(data.err_code, data.err_str);
    } else {
        if (data.do_facebook) publish_to_facebook(data.facebook_details);
    }
}


/**
 * This function removes a thumbnail on step 3 of the adde listing review
 * @param $video_id The ID of the new video
 */
function remove_video_thumbnail($video_id){
    var thumbnailDiv = $('#video_thumb_'+$video_id)[0];
    var thumbnailWrapper = $("#"+thumbnailDiv.parentNode.id)[0];

    thumbnailWrapper.removeChild(thumbnailDiv);
}
