
window.dhtmlHistory.create({ 
	debugMode : false, 
	toJSON: function(o) { 
		return $.toJSON(o); 
	} 
	, fromJSON: function(s) { 
		return $.evalJSON(s); 
	}
});


function initRSH(){
	dhtmlHistory.initialize();
	dhtmlHistory.addListener(historyChange);
	if(location.hash != ''){
		executeHash(location.hash.substr(1));
	}
	
}

function executeHash(hash){

	//alert(hash);
	var hash_s = hash;
	
	var hash_ar = hash_s.split("/");
	
	for( var i=0, len=hash_ar.length; i<len; ++i)
	{
		if(hash_ar[i].toString().indexOf('VideoID') != -1)
		{
			var VideoID = hash_ar[i].replace(/VideoID\=/, "");
			loadVideo(VideoID,'custom',0);
		}
		if(hash_ar[i].toString().indexOf('carousel') != -1)
		{
			var carousel = hash_ar[i].replace(/carousel\=/, "");
			//showCarousel(carousel,0);
		}
		if(hash_ar[i].toString().indexOf('videotab') != -1)
		{
			var videotab = hash_ar[i].replace(/videotab\=/, "");
			//videoTabShow(videotab,0);
		}
	}

}

function historyChange(newLocation, historyData)
{

	// need to look at new location and if the following are set, activate them: VideoID, carousel, videoTab

	// might be better to just do handleURL or something, newLocation is everything after the hash
	executeHash(newLocation);

}

function MakeHash(key,value)
{
// takes the current hash, if there is one, and updates it with a new key/value
var newHash_ar = new Array();
//alert(currentHash.length);

// hash already created
if(location.hash.length > 0){

	var currentHash = location.hash.replace('#','');
	var hash_ar = currentHash.split("/");
	
	// for each element in the hash hash
	for( var i=0, len=hash_ar.length; i<len; ++i)
	{
		if(hash_ar[i].toString().indexOf(key) != -1)
		{
		// found the item we're setting, don't pass it in to the new array
		//newHash_ar.push(key+'='+value);
		}
		else
		{
		// didnt match on this item, pass it to the new array
		 newHash_ar.push(hash_ar[i]);
		}
	}
	// push the new key/val on to the end of the array
	newHash_ar.push(key+'='+value);

}
// no hash created, just pass the new one in
else
{

	newHash_ar.push(key+'='+value);

}

var newHash = newHash_ar.join('/');
return newHash;

}

function logHistory(key,value) // key, value
{
	var newHash = MakeHash(key,value);
//	alert(newHash);
 	dhtmlHistory.add(newHash,'custom');
}
