function cmnRemove_class( eOn, sClass_name ){
	cmnSet_class( eOn, "", sClass_name );
}

function cmnSet_class( eOn, sClass_name, sInstead ){
	if( eOn ){
		sClass_name = ( sClass_name.length ) ? sClass_name.replace( /(^\s+|\s+$)/, "" ) : "";
		if( eOn.className.length ){
			var sOld = sClass_name;
			if( sInstead && sInstead.length ){
				sInstead = sInstead.replace( /\s+(\S)/g, "|$1" );
				if( sOld ){
					sOld += "|";
				}
				sOld += sInstead;
			}
			eOn.className = eOn.className.replace( new RegExp("(^|\\s+)(" + sOld +")($|\\s+)", "g"), "$1" );
		}
		eOn.className += ( eOn.className.length && sClass_name ? " " : "" ) + sClass_name;
	}
}

function cmnMatch_class( eOn, sClass_name ){
	return ( sClass_name && eOn.className && eOn.className.length && eOn.className.match( new RegExp("(^|\\s+)(" + sClass_name +")($|\\s+)") ) );
}

function getAbsoluteCoords(oElement){
	var oResult = {
		iTop  : 0,
		iLeft : 0
		};
	while(oElement) {
		oResult.iTop += oElement.offsetTop;
		oResult.iLeft += oElement.offsetLeft;
		oElement = oElement.offsetParent;
	}
	return oResult;
}
/*
function CheckClassToParent (oElem, sClass){
	while(oElem) {
		if(cmnMatch_class( oElem, sClass ))
			return true;
		oElem = oElem.offsetParent;
	}
	return false;
}
*/
function d(sId) {
	return document.getElementById(sId);
}

function RandomNum(iIntNum1, iIntNum2) {
	return parseInt(iIntNum1 + (iIntNum2 - iIntNum1) * Math.random());
}

function ComeInInterval(iNum, iIntNum1, iIntNum2) {
	if((iNum >= iIntNum1) && (iNum <= iIntNum2)) {
		return true;
	}
	else{
		return false;
	}
}


function ZoomPict(oElem) {
     var iBodyWidth = document.getElementById("measurer").clientWidth;
     var iBodyHeight = document.getElementById("layout").clientHeight;
     var oZoomWindow = document.getElementById("zoom_wibdow");
     var oZoomWindowBg = document.getElementById("zoom_wibdow_bg");
     var oZoomWindowCnt = document.getElementById("zoom_wibdow_content");
     
     var isGecko = !document.attachEvent && document.addEventListener || self.opera;
     var iScroll = (isGecko) ? pageYOffset : document.documentElement.scrollTop ;
     var iInnerHeight = window.innerHeight ? window.innerHeight : document.documentElement.offsetHeight;
     
     if(iBodyHeight < 732){
          iBodyHeight = 732;
          document.getElementById("layout").style.height = '732px';
          document.getElementById("footer").style.top = '642px';
     }
     
     var iContHeight = ( iInnerHeight < 732 ) ? ( ( (iScroll + 732) < iBodyHeight ) ? iScroll : ( iBodyHeight - 732 ) ) : ( ( iInnerHeight / 2 ) - ( 732 / 2 ) + iScroll );
     var oElemNum = parseInt(oElem.id.substr(17));
     
     var oImg = document.getElementById("zoom_" + oElemNum);
     var iWidth = parseInt( $(oImg).attr("width") )
     var iHeight = parseInt( $(oImg).attr("height") );
     //alert();
     
     $(oZoomWindow).css("left", 0);
     $(oZoomWindow).css("top", 0);
     
     $(oZoomWindowCnt).css("left", ( ( iBodyWidth / 2 ) - ( 732 / 2 ) ) );
     $(oZoomWindowCnt).css("top", iContHeight);
     
     $(oZoomWindow).css("width", "100%");
     $(oZoomWindow).css("height", iBodyHeight);
     $(oZoomWindowBg).css("width", "100%");
     $(oZoomWindowBg).css("height", iBodyHeight);
     $(oZoomWindow).css("zIndex", "5000");
     $(oZoomWindow).css("visibility", "hidden");
     cmnSet_class( oZoomWindow, " ", "hidden" );
     
     ZoomImg(oElemNum);
     
}


function ZoomImg(iNum) {
     var oImg = document.getElementById("zoom_" + iNum);
     $("#biege_round img").css("display", "none");
     $("#delete img").css("display", "block");
     $(oImg).css("display", "block");
     var iWidth = parseInt(oImg.getAttribute("width"));
     var iHeight = parseInt(oImg.getAttribute("height"));
     
     var iElemTop = (716 - parseInt( document.getElementById("biege_round").clientHeight )) / 2;
     var iElemLeft = (716 - parseInt( document.getElementById("biege_round").clientWidth )) / 2;
     
     $("#biege_round").css( "left", iElemLeft );
     $("#biege_round").css( "top", iElemTop );
     
     document.getElementById("zoom_wibdow").style.visibility = "visible";
}

function PrevZoomImg() {
     var oContainer = document.getElementById("biege_round_images");
     var oCurrPict = "";
     var arrPicts = new Array();
     var k = 0;
     for (var i=0; i < oContainer.childNodes.length; i++) {
          if (oContainer.childNodes[i].nodeType == 1) {
               if($(oContainer.childNodes[i]).css("display") == "block"){
                    oCurrPict = oContainer.childNodes[i]
               }
               arrPicts[k] = oContainer.childNodes[i];
               k++;
          }
     }
     var iNum = parseInt(oCurrPict.id.substr(5));
     if( (iNum - 1) < 1){
          var iLastNum = parseInt(arrPicts[(arrPicts.length - 1)].id.substr(5));
          ZoomImg( iLastNum );
     }
     else
          ZoomImg( (iNum - 1) );
}

function NextZoomImg() {
     var oContainer = document.getElementById("biege_round_images");
     var oCurrPict = "";
     for (var i=0; i < oContainer.childNodes.length; i++) {
          if (oContainer.childNodes[i].nodeType == 1) {
               if($(oContainer.childNodes[i]).css("display") == "block"){
                    oCurrPict = oContainer.childNodes[i]
               }
          }
     }
     var iNum = parseInt(oCurrPict.id.substr(5));
     var arrBlocks = new Array();
     var oContainer_2 = document.getElementById("random_blocks_container");
     var k = 0;
     for (var i=0; i < oContainer_2.childNodes.length; i++) {
          if (oContainer_2.childNodes[i].nodeType == 1) {
               arrBlocks[k] = oContainer_2.childNodes[i];
               k++;
          }
     }
     if( (iNum + 1) > arrBlocks.length)
          ZoomImg( 1 );
     else
          ZoomImg( (iNum + 1) );
}

function CloseZommBlock() {
	$("#biege_round img").css("display", "none");
	var oZoomWindow = document.getElementById("zoom_wibdow");
	cmnSet_class( oZoomWindow, "hidden", "hidden" );
}
/*
function cmnAdd_event( eOn, sEvent_type, ptrFunction ){
	if( eOn.addEventListener ){
		eOn.addEventListener( sEvent_type, ptrFunction, false );
	}else{
		if( !ptrFunction.$$guid ){ ptrFunction.$$guid = cmnAdd_event.guid++; }
		if( !eOn.events ){ eOn.events = {}; }
		var aHandler = eOn.events[sEvent_type];
		if( !aHandler ){
			aHandler = eOn.events[sEvent_type] = {};
			if( eOn['on' + sEvent_type] ){ aHandler[0] = eOn['on' + sEvent_type]; }
			eOn['on' + sEvent_type] = cmnHandle_event;
		}
		aHandler[ptrFunction.$$guid] = ptrFunction;
	}
}
cmnAdd_event.guid = 1;

function cmnRemove_event( eOn, sEvent_type, ptrFunction ){
	if( eOn.removeEventListener ){
		eOn.removeEventListener( sEvent_type, ptrFunction, false );
	}else if( eOn.events && eOn.events[sEvent_type] && ptrFunction.$$guid ){
		delete eOn.events[sEvent_type][ptrFunction.$$guid];
	}
}

function cmnHandle_event( event ){
	event = event || cmnFixe_event( window.event );
	var returnValue = true;
	var aHandler = this.events[event.type];
	for( var i in aHandler ){
		if( !Object.prototype[i] ){
			this.$$ptrFunction = aHandler[i];
			if( this.$$ptrFunction(event) === false ){ returnValue = false; }
		}
	}
	if( this.$$ptrFunction ){ this.$$ptrFunction = null; }

	return returnValue;
}
//cmnAdd_event( window, "resize", SetNewsBlockHeight );
//cmnAdd_event( document.getElementById("framed_content_container"), "resize", SetNewsBlockHeight );
cmnAdd_event( window, "load", SetNewsBlockHeight );
//window.onload = function () {
	//SetNewsBlockHeight();
//}
*/

function SetNewsBlockHeight() {
	if(!$.browser.safari)
		return;
	var oImg = document.getElementById("framed_bg");
	var oContent = document.getElementById("framed_content_container");
	
	oImg.style.height = oContent.offsetHeight + "px";
}

function GoToUrl(sUrl){
	document.location = sUrl;
}

function CloseZoomWindow(e) {

	var oZoomWindow = document.getElementById("zoom_wibdow");
	if(!oZoomWindow){
		//alert(-2);
		return;
	}
	var isGecko = !document.attachEvent && document.addEventListener || self.opera;
	var oTarget = isGecko ? e.target : event.srcElement;
	if(oTarget.tagName.toLowerCase() == "html"){
		//alert(-1);
		return;
	}
	if(cmnMatch_class(oZoomWindow, "hidden")){
		if($(oZoomWindow).css("visibility") == "hidden"){
			//alert(0);
			return;
		}
	}
	else{
		while(oTarget) {
			if(oTarget.className.substr(0, 11) == "biege_round"){
				//alert(oTarget.id);
				return;
			}
			else{
				if(oTarget.id == "zoom_left_switch"){
					//alert(oTarget.id);
					return;
				}
				else{
					if(oTarget.id == "zoom_right_switch"){
						//alert(oTarget.id);
						return;
					}
				}
			}
			oTarget = oTarget.offsetParent;			
		}
		CloseZommBlock();
	}
}

function CloseCommentWindow(e) {
     var oCommWindow = document.getElementById("add_comment_block");
     if(!oCommWindow)
          return;
     var isGecko = !document.attachEvent && document.addEventListener || self.opera;
     var oTarget = isGecko ? e.target : event.srcElement;
     if(cmnMatch_class(oCommWindow, "hidden")){
          return;
     }
     else{
          while(oTarget) {
               if(oTarget.className.substr(0, 11) == "white_round" || oTarget.className.substr(0, 11) == "biege_round")
                    return;
               oTarget = oTarget.offsetParent;
          }
          HideCommentWindow();
     }
}

document.onmousedown = function(e) {
	CloseCommentWindow(e);
	CloseZoomWindow(e);
}

document.onkeydown = function(e) {
	SafariBlocksHeight();
	//if($.browser.safari) {
		//var event = window.event ? window.event : e;
		//alert(event.keyCode);
		//alert(document.getElementById('framed_content_container').clientHeight)
		//if(event.keyCode == 189 || event.keyCode == 187 || event.keyCode == 45 || event.keyCode == 61){
			
		//}
	//}
	
}

function SafariBlocksHeight(){
	if($.browser.safari){
		var k = 0;
		var arrElems = iElems = [];
		var iElems = document.getElementsByTagName("DIV");
		
		for(var i=0; i < iElems.length; i++){
			if( cmnMatch_class( iElems[i], "framed_bb_bg" ) ){
				arrElems[k] = iElems[i];//.clientHeight;
				k++;
			}
		}
		
		for(var z=0; z < arrElems.length; z++){
			for(var j=0; j < arrElems[z].childNodes.length; j++ ){
				if (arrElems[z].childNodes[j].nodeType == 1) {
					var iHeight = parseInt(arrElems[z].clientHeight);
					//alert(arrElems[z].childNodes[j].height);
					arrElems[z].childNodes[j].height = iHeight;// + "px";
			    }
			}
		}
	}
}

function Place(arrCoords, iLength, iNum, iPlaceWidth, iX, iY, iXRandStepLeft, iXRandStepRight, iYRandStepTop, iYRandStepBottom) {
	if (iNum == iLength)
		return true;
	
	for (var cnt = 0; cnt < 100; ++cnt) {
		arrCoords[iNum][0] += iX;
		arrCoords[iNum][1] += iX;
		arrCoords[iNum][2] += iY;
		arrCoords[iNum][3] += iY;
		var bad = false;
		
		var arrNextCoords = ((iNum + 1) >= (iLength - 1) ) ? false : arrCoords[(iNum + 1)] ;
		var iNextElemWidth = (arrNextCoords) ? arrNextCoords[1] : 0;
		var iRandomX = ( ((arrCoords[iNum][1] + iNextElemWidth + iXRandStepRight) > iPlaceWidth) && ((arrCoords[iNum][1] + iNextElemWidth) < iPlaceWidth) ) ? iPlaceWidth - (arrCoords[iNum][1] + iNextElemWidth) : iXRandStepRight;
		
		for (var j=0; j < iNum; ++j){
			var oCurrCoords = {
				x1: arrCoords[iNum][0],
				x2: arrCoords[iNum][1],
				y1: arrCoords[iNum][2],
				y2: arrCoords[iNum][3]
			}
			var oComparison = {
				x1: arrCoords[j][0],
				x2: arrCoords[j][1],
				y1: arrCoords[j][2],
				y2: arrCoords[j][3]
			}
			
			
			if (Intersect(oCurrCoords, oComparison)) {
				if( arrCoords[iNum][1] > iPlaceWidth ){
					
					arrCoords[iNum][0] -= iX;
					arrCoords[iNum][1] -= iX;
					arrCoords[iNum][2] -= iY;
					arrCoords[iNum][3] -= iY;
					
					iY = arrCoords[(iNum - 1)][3] + RandomNum( iYRandStepTop, iYRandStepBottom );
					iX = RandomNum( iXRandStepLeft, iXRandStepRight );
				}
				else{
					if( (arrCoords[j][3] - arrCoords[iNum][2]) < ( ( arrCoords[iNum][3] - arrCoords[iNum][2] ) / 2 ) ) {
						iX = 0;
						iY = 10;
					}
					else{						
						iX = 10;
						iY = 0;
					}
				}
				bad = true;
				break;
			}
			else{
				if( arrCoords[iNum][1] > iPlaceWidth ){
					
					arrCoords[iNum][0] -= iX;
					arrCoords[iNum][1] -= iX;
					arrCoords[iNum][2] -= iY;
					arrCoords[iNum][3] -= iY;
					
					iY = arrCoords[(iNum - 1)][3] + RandomNum( iYRandStepTop, iYRandStepBottom );
					iX = RandomNum( iXRandStepLeft, iXRandStepRight );
					
					bad = true;
					break;
				}
				else{
					iY = arrCoords[iNum][2] + RandomNum( iYRandStepTop, iYRandStepBottom );
					iX = arrCoords[iNum][1] + RandomNum( iXRandStepLeft, iXRandStepRight );
				}
			}
		}
		if (!bad){
			if (Place(arrCoords, iLength, (iNum + 1), iPlaceWidth, iX, iY, iXRandStepLeft, iXRandStepRight, iYRandStepTop, iYRandStepBottom))
				return true;
		}
	}
	return false;
}

function Intersect( a, b ){
 //return
  
  var aaa = (a.x1 >= b.x1 && a.x1 <= b.x2 || a.x2 >= b.x1 && a.x2 <= b.x2) && (a.y1 >= b.y1 && a.y1 <= b.y2 || a.y2 >= b.y1 && a.y2 <= b.y2) ||
  (b.x1 >= a.x1 && b.x1 <= a.x2 || b.x2 >= a.x1 && b.x2 <= a.x2) && (b.y1 >= a.y1 && b.y1 <= a.y2 || b.y2 >= a.y1 && b.y2 <= a.y2);
  
  return aaa;
}