if (window.focus)
{
    if (document.getElementById("header_facebook"))
    {
        document.getElementById("header_facebook").innerHTML="<span id='facebook_login'>facebook</span>";
    }
    if (document.getElementById("comment_box"))
    {
        document.getElementById("comment_box").innerHTML=comment_box_html;
    } 
}
$('#facebook_login').click(function(){
    FB.login(function(response)
    {
        if (response.session)
        {
            if (response.perms)
            {
                top.location.href='/facebook_connect.php';
            }
            else
            {
                // user is logged in, but did not grant any permissions
            }
        }
        else
        {
            // user is not logged in
        }
    },
    {perms:'email,user_location,user_birthday,user_likes'});
    return false;
});
function display_engage_form(form_button)
{
    document.getElementById("header_login").innerHTML="";
    document.getElementById("engage_form_display").style.display="block";
    document.getElementById("engage_submit_button").innerHTML="<input id='"+form_button+"_submit' class='submit_button' name='"+form_button+"_submit' type='submit' value='"+form_button+"'>";
    document.engage_form.password.focus();
}
function check_numeric(input)
{
    //alert(input);
    //alert(document.getElementById(input).value);
    if (isNaN(document.getElementById(input).value))
    {
        document.getElementById(input).style.border="1px solid #800000;";
    }
}
function set_booked(week_num,week_start)
{
    if ($("."+week_num+"week").hasClass('booked'))
    {
        $("."+week_num+"week").removeClass('booked');
        currently_booked=1;
    }
    else
    {
        $("."+week_num+"week").removeClass('month_day');
        $("."+week_num+"week").addClass('booked');
        currently_booked=0;
    }
    $.ajax({
      url: '/function/ajax/save_booking.php',
      dataType: 'json',
      data: {week_start:week_start},
      success: function (new_html) { }
    });
}
/*these two functions are global to allow anything to be added to the basket from anywhere on the site*/
function updateBag(item_ID,user_ID)
{
    variation_ID=document.getElementById("item_variation"+item_ID).value;
    quantity=document.getElementById("quantity"+item_ID).value;
    $.ajax({
      url: '/function/ajax/update_bag.php',
      dataType: 'json',
      data: { item_ID: item_ID, variation_ID: variation_ID, quantity:quantity, user_ID:user_ID },
      success: function (new_html) { $("#basket_panel").html(new_html); }
    });
}
function saveComment(entityID,entityType,entitySubType)
{
    latestCommentsContent=document.getElementById("latest_comments").innerHTML;
    commentToSave=document.getElementById("add_comment_box").value;
    $.ajax({
      url: '/function/ajax/save_comment.php',
      dataType: 'json',
      data: { entityID:entityID, entityType:entityType, entitySubType:entitySubType, latestCommentsContent:latestCommentsContent, commentToSave:commentToSave },
      success: function (new_html) { $("#latest_comments").html(new_html); }
    });
}
function recordBasicSocialAction(entityID,entityType,entitySubType,actionType,recommendState)
{
    $.ajax({
      url: '/function/ajax/basic_social_action.php',
      dataType: 'json',
      data: { entityID: entityID, entityType: entityType, entitySubType:entitySubType, actionType:actionType, recommendState:recommendState },
      success: function (new_html) { $("#social_button_panel").html(new_html); }
    });
}
function display_large_image(new_path,new_image_name)
{
    set_prev_next(new_path);
    document.getElementById("big_image").src="/"+new_path;
    document.getElementById("large_image_item").innerHTML=new_image_name;
}
function set_prev_next(image_path)
{
    if (images.length>1)
    {
        for(i=0;i<images.length;i++)
        {
            if (images[i][0]==image_path)
            {
                if (i==images.length-1)
                {
                    next_path=images[0][0];
                    prev_path=images[i-1][0];
                    next_image_name=images[0][1];
                    prev_image_name=images[i-1][1];
                }
                else if (i==0)
                {
                    next_path=images[1][0];
                    prev_path=images[images.length-1][0];
                    next_image_name=images[1][1];
                    prev_image_name=images[images.length-1][1];
                }
                else
                {
                    next_path=images[i+1][0];
                    prev_path=images[i-1][0];
                    next_image_name=images[i+1][1];
                    prev_image_name=images[i-1][1];
                }
            }
        }
        document.getElementById("prev_panel").innerHTML="<span onclick='display_large_image(\""+prev_path+"\",\""+prev_image_name+"\")'>Prev</span>";
        document.getElementById("next_panel").innerHTML="<span onclick='display_large_image(\""+next_path+"\",\""+next_image_name+"\")'>Next</span>";
    }
}
