var mvc_url_link;
var mvc_view_id;
var mvc_item;
var mvc_counter=0;
var mvc_temp;
var mvcsn;
var isIE=document.all? true:false;
var all_mvc=new Array();
var right_stop=0;
var left_stop=0;
var move=0;
var all_mvc_num=0;
var action_ok =1;
var F_code = 0;
var stop_update = 0;
//	F_code = 1 (mvc video)
//	F_code = 2 (mvc e-catalog)
//  F_CODE = 3 (HD)


function closemvc(){


		if(!mvcsn) return;

		document.body.style.paddingRight="0px";
		document.body.style.overflow="";
		document.getElementById('myframe_div').style.display="none";
		document.getElementById('myframe_div').innerHTML="";

		if(document.getElementById('search_bar'))
		document.getElementById('search_bar').style.visibility="visible";




		if(!(mvc_item=="video"))
		{

			var request = ajaxFunction();
			var argu = "mvcsn="+mvcsn+"&close=1";
			request.open('POST','/ajax_sql.php');
			request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			request.onreadystatechange = function()
			{
				if(request.readyState == 4)
				{

				}
			}
			request.send(argu);


		}
		mvc_counter = 0;
		mvcsn = "";
		mvc_item	   = "";
		return;

	}
function closemvc2(){

		if(!mvcsn) return;

		document.body.style.paddingRight="0px";
		document.body.style.overflow="";
		document.getElementById('myframe_div').style.display="none";
		document.getElementById('myframe_div').innerHTML="";

		document.getElementById('search_bar').style.visibility="visible";

		mvc_counter = 0;

		mvcsn = "";
	}

function move_mvc(distance,action_flag)
{
	//alert("aaa");
	var now_final=0;
	var now_init=0;
	add_number = (action_flag)?46:-46;

	if(distance==0)
	{
		now_final = parseInt(all_mvc[all_mvc_num-1].style.left.replace("px",""))+parseInt(all_mvc[all_mvc_num-1].style.width.replace("px",""));
		now_init  = parseInt(all_mvc[0].style.left.replace("px",""));
		if(now_final <= 690)
		{
			right_stop=1;
			document.getElementById("next_hd").style.visibility="hidden";
		}
		else
			{
			right_stop=0;
			document.getElementById("next_hd").style.visibility="";
		}
		if(now_init>=0)
			{
			document.getElementById("pre_hd").style.visibility="hidden";
			left_stop=1;
		}
		else
			{
				document.getElementById("pre_hd").style.visibility="";
				left_stop=0;
			}

		action_ok = 1;
		return;
  }
	for(var i=0;i<all_mvc_num;i++)
	{
		var number = all_mvc[i].style.left;
		number=number.replace("px","");
		number=parseInt(number);
		number=number+add_number;
		number = number+"px";
		all_mvc[i].style.left=number;
	}

	distance-=46;
	setTimeout("move_mvc("+distance+","+action_flag+")",10);

	}

function view_MVC_HD(speed,do_action)
	{

		 	//alert(action_ok);

		 	if(right_stop==1 & (!do_action)) return;
			if(left_stop==1 & (do_action)) return;
			if(!action_ok) return;
		 	action_ok=0;

			var now_l=0;

			if(!all_mvc_num)
		 	{
				 	var ALL_hd2 = document.getElementsByTagName("div");
					var mvc_count = 0;
					for(var i=0;i<ALL_hd2.length;i++)
					{
						if(ALL_hd2[i].getAttribute('name')=="HD_number")
						{
							all_mvc[mvc_count] = ALL_hd2[i];

							mvc_count++;

						}
				 	}
			}





			all_mvc_num = all_mvc.length;
			if(!do_action)
			{
					 for(var i=0;i<all_mvc_num;i++)
						{
						 now_l=parseInt(all_mvc[i].style.width)+ parseInt(all_mvc[i].style.left);
						 if(parseInt(all_mvc[i].style.left) == 0)
						 {
						 	move = parseInt(all_mvc[i].style.width);
							break;
							}
						 if(now_l > 690)
						 {
						 	//alert("aaa");

						 	move = now_l - 690;

						 	break;
						 	}
						}
			}
		else
			{

				for(var i=all_mvc_num-1;i>=0;i--)
						{
						 now_l=parseInt(all_mvc[i].style.left);
						 if(now_l <0)
						 {
						 	move = 0-now_l;
							break;
							}
						}

				}

		if(!move) return;
		move_mvc(move,do_action);


		return;
	}

function fixPNG_one_mouse(myImage,pngid)
{
     var imgID = (myImage.id) ? "id='" + myImage.id + "' " : "";
	   var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : "";
	   var imgTitle = (myImage.title) ?
		             "title='" + myImage.title  + "' " : "title='" + myImage.alt + "' ";
	   var imgStyle = "display:inline-block;" + myImage.style.cssText
	   var strNewHTML = "<span " + imgID + imgClass + imgTitle
                  + " style=\"" + "width:" + myImage.width
                  + "px; height:" + myImage.height
                  + "px;" + imgStyle + ";"
                  + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                  + "(src=\'" + myImage.src + "\', sizingMethod='image');\""
                  + " ></span>";

	   myImage.outerHTML = strNewHTML;
	   return false;
}

function ajaxFunction(){
	var xmlHttp;
	var x = new Array();
	x[1]='new XMLHttpRequest();';
	x[0]="new ActiveXObject('Msxml2.XMLHTTP');";
	x[2]="new ActiveXObject('Microsoft.XMLHTTP');";
	var status=0;
	var i=0;
	while(i<x.length){
		try{xmlHttp=eval(x[i]);break;
		}catch(e){}
		i++;
	}
	return xmlHttp;
}
function fixPNG_ones(myImage)
{
    var arVersion = navigator.appVersion.split("MSIE")
		var version = parseFloat(arVersion[1])
    if ((version >= 5.5) && (version < 7) && (document.body.filters))
    {

     var imgID = (myImage.id) ? "id='" + myImage.id + "' " : ""
	   var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : ""
	   var imgTitle = (myImage.title) ?
		             "title='" + myImage.title  + "' " : "title='" + myImage.alt + "' "
	   var imgStyle = "display:inline-block;" + myImage.style.cssText
	   var strNewHTML = "<div " + imgID + imgClass + imgTitle
                  + " style=\"" + "width:" + myImage.width+"visibility:visible"
                  + "px; height:" + myImage.height
                  + "px;" + imgStyle + ";"
                  + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                  + "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></div>"

	   myImage.outerHTML = strNewHTML
		 //alert(myImage.outerHTML);
		return;
    }
    return;
}



function preload_png()
{
	var loadimg1 = new Image();
	var loadimg2 = new Image();
	loadimg1.src = "/images/HD_off.png";
	loadimg2.src = "/images/inquire.png";
	}

function view_respose(opid,view_type)
{
	var D =  new Date();
	mvcsn = D.getTime();  //控制關閉時作用,需有值
	var request = ajaxFunction();
	var ajax_url = "/ajax_sql.php";
	var url_link = mvc_url_link ;
	var argu = "id="+opid+"&view_mvc="+view_type;

	if(mvc_item=='video')
	{
		argu = "newname="+mvc_url_link;
	}

	request.open('POST',ajax_url);
	request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	request.onreadystatechange = function()
	{
		if(request.readyState == 4)
		{
			if(mvc_item=='video')
			{
				response = request.responseText.split("|");
				document.getElementById('view_'+opid).innerHTML = response[0];
				url_link = response[1];
			}
			else
				{
					response = request.responseText.split("|");
					mvc_temp = response[0];
					url_link +=  "/"+"mvcsn="+mvcsn+"&"+mvc_temp;
				}

			document.getElementById('myframe_x').src=url_link;
			setTimeout("iframe_big()",1000);
			document.getElementById('myframe_div').style.display="block";

			mvc_url_link = "";
			mvc_view_id  = "";
			mvc_counter=1;
		}
	}
	request.send(argu);
}


function view_respose2(opid)
{
			var D =  new Date();
			mvcsn = D.getTime();
			var ajax_url = "/ajax_sql.php";
			var url_link = mvc_url_link ;
			var video_bandwidth = usecookie("get","video_bandwidth");

			var request = ajaxFunction();
			var argu = "newname="+mvc_url_link;
			request.open('POST',ajax_url);

			request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

			request.onreadystatechange = function()
			{
				if(request.readyState == 4)
				{
					response = request.responseText.split("|");
					if(opid != '0')  //目前sd不需顯示view
					{
						document.getElementById('view_'+opid).innerHTML = response[0];
					}

					//url_link = response[1];
					url_link = response[1]+"&bw="+video_bandwidth;
					document.getElementById('myframe_x').src=url_link;
					setTimeout("iframe_big()",1000);
					document.getElementById('myframe_div').style.display="block";

					mvc_url_link = "";
					mvc_view_id  = "";
					mvc_item	   = "";
					mvc_counter=1;

				}
			}
			request.send(argu);

		return;
		//alert("qqq");
}

function change_pic(imgid,opid)
{
	if(opid=="0") document.getElementById(imgid).src="/images/HD_off.png";
	else	document.getElementById(imgid).src="/images/HD_on.png";
}

function change_pic2(imgid,opid)
{
	var newPreview  = document.getElementById(imgid);
	var changepic = "";
	if(opid=="0") changepic="/images/HD_off.png";
	else changepic="/images/HD_on.png";

	if(isIE)
	newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = changepic;
	else
	{
		//alert(newPreview.style.backgroundImage);
		//newPreview.style.backgroundImage = "aaa";
		//alert("url("+changepic+")");
		//alert(newPreview.style.backgroundImage);
	}
	}

function go_to()
{
	if(mvc_counter==0)
		{
			var view_id  = mvc_view_id;
			view_respose(view_id,mvc_item);
		}
	else{  closemvc(); }
}


function go_to2()
{
	if(mvc_counter==0)
		{
			var view_id  = mvc_view_id;
			view_respose2(view_id);
		}
	else{  closemvc(); }
}

function show_hd_section(url_link,item,view_id){
mvc_url_link = url_link;
mvc_view_id  = view_id;
mvc_item    = item;

if(item=="video")
{
	record_video_info();
	return false;
	}

var iframe_top = document.body.scrollTop;
var iframe_style = "position:absolute;left:0px;z-index:99;width:0px;height:0px;";
var iframe_str = "<iframe id='myframe_x' allowTransparency='true' name='myframe_xx' frameborder='NO' border='1' src='/ajax_sql.php?showjs=3'  style='"+iframe_style+"'></iframe>";

document.getElementById('search_bar').style.visibility="hidden";

document.getElementById('myframe_div').style.zIndex="99";
document.getElementById('myframe_div').style.position="absolute";
document.getElementById('myframe_div').style.top="0px";

document.getElementById('myframe_div').innerHTML=iframe_str;
}

function show_hd_section2(url_link,item,view_id){


mvc_url_link = url_link;
mvc_view_id  = view_id;
mvc_item    = item;

if(item=="video")
{
	record_video_info();
	return false;
	}

var iframe_top = document.body.scrollTop;
var iframe_style = "position:absolute;left:0px;z-index:99;width:0px;height:0px;";
var iframe_str = "<iframe id='myframe_x' allowTransparency='true' name='myframe_xx' frameborder='NO' border='1' src='/ajax_sql.php?showjs=2'  style='"+iframe_style+"'></iframe>";

document.getElementById('myframe_div').innerHTML=iframe_str;
}

function iframe_big()
{
		var iframe_width = document.body.clientWidth;
		var iframe_height = document.body.clientHeight;
		var iframe_top = document.body.scrollTop;

		var a = document.body.scrollWidth;
		document.body.scroll = "no";
		var b = document.body.scrollWidth;
		document.body.scroll = "yes";

		document.getElementById('myframe_x').style.width=screen.width;
		document.getElementById('myframe_x').style.height=iframe_height;
		document.getElementById('myframe_x').style.top=iframe_top;
		document.body.style.paddingRight=b-a;
		document.body.style.overflow="hidden";
		return;
	}

function getsearch(_self)
{

	if(_self.options[_self.selectedIndex].value=="0")
	{
	document.getElementById("show_keyword").innerHTML="";
	return;
	}
	var request = ajaxFunction();
	var argu = "keyword_id="+_self.options[_self.selectedIndex].value;
	var select_str="<select onChange='location=this.options[this.selectedIndex].value' class= 'input_combobox'>";
	select_str+="<option value=''>Please select sub-category</option>";
	request.open('POST','/ajax_sql.php');
	request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	request.onreadystatechange = function()
	{
		if(request.readyState == 4)
		{
			response = request.responseText.split("|");

			var array_num  = response.length;
			for(var i=0;i<array_num;i++)
				{
				var select_value = response[i].split("*");
				select_str+="<option value='"+select_value[1]+"'>"+select_value[0]+"</option>";
				}
			select_str+="</select>";
			document.getElementById("show_keyword").innerHTML=select_str;

			}
	}
	request.send(argu);


}
function record_video_info()
{
	var request = ajaxFunction();
	var ajax_url = "/ajax_sql.php";
	var url_link = mvc_url_link ;
	argu = "newname="+mvc_url_link;
	request.open('POST',ajax_url);
	request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	request.send(argu);
	request.onreadystatechange = function()
			{
				if(request.readyState == 4)
				{
					response = request.responseText.split("|");
					if(mvc_view_id != '0')
					{
						document.getElementById('view_'+mvc_view_id).innerHTML = response[0];
					}

				}
			}
	return;
}