$(window).bind('load resize', function(){
    $('#shadowRight').css("padding", ($('body').width() > 1290 ? "0 6px" : "0"));
});

function Tabs (obj, i) {
	this.current =-1;
	this.obj = obj;
	this.createHtml();
	this.setItem(i||0);
	
	var self = this;
	this.links.click(function(){
		self.setItem(this.hash.replace(/[^\d]/,'')); 
	});
	$(".footer .link a, .devisions .dvItem a").click(function(){
		self.setItem(this.hash.replace(/[^\d]/,'')); 
	});
}
Tabs.prototype.createHtml = function () {
	this.frame = $('<div class="frame4"><div class="tl">&nbsp;</div><div class="tr">&nbsp;</div><div class="bl">&nbsp;</div><div class="br">&nbsp;</div><table><tr></tr></table></div>');
	var subTable = $('<table class="tabs_sub"><tr></tr></table>');
	var subTableTr = subTable.find('tr');
	
	this.obj.prepend(subTable);
	this.obj.prepend(this.frame);
	
	var i, obj;
	var tabs = this.obj.find('.tabs_title');
	var table = this.frame.find('tr');
	
	for(i=0 ; i<tabs.length ; i++){
		obj = $(tabs[i]);
		obj.remove();
		table.append('<td><p><a href="#'+i+'">'+obj.text()+'</a></p><div>&nbsp;</div></td>');
	}
	this.links = this.frame.find('a');
	this.tds = this.frame.find('td');
	this.frame.find('td:last').addClass('last');
	
	var items = this.obj.find('.tabs_item').hide();
	this.items = new Array();
	this.subTds = new Array();

	
	for (i=0 ; i<items.length ; i++) {
		this.items.push($(items[i]));
		this.subTds.push($('<td>&nbsp;</td>').appendTo(subTableTr));		
	}
	this.obj.find('.frame4 td, .frame4 td > div, .tabs_sub td').css({width: (100/this.tds.length)+'%'});
}
Tabs.prototype.setItem = function (i) {
	if(this.current == i){
		return false;
	}else if (this.current>-1){
		this.items[this.current].hide('fast');
		this.tds[this.current].className = '';
		this.subTds[this.current][0].className = '';
	}else{
		
	}
	
	this.tds[i].className = 'current';
	this.subTds[i][0].className = 'current';
	this.items[i].show('slow');
	this.current = i;
}

function lexica ($n,$a,$b,$c) {
	$n=parseInt($n);
	$x= Math.abs($n>9? (($n+'').substr(-2)) :$n);
	return (($x%=100)>9&&$x<20||($x%=10)>4||$x==0?$c:($x==1?$a:$b));
}

function setCookie (name, value) {
  document.cookie = name + "=" + escape(value) +";path=/";
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function realty() {
	this.ids = getCookie('realtyIds') ? unescape((getCookie('realtyIds'))).split(',') : new Array();
	this.save();
}

realty.prototype.save = function() {
	setCookie('realtyIds', this.ids.join(','));
	
	$('.objectInformer span:eq(0)').text(this.ids.length);
	$('.objectInformer span:eq(1)').text(lexica(this.ids.length, 'объект','объекта','объектов'));
	
	//console.log('ok');
}

realty.prototype.add = function (id) {
	for (var i=0; i<this.ids.length ; i++){
		if (this.ids[i] == id) return false;
	}
	
	this.ids.push(id);
	this.save();
}

realty.prototype.rem = function (id) {
	for (var i=0; i<this.ids.length ; i++){
		if (this.ids[i] == id) this.ids.splice(i,1);
	}
	this.save();
}

realty.prototype.clearAll = function() {
	this.ids = new Array();
	this.save();
}

$(function(){
	$('.ri_params tr:odd').addClass('odd');
	
	var rCart = new realty();
	$('input.realty_select').click(function(){
		var obj = $(this);
		if(obj.attr('checked')) {
			rCart.add(obj.val());
		} else {
			rCart.rem(obj.val());
		}
	});
	
	var tabsHtml = $('.tabs');
	for(var i=0; i<tabsHtml.length ; i++){
		new Tabs($(tabsHtml[i]), document.location.hash.replace(/[^\d]/,''));
	}
	
    $(".specialOffers .soMenu a").click(function(){
        var width = $(".specialOffers .soGroup").width();
        var siblings = $(this).parents("tr:eq(0)").find("div");
        var div = $(this).parents("div:eq(0)");
        var i;
        for (i=0 ; i<siblings.length ; i++){
            if(siblings[i] == div[0]) break;
        }
        
        $(".mainPic").removeClass("pic0 pic1 pic2 pic3");
        $(".mainPic").addClass("pic"+i);
        siblings.removeClass("current");
        div.addClass("current");
        //$(".specialOffers .soSlider").animate({left: -1 * width * i}, 'slow');
        $(".specialOffers .soSlider").animate({left: -1 * 108 * i + "%" }, 'slow');
        
        return false;
    });
    
    $(".devisions .dvList").hide().css("width", "100% !important");
    $(".devisions .dvBtn a").click(function(){
        if (this.className.match(/opened/gim)){
            $(".devisions .dvList").hide('slow');
        }else{
            $(".devisions .dvList").show('slow');
        }
        $(".devisions .dvBtn, .devisions .dvBtn a").toggleClass("opened");
        
        return false;
    });
    $(".devisions").mouseleave(function(){
    	$(".devisions .dvList").hide('slow');
    	$(".devisions .dvBtn, .devisions .dvBtn a").removeClass("opened");
    })
    
    $(".head .mainMenu a").bind("mouseenter mouseleave", function(){
        $(this).parent('div:eq(0)').toggleClass('hover');
    });
    $(".leftMenu a").bind("mouseenter mouseleave", function(){
        $(this).parents('div.item').toggleClass('hover');
    });
    
    $(".tb1 tr").bind("mouseenter mouseleave", function(event) {
    	if (event.type=='mouseenter'){
    		$(this).addClass('hover');
    	}else{
    		$(this).removeClass('hover');
    	}    	
    });
    
    $('.liveBanner').addClass('frame2').append('<div class="tl">&nbsp;</div><div class="tr">&nbsp;</div><div class="bl">&nbsp;</div><div class="br">&nbsp;</div>');
});






