
var hash = { 
	prevLocation: false,
	currLocation: '',
	prevHash: false,
	currHash: window.location.hash,
	
	loading: false,
	hashLink : true,
	
	location: function(href) {
		if(href !== undefined && href !== null) location.href = href;
	},
	anchor: function()
	{
		var result2 = this.currHash.split('#/');

		if(typeof result2[1] !== 'undefined')
		{
			var result = result2[1].split('#');
			var anchor = result[1];
		
			return anchor;
		}
	},
	clicked: function()
	{
		this.loading = true;
	},
	isHashLink: function()
	{
		var linkType = this.hashLink;
		
		this.hashLink = true;
		
		return linkType;
	},
	current: function()
	{	
		//var hash = this.currLocation;
		var hash = window.location.href;
		
		if(hash.search("#\/") == -1 && this.prevHash.length > 0)
		{
			this.location('/');
		}
		
		if(!hash) return true;

		if(hash.search("#\/") == -1) 
		{
			var hash_result = hash.split('/');

			hash_result.shift();
			hash_result.shift();
			if(hash_result[1] == '') return true;
			this.hashLink = false;
			
			return true;
		}
		else
		{
			var hash_result = hash.split('#/');
		}

		hash_result.shift();
		
		if(typeof hash_result[0] === 'undefined') return false;

		hash_result = hash_result[0].split('/');

		return $.isArray(hash_result) ? hash_result : false;
	},
	previous: function() {
		return this.prevLocation == '' ? false : true;
	},
	previousHash: function() {
		var hash = this.prevLocation;
		
		if(hash == "") return false;
		
		if(hash.search("#\/") == -1) return true;
		
		var hash_result = hash.split('#/');
		
		if(typeof hash_result[1] === 'undefined') return false;
		
		hash_result = hash_result[1].split('/');
		
		return $.isArray(hash_result) ? hash_result : false;
	},
	exists: function(check_all_links) {
	
		if(check_all_links) 
		{
			var result = window.location.href.split('/');
			return ( (result[3] || this.currHash) && this.prevLocation === false) ? true : false;
		}
		
		return this.currHash ? true : false;
	},
	
	// Compare new and old hashes and return true if same and false if doesnt
	start: function(intIntervalTime)
	{
		$( window.location ).bind('change', function( objEvent, objData )
		{ 
			hash.change(objEvent, objData);
		});
		
		$( window ).hashchange( function() {
			hash.check();
		});
		
		setTimeout( function() { $(window).hashchange() }, intIntervalTime);
	},
	
	cleanHash: function(strHash)
	{
		return(strHash.substring( 1, strHash.length ));
	},
	splitUrl: function(url)
	{
		var hash_result = url.split('#/');
		if(typeof hash_result[1] === 'undefined') return false;
		hash_result = hash_result[1].split('/');
		
		return $.isArray(hash_result) ? hash_result : false;
	},
	change: function(objEvent, objData)
	{	
		var arrHash = hash.current();

		if(arrHash === true) return;

		var tHash2 = arrHash.join('/');

		Request(tHash2, {callback: function() 
		{
			$("a[href='/#/"+tHash2+"']").find(".menuitem").trigger('click', {originalEvent:false});
		} }, { abort: true } );
		
		this.loading = false
	},
	check: function()
	{
		this.prevLocation = this.currLocation;			

		if (this.currLocation != window.location.href)
		{
			this.currLocation = window.location.href;
			this.prevHash = this.currHash;
			this.currHash = window.location.hash;

			if(this.loading === true) 
			{
				this.loading = false;
				return false;
			}

			//$( window.location ).trigger("change", {currentHref: this.currLocation, currentHash: this.cleanHash( this.currHash ), previousHref: this.prevLocation, previousHash: this.cleanHash( this.prevHash )});
			$( window.location ).trigger("change", {currentHref: this.currLocation, currentHash: this.currHash, previousHref: this.prevLocation, previousHash: this.prevHash});
			return true;
		}

		this.loading = false;
	},
	
	getUrl: function()
	{
		return location.protocol + "//"+location.hostname;
	},
	start2: function(intIntervalTime)
	{
		$(window).hashchange( function() {
			var aHash = location.hash.split('#/');
			aHash.shift();		
			aHash = aHash[0].split('/');

			var strHash = "";

			$.each(aHash, function(item, value) {
				var aSHash = value.split('#');
				strHash += '/' + aSHash[0];
			});
		
			var $hashLink = $("[href='/#"+strHash+"']");

			if($hashLink.find(".menuitem").length > 0)
			{
				$hashLink.find(".menuitem").trigger('click',{originalEvent:false});
			}

			Request('app/main/main/'+aHash[aHash.length - 1], {abort: true,method:'get',append:false});
		});
		
		setTimeout( function() { $(window).hashchange() }, intIntervalTime);		
	},
	changeLinks: function(dest) 
	{
		if(dest && editor.exists() === false)
		{
			var url = this.getUrl();
			
			$(dest).find('a').not("[isHash=true]").each( function() 
			{	
				var href = $(this).attr('href');
				
				if(typeof href !== 'undefined')
				if( (href.substr(4,2) !== ":/" && 
					href.search("\/#\/") == -1 && 
					href.search("#") == -1 && 
					href.search("sites") == -1 &&
					href.search("files") == -1 &&
					href.search("images") == -1 &&
					href.substr(0,11) !== "javascript:" &&
					href.substr(0,6) !== "skype:" &&
					href.substr(0,7) !== "mailto:") || 
					(href.search(url) !== -1 && 
					 href.search("images") == -1 &&
					 href.search("\/#\/") == -1 &&
					 href.search("#") == -1 &&
					 href.search("sites") == -1 &&
					 href.search("files") == -1 ))
					{
					
						if(href.search(url) !== -1)
							$(this).attr('href', '/#'+href.replace(url,'')); // IE FIX
						else
							$(this).attr('href', '/#'+href);
							
						if(href == '') $(this).children().unwrap();
						/*
						if(href.substr(0,1) !== '/' || href == '')
						{
							if(window.location.href.substr(window.location.href.length - 1, 1) !== '/')
							{
								sSplit = '/';
								
								var aLocResult = window.location.href.split('#/');
								var aLocAddress = aLocResult[0].replace('http://','').split('/');
								aLocAddress.shift();
								aLocAddress.pop();
								if(aLocAddress.length > 0) sSplit = '/'+aLocAddress.join('/')+sSplit;
							}
							
							$(this).attr('href', sSplit+'#/'+href.replace(url,'')); // IE FIX
							
							if(href == '') $(this).children().unwrap();
						}*/
					}
			});
		}
		else
			editor.init();
	}
}

$(document).ready(function()
{
	hash.changeLinks(document);
	
	if(editor.exists() === false) hash.start(100);
});
