/*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 fragment = document.createDocumentFragment(),
		scrolling = getBodyScrollTop(body),
		body = document.getElementsByTagName("body")[0],
	    over = document.createElement('div'),
		h,
		conteiner = document.createElement('div'),
		inside = document.createElement('div'),
		style,
		image = document.createElement('img'),
		descr = document.createElement('p');
		 
		
 	over.id = "over";  	         
 	conteiner.id = "conteinerimg";             
	inside.id = "inside"; 
	inside.style.width = width+"px";
	inside.style.height = height+"px";
	inside.style.margin = scrolling+"px auto 0 auto";    
	
	h = document.getElementById("conteiner").offsetHeight;
	h = (scrolling + height>h)?(scrolling + height+ 20):h;
	over.style.height = h + 20 +"px";       
	
	image.width=width;
	image.height=height;
	image.title="Закрыть";
	image.src = "/"+url;
	                                         
	descr.appendChild(document.createElement('span')).innerHTML = alt;
	
	inside.appendChild(image);
	inside.appendChild(descr);
    conteiner.appendChild(inside);
	fragment.appendChild(over);
	fragment.appendChild(conteiner); 
	//////////////////////////////////////////
	image.onclick = function() {
		over.parentNode.removeChild(over);
		conteiner.parentNode.removeChild(conteiner);
	}
	body.appendChild(fragment);
}

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) 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 Switcher(id, start){
	var nav = navigator.appVersion.match(/MSIE/); 
	var st = 0;
	var act = 0;
   	if(start!=null){
    	st = start;
		act = start;		
	}
	var cur = act; 
	var arr = document.getElementById(id).getElementsByTagName("img");
	if(arr.length == 0) return;
	
	
	function switchMe(){
		cur = act;      
		act++;
	  	if(act > arr.length - start)
	  		act = st;                           
		for(var i=st; i<arr.length; i++){
				arr[i].style.zIndex = "1";
		}
		arr[cur].style.zIndex = "2";
		arr[act].style.left = "247px";
		arr[act].style.zIndex = "3";
			if(!nav)
				arr[act].style.opacity = 0;
		   	else                                      
	   			arr[act].style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity=0)";
	   gogo(arr[act], 0)
	}   
	
	setTimeout(switchMe, 1000); 	
	
	function gogo(img, op){      
		var l =  parseInt(img.style.left); 		
		op += 100/l;
		
		if(!nav)
			img.style.opacity = op/100;
	   	else                                      
	   		img.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity="+op+")"     
		
		
		if(l==1)
			l--;
		else
			l -= 2;
		img.style.left = l+"px";
		
		if(l>1)
			setTimeout(function(){gogo(img, op)}, 1);
		else{ 
		   setTimeout(switchMe, 1000);
		}
	} 	
}

function AlkoGallery(id, i){
	var instance = this;
	this.current_image = 0;
	var scrolling = getBodyScrollTop(body);
	var body = document.getElementsByTagName("body")[0];
	this.over = body.appendChild(document.createElement('div'));
	this.over.id = "over";
	var h = document.getElementById("conteiner").offsetHeight;
	this.over.style.height = h + 20 + "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.width = "auto";
	this.inside.style.height = "auto";
	this.inside.style.marginLeft = "auto";
	this.inside.style.marginRight = "auto";
	this.inside.style.marginTop = scrolling+"px";
	this.inside.style.textAlign = 'center';
	this.inside.style.position = 'relative';
	this.inside.style.padding = '30px';
	
	var buttonNext = this.inside.appendChild(document.createElement('img'));
	buttonNext.id = 'buttonNext';
	buttonNext.src = '/images/img_next.gif';
	buttonNext.title = 'Следующая страница';
	buttonNext.onclick = function(){
		instance.NextImage();
	}
	
	var buttonPrev = this.inside.appendChild(document.createElement('img'));
	buttonPrev.id = 'buttonPrev';
	buttonPrev.src = '/images/img_prev.gif';
	buttonNext.title = 'Предыдущая страница';
	buttonPrev.onclick = function(){
		instance.PrevImage();
	}
	
	var buttonExit = this.inside.appendChild(document.createElement('img'));
	buttonExit.id = 'buttonExit';
	buttonExit.src = '/images/img_exit.gif';
	buttonExit.title = 'Закрыть';
	buttonExit.onclick = function(){
		instance.closeImage();
	}
	
	this.image = this.inside.appendChild(document.createElement('img'));
	this.image.onclick = function(){
		instance.NextImage();
	}
	this.image.oncontextmenu = function(){
		return false;
	}
	this.photos = document.getElementById(id).getElementsByTagName('img');
	this.current_image = (i != null) ? i : 0;
	
	this.dsc = this.inside.appendChild(document.createElement('p'));
	this.dsc.innerHTML = '';
	
	this.setImage = function(){
		/*
		display_width = 800;
		display_height = 600;
		if(instance.photos[instance.current_image].type == 0 || true){
			instance.image.setAttribute('height', display_height);
			instance.image.setAttribute('width', Math.round(display_height * instance.photos[instance.current_image].width / instance.photos[instance.current_image].height));
		}else{
			instance.image.setAttribute('width', display_width);
			instance.image.setAttribute('height', Math.round(display_width * instance.photos[instance.current_image].height / instance.photos[instance.current_image].width));
		}
		*/
		
		instance.image.setAttribute('width', instance.photos[instance.current_image].getAttribute('real_width'));
		instance.image.setAttribute('height', instance.photos[instance.current_image].getAttribute('real_height'));
		instance.image.setAttribute("src", instance.photos[instance.current_image].getAttribute('real_src'));
		
		instance.inside.style.width = instance.image.width + "px";
		instance.inside.style.height = instance.image.height + "px";
		instance.dsc.innerHTML = (instance.current_image + 1) + " из " + instance.photos.length;
		
		buttonNext.style.top = Math.round((instance.image.height + buttonNext.width) / 2) + "px";
		buttonPrev.style.top = Math.round((instance.image.height + buttonPrev.width) / 2) + "px";
	}
	
	this.NextImage = function(){
		if(instance.current_image + 1 < instance.photos.length){
			instance.current_image += 1;
		}else{
			instance.closeImage();
			return ;
		}
		instance.setImage();
	}
	
	this.PrevImage = function(){
		if(instance.current_image - 1 >= 0){
			instance.current_image -= 1;
		}
		instance.setImage();
	}
	
	this.closeImage = function(){
		instance.over.parentNode.removeChild(instance.over);
		instance.conteiner.parentNode.removeChild(instance.conteiner);
	}
	
	// this.over.onclick = function(){
		// instance.closeImage();
		// return ;
	// }
	// this.conteiner.onclick = function(){
		// instance.closeImage();
		// return ;
	// }
	
	this.setImage();
}
