var $j = jQuery.noConflict();

var initMarginLeft;
var initMarginRight;

$j(function(){
	
	var $separator = $j("<LI class='separator'>|</LI>");
	$separator.insertAfter("#langContainer UL LI:not(:last)");
	
  $j("#left_sidebar UL.firstLevel > LI > UL.secondLevel")
    .parent("LI")
    .addClass("thereIsSecondLevel");
    
	$j("#left_sidebar UL.firstLevel > LI:not(.section):not(:last):not(.thereIsSecondLevel)").addClass("separator");
	
  
	$j("#mainMenu TD:not(.current) A").hover(
		function(){
			var $parent = $j(this).parent().parent();
			$parent.addClass("hover");
		},
		function(){
			var $parent = $j(this).parent().parent();
			$parent.removeClass("hover");
		}
	);
  
  
  if(window.location.href.indexOf("#") != -1){
    href = window.location.href;
    if($j("#faq_question_list").size() > 0){
      var index = href.lastIndexOf("#");
      id = href.slice(index+1);
      showAnswer(id,true);
    }
  }
  
  //console.log(jsI18N_Options);
  //jsLocalizer(jsI18N_Options);
  var $searchInput = $j("#SearchForm_SearchForm_Search");
  if($searchInput.size() > 0){
    if($searchInput.val() == searchDefaultText){
      $searchInput.addClass("sample");
    }
    $searchInput.click(function(){
      console.log("hgello " + searchDefaultText);
      if($j(this).val() == searchDefaultText){
        $j(this).val("");
        $j(this).removeClass("sample");
      }
    });
  }
  
  $j("#sitemap-list > LI > SPAN").each(function(){
    $j(this).next("UL").find("SPAN").addClass($j(this).attr("class"));
  });
  
  $j("#browser").treeview();
  $j("#sitemap-list").treeview();
  
  
  
  
  if($j.browser.msie && $j.browser.version < 7){
    $j(window).resize(function(){
      if($j(window).width() < 1000){
        $j("#middle_container").css("width","1000px");
      }else{
        $j("#middle_container").css("width","100%");
      }
    });
  }
  
});

function reconfigureMenuMargin(){
  var $mainMenu = $j("#mainMenu");
  var leftPos = $mainMenu.offset().left;
  
  //console.log(leftPos+" "+initMarginLeft+" "+(initMarginRight-(initMarginLeft-leftPos)));
  
  if(leftPos < 0){
    $mainMenu.css({
      marginLeft:initMarginLeft+"px",
      marginRight:"0px"});
  }else if( leftPos < initMarginLeft){
    $j("#mainMenu").css("margin-right",initMarginRight-(initMarginLeft-leftPos));
  }else{
    $j("#mainMenu").css({
        marginRight: initMarginRight+"px",
        marginLeft: initMarginLeft+"px"});
  }
}

function showAnswer(id){
  if($j("#"+id+" .answer:visible").size()>0) return;
  justShow = false;
  if(arguments.length >1){
    justShow = arguments[1];
  }
  
  if(!justShow){
    $j(".answer").slideUp();
    $j("#"+id+" .answer").slideDown();
  }else{
    $j("#"+id+" .answer").show();
  }
}

function jsLocalizer(options){
  var messages = options.messages;
  var where = options.where;
  //console.log(messages);
  //console.log(where);
  
  if(currLang == 'ru') return;
  if($j(where).size() == 0) return;
  
  this.replaceHtml = function(obj,key){
    html = obj.html().replace(key, messages[key][currLang]);  
    obj.html(html);
  }
  
  this.replaceValue = function(obj,key){
    value = obj.val().replace(key, messages[key][currLang]);  
    obj.val(value);
  }
  
  for(key in messages){
    var htmlObj = $j(where+" LABEL:contains("+key+")");
    if(htmlObj.size() > 0){
      replaceHtml(htmlObj,key);
    }
    
    var valueObj = $j(where+" INPUT[value="+key+"]");
    if(valueObj.size() > 0){
      replaceValue(valueObj,key);
    }
  }
};

