/* Bookmarks
---------------------------------------------------------------- */

var bookmarks = function(){
	// Variable: The last hash
	var lasthash = '';
	// Variable: Is Internet Explorer?
	var isie = false;
	// Variable: If Internet Explorer, then setup counter
	var iec = 0;
	// Variable: Internet Explorer version
	var iev = 0;
	// Array: Initialize array to store pages and hashtags in
	var bookmarked = new Array();


	return {
		// Call this function onload
		initialize:function(){
			// Variable: Is the document in quirks mode or not?
			var quirks = document.compatMode;
			// If the browser is Internet Explorer
			if(document.all){
				// Find the current version of Internet Explorer
				if(/MSIE (\d+\.\d+);/.test(navigator.userAgent)){iev = new Number(RegExp.$1);}

				// If the browser is Internet Explorer 8 or higher and in BackCompat mode
				// OR if the browser is Internet Explorer below version 8 (v. 7 or 6 that is).
				if(iev>=8 && quirks=='BackCompat' || iev<8){
					// Call the iframe function
					bookmarks.iframe();
					// Variable: The browser is Internet Explorer
					isie = true;
				}
			}
			// Initialize a timer that checks the browsers location hash
			setInterval("bookmarks.checkhash();", 400);
		},
		// This function is called from the ajax function - ajax.load(...)
		sethash:function(hash){
		
			// If a hash exists
			if(hash){
				// If the browser is Internet Explorer,
				// then add 1 to the counter
				if(isie){iec++;}
				// Create a string
				var str = hash + ',' + iec;
				// Reset the variable
				var num = '';
				// Reset the variable
				var partof = false;
				// Set last has to the current hash
				lasthash = hash;
				// Set the windows location href to the current hash

				window.location.href = hash;



				// Loop through our array of stored pages
				for(var i=0;i<bookmarked.length;i++){
					// Split the string
					var tmp = bookmarked[i].split(",");
					// Compare the hashtag in the array with the current hashtag
					if(tmp[0]==hash){
						// Variable: It already exists in our array, set to true
						partof = true;
						// Remember the pages number (only used if the browser is Internet Explorer)
						num = tmp[1];
					}
				}
				// If the browser is Internet Explorer
				if(isie){


					// If we did not find the hashtag in the array
					if(!partof){
						// THEN: Create a new page in the hidden iframe
						bookmarks.setiframe(hash,iec);
					}else{
						// OTHERWISE: Call a existing page in the hidden iframe
						bookmarks.setiframe(hash,num);
					}
				}
				// If we did not find the hashtag in the array
				// then add the string to the array
///				alert(bookmarked);


				if(!partof)
				{
		
					bookmarked.push(str);
//					alert('bookmarked');
				}
			}
		},
		// This function is called every 400 miliseconds
		checkhash:function(){
			// Variable: Get windows location hash
			var obj = window.location.hash;

			p=q2o(obj.replace('#',''));
			obj='#'+ajx2q(p);	

			
			// Variables: 
			// phas: the hash
			var phas;
			// If the browser has a valid hashtag
			if(obj){
				// If the current hashtag in different from the last hashtag
				// That is: if we have pushed the back / forward buttons
				if(obj!=lasthash){
					// If the last hashtag is different from undefined
					if(lasthash!=undefined){
						// Loop through our array of stored pages
						for(var i=0;i<bookmarked.length;i++){
							// Split the string
							var tmp = bookmarked[i].split(",");
							// If we find the browsers current hashtag in the array

///							alert(tmp[0]+"\n"+obj+"\n"+(tmp[0]==obj)+"\n"+bookmarked.length);
							if(tmp[0]==obj){
								// Set variables
								phas = tmp[0];
////								alert(phas);
								// Break the loop
								break;
							}
						}
						// If all the variables contains values
						if(phas){
							// Set the last hash to the newly found hashtag
							lasthash = phas;
							// Call the ajax function
							gsearch(phas, 1);
						}
					}
				}
			}
		},
		// If the browser is Internet Explorer, then we will store
		// the page changes / hash changes in a hidden iframe.
		iframe:function(){
			// Create iframe
			var bug = document.createElement("iframe");
			// Use this page
			bug.src = 'blank.html';
			// The iframes id
			bug.id = 'bugframe';
			// The iframe width
			bug.style.width = '1px';
			// The iframe height
			bug.style.height = '1px';
			// Dont display it
//			bug.style.display = 'none';
			// Append it to the body tag
			document.body.appendChild(bug);
		},
		// Update the source of the iframe
		setiframe:function(f,num){
			document.getElementById('bugframe').src = 'blank.html?' + num + f;
		},
		// This function is called from "blank.html".
		// The function updates the browsers location hash
		fixiframe:function(f){
			// Variable: Get windows location hash
			var obj = window.location.hash;
			// If the hashtag from the iframe exists
			// AND the hashtag is different from the
			// windows current hashtag, then update it
			if(f){if(f!=obj){window.location.href = f;}}
		}
	};
}();


