/******************
*
* Render.page.js

This class is designed to handle all markup changes that need to be made when a page loads.  Because
some page data is cached, all rendering must happen AFTER backend page load, and only will happen on 
the front end level. Functions are called in the document.ready block, and are arranged based on 
priority.  This file should be included on the page immediately after the jquery library, and is 
dependent on jquery 1.3.

All event listeners should be placed into subsequent action.x.js files that load after the render
file, so that listeners will be attached after post load rendering is completed.

*
*******************/
checkBrowser();
var PROXY_HOST;
$(document).ready(function(){
    $.post('/ajax/get_host_url',function(results){
        var json = eval('('+results+')');
        PROXY_HOST = json.payload;
        //first check login status and setup login box
        checkLoginStatus();
    
        //check if this page has been liked
        checkLikes();
    });
    
});

function checkBrowser()
{
    if (isIE6())
    {
        document.write("<div id=\"notice-for-ie6\">" + 
            "<p>We notice you're using an older browser and our site may not function correctly since this browser is outdated. Please " + 
            "download one of these fantastic browsers instead: <a href=\"http://firefox.com\" target=\"_blank\">Firefox</a> or <a href=\"http://google.com/chrome\" target=\"_blank\">Google Chrome</a>.</p>" + 
            "</div>");
    } 
}

function isIE6()
{
    if((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined))
    {
        return true;
    }
    return false;
}

function checkLoginStatus()
{
    //get logged in status
    //and setup login box accordingly
    //$('#login-conent').html('Loading...');
    executeJSONP('get_user');
}

//NOTE this function is called via a jsonp request in the ajax controller.  see method get_user for information
function loggedInCallback(status)
{
   //a lert(document.domain);
        var json = status;
        var html;
        //logged in
        if(json.status=="ok")
        {
            html = '<p><span>Hi, <strong>' + json.data.username +'</strong>.</span></p>';
            $('.login-smalltext').html('<a id ="edit-profile-popup" href="javascript:void(0);">Edit Profile</a> | <a href="javascript:void(0);" class="logout">Logout</a>');

        }
        else //not logged in
        {
            html = "<p><a id=\"login-button\" href=\"javascript:void(0);\">Log In</a></p>";
            $('.login-smalltext').html('... or <a class="sign-up" href="javascript:void(0);">sign up</a> now.');
            //ONCE THIS IS CALLED BACK
            //we will execute an abstract page method.  first check if defined, and if so, execute
            if(typeof postLogoutHook == 'function')
            {
                postLogoutHook();
            }
        }
        $('#login-speechbubble').html(html);
       
        //Now get cookie info and set it on this domain.
        executeJSONP('get_user_hash_data');

        //After cookies are set, call User()->set_current()
    
}

    
//this is the callback function for get_user_hash_data
//sets cookies for user on local site, and should call set_user (with a regular POST not JSONP request)
function set_user_info(data)
{
    var json = data;
    $.post('/ajax/set_user', json, function(response){
            //ONCE THIS IS CALLED BACK
            //we will execute an abstract page method.  first check if defined, and if so, execute
            if(typeof postLoginHook == 'function')
            {
                postLoginHook();
            }
    });
}


//Function to properly format jsonp request
//This function will run the JSONP script through the proxy host ONLY in firefox, as this is the only browser that will currently support our
//cross site login.  If not firefox, the function will only allow you to perform jsonp requests within the current domain.  In firefox,
//requests will go to the proxy (nnn.com)
function executeJSONP(fn, data)
{
    var browser_info = navigator.userAgent;
    if((browser_info.toLowerCase()).indexOf("firefox")!=-1)
    {
        //is firefox
        var url = PROXY_HOST + '/ajax/' + fn + '?format=json&jsoncallback=?'; 
    }
    else
    {
        //is not firefox
        var url = '/ajax/' + fn + '?format=json&jsoncallback=?'; 
    }
    if(data===undefined)
    {
        $.getJSON(url);
        //this function should have the callback function hardcoded in it. check the specific ajax method for the callback function name
    }
    else
    {
        $.getJSON(url, data);
    }
}

function checkLikes()
{
    //check if page is post page
    if ($('#postid').val() != '' && $('#postid').val() != undefined)
    {
        //check like
         $.post("/ajax/is_liked/" + $('#postid').val(), function(response){
           // alert(response);
           var json = eval('(' + response + ')');
           if (json.payload.favorite)
           {
                $('#like-this-video').html('Unlike');
           }
           else
           {
                $('#like-this-video').html('I like this!');
           }
        });
    }

    return;
}

