var isOpera = (navigator.userAgent.toLowerCase().indexOf("opera") > -1 ? 1 : 0);
var isIE = (navigator.userAgent.toLowerCase().indexOf("msie") > -1 ? 1 : 0);

var facebook_id = 1, lj_id = 2, twitter_id = 3, vkontakte_id = 4, buzz_id = 5, mir_id = 6, odkl_id = 7;

function $(name) {
	return document.getElementById(name);
}

function clock() {
	now= new Date();
	var arMonth=new Array('Января', 'Февраля', 'Марта', 'Апреля', 'Мая', 'Июня', 'Июля', 'Августа', 'Сентября', 'Октября', 'Ноября', 'Декабря');
	var arWeekDay={'Mon':'Понедельник', 'Tue':'Вторник', 'Wed':'Среда', 'Thu':'Четверг', 'Fri':'Пятница', 'Sat':'Суббота', 'Sub':'Воскресенье'};


	var date= now.getDate();
	var month= arMonth[now.getMonth()];
	var year= now.getYear();
	var hours= now.getHours();
	var minutes= now.getMinutes();
	var gmt=now.toGMTString();

	var weekDayStr=gmt.match(/^[^,]+/i);
	var weekDay=arWeekDay[weekDayStr];

	if(date<10)	date='0'+date;
	if(month<10)	month='0'+month;
	var str="Суббота, "+date+" "+month+", "+hours+":"+minutes;

	document.getElementById('date').innerHTML=str;
	//document.write=str;
	setTimeout("clock()",1000);

}






function wait(id) {
	$(id).innerHTML='<img src="http://www.realtors.com.ua/images/icon/wait.gif" width="16" height="16" align=absmiddle>';
}

function ready(id) {
	$(id).innerHTML='';
}


function getpass(id)
    {
    document.getElementById("userpswd"+id).style.display='';
    document.getElementById("psevdopass"+id).style.display='none';
    document.getElementById("userpswd"+id).focus();
    }


function makeStart(url) {
    if (isIE) document.write('<a href="" onClick="this.style.behavior=\'url(#default#homepage)\';this.setHomePage(\''+url+'\');return false;" >'+lang['make_start']+'</a>');
}

function show_media(file, vars, width, height, bgcolor) {
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+ width + '" height="' + height + '" id="test_draw" align="middle"><param name="allowScriptAccess" value="sameDomain" /><PARAM NAME=FlashVars VALUE="' + vars + '"><param name="movie" value="'+ file + '" /><param name="quality" value="high" /><param name="bgcolor" value="'+ bgcolor + '" /><embed src="'+ file + '" FlashVars="' + vars + '"  quality="high" bgcolor="' + bgcolor + '" width="' + width + '" height="' + height + '" name="test_draw" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');
}


function star_out(rate) {
	for(i=1;i<=5;i++) {
		num=rate.substring(i-1,i);

		if (num==1) im='orange';
		else if (num==2) im='grey';
		else im='half_grey';
		document.getElementById('star'+i).src="/img/star_" + im + ".gif";
	}
}


function star_over(number) {
	for(i=1;i<=5;i++) {
		if(i<=number) im='star_orange.gif'; else im='star_grey.gif';
		document.getElementById('star'+i).src="/img/"+im;
	}
}

function star_fireup(id) {
	for ( var i=1; i<=id ;i++) {
		document.getElementById('star'+i).src="/img/star_orange.gif";
	}
	for ( true; i<=5 ;i++){
	document.getElementById('star'+i).src="/img/star_grey.gif";
	}
}


function gamesViewCount(id) {
    JsHttpRequest.query(
        '/view_count/',
        {id: id},
        function(responseJS, responseText) {},
        true
    );
	return false;
}

function gamesVoteCount(id, ball) {
    JsHttpRequest.query(
        '/vote_count/',
        {id: id, ball: ball},
        function(responseJS, responseText) {
			if (responseJS.error!='') {
				alert(responseJS.error)	;
			} else {
				//star_fireup(ball, 1);
				//alert(responseJS.rate);
				$('stars').innerHTML=responseJS.stars;
			}
		},
        true
    );
	return false;
}

function changeCapcha(num) {
	$('capcha'+num).src="http://www."+mainDomain+"/captcha33/?num="+num+"&t="+Math.random();
	return false;
}

function myonlineStat(moduleIdFrom, moduleIdTo) {
    JsHttpRequest.query(
        'http://www.'+mainDomain+'/myonline_stat/',
        {moduleIdFrom: moduleIdFrom, moduleIdTo: moduleIdTo},
        function(responseJS, responseText) {},
        true
    );
	return true;
}

function stat(id) {
    JsHttpRequest.query(
        'http://www.'+mainDomain+'/stat/',
        {id: id},
        function(responseJS, responseText) {},
        true
    );
	return true;
}

// stat, r0mk1n

function __mmad( dm ) {
	var is_exists = __rC( '__onmad' );
	if ( !is_exists || is_exists == null ) {
		__wC( '__onmad', __gMID(), 730, dm );
	} else {
		__wC( '__onmad', is_exists, 730, dm );
	}
}

function __gMID() {
	var date = new Date();
	return Math.ceil( Math.random() * 999999 * ( date.getTime() + ( date.getTime() * Math.random() * 999 ) + Math.random() * 99999 ) ).toString().substring(0, 8) + '' + Math.ceil( Math.random() * 999999 * ( date.getTime() + ( date.getTime() * Math.random() * 999 ) + Math.random() * 99999 ) ).toString().substring(0, 8)+ '' + Math.ceil( Math.random() * 999999 * ( date.getTime() + ( date.getTime() * Math.random() * 999 ) + Math.random() * 99999 ) ).toString().substring(0, 8);
}

function __wC( name, value, days, dm ) {
	var expires = "";
	if ( days ) {
		var date = new Date();
		date.setTime( date.getTime()+(days*24*60*60*1000) );
		expires = "; expires="+date.toGMTString();
	}
	document.cookie = name+"="+value+expires+"; path=/; domain="+dm;
}

function __rC( name ) {
	var nameEQ = name + "=";
	var ca = document.cookie.split( ';' );
	for( var i=0; i < ca.length; i++ ) {
		var c = ca[i];
		while ( c.charAt(0) == ' ' ) {
			c = c.substring( 1, c.length );
		}
		if ( c.indexOf( nameEQ ) == 0 ) {
			return c.substring( nameEQ.length, c.length );
		}
	}
	return null;
}

function __eC( name ) {
	writeCookie( name, "", -1 );
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function goPayShopLink(id, siteId, rnd) {
    JsHttpRequest.query(
    	'/_ajax/go_shop_click/',
    	{id:id, siteId:siteId, rnd:rnd},
    	function(responseJS, responseText) {},
    	true
    );
    return true;
}

var allServFlag = 0;
var globalSearchFlag = 0;
var myMenuFlag = 0;

function allServiceBind() {
	document.getElementById( "all-services-link" ).onmouseover = showAllServices;
	document.getElementById( "all-services-link" ).onmouseout = checkFlag;
	document.getElementById( "all-services" ).onmouseover = showAllServices;
	document.getElementById( "all-services" ).onmouseout = checkFlag;
}

function showAllServices() {
	allServFlag = 1;
	document.getElementById( "all-services-wrap" ).className = "all-services-open";
	var photoTd = document.getElementById( "ph_set" );
	if ( photoTd != null && photoTd != "undefined" ) {
		photoTd.style.position = "static";
	}
	var photo = document.getElementById( "photo" );
    if ( photo != null && photo != "undefined" ) {
        photo.style.position = "static";
    }
}

function showGlobalSearch() {
	if ( globalSearchFlag == 0 ) {
		var photoTd = document.getElementById( "ph_set" );
		if ( photoTd != null && photoTd != "undefined" ) {
			photoTd.style.position = "static";
		}
		var photo = document.getElementById( "photo" );
	    if ( photo != null && photo != "undefined" ) {
	        photo.style.position = "static";
	    }
		globalSearchFlag = 1;
		document.getElementById( "global-search-img" ).src = "http://i.online.ua/search2.gif";
		document.getElementById( "global-search-wrap" ).className = "global-search-open";
		document.getElementById( "search-string" ).focus();
	} else {
		hideGlobalSearch();
	}
}

function showMyMenu( url ) {
	if ( myMenuFlag == 0 ) {
		myMenuFlag = 1;
		document.getElementById( "my-menu-wrap" ).className = "my_menu_open";
		getMyMenuFriends( url );
		getMyMenuMessages( url );
		getMyMenuGroups( url );
		getMyMenuSubs( url );
	} else {
		hideMyMenu();
	}
}

function checkFlag() {
	if ( allServFlag == 1 ) {
		allServFlag = 0;
		setTimeout( "hideAllServices()", 100 );
	} else {
	}
}

function hideAllServices() {
	if ( allServFlag == 0 ) {
		document.getElementById( "all-services-wrap" ).className = "";
		var photoTd = document.getElementById( "ph_set" );
		if ( photoTd != null && photoTd != "undefined" ) {
            photoTd.style.position = "relative";
            photoTd.style.zIndex = "100";
        }
        var photo = document.getElementById( "photo" );
        if ( photo != null && photo != "undefined" ) {
            photo.style.position = "relative";
            photo.style.zIndex = "100";
        }
	}
}

function hideGlobalSearch() {
	globalSearchFlag = 0;
	document.getElementById( "global-search-img" ).src = "http://i.online.ua/search.gif";
	document.getElementById( "global-search-wrap" ).className = "";
	var photoTd = document.getElementById( "ph_set" );
	if ( photoTd != null && photoTd != "undefined" ) {
        photoTd.style.position = "relative";
        photoTd.style.zIndex = "100";
    }
    var photo = document.getElementById( "photo" );
    if ( photo != null && photo != "undefined" ) {
        photo.style.position = "relative";
        photo.style.zIndex = "100";
    }
}

function hideMyMenu() {
	myMenuFlag = 0;
	document.getElementById( "my-menu-wrap" ).className = "";
}

function globalSearchBind() {
	addHandler( document, 'click', toggleGlobalSearch, false );
}

function myMenuBind() {
	addHandler( document, 'click', toggleMyMenu, false );
}

function addHandler(element, event, action, param){
    if (document.addEventListener) 
        element.addEventListener(event, action, param);
    else 
        if (document.attachEvent) 
            element.attachEvent('on' + event, action);
        else 
            element['on' + event] = action;
}

function toggleGlobalSearch( e ) {
	e = e || event;
	var target = e.target || e.srcElement;
	var search = document.getElementById( "global-search-wrap" );
	var search_img = document.getElementById( "global-search-close" );
	if ( search && search_img ) {
		var parent = target;
		if ( parent == search_img ) return;
		while ( parent.parentNode && parent != search ) {
			parent = parent.parentNode;
		}
		if ( parent && parent != search ) {
			 hideGlobalSearch();
		}
	}
}

function toggleMyMenu( e ) {
	e = e || event;
	var target = e.target || e.srcElement;
	var menu = document.getElementById( "my-menu-wrap" );
	if ( menu ) {
		var parent = target;
		while ( parent.parentNode && parent != menu ) {
			parent = parent.parentNode;
		}
		if ( parent && parent != menu ) {
			 hideMyMenu();
		}
	}
}

function absPosition( obj ) {
    var x = y = 0;
    while( obj ) {
        x += obj.offsetLeft;
        y += obj.offsetTop;
        obj = obj.offsetParent;
    }
    return { x : x, y : y };
}

function showRules( elem ) {
	var div = elem.parentNode.getElementsByTagName( "div" )[ 0 ];

	var scrollTop;
	navigator.appName == "Microsoft Internet Explorer" ?
	   scrollTop = document.documentElement.scrollTop :
	   scrollTop = document.body.scrollTop;
	
	var delta = absPosition( elem ).y - scrollTop;
	delta < 450 ? div.style.bottom = delta - 480 + "px" : div.style.bottom = "20px";
	div.style.display == "block" ? div.style.display = "none" : div.style.display = "block";
	return false;
	
}

function hideRules( elem ) {
	
	var div = elem.parentNode;
	div.style.display = "none"
	return false;
	
}

function updateSharingStat( moduleId, contentId, serviceId ) {
	if ( moduleId == 4 ) {
		new Ajax.Request( "http://rtb.online.ua/update_sharing_stat.php?module_id=" + moduleId + "&content_id=" + contentId + "&service_id=" + serviceId );
	} else {
    	JsHttpRequest.query(
            'http://rtb.online.ua/update_sharing_stat.php',
            {module_id: moduleId, content_id: contentId, service_id: serviceId},
            function(responseJS, responseText) {},
            true
        );
	}

}

function windowHeight() {
	var de = document.documentElement;
	return self.innerHeight || ( de && de.clientHeight ) || document.body.clientHeight;
}

function showProvider(mes) {
	var div = document.getElementById( "provider" );
	div.style.top = ( document.documentElement.scrollTop + windowHeight()/2 - 195 ) + "px";
	div.style.display = "block";
    var iframe1 = document.getElementById( "provider_iframe1" );      
    iframe1.src = iframe1.src+'&mes='+mes;
    var link = window.location;    
    var iframe2 = document.getElementById( "provider_iframe2" );      
    iframe2.src = 'http://secure.'+mainDomain+'/form_provider.php?link='+link+'&mes='+mes;
    var iframe3 = document.getElementById( "provider_iframe3" );      
    iframe3.src = 'http://secure.'+mainDomain+'/provider_loader.php';
    //sh_provider(0);
    return false;
}

function show_provider(time){
    var iframe2 = document.getElementById( "provider_iframe2" );      
    var iframe3 = document.getElementById( "provider_iframe3" );      
    setTimeout(function() { 
        iframe3.height= '0px';
        iframe2.height= '45%';
    }, time)
}
function sh_provider(time){
    var iframe2 = document.getElementById( "provider_iframe2" );      
    var iframe3 = document.getElementById( "provider_iframe3" );      
        iframe2.height= '0px';
        iframe3.height= '45%';
}

function closeProvider() {   
	var div = document.getElementById( "provider" );
	div.style.display = "none";                                       
    var iframe2 = document.getElementById( "provider_iframe2" );      
    iframe2.src = '';
    var iframe3 = document.getElementById( "provider_iframe3" );      

    return false;
}
