function searchClear(target, searchText)
{
  if (target.value == searchText)
  {
    target.value = '';
  }
}

function searchRestore(target, searchText)
{
  if (target.value == '')
  {
    target.value = searchText;
  }
}

function bar(url, title) {
	wasOpen = false;
	win     = window.open(url, title);    
	return (typeof(win) == 'object') ? true : false;
}

function myPopImage(imageURL,imageTitle,imageWidth,imageHeight) {

  PositionX = 30;
  PositionY = 30;
  
  ratio = imageWidth/imageHeight;
  maxHeight = screen.height - 105;
  maxWidth = maxHeight*ratio;
  if (maxWidth > screen.width-30) {
    maxWidth = screen.width - 80;
    maxHeight = maxWidth/ratio;
  }
    
  var opt='width=320,height=240,left='+PositionX+',top='+PositionY+',status=0,toolbar=0,menubar=0,location=0';
  imgWin=window.open('about:blank','null',opt);
  with (imgWin.document) {
    writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');
    writeln('<sc'+'ript>');
    writeln('function reSizeToImage(){');
    writeln('if (document.images[0].height > '+maxHeight+' || document.images[0].width > '+maxWidth+'){');
    writeln('height = '+maxHeight+';');
    writeln('width = '+maxWidth+';');
    writeln('document.images[0].width = '+maxWidth+';');
    writeln('document.images[0].height = '+maxHeight+';');
    writeln('window.resizeTo(width+40,height+100);}');
    writeln('else {');
    writeln('height = document.images[0].height;');
    writeln('width = document.images[0].width;');
    writeln('window.resizeTo(width+40,height+100);}}');
    writeln('function doTitle(){document.title="'+imageTitle+'";}');
    writeln('</sc'+'ript>');
    writeln('</head><body bgcolor="FFFFFF" onload="reSizeToImage();doTitle();self.focus()">');
    writeln('<img alt="'+imageTitle+'" title="'+imageTitle+'" src="'+imageURL+'" style="display:block; margin:0 auto;" /></body></html>');
    close();
  }
}

function myPopWindow(windowURL, windowName, windowFeatures) {
  var defaultFeatures = {"status": 0, "toolbar": 0, "location": 0, "menubar": 0,
                         "directories": 0, "resizeable": 1, "scrollbars": 1,
                         "width": 640, "height": 480};
  var features = '';

  for (i in windowFeatures) {defaultFeatures[i] = windowFeatures[i];}
  for (i in defaultFeatures) {features += i + '=' + defaultFeatures[i] + ',';}

  window.open(windowURL, windowName, features);
  return false;
}

function switchBmk(n, css) {
  if (document.getElementById('bmk_header')) {
    var num = document.getElementById('bmk_header').getElementsByTagName('div').length;
    for (var i=1;i<=num;i++) {
      var elt = document.getElementById('bmk_title_' + i);
      var elc = document.getElementById('bmk_content_' + i);
      if (i == n) {
        elt.className = css + '_title active';
        elc.className = css + '_content visible';
      }
      else {
        elt.className = css + '_title';
        elc.className = css + '_content invisible';
      }
    }
  }
}

var inProgress = false;

function switchPanel(a, n, stopLoop) {
  inProgress = true;
  $('stick-item-'+a).hide();
  $('stick-item-'+n).appear({queue: {scope: 'appear', position: 'end'}, duration: 0.5, afterFinish: function(){inProgress = false}});
  if (stopLoop == true && timerId) {
    window.clearTimeout(timerId);
  } else {
    if (n == $$('#top-box .stick-art .item').length) {
      timerId = switchPanel.delay(8, n, 1);
    } else {
      timerId = switchPanel.delay(8, n, n+1);
    }
  }
}

function switchTab(art, link) {
  $$('#bs-content .bs-content').each(function(s){
    if (s.id == art) {
      s.show();
    } else {
      s.hide();
    }
  });
  $$('#bs-header div').each(function(s){
    if (s.id == link) {
      s.className = 'active';
    } else {
      s.className = 'disabled';
    }
  });
}

QCM.YTVideo = function() {
  this.params = {
    w: 656,
    h: 369
  };
  this.players = [];
}
QCM.YTVideo.prototype = {
  init: function(params) {
    this.params = Object.extend(this.params, params);
    document.write('<span id="player_'+this.params.videoId+'"></span>');
    var player_box = $('player_'+this.params.videoId).up('.yt_player');
    player_box.insert(new Element('input', {type: 'button', id: 'enlarge_myytplayer_'+this.params.videoId, value: '+'}))
    var atts = {id: 'myytplayer_'+this.params.videoId};
    swfobject.embedSWF('http://www.youtube.com/e/'+this.params.videoId+'?enablejsapi=1&playerapiid=myytplayer_'+this.params.videoId,
                       'player_'+this.params.videoId, this.params.w, this.params.h, '8', null, null, {allowScriptAccess: 'always', allowFullScreen: 'true'}, atts);
    if (this.params.shortCuts) {
      var h = '<ul class="player_shortcuts">';
      for (var i in this.params.shortCuts) {
        h += '<li><a href="javascript:QCM.YTVideo.goToPosition(\'myytplayer_'+this.params.videoId+'\', '+this.params.shortCuts[i].time+')">'+this.params.shortCuts[i].label+'</a></li>';
      }
      h += '</ul>';
      player_box.insert(h);
    }
    player_box.insert(new Element('div', {'class': 'cleaner'}));
  },
  
  playerReady: function(playerId) {
    var t = QCM.YTVideo;
    t.players[playerId] = $(playerId);
    t.players[playerId].playVideo();
    t.players[playerId].pauseVideo();
    var btn = $('enlarge_'+playerId);
    btn.disabled = false;
    btn.onclick = function(){
      t.enlargePlayer(playerId);
    }
  },

  enlargePlayer: function(playerId) {
    if (this.players[playerId].width == this.params.w) {
      if (Prototype.Browser.WebKit) {
        $$('iframe').invoke('hide');
      }
      this.players[playerId].width = 960;
      this.players[playerId].height = 540;
      this.players[playerId].setPlaybackQuality('hd720');
      this.players[playerId].setSize(960, 540);
    } else {
      if (Prototype.Browser.WebKit) {
        $$('iframe').invoke('show');
      }
      this.players[playerId].width = this.params.w;
      this.players[playerId].height = this.params.h;
      this.players[playerId].setPlaybackQuality('default');
      this.players[playerId].setSize(this.params.w, this.params.h);
    }
  },

  goToPosition: function(playerId, s) {
    if (!!s) {
      this.players[playerId].seekTo(s, true);
      this.players[playerId].playVideo();
    }
  }
}
QCM.YTVideo = new QCM.YTVideo();

if (typeof QCM != 'undefined' && QCM.YTVideo) {
  onYouTubePlayerReady = QCM.YTVideo.playerReady;
}

