//for link opened in new tab
function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
for (var i=0; i < anchors.length; i++) {
	if (anchors[i].getAttribute("href") &&

	anchors[i].getAttribute("rel") == "external") {
		anchors[i].target = "_blank";
	}
}
}

function displayer(id){
    var el = document.getElementById(id);
        if(el.style.display != 'block'){
            el.style.display = 'block';
            }
        else{
            el.style.display = 'none';
            }
    }


function link(){
	var link = document.getElementsByTagName('a');
	for (var i = 0; i < link.length; i++){
		link[i].onmouseover	= "this.style.borderColor = 'red'"
	}
}


//Ищет елементы по классу
function elClass(tag, cl){
	var t = document.getElementsByTagName(tag);
	var ob = new Array;
	ob[0] = false;
	var n = 0;
	for(var i=0; i<t.length; i++){
		var cA = t[i].className.split(' ');
		for(var j=0; j<cA.length; j++){
			if(cA[j]==cl){
				ob[n]=t[i];
				n++;
				break;
			}
		}
		//if(ob)break;
	}
	if(ob[0])return ob;	
	else return false;
}

//проверяем, поддерживает ли браузер интерфейс «Element Traversal»
var traversal = typeof document.createElement('div').childElementCount != 'undefined';
//Функция ищет первый дочерний элемент
var firstChild = traversal ? function(node) {
    	return node.firstElementChild;
	} : function(node) {
    	node = node.firstChild;
    	while(node && node.nodeType != 1) node = node.nextSibling;
    	return node;
	};

function imagesGorCenter(){
//var images = elClass('div', 'competitionPhoto');
//	if(images){
//		for(var i=0; i<images.length; i++){
//			firstChild(images[i]).style.marginTop = '-' + firstChild(images[i]).clientHeight/2 + 'px';
//		}
//	}
var a = '';
}


function parent( elem, num ) {
       num = num || 1;
       for ( var i = 0; i < num; i++ )
               if ( elem != null ) elem = elem.parentNode;
       return elem;
}

function addClass(ob, class1){
	var oldClass = ob.className;
	var newClass = oldClass.split(' ');
	var testClass = true;
	for(var i=0; i<newClass.length; i++){
		if(newClass[i] == class1){
			testClass = false;
			break;
		}
	}
	if(testClass){
		newClass.push(class1);
		ob.className = newClass.join(' ');
	}
	//var newClass = oldClass.split(' ');
}




function removeClass(ob, class1){
	var oldClass = ob.className.split(' ');
	for(var i=0; i<oldClass.length; i++){
		if(oldClass[i]==class1) delete oldClass[i];
		}
	oldClass.sort();
	for(var i=0;i<oldClass.length;i++)if(oldClass[i]===undefined)oldClass.length=i;
	ob.className = oldClass.join(' ');
}

function linkerClick(){
	var linkP = elClass('div', 'rate');
		for(var j=0; j<linkP.length; j++){
			var a = linkP[j].getElementsByTagName('a');
				for(var i=0; i<a.length; i++){
					a[i].onmouseover= function(){
						var n = this.getAttribute('rel');
						for(var aI=0; aI<5; aI++){
							var par = parent(this).getElementsByTagName('a');
							//alert(par);
							//par[aI].className = 'star starActive';
							if(aI<n){
								 addClass(par[aI], 'starHover');
								//.className = 'star ';
								}
							else{
								addClass(par[aI], 'starHoverN');
								}
							}
					}
					
					a[i].onmouseout= function(){
						for(var aI=0; aI<5; aI++){
							var par = parent(this).getElementsByTagName('a');
								removeClass(par[aI], 'starHover');
								removeClass(par[aI], 'starHoverN');
						}
					}
					
//					a[i].onclick= function(){
//						var n = this.getAttribute('rel');
//						for(var aI=0; aI<5; aI++){
//							var par = parent(this).getElementsByTagName('a');
//							//alert(par);
//							//par[aI].className = 'star starActive';
//							if(aI<n){
//								 addClass(par[aI], 'starActive');
//								//.className = 'star ';
//								}
//							else{
//								removeClass(par[aI], 'starActive');
//								}
//							}
//							return false;
//						}
						
				}
		}
}

var m = 0;

function popapClose(){
	var sel = document.getElementsByTagName('select');
	for(var i=0; i<sel.length; i++){
		sel[i].style.display ='inline';
	}
	var popap = elClass('div', 'popap')[0];
	var pic = popap.getElementsByTagName('div')[0].getElementsByTagName('img')[0];
	popap.style.display = 'none';
	elClass('div', 'shadow')[0].style.display = 'none';
	
	pic.style.position = 'absolute';
	pic.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=0)';
	pic.style.opacity = '0';
}

function popapOpen(th){
	src =th.getAttribute('rel');
	var text = th.parentNode.getElementsByTagName('span')[0].innerHTML
	var sel = document.getElementsByTagName('select');
	for(var i=0; i<sel.length; i++){
		sel[i].style.display ='none';
	}
	var topX = document.documentElement.scrollTop + 100;
	var popap = elClass('div', 'popap')[0];
	var pic = popap.getElementsByTagName('div')[0].getElementsByTagName('img')[0];
	popap.style.display = 'block';
	popap.style.top = topX + 'px';
	elClass('div', 'shadow')[0].style.display = 'block';
	pic.src=src;
	popap.getElementsByTagName('div')[1].innerHTML = text;
}


function popapWidth(){
	var popap = elClass('div', 'popap')[0];
	var pic = popap.getElementsByTagName('div')[0].getElementsByTagName('img')[0];
	pic.onload = function(){
		var w = (window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth));
		m = this.width;
		var style = this.style;
		style.position = 'static';
		style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=100)';
		style.opacity = '1';
		popap.style.width = (m+40)+'px';
		if(w>m+20){
			elClass('div', 'popap')[0].style.marginLeft = '-'+m/2+'px';
			elClass('div', 'popap')[0].style.left = '50%';
		}
		else{
			elClass('div', 'popap')[0].style.marginLeft = '0px';
			elClass('div', 'popap')[0].style.left = '10px';
		}
	}
	var pC = elClass('div', 'competitionPhoto');
	for(var i=0; i<pC.length; i++){
		pC[i].getElementsByTagName('img')[0].onclick=function(){
			popapOpen(this);
		}
	}
	
	elClass('img', 'popapClose')[0].onclick=function(){
		popapClose();	
	}
}

function flashClose(){
	//alert('test');
	document.getElementById('flashPopap').style.display='none';	
}

//===================================================================================================

window.onresize = function(){
	var w = (window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth));
	if(w>m+20){
			elClass('div', 'popap')[0].style.marginLeft = '-'+m/2+'px';
			elClass('div', 'popap')[0].style.left = '50%';
		}
		else{
			elClass('div', 'popap')[0].style.marginLeft = '0px';
			elClass('div', 'popap')[0].style.left = '10px';
		}
}



//===================================================================================================

window.onload = function(){

	$('.competitionPhoto').hover(
	
	function(e){
			var pageY = e.pageY;
			var pageX = e.pageX+120;
			$(this).nextAll(".blockMidPopap").removeClass("none");
			$(this).parent().css('zIndex', '200');
			
			//$(this).nextAll(".blockMidPopap").css('top', pageY + 'px');
			//$(this).nextAll(".blockMidPopap").css('left', pageX + 'px');
			
		return false;
	}, 
	
	function(e){
			
			$(this).parent().css('zIndex', '10');
			$(this).nextAll(".blockMidPopap").addClass("none");
			
		return false;
	}
	);
	
	$("body").mousemove(function(e){
		
		$('.curPosition').html(e.pageX + ' ' + e.pageY)
		
	})

}





















