/*global window, document, navigator*/
var flashinstalled = 0,
	flashversion = 0,
	MSDetect = "false";


function M(){
		var D = "undefined",
			S = "Shockwave Flash",
			W = "ShockwaveFlash.ShockwaveFlash",
			q = "application/x-shockwave-flash",
			R = "SWFObjectExprInst",
			x = "onreadystatechange",
			O = window,
			r = "object",
			aa = typeof document.getElementById != D && typeof document.getElementsByTagName != D && typeof document.createElement != D, 
			ah = navigator.userAgent.toLowerCase(), 
			Y = navigator.platform.toLowerCase(), 
			ae = Y ? /win/.test(Y) : /win/.test(ah), 
			ac = Y ? /mac/.test(Y) : /mac/.test(ah), 
			af = /webkit/.test(ah) ? parseFloat(ah.replace(/^.*webkit\/(\d+(\.\d+)?).*$/, "$1")) : false, 
			X = !+"\v1", T,
			ag = [0, 0, 0], 
			ab = null;
		
        if (typeof navigator.plugins != D && typeof navigator.plugins[S] == r) {
            ab = navigator.plugins[S].description;
            if (ab && !(typeof navigator.mimeTypes != D && navigator.mimeTypes[q] && !navigator.mimeTypes[q].enabledPlugin)) {
                T = true;
                X = false;
                ab = ab.replace(/^.*\s+(\S+\s+\S+$)/, "$1");
                ag[0] = parseInt(ab.replace(/^(.*)\..*$/, "$1"), 10);
                ag[1] = parseInt(ab.replace(/^.*\.(.*)\s.*$/, "$1"), 10);
                ag[2] = /[a-zA-Z]/.test(ab) ? parseInt(ab.replace(/^.*[a-zA-Z]+(.*)$/, "$1"), 10) : 0;
            }
        }
        else {
            if (typeof O.ActiveXObject != D) {
                try {
                    var ad = new ActiveXObject(W);
                    if (ad) {
                        ab = ad.GetVariable("$version");
                        if (ab) {
                            X = true;
                            ab = ab.split(" ")[1].split(",");
                            ag = [parseInt(ab[0], 10), parseInt(ab[1], 10), parseInt(ab[2], 10)];
                        }
                    }
                } 
                catch (Z) {
                }
            }
        }
        return {
            pv: ag,
            ie: X
        }
    }
    function F(Z){	
        var Y = M().pv,
			X = Z.toString().split(".");
        X[0] = parseInt(X[0], 10);
        X[1] = parseInt(X[1], 10) || 0;
        X[2] = parseInt(X[2], 10) || 0;
        return (Y[0] > X[0] || (Y[0] == X[0] && Y[1] > X[1]) || (Y[0] == X[0] && Y[1] == X[1] && Y[2] >= X[2])) ? true : false;
    }

function insFl(v,fl,gif,lnk,w,h,flash_vars) {	
	var d=' width='+w+' height='+h,
		flash;
	if (F(v)) {
		flash = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'+d+'><param name="wmode" value="transparent"/><param name="movie" value="'+fl+'"/>';
		 if(flash_vars){
			flash += '<param name="flashvars" value="'+flash_vars+'"/>';
		 }
		flash += '<embed src="'+fl+'" quality=high'+d+' wmode="transparent"';
		if(flash_vars){
			flash += ' flashvars="'+flash_vars+'" ';
		}
		flash += '></embed></object>';
		
		document.write(flash);
	} else {	
		if (gif != '') {
			document.write('<a href='+lnk+'><img src='+gif+d+' border=0></a>')
		}
		else if(M().pv[0]>6){
			document.write("<div id='noFlash'></div>");
			var txt;
			if(M().ie){
				txt = '<object width="310" height="137" id="SWFObjectExprInst" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param name="flashvars" value="MMplayerType=ActiveX"/><param name="movie" value="/images/expressInstall.swf"/></object>';
			}else{
				txt = '<object width=300 height=120 type="application/x-shockwave-flash" data="/images/expressInstall.swf" id="SWFObjectExprInst"><param name="flashvars" value=""/></object>';
			}
				document.write(txt);
		}
		else{
			document.write(noFlash)
		}
	}
}

var swfobject = {
	expressInstallCallback: function(){
		document.getElementById("SWFObjectExprInst").parentNode.removeChild(document.getElementById("SWFObjectExprInst"));
		document.getElementById("noFlash").innerHTML = noFlash;
		}
	}

var noFlash = "<div style='text-align:center;padding:10px'><noindex><div style='width:400px;margin:0 auto;'><h2 style='text-decoration:none'><img width='16' height='16' alt='Внимание' src='/images/alert.gif'/> Внимание</h2><p>Чтобы посмотреть содержимое страницы, должен быть включен JavaScript, и установлена последняя версия Adobe Flash Player.</p><p class='caption'><a target='_blank' href='http://get.adobe.com/flashplayer/'>Скачать Flash Player!</a></p><a target='_blank' href='http://get.adobe.com/flashplayer/'><img  alt='Скачать Flash Player' src='/images/get_flash_player.gif'/></a></div></noindex></div>";

function ShowPhoto(url, width, height) {
	var scroll = (screen.width < 1000) ? "yes" : "no";
	
	var left = (self.screen.width >> 1) - (width >> 1);
	var top = (self.screen.height >> 1) - (height >> 1);
	var param = 'left='+left+',top='+top+',width='+width+',height='+height+',';
	
	window.open(url, '_blank', param+'menubar=no, scrollbars=' + scroll + ', status=no');
}

function emoticon(theSmilie)
{
	doInsert(" " + theSmilie + " ", "", false);
}

function doInsert(ibTag, ibClsTag, isSingle)
{
	var isClose = false;
	var obj_ta = document.post.message;
	
	//----------------------------------------
	// It's IE!
	//----------------------------------------
	if ( (ua_vers >= 4) && is_ie && is_win){
		if (obj_ta.isTextEdit){
			obj_ta.focus();
			var sel = document.selection;
			var rng = sel.createRange();
			rng.colapse;
			if((sel.type == "Text" || sel.type == "None") && rng != null){
				if(ibClsTag != "" && rng.text.length > 0)
					ibTag += rng.text + ibClsTag;
				else if(isSingle)
					isClose = true;
	
				rng.text = ibTag;
			}
		}
		else{
			if(isSingle){
				isClose = true;
			}			
			obj_ta.value += ibTag;
		}
	}
	//----------------------------------------
	// It's MOZZY!
	//----------------------------------------
	
	else if ( obj_ta.selectionEnd ){ 
		var ss = obj_ta.selectionStart,
			st = obj_ta.scrollTop,
			es = obj_ta.selectionEnd;
		
		if (es <= 2){
			es = obj_ta.textLength;
		}
		
		var start  = (obj_ta.value).substring(0, ss);
		var middle = (obj_ta.value).substring(ss, es);
		var end    = (obj_ta.value).substring(es, obj_ta.textLength);
		
		//-----------------------------------
		// text range?
		//-----------------------------------
		
		if (obj_ta.selectionEnd - obj_ta.selectionStart > 0){
			middle = ibTag + middle + ibClsTag;
		}
		else{
			middle = ibTag + middle;
			
			if (isSingle){
				isClose = true;
			}
		}
		
		obj_ta.value = start + middle + end;
		
		var cpos = ss + (middle.length);
		
		obj_ta.selectionStart = cpos;
		obj_ta.selectionEnd   = cpos;
		obj_ta.scrollTop      = st;
	}
	//----------------------------------------
	// It's CRAPPY!
	//----------------------------------------
	else{
		if (isSingle){
			isClose = true;
		}		
		obj_ta.value += ibTag;
	}
	
	obj_ta.focus();

	return isClose;
}	
function getBodyScrollTop() {
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function ShowPhotoHere(url, alt, width, height) {

    var scrolling = getBodyScrollTop(body)
	var body = document.getElementsByTagName("body")[0];
    var over = body.appendChild(document.createElement('div'));
 	over.id = "over";
	var h = document.getElementById("wrapper").offsetHeight;
	if(height > h) h = height + 20 + scrolling;
	over.style.height = h+60+"px"
	if(width > body.offsetWidth) over.style.width = width + 60 +"px"
 	var conteiner = body.appendChild(document.createElement('div'));
 	conteiner.id = "conteinerimg";
	var inside = conteiner.appendChild(document.createElement('div'));
	inside.id = "inside";
	inside.style.width = width+"px";
	inside.style.height = height+"px";
	inside.style.marginLeft = "auto";
	inside.style.marginRight = "auto";
    inside.style.marginTop = scrolling+"px";
	var image = inside.appendChild(document.createElement('img'));
	image.width=width;
	image.height=height;
	image.title="Закрыть";
	var descr = inside.appendChild(document.createElement('p'));
//   	image.setAttribute("src", url);
   	image.setAttribute("src", "/"+url);

	descr.appendChild(document.createElement('span')).innerHTML = alt;

	//////////////////////////////////////////


	image.onclick = function() {
		over.parentNode.removeChild(over);
		conteiner.parentNode.removeChild(conteiner);
	}
}

function getBrowserInfo() {
	var t,v = undefined;
	if (window.opera) t = 'Opera';
	else if (document.all) {
		t = 'IE';
		var nv = navigator.appVersion;
		var s = nv.indexOf('MSIE')+5;
		v = nv.substring(s,s+1);
	}
	else if (navigator.appName) t = 'Netscape';
	return {type:t,version:v};
}
 
function bookmark(a){
	var url = window.document.location;
	var title = window.document.title;
	var b = getBrowserInfo();
	if (b.type == 'IE' && 8 > b.version && b.version >= 4) window.external.AddFavorite(url,title);
	else if (b.type == 'Opera') {
		a.href = url;
		a.rel = "sidebar";
		a.title = url+','+title;
		return true;
	}
	else if (b.type == "Netscape") window.sidebar.addPanel(title,url,"");
	else alert("Нажмите CTRL-D, чтобы добавить страницу в закладки.");
	return false;
}

var jsReady = false;
function isReady() {
 return jsReady;
}
function pageInit() {
 jsReady = true;
 document.forms["form1"].output.value += "\n" + "JavaScript is ready.\n";
}
function thisMovie(movieName) {
 if (navigator.appName.indexOf("Microsoft") != -1) {
     return window[movieName];
 } else {
     return document[movieName];
 }
}
function getXY(value) {
 document.getElementById("coords").value = value;
}

function getNoFlash(){
	if(!F("9.1.15")){
		document.getElementById("noflashhead").style.display = "block";
	}
}

function AlkoGallery(id, i){
	var it = this;
	var scrolling = getBodyScrollTop();
	var body = document.getElementsByTagName("body")[0];
	
	this.current_image = (i != null) ? i : 0;
	this.last_image = -1;
	this.zoomStep = 1;
	this.zoomScale = 0;
	this.zoomScaleMin = 0;
	
	this.over = body.appendChild(document.createElement('div'));
	this.over.id = "over";
	this.over.style.height = document.getElementById("wrapper").offsetHeight + "px";
	
 	this.conteiner = body.appendChild(document.createElement('div'));
 	this.conteiner.id = "conteinerimg";
	
	this.inside = this.conteiner.appendChild(document.createElement('div'));
	this.inside.id = "inside";
	this.inside.style.marginLeft = "auto";
	this.inside.style.marginRight = "auto";
	this.inside.style.marginTop = scrolling + "px";
	this.inside.style.textAlign = 'center';
	
	this.buttonNext = this.inside.appendChild(document.createElement('img'));
	this.buttonNext.id = 'buttonNext';
	this.buttonNext.src = '/images/next.gif';
	this.buttonNext.title = 'Следующая страница';
	this.buttonNext.onclick = function(){
		it.NextImage();
	}
	
	this.buttonPrev = this.inside.appendChild(document.createElement('img'));
	this.buttonPrev.id = 'buttonPrev';
	this.buttonPrev.src = '/images/prev.gif';
	this.buttonNext.title = 'Предыдущая страница';
	this.buttonPrev.onclick = function(){
		it.PrevImage();
	}
	
	this.buttonExit = this.inside.appendChild(document.createElement('img'));
	this.buttonExit.id = 'buttonExit';
	this.buttonExit.src = '/images/window-close.gif';
	this.buttonExit.title = 'Закрыть';
	this.buttonExit.onclick = function(){
		it.closeImage();
	}
	
	this.buttonZoomIn = this.inside.appendChild(document.createElement('img'));
	this.buttonZoomIn.id = 'buttonZoomIn';
	this.buttonZoomIn.src = '/images/zoom-in.gif';
	this.buttonZoomIn.title = 'Увеличить';
	this.buttonZoomIn.onclick = function(){
		it.zoomIn();
	}
	this.buttonZoomOut = this.inside.appendChild(document.createElement('img'));
	this.buttonZoomOut.id = 'buttonZoomOut';
	this.buttonZoomOut.src = '/images/zoom-out.gif';
	this.buttonZoomOut.title = 'Уменьшить';
	this.buttonZoomOut.onclick = function(){
		it.zoomOut();
	}
	
	this.photos = document.getElementById(id).getElementsByTagName('img');
	
	var loading = this.inside.appendChild(document.createElement('div'));
	loading.className = "loading";
	
	this.image = loading.appendChild(document.createElement('img'));
	this.image.className = "loading";
	this.image.onclick = function(){
		it.NextImage();
	}
	this.image.oncontextmenu = function(){
		return false;
	}
	
	this.dsc = this.inside.appendChild(document.createElement('p'));
	this.dsc.innerHTML = '<input type="text" value="' + (this.current_image+1) + '" maxlength="3" style="text-align:center; width: 24px"/> из <strong>' + it.photos.length + '</strong>';
	this.dsc.childNodes[0].onkeyup = function(){
		var n = parseInt(this.value);
		if(n > 0 && n <= it.photos.length){
			it.current_image = n - 1;
			it.setImage();
		}
	}
	
	this.setImage = function(){
		var img_width, img_height, min_width = 800, min_height = 600;
		img_width = parseInt(it.photos[it.current_image].getAttribute('real_width'));
		img_height = parseInt(it.photos[it.current_image].getAttribute('real_height'));
		
		if(img_height * it.zoomScale < min_height){
			it.zoomScale = min_height / img_height;
			it.zoomScaleMin = it.zoomScale;
		}
		img_width = Math.round(img_width * it.zoomScale);
		img_height = Math.round(img_height * it.zoomScale);
		
		if(it.current_image != it.last_image){
			it.image.style.display = "none";
			it.image.onload = function(){
				it.image.style.display = "block";
			}
			it.image.setAttribute("src", it.photos[it.current_image].getAttribute('real_src'));
		}
		it.last_image = it.current_image;
		
		it.image.setAttribute('width', img_width);
		it.image.setAttribute('height', img_height);
		
		it.image.parentNode.style.width = (img_width + 2) + "px";
		it.image.parentNode.style.height = (img_height + 2) + "px";
		
		it.inside.style.width = (img_width + 4) + "px";
		it.inside.style.height = (img_height + 4) +"px";
		it.dsc.childNodes[0].value = (it.current_image + 1);
		it.resizeOver();
	}
	
	this.NextImage = function(){
		if(it.current_image + 1 >= it.photos.length){
			it.closeImage();
			return ;
		}
		it.current_image += 1;
		it.setImage();
	}
	
	this.PrevImage = function(){
		if(it.current_image - 1 >= 0){
			it.current_image -= 1;
		}
		it.setImage();
	}
	
	this.closeImage = function(){
		it.over.parentNode.removeChild(it.over);
		it.conteiner.parentNode.removeChild(it.conteiner);
	}
	/*
	this.over.onclick = function(){
		it.closeImage();
		return ;
	}
	*/
	this.zoomIn = function(){
		it.zoomScale += it.zoomStep;
		if(it.zoomScale >= 1){
			it.zoomScale = 1;
		}
		it.hideZoomButtons();
		it.setImage();
	}
	
	this.zoomOut = function(){
		it.zoomScale -= it.zoomStep;
		if(it.zoomScale <= it.zoomScaleMin){
			it.zoomScale = it.zoomScaleMin;
		}
		it.hideZoomButtons();
		it.setImage();
	}
	
	this.hideZoomButtons = function(){
		if(it.zoomScale >= 1){
			it.buttonZoomIn.style.display = "none";
		}else{
			it.buttonZoomIn.style.display = "block";
		}
		if(it.zoomScale <= it.zoomScaleMin){
			it.buttonZoomOut.style.display = "none";
		}else{
			it.buttonZoomOut.style.display = "block";
		}
	}
	
	this.resizeOver = function(){
		var padding = 35;
		if(it.inside.offsetWidth > document.getElementById("wrapper").offsetWidth){
			it.over.style.width = (it.inside.offsetWidth) +"px";
		}else{
			it.over.style.width = document.getElementById("wrapper").offsetWidth + "px";
		}
		if(it.inside.offsetHeight + parseInt(it.inside.style.marginTop) > document.getElementById("wrapper").offsetHeight){
			it.over.style.height = (parseInt(it.inside.style.marginTop) + it.inside.offsetHeight) +"px";
		}else{
			it.over.style.height = document.getElementById("wrapper").offsetHeight + "px";
		}
		it.buttonNext.style.top = (Math.round(it.inside.offsetHeight / 2)) + "px";
		it.buttonPrev.style.top = it.buttonNext.style.top;
	}
	
	this.setImage();
	this.hideZoomButtons()
}

function resetForm(form){
	for(var i = 0; i < form.elements.length; i++){
		if(form.elements[i].type == 'text') form.elements[i].value = '';
	}
}
