window.defaultNum=5 //kolichectvo blokov sprava po umolchaniyu
window.noSelects = false; //zagrujat dekorativnie selects
////////////////////////////////////////////////////////////////////////////////////////////////////////////
Event.observe(window, "load", function(event) {
if ($('top20dvds')){ TopList.init();}    
//Event.stop($$("body")[0],'mouseover');
//Event.stop($$("table.models div.item a.thumb img"),'mouseover');

/*$$("body")[0].descendants().each(function(item){
Event.observe(item, "mouseover", function(event) {
	//alert(Event.element(event).outerHTML);
});*/
//});

});
////////////////////////////////////////////////////////////////////////////////////////////////////////////

function init() {
Sbr.init();
	
	setTimeout(function()
	{
		
		try
		{
		myCbir = new Cbir(
		{
			def:['images/checkbox_act.gif', 'images/checkbox_def.gif']
		});}
		catch(e){
		//
		}
		//(document.all && !window.print) ? null : (noSelects)?null:Sbr.init();	
		//if (document.getElementsByTagName('select')) 
			//Sbr.init();
			
		if (navigator.userAgent.indexOf("MSIE")>-1)	
			$$("a.thumb img").each(function(item){ item.style.position="relative"; item.style.display="inline";});
	},100);
	//TopList.init();
	//FlashTabs.init();
	//bodyHeight();
}

function initAfterAjax(ajax_id) {
	if (!ajax_id) return false;
	//balloonTip.hide();
	Sbr.init(ajax_id);
	new AddDecor('ul.menu li, div.title h2, div.title div, div.title h3, div.gallerydvd div.thumb_item, div.item_info, .title dt'
							 ,'both',false, ajax_id);
	new AddDecor('table.gallery div.thumb, #balloon', 'bottom'
							 , false, ajax_id);
	new AddDecor('table.models div.item, table.models div.latest_img, div.latest div.latest_img, table.gallery div.item, .framepager_bs .set_items .item, .frameitem, .bonus_sites div.item'
							 , 'both', true, ajax_id);
	try
	{
		myRbir = new Rbir(
		{
			def:[designPath+'/images/radio_act.gif', designPath+'/images/radio_def.gif'], //DEFAULT IMAGES
			dark:[designPath+'/images/radio_act_dark.gif', designPath+'/images/radio_def_dark.gif']
		}, ajax_id);
		
		myCbir = new Cbir(
		{
			def:[designPath+'images/checkbox_act.gif', designPath+'images/checkbox_def.gif']
		}, ajax_id);
	}
	catch(e){
		//
	}
	try
	{
		resizeTabs();
	}
	catch(e){
		//
	}
	return true;
}


// Event-Selectors
var rulesDefault = {
	// Show+Hide balloon-tooltip over the models
	'div.models img:mouseover': function(element, event) {
		if (!element.tip) {
			element.tip = element.getAttribute('alt') ? element.getAttribute('alt') : '';
			//element.setAttribute('alt','');
		}
		balloonTip.show(element.tip, {'x':-15,'y':-35}, 'simpleBalloon', event);
  },
	'div.models img:mouseout': function(element) {
    balloonTip.hide();
  },
	'table.gallery div.thumb img:mouseover': function(element, event) {
		var obj = element;
		while (obj = obj.parentNode) {
			if (obj.className == 'item') break;
		}
		if (info = document.getElementsByClassName('item_info',obj)[0]) balloonTip.show(info, 25,"",event);
  },
	'table.gallery div.thumb img:mouseout': function(element) {
    balloonTip.hide();
  },
  '.up_items td .today td .item .thumb img:mouseover': function(element, event) {
		var obj = element;
		while (obj = obj.parentNode) {
			if (obj.className == 'item') break;
		}
		if (info = document.getElementsByClassName('item_info',obj)[0]) balloonTip.show(info, 25,"",event);
  },
	'.up_items td .today td .item .thumb img:mouseout': function(element) {
    balloonTip.hide();
  },
  'div.model_photos div.wrap img:mouseover': function(element, event) {
		if (!element.tip) {
			element.tip = element.getAttribute('alt') ? element.getAttribute('alt') : '';
			element.setAttribute('alt','');
		}
		balloonTip.show(element.tip, {'x':-15,'y':-35}, 'simpleBalloon');
  },
	'div.model_photos div.wrap img:mouseout': function(element) {
    balloonTip.hide();
  },
  'table.up_items img.th:mouseover': function(element, event) {
		var obj = element;
		var info = obj.up().next();
		if (info) balloonTip.show(info, 25,"",event);
  },
	'table.up_items img.th:mouseout': function(element) {
    balloonTip.hide();
  },
  'div.model_videos img:mouseover': function(element, event) {
		if (!element.tip) {
			element.tip = element.getAttribute('alt') ? element.getAttribute('alt') : '';
			element.setAttribute('alt','');
		}
		balloonTip.show(element.tip, {'x':-15,'y':-35}, 'simpleBalloon');
  },
	'div.model_videos img:mouseout': function(element) {
    balloonTip.hide();
  }
}

Ajax.Responders.register({
  onComplete: function() { EventSelectors.assign(rulesDefault);}
})

function openImage(url,image_x,image_y)
{
    X_pos = (screen.width/2)-(image_x/2);
    Y_pos = (screen.height/2)-(image_y/2);
    window.open(url,'imagewin','scrollbars=no,resizable=no,left='+X_pos+',top='+Y_pos+',width='+image_x+',height='+image_y);
}

/* DOM-ready watcher by brothercake, http://www.brothercake.com/site/resources/scripts/domready/ */
function domFunction(f, a) {
	var n = 0;
	var t = setInterval(function() {
		var c = true;
		n++;
		if (typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null)) {
			c = false;
			if (typeof a == 'object') {
				for (var i in a) {
					if ((a[i] == 'id' && document.getElementById(i) == null) || (a[i] == 'tag' && document.getElementsByTagName(i).length < 1) ) {
						c = true;
						break;
					}
				}
			}
			if (!c) { f(); clearInterval(t); }
		}
		if (n >= 60) {
			clearInterval(t);
		}
	}, 250);
};

/**
 * Add some html for decoring any element by css.
 *
 * elements (required) - elements in css style, like '#menu li'
 * layout - where add decor: 'top', 'bottom' or 'both' (by default)
 * wrap - add all childs to a wrap: true (by default) or false
 */
function AddDecor (elements, layout, wrap, id) {
	if (!elements) return false;
	var searchUnder = id ? '#'+id+' ' : '';
	switch (layout) {
		case 'top': this.layout = 'top'; break;
		case 'bottom': this.layout = 'bottom'; break;
		default: this.layout = 'both';
	}
	this.wrap = wrap == null ? true : (wrap == false ? false : true);

	var selArr = elements.split(',');
	var selNum = selArr.length;
	for (var j=0;j<selNum;j++) {
		var elArr = $$(searchUnder+selArr[j]);
		var elNum = elArr.length;
		for (var i=0;i<elNum;i++) {
			this.work(elArr[i]);
		}
	}
	return true;
}

AddDecor.prototype.work = function(el) {
	elChildsNum = el.childNodes.length;

	if (this.wrap) {
		var wrapObj = document.createElement('div');
		wrapObj.className = 'decorWrap';
		while(el.firstChild) wrapObj.appendChild(el.firstChild);
		el.appendChild(wrapObj);
	}

	if (this.layout == 'top' || this.layout == 'both') {
		var decorTop = document.createElement('b');
		decorTop.className = 'decorStr decorTop';
		var decorLeft = document.createElement('i');
		decorLeft.className = 'decorLeft';
		decorTop.appendChild(decorLeft);
		var decorCentre = document.createElement('i');
		decorCentre.className = 'decorCentre';
		decorTop.appendChild(decorCentre);
		var decorRight = document.createElement('i');
		decorRight.className = 'decorRight';
		decorTop.appendChild(decorRight);

		if (el.firstChild) {
			el.insertBefore(decorTop, el.firstChild);
		} else {
			el.appendChild(decorTop);
		}
	}

	if (this.layout == 'bottom' || this.layout == 'both') {
		var decorBottom = document.createElement('b');
		decorBottom.className = 'decorStr decorBottom';
		var decorLeft = document.createElement('i');
		decorLeft.className = 'decorLeft';
		decorBottom.appendChild(decorLeft);
		var decorCentre = document.createElement('i');
		decorCentre.className = 'decorCentre';
		decorBottom.appendChild(decorCentre);
		var decorRight = document.createElement('i');
		decorRight.className = 'decorRight';
		decorBottom.appendChild(decorRight);

		el.appendChild(decorBottom);
	}
}

/**
 * Select boxes replacer
 * by ZeT, http://zet.od.ua
 * version 0.5.2007-02-02
 *
 * based on http://www.easy-designs.net/articles/replaceSelect2/
 */
var Sbr = {
	init: function(id) {
		this.last = false;
		this.trigger = false;

		if (id && (form = document.getElementById(id))) {
			this.s = form.getElementsByTagName('select');
		} else {
			this.s = document.getElementsByTagName('select');
		}
    this.sCount = this.s.length;
		for (var i=0; i<this.sCount; i++) {
		  if (!this.s[i].multiple) {
  			if (!this.s[i].id) {
  				this.s[i].id = 'select_'+i;
  			}
  			this.run(this.s[i], i);
 			}
		}
	},

	run: function(obj, counter) {
		// building faux select structure
		var fauxSel = document.createElement('dl');
		fauxSel.className = obj.className ? obj.className + ' sbrWrap' : 'sbrWrap';
		obj.className = 'selReplaced';

		fauxSel.onclick = function() {	
			var selectElement = this.nextSibling;
			if (this.childNodes[1].className == 'sbrOptsCol') {
				selectElement.onfocus();
				Sbr.last.className = 'sbrOptsCol';
				Sbr.last = this.childNodes[1];
				Sbr.trigger = false;
				document.onclick = function() { if (Sbr.trigger) Sbr.last.className = 'sbrOptsCol'; Sbr.trigger = true; }
				this.childNodes[1].className = 'sbrOptsExp';
			} else {
				Sbr.lastObjId = false;
				document.onclick = function() {	}
				this.childNodes[1].className = 'sbrOptsCol';
			}
			if (this.childNodes[0].className == '') {
				this.nextSibling.focus();
			}
		}

		var curOpt = document.createElement('dt');
		curOpt.className = '';
		fauxSel.appendChild(curOpt);
		var optsWrap = document.createElement('dd');
		optsWrap.className = 'sbrOptsCol';
		fauxSel.appendChild(optsWrap);
		var optsList = document.createElement('ul');
		optsWrap.appendChild(optsList);
		optsList.style.zIndex = fauxSel.style.zIndex = 1000-counter;

		var opts = obj.options;
		var selectedOpt = (!obj.selectedIndex) ? 0 : obj.selectedIndex;
		for (var i=0; i<opts.length; i++) {
			var li = document.createElement('li');
			var txt = opts[i].text ? document.createTextNode(opts[i].text) : document.createTextNode('blank option');
			li.appendChild(txt);
			li.title = opts[i].title;
			li.selIndex = i;
			li.selectID = obj.id;
			li.onclick = function() {
				Sbr.selectMe(this);
			}
			if (i == selectedOpt) {
				li.className = 'sbrSelected';
				curOpt.appendChild(txt.cloneNode(false));
			}
			if (window.attachEvent) {
				li.onmouseover = function() {
					this.className += ' hover';
				}
				li.onmouseout = function() {
					this.className = this.className.replace(new RegExp(" hover\\b"), '');
				}
			}
			optsList.appendChild(li);
		}
		obj.oldonfocus = obj.onfocus;
		obj.onfocus = function() {
			if (this.oldonfocus) this.oldonfocus();
			this.previousSibling.childNodes[0].className = 'sbrFocused';
		}
		obj.oldonblur = obj.onblur;
		obj.onblur = function() {
			if (this.oldonblur) this.oldonblur();
			this.previousSibling.childNodes[0].className = '';
		}
		obj.oldonchange = obj.onchange;
		obj.onchange = function() {
			if(obj.previous == undefined) {
				obj.previous = obj.value;
			} else {
				if(obj.previous == obj.value) {
					return;
				} else {
					obj.previous = obj.value;
				}
			}
			//alert("Change");
			if (this.oldonchange) this.oldonchange();
			var idx = this.selectedIndex;
			var ul = this.previousSibling.childNodes[1].childNodes[0];
			Sbr.selectMe(ul.childNodes[idx],1);
		}
		obj.onkeypress = obj.onchange;

		obj.parentNode.insertBefore(fauxSel,obj);
	},

	selectMe: function(liObj) {
		var selectElement = liObj.parentNode.parentNode.parentNode.nextSibling;
		var lis = liObj.parentNode.getElementsByTagName('li');
		for (var i=0; i<lis.length; i++) {
			if (lis[i] != liObj) {
				lis[i].className = '';
				lis[i].onclick = function() {
					Sbr.selectMe(this);
				}
			} else {
				this.setVal(liObj.selectID, liObj.selIndex);
				liObj.className = 'sbrSelected';
				var curOpt = liObj.parentNode.parentNode.parentNode.childNodes[0];
				curOpt.removeChild(curOpt.childNodes[0]);
				curOpt.appendChild(liObj.childNodes[0].cloneNode(false));
				if(arguments[1] != 1){
					selectElement.onchange();
				}
			}
		}
	},

	setVal: function(objID, val) {
		var obj = document.getElementById(objID);
		obj.selectedIndex = val;
	}
}


var balloonTip = {
	show: function(source, offset, balloonClass,event) {
		if (!this.balloon) this.build();
		if (!source) return false;
		if (typeof(source) == 'object' && source.tagName != null) {
			this.source = source.cloneNode(true);
			this.source.style.display = '';
		} else {
			this.source = document.createTextNode(source);
		}
		this.offset = offset ? (typeof(offset)!='object' ? {'x':offset,'y':offset} : offset ) : {'x':0,'y':0};
		this.balloon.className = balloonClass ? balloonClass : '';

		this.tip.appendChild(this.source);
		this.sizes = {
			scrollX : (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft),
			scrollY : (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop),
			winX : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth),
			winY : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight)
		};
		balloonTip.posit(event);
		document.onmousemove = function(event) {balloonTip.posit(event);}
		this.balloon.style.display = 'block';
	},

	build: function() {
		this.balloon = document.createElement('div');
		this.balloon.setAttribute('id','balloon');
		this.balloon.style.display = 'none';
		this.tip = document.createElement('div');
		this.tip.setAttribute('id','balloon-tip');
		this.balloon.appendChild(this.tip);
		document.getElementsByTagName('body').item(0).appendChild(this.balloon);
	},

	posit: function(evt) {
		//this.balloon.style.left = 'auto';
		var bX = this.balloon.offsetWidth;
		var bY = this.balloon.offsetHeight;
		var mx = this.mouseX(evt);
		var my = this.mouseY(evt);
		if(mx<0 || my<0){
			return;
		}
		var x = mx + this.offset.x;
		var y = my + this.offset.y;

		if (bX < this.sizes.winX && x - this.sizes.scrollX + bX >= this.sizes.winX) {
			x -= bX + this.offset.x * 2;
		}
		if (bY < this.sizes.winY && y - this.sizes.scrollY + bY >= this.sizes.winY) {
			y -= bY + this.offset.y * 2;
		}

		this.balloon.style.left = x + 'px';
		this.balloon.style.top = y + 'px';
	},

	hide: function() {
		document.onmousemove = function() { return false; }
		try{this.tip.removeChild(this.source);}catch(e){}
		this.source = null;
		this.balloon.style.display = 'none';
		this.balloon.style.left = this.balloon.style.top = '-1000px';
	},

	mouseX: function(evt) {
		if (document.all) if (event) evt = event;
		if (evt.pageX)
			return evt.pageX;
		if (evt.clientX)
			return evt.clientX + this.sizes.scrollX;
		return -1;
	},
	mouseY: function(evt) {
		if (document.all) if (event) evt = event;
		if (evt.pageY)
			return evt.pageY;
		if (evt.clientY)
			return evt.clientY + this.sizes.scrollY;
		return -1;
	}
}

/* hides 'splash' div and sets cookie 'cms_splash_screen' about it */
function hideSplash() {
	expires = 365; // days

	var today = new Date();
	today.setTime( today.getTime() );
	var expires_date = new Date( today.getTime() + (expires * 1000 * 60 * 60 * 24) );
	document.cookie = 'cms_splash_screen=1' + ';expires=' + expires_date.toGMTString();

	$('splash_out').style.display = 'none';
	$('splash').style.display = 'none';

}
/**
*@package EnvisioNext javascript library
*@subpackage Popup static window
*@copyright (C) 2007 EnvisioNext.com  <support@EnvisioNext.com>
*@author Anton Zavidov <anton.zavidov@envisionext.com.ua>
*/

//Base class
var popUpWindow = {
	// Init popuphandler
	// This function must be run after load document
	init : function()
	{
		var popups = $$(".tooltip");
		for(var i=0; i<popups.length; i++){
			var activePopUpElement = this.find(popups[i],"prev");
			if(activePopUpElement)
			{
				Event.observe(activePopUpElement,'click',this.open);
			}
		}
	},
	// Find handler object
	find : function(element,mode)
	{
		var prev = true;
		clas = (mode == "prev")? "popup" : "tooltip";
		while(element.className != clas && prev == true) {
			if((mode == "prev" && element.previousSibling) || (mode != "prev" && element.nextSibling)){
				element = (mode == "prev")? element.previousSibling : element.nextSibling;
			} else {
				prev = false;
				return false;
			}
		}
		return element;
	},
	// Open/Close popup window
	open : function()
	{
		element = (navigator.appName == "Microsoft Internet Explorer")?Event.element(event):this;
		var popUp = popUpWindow.find(element, "next");
		if(popUp)
		{
			if(popUp.style.display == "" || popUp.style.display == "none")
			{
				popUp.style.display = "block";
			} else {
				popUp.style.display = "none";
			}
		}
	},
	// Close popup window if clicked 'close'
	closePopUp : function(element)
	{
		element.parentNode.parentNode.style.display = "none";
	}
}

/**
*@package EnvisioNext javascript library
*@subpackage Toplist
*@copyright (C) 2007 EnvisioNext.com  <support@EnvisioNext.com>
*@author Anton Zavidov <anton.zavidov@envisionext.com.ua>
*/
var TopList = {
	globalDivClass : "list",
	linkClass : "name",
	coverImg : "coverImg",
	coverLink : "coverLink",
	urls1 : false,
	urls2 : false,
	imgs : false,
	init : function()
	{
		var element = $$("div."+TopList.globalDivClass+" a."+TopList.linkClass);
		this.urls1 = $$("." + TopList.coverLink);
		this.urls2 = $$("div.img a.name");
		this.imgs = $$("." + TopList.coverImg);
		if ($('top20dvds')){
		for (var i=0; i<element.length; i++) {
			Event.observe(element[i], 'mouseover', this.mouseOver.bindAsEventListener(this));
		}
		}
	},
	mouseOver : function(event)
	{
		var element = Event.element(event);
		if(element.tagName!="A"){
			element = element.parentNode;
		}
		this.urls1[0].href = element.href;
		this.urls1[1].href = element.href;
		this.urls2[0].href = element.href;
		this.urls2[1].href = element.href;
		var div = element.nextSibling;
		if(!div)div = element.parentNode.nextSibling;
			try{
				this.imgs[0].src = div.childNodes[0].src;
				this.imgs[1].src = div.childNodes[1].src;
			} catch(e){
				var div = element.parentNode.parentNode.nextSibling;
				try{
					this.imgs[0].src = div.childNodes[0].src;
					this.imgs[1].src = div.childNodes[1].src;
				} catch(e){}
			}
	}
}
