$(function(){
    window.defaultState = {};
    window.partsToLoad = new Array('title', 'keywords', 'description');


    window.defaultContentParser = function (){
         var size = partsToLoad.length;
        for (var i=0; i < size; i++) {

            switch(partsToLoad[i]){
                case 'keywords':
                case 'description':
                    defaultState[partsToLoad[i]] = $('meta[name="'+partsToLoad[i]+'"]').attr('content');
                    break;
                default:
                    defaultState[partsToLoad[i]] = $(""+partsToLoad[i]+"").html();
            } // switch

        }

    };

    /// load certain parts of the newly generated html
    window.partialContentLoader = function(html){
        var size = partsToLoad.length;
        for (var i=0; i < size; i++) {

           switch(partsToLoad[i]){
            case 'title':
                var matches = html.match(/<title>(.*?)<\/title>/);

                if (matches && matches.length > 1) {
                    var spUrlTitle = matches[1];
                    $(document).attr('title', spUrlTitle);
                }

                break;
            case 'keywords':

                var find = html.match(/<meta name=\"keywords\" content=\"(.*?)\"/);

                if (find && find.length > 1){
                    $('meta[name="keywords"]').attr('content', find[1])
                }
                break;
            case 'description':

                var find = html.match(/<meta name=\"description\" content=\"(.*?)\"/);

                if (find && find.length > 1){
                    $('meta[name="description"]').attr('content', find[1])
                }

                break;
            default:
                $(""+partsToLoad[i]+"").html($(html).find(partsToLoad[i]).html());
           }
        }

    };


   window.restoreDefaults = function(){
       var size = partsToLoad.length;
       for (var i=0; i < size; i++) {
            switch(partsToLoad[i]){
                case 'title':
                    $(document).attr('title', defaultState['title']);
                    break;
                case 'keywords':
                case 'description':
                    $('meta[name="'+partsToLoad[i]+'"]').attr('content', defaultState[""+partsToLoad[i]+""])
                    break;
                default:
                    $(""+partsToLoad[i]+"").html(defaultState[""+partsToLoad[i]+""]);
           }

        }

   }
   //parse the default values
   defaultContentParser();

});
