//window.onerror = function(){ return true;}
function addOnLoadEvent() {

	var root = window.addEventListener || window.attachEvent ? window : document.addEventListener ? document : null;
	if (root) {
		for (var i = 0; i < arguments.length; i++) {
			if ('function'  == typeof(arguments[i]))
				addHandler(root,'load',arguments[i]);
		}
	}
}

addOnLoadEvent(initCounter);

function initCounter() {	reloadCounter();}function reloadCounter() {	$.get('/counter.php');}

function addHandler(object, event, handler) {
  if (typeof (object.addEventListener) != 'undefined')
    object.addEventListener(event, handler, false);
  else if (typeof (object.attachEvent) != 'undefined')
    object.attachEvent('on' + event, handler);
  else {
    var handlersProp = '_handlerStack_' + event;
    var eventProp = 'on' + event;
    if (typeof (object[handlersProp]) == 'undefined') {
      object[handlersProp] = [];
      if (typeof (object[eventProp]) != 'undefined')
        object[handlersProp].push(object[eventProp]);
      object[eventProp] = function(e) {
        var ret = true;
        for (var i = 0; ret != false && i < object[handlersProp].length; i++)
          ret = object[handlersProp][i](e);
        return ret;
      }
    }
    object[handlersProp].push(handler);
  }
}
// $(function(){
	// $('span.menu').hover(function(){
		// $('ul.submenu', $(this).parent()).slideDown();
		// },
		// function() {
			// time = window.setTimeout(function(){
			// $('ul.submenu',$(this).parent()).slideUp();
			// }, 100);
			// })
	// $('ul.submenu',$(this).parent()).hover(function(){
		// window.clearTimeout(time);
	// },
		// function() {
			// $('ul.submenu').slideUp();
		// }
	// )
// })
var ShowPics = new Array;
/* ShowPic */
function initShowPic() {
        if ('function' == typeof(ShowPic)) {
                var imgs = document.getElementsByTagName('img');
                if (imgs.length) {
                        for (var i = 0; i < imgs.length; i++) {
                                var obj = imgs[i];
                                if (obj.className.indexOf('sp') != -1) {
									obj.style.cursor = 'pointer';
									ShowPics.push(obj);
									obj.onclick = function() { ShowPic(this); return false;}
                                }
                        }
                        createShowPic();
                }
        }
}
addOnLoadEvent(initShowPic);

var ClickToClose = "Закрыть";
var NextHTML =  '&raquo;';//"<img src='/pict/sp/right16.gif' align='absmiddle'>";
var PrevHTML = '&laquo;';// "<img src='/pict/sp/left16.gif' align='absmiddle'>";

function isPattern(pattern, str) {
	if(str.length && pattern.length) {
		var re = new RegExp(pattern, "g");
		return re.test(str);
	}
	return false;
}

function isEmail(str) {
	return isPattern("^([a-z0-9_-]+)(\\.[a-z0-9_-]+)*@((([a-z0-9-]+\\.)+(com|net|org|mil|edu|gov|arpa|info|biz|inc|name|[a-z]{2}))|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))$", str.toLowerCase());
}


 function subscribs(obj){
    var email = document.getElementById('email');
	if ( email.value == '' || !isEmail(email.value)){
		alert('Необходимо ввести правильный email');
		return false;
	}else {
        document.getElementById('podpiska').submit();
	}
}
addOnLoadEvent(showdescr, Tabs, leftMenu, initAnimation, sortBy, tableRows, iBlock, uTabs);
//var kol = 0;
function showdescr() {
	$('.refimg').toggle(function() {
		//$('.descr').hide();
		//$('.refimg').attr('src', '/pict/down.gif');
		$('.descr', $(this).parent().parent()).slideDown('slow');
		//$('.descr', $(this).parent().parent()).attr('display', 'yes');
		$(this).attr('src', '/pict/up.gif');
	}, function() {
		$('.descr', $(this).parent().parent()).slideUp('slow');
		$(this).attr('src', '/pict/down.gif');
	});
}

function Tabs() {
    $('#container-1').tabs();
}
function leftMenu() {
	$('.leftmenu').click(function() {
		for(var i =1; i<=3; i++) {
			$('#left_' + i).slideUp('slow');
		}
		id = $(this).attr('id');
		if($('#left_' + id).css('display') == 'none') {
			$('#left_' + id).slideDown('slow');
		}else {
			$('#left_' + id).slideUp('slow');
		}
	});
}

function initAnimation() {
    for (var i = 1; i <= 10; i++) {
        var obj = document.getElementById('rec_' + i);
        if (obj) {
            if (obj.getAttribute('pict_2')) {
                window.setTimeout('window.setInterval("doAnimation('+ i + ')",3000)',1000 * (i * 1.1));
            }
        }
    }
}

function doAnimation(i) {
   var obj = document.getElementById('rec_' + i);
   if (!obj.getAttribute('next')) {
        var next = 2;
   } else {
        var next = obj.getAttribute('next');
   }
   obj.src = obj.getAttribute('pict_' + next);

   next++
   if (!obj.getAttribute('pict_' + next)) {
    next = 1;
   }
   obj.setAttribute('next',next);
}
	//  меню на главной  'сортировать по'//
time = null;
show = null;
function sortBy() {
	$('#sortby').hover(function() {
		if(time) {
			window.clearTimeout(time);
		}
		//slide = true;
		show = window.setTimeout('shw()', 550);
	}, function() {
		window.clearTimeout(show);
		if($('#sortby').attr('sh') == 1) {
			time = window.setTimeout("hdw()", 550);
		}
	});
	$('div#sortby_submenu').hover(function() {
		window.clearTimeout(time);
	}, function() {
		time = window.setTimeout("hdw()", 750);
	});
}
function shw() {
	$('#sortby_submenu').slideDown('slow');
	$('#sortby').attr('sh', 1);
}
function hdw() {
	$('#sortby_submenu').slideUp('slow');
	$('#sortby').attr('sh', 0);
}

function tableRows() {
	$('.table_hover').hover(function() {
		$(this).css('background', '#B6EFB3');
	}, function(){
		var bg = $(this).attr('bgcolor');
		$(this).css('background', bg);
	});
}

function iBlock () {
	$('.div_iBlock').click(function() {
		document.location.href = $(this).attr('_href');
	}).mouseover(function() {
		$(this).attr('class', 'div_iBlock_over');
		$('img#img_iBlock').attr('src', $(this).attr('_src'));
		$('a#a_iBlock').attr('href', $(this).attr('_href'));
	}).mouseout(function() {
		$(this).attr('class', 'div_iBlock');
	});
}

function uTabs() {
	$('div#uTabContain').html($('div#uTab_'+$('div#uTabContain').attr('default')).html());
	$('.uTabHeader').mouseover(function() {
		__setutabs($(this).attr('_id'));
		$('div#uTabContain').html($('div#uTab_'+$(this).attr('_id')).html());
	}).click(function() {
		document.location.href = $(this).attr('_href');
	});
}

function __setutabs(_cur) {
	$('.uTabHeader').each(function() {
		if ($(this).attr('_id') == _cur) {
			$(this).attr('class', 'uTabHeader '+$(this).attr('_overclass'));
		} else {
			$(this).attr('class', 'uTabHeader '+$(this).attr('_outclass'));
		}
	});
}

function OpenComments(path) {
	var _w = 500;
	var _h = 370;
	var xpos = (screen.width-_w)/2;
	var ypos = (screen.height-_h)/2;
	qqq = window.open(path,'displayWindow','fullscreen=no,channelmode=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width='+_w+',height='+_h+',top='+ypos+',left='+xpos);
	qqq.focus();
	return false;
}

$(document).ready(function() {
	$('code').each(function(){
		$(this).html($.trim($(this).html()));
	});
	$('a.lightbox').lightbox({
		fileLoadingImage: '/js/lightbox/images/loading.gif',
		fileBottomNavCloseImage: '/js/lightbox/images/closelabel.gif'
	});
});
