/*
	Stavropol College of Communication
	core lib for all scripts, based on examples at javascript.ru
	
	Tkachev Philipp
	zoonman at gmail . com
	
*/




/* task after onDomReady */
var readyList = [];
/* element cache */
var _eCache = {};
/* element accessor */
function $(id) {
	return _eCache[id] || (_eCache[id] = $_(id));
}
/* element accessor w/o cache */
function $_(id) {
	return ((document.getElementById) ? document.getElementById(id) : document.all(id));
}
 
function bindReady(handler){
	var called = false;
	function ready() { // (1)
		if (called) return;
		called = true;
		handler();
	}
	if ( document.addEventListener ) { // (2)
		document.addEventListener( "DOMContentLoaded", function(){
			ready();
		}, false );
	} 
	else if ( document.attachEvent ) {  // (3)
		// (3.1)
		if ( document.documentElement.doScroll && window == window.top ) {
			function tryScroll(){
				if (called) return;
				if (!document.body) return;
				try {
					document.documentElement.doScroll("left");
					ready();
				} catch(e) {
					setTimeout(tryScroll, 0);
				}
			}
			tryScroll();
		}
		// (3.2)
		document.attachEvent("onreadystatechange", function(){
			if ( document.readyState === "complete" ) {
				ready()
			}
		});
	}
	// (4)
  if (window.addEventListener)
      window.addEventListener('load', ready, false)
  else if (window.attachEvent)
      window.attachEvent('onload', ready)
  /*  else  // (4.1)
      window.onload=ready
	*/
}

/* Has class? */
function has_class(obj, clsname) {
	return new RegExp("(^|\\s)"+clsname+"(\\s|$)").test(obj.className)
}

/* OnDomReady */
function onReady(handler) {
	if (!readyList.length) {
		bindReady(function() {
			for(var i=0; i<readyList.length; i++) {
				readyList[i]()
			}
		})
	}
	readyList.push(handler)
}
/* Binding Events to Nodes */ 
function addEvent(elem, type, handler){
  if (elem && elem.addEventListener){
    elem.addEventListener(type, handler, false)
  } 
	else if(elem) {
    elem.attachEvent("on"+type, handler)
  }
} 
/* Navigation Ctrl + Left or Right */
function navigate(e) {
	var event = e || window.event; 
	if($('prev_page')) {
		if(event.ctrlKey == true && event.keyCode == 37) {
			window.location =  $('prev_page').href;
		};
	}
	if($('next_page')) {
		if(event.ctrlKey == true && event.keyCode == 39) {
			window.location =  $('next_page').href;
		};
	}
}


/* форма обратной связи */
function maxlength(){
	var message=document.feedback.message.value;
	var maxLenght=1000;//Максимальное количество символов в сообщении
	if (message.length>maxLenght) document.feedback.message.value=message.substring(0,maxLenght);
	verifyForm(document.feedback);
}

function verifyForm(form) {
	if(form.email.value=="" 
			|| (!/^[a-z0-9\._-]+@[a-z0-9\._-]+\.[a-z]{2,4}$/i.test(form.email.value))) {
		form.email.style.color='red';
	}
	else {
		form.email.style.color='green';
	}
	
	if(form.name.value=="" 
			|| form.email.value=="" 
			|| (!/^[a-z0-9\._-]+@[a-z0-9\._-]+\.[a-z]{2,4}$/i.test(form.email.value))
			|| form.message.value=="")	{
		form.submit.disabled=true;
	}
	else {
		form.submit.disabled=false;
	}
}



/*
 Ajax Loader Class
*/

/* namespacing object */
var net=new Object();
net.READY_STATE_UNINITIALIZED=0;
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
net.READY_STATE_COMPLETE=4;
/*--- content loader object for cross-browser requests ---*/
net.ContentLoader=function(url,onload,onerror,method,params,contentType,update_object){
  this.req=null;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  this.update_object = update_object;
  this.loadXMLDoc(url,method,params,contentType);
}
net.ContentLoader.prototype.loadXMLDoc=function(url,method,params,contentType){
  if (!method){
    method="GET";
  }
  if (!contentType && method=="POST"){
    contentType='application/x-www-form-urlencoded';
  }

  if (!this.req) try {
    this.req = new XMLHttpRequest();
  } catch (e){;};
  if (!this.req) try {
    this.req = new ActiveXObject('Msxml2.XMLHTTP');
  } catch (e){;};
  if (!this.req) try {
    this.req = new ActiveXObject('Microsoft.XMLHTTP');
  } catch (e){;};
  
  if (this.req){
    try{
      var loader=this;
      this.req.onreadystatechange=function(){
        net.ContentLoader.onReadyState.call(loader);
      }
      this.req.open(method,url,true);
      if (contentType){
        this.req.setRequestHeader('Content-Type', contentType);
      }
      this.req.send(params);
    }catch (err){
      this.onerror.call(this);
    }
  }
}
net.ContentLoader.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState;
  
  if (ready==net.READY_STATE_COMPLETE){
  	var httpStatus=req.status;
    if (httpStatus==200 || httpStatus==0){
      this.onload.call(this);
    }else{
      this.onerror.call(this);
    }
  }
}
net.ContentLoader.prototype.defaultError=function(){
  alert("error fetching data!"
    +"\n\nreadyState:"+this.req.readyState
    +"\nstatus: "+this.req.status
    +"\nheaders: "+this.req.getAllResponseHeaders());
}
/*
	Simple Loader w/o callback
*/
function ajquery(quer,t) {
	var l=new net.ContentLoader(quer, function(t) {
		//alert(t);
	 //return t = this.req.responseText	;	
	 },null);
	//
}


/* Menu Implementation */
var menu_t = new Array;
function show_menu(m) {
	$(m).style.display='block'
	window.clearTimeout(menu_t[m]);
}

function hide_menu(m) {
	var fcb = hide(m);
	menu_t[m] = window.setTimeout(fcb,150);
}

function hide(m) {
	return(function(){
	 $(m).style.display='none'
	});
}

function hc(o){
	return (function(){
		hide_menu(o)
	});
}

function sc(o){
	return (function(){
		show_menu(o)
	});
}



onReady( function () {
	/* binding menu navigation */
	var bind_objects = {'menu1':'menu1-invoker','menu2':'menu2-invoker','menu3':'menu3-invoker'};
	for(var o in bind_objects) {
		var mt = hc(o); 
		var mr = sc(o); 
		addEvent($(bind_objects[o]), 'mouseout', mt);
		addEvent($(bind_objects[o]), 'mouseover',mr);
		addEvent($(o), 'mouseout',  mt);
		addEvent($(o), 'mouseover', mr);
	}

	/* binding Ctrl+ navigation */
	addEvent(document, 'keyup', navigate);
	//document.onkeyup = ;
	/* binding e-mail test
		todo: move it to callback template
	*/
	if ($_('feedback')) {
		setInterval("maxlength()",100);
		if( /\#s/.test(window.location) ) {
			var index = window.location.hash.substring(3);
			$('theme').selectedIndex  = index;
		}
	}
	if ($('container1'))	var c1 = new FadeViewer('container1',20, 10000);
});



function stop_bubbling(e) {
    e = e || window.event
    e.preventDefault ? e.preventDefault() : (e.returnValue=false)
}


FadeViewer = function (object_name, speed, changeDuration) {
	// Edit element cache 
	this.container = document.getElementById(object_name);
	this.items = [];
	this.currentItemIndex = 0;
	this.nextItemIndex = 0;
	this.fadeTimer = null;
	this.opacityTimer = null;
	this.currentOpacity = 0;
	if (speed) {
		this.speed = speed;
	}
	else {
		this.speed = 10	;
	}
	
	if (changeDuration) {
		this.changeDuration = changeDuration;
	}
	else {
		this.changeDuration = 5000;
	}
	this.notBusy = true;
	this.mouseBusy = true;
	var parobj = this;
	// all Container 
	var lc = document.getElementById(object_name).getElementsByTagName('DIV');
	for(var i= 0; i < lc.length; i++) {
		if (/prev/.test(lc[i].className)) this.prev = lc[i];
		if (/next/.test(lc[i].className)) this.next = lc[i];
		if (/item/.test(lc[i].className)) {this.items.push(lc[i]);
		set_opacity(lc[i],0)}
	}
	this.currentElement = this.items[this.currentItemIndex];
	this.nextElement = this.items[this.nextItemIndex];
	
	this.prev.onclick = function () {
		if (parobj.notBusy) setItem(-1);
	}
	this.next.onclick = function () {
		if (parobj.notBusy) setItem(1);
	}
	
	this.container.onmouseover = function () {
		set_opacity(parobj.prev,0.5);
		set_opacity(parobj.next,0.5);
		parobj.mouseBusy = false;
	}
	
	this.container.onmouseout = function () {
		set_opacity(parobj.prev,0.1);
		set_opacity(parobj.next,0.1);
		parobj.mouseBusy = true;

	}
	
	function setItem(dir) {
		if (dir > 0) {
			if (parobj.nextItemIndex < parobj.items.length-1) {
				parobj.nextItemIndex++; 
			}
			else {
				parobj.nextItemIndex=0;
			}
		}
		else {
			if (parobj.nextItemIndex > 0) {
				parobj.nextItemIndex--; 
			}
			else {
				parobj.nextItemIndex=parobj.items.length-1;
			}
		}
		parobj.currentElement = parobj.nextElement;
		parobj.nextElement = parobj.items[parobj.nextItemIndex];
		parobj.notBusy = false;
		parobj.opacityTimer = window.setInterval(fade_out, parobj.speed);
	}
	
	function fade_out() {
		parobj.currentOpacity = parobj.currentOpacity - 0.04;
		set_opacity(parobj.currentElement,parobj.currentOpacity);
		if(parobj.currentOpacity <= 0) {
			window.clearInterval(parobj.opacityTimer);
			parobj.currentElement.style.display = 'none';
			parobj.nextElement.style.display = 'block';
			parobj.opacityTimer = window.setInterval(fade_in, parobj.speed);
		}
	}
	
	function fade_in() {
		parobj.currentOpacity = parobj.currentOpacity + 0.04;
		set_opacity(parobj.nextElement,parobj.currentOpacity);
		if(parobj.currentOpacity >= 1) {
			window.clearInterval(parobj.opacityTimer);
			parobj.notBusy = true;
		}
	}
		
	function set_opacity(tElement,opacity) {
		//document.getElementById('tmmm').innerHTML = parobj.currentOpacity + tElement.innerHTML
		if(document.all) {
			tElement.style.filter = 'alpha(opacity=' + opacity*100 + ')';
		}
		else {
			tElement.style.opacity = opacity;
		}
	}
	function nextItem() {
		if (parobj.notBusy && parobj.mouseBusy)  setItem(1);
	}
	parobj.fadeTimer = window.setInterval(nextItem, this.changeDuration);
	this.currentOpacity = 1;
	this.currentElement.style.display = 'block';
	set_opacity(this.currentElement,1);
}	



/* есть ли класс у объекта */
function has_class(obj, clsname) {
	return new RegExp("(^|\\s)"+clsname+"(\\s|$)").test(obj.className)
}
/* переключить класс объекта */
function switch_class(obj, clsname, ncls){
	if (has_class(obj, clsname)) {
		var ro =  new RegExp("(^|\\s)"+clsname+"(\\s|$)"); 
		obj.className = obj.className.replace(ro, '$1'+ncls+'$2') 
	}
	else {
		if (! has_class(obj, ncls))	obj.className += " "+ncls;
	}
}

/* триггер класса */
function trigger_class(obj,clss) {
	if (has_class(obj,clss)) {
		switch_class(obj,clss,'');
	}
	else{
		obj.className += " "+clss;
	}
}
