// scripts-common.js for EES 2.4

// CREATING XHR-OBJECT
var getXHR = (function(){	var xhr;
	if(typeof XMLHttpRequest != "undefined") return function(){ return xhr = new XMLHttpRequest() }
	else if(window.ActiveXObject){		var xmlVers = ["MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp"];
		for(var i=0; i<xmlVers.length; i++){			try{				xhr = new ActiveXObject(xmlVers[i]);
				return function(){ return xhr }
			} catch(err){}
		}
		throw new Error("Невозможно создать объект XMLHttp");
	}
	return function(){ return xhr }
})()
function sendXHR( http_string, post_string, on_ready, on_false, obj ){	var xhr = getXHR();
	xhr.open("POST", http_string, true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
	xhr.onreadystatechange = function(){
		if(xhr.readyState != 4) return
		clearTimeout(xhrTimeOut);
		if(xhr.status == 200){
			var r = eval("("+xhr.responseText+")");
			if( +r["rc"] ){
				on_ready.call(null,r, obj);
			}
			else{				on_false.call(null,r["msg"]);
			}
		}
	}
	xhr.send(post_string);
	var xhrTimeOut = setTimeout( function(){ on_false.call(null,"Время ожидания ответа истекло."), xhr.abort() }, 10000);
}

// NO DIRECT FOLLOW THOROUGH LINKS
function noFollow(event){	event.preventDefault();
	$("a.from").unbind();
	$(aw_bg).css( {zIndex:"111"} );
	doFollow.call(this);
}

// FADE IN BG AND GO TO LINK
function goHref(link){	$(aw_bg).animate( {opacity:1}, {duration:444, complete:function(){
		window.location.href = link;
	}} );
}

// HOVER MENU HANDLERS
function mouseover(){
	$(this).stop().animate( {opacity: 0.66}, {duration:444});
}
function mouseout(){
	$(this).stop().animate( {opacity: 1}, {duration:444});
}

// -----------------------------------------------------------------------------
// DAY-NIGHT FUNCTIONS
// -----------------------------------------------------------------------------

function dn_mainpage(){
	$(this).unbind("click");
	if( day ){
		$("#main-logo").children().attr("src","/images/mystech-logo-big-tr.png");
		menu_strip.addClass("dn").removeClass("main-menu-strip-w");;
		mi.css( {opacity:""} ).addClass("mi-tr").removeClass("mi-w");
		$("#w").animate( {opacity:0}, {duration:999, complete:function(){
			day = false;
			mi.addClass("mi-b").removeClass("mi-tr");
			menu_strip.addClass("main-menu-strip-b").removeClass("dn");
			$("#main-logo").children().attr("src","/images/mystech-logo-big-b.png");
			$("#switcher").bind("click",dn_mainpage);
		}} );
	}
	else{
		$("#main-logo").children().attr("src","/images/mystech-logo-big-tr.png");
		menu_strip.addClass("dn").removeClass("main-menu-strip-b");
		mi.css( {opacity:""} ).addClass("mi-tr").removeClass("mi-b");
		$("#w").animate( {opacity:1}, {duration:999, complete:function(){
			day = true;
			mi.addClass("mi-w").removeClass("mi-tr");
			menu_strip.addClass("main-menu-strip-w").removeClass("dn");
			$("#main-logo").children().attr("src","/images/mystech-logo-big-w.png");
			$("#switcher").bind("click",dn_mainpage);
		}} );
	}
}

// -----------------------------------------------------------------------------
// WORKS FUNCTIONS
// -----------------------------------------------------------------------------

// Select works' filter
function wClick(){	$("table.work-cats td").css( {fontWeight:"normal"} );
	this.style.fontWeight = "bold";

	if( this.getAttribute("id") == "w-all" ){    	$("div.h-line, div.v-line").css( {display:"block"} );
    	$("div.pr-text").css( {display:"none"} );
    	works_items.css( {display:"block"} );
	}
	else{		works_items.css( {display:"none"} );

		if( this.getAttribute("id") == "pr-w" ){
			works_items.css( {display:"none"} );			$("div.h-line, div.v-line").css( {display:"none"} );
			$("div.pr-text").css( {display:"block"} );
		}
		else{			$("div.h-line, div.v-line").css( {display:"block"} );
			$("div.pr-text").css( {display:"none"} );
			works_items.filter("." + this.getAttribute("id").charAt(0) + "-i").css( {display:"block"} );
		}
	}
}

// Trim string
function trim(str){
    charlist = ' \xA0';
    var re = new RegExp('^[' + charlist + ']+|[' + charlist + ']+$', 'g');
    return str.replace(re, '');
};

// Ask a question
var req_succ = function(r, obj){
	$('#status-request').html(r['msg']).fadeIn(333);
	setTimeout( function(){ $('#status-request').fadeOut(333) }, 3000 );
	$('#form').fadeOut( 555 ).remove();
}
var req_false = function(r){
	$('#status-request').html(r['msg']).fadeIn(333);
	setTimeout( function(){ $('#status-request').fadeOut(333) }, 3000 )
}
function sendRequest(){
	var name = document.getElementById('vcomm-name').value;
	var contact = document.getElementById('vcomm-cont').value;
	var comment = document.getElementById('vcomm-ctext').value;
	if( !trim(name) ){
		return	alert('Укажите, пожалуйста, как к  вам обращаться.')
	}
	if( !trim(contact) ){
		return	alert('Укажите свой контактный e-mail и/или телефон для связи')
	}
	if( !trim(comment) ){
		return 	alert('Ваши вопросы/комментарии?')
	}
	sendXHR( '/contacts/ask', 'name='+encodeURIComponent(name)+'&contact='+encodeURIComponent(contact)+'&comment='+encodeURIComponent(comment), req_succ, req_false, {} );
}

// *****************************************************************************
// ON READY
// *****************************************************************************

// WORKS BLOCKS
var works_items;

// IMGS PRELOAD
var img1 = new Image().src = "/images/bg-item.png";
var img2 = new Image().src = "/images/bg-tip.png";

$(document).ready(function(){
	$("a.from").bind("click", noFollow);

	// WORKS ITEMS
	works_items = $("div.w-item");
	works_items.hover( function(){		if( $(this).hasClass("wpcpr-i") ){
			this.style.backgroundPosition = "-12px -36px";
		}
		else if ( $(this).hasClass("w-i") ){
			this.style.backgroundPosition = "-12px 0px";
		}
		else if( $(this).hasClass("p-i") ){			this.style.backgroundPosition = "-12px -12px";
		}
		else if( $(this).hasClass("c-i") ){        	this.style.backgroundPosition = "-12px -24px";
		}

		// CREATING TIP
		this.innerHTML = '<div class="tip"><div class="t-content">' + $('#i'+this.getAttribute('nid')).html() +
			'</div><div class="t-sh t-tl"></div><div class="t-sh t-tr"></div><div class="t-sh t-bl"></div><div class="t-sh t-br"></div></div>';

		var top = 0;
		var obj = this;
		while(obj){			top += obj.offsetTop;
			obj = obj.offsetParent;
			if( obj.className == "w-canvas" ) break;
		}

		if( this.firstChild.offsetHeight > h - top ){			this.firstChild.style.marginTop = ( 4 - this.firstChild.offsetHeight ) + 'px';
		}
		this.firstChild.style.visibility = "visible";
	}, function(){		if( $(this).hasClass("wpcpr-i") ){			this.style.backgroundPosition = "0px -36px";
		}
		else if( $(this).hasClass("w-i") ){
			this.style.backgroundPosition = "0px 0px";
		}
		else if( $(this).hasClass("p-i") ){
			this.style.backgroundPosition = "0px -12px";
		}
		else if( $(this).hasClass("c-i") ){
        	this.style.backgroundPosition = "0px -24px";
		}
		else{
		}
		// DELETING TIP
		this.innerHTML = "";
	} );

	// WORKS AXIS VAL TITLES
	$("table.work-table td").hover( function(){		this.style.background = "url('./images/bg-item.png')";

		var top = 0;
		var obj = this;
		while(obj){
			top += obj.offsetTop;
			obj = obj.offsetParent;
			if( obj.className == "w-canvas" ) break;
		}
		$("#val-ttl-y").css( {top: top + Math.round( h_dy / 2 ) - 5 + 'px'} ).text(this.getAttribute("kr"));

		var left = 0;
		var obj = this;
		while(obj){
			left += obj.offsetLeft;
			obj = obj.offsetParent;
			if( obj.className == "w-canvas" ) break;
		}
		$("#val-ttl-x").text(this.getAttribute("kc"));
		var w_ttl = $("#val-ttl-x").width();
		$("#val-ttl-x").css( {left: left + Math.round( (v_dx - w_ttl) / 2 ) + 'px'} )

	}, function(){		this.style.background = "none";
		$("#val-ttl-y").text('');
		$("#val-ttl-x").text('');	});

	// MYTH
	$("#myth").click( function(){		$("div.tip").css({display:"block"});	} );
	$("#myth_close").click( function(){
		$("div.tip").css({display:"none"});
	} );
});
