$(document).ready(function(){

$('.networks-branches').colorbox({iframe:true, width:"80%", height:"80%"});
$('.feedback-colorbox').colorbox();

	/*		
      zindex = 0;
      hiConfig = {
      sensitivity: 3, 
      interval: 200,
      timeout: 200,
      over: function() {
	    zindex = jQuery(this).css('z-index');
	    jQuery(this).find('.tooltip').delay(500).fadeIn('fast');
	    jQuery(this).stop().css({'cursor':'pointer','position:':'absolute','z-index':10});
      },
      out: function() {
	    $(this).stop().find('.tooltip').fadeOut('fast');
	    $(this).stop().css({'position:':'absolute','z-index':0});
      }
      }
      $("div.products ul li, div.products .item").hoverIntent(hiConfig);
      */
	
      $('.branch li a').click(function(){
	    if ($(this).parent().find('div.branch-info').css('display') == 'none') {
		  $(this).parent().find('div.branch-info').slideDown('fast');
	    } else {
		  $(this).parent().find('div.branch-info').slideUp('fast');
	    }
      });
      
      //init top menu
      $("#top-menu").jmenu();
      $(".container-about-menu ul.about-menu").jmenu();

      //init corners
      $("#doc").corner("30px");

      //menu
      $("#hd ul#top-menu li ul").corner("bottom 15px");
      $("div.special-programs .header").corner("tr 15px");
      $("div.special-programs .container").corner("right bottom 15px");

      $("div.tooltip .rounder").corner("round 15px").parent().css('padding', '4px').corner("round 15px");
      $(".sub-menu ul, .product-actions, .form").corner("15px");
      $(".product-actions li").corner("5px");
      $(".selected-box-corner").append('<div class="tl"></div><div class="tr"></div><div class="bl"></div><div class="br"></div>');
      $(".city-box p").corner("round 5px").parent().css('padding', '1px').corner("round 5px");
      $(".cities .rounder").corner("round 5px").parent().css('padding', '1px').corner("round 5px");


      //init carousel
      /*
      $('div.carousel').cycle({
	    fx: 'fade',
	    time: 6000,
	    speed: 1000
      });*/


      //init hover top menu
      if ($("ul#top-menu .tm-first").attr('class') != 'first-active' && $("ul#top-menu .tm-first").next('li').attr('class') != 'active') {
	    $("ul#top-menu .tm-first").hover(function(){
			$(this).next('li').addClass('active');
			$(this).addClass('first-active');
	    }, function() {
			$(this).next('li').removeClass('active');
			$(this).removeClass('first-active');
	    });
      }
      if ($("ul#top-menu .tm-first").next('li').attr('class') != 'active') {
	    $("ul#top-menu .tm-first").next('li').hover(function(){
			$("ul#top-menu .tm-first").addClass('first-active');
	    }, function() {
			$("ul#top-menu .tm-first").removeClass('first-active');
	    });
      }
      if ($("ul#top-menu .tm-first").next('li').attr('class') == 'active') {
	    $("ul#top-menu .tm-first").addClass('first-active');
      }

      if ($("ul#top-menu .tm-last").attr('class') != 'last-active' && $("ul#top-menu .tm-last").prev('li').attr('class') != 'active'){
	    $("ul#top-menu .tm-last").hover(function(){
			$(this).addClass('last-active');
			$(this).prev('li').addClass('active');
	    }, function() {
			$(this).removeClass('last-active');
			$(this).prev('li').removeClass('active');
	    });
      }
      if ($("ul#top-menu .tm-last").prev('li').attr('class') != 'active'){
	    $("ul#top-menu .tm-last").prev('li').hover(function(){
			$("ul#top-menu .tm-last").addClass('last-active');
	    }, function() {
			$("ul#top-menu .tm-last").removeClass('last-active');
	    });
      }
      if ($("ul#top-menu .tm-last").prev('li').attr('class') == 'active') {
	    $("ul#top-menu .tm-last").addClass('last-active');
      }


      /*var branch_id = 133;
      var branchIsLoaded = false;*/

      /* change city */
      $('.city-box img').click(function(){
      if($('div.cities').css('display') == 'none') {
      if (!branchIsLoaded) {
	    $.ajax('ru/ajax/zagruzka_spiska_filialov', {
		  success : function (data) {
		  branchIsLoaded = true;
		  $('div.cities div.rounder').html(data);		        
		  $('.cities li a').click(function(){
			city = $(this).html()
			$('.city span').html(city);
			$('div.cities').fadeOut();
			id = $(this).parent().attr('id');
			$('.cities li').removeClass('active');
			$(this).parent().addClass('active');
			branch_id = parseInt(id.replace(/branch-(.+)/, "$1"));
			$.ajax('ru/ajax/branch', {
			type: "POST",
			data: "branch_id=" + branch_id,
			success : function (data) {
			branch = $.parseJSON(data);
			$('p.branch a').html(branch.name);
			$('p.address').html(branch.address);
			$('.phone').html(branch.phone);
			$('.timework').html(branch.timework);
			$.cookie("sblbranchid", branch_id, {path: '/'});
			
			}
			});
		  });
		  
		  },
		  error: function () {
		  $('div.cities').fadeOut();
		  }
	    });
	    $('div.cities').fadeIn();
      } else {
	    $('div.cities').fadeIn();
      }
      } else {
	    $('div.cities').fadeOut();
      }
      });

      
      branchInfo = function () {
	    branch = null;
	    $.ajax('ru/ajax/branch', {
	    type: "POST",
	    data: "branch_id=" + parseInt(branch_id) + "&branchInfo=1",
	    success : function (data) {
		  branch = $.parseJSON(data);
		  $('.branch-info h2').html('г. ' + $('.city').html());
		  $('.branch-region').html(branch.region);
		  $('.branch-director').html('<strong>Директор филиала:</strong><br />' + branch.director);
		  $('.branch-contacts').html('<strong>Контакты:</strong><br />основной тел.: ' 
						+ branch.phone + (branch.altphone ? '<br >доп. тел.: ' + branch.altphone : '') 
						+ (branch.fax ? '<br />факс: ' + branch.fax : '')
						+ (branch.email ? '<br />e-mail: <a href="mailto:'+branch.email+'">'+branch.email+'</a>' : '')
						);
		  $('.branch-address').html('<strong>Адрес филиала:</strong><br />' + ((branch.address != '') ? branch.address : 'временно отсутствует'));
		  if(branch.content != '') $('.branch-content').css('display','block').html(branch.content);
		  else $('.branch-content').css('display','none');
		  $.colorbox({inline:true, href:"#branch-info"});
	    }
            });
     }

//branch map
//$('.branch-map a').click(function(){
//if ($(this).parent().find('div').css('display') == 'block') {
//	$(this).html('Скрыть карту');
//} else {
//	$(this).html('Показать на карте');
//}
//});
branchShowMap = function (target, coordinates) {
	if ($(target).css('display') != 'block') {
	  var regex = /(\d+.\d+),(\d+.\d+)\|(\d+)/gi;
	  geo = regex.exec(coordinates);
	  latitude = geo[1];
	  longitude = geo[2];
	  zoom = geo[3];
	  var map = new YMaps.Map($(target));
	  
	  map.setCenter(new YMaps.GeoPoint(latitude, longitude), zoom);
	  map.addControl(new YMaps.Zoom());
          map.addControl(new YMaps.ToolBar());

	  var s = new YMaps.Style();
	  s.iconStyle = new YMaps.IconStyle();
	  s.iconStyle.href = "http://www.sberleasing.ru/images/sberbank.png";
	  s.iconStyle.size = new YMaps.Point(26, 27);
	  s.iconStyle.offset = new YMaps.Point(-9, -29);
	  
	  var placemark = new YMaps.Placemark(new YMaps.GeoPoint(latitude, longitude), {style:s});
	  placemark.description = $(target).parent().parent().find('p.branch-address').html();
	  map.addOverlay(placemark);
$(target).slideDown('fast', function(){
	$(target).parent().find('a.show-map').html('Скрыть карту');
});

} else {
	$(target).slideUp('fast', function(){
	$(this).css('display','none');
});
$(target).parent().find('a.show-map').html('Показать на карте');
}
    
}
	
});
