//vip add union
function vipUnion(type, value){
	
	if("unionPhone400" == type){
		if (typeof(unionPhone400) != 'undefined' && unionPhone400.length > 1) {
			value = unionPhone400;
		}
	}else if("unionTailPrefix" == type){
		if (typeof(unionTailPrefix) != 'undefined' && unionTailPrefix.length > 1) {
			value = unionTailPrefix + value;
		}
	}
	return value;	
}

// moved here from xdt, jh
$(".all_city_box").mouseover(function () {
	$(this).addClass("change_city_hover");
});
$(".all_city_box").mouseout(function () {
	$(this).removeClass("change_city_hover");
});
$(".change_city").click(function (e) {
	return false;
});

// moved here from jy
$("#nav_box li div").parent("li").mouseover(function () {
	$(this).addClass("cs_hover");
});
$("#nav_box li div").parent("li").bind("mouseleave", function () {
	$(this).removeClass("cs_hover");
});

// moved here from 263
$("#nav li ul").parent("li").mouseover(function(){
	$(this).addClass("cs_hover");
});
$("#nav li ul").parent("li").bind("mouseleave", function(){
	$(this).removeClass("cs_hover");		
});

// used for slideviewer. picWidth and picHeight should be defined in advance
//if(typeof(picWidth) != "undefined" && typeof(picHeight) != "undefined"){
//	$("div#playpic").slideView({toolTip: false, pictWidth:picWidth, pictHeight:picHeight});
//}


// for photo album
try{initSlide(0);}catch(e){};


// below: send phone numbers on sms
function get_sendsms_div_ul(phones, more_than_one){
	var html = "";
	if(more_than_one){
		html += "<h2>选择您要发送的号码:(号码将在5分钟内发送到您的手机。)</h2>";
		var count = Math.floor(phones.length / 3);	// 共3列
		if(phones.length % 3 != 0){
			count += 1;
		}
		var j = 0;
		for(var i = 0; i < phones.length; i ++){
			if(0 == j){
				html += '<div class="select_ph_list">';
				html += '<ul>';
			}
			var phone = phones[i];
			html += '<li><label><input type="checkbox" name="phone_select" xiaoqu="'+ phone.name + '"/>' + phone.name + ' <strong>' + vipUnion('unionPhone400' ,phone.phone400) + '转' + vipUnion('unionTailPrefix' ,phone.phone400Tail) + '</strong></label></li>';
			j ++;
			if(j == count){
				j = 0;
				html += "</ul>";
				html += "</div>";
			}
		}
		if(0 != j){
			html += "</ul>";
			html += "</div>";
		}
	}
	else{
		if(phones.length > 0){
			var phone = phones[0];
			html += '<div class="select_ph_list" style="float:none; width:700px; padding-left:0; _padding:0;">';
			html += phone.name + ' <strong>' + vipUnion('unionPhone400' ,phone.phone400) + '转' + vipUnion('unionTailPrefix' ,phone.phone400Tail) + '</strong>';
			html += '</div>';
		}
	}
	return html;
}
function get_sendsms_div(phones, msg){
	var width = 700;
	if(typeof(phones) != "undefined" && phones.length > 1){
		width = 900;
	}
	var html = '<div class="outter" style="width:'+width+'px"><div class="inner">';

	html += get_sendsms_div_ul(phones, phones.length > 1 ? true : false);
	
	html += '<div class="C"></div>';
    html +=	'</div>';
    html += '<div class="ph_list_btn">';
	html += '<div class="send_ph_num">';
	html += '<input class="send_text" type="text" name="" />';
	html += '<input class="send_btn" type="button" value="" onclick="send_sms(\'' + msg.replace(/'/gi, '').replace(/"/gi, '') + '\')" />';
    html += '</div>';
	html += '<div class="ph_list_tip" style="display:none"></div>';
	html += '</div>';
	html += '</div>';
	return html;
}
function default_gen_phone_info(phones){
	var phoneinfo = "";
	for(var i = 0; i < phones.length; i ++){
		var p = phones[i].name + "" + phones[i].phone;
		if(phoneinfo == ""){
			phoneinfo = p;
		}
		else{
			phoneinfo += "," + p;
		}
	}
	return phoneinfo;
}
function send_sms(msg){
	var phoneinfo = "";
	var phones = [];
	if(phonenumbers.length > 1){
		$(".select_ph_list :checked").each(function(i){
			var p = {};
			p.name = $(this).attr("xiaoqu");
			p.phone = $(this).next("strong").text();
			phones.push(p);
		});
		if(phones.length == 0){
			$("#fancy_div .ph_list_tip").text("您还没有选择要发送的号码！").show();
			return;
		}
	} else{
		var p = {};
		p.name = phonenumbers[0].name;
		p.phone = vipUnion('unionPhone400' ,phonenumbers[0].phone400) + "转" + vipUnion('unionTailPrefix' ,phonenumbers[0].phone400Tail);
		phones.push(p);
	}
	var sms = $("#fancy_div .send_text").val();
	if(!/^\s*\d{11}\s*$/.test(sms)){
		$("#fancy_div .ph_list_tip").text("请输入正确的手机号码！").show();
		return;
	}
	if(typeof(gen_phone_info) == "function"){
		phoneinfo = gen_phone_info(phones);
	}
	else{
		phoneinfo = default_gen_phone_info(phones);
	}
	if(msg == "")
		msg = phoneinfo;
	else
		msg = msg.replace("{{phoneinfo}}", phoneinfo).replace("{{sitedomain}}", window.location.host);
	msg = msg.replace("咨询电话", "请拨打电话");
	$("#fancy_div .ph_list_tip").text("正在发送短信...").show();
	
	/*
	var langtaojin_union_js = document.createElement("script");
	if(langtaojin_union_js){
		langtaojin_union_js.src = 'http://bj.j.cn/sendsms?sendto='+sms+'&message='+msg;
		langtaojin_union_js.onload = langtaojin_union_js.onreadystatechange = function(){alert("new");$("#fancybox-inner .ph_list_tip").text("发送成功，请稍候查收。").show();setTimeout("$('#fancybox-close').click()", 3000);};
	}
	document.getElementsByTagName("head")[0].appendChild(langtaojin_union_js);
	'http://www.jcn.com/service.htm?method=sendsms&sendto='+sms+'&message='+encodeURIComponent(msg),
	*/
	
	
	$.getScript(
		'http://shenghuo.j.cn/service.htm?method=sendsms&sendto='+sms+'&message=' + msg,
		function() {
			$("#fancy_div .ph_list_tip").text("发送成功，请稍候查收。(号码将在5分钟内发送到您的手机。)").show();
			setTimeout("$('#fancy_close').click()", 3000);
		}
	);
	/*
	$.ajax({
	   type: "POST",
	   url: "http://www.jcn.com/service.htm?method=sendsms",
	   data: '&sendto='+sms+'&message='+encodeURIComponent(msg),
	   success: function(msg){
			$("#fancybox-inner .ph_list_tip").text("发送成功，请稍候查收。").show();
			setTimeout("$('#fancybox-close').click()", 3000);
	   }
	 });
	 */
}
function init_sendsms(){
	var sendsms = $("a#sendsms");
	sendsms.click(function() {
		if(sendsms.length > 0 && typeof(phonenumbers) != 'undefined' && phonenumbers.length > 0){
			/**
			sendsms.fancybox({
				'hideOnContentClick': true, 
				padding: 0, 
				margin: 0, 
				centerOnScroll: true, 
				scrolling: 'no', 
				hideOnContentClick: false, 
				width: phonenumbers.length == 1? 280 : 920, 
				autoHeight:true, 
				autoWidth:true
			});		// 不放到document.ready里的话, ie7下浏览器报错页面打不开！
			
			if(typeof(smsmsg) == 'undefined')
				smsmsg = "";
			$("#sendsmsdiv").html(get_sendsms_div(phonenumbers, smsmsg));
			var count = Math.floor(phonenumbers.length / 3);	// 共3列
			if(phonenumbers.length % 3 != 0){
				count += 1;
			}
			var fancyheight = 145 + count * 25;
			var fancywidth = 350;
			if(phonenumbers.length > 1){
				fancywidth = 1000;
			}
			*/
			if(typeof(smsmsg) == 'undefined')
				smsmsg = "";
			$("div#myfancybox_content").html(get_sendsms_div(phonenumbers, smsmsg));
			var count = Math.floor(phonenumbers.length / 3);	// 共3列
			if(phonenumbers.length % 3 != 0){
				count += 1;
			}
			var fancyheight = 105 + count * 25;
			var fancywidth = 720;
			if(phonenumbers.length > 1){
				fancywidth = 920;
			}
			initFancybox();
			$.fn.fancybox.settings('itemLoadCallback',null);
			$.fn.fancybox.settings('frameHeight', fancyheight);
			$.fn.fancybox.settings('frameWidth', fancywidth);
			$('a#myfancybox_a').click();
		
			// $("#myfancybox").show();
		}
	});
	//sendsms.show();
}

