// global variables
var flash_mv_ready = false;
var flash_mv_now = -1;
var flash_mv_vol = 100;
var flash_mv_obj = null;
var flash_mv_panel_moving = 0;
var flash_mv_panel_initstep = 5;
var flash_mv_speedup_factor = 1.25;
var flash_mv_timeout = 5000;
var flash_mv_timer_id = null;
var objFlashMVBlock = null;
var objFlashMVContent = null;
var objFlashMVLayout = null;
var objFlashMVAlbumInfo = null;
var objFlashMVActivities = null;
var _is_ie = (navigator.appName && navigator.appName.indexOf("Microsoft") != -1);
var _rect_offset_x = null;
var _rect_offset_y = null;

function do_fscmd(cmd, args) {
  switch (cmd) {
  case 'flash_mv_vol':
    flash_mv_vol = Math.round(args);
    _setCookie('FlashMVVol', flash_mv_vol);
    break;
  case 'flash_mv_playing':
    _flash_mv_hide_obj();
    break;
  case 'flash_mv_complete':
    setTimeout('flash_mv_complete()', 500);
    break;
  }
}

function flash_mv_preload_images(dir, arr_img) {
  var ret = new Array();
  var num = arr_img.length;
  for (var i = 0; i < num; i++) {
    ret[i] = new Image();
    ret[i].src = (dir + arr_img[i]);
  }
  return ret;
}

function _setCookie(name, val, days_to_expire) {
  var d = 30;
  if (arguments.length < 2 || arguments.length > 3) return;
  if (arguments.length == 3) d = days_to_expire;
  var exp = new Date();
  exp.setTime(exp.getTime() + d * 1000 * 60 * 60 * 24);
  document.cookie = name + '=' + escape(val)
		  + ';expires=' + exp.toGMTString()
		  + ';path=/;domain=.kkbox.com.tw';
}

function _getCookie(name) {
  var s, e;
  if (document.cookie.length > 0) {
    if ((s = document.cookie.indexOf(name + "=")) != -1) {
      s += name.length + 1;
      if ((e = document.cookie.indexOf(";", s)) == -1) e = document.cookie.length;
      return unescape(document.cookie.substring(s, e));
    }
  }
  return '';
}

function _flash_mv_hide_obj() {
  objFlashMVAlbumInfo.style.display = 'none';
  objFlashMVActivities.style.display = 'none';
  document.getElementById('flash_mv_wmv').style.display = 'none';
}

// mouse events and timer event
function flash_mv_mover(ev) {
  var obj, mat, idx;
  var do_change = false;
  var run_timer = false;
  if (flash_mv_ready) {
    if (arguments.length == 2) { // timer
      idx = arguments[0];
      if (arguments[0] != flash_mv_now) {
	obj = document.getElementById('flash_mv_item_' + idx);
	do_change = true;
	if (flash_mv_run_timer == true) run_timer = true;
      }
    } else { // mouse
      obj = (_is_ie ? event.srcElement : ev.target);
      mat = obj.id.match(/^flash_mv_item_(\d+)$/);
      if (mat && mat.length == 2) {
	idx = parseInt(mat[1]);
	if (mat[1] != flash_mv_now) {
	  do_change = true;
	}
      } else {
	return;
      }
    }

    if (do_change == true) {
      if (idx != flash_mv_idx) {
	if (flash_mv_idx != flash_mv_now) {
	  document.getElementById('flash_mv_item_' + flash_mv_idx).className = 'flash_mv_item';
	}
	flash_mv_idx = idx;
      }
      obj.className = 'flash_mv_item_ov';
    }
    document.getElementById('flash_mv_ss').src = flash_mv_ss[idx].src;

    if (run_timer == true && flash_mv_now == -1) {
      flash_mv_timer_id = setTimeout('flash_mv_mover(' + ((flash_mv_idx + 1) % flash_mv_num) + ',0)', flash_mv_timeout);
    } else {
      if (flash_mv_timer_id != null) {
	clearTimeout(flash_mv_timer_id);
	flash_mv_timer_id = null;
      }
    }
  }
}

// mouse events
function flash_mv_mout(ev) {
  var obj, mat, idx = -1;
  if (flash_mv_ready) {
    var obj = (_is_ie ? event.srcElement : ev.target);
    var mat = obj.id.match(/^flash_mv_item_(\d+)$/);
    if (mat && mat.length == 2) {
      if (flash_mv_timer_id != null) {
	clearTimeout(flash_mv_timer_id);
	flash_mv_timer_id = null;
      }
      flash_mv_idx = parseInt(mat[1]);
      if (flash_mv_now >= 0) {
	if (flash_mv_idx != flash_mv_now) {
	  obj.className = 'flash_mv_item';
	  document.getElementById('flash_mv_ss').src = flash_mv_ss[flash_mv_now].src;
	}
      } else {
	if (flash_mv_run_timer == true) {
	  flash_mv_timer_id = setTimeout('flash_mv_mover(' + ((flash_mv_idx + 1) % flash_mv_num) + ',0)', flash_mv_timeout);
	} else {
	  obj.className = 'flash_mv_item';
	}
      }
    }
  }
}

// mouse events
function flash_mv_click(ev) {
  if (flash_mv_ready && !flash_mv_panel_moving) {
    var obj = (_is_ie ? event.srcElement : ev.target);
    var mat = obj.id.match(/^flash_mv_item_(\d+)$/);
    if (mat && mat.length == 2) {
      if (flash_mv_timer_id != null) {
	clearTimeout(flash_mv_timer_id);
	flash_mv_timer_id = null;
      }
      if (mat[1] != flash_mv_now) {
	if (flash_mv_now >= 0) {
	  document.getElementById('flash_mv_item_' + flash_mv_now).className = 'flash_mv_item';
	}
	obj.className = 'flash_mv_item_selected';
	flash_mv_now = parseInt(mat[1]);

	flash_mv_click_cnt(1);

	if(ap!='WEB'){ 
	  document.getElementById('func_frame').src = 'kkbox://playctrl_pause/';
	}

	// processing flash panel, now (flash_mv_panel_moving == 0)
	if (objFlashMVLayout.style.display == '') { // already open
	  flash_mv_play();
	} else {
	  flash_mv_open();
	}
      }
    }
  }
}

function flash_mv_panel_move(step) {
  var h = parseInt(objFlashMVBlock.style.height) + step;

  if (step > 0) { // opening
    objFlashMVBlock.style.display = '';
    if (h >= flash_mv_h) {
      objFlashMVBlock.style.height = flash_mv_h;
      flash_mv_panel_moving = 0;
      objFlashMVLayout.style.display = '';
      setTimeout('flash_mv_play()', _is_ie ? 0 : 300);
      return;
    }
  } else { // closing
    if (h <= 0) {
      objFlashMVBlock.style.height = 0;
      objFlashMVBlock.style.display = 'none';
      flash_mv_panel_moving = 0;
      if (flash_mv_now >= 0) {
	flash_mv_idx = flash_mv_now;
	flash_mv_now = -1;
	if (flash_mv_run_timer == true) {
	  document.getElementById('flash_mv_item_' + flash_mv_idx).className = 'flash_mv_item_ov';
	  flash_mv_timer_id = setTimeout('flash_mv_mover(' + ((flash_mv_idx + 1) % flash_mv_num) + ',0)', flash_mv_timeout);
	} else {
	  document.getElementById('flash_mv_item_' + flash_mv_idx).className = 'flash_mv_item';
	}
      }
      return;
    }
  }

  objFlashMVBlock.style.height = h;
  setTimeout('flash_mv_panel_move(' + (step * flash_mv_speedup_factor) + ')', 50);
}

function _flash_mv_rect_old(obj) {
  var rect;
  if (_is_ie) {
    if (_rect_offset_x == null || _rect_offset_y == null) {
      var tmpobj = document.createElement('div');
      tmpobj.style.position = 'absolute';
      tmpobj.style.pixelLeft = -1000;
      tmpobj.style.pixelTop = -1000;
      document.body.appendChild(tmpobj);
      var tmprect = tmpobj.getBoundingClientRect();
      _rect_offset_x = (tmprect.left + document.body.scrollLeft) - tmpobj.style.pixelLeft;
      _rect_offset_y = (tmprect.top + document.body.scrollTop) - tmpobj.style.pixelTop;
      tmpobj.removeNode(true);
    }
    rect = obj.getBoundingClientRect();
    rect.left = rect.left - _rect_offset_x + document.body.scrollLeft;
    rect.top = rect.top - _rect_offset_y + document.body.scrollTop;
    rect.right = rect.right - _rect_offset_x + document.body.scrollLeft;
    rect.bottom = rect.bottom - _rect_offset_y + document.body.scrollTop;
  } else {
    rect = document.getBoxObjectFor(obj);
    rect.left = rect.x;
    rect.top = rect.y;
    rect.right = rect.left + rect.width;
    rect.bottom = rect.top + rect.height;
  }
  return rect;
}
function _flash_mv_rect(obj) {
  var rect = {x:0, y:0, width:obj.offsetWidth, height:obj.offsetHeight};
  while (obj) {
    rect.x += obj.offsetLeft;
    rect.y += obj.offsetTop;
    obj = obj.offsetParent;
  }
  rect.left = rect.x;
  rect.top = rect.y;
  rect.right = rect.left + rect.width;
  rect.bottom = rect.top + rect.height;
  return rect;
}

function flash_mv_obj_resetpos(id) {
  var obj, rect1, rect2;
  if (document.getElementById('flash_mv_bridge').style.display != 'none' || id == 'flash_mv_bridge') {
    obj = document.getElementById('flash_mv_bridge');
    rect1 = _flash_mv_rect(document.getElementById('flash_mv_item_' + flash_mv_now));
    if (parseInt(obj.style.width) == 0) {
      rect2 = _flash_mv_rect(objFlashMVBlock);
      obj.style.width = rect2.left - (rect1.right + 2);
    }
    obj.style.left = rect1.right + 2 + 'px';
    obj.style.top = rect1.top + (rect1.bottom - rect1.top - parseInt(obj.style.height)) / 2 - (navigator.userAgent.indexOf('Safari') != -1 ? 4 : 0) + 'px';
  }
  if (objFlashMVAlbumInfo.style.display != 'none' || id == 'flash_mv_album_info') {
    obj = document.getElementById('flash_mv_album_info');
    rect1 = _flash_mv_rect(objFlashMVBlock);
    obj.style.left = (rect1.left + 72) + 'px';
    obj.style.top = (rect1.top + 25) + 'px';
  }
  if (objFlashMVActivities.style.display != 'none' || id == 'flash_mv_activities') {
    obj = document.getElementById('flash_mv_activities');
    rect1 = _flash_mv_rect(objFlashMVBlock);
    obj.style.left = (rect1.left + 72) + 'px';
    obj.style.top = (rect1.top + 150) + 'px';
  }
  if (document.getElementById('flash_mv_wmv').style.display != 'none' || id == 'flash_mv_wmv') {
    obj = document.getElementById('flash_mv_wmv');
    rect1 = _flash_mv_rect(objFlashMVBlock);
    obj.style.left = (rect1.left + 380) + 'px';
    obj.style.top = (rect1.top + 250) + 'px';
  }
}

function flash_mv_bridge(act) {
  var bri = document.getElementById('flash_mv_bridge');
  if (act == 'show') {
    flash_mv_obj_resetpos('flash_mv_bridge');
    bri.style.display = '';
  } else {
    bri.style.display = 'none';
  }
}

function flash_mv_open() {
  flash_mv_obj = new flash_mv_object();
  flash_mv_panel_moving = 1;
  flash_mv_panel_move(flash_mv_panel_initstep);
}

function flash_mv_close() {
  flash_mv_stop();
  flash_mv_bridge('hide');
  objFlashMVAlbumInfo.style.display = 'none';
  objFlashMVActivities.style.display = 'none';
  objFlashMVLayout.style.display = 'none';
  document.getElementById('flash_mv_wmv').style.display = 'none';
  flash_mv_panel_moving = -1;
  flash_mv_panel_move(-flash_mv_panel_initstep);

  return false;
}

function flash_mv_stop() {
  if (flash_mv_obj != null) {
    flash_mv_obj.stop();
  }
}

function flash_mv_play() {
  _flash_mv_hide_obj();

  if (flash_mv_obj == null) {
    flash_mv_obj = new flash_mv_object();
  }
  flash_mv_obj.play();
  flash_mv_bridge('show');

  // preloading album cover silently
  if (flash_mv_album_cover[flash_mv_now] == null || flash_mv_album_cover[flash_mv_now] == undefined) {
    flash_mv_album_cover[flash_mv_now] = new Image();
    flash_mv_album_cover[flash_mv_now].src = flash_mv_album_cover_url[flash_mv_now];
  }
}

function flash_mv_complete() {
  var descr;
  var rect = null;

  var bg = document.getElementById('flash_mv_content');
  if(bg != null) {
    if(bg.offsetHeight <= 300){
      return;
    }
  }

  if (flash_mv_album_id[flash_mv_now] != '') {
    descr  = '<A HREF="' + flash_mv_album_url[flash_mv_now] + '" CLASS="flash_mv_album_link" onclick="return flash_mv_click_cnt(2)">' + flash_mv_album_name[flash_mv_now] + '</A><BR>';
    descr += '<A HREF="' + flash_mv_artist_url[flash_mv_now] + '" CLASS="flash_mv_album_link">' + flash_mv_artist_name[flash_mv_now] + '</A><BR>';
    if (flash_mv_album_descr[flash_mv_now].length >= 5) {
      descr += flash_mv_album_descr[flash_mv_now] + ' [<A CLASS="flash_mv_album_link" HREF="' + flash_mv_album_url[flash_mv_now] + '" onclick="return flash_mv_click_cnt(2)">更多</A>]';
    }

    document.getElementById('flash_mv_album_link').href = flash_mv_album_url[flash_mv_now];
    document.getElementById('flash_mv_album_cover').src = flash_mv_album_cover[flash_mv_now].src;
    document.getElementById('flash_mv_album_descr').innerHTML = descr;

    flash_mv_obj_resetpos('flash_mv_album_info');
    objFlashMVAlbumInfo.style.display = '';
  }

  if (flash_mv_activities_title[flash_mv_now] != '') {
    var tg = '';
    if (flash_mv_activities_newpage[flash_mv_now] == 1) {
      tg = ' TARGET="_blank"';
    }
    descr  = '<A HREF="' + flash_mv_activities_url[flash_mv_now] + '" CLASS="flash_mv_album_link"' + tg + ' onclick="return flash_mv_click_cnt(3)">' + flash_mv_activities_title[flash_mv_now] + '</A><BR>';
    if (flash_mv_activities_descr[flash_mv_now].length >= 5) {
      descr += flash_mv_activities_descr[flash_mv_now] + ' [<A CLASS="flash_mv_album_link" HREF="' + flash_mv_activities_url[flash_mv_now] + '"' + tg + ' onclick="return flash_mv_click_cnt(3)">更多</A>]';
    }

    document.getElementById('flash_mv_activities_link').href = flash_mv_activities_url[flash_mv_now];
    if (tg != '') {
      document.getElementById('flash_mv_activities_link').target = '_blank';
    } 
    document.getElementById('flash_mv_activities_cover').src = flash_mv_activities_img[flash_mv_now].src;
    document.getElementById('flash_mv_activities_descr').innerHTML = descr;

    flash_mv_obj_resetpos('flash_mv_activities');
    objFlashMVActivities.style.display = '';
  }

  if (flash_mv_wmv_url[flash_mv_now] != '') {
    var _obj = document.getElementById('flash_mv_wmv');
    document.getElementById('flash_mv_wmv_link').href = flash_mv_wmv_url[flash_mv_now];
    flash_mv_obj_resetpos('flash_mv_wmv');
    _obj.style.display = '';
  }
}

flash_mv_object = function (_swf, _id, _w, _h, _bgc) {
  var swf = (_swf != null) ? _swf : flash_mv_swf;
  var nid  = (_id  != null) ? _id  : flash_mv_id;
  var w   = (_w != null)   ? _w   : flash_mv_w; 
  var h   = (_h != null)   ? _h   : flash_mv_h;
  var bgc = (_bgc != null) ? _bgc : '#111111';
  var first_time = true;

  this.play = function (_vol) {
    if (flash_mv_now < 0 || flash_mv_now >= flash_mv_num) return false;
    if (first_time == true) {
      first_time = false;
      var target = document.getElementById(flash_content_id);
      var html = '';
      var vol = (_vol != null) ? _vol : flash_mv_vol;
      var flashvars = 'flash_mv_url=' + this.getFlvPath() + '&flash_mv_vol=' + vol;

      if (_is_ie) {
	html += '<OBJECT CLASSID="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"';
	html += ' CODEBASE="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"';
	html += ' WIDTH="' + w + '" HEIGHT="' + h + '" ID="' + nid + '" ALIGN="middle">';
	html += '<PARAM NAME="allowScriptAccess" VALUE="sameDomain" />';
	html += '<PARAM NAME="movie" VALUE="' + swf + '" />';
	html += '<PARAM NAME="quality" VALUE="high" />';
	html += '<PARAM NAME="wmode" VALUE="transparent" />';
	html += '<PARAM NAME="bgcolor" VALUE="' + bgc + '" />';
	html += '<PARAM NAME="flashvars" VALUE="' + flashvars + '" />';
	html += '</OBJECT>';
      } else {
	html += '<EMBED SRC="' + swf + '" QUALITY="high" BGCOLOR="' + bgc + '"';
	html += ' WIDTH="' + w + '" HEIGHT="' + h + '" SWLiveConnect="true"';
	html += ' NAME="' + nid + '" ALIGN="middle" AllowScriptAccess="sameDomain"';
	html += ' WMODE="transparent"';
	html += ' TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"';
	html += ' flashvars="' + flashvars + '" />';
      }

      if(flashinstalled == 1){
        html = "<div style=\"width:400px; height:328px; background-color:#111122;\" align=\"center\"><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p style=\"color:#FFFFFF\">因為您現在沒有安裝Flash Player, 所以沒有辦法看到強打MV喔!</p><a href=\"kkbox://http://www.adobe.com/go/getflashplayer/\"><p style=\"color:#FFFFFF\">按此進入安裝頁面! </p></a></div>";
      }

      target.innerHTML = html;

      if (_is_ie == true) {
	document.getElementById(nid).attachEvent('FSCommand', do_fscmd);
      } 
    } else {
      var swfobj = this.getFlashMovieObject(nid);
      if (swfobj != null) {
	swfobj.SetVariable('flash_mv_url', this.getFlvPath());
      }
    }

    return true;
  }

  this.getFlashMovieObject = function (movieName) {
    if (window.document[movieName]) return window.document[movieName];
    if (_is_ie == false) {
      if (document.embeds && document.embeds[movieName]) return document.embeds[movieName]; 
    } else {
      return document.getElementById(movieName);
    }
  }

  this.stop = function () {
    var swfobj = this.getFlashMovieObject(nid);
    if (swfobj != null) {
      try {
        swfobj.SetVariable('flash_mv_stop', 1);
      } catch(e){
      }
    }
  }

  this.getFlvPath = function () {
    if (flash_mv_file[flash_mv_now].indexOf(':') == -1) {
      return (flash_mv_dir + flash_mv_file[flash_mv_now]);
    } else {
      return flash_mv_file[flash_mv_now];
    }
  }
}

function flash_mv_clear_timer() {
  flash_mv_run_timer = false;
}

function flash_mv_init() {
  if (flash_mv_num == 0) {
    return true;
  }

  var tmp;
  if ((tmp = _getCookie('FlashMVVol')) != '') {
    flash_mv_vol = parseInt(tmp);
  }

  document.getElementById('flash_mv_items').onclick = flash_mv_click;
  document.getElementById('flash_mv_items').onmouseover = flash_mv_mover;
  document.getElementById('flash_mv_items').onmouseout = flash_mv_mout;
  document.getElementById('flash_mv_table').title = '';

  objFlashMVBlock = document.getElementById('flash_mv_block');
  objFlashMVContent = document.getElementById(flash_content_id);
  objFlashMVLayout = document.getElementById('flash_mv_layout');
  objFlashMVAlbumInfo = document.getElementById('flash_mv_album_info');
  objFlashMVActivities = document.getElementById('flash_mv_activities');

  if (_is_ie) {
    window.attachEvent('onresize', flash_mv_obj_resetpos);
  } else {
    window.addEventListener('resize', flash_mv_obj_resetpos, false);  
  }

  flash_mv_ready = true;

  if (flash_mv_run_timer == true) {
    flash_mv_mover(flash_mv_idx,0);
  } else {
    document.getElementById('flash_mv_ss').src = flash_mv_ss[flash_mv_idx].src;
  }

  setTimeout('flash_mv_clear_timer()', 10 * 60 * 1000);
}

function flash_mv_click_cnt(cate_id) {
  if (document.getElementById('func_frame') && flash_mv_ch_id!=undefined && flash_mv_now!=undefined) {
    document.getElementById('func_frame').src = "/banner_click_cnt.php?type=flashmv&ch_id="+flash_mv_ch_id+"&id=" + flash_mv_ids[flash_mv_now] + "&cate_id=" + cate_id;
  }
  return true;
}

function _debug(str) {
//  if (document.getElementById('debug') != null)
    document.getElementById('debug').innerHTML += str + ' &nbsp; ';
}
