/**
 * begin of recent view scripts.
 */
function saveRecentView(urlPattern,cookieName) {
    if (urlPattern!="" && parent.document.URL.indexOf(urlPattern)==-1) {
        return;
    }
    //URL matched, create hidden iframe to save view history
    
    var recentViews = getCookie(cookieName);
    if (!recentViews) {
        recentViews="";
    }
    var curView = composeStrView(parent.document.URL,parent.document.title,urlPattern);
    var viewsInArray = toArrayViews(recentViews);
    recentViews = appendView(viewsInArray,curView);
    setCookie(cookieName,recentViews,null,null,null,null);    
}

function appendView(arrayViews,aView) {
    var newStrViews = aView;
    //only show 5 latest views
    if (arrayViews) for (i=0;i<arrayViews.length&&i<4;i++) {
        newStrViews = newStrViews+"~~"+arrayViews[i];
    }
    return newStrViews;
}
function composeStrView(viewUrl,viewName,prefix) {
	var idx = viewName.indexOf(" | ");
	var shortName = idx==-1?viewName:viewName.substring(idx+3,viewName.length);
	
    return viewUrl.substring(viewUrl.indexOf(prefix,10)+prefix.length,viewUrl.indexOf(".html",20))+"~"+shortName;
}
function toArrayViews(strViews) {
    var arrayViews = null;
    if (strViews && strViews!="") {
        arrayViews=strViews.split("~~",5);
    }
    return arrayViews;
}
function getViewDetails(strView) {
    return strView.split("~");
}
function saveProductRecentView() {
    saveRecentView("/products_","recentProductCookies");
}
function saveCategoryRecentView() {
    saveRecentView("/categorys_short_","recentCategoryCookies");
}
function saveAllRecentViews() {
	if (navigator.cookieEnabled) {
	    saveProductRecentView();
    	saveCategoryRecentView();
	}
}
function getRecentViews(cookieName) {
	return navigator.cookieEnabled?toArrayViews(getCookie(cookieName)):null;
}

function getProductRecentViews() {
	return getRecentViews("recentProductCookies");
}
function getCategoryRecentViews() {
	return getRecentViews("recentCategoryCookies");
}

