/* form.js */


var keys = new Array(
  'xml',
  'file',
  'image',
  'title',
  'color',
  'bgcolor',
  'corner'
);

var init = new Array(
  'cookie',
  'toolzoom',
  'loop',
  'repeat',
  'autoplay',
  'follow',
  'bright',
  'smooth',
  'aspect',
  'getmeta',
  'wheel',
  'scroll',
  'volume'
);



function setColor(id,color){
  if($(id)) $(id).value = color;
}

function viewColorTable(id){
  var obj = $('form-'+id);
  obj.style.display = (obj.style.display != 'none')? 'none': 'block';
  $('table-'+id).style.display = (obj.style.display == 'none')? 'block': 'none';
  return false;
}

function getPlayerSource(){
  var params = new Array();
  if($('loop-1').checked) $('follow-1').checked = true;
  for(var i=0;i<keys.length;i++){
    var obj = $(keys[i]);
    if(!obj) continue;
    if(keys[i].indexOf('color') != -1) obj.value = checkColor(obj.value);
    if(obj.value.length > 0){
      params[params.length] = (keys[i] == 'title')? keys[i] + '=' + encodeURIComponent(obj.value): keys[i] + '=' + obj.value;
    }
  }
  if($('init-list').style.display != 'none'){
    for(var i=0;i<init.length;i++){//alert(i);
      if(init[i] == 'aspect'){
        if($(init[i]+'-1').checked) params[params.length] = init[i] + '=auto';
        else if($(init[i]+'-0').checked) params[params.length] = init[i] + '=just';
      }else if($(init[i]+'-0')){
        if($(init[i]+'-1').checked) params[params.length] = init[i] + '=true';
        else if($(init[i]+'-0').checked) params[params.length] = init[i] + '=false';
      }else if($(init[i]).value.length > 0){
        if($(init[i]).value >= 0 && $(init[i]).value <= 100) params[params.length] = init[i] + '=' + $(init[i]).value;
        else $(init[i]).value = '';
      }
    }
  }
  var width = (Number($('width').value) > 290)? Number($('width').value) + 10: 300;
  var height = (Number($('height').value) > 0)? Number($('height').value) + 50: 45;
  var html = '';
  html += '<object data="' + $('flaver').value + '" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '">\n';
  html += '<param name="movie" value="' + $('flaver').value + '" />\n';
  html += '<param name="FlashVars" value="' + params.join('&amp;') + '" />\n';
  html += '<param name="allowFullScreen" value="true" />\n';
  html += '<param name="allowScriptAccess" value="always" />\n';
  html += '</object>\n';
  $('source').value = html;
  var html = '';
  html += '<object data="/webtool/flaver3/flaver.swf" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '">';
  html += '<param name="movie" value="/webtool/flaver3/flaver.swf" />';
  html += '<param name="FlashVars" value="' + params.join('&amp;') + '&amp;xml=&amp;file=/webtool/flaver3/dummy.flv" />';
  html += '<param name="allowFullScreen" value="true" />';
  html += '<param name="allowScriptAccess" value="always" />';
  html += '</object>';
  $('demo').innerHTML = html;
  return false;
}

function resetPlayer(){
  $('demo').innerHTML = '';
}

function checkColor(color) {
  if(color.substring(0,1) == '#') color = color.split('#')[1];
  else if(color.substring(0,2) == '0x') color = color.split('0x')[1];
  if(color.length != 6) return '';
  var str = '0123456789abcdefABCDEF';
  var rgb = '';
  for(var i=0;i<=5;i++) {
    if(str.indexOf(color.substr(i,1)) != -1) rgb += color.substr(i,1);
  }
  if(rgb.length == 6) return '0x' + rgb.toUpperCase();
  else return '';
}
function viewInitList(){
  var menu = $('init-list');
  menu.style.display = (menu.style.display != 'none')? 'none': 'block';
  return false;
}

function resetInit(id){
  if($(id+'-0')){
    $(id+'-0').checked = $(id+'-1').checked = false;
  }else if($(id).value.length > 0) $(id).value = '';
  return false;
}

function setSourceSelect(){
  try{
    var textarea = $$('','textarea');
    for(var i=0;i<textarea.length;i++){
      if(textarea[i].getAttribute('class') == 'SelectAll' || textarea[i].getAttribute('className') == 'SelectAll'){
        textarea[i].onclick = function(){ this.select(); }
        textarea[i].onfocus = function(){ this.select(); }
        textarea[i].onmouseup = function(){ this.select(); }
        textarea[i].readOnly = true;
      }
    }
  }catch(e){}
}

function setSourceHeight(){
  var range = 70;
  var textarea = $$('','textarea');
  if(!textarea.length) return false;
  for(var i=0;i<textarea.length;i++){
    var line = textarea[i].value.replace(/\t/g,'').replace(/\r\n/g,'\n').replace(/\r/g,'\n').split('\n');
    var len = line.length + 1;
    for(var j=0;j<line.length;j++){
      var bytes = line[j].length;
      for(var k=0;k<line[j].length;k++){
        var str = line[j].charCodeAt(k);
        if(str >= 256 && (str < 0xff61 || str > 0xff9f)) bytes += 1;
      }
      if(bytes >= range) len += Math.floor(bytes / range);
    }
    textarea[i].style.height = (len <= 6)? '100px': String(len * 16) + 'px';
  }
  setTimeout('setSourceHeight()',100);
}



setOnload(setSourceSelect);

setOnload(setSourceHeight);
