/// <reference path="../../typings/jquery/jquery.d.ts"/>
//var pbar;

/////////////////CHARTS START HERE/////////////////

// Load the Visualization API and the corechart package.
google.charts.load('current', {'packages':['corechart', 'controls']});
google.load("visualization", "1.1", {packages:["corechart", "controls"]});
// // Set a callback to run when the Google Visualization API is loaded.
 //google.charts.setOnLoadCallback(drawChart);

// // Callback that creates and populates a data table,
// // instantiates the pie chart, passes in the data and
// // draws it.
function drawChartPie(chart_data) {

        // Create the data table.
        var jsonData = chart_data;
        var projectData = new google.visualization.DataTable();
        projectData.addColumn('string', 'Type');
        projectData.addColumn('string', 'CertType');
        projectData.addColumn('number', 'Counts');
        // projectData.addColumn({type: 'string', role: 'annotation'}); , jsonData.CertType.concat(jsonData.rowCounts)

        $.each(jsonData, function(i, jsonData) {
            projectData.addRow([ jsonData.stat, jsonData.CertType ,jsonData.rowCounts ]);
        });

        // alert(JSON.stringify(projectData));

        var certPicker = new google.visualization.ControlWrapper({
                'controlType': 'CategoryFilter',
                'containerId': 'filter_div',
                'options': {
                    'filterColumnIndex': 1,
                    'ui': {
                            'width': 500,
                             'height': 400,
                            'pieSliceText': 'value',
                            'legend': 'right',
                            'allowMultiple': false,
                            'allowNone' : false
                        }
                    }
                });

            var dashboard = new google.visualization.Dashboard(
            document.getElementById('dash_div'));

            
            var pieChart = new google.visualization.ChartWrapper({
                'chartType': 'PieChart',
                'containerId': 'chart_div',
                'options': {
                    'width': 400,
                    'height': 400, 
                    'tooltip': {isHtml: true}, 
                    // 'displayAnnotations': true,
                     'legend': { position: 'labeled' },
                     'chartArea': {
                                    'width': "100%"
                            }                 
                },
                'view': {
                    'columns': [0, 2]
                }
            });

            dashboard.bind(certPicker, pieChart);
            // Draw the dashboard.
            dashboard.draw(projectData);
           
}

function drawChartUsersPie(chart_data) {

        // Create the data table.
        var jsonData = chart_data;
        var usersData = new google.visualization.DataTable();
        usersData.addColumn('string', 'Type');
        usersData.addColumn('string', 'Activity');
        usersData.addColumn('number', 'Counts');

        $.each(jsonData, function(i, jsonData) {
            usersData.addRow([ jsonData.stat, jsonData.activity ,jsonData.rowCounts ]);
        });

        var activePicker = new google.visualization.ControlWrapper({
                'controlType': 'CategoryFilter',
                'containerId': 'filter_div2',
                'options': {
                    'filterColumnIndex': 1,

                    'ui': {
                            'width': 500,
                            'height': 400,
                            'pieSliceText': 'value',
                            'legend': 'right',
                            'allowMultiple': false,
                            'allowNone' : false
                        }
                    }
                });

            var userDashboard = new google.visualization.Dashboard(
            document.getElementById('user_dash_div'));

         
            
            var userPieChart = new google.visualization.ChartWrapper({
                'chartType': 'PieChart',
                'containerId': 'chart_div2',
                'options': {
                    'width': 400,
                    'height': 400,     
                    'legend': { 'position': 'labeled',
                                'labeledValueText': 'both',
                             },
                    'chartArea': {
                                    'width': "100%"
                            }               
                },
                'view': {
                    'columns': [0, 2]
                }
            });

            userDashboard.bind(activePicker, userPieChart);
            // Draw the dashboard.
            userDashboard.draw(usersData);


}



/////////////////CHARTS END HERE/////////////////



var OngoingContainer = null;
var CompletedContainer = null;
var PendingContainer = null;
var loadingMsgContainer = null;
var loadingMsgSpan = null;

$(document).ready(function () { 

    // $("#id-nav-holder-right").css("border-bottom", "2px solid #555");
    $("img").unveil();
    $("img").trigger('unveil');

    loadingMsgContainer = $(".cl-section-loading-msg");
    loadingMsgSpan = $(".cl-span-loading-msg");
    OngoingContainer = $(".cl-div-ongoing-p-holder").find(".cl-div-p-item-holder");
    CompletedContainer = $(".cl-div-completed-p-holder").find(".cl-div-p-item-holder");
    PendingContainer = $(".cl-div-pending-p-holder").find(".cl-div-p-item-holder");

    // variable for storing an admin/reviewers list
    var reviewers ={};
    // get the list of reviewers 
    $.post("getUserInfo.php",{user:"REVIEWERS"},function(data2){
        reviewers =  data2;
    },"json");

    $.post("API.php",{all_users: true}, function(data){
        $("#id-table-dashboard").DataTable( {
            "scrollX": true,
            "data": data,
            "columns": [
                {data : 'first_name'},
                {data : 'last_name'},
                {data : 'organization_id'},
                {data : 'email_id'},
                {data : 'phone'},
                {data : 'country_name'},
                {data : 'projCounts'},
                {data : 'ts_last_login_success'},
                {data : 'login_success_count'}
            ],
            "order": [[ 6, "desc" ]]
        } );

        
    },"json"); 


    $.post("API.php",{project: "LIST"}, function(data){
        // Create our number formatter.
        
        $("#id-table-dashboard-projects").DataTable( {
            "scrollX": true,
            "data": data,
            "columns": [
                {data : 'project_title'},
                
                {data : 'project_cost'},
                {data : 'project_contact_person'},
                {data : 'project_country'},
                {data : 'project_state'},
                {data : 'award_percentage'},
                {data : 'isud_status_text'},
                {data : 'project_submission_type_text'}
            ],
            "order": [[ 6, "desc" ]]
        } );
    },"json"); 


     $.post("API.php",{dashboard: "PROJSTATS"}, function(data){
        // Create our number formatter.
            google.charts.load('current', {
                    'packages' : [ 'corechart' ]
                });
                google.charts.setOnLoadCallback(function() {
                    drawChartPie(data);
                });
    },"json");

    $.post("API.php",{dashboard: "USERSTATS"}, function(data){
        // Create our number formatter.
            google.charts.load('current', {
                    'packages' : [ 'corechart' ]
                });
                google.charts.setOnLoadCallback(function() {
                    drawChartUsersPie(data);
                });
    },"json");


    $.post("API.php",{project: "LIST"}, function(data){
        var project = $(OngoingContainer).find(".cl-div-project").clone();
        $(OngoingContainer).empty();
        $(CompletedContainer).empty();
        
        $.each(data, function(index, elem){
            var projectTemplete = $(project).clone();
            var projectTitle = $(projectTemplete).find(".cl-span-project-title-text");
            var projectEditIcon = $(projectTemplete).find(".cl-i-project-edit");
            var projectDeleteIcon = $(projectTemplete).find(".cl-i-project-delete");
            var projectLeftContainer = $(projectTemplete).find(".cl-div-project-info-left");
            var projectRightContainer = $(projectTemplete).find(".cl-div-project-info-right");
            var projectExpandedTopContainer = $(projectTemplete).find(".cl-div-project-info-expanded-top");
            var projectUpdateButton = $(projectTemplete).find(".cl-project-update-btn");
            var projectSubmitButton = $(projectTemplete).find(".cl-project-submit-btn");
            var projectReviewer = $(projectTemplete).find(".cl-dropdown-reviewer");

            $(projectTemplete).attr("id","id-"+elem.id);
            $(projectTemplete).css("display","flex");

            $(projectTitle).html(elem.project_title);

            $(projectUpdateButton).attr("data-project-id",elem._id);
            $(projectSubmitButton).attr("data-project-id",elem._id);
            $(projectEditIcon).attr("data-project-id",elem._id);
            $(projectDeleteIcon).attr("data-project-id",elem._id);
            $(projectReviewer).attr("data-project-id",elem._id);
            
            var projectInfoItem = $(projectLeftContainer).find(".cl-div-project-info-item");
            var projectExpandedInfoItem = $(projectExpandedTopContainer).find(".cl-div-project-info-item");
            var projectAwardDetailsHolder = $(projectRightContainer).find(".cl-div-project-award-details-holder");
            var projectEarnedCredits = $(projectAwardDetailsHolder).find(".cl-span-project-award-credit-earned");
            var projectApplicableCredits = $(projectAwardDetailsHolder).find(".cl-span-project-award-credit-applicable");
            var projectAwardChart = $(projectAwardDetailsHolder).find(".cl-div-project-award-progress-container");

            $(projectLeftContainer).empty();
            $(projectExpandedTopContainer).empty();

            //Project Number
            var projectInfoItemClone = $(projectInfoItem).clone();
            $(projectInfoItemClone).find(".cl-span-project-info-item-header-text").html("Number");
            $(projectInfoItemClone).find(".cl-span-project-info-item-value-text").html(elem.sl_pr_no);
            $(projectInfoItemClone).find(".cl-span-project-info-item-value-text").val(elem.sl_pr_no);
            $(projectLeftContainer).append(projectInfoItemClone);

            //Project Owner
            var projectInfoItemClone = $(projectInfoItem).clone();
            $(projectInfoItemClone).find(".cl-span-project-info-item-header-text").html("Owner");
            $(projectInfoItemClone).find(".cl-span-project-info-item-value-text").html(elem.project_owner);
            $(projectInfoItemClone).find(".cl-span-project-info-item-value-text").val(elem.project_owner);
            $(projectLeftContainer).append(projectInfoItemClone);

            //Architect
            var projectInfoItemClone = $(projectInfoItem).clone();
            $(projectInfoItemClone).find(".cl-span-project-info-item-header-text").html("Architect");
            $(projectInfoItemClone).find(".cl-span-project-info-item-value-text").html(elem.project_architect);
            $(projectInfoItemClone).find(".cl-span-project-info-item-value-text").val(elem.project_architect);
            $(projectLeftContainer).append(projectInfoItemClone);

            //Floor Area
            var projectInfoItemClone = $(projectInfoItem).clone();
            $(projectInfoItemClone).find(".cl-span-project-info-item-header-text").html("Floor Area");
            $(projectInfoItemClone).find(".cl-span-project-info-item-value-text").html(elem.habitable_floor_area);
            $(projectInfoItemClone).find(".cl-span-project-info-item-value-text").val(elem.habitable_floor_area);
            $(projectLeftContainer).append(projectInfoItemClone);

            //Site Area
            var projectInfoItemClone = $(projectInfoItem).clone();
            $(projectInfoItemClone).find(".cl-span-project-info-item-header-text").html("Site Area");
            $(projectInfoItemClone).find(".cl-span-project-info-item-value-text").html(elem.site_area);
            $(projectInfoItemClone).find(".cl-span-project-info-item-value-text").val(elem.site_area);
            $(projectLeftContainer).append(projectInfoItemClone);

            //Building Type
            var building_type_string = elem.building_type.replace(new RegExp(";{2,}","g"), "").replace(new RegExp(";","g"),",").replace(new RegExp("^,+","g"),"").replace(new RegExp(",+$","g"),"");
            var projectInfoItemClone = $(projectInfoItem).clone();
            $(projectInfoItemClone).find(".cl-span-project-info-item-header-text").html("Building Type");
            $(projectInfoItemClone).find(".cl-span-project-info-item-value-text").html(building_type_string);
            $(projectInfoItemClone).find(".cl-span-project-info-item-value-text").val(building_type_string);
            $(projectLeftContainer).append(projectInfoItemClone);

            /// Reviewer List
            // collect the list of reviewers in the array
            var output = [];
            $.each(reviewers, function(index_reviewer, reviewer)
            {
                // console.log( reviewer.first_name );
                output.push('<option value="'+ reviewer.email_id +'">'+ reviewer.first_name +'</option>');
            });
            $(projectReviewer).html(output.join(''));
            $(projectReviewer).val(elem.reviewer);

            //// Expanded Section
            //Start Date
            var projectExpandedInfoItemClone = $(projectExpandedInfoItem).clone();
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-header-text").html("Start Date");
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").html(elem.project_a_start_date);
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").val(elem.project_a_start_date);
            $(projectExpandedTopContainer).append(projectExpandedInfoItemClone);

            //End Date
            var projectExpandedInfoItemClone = $(projectExpandedInfoItem).clone();
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-header-text").html("End Date");
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").html(elem.project_a_end_date);
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").val(elem.project_a_end_date);
            $(projectExpandedTopContainer).append(projectExpandedInfoItemClone);

            //Project Cost
            var projectExpandedInfoItemClone = $(projectExpandedInfoItem).clone();
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-header-text").html("Cost");
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").html(elem.project_cost);
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").val(elem.project_cost);
            $(projectExpandedTopContainer).append(projectExpandedInfoItemClone);

            //Primary Contact
            var projectExpandedInfoItemClone = $(projectExpandedInfoItem).clone();
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-header-text").html("Primary Contact");
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").html(elem.project_contact_person);
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").val(elem.project_contact_person);
            $(projectExpandedTopContainer).append(projectExpandedInfoItemClone);

            //Contact Email
            var projectExpandedInfoItemClone = $(projectExpandedInfoItem).clone();
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-header-text").html("Contact Email");
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").html(elem.project_contact_email);
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").val(elem.project_contact_email);
            $(projectExpandedTopContainer).append(projectExpandedInfoItemClone);

            //Telephone
            var projectExpandedInfoItemClone = $(projectExpandedInfoItem).clone();
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-header-text").html("Telephone");
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").html(elem.project_contact_telephone);
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").val(elem.project_contact_telephone);
            $(projectExpandedTopContainer).append(projectExpandedInfoItemClone);

            //Address
            var projectExpandedInfoItemClone = $(projectExpandedInfoItem).clone();
            var projectExpandedInfoItemSpanTextClone = $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").clone();
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-header-text").html("Address");
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").html(elem.project_address_line_1);
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").val(elem.project_address_line_1);
            if(elem.project_address_line_2){
                var newSpanLine = $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").clone();
                $(newSpanLine).val(elem.project_address_line_2);
                $(newSpanLine).html(elem.project_address_line_2);
                $(projectExpandedInfoItemClone).find(".cl-div-project-info-item-value").append(newSpanLine);
            }
            $(projectExpandedTopContainer).append(projectExpandedInfoItemClone);
            
            //// City
            var projectExpandedInfoItemClone = $(projectExpandedInfoItem).clone();
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-header-text").html("City");
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").html(elem.project_city);
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").val(elem.project_city);
            $(projectExpandedTopContainer).append(projectExpandedInfoItemClone);

            //// State
            var projectExpandedInfoItemClone = $(projectExpandedInfoItem).clone();
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-header-text").html("State");
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").html(elem.project_state);
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").val(elem.project_state);
            $(projectExpandedTopContainer).append(projectExpandedInfoItemClone);

            //// Country
            var projectExpandedInfoItemClone = $(projectExpandedInfoItem).clone();
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-header-text").html("Country");
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").html(elem.project_country);
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").val(elem.project_country);
            $(projectExpandedTopContainer).append(projectExpandedInfoItemClone);

            //// Zipcode
            var projectExpandedInfoItemClone = $(projectExpandedInfoItem).clone();
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-header-text").html("Zipcode");
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").html(elem.project_zipcode);
            $(projectExpandedInfoItemClone).find(".cl-span-project-info-item-value-text").val(elem.project_zipcode);
            $(projectExpandedTopContainer).append(projectExpandedInfoItemClone);

            $(projectExpandedTopContainer).append(projectExpandedInfoItemClone);

            attachEventListeners(projectTemplete);

            if(parseInt(elem.isud_status,10) === 0){
                $(OngoingContainer).append(projectTemplete);
                attachSubmitEventListener(projectTemplete, true);
            } else if(parseInt(elem.isud_status,10) === 1){
                $(CompletedContainer).append(projectTemplete); 
                attachSubmitEventListener(projectTemplete, false);               
            } else if(parseInt(elem.isud_status,10) === 2){
                $(PendingContainer).append(projectTemplete);                                    
                attachSubmitEventListener(projectTemplete, false);               
            }

            $(projectAwardChart).attr("id",$(projectAwardChart).attr("id")+elem._id);

            var pbar = new ProgressBar.Circle("#"+projectAwardChart.attr("id"), {
                color: '#F7941E',
                strokeWidth: 10,
                trailColor: '#ffffff',
                trailWidth: 10,
                outlineColor: '#444444',
                outlineWidth: 2,
                text: {
                    autoStyleContainer: false
                }
            });
            pbar.setText("0%");
            pbar.set(0.0);
            pbar.text.style.fontSize = '1em';
            pbar.text.style.color = 'black';
            pbar.text.style.fontWeight = '600';


            var percentage_completion = Math.round(parseFloat(elem.award_percentage) + (parseInt(elem.bonus_credits,10) > 50 ? 10 : elem.bonus_credits/5));
            pbar.setText(percentage_completion+"%");
            pbar.set(percentage_completion/100.0);
            projectEarnedCredits.html(elem.earned_credits);
            projectApplicableCredits.html(elem.applicable_credits);
            projectSubmitButton.attr("data-perc", percentage_completion);
            projectSubmitButton.attr("data-applicable", elem.applicable_credits);
            projectSubmitButton.attr("data-scope", elem.project_scope);
        });

        updateEmptyContainers();

    },"json");

});

// Show empty div if no projects in container
function updateEmptyContainers(){
    $(".cl-div-pending-p-holder").find(".cl-div-p-empty-holder").css("display", $(PendingContainer).children().length <= 0 ? "flex" : "none");
    $(".cl-div-ongoing-p-holder").find(".cl-div-p-empty-holder").css("display", $(OngoingContainer).children().length <= 0 ? "flex" : "none");
    $(".cl-div-completed-p-holder").find(".cl-div-p-empty-holder").css("display", $(CompletedContainer).children().length <= 0 ? "flex" : "none");
}

function attachEventListeners(projectElem){
    attachEditEventListener(projectElem, true);
    attachDeleteEventListener(projectElem, true);
    //attachSubmitEventListener(projectElem, true);
    attachUpdateEventListener(projectElem, true);
    attachDetailsExpandEventListener(projectElem, true);
    attachAssignProjectReviewerDropDownListener(projectElem,true);
}

function attachEditEventListener(projectElem, attach){
    var btn = $(projectElem).find(".cl-i-project-edit");
    if(!attach){
        $(btn).css("cursor", "no-drop");
        $(btn).click(function(ev){
            return false;
        });
    } else {
        $(btn).css("cursor", "pointer");
        $(btn).click(function(ev){
            var target = "createProject.php?edit=true&p=" + $(this).attr("data-project-id");
            window.location.href = target;
        });
    }
}

function attachDeleteEventListener(projectElem, attach){
    var btn = $(projectElem).find(".cl-i-project-delete");
    if(!attach){
        $(btn).css("cursor", "no-drop");
        $(btn).click(function(ev){
            return false;
        });
    } else {
        $(btn).css("cursor", "pointer");
        $(btn).click(function(ev){
            var clickedIcon = $(this);
            var resp = confirm("Are you sure you want to delete this project permanently?");
            if(resp){
                $.post("API.php", {project: "DELETE", id: $(this).attr("data-project-id")}, function(data){
                    console.log(data.message);
                    clickedIcon.parent().parent().parent().remove();
                    updateEmptyContainers();
                },"json");
            } else {

            }
        });
    }
    
}

function attachSubmitEventListener(projectElem, attach){
    var btn = $(projectElem).find(".cl-project-submit-btn");
    if(!attach){
        $(btn).css("cursor", "no-drop");
        btn[0].addEventListener("click", function(ev){
            return false;
        }, {capture: true});
    } else {
        $(btn).css("cursor", "pointer");
        btn[0].addEventListener("click", function(ev){
            var clickedBtn = $(this);
            var applicableThreshold = parseInt($(this).attr("data-scope"), 10) == 0? 50 : 55;
            if (((parseFloat($(this).attr("data-perc")) < 60) && (parseInt($(this).attr("data-applicable"), 10) >= applicableThreshold))){
                alert("Project cannot be submitted because it doesn't meet the minimum threshold of completion.");
            } else {
                if(confirm("Are you sure you want to submit the project? Project will be locked, and no further updates can be made.")){
                    var target = "submitProject.php?pid=" + $(this).attr("data-project-id");
                    window.location.href = target;
                    
                    /*
                    //Use this code to bypass doc submission and directly submit for review

                    $(loadingMsgContainer).css("display","flex");
                    $(loadingMsgSpan).html("Please wait for the project to be submitted.");
                    $.post("API.php", {project: "SUBMIT_REVIEW", id: $(this).attr("data-project-id")}, function(data){
                        console.log(data.message);
                        $(loadingMsgSpan).html("Project Submission Status : "+ data.response ? "SUCCESS": "FAILURE"); 
                        $(loadingMsgContainer).css("display", "none");                           
                        var project = clickedBtn.parent().parent().parent().parent().parent().parent();
                        var projectClone = $(project).clone();
                        $(project).remove();
                        $(PendingContainer).append(projectClone);

                        updateEmptyContainers();
                        attachDeleteEventListener(projectClone, true);
                        attachEditEventListener(projectClone, true);
                        attachUpdateEventListener(projectClone, true);
                        attachSubmitEventListener(projectClone, false);
                    },"json");

                    */
                }
            }
        }, {once: true});
    }
}

function attachUpdateEventListener(projectElem, attach){
    var btn = $(projectElem).find(".cl-project-update-btn");
    if(!attach){
        $(btn).css("cursor", "no-drop");
        $(btn).click(function(ev){
            return false;
        });
    } else {
        $(btn).css("cursor", "pointer");
        $(btn).click(function(ev){
            var target = "adminSolution.php?edit=true&id=" + $(this).attr("data-project-id");
            window.location.href = target;
        });
    }
}

function attachDetailsExpandEventListener(projectElem, attach){
    var btn = $(projectElem).find(".cl-img-project-details-expand");
    if(!attach){
        $(btn).css("cursor", "no-drop");
        $(btn).click(function(ev){
            return false;
        });
    } else {
        $(btn).css("cursor", "pointer");
        $(btn).click(function(ev){
            $(btn).toggleClass("expanding");

            if($(btn).hasClass("expanding")){
                $(btn).attr("src","images/unitarrow_open.672fc522.png");
                $(btn).parent().parent().find(".cl-div-project-info-expanded-holder").find(".cl-div-project-info-item").css("max-height","100%");
                $(btn).parent().parent().find(".cl-div-project-info-expanded-holder").slideDown();
                $(btn).parent().parent().find(".cl-span-project-info-item-value-text").css("max-width","100%");
                $(btn).parent().parent().find(".cl-div-project-info-item").css("max-width","100%");
            } else {
                $(btn).attr("src","images/unitarrow_closed.0987a391.png");
                $(btn).parent().parent().find(".cl-div-project-info-expanded-holder").slideUp();
                $(btn).parent().parent().find(".cl-div-project-info-expanded-holder").find(".cl-div-project-info-item").css("max-height","4em");                
                $(btn).parent().parent().find(".cl-span-project-info-item-value-text").css("max-width","250px");
                $(btn).parent().parent().find(".cl-div-project-info-item").css("max-width","250px");
            }
        });
    }
}

function attachAssignProjectReviewerDropDownListener(projectElem, attach){
    var drop_down = $(projectElem).find(".cl-dropdown-reviewer");
    if(!attach)
    {
        $(drop_down).css("cursor", "no-drop");
        $(drop_down).click(function(ev){
            return false;
        });
    } 
    else 
    {
        $(drop_down).css("cursor", "pointer");
        $(drop_down).change(function()
        {
            var reviewer = $(this).val();
            var project_id = $(this).attr("data-project-id");
            console.log( project_id + " : " + reviewer );

            $.post("updateProjectReviewer.php",{ project_id: project_id , reviewer:reviewer },function(resp)
            {
                console.log("Reviewer updated successfully");
                console.log(resp);
            },"json");

            alert("Reviewer updated successfully");            
        });
    }
};