$("html").addClass("js");
$(function() {
  $.fn.vAlign = function() {
    return this.each(function(i){
      var h = $(this).height();
      var oh = 150;      
      var mt = (oh - h) / 2;      
      $(this).css("padding-top", mt + "px");
    });	
  };
  
  $(".carousel ul li p").vAlign();
  
  $("#sidebar").accordion({initShow : ".nav-selected"});
  $("html").removeClass("js");
  $(".carousel").jCarouselLite({
    auto: 10000,
    speed: 100,
    visible: 1,
    btnPrev: ".invest .more #prev",
    btnNext: ".invest .more #next"
  });
  
  $("#spb-photos").jCarouselLite({
    auto: 10000,
    speed: 100,
    visible: 1,
    btnPrev: "#spb-photos #prev",
    btnNext: "#spb-photos #next"
  });
  
  //$("#spbphotosplaceholder").append($("#spb-photos"));
 
  
  $("div.toggle div").hide();  
  
  $("div.toggle div a.hide").click(function(){
    $(this).parent().parent().hide();
  });
  
  $("div.toggle h2 a").click(function(){
    $(this).parent().parent().find('div').toggle();
  });
   
  $(".map #wrap").click(function(){
    window.location.replace("http://st-petersburg.1895.ru/where");
  });
  
  $('.print a').click(function(){    
    $('head').append('<link rel="stylesheet" media="screen" type="text/css" href="/themes/st-petersburg/print.css">');
  });
  
  if ( window.location.hash == '#print' ){
    $('head').append('<link rel="stylesheet" media="screen" type="text/css" href="/themes/st-petersburg/print.css">');
  }
  
  $('#subscribe input[type="text"]').focusin(function(){
    $(this).val('');
  });
  
  $("td.ms-sbgo img").css("vertical-align","top");
  
  treeUpdater();
  
});


function handleSliderChange(e, ui)
{
  var maxScroll = $("#scroll").attr("scrollWidth") -
                  $("#scroll").height();
  $("#scroll").animate({scrollUp: ui.value *
     (maxScroll / 100) }, 1000);
}

function handleSliderSlide(e, ui)
{
  var maxScroll = $("#scroll").attr("scrollWidth") -
                  $("#scroll").height();
  $("#scroll").attr({scrollUp: ui.value * (maxScroll / 100) });
}

function closeallcharts()
{
	$.window.getSelectedWindow().close();
}

function menutab(a)
{
	$(".chart-tabs li").removeClass('active');
	$(a).addClass('active');
	$(a).parent().addClass('active');
}
function printversion()
{
$('head').append('<link rel="stylesheet" media="screen" type="text/css" href="/css/print.css">');
}

var maxChecksAllowed = 10;
var totalchecks = maxChecksAllowed;

function treeUpdater(){
	$(".statetreecontainer").find("input[type=checkbox]").each(function(){
		$(this).change(function() {
			var cbox = $(this);
  			if(this.checked)
  			{
  				if(totalchecks>0){
  					addRecordFromTree(cbox );					
  				}
  				else{
  					alert('Only ' + maxChecksAllowed + ' choises allowed' );
  					changeinput(cbox);
  				}
  				totalchecks =totalchecks -1;
  			}
   			else{
  				remRecordFromTree(cbox );
  				totalchecks=totalchecks+1;
  				if(totalchecks>totalchecks)
  					totalchecks=totalchecks;
  			}
  		});
  		if($(this).attr("checked"))
  		{
  			addRecordFromTree($(this));
  			var cbox = $(this);
  			if(totalchecks>0){
  				totalchecks =totalchecks -1;
  			}
  			else
  			{
  				changeinput(cbox);
  			}
  		}
  		
  		stTreeCheckerLabelToBeActive($(this));
  	});
}

function changeinput(inpt){
	if($.browser.msie)
	{
		inpt.click().click();   
		if(inpt.is(":checked"))
			inpt.attr('checked', false); 
		else
			inpt.attr('checked', true);     
	}
	else
	{
		inpt.click();
	}
}

function stTreeCheckerLabelToBeActive(cb){
	
	var span = cb.parent().find('span');
	span.click(function(){
		var inpt = $(this).parent().find('input');
		
		changeinput(inpt);
	});
	span.css('cursor','pointer');
}

function doChange(cb){
	cb.click();
}

function addRecordFromTree(cb){
	var id = cb.attr("id");
	
	var text = "<div class='treerecord' relid='"+id+"'><table><tr><td width='100%'><div class='treerecordtext'>" + cb.parent().find('span').html().trim() + "</div></td><td><div class=\"treerecordclose\" onClick=\"uncheckTree('"+id+"', this)\">X</div></td></tr></table></div>";
	
	$(".treeselection").append(text);
}

function remRecordFromTree(cb){
	var id = cb.attr("id");
	var selector = "div[relid="+id+"]";
	$(selector).remove();
}

function uncheckTree(relid, div){
	if(window.confirm('Delete?'))
	{
		var selector = "#"+relid;
		$(selector).attr("checked",false);
		selector = "div[relid="+relid+"]";
		$(selector).remove();
		totalchecks=totalchecks+1;
	}
}
