/*JQ*/
$(function(){
	 /*手机详细页*/
	$(".function li").hover(function(){
	  // $(this).find(".tip").css("display","block");
	  $(".tip", this).css("display","block");
	},function(){
	  // $(this).find(".tip").css("display","none");
	  $(".tip", this).css("display","none");
	});
	$(".price_mobile_up, .price_mobile_down").hover(function(){
	  // $(this).find(".price_circs_tip").css("display","block");
	  $(".price_circs_tip", this).css("display","block");
	},function(){
	  // $(this).find(".price_circs_tip").css("display","none");
	  $(".price_circs_tip", this).css("display","none");
	});

	/*
	$("#detail_content").mouseover(function(){  
       $(this).addClass("detail_content_hover");}).mouseout(function(){ 
       $(this).removeClass("detail_content_hover");
	 });
	$("#comment").mouseover(function(){  
       $(this).addClass("comment_hover");}).mouseout(function(){ 
       $(this).removeClass("comment_hover");
	 });
	*/


	/*详细正文收缩*/
	/*
	$(".detail_content button").click(function(){
		$(".detail_content button").toggleClass("check_action");
		$(".detail_top h3").toggleClass("h3_action");
		var answer=$(".depict");
		if (answer.is(':visible')){
			answer.hide(1100);
		}else{
			answer.show(1100);
		}
	});*/
	
	/*评论收缩*/
	/*
	$(".comment button").click(function(){
		$(".comment button").toggleClass("check_action");
		$(".comment_top h3").toggleClass("h3_action");
		var answer=$(".comment_form");
		if (answer.is(':visible')){
			answer.hide(1100);
		}else{
			build_comments(1);
			answer.show(1100);
		}
	});*/
	// $(".comment_list li:even").addClass("stripe");
	$.cookie('3gpwShowDescription', '1', { expires: 365 });
	if($.cookie('3gpwShowDescription') == 1) {
		showHideDescription();
	}

	if($.cookie('3gpwShowComment') == 1) {
		showHideComment();
	}	
});
function showHideDescription()
{
	
	$("#detail_content button").toggleClass("check_action");
	$("#detail_top h3").toggleClass("h3_action");
	var answer=$(".depict");
	if (answer.is(':visible')){
		$.cookie('3gpwShowDescription', null);
		//answer.hide(1100);
		answer.css("display","none");
		$(".get_top").hide();
	}else{
		$.cookie('3gpwShowDescription', '1', { expires: 365 });
		//answer.show(1100);
		answer.css("display","block");
		$(".get_top").show();
	}
}

function showHideComment()
{
	$("#comment button").toggleClass("check_action");
	$("#comment_top h3").toggleClass("h3_action");
	var answer=$("#comment_form");
	if (answer.is(':visible')){
		$.cookie('3gpwShowComment', null);
		answer.css("display","none");
	}else{
		$.cookie('3gpwShowComment', '1', { expires: 365 });
		build_comments(1);
		answer.css("display","block");
	}
	
}

function select_price(price, price_id)
{
	
	if($("#price_chart_"+price_id).val() == "1") {
		$("#chart_show").show();
		$(".price_circs_tip img").get(0).src = $("#price_chart_path_"+price_id).val();
	}else {
		$("#chart_show").hide();
	}
	$(".price_tab label").removeClass("nonce");
	$("#price_label_"+price_id).addClass("nonce");
	var price_class = $("#price_class_"+price_id).get(0).value;
	
	var oprice = $("#oprice_value strong").html();
	var html = $("#oprice_value").html();
	$("#oprice_value").removeClass();
	$("#oprice_value").addClass(price_class);
	html = html.replace(oprice, price);
	$("#oprice_value").html(html);
	document.getElementById("sprice_id").value = price_id;
	if($("#price_state_"+price_id).val() == "0") {
		$("#btn_buy").get(0).src = "/web/normal/images/mobile_buy_de.gif";
		$("#btn_buy").parent().unbind('click');
		$("#btn_buy").get(0).alt = "缺货中";
		// $("#btn_buy").parent().get(0).href = "";
	}else {
		$("#btn_buy").get(0).src = "/web/normal/images/mobile_buy.gif";
		$("#btn_buy").get(0).alt = "购买";
		$("#btn_buy").parent().click(function(){order()});
		// $("#btn_buy").parent().get(0).href = "#";
	}
	
}

function order()
{
	var price_id = document.getElementById("sprice_id").value;
	window.location = "phone_order.html?price_id="+price_id;
	// window.location = "cart.html?price_id="+price_id;
}

function vorder(item_id)
{
	if(_env_member_id == "0") {
		if(confirm("兑换商品需要登录，确定要转入登录页面吗？")) {
			var a = encode64("vproduct_order.html?item_id="+item_id);
			window.location = "/member_login.html?forward="+a;
		}
		// alert("请先登陆！");
		return;
	}
	window.location = "vproduct_order.html?item_id="+item_id;
}


//将Ansi编码的字符串进行Base64编码
function encode64(input) {
	var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	var output = "";
	var chr1, chr2, chr3 = "";
	var enc1, enc2, enc3, enc4 = "";
	var i = 0;
	
	do {
		 chr1 = input.charCodeAt(i++);
	 chr2 = input.charCodeAt(i++);
	 chr3 = input.charCodeAt(i++);
	
	 enc1 = chr1 >> 2;
	 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
	 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
	 enc4 = chr3 & 63;
	
	 if (isNaN(chr2)) {
	    enc3 = enc4 = 64;
	 } else if (isNaN(chr3)) {
	    enc4 = 64;
	 }
	
	 output = output + 
	    keyStr.charAt(enc1) + 
	    keyStr.charAt(enc2) + 
	    keyStr.charAt(enc3) + 
	    keyStr.charAt(enc4);
	 chr1 = chr2 = chr3 = "";
	 enc1 = enc2 = enc3 = enc4 = "";
	} while (i < input.length);
	
	return output;
}

function build_comments(page)
{
	var data = "item_id=" + item_id + "&page=" + page;
	$.ajax({ url: "phone_ajaxgetcomments.html",
		timeout:30000,
		type:"post",
		dataType:"text",
		data: data,
		success:function(data){
			// 发表成功					
			if(data == "-1") {
				// alert('加载失败，请联系管理员！');				
			}else {
				$("#comments_list").html(data);
			}
		},
		complete:function(){
		},
		error:function(){
			alert('加载失败，请联系管理员！');
		}
	});
}

function save_comment()
{
	var content = document.getElementById("content");
	if(content.value == "") {
		alert("请填写评论内容！");
		content.focus();
		return false;
	}
	// submitbtn onsubmitbtn
	var submitbtn = document.getElementById("submitbtn");
	submitbtn.className = "submited";
	submitbtn.value = "发表中，请稍后...";
	submitbtn.disabled = true;
	
	var data = "content=" + content.value + "&item_id=" + item_id;
	$.ajax({ url: "phone_ajaxsavecomment.html",
		timeout:5000,
		type:"post",
		dataType:"text",
		data: data,
		success:function(data){
			// 发表成功
			content.value = "";
			$(".appear").hide();
						
			if(data == "1") {
				$(".appear_tip").animate({
					height: 'show',
					opacity: 'show'
				}, 'slow');		
				build_comments(1);		
			}else {
				alert('操作失败，请联系管理员！\n错误描述：' + data);
				
			}
		},
		complete:function(){
			submitbtn.className = "submit";
			submitbtn.value = "发表";
			submitbtn.disabled = false;		
		},
		error:function(){
			alert('操作失败，请联系管理员！');
		}
	});
}

function close_tip()
{
	$(".appear_tip").hide();
	$(".appear").show();
}
// jQuery cookie 插件
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};