$(document).ready(function(){
    
    /* main.html */
    var grad = [[143,192,117],[41,104,159],[154,118,188]];
    $('h1.grad').each(
        function(k,el) {
            var text = $(el).text();
            var text2 = '';
            for(i=0;i<text.length; i++) {
                var e = (i/text.length * (grad.length-1));
                var a = parseInt(e);
                if (text.charAt(i)!= ' ')
                text2 += '<span style="color:rgb('
                      +parseInt(grad[a][0]-(grad[a][0]-grad[a+1][0])*(e-Math.floor(e)))
                      +','
                      +parseInt(grad[a][1]-(grad[a][1]-grad[a+1][1])*(e-Math.floor(e)))
                      +','
                      +parseInt(grad[a][2]-(grad[a][2]-grad[a+1][2])*(e-Math.floor(e)))
                      +')">' + text.charAt(i) + '</span>';
                 else text2 += ' ';
            }
            $(el).html(text2);
        }
    );
    

    $('a.hidden_next').click(function(){
       $(this).parent().hide().nextUntil('div.prefooter').show();
       return false;
    }).parent().nextUntil('div.prefooter').hide();
    
    $('#services h2').each(function(k,el){
        //$($(el).next()).prepend($('<h2>'+$('a',el).html()+'<h2>'));
    });
    
	// top-menu
    $('.shadow').html(function() {
         return '<div class="shadowbox-wrap"><div class="shadowbox-outer">'+
                '<div class="shadow-bg shadow-bg-n"></div><div class="shadow-bg shadow-bg-ne"></div>'+
                '<div class="shadow-bg shadow-bg-e"></div><div class="shadow-bg shadow-bg-se"></div>'+
                '<div class="shadow-bg shadow-bg-s"></div><div class="shadow-bg shadow-bg-sw"></div>'+
                '<div class="shadow-bg shadow-bg-w"></div><div class="shadow-bg shadow-bg-nw"></div>'+
                '<div class="shadowbox-content">'+$(this).html() + '</div></div></div>';
        });
    
    $('#top-first-menu > li ').mouseover(function(){
        $('div.sub-menu',this).show();
    }).mouseleave(function(){
        $('div.sub-menu',this).hide();
    });
    
    $('#services > div ').mouseover(function(){
        $('div.shadow',this).show();
        $('div.shadow .shadowbox-content',this)
            .css('padding-top',$('h2',this).height() +25);
        $('h2',this).addClass('black');
    }).mouseleave(function(){
        $('div.shadow',this).hide();
        $('h2',this).removeClass('black');
    })
    
    // Сделаем окно для входа в биллинг
	//$('#billing a').overlay();
	$('#billing a.billing-link').fancybox({
		'transitionIn' : 'none',
        'transitionOut' : 'none',
        'overlayColor':'#DDD',
        'showNavArrows': false,
		'scrolling': 'no',
	    'titleShow': false,
		'href': '#billing-login',
		'onStart': function(){
			//покажем форму
			$('#billing-login').show();
		},
		'onClosed': function(){
			//спрячем форму
            $('#billing-login').hide();
		}
	});
	
	// По нажатию на кнопку входа в биллинг залогиниваемся
	$('#billing-login .submit-button').click(function(){
		$('#billing-login form').submit();
	});
    
    /* arguments.html */
   $arguments = $('#arguments');
   if($arguments.length) {
      
       $('div.text > *',$arguments).hide();
       
       var anchor = document.location.hash;
       anchor = anchor.replace('#','');
       
       if (anchor == '') {
           $('div.text a.anchor:eq(0)', $arguments).nextUntil('a.anchor').show();
           $('div.menu a.dotted_border:eq(0)',$arguments).parent().addClass('selected');
       }
       else {
           $('div.text a.anchor[name=' + anchor + ']', $arguments).nextUntil('a.anchor').show();
           $('div.menu a.dotted_border[href=#'+anchor+']',$arguments).parent().addClass('selected');
       } 
       
       $('div.menu a.dotted_border',$arguments).click(function() {
           $('div.menu li',$arguments).removeClass('selected');
           $(this).parent().addClass('selected')
           $('div.text > *',$arguments).hide();
           var anchor = $(this).attr("href");
           
           anchor = anchor.replace('#','');
           $('div.text a.anchor[name='+anchor+']',$arguments).nextUntil('a.anchor').show();
           document.location.hash='#'+anchor;
       }); 
   }
   
    /* Общая информация */
	$('#letter_opener').fancybox({
	    'transitionIn' : 'none',
        'transitionOut' : 'none',
        'overlayColor':'#DDD',
        'showNavArrows': false,
        'scrolling': 'auto',
        'titleShow': false,
        'href': $(this).attr('href'),
        'autoDimensions': false,
        'width': 900,
        'height': 400
	});
   
   /* about2.html */
   /* fancybox-content */
   $("#virtual_tour a[rel=vtour]").fancybox({
        'transitionIn' : 'none',
        'transitionOut' : 'none',
        'overlayColor':'#DDD',
        'padding': 65,  
        'showNavArrows': true,
        'onStart':function(){
            $('body').addClass('virtual_tour');
        },
        transitionIn:'none', transitionOut:'none',
		easingIn:'none', easingOut:'none',
		changeSpeed:0,
		speedIn:0, speedOut:0,
        onClosed:function(){
            $('body').removeClass('virtual_tour'); 
        },
        'titlePosition': 'inside',
        'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
            if(currentIndex==0) $('#fancybox-left-ico').hide();
            else $('#fancybox-left-ico').show();
            return '<div class="x-large" style="padding:15px 0"><b>' +this.orig.html() +'</b></div>';
        }
    });

    $('#virtual_tour_open').click(function(){
        $("#virtual_tour a:eq(0)").click();
        return false;
    });
    
    /* about2.html */
    $('div.content.faq h3').each(function(){
        var $capt = $(this);
        $capt.wrapInner('<a href="#" class="dotted_border" />');
        //$capt.nextUntil('h3').hide();
        $('a', this).addClass('black');
        $('a',this).click(function(){
            $(this).toggleClass('black');
            $capt.nextUntil('h3').toggle();
            return false;
        });
    });
    
    // развернуть все пункты
    $('.hide-unhide-all').click(function(){
    	$('div.content.faq h3 a').click();
    });
    
    /* Благотворительность */
    $('.charity-photos a').fancybox({
        'transitionIn' : 'none',
        'transitionOut' : 'none',
        'overlayColor':'#DDD',
        'padding': 65,  
        'showNavArrows': true,
        'onStart':function(){
            $('body').addClass('virtual_tour');
        },
        transitionIn:'none', transitionOut:'none',
		easingIn:'none', easingOut:'none',
		changeSpeed:0,
		speedIn:0, speedOut:0,
        onClosed:function(){
            $('body').removeClass('virtual_tour'); 
        },
        'titlePosition': 'inside',
        'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
            if(currentIndex==0) $('#fancybox-left-ico').hide();
            else $('#fancybox-left-ico').show();
            if(currentArray.length < 2){
            	$('#fancybox-left-ico').hide();
            	$('#fancybox-right-ico').hide();
            }
            return '<div class="x-large" style="padding:15px 0"><b>' +this.orig.html() +'</b></div>';
        }
    });
    
    // !!! оч. странное расположение элементов в DOM
    $('.fancybox-photo').click(function(){
    	$(this).next().find("a:eq(0)").click();
        return false;
    });
    
    // примечание для валют
    $('.notes [name="money-note"]').each(function(){
        // получим указаный индекс
        var index = $(this).parent().find('.index').html();
        index = index.substring(0,2)
        index = $.trim(index)
        // проставим индекс в метках валют
        $('a[href="#money-note"]').html(index);
    });
});

