//
// Dependency updater
//
function select_depend(id, criteria, skip){
	var r = d.getElementById(id);
	var br = d.getElementById('backup_'+id );
	skip = skip || 0;

	var oldval = r.value;
	var oldvalpresent = false;
	while( r.firstChild ){ r.removeChild(r.firstChild, true);}

	var o = br.options;
	for( var i = 0; i < o.length; i++ ){
		if( o[i].value == skip || criteria(o[i]) ){
			if( o[i].value == oldval ){
				oldvalpresent = true;
			}
			r.appendChild(o[i].cloneNode(true));
		}
	}
	r.value = oldvalpresent? oldval :r.options[0].value || 0;
}


//
// Backups select for future usage
//
function backup_select(id){
	var ns = d.createElement('select');
	ns.id = 'backup_'+id;
	ns.style.visibility = 'hidden';
	ns.style.display = 'none';

	var r = d.getElementById(id);
	var o = r.options;
	for( var i = 0; i < o.length; i++){
		ns.appendChild(o[i].cloneNode(true));
	}

	d.body.appendChild(ns);
}

//
// Marks tab as hover
//
function hovertab(tabid, tabsetid){
	d.getElementById(_get_tab(tabid,tabsetid).tabgid).className = 'tab_hover';
}

//
// Marks tab as unhover
//
function unhovertab(tabid, tabsetid){
	var ts = _get_tabset(tabsetid);
	var t = _get_tab(tabid, tabsetid);
	var tobj = d.getElementById(t.tabgid);

	if( tabid == ts.activetab ){
		tobj.className = 'tab_selected';
	}
	else{
		tobj.className = 'tab';
	}
}

//
// Switch tabs, changetab_local can be defined
//
function changetab(tabid, tabsetid){
    var ts = _get_tabset(tabsetid);
    var t = _get_tab(tabid, tabsetid);
    var atid = _get_activetab(tabsetid);

    _set_urlvar(tabsetid, tabid);

    if( atid != undefined && atid != tabid ){
    	//
    	// unselect activetab if defined
        //
    	var at = _get_tab(atid, tabsetid);
    	var atobj = d.getElementById(at.tabgid);
    	atobj.className = 'tab';
    	_hideitem(at.contgid);
    }

    ts.activetab = tabid;
    _showitem(t.contgid);
    d.getElementById(t.tabgid).className = 'tab_selected';

	if( typeof(changetab_local) != 'undefined' ){
		changetab_local(tabid, tabsetid);
	}
}

//
// Init tabset with default value; URL specified key overrides default
//
function init_tabset(tabid, tabsetid){
	if( tabsetid == undefined || tabsetid == '' ){
		tabsetid = 'default';
	}

	if( _vars.urlvars[tabsetid] != undefined ){
		tabid = _vars.urlvars[tabsetid][0];
	}
//	window.alert('tsinint: '+tabsetid + tabid);
	changetab(tabid, tabsetid );
}

//
// Tabsets data structure
//
// tabset
// tabset.sets = {} - collection of tabsets
// tabset.sets.default = {} - default tabset, without name
// tabset.sets.default.activetab = id - id of activetab for tabset
// tabset.sets.default.tabs = {} - collection of tabs
// tabset.sets.default.tabs[tabid].tabgid = id - global document id of tab, for non default format is tabset_tab
// tabset.sets.default.tabs[tabid].contgid = id - global document id of tab content, for non default format is content_tabset_tab
//
_vars.tabsets = {};

//
// Returns reference to tabset, fill with defaults
//
function _get_tabset(tabsetid){
	if(tabsetid == undefined || tabsetid == ''){
		tabsetid = 'default';
	}
	var ts = _vars.tabsets;
	
	if( ts.sets == undefined ){
		//
		// first call
        //
		ts.sets = {};
	}
	var tss = ts.sets;

	if( tss[tabsetid] == undefined){
		//             
		// first call of this tabset
        //
		tss[tabsetid] = {};
		tss[tabsetid].activetab = undefined;
		tss[tabsetid]["tabs"] = {};
	}

	return(tss[tabsetid]);
}

//
// Getting active tab
//
function _get_activetab(tabsetid){
	return(_get_tabset(tabsetid || 'default').activetab);
}

//
// Return reference to tab, fill with defaults
//
function _get_tab(tabid, tabsetid){
	if(tabsetid == undefined || tabsetid == ''){
		tabsetid = 'default';
	}

	var ts = _get_tabset(tabsetid);

	if( ts.tabs["tabid"] == undefined){
		//
		// first call of this tab
        //
		ts.tabs[tabid] = {
			tabgid: (tabsetid == 'default' ? '' : tabsetid+'_')+tabid,
			contgid: 'content_'+(tabsetid == 'default' ? '' : tabsetid+'_')+tabid};
	}
	return(ts.tabs[tabid]);
}


//
// Init page function
//
var hints = 0;
$('document').ready(function(){
	var url = document.location.href;
	// assign hits

	$('[wrhint]').tooltip({
		showURL: false,
		bodyHandler: function(){
			return($('<div class="hintcontent">'+$(this).attr('wrhint')+'</div>'));
	}});


	$.Lightbox.construct({
		"files": {
			"images": {
				"next": "../../images/galery/next.png",
				"prev": "../../images/galery/prev.png"
			},
			"compressed":{
				"styles": {
					"lightbox": 'jquery/jquery-lightbox/styles/jquery.lightbox.css'
				}
			}
		},
		"text": {
			// For translating
			"image":		"Изображение",
			"of":			"",
			"close":		"Закрыть X",
			"closeInfo":	"Кликните вне изображения, чтобы закрыть.",
			"download":		"Скачать.",
			"help": {
				"close":	"Нажмите чтобы закрыть",
				"interact":	"Наведите для управления"
			},
			"about": {
				"text": 	"Просмотр изображений на WowRaider.Ru",
				"title":	"С помощью jQuery Lightbox Plugin (balupton edition), Licenced under the GNU Affero General Public License.",
				"link":		"http://www.balupton.com/projects/jquery-lightbox"
			}
		}
	});

   $('a:has(img.userimage)').each(function(index){
   		$(this).attr('title', $(this).children('img.userimage').attr('title'));
   	});
   $('a:has(img.userimage)').lightbox();
   $('a:has(div.zonemap)').lightbox();

   // hide triggers
   $('.trigger').each(function(index){
   		$('#content_'+$(this).attr('id')).hide();
   		$(this).click(function(index){
   			$('#content_'+$(this).attr('id')).toggle();
   		});
   });

});


//
// Print addlinks buttons
//
function addlinks(tid, topic, dsc, buzz, prefix, domain){
if( !prefix ){
	prefix = 'message';
}
if( !domain ){
	domain = 'wowraider.ru';
}

d.write('<a class=bml target=_blank rel=nofollow href="http'+'://www.mister-wong'+'.ru/index.php?action=addurl&bm_url=http%3A%2F%2F'+domain+'%2F'+prefix+'%2F'+tid+'%2F'+topic+'&bm_description='+dsc+'&bm_tags='+tags_space+'"><img border=0 src="http://wowraider.ru/images/relicons/mrwong.png" width=16 height=16 alt="Добавить закладку на Мр.Вонг" title="Добавить закладку на Мр.Вонг"></a>');
d.write('<a class=bml target=_blank rel=nofollow href="http'+'://moemesto'+'.ru/post.php?url=http%3A%2F%2F'+domain+'%2F'+prefix+'%2F'+tid+'%2F'+topic+'&title='+dsc+'&tags='+tags_under+'"><img border=0 src="http://wowraider.ru/images/relicons/moemesto.png" width=16 height=16 alt="Добавить закладку на Моё Место" title="Добавить закладку на Моё Место"></a>');
d.write('<a class=bml target=_blank rel=nofollow href="http'+'://memori'+'.ru/link/?sm=1&u_data[url]=http%3A%2F%2F'+domain+'%2F'+prefix+'%2F'+tid+'%2F'+topic+'&u_data[name]='+dsc+'&u_data[tag]='+tags_comma+'"><img border=0 src="http://wowraider.ru/images/relicons/memori.png" width=16 height=16 alt="Добавить закладку на Мемори" title="Добавить закладку на Мемори"></a>');
d.write('<a class=bml target=_blank rel=nofollow href="http'+'://www.100zakladok'+'.ru/save/?bmurl=http%3A%2F%2F'+domain+'%2F'+prefix+'%2F'+tid+'%2F'+topic+'&bmtitle='+dsc+'"><img border=0 src="http://wowraider.ru/images/relicons/100zakladok.png" width=16 height=16 alt="Добавить закладку на 100 Закладок" title="Добавить закладку на 100 Закладок"></a>');
d.write('<a class=bml target=_blank rel=nofollow href="http'+'://myscoop'+'.ru/add/?URL=http%3A%2F%2F'+domain+'%2F'+prefix+'%2F'+tid+'%2F'+topic+'&title='+dsc+'"><img border=0 src="http://wowraider.ru/images/relicons/myscoop.png" width=16 height=16 alt="Добавить закладку на Myscoop" title="Добавить закладку на Myscoop"></a>');
d.write('<a class=bml target=_blank rel=nofollow href="http'+'://www.bobrdobr'+'.ru/addext.html?url=http%3A%2F%2F'+domain+'%2F'+prefix+'%2F'+tid+'%2F'+topic+'&title='+dsc+'"><img border=0 src="http://wowraider.ru/images/relicons/bobrdobr.png" width=16 height=16 alt="Добавить закладку на Бобр Добр" title="Добавить закладку на Бобр Добр"></a>');
//d.write('<a class=bml target=_blank rel=nofollow href="http'+'://delicious'+'.com/save?v=5&amp;noui&amp;jump=close&amp;url=http%3A%2F%2F'+domain+'%2F'+prefix+'%2F'+tid+'%2F'+topic+'&title='+dsc+'"><img src="http://static.delicious.com/img/delicious.small.gif" height="16" width="16" border=0 alt="Добавить на Delicious" title="Добавить на Delicious" /></a>');
//d.write('<a title="Post to Google Buzz" class="google-buzz-button" href="http://www.google.com/buzz/post" data-button-style="small-count" data-locale="ru"></a></div>');
}


//
// Print author data
//
function addauthor(){

}

//
// toggle message container
//
function togglemessage( id ){
	var link = d.getElementById('l'+id);
	if( link.innerText == 'Скрыть комментарий' || link.innerText == '' ){
		// hiding
		_hideitem('a'+id);
		_hideitem('b'+id);
		_hideitem('c'+id);
		link.innerText = 'Показать комментарий';
	}
	else{
		// showing
		_showitem('a'+id);
		_showitem('b'+id);
		_showitem('c'+id);
		link.innerText = 'Скрыть комментарий';
	}
}



