window.pemco = window.pemco || {};

var objRequest = createRequest();

function createRequest() {
    var obj;
    var browser = navigator.appName;
    if (browser == "Microsoft Internet Explorer") {
        obj = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        obj = new XMLHttpRequest();
    }
    return obj;
}

/***********************************************************
* login
***********************************************************/

function lightboxLoadContent(strLightboxName, height, width, full_link) {
    var objElement = document.getElementById('lightbox');

    if (objElement != null) {

        //Positioning Login Box Location and Width
        if (strLightboxName == "User Login") {
            //objElement.style.top = '75px';
            //objElement.style.right = '150px';
        }
        else if (strLightboxName == "User Registration") {
            //objElement.style.top = '75px';
            //objElement.style.right = '150px';
        }
    }

    //Convert Link
    if (full_link.toString().indexOf("?") > 0) {
        full_link += "&title=" + strLightboxName;
    }
    else {
        full_link += "?title=" + strLightboxName;
    }

    //Submit Request
    objRequest.open('get', full_link);
    objRequest.onreadystatechange = lightboxLoadContentResponse;
    objRequest.send(null);
}

function lightboxLoadContentResponse() {
    if (objRequest.readyState == 4) {
        // this is the content of the called page
        var response = objRequest.responseText;

        //process the response
        if (response) {
            var objElement = document.getElementById('lightbox');
            if (objElement != null) {
                $('#lightbox').html(response.toString());
                //objElement.innerHTML = response.toString();
            }
        }
    }
}

/*
Connection between ASP scripts and PEMCO widgets
*/
(function($) {
    var pub = pemco.ajax = {};

    var setting = {};
    setting.url = 'ajax/default.aspx';          //This link will change
    setting.query = '';
    setting.timeout = 30000;
    setting.transSpeed = 500;
    setting.flashTimeout = 5000;

    var flashTimer;
    pub.flash = function(message, type, time) {
        //message stuff
        //use a better handler
        if (type == "Oops!") {
            $('#flash-message').html(type + ' ' + message);
        }
        else {
            $('#flash-message').html((type || 'success') + ': ' + message);
        }
        $('#flash-container').fadeIn(setting.transSpeed, function() {
            var $this = $(this);
            clearTimeout(flashTimer);
            flashTimer = setTimeout(function() {
                $this.fadeOut(setting.transSpeed)
            }, time || setting.flashTimeout);
        });
        pemco.bbq.loadComplete();
    }

    var errorMessage = function(xhr, errorType) {
        switch (xhr.status) {
            case 404:
                pub.flash('ajax not found');
                break;
            default:
                pub.flash(errorType, 'Error');
                break;
        }
    }

    var execute = function(type, data, fn) {
        // Allows for no data paramater

        if ($.isFunction(data) && fn === undefined) {
            fn = data;
            data = {};
            for (var key in fn) if (key != 'prototype') data[key] = fn[key];
        }

        $.ajax({
            type: type,
            url: setting.url + "?" + setting.query,
            data: data,
            success: function(response, status) {
                if (response.message) {
                    pub.flash(response.message);
                }

                fn.call(this, response);
            },
            error: errorMessage,
            /*
            complete: !window.console?false:function(xhr) {
            try {
            eval(xhr.responseText);
            } catch (e) {
            console.dir(e);
            }
            },
            */
            dataType: 'json',
            timeout: setting.timeout
        });

        setting.query = '';
    }



    pub.createAccount = function(data, fn) {
        data.action = 'create_account';

        execute('post', data, fn);
    }

    pub.deleteUserProfile = function(data, fn) {
        data.action = 'delete_user_profile';

        execute('post', data, fn);
    }

    pub.getCarouselSidebar = function(data, fn) {
        data.action = 'get_carousel_sidebar';

        execute('get', data, fn);
    }

    pub.getMiniProfiles = function(data, fn) {
        data.action = 'get_mini_profiles';

        execute('get', data, fn);
    }

    pub.getMonthEvents = function(data, fn) {
        data.action = 'get_month_events';

        execute('get', data, fn);
    }

    pub.getPemcoProfiles = function(data, fn) {
        data.action = 'get_pemco_profiles';

        execute('get', data, fn);
    }

    pub.getProfileCards = function(data, fn) {
        data.action = 'get_profile_cards';

        execute('get', data, fn);
    }

    pub.getPemcoProfileCommercials = function(data, fn) {
        data.action = 'get_pemco_profile_commercials';

        execute('get', data, fn);
    }

    pub.getPemcoProfileLinks = function(data, fn) {
        //data.action = 'get_pemco_profile_links';

        //execute('get', data, fn);
    }

    pub.getPemcoProfileCopy = function(data, fn) {
        data.action = 'get_pemco_profile_copy';

        execute('get', data, fn);
    }

    pub.getPemcoProfileCommercial = function(data, fn) {
        data.action = 'get_pemco_profile_commercial';

        execute('get', data, fn);
    }

    /*pub.getProfileShirt = function(data, fn) {
    data.action = 'get_profile_shirt';

        execute('get', data, fn);
    }*/

    pub.getTwitterTimeline = function(data, fn) {
        data.action = 'get_twitter_timeline';

        execute('get', data, fn);
    }

    pub.getTwitterFollowers = function(data, fn) {
        data.action = 'get_twitter_followers';

        execute('get', data, fn);
    }

    /*pub.getUserAccount = function(data, fn) {
    data.action = 'get_user_account';

        execute('get', data, fn);
    }*/

    pub.getUserProfile = function(data, fn) {
        data.action = 'get_user_profile';

        execute('get', data, fn);
    }

    pub.likeProfile = function(data, fn) {
        data.action = 'like_user_profiles';

        execute('post', data, fn);
    }

    pub.login = function(data, fn) {
        data.action = 'login';

        execute('post', data, fn);
    }

    pub.logout = function(data, fn) {
        data.action = 'logout';

        execute('post', data, fn);
    }

    pub.saveUserProfile = function(data, fn) {
        data.action = 'save_user_profile';

        execute('post', data, fn);
    }

    pub.spottedProfilesSearch = function(data, fn) {
        data.action = 'spotted_profiles_search';

        execute('get', data, fn);
    }

    pub.suggestEvent = function(data, fn) {
        setting.query = "action=suggest_an_event";
        data.action = 'suggest_an_event';

        execute('post', data, fn);
    }

    pub.updateAccount = function(data, fn) {
        data.action = 'update_account';

        execute('post', data, fn);
    }

    pub.uploadUserProfilePhoto = function(data, fn) {
        data.action = 'upload_user_profile_photo';

        execute('post', data, fn);
    }

    pub.newSpottedProfile = function(data, fn) {
        data.action = 'new_spotted_profile';

        execute('get', data, fn);
    }

})(jQuery);
