// -------------------------------------- //
/*										  */
// ----- Ｃ版　公用　functions ---------- //
/*										  */
// -------------------------------------- //

/**
 * 获取地址参数
 * @param string key 参数名
 * @return string 参数值
 */
function geturl(key)
{
	var urlArray = document.location.href.split("/");
	var filename = urlArray[urlArray.length -1];
	
	pattern = "/-" + key + "([^-\.]*)/i";
	pattern = eval(pattern);
	
	rs = filename.match(pattern);
	
	if(rs != null){
		return rs[1];
	}
	
	return '';
}

/**
 * 设置地址参数
 * @param string key 要设置的参数名
 * @param mix	 value 参数值
 * @return string 设置参数后的URL
 */
function seturl(key, value, url)
{
	if(url == undefined){
		url = document.location.href;
	}
	
	var urlArray = url.split("/");
	var filename = urlArray[urlArray.length -1];
	
	pattern = "/-" + key + "([^-\/\.])*/i";
	pattern = eval(pattern);
	replacetext = "-" + key + value;
	
	if(!pattern.test(filename)){
		pattern = /\.html/;
		replacetext += ".html";
	}
	
	urlArray[urlArray.length -1] = filename.replace(pattern, replacetext);
	
	return urlArray.join("/");
}

/**
 * 初始化 select 对像
 * @param string oSelect select标签对象
 * @param string value 选中项值
 */
function selectInit(oSelect, value)
{
	if(value == "")return false;
	var obj;
	if(typeof(oSelect) != "object"){
		obj = document.getElementById(oSelect);
	}else{
		obj = oSelect;
	}
	if(obj == null)return false;
	for(i = 0; i < obj.options.length; i++){
		if(value == obj.options[i].value){
			obj.options[i].selected = true;
		}
	}
}

/**
 * 设置链接地址
 * @param object obj(this对象)
 * @param string key 参数名
 * @param string value 参数值
 */
function setHref(obj, key, value, url)
{
	if(typeof(obj) != "object") return false;
	obj.href = seturl(key, value, url);
}

/**
 * 解析MASK 值, 确定产品类型值(代金产品，促销产品)
 * @param int mask 码值
 * @param string returnType 参数名
 * @return string or array
 */
function parseMask(mask, returnType)
{
	if(parseInt(mask) <= 0){
		return 0;
	}

	var m = 0;
	var values = new Array();
	
	// 解析掩码
	while (mask > 0){
		i = 0; 
		j = 0;
		
		while (j <= mask){
			j = Math.pow(2, ++i);
		}
		
		j = Math.pow(2, --i);
		values[m++] = j;
		mask = mask - j;
	}

	// check return type
	if(returnType == "array"){
		return values;
	}else{
		return values.join(",");
	}
}

/**
 * 获取产品类型属性值
 *
 */
function getGoodsStatusValue(type)
{
	var typeValue = 0;
	switch(type){
		case "buy":
			typeValue = "1";
			break;
		case "discount":
			typeValue = "2";
			break;
		case "ticket":
			typeValue = "4";
			break;
		case "sale":
			typeValue = "8";
			break;
		case "vendue":
			typeValue = "16";
			break;
	}
	
	if(typeValue == 0){
		return '';	
	}
	
	return typeValue;
}

/**
*  get sales Image
*/
function getImage(typeValue, style)
{
    var path = "/subSite/tpl/images/" ;
    switch(typeValue){
        case "2":
            return  style ? path+"tcj_c.gif" : path+"but_cxhd.gif" ;
            break;
        case "4":
            return  style ? path+"tcj_j.gif" : path+"but_ljq.gif" ; 
            break;
        case "8":
            return  style ? path+"tcj_t.gif" : path+"but_tjsp.gif" ; 
            break;
    }     
}

/**
 * 确定产品类型(代金产品，促销产品)
 * @param int mask 码值
 * @param string type 类型
 * @param string formartString 参数名
 * @return string or array
 */
function productStatus(mask, goodsid, style)
{
	var statusStr = parseMask(mask); 
	if(statusStr.length ==0){
		return false;
	}
	
	if(!parseInt(goodsid)){
		return false;
	}
	
	var typeDiscountValue = getGoodsStatusValue("discount");
	var typeTicketValue = getGoodsStatusValue("ticket");   
    var typeSaleValue = getGoodsStatusValue("sale");    
	var html = "";
	
	if(statusStr.indexOf(typeDiscountValue) != -1){
		html += '<a href="/sales/viewcx-gid' + goodsid + '.html" target="_blank"><img src="' + getImage(typeDiscountValue, style)+'" alt="促销信息" /></a>';
	}
	
	if(statusStr.indexOf(typeTicketValue) != -1){
		html += '<a href="/ticket/view-gid' + goodsid + '.html" target="_blank"><img src="' + getImage(typeTicketValue, style)+'" alt="代金券信息" /></a>';
	}
	
    if(statusStr.indexOf(typeSaleValue) != -1){
        html += '<a href="/ticket/view-gid' + goodsid + '.html" target="_blank"><img src="' + getImage(typeSaleValue, style)+'" alt="特价信息" /></a>';
    }
    
	/**
	var typeBuyValue = getGoodsStatusValue("buy");
	var typeSaleValue = getGoodsStatusValue("sale");
	var typeVendueValue = getGoodsStatusValue("vendue");

	if(statusStr.indexOf(typeBuyValue) != -1){
		html += '<a href="/group/view-gid"' + goodsid + ' target="_blank"><img src="/" alt="团购商品" /></a>';
	}
	if(statusStr.indexOf(typeSaleValue) != -1){
		html += "";
	}
	if(statusStr.indexOf(typeVendueValue) != -1){
		html += "";
	}
	**/
	
	document.write(html);
}


/**
 * 确定产品属性是否存在
 * @param int mask 码值
 * @param string type 类型
 * @return boolean
 */
function statusExist(mask, type){
	
	if(!parseInt(mask)){
		return false;	
	}
	
	var typeValue = getGoodsStatusValue(type);
	if(!typeValue){
		return false;
	}

	statusStr = parseMask(mask);
	
	if(statusStr.length == 0){
		return false;	
	}
	
	if(statusStr.indexOf(typeValue) != -1){
		return true;
	}
	
	return false;
}

/**
 * Cookie
 *
 */
function Cookie()
{
	// Example:
	// Cookie.write("myCookie", "my name", 24);
	this.write = function(name, value, hours){
		var expire = "";
		if(hours != null){
			expire = new Date((new Date()).getTime() + hours * 3600000);
			expire = "; expires=" + expire.toGMTString();
		}
		
		path = "; path=/;";
		
		document.cookie = name + "=" + escape(value) + expire + path;
	}
	
	// Example:
	// Cookie.read("myCookie");
	this.read = function(name){
		var cookieValue = "";
		var search = name + "=";
		if(document.cookie.length > 0){ 
			offset = document.cookie.indexOf(search);
			if (offset != -1){ 
				offset += search.length;
				end = document.cookie.indexOf(";", offset);
				if (end == -1) end = document.cookie.length;
				cookieValue = unescape(document.cookie.substring(offset, end));
			}
		}
		return cookieValue;
	}
}

/**
 * Cookie 数组
 * 
 */
function Visited(nums, cookieName)
{
	this.nums = nums;
	
	this.cookieName = cookieName;
	
	this.cookieExpire = 24*30;
	
	this.separatorGroup  = "@@";
	
	this.separatorElement = "||";
	
	Cookie.call(this); // 从 Cookie 继承
	
	// get array
	this.toArray = function(){
		cookies = this.read(this.cookieName);
		if(cookies.length == 0){
			return '';
		}
		
		visitedArray = new Array();
		temp = cookies.split(this.separatorGroup);
		
		for(i = 0; i < temp.length; i++){
			if(temp[i].length > 0){
				visitedArray[i] = temp[i].split(this.separatorElement);
			}
		}
		
		return visitedArray;
	}
	
	// set value
	this.push = function(value){
	
		// get old data
		cookies = this.read(this.cookieName);
		temp = cookies.split(this.separatorGroup);
		
		// format value
		if(typeof(value) == "object"){
			value = value.join(this.separatorElement);
		}
		
		// push value
		temp.unshift(value);
		if(temp.length > this.nums){
			temp.length = this.nums;
		}
		
		// write cookie
		this.write(this.cookieName, temp.join(this.separatorGroup), this.cookieExpire);
	}
	
	this.clear = function(){
		this.write(this.cookieName, "", this.cookieExpire);
	}
}


/**
 * 最近访问页面记录
 *
 */
function VistedPage()
{
	this.visited = new Visited(20, "visited_page");
	
	/**
	 * 列表最近访问过的页面
	 *
	 */
	this.list = function(nums, formart, out){
	
		if(!formart){
			formart = "[img]<a href='[href]'>[title]</a><br />";
		}
		
		reImg = /\[img\]/ig;
		reTitle = /\[title\]/ig;
		reHref = /\[href\]/ig;
		
		list = this.visited.toArray();
		
		if(!list) return false;
		
		html = ""
		for(i = 0; i < nums; i++){
			if(list[i] == undefined) break;

			itemHtml = formart.replace(reTitle, list[i][0]);
			itemHtml = itemHtml.replace(reImg, list[i][1]);
			itemHtml = itemHtml.replace(reHref, list[i][2]);

			html += itemHtml;
		}
		
		if(out != false){
			document.write(html);
			return true;
		}
		
		return html;
	}
	
	/**
	 * 设置访问页面
	 *
	 */
	this.set = function(title, img){
		
		list = this.visited.toArray();

		if(list){
			for(i = 0; i < list.length; i++){
				if(list[i][2] == document.location.href){
					// 已经设置
					return false;	
				}
			}
		}
		
		if(img){
			this.visited.push(new Array(title, img, document.location.href));
		}
	}
	
	/**
	 * clear all 
	 *
	 */
	this.clear = function(){
		this.visited.clear();
	}
}
