var indexPromoAnim=false;

$(document).ready(function ()
{
	jQuery.ifixpng('/i/s.gif');
	//jQuery('img').ifixpng();	
	//jQuery('div').ifixpng();
	//jQuery('a').ifixpng();
	jQuery('.png').ifixpng();
	

	//menu
	$(".menu").mouseenter(function(){
		$(this).css({ "cursor":"pointer" });
		$(this).addClass("menu-active");
	}).mouseleave(function(){
		$(".menu").removeClass("menu-active");
	}).click(function(e){
		e.preventDefault();		
		$(this).addClass("menu-active");
		window.location.href=$(this).find("a").attr("href");
	});	
    
    $(".photo-small a").hover(
	  function () {
	    $(this).addClass("active");
	  },
	  function () {
	    $(this).removeClass("active");
	  }
	);	

	//branches
     $(".district a").click(function(e){
        e.preventDefault();
        $("#adr").html("");

        if(document.getElementById("_flashmap") && typeof document.getElementById("_flashmap").showRegion == 'function')
        	document.getElementById("_flashmap").showRegion($(this).attr("code"));

        $(".district a").removeClass("select");
        $(this).addClass("select");
        $.ajax( {
            type: 'POST',
            url: $("#get_cities_list").val(),
            data: "reg=" + $(this).attr("rel"),
            success: function(data)
            {
                $("#adf").html(data);
            }
        } );            
     });
     
     $(".city-one a").live('click', function(e) {
        e.preventDefault();

        $(".city-one a").removeClass("select");
        $(this).addClass("select");
        $.ajax( {
            type: 'POST',
            url: $("#get_departments").val(),
            data: "reg=" + $(this).attr("rel"),
            success: function(data)
            {
                $("#adr").html(data);
            }
        } );            
     });
     
     $(".region_area.selected").trigger("click");
     $(".place-on-map").live('click', function(e){
        e.preventDefault();
        showMap($(this).attr("rel_x"),$(this).attr("rel_y"), $(this), $(this).attr("rel_zoom"));
     });
	//branches    
              
	
	
	
	
    $(".main-image").click(function()
    {  
        document.location = $(this).find(".layer .active a").attr("href");
    });
    
    $(".show a").click(function(e)
    {  
        e.preventDefault(); 
        $lang = "";
        if($("#lang").val()=="eng") $lang = "/eng";
        document.location = $lang+"/branch-cashpoint/"+$(".city").val()+".html";
    });    
    
    $(".main-image").mouseenter(function()
    {  
        $(this).css("cursor","pointer");
    });
    
    $(".main-image").mouseleave(function()
    {  
        $(this).css("cursor","");
    });
        
    $('.fl .pages a').click(function(){
        var curMarginLeft =  0-$(this).index()*244 ;

        $('.gallery>div').stop(true).animate({
            marginLeft: curMarginLeft+"px"
        }, 'fast' );
        
        $('.fl .pages a').removeClass('active');
        $(this).addClass('active');
         
        return false;
    });
    
    
    $('.photo-small a').click(function(){
        
        $('.photo-big img').fadeTo('fast', 0.5)
        $('.photo-big').append('<img class="preloader" src="/i/ajax-loader.gif">');
        
        
        var thumbImageSrc = $(this).parent().css('background-image').replace(/"/g, '').slice(4, -1);
        $('<img />')
        .attr('src', thumbImageSrc.replace('x66', '454x') )
        .load(function(){
            $('.photo-big').html( $(this) );
        });   
            
        $('.photo-small a.current').removeClass('current');
        $(this).addClass('current');
        return false;
    })
    
    $(".slide_name").css("color","#221F7C");
	$(".slide").hide();
	$(".slide_name").click(function () { 
		$(".slide").slideUp();
		$(".slide_name").css("color","#221F7C");
		$(this).css("border-bottom","none");
		$(this).next("div.slide").slideDown();
	});
    
   
   //START on main promoblock
        $('.main-image .arrow-right').click(function(e){
            e.preventDefault();
            
            
                $curPromoIndex = $('.promo-pages>a.active').index();
                
                //console.log($curPromoIndex+1);
                
                //~ console.log($('.promo-pages a').length);
                if( $curPromoIndex+1 >= $('.promo-pages>a').length )
                    goIndexPromo(0);
                else
                    goIndexPromo($curPromoIndex+1);
            
        })
        $('.main-image .arrow-left').click(function(e){
            e.preventDefault();
            
            $curPromoIndex = $('.promo-pages>a.active').index();
            
            //if($('.promo-pages a.active').index() == $('.promo-pages a').length() )
            if( $curPromoIndex-1 < 0)
                goIndexPromo( $('.promo-pages>a').length-1 );
            else
                goIndexPromo($curPromoIndex-1);
                
            //~ console.log($curPromoIndex-1);
            //~ goIndexPromo($curPromoIndex-1);
        })
        $('.promo-pages>a').click(function(e){
            e.preventDefault();
            e.stopPropagation();
            goIndexPromo( $(this).index() );

            clearInterval($nextPromoSlide);
        })
        
        $nextPromoSlide = setInterval( "goNextPromo()", 4000 );   

   //END on main promoblock
   
   
  //START our values      
        $nextOurValues = setInterval( "changeOurValuesBanners()", 4000 );   
    $('.arrow-left-ourvalues, .arrow-right-ourvalues, .pages-ourvalues a').click(function(e){
        e.preventDefault();
    })
    $('.arrow-right-ourvalues').click(function(){
        changeOurValuesBanners();
    })
    $('.arrow-left-ourvalues').click(function(){
        $curActiveValuesL = $('#banner-ourvalues').find('.active');
        if( $curActiveValuesL.index()<=0 )
        {
            goToChOurValuesBanner( ($('#banner-ourvalues div').length-2) );
        }
        else
        {
            goToChOurValuesBanner($curActiveValuesL.index()-1);
        }
    })
    $('.pages-ourvalues a').click(function(){
        
        goToChOurValuesBanner($(this).index());
    })
  //END our values    
  
    
    
    
});

function goNextPromo()
{
    $curPIn = $('.promo-pages>a.active').index();
    
    //TODO: make this check in  goIndexPromo()
    if( $curPIn+1 >= $('.promo-pages>a').length )
        goIndexPromo(0);
    else
        goIndexPromo($curPIn+1);

}

function goIndexPromo(index)
{
    
    if(indexPromoAnim!=true){
        indexPromoAnim=true;
        
        $('.promo-pages>a.active').removeClass('active');
        $('.promo-pages>a:eq('+index+')').addClass('active');
        
        
        $('.main-image .descr.active').hide().removeClass('active');
        $('.main-image .name.active').hide().removeClass('active');
        
        $thisLair = $('.main-image .layer');

        if($.browser.msie)
        	$thisLair.css("top", "300px");
        else
	        $thisLair.stop(true, true).animate({top: '300px'}, 'slow');
        
        $thisShadow = $('.main-image .shadow');

        if($.browser.msie)
	        $thisShadow.css("height", "0px");
        else
    	    $thisShadow.stop(true, true).animate({height: '0px'}, 'slow');
        
        if($.browser.msie)
	        $('.main-image .picture.active').hide().removeClass('active');
	    else
	        $('.main-image .picture.active').stop(true, true).fadeOut("slow").removeClass('active');
	    
        $('.main-image .picture:eq('+index+')').stop(true, true).fadeIn(($.browser.msie?0:"slow"), function(){
            
            $('.main-image .descr:eq('+index+')').show().addClass('active');
            $('.main-image .name:eq('+index+')').show().addClass('active');
            
	        if($.browser.msie)
	        {
    	        $thisShadow.css("height", "110px");
        	    $thisLair.css("top", "80px");
        	    indexPromoAnim=false;
	        }
	        else
	        {
    	        $thisShadow.stop(true, true).animate({height:'110px'}, 'slow');
        	    $thisLair.stop(true, true).animate({top:'80px'}, 'normal', function(){
	                indexPromoAnim=false;
	            });
            }
        
            
        })
        .addClass('active');
    }
    
}


function changeOurValuesBanners()
{
        $curActiveValuesL = $('#banner-ourvalues').find('.active');
        if( $curActiveValuesL.index()==($('#banner-ourvalues div').length-2) )
        {
            goToChOurValuesBanner(0);
        }
        else
        {
            goToChOurValuesBanner($curActiveValuesL.index()+1);
        }
}
function goToChOurValuesBanner(index)
{
            $('#banner-ourvalues .active').removeClass('active').stop(true,true).fadeOut();
            $('#banner-ourvalues').children('div:eq('+index+')').stop(true,true).fadeIn().addClass('active');
            $('.pages-ourvalues .active').removeClass('active');
            $('.pages-ourvalues').children('a:eq('+index+')').addClass('active');

}


function changeRegion() 
{
    $('#city_select').css("display","");
    var params = 'reg='+$('#reg').val();
    $.post("/get_cities.html", params,
          function(data){
                $("#citySelect").html(data);
          });
}


function showMap(x,y, item, mapzoom)
{
    //google.load("maps", "2.x");
    //google.setOnLoadCallback(initialize(x,y,item));
    var latlng = new google.maps.LatLng(x,y);

    var options = {
      zoom: parseInt(mapzoom),
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }; 

    
    $(".map").css("display", "none");
    $map_div = item.closest("tr").next().next().find(".map");
    //setTimeout('map_div.css("display", "block")',5000);

    item = $map_div.get(0);
    item.style.display = "block";
    var map = new google.maps.Map($map_div.get(0), options);

    var marker = new google.maps.Marker({
       position: latlng, 
       map: map 
    });
    
    //map_div.css("display", "block")
    //alert(jQuery.browser);
    //$map_div.css("display", "block");
    /*if($.browser.msie) 
    {
        setTimeout('show_map_div()',3000);
    }
    else $map_div.css("display", "block");
    */
    
}



function show_map_div()
{
    $map_div.css("display", "block")
}

function showRegionJs(id)
{
	$('.region_area[code="'+id+'"]').trigger("click");
}

/*function initialize(x,y, item) 
{
    $(".map").css("display", "none");
    $map_div = item.closest("tr").next().next().find(".map");
        
    var map = new google.maps.Map2($map_div.get(0));
    map.setCenter(new google.maps.LatLng(x, y), 13);
    show_map_div();
}*/


