$j(document).ready(function() {


    // $j("<div style='display: none'><p>Hello 1</p></div>").appendTo("body")

    /*
    $j('#ka_videoTab').remove();
    $j('span', $j('#ka_photoTab_nav')).text("work");
    $j('#ka_photoTab_nav').attr({
    href: "/service/searchEverything.kickAction?as=126249&mediaType=photo,video&sortType=recent&tab=yes&includePhoto=on&includeVideo=on"
    });
    */

    // rename all instances of photos to images 

    $j('span', $j('#ka_photoTab_nav')).text("images");
    $j('#ka_headerSubNav_list li.ka_addMedia_photo a').text("add an image");
    $j('li#ka_subMyPhoto a').text("my images");



    // Use this function to check all advanced search boxes (they are still hidden)

    function checkSearchBoxes() {
        $j("#includeVideo").attr('checked', true);
        $j("#includePhoto").attr('checked', true);
        $j("#includeBlog").attr('checked', true);
        $j("#includeMessages").attr('checked', true);
        $j("#includeUser").attr('checked', true);
    }

    checkSearchBoxes();

    if (Ka.Info.PAGE == 'pages/myPlace.jsp') {
        $j("h2#ka_myHome").text("Your Home");
        // $j("h3#ka_newPhoto_header").text("Recent Work");
        $j("#ka_userModule").insertAfter("#ka_newestModule");
        $j("#ka_newestModulePhoto").insertBefore("#ka_newestModule");

    } else if (Ka.Info.PAGE == 'pages/kickPlaceServerSide.jsp') {

        // $j("<div style='display: none'><p>Hello 2</p></div>").appendTo("body")


        /*
        $j('.ka_profileSegHeader h5').each(function(){
        if($j(this).text('About Me')){
        $j(this).text("About"));
        }
        });
        */


        $j('#ka_profileActivityFeed').remove();
        $j('h5', $j('#ka_profilePhotos')).text("Work");
        $j('.ka_profileMediaToggle', $j('#ka_profilePhotos')).remove();


        // Swap small images for big ones
        i = 0;

        while (i < $j('span', $j('#ka_profilePhotos')).length) {
            var string = $j('span', $j('#ka_profilePhotos')).eq(i).attr('style').replace("48X48", "160X120");
            $j('span', $j('#ka_profilePhotos')).eq(i).attr('style', string);
            i++;
        }

        i = 0;

        while (i < $j('span', $j('#ka_profileLatestMedia')).length) {
            //alert("Hello");
            var string = $j('span', $j('#ka_profileLatestMedia')).eq(i).attr('style').replace("48X48", "160X120");
            $j('span', $j('#ka_profileLatestMedia')).eq(i).attr('style', string);
            i++;
        }
        // ka_profileAbout

        $j('#ka_profilePhotos').prependTo('#ka_profileAbout');
        //$j('.ka_profileSegHeader', $j('#ka_profileVideos')).remove();

        i = 0;
        if ($j('#ka_profilePhotos').length > 0) { // added by RG to check if the person has photos on their profile

            while (i < $j('span', $j('.ka_profileMediaList', $j('#ka_profileVideos'))).length) {
                // $j("<div style='display: none'><p>Hello 4</p></div>").appendTo("body")
                var string = $j('span', $j('.ka_profileMediaList', $j('#ka_profileVideos'))).eq(i).attr('style').replace("48X48", "160X120");
                $j('span', $j('.ka_profileMediaList', $j('#ka_profileVideos'))).eq(i).attr('style', string);
                $j('li', $j('.ka_profileMediaList', $j('#ka_profileVideos'))).eq(i).appendTo($j('.ka_profileMediaList', $j('#ka_profilePhotos')));
                i++;
            }
            // Once complete tidy the page up so there are no odd spaces
            $j('#ka_profileVideos').remove();
        }
        else { // The user does not have photos, therefor we can't use.appendTo to add to a division that does not exist
            while (i < $j('span', $j('.ka_profileMediaList', $j('#ka_profileVideos'))).length) {
                var string = $j('span', $j('.ka_profileMediaList', $j('#ka_profileVideos'))).eq(i).attr('style').replace("48X48", "160X120");
                $j('span', $j('.ka_profileMediaList', $j('#ka_profileVideos'))).eq(i).attr('style', string);
                $j('li', $j('.ka_profileMediaList', $j('#ka_profileVideos'))).eq(i).appendTo($j('.ka_profileMediaList', $j('#ka_profilePhotos')));
                i++;
            }
            $j("#ka_profileVideos").insertBefore("#ka_profileAbout");
            $j('h5', $j('#ka_profileVideos')).text("Work");
        }

        if ($j('h5', $j('#ka_profilePhotos')).length != 0) {
            $j('h5', $j('#ka_profilePhotos')).append('<div class="ka_profileMediaToggle"><a href="/service/searchEverything.kickAction?as=126249&u=' + Ka.Info.PROFILEID + '&sortType=recent&tab=yes&includePhoto=on&includeVideo=on&includeAudio=on">view all</a></div>');
        } else {
            $j('h5', $j('#ka_profileVideos')).append('<div class="ka_profileMediaToggle"><a href="/service/searchEverything.kickAction?as=126249&u=' + Ka.Info.PROFILEID + '&sortType=recent&tab=yes&includePhoto=on&includeVideo=on&includeAudio=on">view all</a></div>');
        }

        //alert(Ka.Info.PROFILENAME);
        $j("#ka_profileLeft").append("<div id='cs_showcaseLink'><a href='http://www.thisiscentralstation.com/showcase/user.aspx?user=" + Ka.Info.PROFILENAME + "'>View my Showcase</a></div>");

        $j("#ka_profileFriends").insertAfter("ul#ka_profileUserInfo");
        $j("div#ka_profileFans").insertAfter("div#ka_profileFriends");

    } else if (Ka.Info.PAGE == 'pages/mediaPlayPage.jsp') {

        $j('#ka_playPageStats').before('<div class="ka_contentBody" id="censta_description"></div>');
        $j('#ka_descriptionText').prependTo('#censta_description');
        $j('#ka_playPageDetails').after('<div class="ka_contentBody" id="censta_userrssfeed"></div>');
        $j('#ka_relatedWrap').append('<div class="ka_contentBody" id="censta_rssimages"></div>');
        $j('#ka_relatedWrap').remove();

        var rssFeedToLoad = "";
        $j('.ka_rssLink_user').each(function(i) {
            rssFeedToLoad = $j('a', this).attr("href");
        });
        if (rssFeedToLoad != "") {
            // Add RSS Reed to the page
            jQuery.getFeed({
                url: rssFeedToLoad,
                success: function(feed) {
                    jQuery('#censta_userrssfeed').append('<b>'
                    /*+ '<a href="'
                    + feed.link
                    + '">'*/
        + 'More ' + Ka.Info.MEDIATYPE.toLowerCase() + 's by ' + Ka.Info.Media.CONTRIBUTER
                    /* + '</a>'*/
        + '</b>');

                    var html = '';

                    for (var i = 0; i < feed.items.length && i < 5; i++) {

                        var item = feed.items[i];
                        html += '<div style="clear:both;">';

                        html += '<img src="' + item.thumnails[0].url + '" />';

                        html += '<h3>' + '<a href="' + item.link + '">' + item.title + '</a>' + '</h3>';

                        html += '</div>';
                    }

                    jQuery('#censta_userrssfeed').append(html);
                }
            });
        }
        $j("div#ka_descriptionText").insertAfter("div#ka_playPagePlayer");

    } else if (Ka.Info.PAGE == "search/searchPage.jsp") {

        for (var i = 0; i < queryArr.length; i++) {
            if (queryArr[i] == "mediaType=photo") {

                $j('#ka_contentContainer').before('<div id="censta_featuredcontent"></div>');


                if (rssFeedToLoad != "") {
                    // Add RSS Reed to the page
                    jQuery.getFeed({
                        url: 'http://affiliate.kickapps.com/service/getFeed.kickAction?as=' + Ka.Info.AFFILIATESITEID + '&mediaType=photo&tab=yes&includePhoto=on&d-7095067-p=1&quantity=5&adminTags=featured',
                        success: function(feed) {
                            jQuery('#censta_featuredcontent').append('<h3>Featured</h3>');

                            var html = '';

                            for (var i = 0; i < feed.items.length && i < 5; i++) {

                                var item = feed.items[i];
                                html += '<div style="clear:both;">';

                                html += '<a href="' + item.link + '"><img src="' + item.thumnails[0].url + '" /></a>';

                                html += '<h3>' + '<a href="' + item.link + '">' + item.title + '</a>' + '</h3>';

                                html += '</div>';

                            }

                            jQuery('#censta_featuredcontent').append(html);
                        }
                    });
                }

            }
        }


        for (var i = 0; i < queryArr.length; i++) {
            //alert(queryArr[i].substring(0, 9));
            if (queryArr[i].substring(0, 9) == "mediaType") {
                // If we're on the Images and video page
                if (queryArr[i].substring(10, queryArr[i].length) == "photo,video") {
                    $j("h3", $j("#ka_contentContainer")).text("Work");
                    $j("#ka_photoTab").addClass("current");
                    $j("#ka_headerSubNav li").remove();
                    $j("#ka_headerSubNav ul").append('<li class="ka_addMedia ka_addMedia_video ka_sprite_media"><a href="/view/displayManageMyMedia.kickAction?as=126249">add a work</a></li>');
                    //alert(queryArr[i].substring(10, queryArr[i].length));
                } else if (queryArr[i].substring(10, queryArr[i].length) == "user") {
                    //$j("h3", $j("#ka_contentContainer")).text("Members");
                    var membersMenu = "<div id='cs_membertags' class=''>";
                    //alert("Member Search");
                    membersMenu += "<div id='left_sub1''><ul><li>Tags<ul id='cs_tagslist'>";

                    membersMenu += "</ul></li></ul></div>";
                    membersMenu += "</div>";
                    $j("#ka_contentContainer").prepend(membersMenu);



                    jQuery.getFeed({
                        url: "/service/searchEverythingAsRss.kickAction?as=126249&mediaType=tags&includeTags=on&quantity=25",
                        success: function(feed) {
                            var html = '';
                            var currentTag = "";
                            for (var i = 0; i < queryArr.length; i++) {
                                if (queryArr[i].substring(0, 8) == "keywords") {
                                    currentTag = queryArr[i].substring(9, queryArr[i].length);
                                }
                            }
                            //alert(urldecode(currentTag));
                            for (var i = 0; i < feed.items.length; i++) {
                                var item = feed.items[i];
                                //alert(item.title);
                                var currentClass = (item.title == urldecode(currentTag)) ? "current" : "";
                                html += '<li class="' + currentClass + '">' + '<a href="/service/searchEverything.kickAction?as=126249&mediaType=user&includeUser=on&keywords=' + item.title + '">' + item.title + '</a>' + '</li>';
                            }

                            jQuery('#cs_tagslist').append(html);
                        }
                    });
                }
            }
            if (queryArr[i].substring(0, 8) == "keywords") {
                // on search page
                //alert("search page");
                $j("#ka_headerSubNav li").remove();
            }
        }
    } else if (Ka.Info.PAGE == 'pages/forum.jsp') {

    } else if (Ka.Info.PAGE == "pages/manageMyMedia.jsp"
        || Ka.Info.PAGE == "pages/commentDetails.jsp"
        || Ka.Info.PAGE == "pages/manageVideo.jsp"
        || Ka.Info.PAGE == "pages/managePhoto.jsp"
        || Ka.Info.PAGE == "pages/manageBlog.jsp"
        || Ka.Info.PAGE == "pages/manageFavorites.jsp"
        || Ka.Info.PAGE == "pages/myWidgets.jsp") {
        $j("a", $j("#ka_mkpMyPhotos")).text("My Images");
    }

    switch (Ka.Info.PAGE) {
        case "pages/manageProfileQuestions.jsp":
        case "pages/manageAccountSettings.jsp":
        case "pages/manageRSS.jsp":
        case "pages/manageLinks.jsp":
            $j("#ka_mkpEditProfile").after($j("#ka_mkpEditAccount"));
            $j("a", $j("#ka_mkpEditProfile")).text("Edit Profile 1");
            $j("span", $j("#ka_mkpEditProfile")).text("Edit Profile 1");
            $j("a", $j("#ka_mkpEditAccount")).text("Edit Profile 3");
            $j("span", $j("#ka_mkpEditAccount")).text("Edit Profile 3");
            $j("#ka_mkpEditProfile").after('<li id="umb_EditProfile"><a href="http://www.thisiscentralstation.com/UpdateMemberDetails.aspx">Edit Profile 2</a></li>');
            break;
    }

    relabelMenu();
    function relabelMenu() {
        // $j("#ka_myhomeTab a span").text("my community");
        // $j("#ka_messageBoardsTab a span").text("boards");
    }
    // If we're on a play page

    if (Ka.Info.PAGE == 'pages/mediaPlayPage.jsp') {
        //alert("we're on play page");

        // swap comments div order
        //$j("#ka_shoutBoxArea").insertAfter("#ka_commentLog"); // check after() examples
    }
    $j("#ka_becomeAMember a:eq(2)").css("display", "none");
});

function urldecode(str) {
    // Decodes URL-encoded string  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/urldecode
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +   improved by: Orlando
    // %        note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin van Zonneveld!'
    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // *     returns 2: 'http://kevin.vanzonneveld.net/'
    // *     example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'

    var hash_map = {}, ret = str.toString(), unicodeStr = '', hexEscStr = '';

    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };

    // The hash_map is identical to the one in urlencode.
    hash_map["'"] = '%27';
    hash_map['('] = '%28';
    hash_map[')'] = '%29';
    hash_map['*'] = '%2A';
    hash_map['~'] = '%7E';
    hash_map['!'] = '%21';
    hash_map['%20'] = '+';
    hash_map['\u00DC'] = '%DC';
    hash_map['\u00FC'] = '%FC';
    hash_map['\u00C4'] = '%D4';
    hash_map['\u00E4'] = '%E4';
    hash_map['\u00D6'] = '%D6';
    hash_map['\u00F6'] = '%F6';
    hash_map['\u00DF'] = '%DF';
    hash_map['\u20AC'] = '%80';
    hash_map['\u0081'] = '%81';
    hash_map['\u201A'] = '%82';
    hash_map['\u0192'] = '%83';
    hash_map['\u201E'] = '%84';
    hash_map['\u2026'] = '%85';
    hash_map['\u2020'] = '%86';
    hash_map['\u2021'] = '%87';
    hash_map['\u02C6'] = '%88';
    hash_map['\u2030'] = '%89';
    hash_map['\u0160'] = '%8A';
    hash_map['\u2039'] = '%8B';
    hash_map['\u0152'] = '%8C';
    hash_map['\u008D'] = '%8D';
    hash_map['\u017D'] = '%8E';
    hash_map['\u008F'] = '%8F';
    hash_map['\u0090'] = '%90';
    hash_map['\u2018'] = '%91';
    hash_map['\u2019'] = '%92';
    hash_map['\u201C'] = '%93';
    hash_map['\u201D'] = '%94';
    hash_map['\u2022'] = '%95';
    hash_map['\u2013'] = '%96';
    hash_map['\u2014'] = '%97';
    hash_map['\u02DC'] = '%98';
    hash_map['\u2122'] = '%99';
    hash_map['\u0161'] = '%9A';
    hash_map['\u203A'] = '%9B';
    hash_map['\u0153'] = '%9C';
    hash_map['\u009D'] = '%9D';
    hash_map['\u017E'] = '%9E';
    hash_map['\u0178'] = '%9F';
    hash_map['\u00C6'] = '%C3%86';
    hash_map['\u00D8'] = '%C3%98';
    hash_map['\u00C5'] = '%C3%85';

    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr]; // Switch order when decoding
        ret = replacer(hexEscStr, unicodeStr, ret); // Custom replace. No regexing
    }

    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);

    return ret;
}
