function hosm(oid) {
	if(document.getElementById(oid).style.display=="none") document.getElementById(oid).style.display="block";
	else document.getElementById(oid).style.display="none";
	return false;
} 

function AnimateText() {
	$('#top-text').html($('#top-text').html()+$('#top-text').html());
	this.firstText=$('#top-text p:first');
	this.lastText=$('#top-text p:last');
	this.textWidth=this.firstText.width();
	this.elWidth=$('#top-text').width();
	this.div=20;
	this.step1();
}

AnimateText.prototype.step1=function() {
	text=this;
	this.lastText.css({left:-(this.textWidth-this.elWidth-this.div)+'px'});
	this.firstText.animate({left:-(this.textWidth-this.elWidth)+'px'}, this.getDur(this.textWidth-this.elWidth,0), 'linear', function() { text.step2()});
}

AnimateText.prototype.step2=function() {
	text=this; 
	this.firstText.animate({left:-(this.textWidth)+'px'}, this.getDur(this.textWidth,(this.textWidth-this.elWidth)), 'linear');
	this.lastText.animate({left:-(2*this.textWidth-this.elWidth+this.div)+'px'}, this.getDur(2*this.textWidth-this.elWidth+this.div,this.textWidth-this.elWidth-this.div), 'linear', function() { text.step3()});
}

AnimateText.prototype.step3=function() {
	text=this;
	this.firstText.css({left:this.elWidth+'px'});
	this.lastText.animate({left:-(2*this.textWidth)+'px'}, this.getDur(2*this.textWidth,2*this.textWidth-this.elWidth+this.div), 'linear');
	this.firstText.animate({left:0}, this.getDur(this.elWidth,0), 'linear', function() { text.step1()});
}

AnimateText.prototype.getDur=function(width, left) {
	return (width-left)*35;
}

function order(link) {
        var ll=(document.body.clientWidth-550)/2;
        newWindow = window.open(link+"&tem=2", "ttt", "scrollbars=1, width=560 , height=450 ,left="+ll+", top=50");
        newWindow.focus();
        return false;
}

function Reiting() {
	this.flag=0;
	el=this;
	$('.golos').mouseover(function(){ el.onOver(this.id); }).mouseout(function(){ el.onOut(this.id); }).click(function(){ if(!el.flag) { el.flag=1; el.onClick(this.id); } });
}

Reiting.prototype.onOver=function(sid) {
	fid=sid.substr(7);
	$('#reit'+fid+' .golos').removeClass('golos-on').addClass('golos-off');
	$('#'+sid).removeClass('golos-off').addClass('golos-on');
}

Reiting.prototype.onOut=function(sid) {
	fid=sid.substr(7);
	$('#'+sid).removeClass('golos-on').addClass('golos-off');
	reit=this.getReiting('reit'+fid);
	if (reit) {
		for (i=1; i!=reit+1; i++) {
			$('#gol'+i+'for'+fid).removeClass('golos-off').addClass('golos-on');
		}
	}
}

Reiting.prototype.onClick=function(sid) {
	el=this;
	fid=sid.substr(7);
	addRait=sid.substr(3,1);
	$.post('/scripts/reiting.php', { action:'add_reiting', id:fid, rait:addRait }, function(data){ el.setNewRaitg(data,fid); el.flag=0; });
}

Reiting.prototype.setNewRaitg=function(data,sid) {
	newReit='';
	eval(data);
	if (newReit!='') {
		reit=this.getReiting('reit'+sid);
		$('#reit'+sid).removeClass('count'+reit).addClass('count'+newReit);
		$('#reit'+sid+' .golos').removeClass('golos-on').addClass('golos-off');
		$('#message').html(message).show();
		setTimeout(function() { $('#message').hide() }, 1500);
		this.onOut('gol1for'+sid);
	}
	else {
		$('#message').html(message).show();
		setTimeout(function() { $('#message').hide() }, 1500);
	}
}

Reiting.prototype.getReiting=function(sid) {
	reit=0;
	if ($('#'+sid).hasClass('count1')) reit=1;
	if ($('#'+sid).hasClass('count2')) reit=2;
	if ($('#'+sid).hasClass('count3')) reit=3;
	if ($('#'+sid).hasClass('count4')) reit=4;
	if ($('#'+sid).hasClass('count5')) reit=5;
	return reit;
}

jQuery(document).ready(function() {
	if(document.getElementById('top-text')) {
		var AnText=new AnimateText();
	}
	$('.catalog a.order').click(function(){ order(this.href); return false; });
	$('#popupwindow').click(function(){ order(this.href); return false; });
	addReiting=new Reiting();
	if ($.cookie('path') !== null){
	   prepare_history();
	}
	var RegExp = /[dom|cottage|siti|jubilejnye\-doma|trend|esite|classic|easy|optimi|mini|town\-houses]..tem=/;
	var url = document.location.toString().replace(/http:\/\/[^\/]*\/(.*)/,'$1');
	var matchPos = url.toString().search(RegExp);
	if(matchPos != -1){
	  if ($.cookie('path') == null){
	   path = '';
	  } else {
	   path = $.cookie('path');
	  }
// alert(path.indexOf(url,0));
//	  if ((path.indexOf(url,0) == -1) || (path == '')){
// alert(encodeURIComponent(url));
//	    path = path.replace(encodeURIComponent(url), '');
	    path = path.replace(url, '');
	    path = path.replace('||', '|');
	    path = url + '|' + path;
	    arr = path.split('|', 10);
	    path = arr.join('|');
	    $.cookie('path', path, { expires: 365, path: '/'});
//	  }
	}



});




function prepare_history(){
  if ($.cookie('history_tray') == null){
	 history_tray = 0;
	$.cookie('history_tray', history_tray, { expires: 365, path: '/'});
  } else {
	 history_tray = $.cookie('history_tray');
  }

  $('body').append('<div id="history_block" style="position: absolute; bottom: 0px; right: 2px; font: 9px Arial,sans-serif; padding: 2px; width: 210px;"></div>');
  $('#history_block').append('<div id="history" style="right: 2px; background: #FFF; text-align: right; font: 9px Arial,sans-serif; padding: 2px; opacity: 0.7; cursor:pointer;">Свернуть</div>');
  $('#history_block').append('<div id="historyblock" style="display: inline; overflow-y: scroll; overflow-x: hidden; position: absolute; bottom: 20px; right: 0px; max-height: 225px; width: 210px; background: white; font-size: 10px; padding: 2px; cursor:pointer; border: 1px dashed grey; background-color: #EEEEEE"></div>');
//  $('body').append('<div id="historyblock" style="overflow-y: scroll; overflow-x: hidden; position: absolute; bottom: 0px; right: 0px; max-height: 225px; width: 210px; background: white; font-size: 10px; padding: 2px; cursor:pointer; border: 1px dashed grey; background-color: #EEEEEE"></div>');
  $('#historyblock').load('/history.php', function(){
	$('.catalog a.order').click(function(){ order(this.href); return false; });
  });

  $(window).scroll(function () { 
    $("#history_block").css("bottom", -$(this).scrollTop()); 
//    $("#historyblock").css("bottom", 20 - $(this).scrollTop()); 
  }).scroll();



  $("#history").click(function () {
	if (history_tray == 0) {
    $("#history_block").css("width", '210px');
//	alert(history_tray);
	history_tray = 1;
	$("#history").html('Свернуть');
	$.cookie('history_tray', history_tray, { expires: 365, path: '/'});

	} else {
    $("#history_block").css("width", '60px');
	$("#history").html('Развернуть');
	history_tray = 0;
	$.cookie('history_tray', history_tray, { expires: 365, path: '/'});

  	}
	$(window).scroll();
    $("#historyblock").toggle();
    $("#historyblock").css("overflow-y", "scroll").css("overflow-x", "hidden");
  });    

/*
  $("#historyblock").hover(
    function () {},
    function () {
	$("#historyblock").toggle("slow");
    }
  );
*/
if (history_tray == 0) {
    $("#history").html('Развернуть');
    $("#history_block").css("width", '60px');
    $("#historyblock").toggle();
} else {
    $("#history").html('Свернуть');
    $("#history_block").css("width", '210px');

}
// $("#history").attr('bottom', history_tray*230+10);


}

