/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


function synCtrl(){
    version  = '0.1';

    output   = '';
    base     = '';

    prefix   = '';
    suffix   = '/ajax/1';

    marker   = '#';
    anchor   = '';
    markerc  = '';

    h_detect = false;
    init     = false;

    var link = document.location.toString();

    if(link){
        for(var i=0; i<marker.length; i++){
            markerc += '\\' + marker.substr(i,1);
        }
        
        eval('var regex = /(http[s]*\:\\/\\/[^\\/]+)[\\/]*[^\\#]+' + markerc + '(.*)/;');

        regex.exec(link);

        base = RegExp.$1;
        
        //alert("base: " + base);

        if(!base || base == '/'){
            base = document.location.toString();
        }

        if(base.substr(-1,1) != '/'){
            base += '/';
        }

       // alert("init base: " + base);
        anchor = RegExp.$2;

        if(anchor.length) h_detect = true;

        if(anchor.substr(0,1) != '/'){
            anchor = '/' + anchor;
        }

        init = true;
    }
}

with(synCtrl){
    prototype.info = function(){
        return version;
    }

    prototype.assemble = function(parts, skip_suffix){
        if(typeof(parts) != 'object'){
            return;
        }

        if(typeof(skip_suffix) != 'boolean'){
            skip_suffix = false;
        }

        var link = '';

        for(var part_idx in parts){
            if(part_idx == 0){
                link += parts[part_idx];

            }else{
                if(link.substr(-1,1) != '/'){
                    link += '/';
                }

                if(parts[part_idx].substr(0,1) == '/'){
                    link += parts[part_idx].substr(1,parts[part_idx].length-1);
                }else{
                    link += parts[part_idx];
                }
            }
        }

        if(!skip_suffix && suffix){
            link += suffix;
        }

        return link;
    }

    prototype.startup = function(){
        if(h_detect){
           var tmp = synCtrl.prototype.assemble([base,anchor], true);
           window.location.href = tmp;
        }

        return h_detect;
    }

    prototype.observe = function(){
        $(window).hashchange(synCtrl.prototype.handle);
    }

    prototype.handle = function(){
        if(!document.location.hash.length){
            return;
        }

        eval('var regex = /' + markerc + '(.+)/;');

        regex.exec(document.location.hash);

        anchor = RegExp.$1;

        if(!anchor.length){
            return;
        }

/*
        alert(suffix);
        alert(base);
        alert(anchor);
            */
       var tmp = synCtrl.prototype.assemble([base,anchor]);
       //alert(tmp);
       $(output).load(tmp);
    }

    prototype.transform = function(){
        //$(window).hashchange(synCtrl.prototype.assemble);//
    }
}



/*
 *
 *
     prototype.startup=function(){
        if(document.location.hash.length){
            eval('var regex = /' + this.marker + '(.+)/;');

            regex.exec(document.location.hash);

            this.hashtag = RegExp.$1;
        }
    }
****


http://www.howtocreate.co.uk/tutorials/javascript/objects


http://www.phpied.com/3-ways-to-define-a-javascript-class/
    if(myFile.match('#!')){
        window.location.href = getLinkPrefix() + getHashTag() + link_suffix;
    }


    if (myFile.match('#')){
        var regex = /(http[s]*\:\/\/[^\/]+)[^\#]+\#\!(.+)/;

        regex.exec(myFile);

        var target = RegExp.$1;
        
    }
}
*/

/*
var link = new function (){
    this.prefix = document.location.toString();
    this.suffix = '/ajax/1';
}
*/

// jQuery.observeHashChange({interval: 1000});
