﻿function getFlashVersion(){try{try{var axo=new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');try{axo.AllowScriptAccess='always';}catch(e){return '6,0,0';}}catch(e){}return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g,',').match(/^,?(.+),?$/)[1];}catch(e){try{if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){return(navigator.plugins["Shockwave Flash 2.0"]||navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g,",").match(/^,?(.+),?$/)[1];}}catch(e){}}return '0,0,0';} 

function closeModalMini(){	
	$(".overlay:first").animate({ opacity: 0 }, 500, function() { $(this).remove(); });
	$(".modal:first").animate({ opacity: 0 }, 500, function() { $(this).remove(); });
}

//flash detection
function flashobj(obj){
if ( $(obj).length > 0 ){
	var version = getFlashVersion().split(',').shift();
	if (version < 8){
		//openModal("/modal/missingflash");
		$img = $(obj).find('img:first');
		if($img.length){
	
			var $object = obj;
			var $overlay = $("<div class=\"overlay mini\"></div>").animate({ opacity: 0 }, 0).appendTo($object);
			
			var msg = "<h4>此专题的全屏模式采用Adobe Flash Player播放,请</h4><p><br/><a href='http://get.adobe.com/flashplayer/' target='_blank'>点击这里</a> 下载/更新您的Flash Player"
					+"<br/><br/>如果您选择不安装Flash Player,将只显示文件的静态版本</p>"
					+"<p><em><a href='#' class='close'>点击‘—’以关闭信息</a></em></p>";

			var $miniModal = $("<div class=\"modal mini\"><div class=\"modal-tr\"><div class=\"modal-br\"><p class=\"close\"><a>[-]</a><div class=\"modal-contents\"><div class=\"modal-text\">"+msg+"</div></div></div></div></div>").appendTo($object).animate({ opacity: 0 }, 0);
			centerModal();
			$miniModal.find(".close").click(closeModalMini);
			$miniModal.animate({ opacity: 1 }, 500);
			$overlay.animate({ opacity: .9 }, 1000);
		 $(obj).find("img").show();
		}
	}else{
		 $(obj).find('.flash').show();
	}
	
}
}

$(function(){
	flashobj('.flash-intro');
	flashobj('.flash-installations');
});	
	

$.fn.extend
({
	hrefId: function() { return $(this).attr("href").substr($(this).attr("href").indexOf("#")); }
,	hrefNoId: function() { return $(this).attr("href").substr(0, $(this).attr("href").indexOf("#")); }
});


// clear default text in forms
$(":text,textarea").each(function()
{
	if (this.value != "")
	{
		this.defaultText = this.value;
		$(this).focus(function() { if (this.value == this.defaultText) this.value = ""; }).blur(function() { if (this.value == "" && this.defaultText) this.value = this.defaultText; });
	}
});


$(".tools form.language select").change(function() { if($(this).val() == 0) return false; window.location=$(this).val(); /*$(this).closest("form").submit();*/ });


// news ticker on home page
var $newsTicker = $(".news-ticker ul:first");
if ($newsTicker.length)
{
	$newsTicker.find(">li:not(:first)").hide();

	setInterval(function()
	{
		var $item = $newsTicker.find("li.selected:first");
		if (!$item.length)
			$item = $newsTicker.find("li:first");

		var $next = $item.next();
		if (!$next.length)
			$next = $newsTicker.find("li:first");

		$item = $item.removeClass("selected").fadeOut(500, function() { $next.addClass("selected").fadeIn(500, function() { if ($.browser.msie && $.browser.version == 7) this.style.removeAttribute("filter"); }); });
	}, 7500);
}


$(".accordion>li").each(function(){
	$(this).find("h4:first-child").wrapInner("<a></a>").prepend("<a class=\"details\">+ 详细内容</a>").prependTo(this);
})

/*$(".accordion").accordion({collapsible:true, clearStyle: true,active:false});

$(".accordion").bind('accordionchange', function(event, ui){

	$(ui.newHeader).find(".details").text("- Close");
	$(ui.oldHeader).find(".details").text("+ Details");
	
});*/



// accordion menu
$(".accordion>li").each(function()
{
	//$(this).wrapInner("<div></div>").find(">div>h4:first-child").clone().wrapInner("<a></a>").prepend("<a class=\"details\">+ Details</a>").prependTo(this);
	$(this).height($(this).children("h4").outerHeight());
	$(this).find(">div:first").addClass("data");
	$(this).find(">h4").click(function()
	{
		var $ch = $(this).closest("li").outerHeight()-1;
		$(this).closest("li").siblings().each(function()
		{
			var $h = $(this).children("h4").animate({ marginTop: "0px" }, 500);
			$(this).animate({ height: $h.outerHeight() }, 500);
		});
		
		if($ch  > $(this).outerHeight()){
			$(this).find(".details").text("+ 详细内容");
			$(this).closest("li").animate({ height:$(this).outerHeight() }, 500);
		
		}else{
		//var $h = $(this).closest("h4");
		//$h.animate({ marginTop: -$h.outerHeight() + "px" }, 500);
			$(this).parent().parent().find(".details").text("+ 详细内容");
			$(this).find(".details").text("- 关闭");
			var $li = $(this).closest("li");
			$li.animate({ height: $li.children("div").outerHeight()+$(this).outerHeight() }, 500);

		}
		return false;
	});
});


// product details accordion
$(".media>li>h3:first-child").prepend("<span class=\"details\">+ 详细内容</span>").click(function()
{
	if ($(this).hasClass("selected"))
		return;
	$(this).addClass("selected").children(".details").text("").parent().next().slideDown(500).parent().siblings().children("h3:first-child").removeClass("selected").children(".details").text("+ 详细内容").parent().next().slideUp(500);
});
$(".media>li:first-child>h3").click();


// generic tabs
$(".tabs>li>a,.team>ul.members a").not(".team>ul.members>li:last-child>a").click(function()
{
	if ($(this).hasClass("selected"))
		return false;
	$(this).addClass("selected").parent().siblings().children("a").removeClass("selected");
	$($(this).hrefId()).show();
	$("." + $(this).parent().parent().attr("id") + ":not(" + $(this).hrefId() + ")").hide();
	return false;
});
$(".tabs>li:first-child>a,.team>ul.members>li:first-child>a").click();


// submenu on right column
/*$(".submenu li.selected").each(function() { $(this).removeClass("selected").children("ul").show(); $(this).children("strong:first").addClass("selected"); });
$(".submenu strong").click(function() { $(this).toggleClass("selected").next("div").toggle(); });
*/


// custon checkboxes and radio buttons
function updateCheckboxLabel() { $("label[for=" + $(this).attr("id") + "]").toggleClass("checkbox-selected", $(this).attr("checked")); }
$(":checkbox").click(updateCheckboxLabel).change(updateCheckboxLabel).each(updateCheckboxLabel);

function updateRadioLabel() { $("input[name=" + $(this).attr("name") + "]").each(function() { $("label[for=" + $(this).attr("id") + "]").removeClass("radio-selected"); }); $("label[for=" + $(this).attr("id") + "]").toggleClass("radio-selected", $(this).attr("checked")); }
$(":radio").click(updateRadioLabel).change(updateRadioLabel).each(updateRadioLabel);




/*
 * custom, lighter "lightbox" or rather just modal "window"
 */
function closeModal()
{
	$(".overlay:first").animate({ opacity: 0 }, 500, function() { $(this).remove(); });
	$(".modal:first").animate({ opacity: 0 }, 500, function() { $(this).remove(); $selects.removeAttr("style"); });
}


function openModal(url, data, callback)
{
	var $overlay = $("<div class=\"overlay\"></div>").animate({ opacity: 0 }, 0).appendTo("body").click(closeModal);
	if ($.browser.msie && $.browser.version == 6)
	{
		var pos = $("body").offset();
		$overlay.css("left", -pos.left + "px").height($("body").height()).width($(window).width());

		$(window).resize(function() { var pos = $("body").offset(); $overlay.css("left", -pos.left + "px").width($(window).width()); });
	}
	$overlay.animate({ opacity: .9 }, 500);

	var $modal = $("<div class=\"modal\"><div class=\"modal-tr\"><div class=\"modal-br\"><p class=\"close\"><a>[-]</a><div class=\"modal-contents\"></div></div></div></div>").appendTo("body").animate({ opacity: 0 }, 0);

	if (typeof url == "string")

		$.get(url, data, function(data)
		{
				
			var $contents = $modal.find(".modal-contents");
			var $data = $(data).appendTo($contents);
			$modal.find(".close a,a.close").click(function() { closeModal(); return false; });

			$selects.css("visibility", "hidden");
			var $mediaplayer = $modal.find("#player");
			var $images = $modal.find("img");
			if ($images.length > 0 && $mediaplayer.length <= 0)
			{
				var i = 0;
				$images.load(function()
				{
					i++;
					if (i >= $images.length/2 || $images.length ==1)
					{
						
						$modal.css({ width: ($data.outerWidth() + 30) + "px", marginTop: (($data.outerHeight() + 30) / -2) + "px", marginLeft: ($data.outerWidth() / -2) + "px" }).animate({ opacity: 1 }, 500, function() { if ($.browser.msie && $.browser.version == 7) this.style.removeAttribute("filter"); });
						$overlay.addClass("modal-loaded");
					}
				
				});
				
			}
			else
			{
				$modal.css({ width: ($data.outerWidth() + 30) + "px", marginTop: (($data.outerHeight() + 30) / -2) + "px", marginLeft: ($data.outerWidth() / -2) + "px" }).animate({ opacity: 1 }, 500, function() { if ($.browser.msie && $.browser.version == 7) this.style.removeAttribute("filter"); });
				$overlay.addClass("modal-loaded");
			
			}

			if (typeof callback == "function")
				callback($contents);
		}, "html");
		
}


function centerModal() { var $modal = $(".modal:first"); $modal.css({ marginTop: ($modal.outerHeight() / -2) + "px", marginLeft: ($modal.outerWidth() / -2) + "px" }); }


$("p.photo a").not(".button-download").click(function()
{
	openModal($(this).attr("href"));
	return false;
});


$(".gallery a").click(function()
{
	openModal($(this).attr("href"), null);
	return false;
});


$("a.button-download-modal").click(function()
{
	openModal($(this).attr("href"));
	return false;
});

function validateAndAddEmail(){

	return false;

}


$(".modal-gallery>ul>li>a").live("click", function()
{
	if ($(this).hasClass("selected"))
		return false;
	$(this).addClass("selected").parent().siblings().children("a").removeClass("selected");
	var $parent = $(this).closest(".modal-gallery");
	$parent.children("h1:first").text($(this).children("img").attr("alt"));
	
	if($parent.find(".media").length > 0){
		//$parent.find(".media").html(" ");
		
	
		var old = $parent.find(".media").contents().each(function(){
			$(this).unbind();
			$(this).remove();
		  });
		delete old
		
		var url = $(this).attr("href");
		$.get(url, function(data)
		{
			$parent.find(".media").html(data)
		});
		
	}else{
		$parent.find("img:first").attr("src", $(this).attr("href"));
		
	}
	
	if($(this).parent().next().find("a").length > 0 ){
		$parent.find(".button-next").removeClass('disabled');
		$parent.find(".button-next").attr("href", $(this).parent().next().find("a").attr("href"));
	
	}else{
		$parent.find(".button-next").addClass('disabled');
	}
	

	if($(this).parent().prev().find("a").length > 0 ){
		$parent.find(".button-previous").removeClass('disabled');
		$parent.find(".button-previous").attr("href", $(this).parent().prev().find("a").attr("href"));
	
	}else{
		$parent.find(".button-previous").addClass('disabled');
	}
	
	$parent.find(".description:first .copy").html($(this).next(".description-hidden").html());

	return false;
});

$(".modal-gallery").find("a.button-next").live("click",function(){
	var $parent = $(this).closest(".modal-gallery");
	var $next = $parent.find('.selected').parent().next("li").find("a");
	if($next.length > 0){
		$next.click();
		$(".modal-gallery ul").scrollTo(".selected");
	}
	return false;
});
$(".modal-gallery").find("a.button-previous").live("click",function(){
	var $parent = $(this).closest(".modal-gallery");
	var $prev = $parent.find('.selected').parent().prev("li").find("a");
	if($prev.length > 0){
		$prev.click();
		$(".modal-gallery ul").scrollTo(".selected");
	}
	return false;
});

// custom styled selects
var $selects = $("select").each(function()
{
	if ($.browser.msie && $.browser.version < 7)
		return false;

	$(this).before("<span class=\"select\"><strong>" + $(this).children("option:selected").text() + "</strong></span>").change(function() { $(this).prev().children().text($(this).children("option:selected").text()); });
	var w = $(this).outerWidth() - 23, h = 18;
	$(this).prev().css({ width: w + "px", height: h + "px" });
	if ($(this).attr("disabled"))
		$(this).prev().addClass("select-disabled");
});

$('select#region').change(function(){
		var selOpt = $(this).val().toLowerCase();
		var cty = $("select#subregion");
		var pageid = $("input#pageid").val();
		if(selOpt != 0){
		
		$.ajax({
    	    data:  "regionid="+selOpt+"&pageid="+pageid,
    	    type: "POST",
    	    url: "/ajax/getlocation",
    	    timeout: 20000,
    	    contentType: "application/x-www-form-urlencoded;charset=UTF-8",
    	    success: function(data){
																
				if(data.length > 0){
					cty.html(data);
					cty.removeAttr("disabled");
					cty.prev().removeClass("select-disabled");
					cty.prev().children().text(cty.children("option:selected").text());
				}else{
					var cloned = $(cty.children().get(0)).clone();
					cty.html("");
					cty.append(cloned);
					cty.attr("disabled","disabled");
					cty.prev().addClass("select-disabled");
					cty.prev().children().text(cty.children("option:selected").text());

				}
			}
		});

		
			/*$.post("/ajax/getlocation", "regionid="+selOpt+"&pageid="+pageid,function(data){
																
				if(data.length > 0){
					cty.html(data);
					cty.removeAttr("disabled");
					cty.prev().removeClass("select-disabled");
					cty.prev().children().text(cty.children("option:selected").text());
				}else{
					var cloned = $(cty.children().get(0)).clone();
					cty.html("");
					cty.append(cloned);
					cty.attr("disabled","disabled");
					cty.prev().addClass("select-disabled");
					cty.prev().children().text(cty.children("option:selected").text());

				}
			});*/
		}else{
			var cloned = $(cty.children().get(0)).clone();
			cty.html("");
			cty.append(cloned);
			cty.attr("disabled","disabled");
			cty.prev().addClass("select-disabled");
			cty.prev().children().text(cty.children("option:selected").text());
		}
})


$('select#utft_product').change(function(){
		var selOpt = $(this).val().toLowerCase();
		var cty = $("select#utft_prod_app");
		if(selOpt != 0){
		
			$.post("/ajax/getSubSolution", "id="+selOpt,function(data){
																
				if(data.length > 0){
					cty.html(data);
					cty.removeAttr("disabled");
					cty.prev().removeClass("select-disabled");
					cty.prev().children().text(cty.children("option:selected").text());
				}else{
					var cloned = $(cty.children().get(0)).clone();
					cty.html("");
					cty.append(cloned);
					cty.attr("disabled","disabled");
					cty.prev().addClass("select-disabled");
					cty.prev().children().text(cty.children("option:selected").text());

				}
			});
		}else{
			var cloned = $(cty.children().get(0)).clone();
			cty.html("");
			cty.append(cloned);
			cty.attr("disabled","disabled");
			cty.prev().addClass("select-disabled");
			cty.prev().children().text(cty.children("option:selected").text());
		}
})

/*$('.utft-go').click(function(){
	alert($('#utft_prod_app').val());
	var thelink = $('#utft_prod_app').val();
	if(thelink.match("http:")){
		window.open(thelink);
	}else{
		window.location=thelink;
	}
})*/

// rounded corners
$(".box-about li.left").append("<div class=\"corner corner-tl\"></div><div class=\"corner corner-bl\"></div>");
$(".box-about li.right").append("<div class=\"corner corner-tr\"></div><div class=\"corner corner-br\"></div>");
$(".items.persons p.photo").append("<div class=\"corner corner-br\"></div>");


// IE 6/7 fixes
if ($.browser.msie && $.browser.version <= 7)
{
	$("a:not([href])").attr("href", "#");

	$(".header li").hover
	(
		function() { $(this).addClass("hover").children("strong").addClass("hover"); $(this).children("ul").show(); }
	,	function() { $(this).removeClass("hover").children("strong").removeClass("hover"); $(this).find("ul").hide(); }
	);

	if ($.browser.version <= 6)
	{
		// please do not shorten code below to using toggleClass, as it can produce undesired results
		$("ul.three-boxes>li,ul.submenu strong,ul.submenu-alt>li,.box-main,ul.box-about>li").hover(function() { $(this).addClass("hover"); }, function() { $(this).removeClass("hover"); });

		$(".header>ul").hover(function() { $selects.css("visibility", "hidden"); }, function() { $selects.removeAttr("style"); });

		// fix transparent PNG photos of executive members
		$(".persons p.photo>img[src$=.png],.team p.photo>img[src$=.png]").each(function() { $(this).css("visibility", "hidden").parent().css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\"" + $(this).attr("src") + "\", sizingMethod = \"crop\"); "); });
	}
}




$("li.external a,a.external").attr("target","_blank");

/*$('.corners').wrap("<div class='photo'></div>").after("<p class='corners'></p>");*/


$(".header ul:first").children().each(function(){
	if ($(this).find("a.selected").length > 0 ){
		$(this).find("a:first").addClass("selected");
	}
});

$(".submenu").find("ul").andSelf().each(function(){
	$(this).children().each(function(){
		if ($(this).find("a.selected").length > 0 ){
			$(this).addClass("selected")
		}
	})
});



$(".submenu li.selected").each(function() { $(this).removeClass("selected").children("div").show(); $(this).children("strong:first").addClass("selected"); });
$(".submenu strong").click(function() { $(this).toggleClass("selected").next("div").toggle(); });

$(".utft-go").click(function(){
	var linkValue = $("#utft_prod_app").val();
	if(linkValue.match('http') ){
		window.open(linkValue);
	}else{
		window.location = linkValue;
	};
	return false;
	
})


