$(document).ready(function()
{
     // When 'more filter options' link is clicked
    $("#moreFilterOptionsLink").click(function()
    {
        // Extract image's source and get filename from it
        var imageSource = (this.src).split("/");
        var imageFilename = imageSource.pop();

        // If image's filename does not contain '+' (plus) then replace '-' (minus) with '+' (plus)
        if (0 > imageFilename.indexOf("+"))
        {
            imageFilename = imageFilename.replace("-", "+");
        }
        // If image's filename contains '+' (plus) then replace '+' (plus) with '-' (minus)
        else
        {
            imageFilename = imageFilename.replace("+", "-");
        }

        // Change image's source
        imageSource.push(imageFilename)
        this.src = imageSource.join("/");

        // Toggle display of 'more filter options' box
        $("#more_search").toggle();
    });

    // When country is changed then get its regions drop-down using AJAX request
    $("#JobCountryId").change(function()
    {
        $.get(baseUrl + "jobs/get_regions_for_country/" + this.options[this.selectedIndex].value, function(response)
        {
            $("#JobRegion").html(response);
        });
    });

    // Bind event for page
    bindPageEvents();
});

// Function used to bind event for page
function bindPageEvents()
{
    // When any pagination link is clicked
    $(".paginationLink").click(function()
    {
        // Send AJAX request to get page
        $.get(this.href, function(response)
        {
            $("#jobsList").html(response);
            bindPageEvents();
        });

        // By default return false
        return false;
    });

    // Code to show details of Job
    $(".plus").click(function()
    {
        // Box id
        var boxId = (this.id).replace("plus", "");

        // Send ajax request to log job's statistics
        $.get(baseUrl + "job_statistics/add/" + boxId);

        // Add box ID to opened boxes cookie
        addJobToCookie(boxId);

        // Display job details
        openBox(boxId);
    });

    // Code to hide the details of job
    $(".hide").click(function()
    {
        var boxId = (this.id).replace("hide", "");
        removeJobFromCookie(boxId);
        closeBox(boxId);
    });

    // When apply button is clicked then get confirmation from user and redirect to 'apply for job' page
    $(".applyButton").click(function()
    {
        if (confirm(applyConfirmMessage))
        {
            window.location = baseUrl + "job_applications/add/" + (this.id).replace("applyButton_", "");
        }
    });

    // Opened jobs set in cookie
    var openedJobs = $.cookie("openedJobs");

    // If any opened jobs set in cookie then proceed further
    if (null != openedJobs)
    {
        // Make opened jobs as array
        if (0 > openedJobs.indexOf(","))
        {
            openedJobs = new Array(openedJobs);
        }
        else
        {
            openedJobs = openedJobs.split(",");
        }

        // Display needed job details boxes open
        for (var i in openedJobs)
        {
            openBox(openedJobs[i]);
        }
    }
}

// Function used to add particular job to 'opened jobs' cookie
function addJobToCookie(jobId)
{
    // Opened jobs set in cookie
    var openedJobs = $.cookie("openedJobs");

    // Make opened jobs as array
    if (null == openedJobs)
    {
        openedJobs = new Array();
    }
    else if (0 > openedJobs.indexOf(",") && jobId != openedJobs)
    {
        openedJobs = new Array(openedJobs);
    }
    else
    {
        openedJobs = openedJobs.split(",");
    }

    // If given job not already added to cookie then add it
    if (0 > $.inArray(jobId, openedJobs))
    {
        openedJobs[openedJobs.length] = jobId;
        $.cookie("openedJobs", openedJobs.join(","), { expires: 365 });
    }
}

function closeBox(jobId)
{
    $(".show" + jobId).hide();
    $("#plus" + jobId).show();
    $("#hide" + jobId).hide();
}

function openBox(jobId)
{
    $(".show" + jobId).show();
    $("#hide" + jobId).show();
    $("#plus" + jobId).hide();
}

// Function used to remove particular job from 'opened jobs' cookie
function removeJobFromCookie(jobId)
{
    // Opened jobs set in cookie
    var openedJobs = $.cookie("openedJobs");

    // Make opened jobs as array
    if (null == openedJobs)
    {
        openedJobs = new Array();
    }
    else if (0 > openedJobs.indexOf(",") && jobId != openedJobs)
    {
        openedJobs = new Array(openedJobs);
    }
    else
    {
        openedJobs = openedJobs.split(",");
    }

    // Get given job's position
    var position = $.inArray(jobId, openedJobs);

    // If given job not already removed from cookie then remove it
    if (0 <= position)
    {
        openedJobs.splice(position, 1);
        $.cookie("openedJobs", openedJobs.join(","), { expires: 365 });
    }
}