﻿// JScript 檔
var gUrl='http://comm.im.tv';

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
function SearchHotKey(k){
	document.forms['searchform'].elements['q'].value=k;
	document.forms['searchform'].elements['stype'].value=thisService;
	document.forms['searchform'].submit();
}

//images
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//-------------cookie
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}
//---------------style
function setActiveStyleSheet(s) {
  var i, a, main;
  var title = (s=="b")?"imsearch_black":"imsearch_white"
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
			a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  document.bgColor = document.bgColor;
  //setStyleCookie()
}
function getActiveStyleSheet() {
  var i, a;
	var nss = "imsearch_white"
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) nss = a.getAttribute("title");
  }
  return (nss=="imsearch_white")?"w":"b";
}
function getPreferredStyleSheet() {
  var i, a;
	var pss = "imsearch_white"
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("rel").indexOf("alt") == -1 && a.getAttribute("title"))
    pss = a.getAttribute("title");
  }
  return (pss=="imsearch_white")?"w":"b";
}
function setStyleCookie(){
  var title = getActiveStyleSheet();
  createCookie("imss", title, 365);
}
//-------------active style
window.onload = function(e) {
  var cookie = readCookie("imss");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}
window.onunload = setStyleCookie;
var cookie = readCookie("imss");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

function Install_Accelerator(st){
	var str_acc;
	var scc_float = (st=="r")?"float:right;margin-right:75px;":"float:left;margin-top:20px;";
	if(navigator.appVersion.toLowerCase().indexOf("msie 8") >= 0){
		str_acc="<div style=\""+scc_float+"margin-bottom:10px;\"><img src=\"/ie8/acc.png\" align=\"absmiddle\">方便搜尋更多影音內容, <a href=\"/ie8/\" target=\"_blank\">使用 imtv影音搜尋加速器</a> <input type=\"button\" value=\"安裝\" class=\"bu\" onclick=\"window.external.addService('http://www.im.tv/ie8/imacc.xml')\" /></div><div class=\"clear\"></div>";
	}else{
		str_acc="<div style=\""+scc_float+"margin-bottom:10px;\"><img src=\"/ie8/acc.png\" align=\"absmiddle\">方便搜尋更多影音內容, 使用 <a href=\"/ie8/download.htm\" target=\"_blank\">ie8</a>及 <a href=\"/ie8/\" target=\"_blank\">imtv影音搜尋加速器</a></div><div class=\"clear\"></div>";
	}
	document.write(str_acc);
}

//-------------preview video 20090917
var imblargus, imbmargus;
function imPreviewVideo(t,u,fid,idx,p){
	imblargus = 'i='+idx+'&p='+p+'&l='+encodeURIComponent(u);
	imbmargus = 'method=outtrack&title='+encodeURIComponent(t);
	ta = '<a href="'+u+'" target="_blank">'+t+'</a>';
	flashh = (checkIE6() || checkIE7())?'424':'390';
	iframeh = (checkIE6() || checkIE7())?514:548;
	$.get("/imsearch/getembedsrc.ashx", { u: encodeURIComponent(u) },
	function(data){
		if(data!=""){
		  $("#embedBlock").flashembed({src: data, w3c: true, id: "vlog"+fid});
		  $.fn.colorbox({open:true, width:'455px', height:flashh+'px', title:ta, inline:true, href:"#embedWarper", close:'關閉預覽'});
		  $("#embedWarper").css("margin-left","auto");
		  $("#embedWarper").css("margin-right","auto");
		  $("#embedWarper").show();
		  imbmargus+='&src='+encodeURIComponent(data);
		}else{
		  $.fn.colorbox({href:u, open:true, iframe:true, width:'600px', height:'600px', title:ta, close:'關閉預覽'});
		  imbmargus+='&url='+encodeURIComponent(u);
		  $("#cboxLoadedContent").height(iframeh);
		}
		if(checkIE6()==true)
		  setTimeout(ie6fix,600);
		
		$("#cboxTitle").after("<div id='im_cboxToolTip' style='display:none;'></div>");	
		$("#im_cboxToolTip").after("<div id='im_cboxAddToBlog'><img width='16' height='16' title='轉貼到blog' alt='轉貼到blog'></div>");
		$("#im_cboxAddToBlog").click(function(){
		  if(checkIE6()==true || checkIE7()==true)
				location.href('/newbackend/addarticle.asp?'+imbmargus);
			else
				window.open("/newbackend/addarticle.asp?"+imbmargus,"mywindow");
		});
		$("#im_cboxAddToBlog").hover(
	    function () {
	      showToolTip("b");
	    }, 
	    function () {
	      $("#im_cboxToolTip").hide();
	    }
	  );
		if(fid==""){
			$("#im_cboxAddToBlog").after("<div id='im_cboxBrokenLink'><img src='/imsearch/img/1.gif' width='16' height='16' title='回報失效連結' alt='回報失效連結'></div>");
			$("#im_cboxBrokenLink").click(function(){
				$.get('/imsearch/reportbrokenlink.ashx?'+imblargus);
				alert("謝謝您的回報資料!");
				$.fn.colorbox.close()
			});
			$("#im_cboxBrokenLink").hover(
	      function () {
	        showToolTip("r");
	      }, 
	      function () {
	        $("#im_cboxToolTip").hide();
	      }
	    );
		}	
	});
}

function ie6fix(){
	$(document).scrollTop($(document).scrollTop()+1);
	$(document).scrollTop($(document).scrollTop()-1);
}

function ImgError(source){
    source.src = "/imsearch/img/default_web_video_image.jpg";
    source.onerror = "";
    return true;
}

function showToolTip(t){
	if(t=="b"){
		//$("#im_cboxToolTip").css("left","6px");
		$("#im_cboxToolTip").text("轉貼到blog");
	}else{
		//$("#im_cboxToolTip").css("left","24px");
		$("#im_cboxToolTip").text("回報失效連結");
	}
	$("#im_cboxToolTip").fadeIn("fast");
}

function checkIE6(){	
	var rtn=false;
	jQuery.each(jQuery.browser, function(i, val) {
	  if(i=="msie" && jQuery.browser.version.substr(0,3)=="6.0")
	     rtn=true;
	});
	return rtn;
}

function checkIE7(){	
	var rtn=false;
	jQuery.each(jQuery.browser, function(i, val) {
	  if(i=="msie" && jQuery.browser.version.substr(0,3)=="7.0")
	     rtn=true;
	});
	return rtn;
}
