flash_map = "<p><img alt='' src='/img/nofla/primedesign.gif'></img></p>";
var zindex = 9000;
var need_login=true;
var price_required=0;

$(document).ready(function(){
	need_login=$(".ent a").length;
	$(":button, :submit").attr("class", "button");

	/*$("div.right_column").css("padding-bottom", $("div.search").height() + 30);*/

	if ($("div.rc").length == 0) $("div.rekl div.margin").css("display", "none");
	var num = $("div.rc").length;
	var i;
	for (i = 0; i < num; i++){
		if ((i+1)%3 == 0) {
			$("div.rc").eq(i).after("<div class='clear'>&nbsp;</div>");
			$("div.rc").eq(i).css("margin-right", "0px");
		}
	}
	
	$("div.sel_furniture select").each(function(){
		bind_select($(this));
	});
	
	$("div.soc_comments_tabs div.tabs").each(function(){
		bind_soc_tabs($(this));
	});
	
	noflash();
	// update from 24.05.11
	$(".sb_drop").bind("change", function(e) {
		$("#af").removeAttr("checked");
		$("#bf").removeAttr("checked");
	});
});

function valignIMG($el){
	alert($el.height());
	$el.css("top", (134-$el.height())/2);		
}

function callei() {
    thisMovie("map").print_map();
}

function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName]
    }
}

function counter () {
	document.write(' <a href=\"http://top.mail.ru/jump?from=715201\"><img src=\"http://top.list.ru/counter?id=715201;t=49\" border=\"0\" height=\"1\" width=\"1\" /></a> ') 
	document.write(' <a href=\"http://top100.rambler.ru/top100/\"><img src=\"http://counter.rambler.ru/top100.cnt?635804\" alt=\"Ramblers Top100\" width=\"1\" height=\"1\" border=\"0\"></a> ') 
	document.write(' <a href=\"http://www.liveinternet.ru/click\" '+'target=liveinternet><img src=\"http://counter.yadro.ru/hit?t11.3;r'+escape(document.referrer)+((typeof(screen)=='undefined')?'':';s'+screen.width+'*'+screen.height+'*'+(screen.colorDepth?screen.colorDepth:screen.pixelDepth))+';'+Math.random()+'\" alt=\"liveinternet.ru\" '+'border=0 width=1 height=1></a>')
}

function add_fav(id) {
	$.get("/fav.php", {action: "set", id: id}, function (d) {
		if (d=='1') $(".favorites").html('<a href="javascript:void(0)" onclick="del_fav(' + id + ')">Удалить из избранного</a>');
	});
}

function del_fav(id) {
	$.get("/fav.php", {action: "del", id: id}, function (d) {
		if (d=='1') $(".favorites").html('<a href="javascript:void(0)" onclick="add_fav(' + id + ')">Добавить в избранное</a>');
	});
}

function do_login() {
	$("#LoginForm").ajaxSubmit({ success: function(d) {
		if ($(".success", d).length) {
			need_login=false;
			if (price_required && card_el) {
				open_1(card_el, true);
				price_required=false;
				card_el=null;
			} else setTimeout("location.reload(true);",250);
		}
	} });
}

function do_register() {
	$("#Registerform").ajaxSubmit({ success: function(d) {
		if ($(".errors", d).length) {
			$(".formtable").html($(".formtable", d).html());
			err0resize();
		} else {
			need_login=false;
			if (price_required && card_el) {
				open_1(card_el, true);
				price_required=false;
				card_el=null;
			} else setTimeout("location.reload(true)",250);
		}
	} });
}

function err0resize(){
	var height = parseInt($("div.contentHolder table.formtable").height()) + 85;
	var pos = _getCenterPos($("div.contentHolder").width(), height);
	$('div.contentHolder').css({
		'height': height,
		'top': pos['top']
	});
}

function flashVersion() {
      var ua = navigator.userAgent.toLowerCase();
      var isIE = (ua.indexOf("msie") != -1 && ua.indexOf("opera") == -1 && ua.indexOf("webtv") == -1);
      var version = 0;
      var lastVersion = 11;
      var i;
      if (isIE) {
            try {
                  for (i = 3; i <= lastVersion; i++) {
                        if (eval('new ActiveXObject("ShockwaveFlash.ShockwaveFlash.'+i+'")')) {
                              version = i;
                        }
                  }
            } catch(e) {}
      } else {
            for (i = 0; i < navigator.plugins.length; i++) {
                  if (navigator.plugins[i].name.indexOf('Flash') > -1) {
                        version = (parseInt(navigator.plugins[i].description.charAt(16)) > version) ? parseInt(navigator.plugins[i].description.charAt(16)) : version;
                  }
            }
      }
      return version;
}

function noflash(){
	if (flashVersion() == 0){
		$("div.picture").children().remove().end().html("<img src='/attachements/sec2/mainpic_noflash.jpg' alt='' />");
		$("p.flash_map").replaceWith(flash_map);
	}
	else return false;
}

// update from 24.05.11
function require_price(id, tok) {
	price_required=0;
	if (need_login) {
		price_required=id;
		op_pop($(".ent a:first"));
	} else $.get("/fav.php", {action: "price", id: id, tok: tok}, function (d) {
		if (d=='1') $(".pr_req").html('Спасибо! Наш менеджер скоро свяжется с Вами.');
	});
}
function bind_select(sel){
	zindex = zindex - 1;
	sel.wrap("<div class=head_sel style=z-index:" + zindex + "></div>").hide();
	sel.hide();

	var webkit = 0;
	if($.browser.webkit) webkit = -2;
	var head = sel.parent();
	head.width(sel.width()+4+webkit);
	head.css("background-position",sel.width()+webkit-14+"px 1px");
	head.append("<span class='boldtext'></span><div class=opt_sel></div>");

	var cont = head.children().eq(2);
	cont.width(head.width());//
	var span = head.children().eq(1);
	var num_selected = 0;
	var i = 0;

	sel.children().each(function(){
                  var tex_t = $(this).text();
		  var int_text = tex_t.indexOf('(')+1;
		  if(int_text){
		      tex_t = tex_t.substring(0,int_text-1)+"<span>"+tex_t.substring(int_text-1)+"</span>";
		  };
		  cont.append("<div class='opt_opt' id=" + $(this).val() + ">" + tex_t + "</div>");
		  if ($(this).attr("selected")) num_selected = i;
		  i++;
	});

	span.text(sel.children().eq(num_selected).text());
        cont.children().eq(num_selected).addClass("selected_val");

	var opt = cont.children();
	countEl = opt.size();

	cont.find(":first").css("padding-top", "2px");
	cont.find(":last").css({"padding-bottom": "2px", "margin-bottom": "1px"});
	//!!!!!!!!!
	var rozbiv = 23;
	var cont_child_len = cont.children().length;
	
	if(cont_child_len > rozbiv){
	       opt.css({"width":"190px", "margin-right":"10px", "margin-left":"5px", "padding-left":"5px"});
	       opt.eq(0).css({"float":"none", "color":"#000000", "padding-bottom":"6px","padding-top": "5px", "cursor":"pointer","font-weight":"bold"});
	       opt.eq(0).children().css({"color":"#000000"});
	       var colum_c = Math.ceil(cont.children().length / rozbiv);
               cont.width(210*colum_c+(colum_c-1)*5);
               cont.css({"margin-top":"-6px", "border": "1px solid #D7A94C","z-index":"100000"});
	       //!!!!!!!!!!!!!!
	       
	       for(i=1;i<=colum_c;i++){
                    opt.slice((i-1)*rozbiv+1, rozbiv*i+1).wrapAll("<div class='pr'  style='margin-left:0px'></div>");
	       }
	       cont.find("div.pr:first").css("margin-left","0px");

               opt.eq(cont_child_len-1).parent().after("<div class='clear'></div>");
               var opt = cont.find("div.opt_opt");
	};

	span.bind("click", function(){
		//cont.fadeToggle("fast");
		if($(this).hasClass("open_sel_")){
		      $(this).removeClass("open_sel_");
		      cont.fadeOut("fast");
		      $("div#select_bg_div").remove();
		}else{
		     // $("div#select_bg_div").remove();
		      $("div.head_sel span").not($(this)).next().removeClass("open_sel_").hide();
		      $(this).addClass("open_sel_");
		      cont.fadeIn("fast");
		      var html = "<div id='select_bg_div' ></div>";
		      $(html).insertBefore("div.drop_shadow");
		      $("#select_bg_div").css({"position":"absolute" ,"left":0 ,"top":0 ,"width":"100%", "height": $("body").height(),"z-index": zindex-1000 });
		      $("#select_bg_div").bind("click",function(){$("div.opt_sel").hide();$("div.opt_sel").prev().removeClass("open_sel_");$("div#select_bg_div").remove();});
		};

	});

	opt.bind("click", function(){
	        if($(this).parent().hasClass("pr")){
                    $("#tf option").removeAttr("selected");
                    $("#sf option").removeAttr("selected");
                    $("#pf option").removeAttr("selected");
                    rebuild_sel($("#tf"));
		    rebuild_sel($("#sf"));
		    rebuild_sel($("#pf"));
	        }
	        $("div#select_bg_div").remove();
	        cont.find("div#"+sel.val()).removeClass("selected_val");
		sel.val($(this).attr("id"));
  		$(this).addClass("selected_val");
		sel.change();
		span.text($(this).text());
		if($(this).parent().hasClass("pr")){
		     $(this).parent().parent().fadeOut("normal");
		}else{
		      $(this).parent().fadeOut(200);
		};
	});
}

function rebuild_sel(sel){
	var span = sel.next();
	span.unbind("click");
	var head = sel.parent();
	var cont = sel.next().next();
	var i = 0;
	span.empty();
	cont.empty();

 	sel.children().each(function(){
                  var tex_t = $(this).text();
		  var int_text = tex_t.indexOf('(')+1;
		  if(int_text){
		      tex_t = tex_t.substring(0,int_text-1)+"<span>"+tex_t.substring(int_text-1)+"</span>";
		  };
		  cont.append("<div class='opt_opt' id=" + $(this).val() + ">" + tex_t + "</div>");
		  if ($(this).attr("selected")) num_selected = i;
		  i++;
	});
	
	span.text(sel.children().eq(num_selected).text());
	cont.children().eq(num_selected).addClass("selected_val");

	var opt = cont.children();
	cont.find(":first").css("padding-top", "2px");
	cont.find(":last").css({"padding-bottom": "2px", "margin-bottom": "1px"});
	span.bind("click", function(){
		//cont.fadeToggle("fast");
		if($(this).hasClass("open_sel_")){
		      $(this).removeClass("open_sel_");
		      cont.fadeOut("fast");
		      $("div#select_bg_div").remove();
		}else{
		     // $("div#select_bg_div").remove();
		      $("div.head_sel span").not($(this)).next().removeClass("open_sel_").hide();
		      $(this).addClass("open_sel_");
		      cont.fadeIn("fast");
		      var html = "<div id='select_bg_div' ></div>";
		      $(html).insertBefore("div.drop_shadow");
		      $("#select_bg_div").css({"position":"absolute" ,"left":0 ,"top":0 ,"width":"100%", "height": $("body").height(),"z-index": zindex-1000 });
		      $("#select_bg_div").bind("click",function(){$("div.opt_sel").hide();$("div.opt_sel").prev().removeClass("open_sel_");$("div#select_bg_div").remove();});
		};

	});

	opt.bind("click", function(){
	        $("div#select_bg_div").remove();
	        cont.find("div#"+sel.val()).removeClass("selected_val");
		sel.val($(this).attr("id"));
  		$(this).addClass("selected_val");
		sel.change();
		span.text($(this).text());
		$(this).parent().hide();
	});
}

function show_more(url) {
	$.post(url, function(d) {
		$(".catalog_list").append($(".catalog_list", d).html());
		$(".catalog .smtop").replaceWith($(".smtop", d));
		$(".catalog .h1_paginator").replaceWith($(".h1_paginator.bottom", d));
	});
}
function bind_soc_tabs(el){
	 el.bind("click",function(){
	      if(!el.hasClass("tabs_cur")){
		    $("div.soc_comments_tabs div.tabs").removeClass("tabs_cur");
		    el.addClass("tabs_cur");
		    $("div.tab_cont").addClass("hidden");
		    $("div#"+el.attr("id").substr(0,3)+"cont").removeClass("hidden");
	      }else{
	            return;
	      };
	 });
}
