// $Id: search1.js 3176 2007-01-23 23:08:29Z pro $
// Copyright (C) 2003-2007 Oleg Alexeenkov http://pro.setun.net/search/ proler@gmail.com icq#89088275

var userAgent	= navigator.userAgent.toLowerCase();
var is_saf	= ((userAgent.indexOf('applewebkit') > -1) || (navigator.vendor == 'Apple Computer, Inc.'));
var is_ie	= ((navigator.appName.indexOf('Microsoft') > -1) || (navigator.appName.indexOf('MSIE') > -1)) && userAgent.indexOf('opera') < 0;
var allow_show_adv_auto = 1;
var i;
var adjust_sel;
var dls_cleared = 0;
var select_hidden_objects = [];

//alert(is_ie + ':' + userAgent);

function my_getbyid(id) {
	var obj = null;
	if (document.getElementById)	{ obj = document.getElementById(id); }
	else if (document.all)		{ obj = document.all[id]; }
	else if (document.layers)	{ obj = document.layers[id]; }
	return obj;
}
// Show/hide toggle
function toggleview(id) {
	if ( ! id ) return;
	if ( itm = my_getbyid(id) ) {
		if (itm.style.display == 'none')	{ my_show_div(itm); return 1; }
		else					{ my_hide_div(itm); return 0; }
	}
}

function show_id(id) {
	if (!id) return;
	if (obj=my_getbyid(id)) 
		if (obj.style.display == 'none')
			my_show_div(obj); 
}

function hide_id(id) {
	if (!id) return;
	if (obj = my_getbyid(id)) 
		if (obj.style.display != 'none')
			my_hide_div(obj); 
}

function my_hide_div(obj) {
	if (!obj) return;
	obj.style.display = 'none';
	show_under(obj);
}
function my_show_div(obj) {
	if (!obj) return;
	obj.style.display = '';
	hide_under(obj);
}

function repl(where, what, wit) {
	if (! where || ! what) return;
        where = my_getbyid(where);
	if (! where) return;
	where[what] = wit;
//	where.focus();
        where.className = '';
}

// thanks to yandex.ru for following script
var searchInputIsActive = false;
var CtrlUp = false;


function rimg(a) {
  var img = new Image();
  img.src = a;
}

function rhit(a) {
//  var img = new Image();
//  img.src = search_url + '?view=http-redirect&save=' + a;
  rimg(search_url + '?view=http-redirect&save=' + a);
}

function rvote(a, obj) {
  rimg(a);
  if(String(obj.innerHTML).search(/\+1$/) < 1)obj.innerHTML+='+1';
}

function r(a) {  // old: var img = new Image();	img.src = a;
  if (dls_cleared || dls_string == 0) {
    dls_string = ''; 
    dls_cleared = 0;
  }
  createCookie('dls', a + dls_string , cookie_days);
  rhit(a);
}

function init() {
	if (document.getElementById) {
		document.onkeydown = register;
		if (document.forms['searchform']['q']) {
			document.forms['searchform']['q'].onfocus = function () {
				searchInputIsActive = true; 
				if (document.forms['searchform']['q'].select && CtrlUp) {document.forms['searchform']['q'].select();}
			};
			document.forms['searchform']['q'].onblur = function () {searchInputIsActive = false; CtrlUp = false;};
		}
	}

/* todo: menu area checking
		if (window.attachEvent && !is_saf)
		{
			document.attachEvent('onclick', menu_hide);
			window.attachEvent('onresize', menu_hide);
		}
		else if (document.addEventListener && !is_saf)
		{
			document.addEventListener('click', menu_hide, false);
			window.addEventListener('resize', menu_hide, false);
		}
		else
		{
			window.onclick = menu_hide;
			window.onresize = menu_hide;
		}
*/
}

function register(e) {
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if ((code == 13) && (e.ctrlKey == true)) document.forms['searchform'].submit();
	if (!searchInputIsActive) {
		if ((code == 37) && (e.ctrlKey == true)) {
			var destination = my_getbyid('prev_page');
			if (destination)	location.href = destination.href;
		}
		if ((code == 39) && (e.ctrlKey == true)) {
			var destination = my_getbyid('next_page');
			if (destination) location.href = destination.href;
		}
	}
	if ((code == 38) && (e.ctrlKey == true) && document.forms['searchform']['q']) {
		CtrlUp = true;
		document.forms['searchform']['q'].focus();
	}
}

/* http://www.alistapart.com/articles/alternate/ */
/* thanks to $FreeBSD: www/en/layout/js/styleswitcher.js,v 1.3 */
function setActiveStyleSheet_one (title, a) {
  if (a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('title')) {
    a.disabled = true;
    if (a.getAttribute ('title').indexOf(title) != -1) a.disabled = false;
  }
}

function setActiveStyleSheet (title) {
  var i, a, main;
  for (i = 0; (a = document.getElementsByTagName ('link')[i]); i++) 
     setActiveStyleSheet_one(title, a);
  for (i = 0; (a = document.getElementsByTagName ('style')[i]); i++)
     setActiveStyleSheet_one(title, a);
}

function createCookie(name, value, days) {
  if (!days) days = cookie_days;
//alert('cook|' + 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 cookie_checkbox(obj) {
  createCookie(obj.name, (obj.checked ? 1 : 0), cookie_days);
}

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;
}

function eraseCookie(name) { createCookie(name,'',-1); }

function absolute_coords(obj) {
	var parent = obj;
	var x = 0;
	var y = 0;
	while (parent != null) {
		x += parent.offsetLeft;
		y += parent.offsetTop;
		parent = parent.offsetParent;
	}
	return [x,y];
}

function set_position(parent, obj, dx, dy) {
	coords = absolute_coords(parent);
	px = coords[0] + dx; py = coords[1] + dy;
//	obj.style.display = '';
	py = py + (dy >= 0 ? parent.offsetHeight : 0);
	if (document.all) max_width = document.body.clientWidth - 10;
	else              max_width = window.innerWidth - 10;
	if (px + obj.offsetWidth > max_width) {
		px = max_width - obj.offsetWidth;
		if (px < 0) {
			obj.style.width = (max_width - 40) + 'px';
			px = 0;
		}
	}
	obj.style.left = px + 'px';
	obj.style.top  = py + 'px';
}

function toggle_position(parent, id, dx, dy) {
	if (!id) return;
		if (id.style.display == 'none')	{ set_position(parent, id, dx, dy); my_show_div(id); }
		else				{ my_hide_div(id); }
//	}
}


function show_adv() {
  show_id('tr_adv');
  hide_id('form-link-specify');
}

var current_menu, no_hide;

function no_hide_reset() { 
  no_hide = 0;
}


function menu_show() { 
}

function menu_hide() { 
//alert('hide');
	if (no_hide) return;
	my_hide_div(current_menu);
        current_menu = null;
	no_hide = 1;
        setTimeout('no_hide_reset()', 1);
}


function menu_toggle(parent, id, dx, dy) {
//alert('toggle');
	if ( !id ) return;
        if (current_menu == id) {menu_hide(); return;}
	menu_hide();
//	my_hide_div(current_menu);
//	if ( itm = my_getbyid(id) ) {
		if (id.style.display == 'none')	{ set_position(parent, id, dx, dy); my_show_div(id);  }
		else				{ my_hide_div(id); }
	current_menu = id;
}


// http://www.artlebedev.ru/svalka/InputPlaceholder.js
function InputPlaceholder(input, value, cssFilled, cssEmpty) {
	var thisCopy = this
	this.Input = input
	this.Value = value
	this.SaveOriginal = (input.value == value)
	this.CssFilled = cssFilled
	this.CssEmpty = cssEmpty
	this.setupEvent (this.Input, 'focus', function() {return thisCopy.onFocus()})
	this.setupEvent (this.Input, 'blur',  function() {return thisCopy.onBlur()})
	this.setupEvent (this.Input, 'keydown', function() {return thisCopy.onKeyDown()})
//if (this.Value)alert(this.Value+' = '+input.value );
	if (input.value == '' || input.value == this.Value) this.onBlur();
	return this
}
InputPlaceholder.prototype.setupEvent = function (elem, eventType, handler) {
	if (elem.attachEvent)	elem.attachEvent ('on' + eventType, handler);
	if (elem.addEventListener)	elem.addEventListener (eventType, handler, false);
}
InputPlaceholder.prototype.onFocus = function() {
	if (!this.SaveOriginal &&  this.Input.value == this.Value)	this.Input.value = '';
	else	this.Input.className = '';
}
InputPlaceholder.prototype.onKeyDown = function() { this.Input.className = ''; }
InputPlaceholder.prototype.onBlur = function() {
	if (this.Input.value == '' || this.Input.value == this.Value) {
		this.Input.value = this.Value;
		this.Input.className = this.CssEmpty;
	}
	else	this.Input.className = this.CssFilled
}

/*
var saveclass = [];
function selected(id, class) {
	if ( ! id ) return;
	if ( itm = my_getbyid(id) ) {
		saveclass[id] = itm.className;
		itm.className = class;
	}
}

//selected('form-link-hide-advanced', 'selected');
*/

function hide_adv() {  hide_id('tr_adv');hide_id('form-link-hide-specify');show_id('form-link-specify'); }
function show_adv(auto) {  
// alert (auto + "+" + allow_show_adv_auto);
	if (!allow_show_adv_auto && auto) return;
	show_id('tr_adv');show_id('form-link-hide-specify');hide_id('form-link-specify'); 
}

var hilight_obj = [];
var hilight_class = [];
function hilight(num, obj, classname) {
//alert(num+', '+obj+', '+classname);
	if (!num) num = 0;
	if (hilight_obj[num]) hilight_obj[num].className = hilight_class[num];
	if (!obj) return;
	hilight_obj[num] = obj;
	hilight_class[num] = hilight_obj[num].className;
	hilight_obj[num].className = classname;
}




function hide_under(obj) {
	if (!is_ie) return;
	var coords = absolute_coords(obj);
	var x = coords[0]; var y = coords[1];
	var selects = document.getElementsByTagName('SELECT');
	if (!select_hidden_objects[obj.id]) select_hidden_objects[obj.id] = [];
	for (i = 0; i < selects.length; i++) {
		var select = selects[i];
		if (select.parentNode == obj) continue;
		coords = absolute_coords(select);
		var sx = coords[0]; var sy = coords[1];
		if ((((x < sx) && (x + obj.offsetWidth  > sx)) || ((sx < x) && (sx + select.offsetWidth  > x))) &&
		    (((y < sy) && (y + obj.offsetHeight > sy)) || ((sy < y) && (sy + select.offsetHeight > y))) &&
                    select.style.visibility != 'hidden') {
			select_hidden_objects[obj.id][select_hidden_objects[obj.id].length] = select;
			select.style.visibility = 'hidden';
		}
	}
}

function show_under(obj) {
	if (!is_ie || !select_hidden_objects[obj.id]) return;
	for (i = 0; i < select_hidden_objects[obj.id].length; i++)
		select_hidden_objects[obj.id][i].style.visibility = 'visible';
	select_hidden_objects[obj.id] = [];
}

//#print '<td><a ',nameid('res_play_link'.$work{'n'}),' href="#" onclick="toggleview(\'res_play',$work{'n'},'\');this.innerHTML=\'zz\';">SHOW</a></td>' if $row->{'can_show'};

function show_image(n) {}
function hide_image(n) {}

var created_plays = [], last_play, destroys = [], autocloses = [], showtimeout = [];

function toggle_play(n, full, type, destroy, autoclose) {
  var obj = my_getbyid('res_play'+n);
//alert(obj +' '+ full + ' ' +type);
  if (!obj || !full ) return; //|| !type
var height = (this["play_height_"+type] || this["play_height"]); 
var width = (this["play_width_"+type] || this["play_width"]); 
//var destroy = parseInt(this["destroy_on_hide_"+type] || this["destroy_on_hide"]); 
destroys[n] = destroy;
autocloses[n] = autoclose;

//alert(height +' '+ width + ' ' +destroy+':'+this["destroy_on_hide_"+type] + ':' + this["destroy_on_hide"]);
  if (toggleview('res_play' + n) ) {
    if (autoclose && last_play && autocloses[last_play]) {
      toggleview('res_play' + last_play);
      play_hide(last_play); //, destroy
//      if (destroy) my_getbyid('res_play'+last_play).innerHTML = '', created_plays[last_play] = '';
    }
    if (autoclose) last_play = n;
    if (showtimeout[n]) clearTimeout(showtimeout[n]);
    
    if (!created_plays[n]) {
      created_plays[n] = full;

      var player = "";
      switch (type) {
       case 'image':
	player += '<img '
	if (width)	player += 'width="'+width+'" '
	if (height)	player += 'height="'+height+'" '
	player += 'src="'+full+'">';
       break;         
       case 'audio':
       case 'video':
       case 'playlist':
	player += "<OBJECT ID='Player' height='"+height+"' width='"+width+"' CLASSID='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6'>"
	player += "<PARAM name='URL' value='" + full + "' id='insert_1'>"
	player += "<PARAM name='uiMode' value='full'>"
	player += "<PARAM name='mute' value='false'>"
	player += "<PARAM name='ShowControls' value='1'>"
	player += "<PARAM name='ShowStatusBar' value='1'>"
//	player += "<PARAM name='ShowDisplay' value='1'>"
        player += "<param name='controller' value='1'>"
	player += "<PARAM NAME='AutoSize' VALUE='1'>"
	player += "<EMBED type='application/x-mplayer2' "
	player += "pluginspage = 'http://www.microsoft.com/Windows/MediaPlayer/' "
	player += "SRC='" + full + "' "
	player += "name='Player' "
	if (width)	player += "width='"+width+"' "
	if (height)	player += "height='"+height+"' "
	player += "AutoStart='true' "
	player += "showcontrols='1' "
	player += "showstatusbar='1' "
//	player += "showdisplay='1' "
	player += "AutoSize='1' "
	player += "controller='1' "
	player += "id='insert_2'>"
	player += "</EMBED>"
/*<noembed>ˆ§¢¨­¨â¥, ¤«ï ¨£àë ¢ ¬ ­ã¦¥­ <a href="http://www.macromedia.com/go/getflashplayer/" target="_blank">Flash Player</a>Curveball</noembed>*/
	player += "</OBJECT>"
//	document.getElementById('radio').innerHTML = player;
//<embed width="320" height="260" src=".....wmv"></embed>
       break;         

       
       case 'flash':
//	var player = "";
///*
       player += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
       player += 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" '
       if (width)	player += 'width="'+width+'" '
       if (height)	player += 'height="'+height+'" '
       player += '>'
//       player += '<param name="menu" value="false" />'
	player += '<param name="movie" value="'+ full +'" />'
	player += '<param name="type" value="application/x-shockwave-flash" />'
	player += '<param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer/" />'
	player += '<param name="bgcolor" value="#000" />'
	player += '<param name="quality" value="high" />'
	if (width)	player += '<param name="width" value="'+width+'"/>'
	if (height)	player += '<param name="height" value="'+height+'"/>'
	player += '<embed src="'+ full +'" '
	if (width)	player += 'width="600" '
	if (height)	player += 'height="600" ' 
	player += 'bgcolor="#000" quality="high" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer/" menu="false"></embed>'
	player += '<noembed>Èçâèíèòå, âàì íóæåí <a href="http://www.macromedia.com/go/getflashplayer/" target="_blank">Flash Player</a></noembed>'
	player += '</object>'
//*/
	default:
	break;
      } 
      if (player) rhit(full), obj.innerHTML = player
//       + '<br/>dbg: full=' +full +' <br/> type=' + type + ' destroy='+ destroy + ' autoclose=' +autoclose + ' width='+ width+ ' height=' +height+ (width ? ' wt' : ' wf') + (height? ' ht' : ' hf') 
    }

    my_getbyid('res_play_link'+n).innerHTML = lang_hide;

  } else {
    play_hide(n, destroy)
    if(autoclose) last_play = '';
  }
}


function play_hide(n) { //, destroy
    if (!n) return;
    var obj = my_getbyid('res_play'+n);
    my_getbyid('res_play_link'+n).innerHTML = lang_show;
    if (destroys[n]) {
//alert('destrooo='+destroy);
      obj.innerHTML = '';
      created_plays[n] = '';
    }
}



function dmp(obj){var r='';if (obj!='null') { for(i in obj){r+=i+" - "+obj[i]+"<br>\n"; } } return r;} 
//document.writeln(dmp(document.body.all["!!"]));
