function resize_window(){
	document.location.href = document.location.href;
}

function set_width($html, $offset){
	var $width = document.getElementById("sizer").offsetLeft;
	document.write($html.replace("%W", $width + $offset));
}


var $start_X_position 		= 0;
var $slider_start_position 	= 0;
var $left_border 			= 0;

function drag_menu($event){
	var $event = $event ? $event : window.event ;
	var $X_position = $event.pageX ? $event.pageX : $event.clientX + document.body.scrollLeft;
	var $offset = $start_X_position - $X_position;

	$POSITION = $slider_start_position - $offset;
	if ($POSITION <= $left_border){
		$POSITION = $left_border;
	}
	if ($POSITION >= $right_border){
		$POSITION = $right_border;
	}
	document.getElementById("slider").style.left = ($POSITION) + "px";

 	document.getElementById("content-line").style.marginLeft = ($POSITION - 206)*(-1*$koefficient)+"px";

	document.getElementById("tess").innerHTML = ($POSITION - 196)*(-1*$koefficient);

	if (window.event){window.event.cancelBubble = true;} else {$event.stopPropagation();}
	return false; 
}

function start_drag($event){
	$event 			= $event ? $event : window.event ;
	$slider_start_position 	= document.getElementById("slider").offsetLeft*1;
	$start_X_position 	= ($event.pageX) ? $event.pageX : $event.clientX + document.body.scrollLeft;
	$left_border		= document.getElementById("scroller").offsetLeft;
	$right_border		= document.getElementById("scroller").clientWidth + $left_border - document.getElementById("slider").clientWidth;
        //$left_border -= 21; // Dobavilsa treugolnik
        //$right_border += 25; // Dobavilsa treugolnik

	window.document.body.onmousemove 	= drag_menu;
	window.document.body.onmouseup 		= stop_drag;
	window.document.body.onmousdown 	= stop_drag;
	if (window.event){window.event.cancelBubble = true;} else {$event.stopPropagation();}
	return false;

}

//----------------------------------------------
var sliderMove = 0;

function drag_menu_move(moveSide){
	
	if(moveSide == 'right'){moveWidth = 2;}else{moveWidth = -2;}
	
	$left_border		= document.getElementById("scroller").offsetLeft;
	$right_border		= document.getElementById("scroller").clientWidth + $left_border - document.getElementById("slider").clientWidth;
	$slider				= document.getElementById("slider").offsetLeft;
        //$left_border -= 21; // Dobavilsa treugolnik
        //$right_border += 25; // Dobavilsa treugolnik
	
	$POSITION = $slider + moveWidth;

	if ($POSITION <= $left_border){
		$POSITION = $left_border;
	}
	if ($POSITION >= $right_border){
		$POSITION = $right_border;
	}
	
	document.getElementById("slider").style.left = ($POSITION) + "px";
	
 	document.getElementById("content-line").style.marginLeft = ($POSITION - 206)*(-1*$koefficient)+"px";

	document.getElementById("tess").innerHTML = ($POSITION - 196)*(-1*$koefficient);
	
	if(sliderMove){setTimeout("drag_menu_move('"+moveSide+"')", 50);}

	return false; 
}

function scroll_start(moveSide){
	sliderMove = 1;
	drag_menu_move(moveSide);
}

function scroll_stop(){
	sliderMove = 0;
}

//----------------------------------------------
function stop_drag($event){
	window.document.body.onmousemove 	= "";
	window.document.body.onmouseup 		= "";
	window.document.body.onmousdown 	= "";
	if (window.event){window.event.cancelBubble = true;} else {$event.stopPropagation();}
}

function move_lenta($offset){
	if (!document.getElementById("scroller")){
		return;
	}
	$left_border			= document.getElementById("scroller").offsetLeft;
	$right_border			= document.getElementById("scroller").clientWidth + $left_border - document.getElementById("slider").clientWidth;
        //$left_border -= 21; // Dobavilsa treugolnik
        //$right_border += 25; // Dobavilsa treugolnik


	$POSITION = document.getElementById("slider").style.left.replace("px", "") * 1  + $offset*-8;

	if ($POSITION <= $left_border){
		$POSITION = $left_border;
	}
	if ($POSITION >= $right_border){
		$POSITION = $right_border;
	}



	document.getElementById("slider").style.left = $POSITION;

	document.getElementById("content-line").style.marginLeft = ($POSITION - 206)*(-1*$koefficient)+"px";
}

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

$GTK_window = '';
$GTK_bg	= '';

function sendform($active_tree, $form_type){

	$GTK_bg = document.createElement("DIV");
	$GTK_bg.className = "GTK_prompt_bg";
	document.body.appendChild($GTK_bg);


	if ($form_type == 2){
		$width 	= 300;
		$height = 150;
	} else {
		$width 	= 450;
		$height = 270;
	}
	$GTK_window = document.createElement("DIV");
	$GTK_window.className = "GTK_prompt";

	$left 	= $GTK_bg.clientWidth/2 - $width/2;
	$top 	= $GTK_bg.clientHeight/2 - $height/2;

	$GTK_window.style.left = $left +"px";
	$GTK_window.style.top = $top +"px";
	$GTK_window.style.width = $width + "px";
	$GTK_window.style.height = $height + "px";
//screen.width/2

	if (!$form_type){
		if($active_tree == 5){
			$text_1 = 'My questions';
			$text_2 = 'Name';
			$text_3 = 'E-mail';
			$text_4 = 'Send';
		}else if($active_tree == 1){
			$text_1 = 'Мой вопрос';
			$text_2 = 'Имя';
			$text_3 = 'Эл. почта';
			$text_4 = 'Отправить';
		}else{
			$text_1 = 'Mans jautājums';
			$text_2 = 'Vārds';
			$text_3 = 'E-pasts';
			$text_4 = 'Sūtīt';
		}
		var $string =  '<div style="text-align:right; padding:4px"><img src="images/building/close.gif" border="0" onclick="closeform()"/></div>'+
		'<div style="padding:0px 20px"><div id="questionError" style="height:13px;font-size:11px; color:red;"></div><form method="post" action="" name="frmQuestion" id="frmQuestion">'+
		$text_1+':'+
		'<textarea style="width:100%" rows="5" name="question" id="question"></textarea><br>'+
		'<table><tr><td>'+ $text_2 +':</td><td><input type="text" size="30" name="name" id="name" value=""/></td></tr><tr><td>'+ $text_3 +':</td><td><input type="text" size="30" name="email" id="email" value=""/></td></tr></table><br>'+
		'<div style="text-align:right"><input class="button" type="button" name="send_question" value="'+ $text_4 +'" onclick="checkQuestionForm('+$active_tree+');"/></div>'+
		'</form></div>';
	} else if ($form_type == 2){
		if($active_tree == 5){
			$text_1 = 'Username';
			$text_2 = 'Password';
			$text_3 = 'Login';
		}else if($active_tree == 1){
			$text_1 = 'Имя пользователя';
			$text_2 = 'Пароль';
			$text_3 = 'Login';
		}else{
			$text_1 = 'Lietotājvārds';
			$text_2 = 'Parole';
			$text_3 = 'Pieslēgties';
		}
		var $string =  '<div style="text-align:right; padding:4px"><img src="images/building/close.gif" border="0" onclick="closeform()"/></div>'+
		'<div style="padding:0px 10px"><div id="loginform" style="display:none"></div><div id="loginmessage" style="color:red; text-align:center"></div><form method="post" action="" name="">'+
		'<div id="loginresult"><table><tr><td align="right">'+ $text_1 +':</td><td><input type="text" size="36" name="f_name" id="f_name" value="" style="width:100%"/></td></tr>'+
		'<tr><td align="right">'+ $text_2 +':</td><td><input type="password" size="36" name="f_pass" id="f_pass" value=""/></td></tr>'+
		'</table><br>'+
		'<div style="text-align:right"><input class="button" type="button" name="send_tofriend" onclick="make_login()" value="'+ $text_3 +'"/></div></div>'+
		'</form></div>';

	} else {
		if($active_tree == 5){
			$text_1 = 'Name of friend';
			$text_2 = 'E-mail address of your friend';
			$text_3 = 'Your message';
			$text_4 = 'Your name';
			$text_5 = 'Your e-mail';
			$text_6 = 'Send';
		}else if($active_tree == 1){
			$text_1 = 'Имя друга';
			$text_2 = 'Эл. почта друга';
			$text_3 = 'Ваше сообщение';
			$text_4 = 'Ваше имя';
			$text_5 = 'Ваша эл. почта';
			$text_6 = 'Отправить';
		}else{
			$text_1 = 'Drauga vārds';
			$text_2 = 'Drauga e-pasts';
			$text_3 = 'Jūsu ziņa';
			$text_4 = 'Jūsu vārds';
			$text_5 = 'Jūsu e-pasts';
			$text_6 = 'Sūtīt';
		}
		var $string =  '<div style="text-align:right; padding:4px"><img src="images/building/close.gif" border="0" onclick="closeform()"/></div>'+
		'<div style="padding:0px 10px"><div id="SendToFriendError" style="height:13px;font-size:11px; color:red;"></div><form method="post" action="" name="frmSendToFriend" id="frmSendToFriend">'+
		'<table width="380"><tr><td align="right">'+ $text_1 +':</td><td><input type="text" size="30" name="f_name" value=""/></td></tr>'+
		'<tr><td align="right">'+ $text_2 +':</td><td><input type="text" size="30" name="f_email" value=""/></td></tr>'+
		'<tr><td align="right">'+ $text_3 +':</td><td><textarea style="width:100%" rows=4 name="zina"></textarea></td></tr>'+
		'<tr><td align="right">'+ $text_4 +':</td><td><input type="text" size="30" name="name" value=""/></td></tr>'+
		'<tr><td align="right">'+ $text_5 +':</td><td><input type="text" size="30" name="email" value=""/></td></tr>'+
		'</table><br>'+
		'<div style="text-align:right"><input class="button" type="button" name="send_tofriend" value="'+ $text_6 +'" onclick="checkSendToFriendForm('+$active_tree+');"/></div>'+
		'</form></div>';
	}

	$GTK_window.innerHTML = $string;

	document.body.appendChild($GTK_window);
	if (document.getElementById("f_name")){
		document.getElementById("f_name").focus();
	}
}

function Validate_String(string, return_invalid_chars)
         {
         valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
         invalid_chars = '';
         
         if(string == null || string == '')
            return(true);
         
         //For every character on the string.   
         for(index = 0; index < string.length; index++)
            {
            char = string.substr(index, 1);                        
            
            //Is it a valid character?
            if(valid_chars.indexOf(char) == -1)
              {
              //If not, is it already on the list of invalid characters?
              if(invalid_chars.indexOf(char) == -1)
                {
                //If it's not, add it.
                if(invalid_chars == '')
                   invalid_chars += char;
                else
                   invalid_chars += ', ' + char;
                }
              }
            }                     
            
         //If the string does not contain invalid characters, the function will return true.
         //If it does, it will either return false or a list of the invalid characters used
         //in the string, depending on the value of the second parameter.
         if(return_invalid_chars == true && invalid_chars != '')
           {
           last_comma = invalid_chars.lastIndexOf(',');
           
           if(last_comma != -1)
              invalid_chars = invalid_chars.substr(0, $last_comma) + 
              ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
                      
           return(invalid_chars);
           }
         else
           return(invalid_chars == ''); 
         }


function Validate_Email_Address(email_address)
         {
         //Assumes that valid email addresses consist of user_name@domain.tld
         at = email_address.indexOf('@');
         dot = email_address.indexOf('.');
         
         if(at == 0 ||
			dot == 0 ||
			at == -1 || 
            dot == -1 || 
            dot <= at + 1 ||
            dot > email_address.length - 3)
            return(false);
            
         user_name = email_address.substr(0, at);
         domain_name = email_address.substr(at + 1, email_address.length);                  
         
         if(Validate_String(user_name) === false || 
            Validate_String(domain_name) === false)
            return(false);                     
         
         return(true);
         }
		 
function checkQuestionForm($active_tree)
{	
		if($active_tree == 5){
			$text_1 = 'Please fill in all fields correctly!';
			$text_2 = 'Mistake in e-mail!';
		}else if($active_tree == 1){
			$text_1 = 'Просим правильно заполнить все поля!';
			$text_2 = 'Неправильно указана эл. почта!';
		}else{
			$text_1 = 'Lūdzu aizpildiet visus laukus korekti!';
			$text_2 = 'E-pasts nav norādīts korekti!';
		}
		
		if (window.document.frmQuestion.question.value=='') {
			document.getElementById("questionError").innerHTML = $text_1;
			window.document.frmQuestion.question.focus();
			return;
		}else if (window.document.frmQuestion.name.value=='') {
			document.getElementById("questionError").innerHTML = $text_1;
			window.document.frmQuestion.name.focus();
			return;
		}else if (window.document.frmQuestion.email.value=='') {
			document.getElementById("questionError").innerHTML = $text_1;
			window.document.frmQuestion.email.focus();
			return;
		} else {
			if(Validate_Email_Address(window.document.frmQuestion.email.value)){
				window.document.frmQuestion.submit();
			}else{
				document.getElementById("questionError").innerHTML = $text_2;
				window.document.frmQuestion.email.focus();
			}
		}
}

function checkSendToFriendForm($active_tree)
{	
		if($active_tree == 5){
			$text_1 = 'Please fill in all fields correctly!';
			$text_2 = 'Mistake in e-mail!';
			$text_3 = 'Mistake in e-mail of your friend!';
		}else if($active_tree == 1){
			$text_1 = 'Просим правильно заполнить все поля!';
			$text_2 = 'Неправильно указана Ваша эл. почта!';
			$text_3 = 'Неправильно указана эл. почта друга!';
		}else{
			$text_1 = 'Lūdzu aizpildiet visus laukus korekti!';
			$text_2 = 'Jūsu E-pasts nav norādīts korekti!';
			$text_3 = 'Drauga E-pasts nav norādīts korekti!';
		}
		
		if (window.document.frmSendToFriend.f_name.value=='') {
			document.getElementById("SendToFriendError").innerHTML = $text_1;
			window.document.frmSendToFriend.f_name.focus();
			return;
		}else if (window.document.frmSendToFriend.f_email.value=='') {
			document.getElementById("SendToFriendError").innerHTML = $text_1;
			window.document.frmSendToFriend.f_email.focus();
			return;
		}else if (window.document.frmSendToFriend.zina.value=='') {
			document.getElementById("SendToFriendError").innerHTML = $text_1;
			window.document.frmSendToFriend.zina.focus();
			return;
		}else if (window.document.frmSendToFriend.name.value=='') {
			document.getElementById("SendToFriendError").innerHTML = $text_1;
			window.document.frmSendToFriend.name.focus();
			return;
		}else if (window.document.frmSendToFriend.email.value=='') {
			document.getElementById("SendToFriendError").innerHTML = $text_1;
			window.document.frmSendToFriend.email.focus();
			return;
		} else {
			if(Validate_Email_Address(window.document.frmSendToFriend.f_email.value)){
				if(Validate_Email_Address(window.document.frmSendToFriend.email.value)){
					window.document.frmSendToFriend.submit();
				}else{
					document.getElementById("SendToFriendError").innerHTML = $text_2;
					window.document.frmSendToFriend.email.focus();
				}
			}else{
				document.getElementById("SendToFriendError").innerHTML = $text_3;
				window.document.frmSendToFriend.f_email.focus();
			}
			
		}
}

function closeform(){
	$GTK_window.parentNode.removeChild($GTK_window);
	$GTK_bg.parentNode.removeChild($GTK_bg);
}

function afterlogin(){
	window.document.location.href  = window.document.location.href;
}

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


function make_login(){
	var $login = document.getElementById("f_name").value;
	var $pass = document.getElementById("f_pass").value;
	$url = $URL+"modules/components/rpc.login.php?login="+$login+"&pass="+$pass;
	wbg_rpc_get_file($url, "loginform", 0);

}

function init(){
	/** Initialization code.
	 * If you use your own event management code, change it as required.
	 */
	if (window.addEventListener)
	        /** DOMMouseScroll is for mozilla. */
	        document.getElementById("content-area").addEventListener('DOMMouseScroll', wheel, false);
	/** IE/Opera. */
	document.getElementById("content-area").onmousewheel = wheel;
}

function wheel(event){
        var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta)
                handle(delta);
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault)
                event.preventDefault();
	event.returnValue = false;
}

function handle($delta) {
	move_lenta($delta);
}

$scroll_marker = '';
function scroller_marker($event){
	var $event = $event ? $event : window.event ;
	var $X_position = $event.pageX ? $event.pageX : $event.clientX + document.body.scrollLeft;
	var $Y_position = $event.pageY ? $event.pageY : $event.clientY + document.body.scrollTop;
	if (!$scroll_marker){
		$scroll_marker = document.createElement("DIV");
		$scroll_marker.style.position = "absolute";
		document.body.appendChild($scroll_marker);
		
	}
	$scroll_marker.style.top = $Y_position;
	$scroll_marker.style.left = ($X_position*1-24)+"px";
	$scroll_marker.style.zindex = "111111";
}

function hide_marker(){
	if ($scroll_marker){
		$scroll_marker.parentNode.removeChild($scroll_marker);
		$scroll_marker = '';
	}
}