// scripts-common.js for EES 2.3




// FadeIn by clicking
function a_def(){
	$('#black').fadeIn(333);
}

// 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 }
})()

// Creating button and bind onclick-event to parent form's submit-action
function createButton(a){
	var btn = $(this).get(0)
	var name = btn.innerHTML
	btn.innerHTML = "<div class='button-right'><div class='button-center'><span>"+name+"</span></div></div>"
	btn.className = 'button-left'
	btn.style.cursor = 'pointer'
	btn.style.width = btn.firstChild.firstChild.firstChild.offsetWidth + 24 + 'px'

	if( ! btn.firstChild.firstChild.firstChild.offsetWidth ) btn.style.width = '80px'

	btn.onmouseover = function(){
		btn.style.backgroundPosition = '0px -84px'
		btn.firstChild.style.backgroundPosition = '100% -140px'
		btn.firstChild.firstChild.style.backgroundPosition = '0px -112px'
	}
	btn.onmouseout = function(){
		btn.style.backgroundPosition = '0px 0px'
		btn.firstChild.style.backgroundPosition = '100% -56px'
		btn.firstChild.firstChild.style.backgroundPosition = '0px -28px'
	}

	var parent = btn.parentNode
	for( ; ; ){
		if( parent.tagName == 'FORM' ){
			btn.onclick = function(p){
				return function(){
					$('#black').fadeIn(333, function(){p.submit()});
				}
			}(parent)
			break
		}
		parent = parent.parentNode
	}
}


// Get overall height
function getHeight(){
	return (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
}

// Trim string
function trim(str){
    charlist = ' \s\xA0';
    var re = new RegExp('^[' + charlist + ']+|[' + charlist + ']+$', 'g');
    return str.replace(re, '');
}

// SetCookies
function setCookie( c_name, value, expiredays, path, domain ){
	var expdate = new Date();
	expdate.setDate(expdate.getDate()+expiredays);
	document.cookie = c_name + "=" + escape(value) +
		((expiredays==null) ? "" : ";expires=" + expdate.toGMTString()) +
		((path) ? ";path=" + path : "") +
        ((domain) ? ";domain=" + domain : "");
}

// E-mail link
function hrefEmail(a1, a2){
	document.location.href = 'mailto:' + a1 + '@' + a2;
	if (a1 == 'artisan-shafran') {
		_gaq.push(['_trackEvent', 'Click', 'E-mail', 'Bottom']);
		yaCounter11867065.reachGoal('mailartisanshafran');
	}
	if (a1 == 'shafranovs') {
		_gaq.push(['_trackEvent', 'Click', 'E-mail', 'Top']);
		yaCounter11867065.reachGoal('maishafranovs');
	}
}




//******************************************************************************
// Executable functions
//******************************************************************************

// ------------------
// creating Accordion
// ------------------
function createAccordion(num){
	var div_st0 = $("div.st0");
	div_st0.hover( function(){ m_1st_over(this) },function(){ m_1st_out(this) } );

	var div_st0_link = $('div.st0-link');
	div_st0_link.hover( function(){ m_1st_over(this) },function(){ m_1st_out(this) } );
	div_st0_link.filter('.active').removeClass('active').each( function(){ m_1st_over(this) } ).unbind();

	div_st0.click( function(){
		closeActiveItem();
		openActiveItem(this);
	} );

	var f = div_st0.filter('[index='+num+']')
	if( f.length ){
		f.mouseover().click();
	}
	else if( num !== false ){
		div_st0.eq(num).mouseover().click();
	}
}
// Open Active Item
function openActiveItem(el){
	var div_this = $(el).not('.active');
	div_this.mouseover().addClass('active');

	var div_block_to_open = div_this.next();
	var h_div_block_to_open = div_block_to_open.height();
	div_block_to_open.css( {display: 'block', height: '0px', overflow: 'hidden'} )
	.animate( {height: h_div_block_to_open+'px'}, { duration: 555, queue: false, easing: 'swing', complete: function(){
		$(this).css( {height: 'auto', overflow: 'auto'} );
	} } )
}
// Close Active Item
function closeActiveItem(){
	var div_this = $('div.active').not(':animated');
	div_this.each( function(){m_1st_out(this, true)} );
	var div_block_to_close = div_this.next();
	div_block_to_close.animate( {height: '0px'}, { duration: 555, queue: false, easing: 'swing', complete: function(){
		$(this).css( {display: 'none', height: 'auto', overflow: 'hidden'} ).prev().removeClass('active');
	} } )
}
// On Nouse Over Item
function m_1st_over(el){
	if( !$(el).hasClass('active') ){
		el.style.color = '#cdaf89';
		el.style.backgroundPosition = '-5px -41px';
       	el.firstChild.style.backgroundPosition = '100% -202px';
       	el.firstChild.firstChild.style.backgroundPosition = '0px -120px';
       	el.firstChild.firstChild.firstChild.style.backgroundPosition = '-12px -21px';
	}
	else{
		el.firstChild.firstChild.firstChild.style.backgroundPosition = '-12px -51px';
	}
}
// On Nouse Out Item
function m_1st_out(el, force){
	if( ( el && el.style && !$(el).hasClass('active') ) ||
		( el && el.style && force) ){
		el.style.color = '#dbdada';
		el.style.backgroundPosition = '-5px 1px';
       	el.firstChild.style.backgroundPosition = '100% -160px';
       	el.firstChild.firstChild.style.backgroundPosition = '0px -78px';
       	el.firstChild.firstChild.firstChild.style.backgroundPosition = '-12px -109px';
	}
	else if( ( el && el.style && $(el).hasClass('active') ) ){
		el.firstChild.firstChild.firstChild.style.backgroundPosition = '-12px -51px';
	}
}

// ----------------------------
// creating subAccordion's Menu
// ----------------------------
function createSubMenu(ids){
	$('div.st1').hover( function(){m_2st_over(this,'st1')},function(){m_2st_out(this,'st1')} ).click( function(){ closeActive2stItem('st1'); openActive2stItem(this,'st1'); } );
	$('div.st2').hover( function(){m_2st_over(this,'st2')},function(){m_2st_out(this,'st2')} ).click( function(){ closeActive2stItem('st2'); openActive2stItem(this,'st2'); } );
	$('div.st3').hover( function(){m_2st_over(this,'st3')},function(){m_2st_out(this,'st3')} ).click( function(){ closeActive2stItem('st3'); openActive2stItem(this,'st3'); } );
	$('div.st4').hover( function(){m_2st_over(this,'st4')},function(){m_2st_out(this,'st4')} ).click( function(){ closeActive2stItem('st4'); openActive2stItem(this,'st4'); } );

	for( k in ids ){
		openActive2stItem(document.getElementById('st'+ids[k]),'st'+k);
	}
}
// Open Active Item
function openActive2stItem(el,cN){
	var div_this = $(el).not('.active'+cN);
	div_this.addClass('active'+cN).mouseover();

	var div_block_to_open = div_this.next();
	var h_div_block_to_open = div_block_to_open.innerHeight();
	div_block_to_open.css( {display: 'block', height: '0px', overflow: 'hidden'} )
	.animate( {height: h_div_block_to_open+'px'}, { duration: 555, queue: false, easing: 'swing', complete: function(){
		$(this).css( {height: 'auto', overflow: 'auto'} );
	} } )
}
// Close Active Item
function closeActive2stItem(cN){
	var div_this = $('div.active'+cN).not(':animated');
	div_this.each( function(){m_2st_out(this,cN,true)} );
	var div_block_to_close = div_this.next();
	div_block_to_close.animate( {height: '0px'}, { duration: 555, queue: false, easing: 'swing', complete: function(){
		$(this).css( {display: 'none', height: 'auto', overflow: 'hidden'} ).prev().removeClass('active'+cN);
	} } )
}
// On Nouse Over Item
function m_2st_over(el,cN){
	if( !$(el).hasClass('active'+cN) ){
		el.style.color = '#cdaf89';
       	el.firstChild.style.backgroundPosition = '-12px -21px';
	}
	else{
		el.style.color = '#cdaf89';
		el.firstChild.style.backgroundPosition = '-12px -51px';
	}
}
// On Nouse Out Item
function m_2st_out(el,cN,force){
	if( ( el && el.style && !$(el).hasClass('active'+cN) ) ||
		( el && el.style && force) ){
		el.style.color = '#dbdada';
       	el.firstChild.style.backgroundPosition = '-12px -109px';
	}
	else if( ( el && el.style && $(el).hasClass('active'+cN) ) ){
		el.firstChild.style.backgroundPosition = '-12px -51px';
	}
}

// ---------------------------
// open|close comments' column
// ---------------------------
function chComments(){
	var td = $('td.right-col');
	var header = $('#comments-h-l');
	var body = $('#comments-b-layer');

	if( +td.width() > 100 ){
    	header.animate( {marginLeft: '334px', paddingLeft: '13px'},{duration: 444, queue: false, complete: onCommentsHide} );
    	if(jQuery.browser.msie) body.css({display: 'none'})
    	else body.fadeOut(444)
	}
	else{
    	td.width( 391+'px' );
    	header.css( {marginLeft:'334px'} ).animate( {marginLeft: '0px', paddingLeft: '334px'},{duration: 444, queue: false, complete: onCommentsShow} );
    	if(jQuery.browser.msie) body.css({display: 'block'})
    	else body.fadeIn(444)
	}
}

// -------------------------------
// Prev & Next in pagination block
// -------------------------------
function doPrev(){
	var _this = $(this)
	var _jr = _this.next().children().children()
	var left = parseInt(_jr.css('margin-left'));
	if(left + 240 <= 0){
		var full_shift = left + 240;
		_jr.animate({marginLeft:full_shift+'px'},111);
	}
}
function doNext(){
	var _this = $(this)
	var _jr = _this.prev().children().children()
	var w = +_jr.width();
	var left = parseInt(_jr.css('margin-left'));
	if( left - 240 > -w ){
		var full_shift = left - 240;
		_jr.animate({marginLeft:full_shift+'px'},111);
	}
}

// Banner slideshow
function slideSwitch() {
    var $active = $('#banner-slides DIV.active_slide');

    if ( $active.length == 0 ) $active = $('#banner-slides DIV:last');

    // use this to pull the divs in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#banner-slides DIV:first');

    // uncomment below to pull the divs randomly
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active_slide')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active_slide last-active');
        });
}
