/*  
	Title : Cookie Crumbs
	Author : Tom Coote
	Wedsite : http://www.tomcoote.co.uk
*/



/************************************************/
/** (START) Add Function from Basic (08/05/12) **/
/************************************************/
	function HistoryLayoutTr(sltLink,sltTitle,sltImage){
		var tr = '';
		if(sltTitle=="undefined"){return tr;}
		tr = '';
		tr +=	'<div>';
		tr +=		'<div style="float: left; width:24px;"><div style="padding: 10px 10px 5px 10px;"><img src="/images/ring/side/icon02.gif"/></div></div>';
		tr +=		'<div style="float: left; width:111px;"><div style="padding: 8px 5px 5px 0px;"><a style="color:#645946; font-size: 12px;" href="'+sltLink+'">'+sltTitle+'</a></div></div>';
		tr +=		'<div style="float: right; width:65px;"><div style="padding: 5px 5px 5px 0px;"><a href="'+sltLink+'"><img border="0" src="/files/ring/'+sltImage+'" width="60"/></a></div></div>';
		tr +=	'</div>';
		tr +=	'<div style="background-color:#CFC7BA;" class="clear"><img width="200" height="1" src="/images/ring/spacer.gif"/></div>';

		return tr;
	}
	function HistoryLayoutTable(tr,ccnt){
		var tbl = '';
		if(ccnt<1){ return tbl; }

		tbl = '';
		tbl +=	'<div style="padding: 0px 0px 0px 0px;"><img alt="HISTORY" src="/images/ring/side/history.gif"/></div>';
		tbl +=	'<div style="background-color:#F4F4F4;">';
		tbl +=		'<div>';
		tbl +=			'<div style="padding: 7px 5px 7px 7px; float: left;"><img src="/images/ring/side/icon01.gif"/></div>';
		tbl +=			'<div style="padding: 7px 10px 7px 0px; float: left; color:#BC4949; font-size: 12px; font-weight: bold;">最近見たリング</div>';
		tbl +=			'<div style="padding: 8px 0px 0px 0px; float: left; color:#645946; font-size: 10px;">最新の'+ccnt+'件を表示</div>';
		tbl +=		'</div>';
		tbl +=		'<div style="background-color:#CFC7BA;" class="clear"><img width="200" height="1" src="/images/ring/spacer.gif"/></div>';
		tbl +=		''+ tr;
		tbl +=	'</div>';

		return tbl;
	}
	function getCrasHallName(){
		try{
			var tn = document.getElementById("detail_title");
			if(tn){
				return tn.firstChild.nodeValue;
			}
		}catch(e){}
	}
	function getCrasHallImage(){
		try{
			var tn = document.getElementById("detail_image").src;
			tn = tn.split("/");
			return tn[5];
		}catch(e){}
	}
/************************************************/
/** (END) Add Function from Basic (08/05/12) **/
/************************************************/



 /**
	* @argument iNumberOfCrumbs - How many crumbs do you want to show on the page, ie the last 5 page views!
	*
	*/
function CookieCrumbs(iNumberOfCrumbs) {
    
	var that = {}; 

	/* Private Variables */
	var sCookieCrumbsSeparator = '|',	// The value used to separate each 'link & title' pair in the cookie
		sLinkTitleSeparator = '::';		// The value used to separate link from title for each pair
		/* NOTE: don't use the above values in your page titles */


    /* Private Functions */

	function getCookie(name) {
		var start = document.cookie.indexOf(name + "=");
		var len = start + name.length + 1;

		if ((!start) && (name != document.cookie.substring(0, name.length))) {
			return null;
		}

		if (start == -1) {
			return null;
		}

		var end = document.cookie.indexOf( ";", len );

		if (end == -1) {
			end = document.cookie.length;
		}

		return unescape(document.cookie.substring(len, end));
	}

	function setCookie(name, value, expires, path, domain, secure) {
		var today = new Date();
		today.setTime(today.getTime());

/************************************************/
/** (START) Add Function from Basic (08/05/12) **/
/************************************************/
		expires = 7;
/************************************************/
/** (END) Add Function from Basic (08/05/12)   **/
/************************************************/

		if (expires) {
			expires = expires * 1000 * 60 * 60 * 24;
		}

		var expires_date = new Date(today.getTime() + (expires));
		
		document.cookie = name+"="+escape(value) +
			((expires) ? ";expires="+expires_date.toGMTString() : "") + 
/************************************************/
/** (START) Add Function from Basic (08/05/12) **/
/************************************************/
//			((path) ? ";path=" + path : "") +
			(";path=/") +
/************************************************/
/** (END) Add Function from Basic (08/05/12)   **/
/************************************************/
			((domain) ? ";domain=" + domain : "") +
			((secure) ? ";secure" : "");

		return getCookie(name);
	}

	function deleteCookie(name, path, domain) {
		if (getCookie(name)) {
			document.cookie = name + "=" +
			((path) ? ";path=" + path : "") +
			((domain) ? ";domain=" + domain : "") +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
		}
	}

	function setLocation(iPos) {

/************************************************/
/** (START) Add Function from Basic (08/05/12) **/
/************************************************/
		return parent.location.href;
/************************************************/
/** (END) Add Function from Basic (08/05/12)   **/
/************************************************/

		var sLocation = formatLastPage(document.location.toString());

		if (sLocation.indexOf('?') !== -1) {
			return sLocation + '&ccp=' + iPos;
		}
		else {
			return sLocation + '?ccp=' + iPos;
		}
	}

	function sliceCookie(Cookie) {
		var sLocation = document.location.toString(),
			iPos = -1, sCookies = '', sCrumbTrail = '', i = 0;

		if (sLocation.indexOf('?ccp=') !== -1) {
			sLocation = sLocation.slice(sLocation.indexOf('?ccp=')+5, sLocation.length); // will always be at the end so can use length
			iPos = parseInt(sLocation,10);
		}

		if (sLocation.indexOf('&ccp=') !== -1) {
			sLocation = sLocation.slice(sLocation.indexOf('&ccp=')+5, sLocation.length);
			iPos = parseInt(sLocation,10);
		}
		
		if (iPos === -1) {
			return Cookie;
		}
		else {
			sCookies = Cookie.split(sCookieCrumbsSeparator);

			if (sCookies.length-1 === iPos) {
				return Cookie;
			}

			for (i=0; i < iPos; i++) {
				sCrumbTrail += sCookieCrumbsSeparator + sCookies[i]; 
			}
		}

		sCrumbTrail = (sCrumbTrail === '') ? '||' : sCrumbTrail;
		return setCookie('CookieCrumbs', sCrumbTrail.replace(sCookieCrumbsSeparator,''), 1);
	}

	function formatLastPage(sLastPage) {

		if (sLastPage.indexOf('?ccp=') !== -1) {
			sLastPage = sLastPage.replace(sLastPage.slice(sLastPage.indexOf('?ccp='), sLastPage.length),'');
		}

		if (sLastPage.indexOf('&ccp=') !== -1) {
			sLastPage = sLastPage.replace(sLastPage.slice(sLastPage.indexOf('&ccp='), sLastPage.length),'');
		}

		return sLastPage;
	}

	function canUpdateExistingCookie(sLastPage) {
		if (document.referrer.toString() === document.location.toString()) {
			return false;
		}

		if (document.referrer.toString() === '') {
			return false;
		}

		if (formatLastPage(sLastPage) === document.location.toString()) {
			return false;
		}

		if (formatLastPage(sLastPage) === formatLastPage(document.location.toString())) {
			return false;
		}

		return true;
	}


	/* Public Functions */

	that.GetCrumbs = function() {
		try
		{
			var Cookie = getCookie('CookieCrumbs'),
				sCrumbTrail = '',
				sPageTitle = document.getElementsByTagName('title')[0].innerHTML || '',
				sCookies = '', sTitle = '', sLink = '', i = 0;

/************************************************/
/** (START) Add Function from Basic (08/05/12) **/
/************************************************/
			sPageTitle = getCrasHallName();
			if(sPageTitle != undefined){
				var crasHallImage = getCrasHallImage();
				if(crasHallImage != undefined){
					sPageTitle = sPageTitle + sLinkTitleSeparator + crasHallImage;
/************************************************/
/** (END) Add Function from Basic (08/05/12)   **/
/************************************************/

					if (document.referrer.toString() === '') {
						Cookie = setCookie('CookieCrumbs', sCookieCrumbsSeparator + setLocation(0) + sLinkTitleSeparator + sPageTitle, 1);
					}else if (Cookie !== null) {
						Cookie = sliceCookie(Cookie);	
						sCookies = Cookie.split(sCookieCrumbsSeparator);
						sLastPage = sCookies[sCookies.length-1].split(sLinkTitleSeparator)[0];

						if (canUpdateExistingCookie(sLastPage)) {
							Cookie = setCookie('CookieCrumbs', Cookie + sCookieCrumbsSeparator + setLocation(sCookies.length) + sLinkTitleSeparator + sPageTitle, 1);
						}
					}
					else {
						Cookie = setCookie('CookieCrumbs', sCookieCrumbsSeparator + setLocation(0) + sLinkTitleSeparator + sPageTitle, 1);
					}

/************************************************/
/** (START) Add Function from Basic (08/05/12) **/
/************************************************/
				}
			}
/************************************************/
/** (END) Add Function from Basic (08/05/12)   **/
/************************************************/


			if (Cookie === null) { return ''; } // for browsers that have cookies blocked

			sCookies = Cookie.split(sCookieCrumbsSeparator);
			if (iNumberOfCrumbs === -1) {
				iNumberOfCrumbs = sCookies.length;
			}
			else {
				iNumberOfCrumbs = (sCookies.length < iNumberOfCrumbs) ? sCookies.length : iNumberOfCrumbs;
			}

			var dupArray = new Array();
			var arrayCount = 0;
			for (i=sCookies.length-1; i > sCookies.length-1-iNumberOfCrumbs; i--) {
				if (sCookies[i] !== '') {
					var sLinkTitle = sCookies[i].split(sLinkTitleSeparator);

/************************************************/
/** (START) Add Function from Basic (08/05/12) **/
/************************************************/
					// CommentOut // sCrumbTrail = /* '&#62; */ '<div id=\'history\'><a href=\'' + sLinkTitle[0] + '\' style=\'font-size:12px; color:#5A4A3D; text-decoration:none;\'>' + sLinkTitle[1] + '</a></div> ' + sCrumbTrail;
					if(dupArray[sLinkTitle[1]]!=1){
						var historyTr = HistoryLayoutTr(sLinkTitle[0],sLinkTitle[1],sLinkTitle[2]);
						sCrumbTrail = sCrumbTrail + historyTr;
						dupArray[sLinkTitle[1]] = 1;
						arrayCount++;
					}
/************************************************/
/** (END) Add Function from Basic (08/05/12)   **/
/************************************************/

				}
			}

/************************************************/
/** (START) Add Function from Basic (08/05/12) **/
/************************************************/
			sCrumbTrail = HistoryLayoutTable(sCrumbTrail,arrayCount);
/************************************************/
/** (END) Add Function from Basic (08/05/12)   **/
/************************************************/

			return sCrumbTrail.replace('&#62; ','');
		}
		catch(err) {
			//return err;  // For debugging
			return '';
		}
	};

	that.DeleteCookieTrail = function() {
		deleteCookie('CookieCrumbs');
	};

    return that;
}

