var instacheckinUrl = 'http://www.instacheckin.com/';
jQuery( function( $ ) {
    $.jsonp({
        url : "" + instacheckinUrl + "instapressNewBlogClient?callback=?",
        data : {
            fun : "getloginurl",
            callingpage : document.URL
        }, success : function( userProfile ) {
            document.getElementById( "userLogin" ).innerHTML = '';
            if( userProfile.url ) {
                document.getElementById( "userLogin" ).innerHTML = '<div class="fbSprite"><a href="javascript:void(0);" onclick="getLoginUrl();" class="fbLogin fbActive" title="signin with facebook" style="cursor:pointer"></a></div>';
                setCookie( "userName", "", -365 );
                setCookie( "userImageLink", "", -365 );
                setCookie( "userId", "", -365 );
                setCookie( "userSessionId", "", -365 );
            } else if( userProfile.logouturl ) {
                var userLogoutUrl = userProfile.logouturl; //logout url.
                var userName = userProfile.username; //username containing firstname + lastname.
                var userImageLink = userProfile.userimagelink; //user image link.
                var userInstacheckinUserId = userProfile.instacheckinuserid;
                var userInstacheckinSessionId = userProfile.instacheckinsessionid;
                //set cookies.
                if( checkCookie( 'userSessionId' ) != userInstacheckinSessionId ) {
                    setCookie( "userName", userName, 7 );
                    setCookie( "userImageLink", userImageLink, 7 );
                    setCookie( "userId", userInstacheckinUserId, 7 );
                    setCookie( "userSessionId", userInstacheckinSessionId, 7 );
                    document.location.href = document.URL;
                } else {
                    setCookie( "userName", userName, 7 );
                    setCookie( "userImageLink", userImageLink, 7 );
                    setCookie( "userId", userInstacheckinUserId, 7 );
                    setCookie( "userSessionId", userInstacheckinSessionId, 7 );
                    document.getElementById("userLogin").innerHTML = '<img class="usrImg" src="http://' + userImageLink + '" alt="' + userName + '" title="' + userName + '" width="50px" height="50px" /> <span class="nameLogout"><span class="txt">Hi ' + userName + ', </span> <img class="logoutImg" src="' + instacheckinUrl + 'images/logout.jpg" title="Logout" alt="Logout" style="cursor: pointer" onclick="logout()" /></span>';
                }
            }
        }, error : function( d, msg ) {
            //alert("error1 "+d['_id']+msg);
        }
    });
    var currnetPageUrl = document.URL;
    if( currnetPageUrl.indexOf( "#_=_", 0 ) != -1 ) {
        currnetPageUrl = currnetPageUrl.replace( "#_=_", "" );
        location.href = currnetPageUrl;
    }
});

function getLoginUrl() {
    $.jsonp({
        url : ""+instacheckinUrl+"instapressNewBlogClient?callback=?",
        data :{
            fun: "getloginurl",
            callingpage: document.URL
        }, success : function(userProfile) {
            if(userProfile.url)
            {
                //document.getElementById("userLogin").innerHTML = '<div class="fbSprite"><a href="'+userProfile.url+'" class="fbLogin fbActive" title="signin with facebook" style="cursor:pointer"></a></div>';
                setCookie("userName", "", -365 );
                setCookie("userImageLink", "", null, -365 );
                setCookie("userId", "", -365 );
                setCookie("userSessionId", "", -365 );
                document.location.href = userProfile.url;
            }
            else if( userProfile.logouturl ) {
                var userLogoutUrl = userProfile.logouturl; //logout url.
                var userName = userProfile.username; //username containing firstname + lastname.
                var userImageLink = userProfile.userimagelink; //user image link.
                var userInstacheckinUserId = userProfile.instacheckinuserid;
                var userInstacheckinSessionId = userProfile.instacheckinsessionid;
                //set cookies.
                if( checkCookie( 'userSessionId' ) != userInstacheckinSessionId ) {
                    setCookie( "userName", userName, 7 );
                    setCookie( "userImageLink", userImageLink, 7 );
                    setCookie( "userId", userInstacheckinUserId, 7 );
                    setCookie( "userSessionId", userInstacheckinSessionId, 7 );
                    document.location.href = document.URL;
                } else {
                    setCookie( "userName", userName, 7 );
                    setCookie( "userImageLink", userImageLink, 7 );
                    setCookie( "userId", userInstacheckinUserId, 7 );
                    setCookie( "userSessionId", userInstacheckinSessionId, 7 );
                    document.getElementById("userLogin").innerHTML = '<img class="usrImg" src="http://' + userImageLink + '" alt="' + userName + '" title="' + userName + '" width="50px" height="50px" /> <span class="nameLogout"><span class="txt">Hi ' + userName + ', </span> <img class="logoutImg" src="' + instacheckinUrl + 'images/logout.jpg" title="Logout" alt="Logout" style="cursor: pointer" onclick="logout()" /></span>';
                }
            }
        }, error : function( d, msg ) {
            //alert("error2 "+d['_id']+msg);
        }
    });
}

function logout() {
    setCookie("userName", "", -365 );
    setCookie("userImageLink", "", -365 );
    setCookie("userId", "", -365 );
    setCookie("userSessionId", "", -365 );
    $.jsonp({
        url : "" + instacheckinUrl + "logout/?callback=?",
        success : function (userProfile) {
            if (userProfile.mesg) {
                //window.location.href=window.location.href; //refersh page.
                //window.location.href=userProfile.mesg;
                $.jsonp({
                    "url": userProfile.mesg,

                    "success": function (userProfile) {

                    },
                    "error": function (d, msg) {
                        //alert("error "+d['_id']);
                    }
                });
            }
            document.location.href = document.URL; //refersh page.
        }, error : function (d, msg) {
            //alert("error3 "+d['_id']);
        }
    });
}

function setCookie( c_name, value, exdays ) {
    var exdate = new Date();
    exdate.setDate( exdate.getDate() + exdays );
    var hostKeTukde = document.location.host.split('.');
    var domain = '.' + hostKeTukde[ hostKeTukde.length - 2 ] + '.' + hostKeTukde[ hostKeTukde.length - 1 ];
    var c_value = escape( value ) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString()) + "; path=/" + ";domain=" + domain;
    document.cookie = c_name + "=" + c_value;
}

function getCookie( c_name ) {
    var i, x, y, ARRcookies = document.cookie.split( ";" );
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[ i ].substr(0, ARRcookies[ i ].indexOf( "=" ) );
        y = ARRcookies[ i ].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
}

function checkCookie(cookieName) {
    var username = getCookie(cookieName);
    return username;
}
