var APP_URL = "http://talentequity.in/";

/*
 * Shows a pnotify powered dialog box
 *
 * @param title The 'title' of the dialog box.
 * @param text The content/text to display in the dialog box.
 * @param type The 'type' (notice or error) of the dialog box.
 * @scope Depends upon jQuery and pnotify js files.
 */
showMessage = function(title, text, type)
{
    if(title)
    {
        $.pnotify({
            pnotify_title: title,
            pnotify_text: text,
            pnotify_history: false,
            pnotify_addclass: "notification "+type+"",
            pnotify_width:"30%",
            pnotify_animation: "fade",
            pnotify_animate_speed: 1000,
            pnotify_type: type,
            pnotify_shadow: true,
            pnotify_closer: true,
            pnotify_hide: true
        });
    }
    else
    {
        $.pnotify({
            pnotify_text: text,
            pnotify_history: false,
            pnotify_addclass: "notification "+type+"",
            pnotify_width:"30%",
            pnotify_animation: "fade",
            pnotify_animate_speed: 1000,
            pnotify_type: type,
            pnotify_shadow: true,
            pnotify_closer: true,
            pnotify_hide: true
        });
    }
}
/**
 * populates the selectbox(elementId) with the response
 *
 * useful when the cities select box needs to be populated according to state selected
 */
populateSelectBox = function(elementId,method,url,data){
    var $element = $('#'+elementId+'');
    $.ajax({
        type: method,
        url: url,
        data: data,
        dataType: "json",
        success: function (data) {
            $element.empty();
            $element.append('<option value="">Select</option>');
            if(data)
            {
                $.each(data, function () {
                    $element.append('<option value="'+this.id+'">'+this.name+'</option>');
                });
            }
        },
        beforeSend: function(){
            $element.addClass('show_loading_in_center')
        },
        complete: function(){
            $element.removeClass('show_loading_in_center')
        }
    });
}

/**
 * Returns a particular URL variable's value or all the variable's values as an array.
 * It is an addition to the jQuery's namespace.
 *
 * NOTE: works only when the URL is in this format i.e index.php?page=zyx&var1=value1 etc. etc
 */
$.extend({
    getUrlVars: function(){
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++)
        {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    },
    getUrlVar: function(name){
        return $.getUrlVars()[name];
    }
});

/**
 * vote
 *
 * @param activity The action action to send
 * @param id ID of review
 * @author Gaurav Sharma, Created on 25 January 2010 10:31 PM
 */
vote = function(activity, id)
{
    var isloggedin = 'yes';
    $.ajax({
        type: 'get',
        url: APP_URL+'ajax.php?action=votereview',
        data: {
            'activity':activity,
            'reviewid':id
        },
        dataType: 'html',
        success: function(data)
        {
            var text;
            var type;
            if(data == "1")
            {
                text = "Your vote has been recorded successfully.";
                type = "notice";
            }
            else if(data == "2")
            {
                text = "Please login first.";
                type = "notice";
                isloggedin = "no";
            }
            else if(data == "3")
            {
                text = "You have already voted for that review.";
                type = "notice";
            }
            else
            {
                text = "There was some problem recording your vote. Please try again later.";
                type = "error";
            }
            showMessage("", text, type);
        },
        complete: function(){
            if(isloggedin == "yes")
            {
                if(activity == "up")
                {
                    oldval = parseInt($('#voteup').text());
                    newval = oldval + 1;
                    $('#voteup span').text(newval);
                }
                else if(activity == "down")
                {
                    oldval = parseInt($('#votedown').text());
                    newval = oldval + 1;
                    $('#votedown span').text(newval);
                }
                else
                {
                    alert('invalid activity performed on voting control.. Reporting back to server..........');
                }
            }
            $('#vote_'+id+' #voteup').addClass('vote');
            $('#vote_'+id+' #votedown').addClass('vote');
            $('#vote_'+id).removeClass('vote show_loading_in_right');
        },
        beforeSend: function(){
            $('#vote_'+id+' #voteup').removeClass('vote');
            $('#vote_'+id+' #votedown').removeClass('vote');
            $('#vote_'+id).addClass('vote show_loading_in_right');
        }
    });
}

/**
 * watches / unwatches a company
 *
 * @param companyid The id of company that is to be watched / unwatched
 * @param task The task that needs to be done must be one of {watch or unwatch} case-sensitive
 */
watchcompany = function(companyid, task)
{
    $.ajax({
        type: "get",
        url: APP_URL+"ajax.php?action=watchcompany",
        data: {
            companyid: companyid,
            task: task
        },
        dataType: "string",
        success: function (data) {
            if(data == "1")
            {
                showMessage("", "You are now watching this company.", "notice");
            }
            else if(data == "2")
            {
                showMessage("", "You are already watching this company.", "notice");
            }
            else if(data == "3")
            {
                showMessage("", "You have successfully unwatched this company.", "notice");
            }
            else if(data == "4")
            {
                showMessage("", "You have already unwatched this company", "notice");
            }
            else
            {
                showMessage("", "There was some problem watching this company. Pleas try again later.", "error");
            }
        },
        beforeSend: function(){
            $('#'+task+'company').addClass('show_loading_in_center')
        },
        complete: function(){
            $('#'+task+'company').removeClass('show_loading_in_center');
        }
    });
}

/**
 * Loads all comments related to a particular reviewid
 *
 * @param reviewid The ID of review
 * @author Gaurav Sharma
 * @description For now this function will work only on companyreview.html page because it has some elements
 * which are populated by the ajax response.
 */
loadcomments = function(reviewid) {
    $.ajax({
        type: 'get',
        url: APP_URL+"ajax.php?action=loadcomments",
        data: {
            reviewid: reviewid
        },
        dataType: 'json',
        success: function(data)
        {
            $('#comments').append('<h2>Comments</h2>');
            if(data)
            {
                $.each(data, function (){
                    $('#comments').append('<div class="zp-100 clear"><blockquote>'+this.comment+'<br/> -- <span class="small clear">'+this.email+'... on '+this.postedon+'</span></blockquote></div>');
                })
            }
            else
            {
                $('#comments').append('<h2>No comments have been posted yet.</h2>');
            }
        },
        beforeSend: function(){
            $('#comments').addClass('show_loading_big_in_center');
        },
        complete: function(){
            $('#comments').removeClass('show_loading_big_in_center');
        }
    });
};
