$(function() {

	$("a[rel=example_group]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Изображение ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	$(".zoom").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Изображение ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});

	var flashvars = {};
	var params = {};
	params.loop = "true";
	params.wmode = "transparent";
	var attributes = {};
	swfobject.embedSWF("/img/logo.swf", "idFlash", "42", "43", "8.0.0", "expressInstall.swf", flashvars, params, attributes);
    swfobject.embedSWF("/img/ru/Clock_ru.swf", "idFlash-ru", "246", "62", "8.0.0", "expressInstall.swf", flashvars, params, attributes);
	swfobject.embedSWF("/img/en/Clock_en.swf", "idFlash-en", "246", "62", "8.0.0", "expressInstall.swf", flashvars, params, attributes);


    $("a.zoom").each(function(){
		if ($(this).find('span').length > 0) {
		   
		} else {
		   $(this).append($('<span class="s">&nbsp;</span><span class="f">&nbsp;</span>'));
		}
	});


	
	

    $('.tab')
    .mouseover(function(event){
        var $this = $(this);
        $this.data('mouseover', true);
        if(!$this.data('submenuId')){
            $this.data('submenuId', $(this).attr('rel'));
        }
			
        if($this.data('submenuId')){
            var offset = $(this).parent('li').position();
            var offset1 = $(this).parents('ul:first').position();
            $('#' + $this.data('submenuId')).fadeIn(150);
        }
    })
    .mouseout(function(event){
        var $this = $(this);
        $this.data('mouseover', false);
        if($this.data('submenuId')){
            setTimeout("$('#" + $this.data('submenuId') + "').hideSubmenu();", 500);
        }
    });
    $('ul.inners-menu')
    .mouseenter(function(event){
        $(this).data('mouseover', true);
        $('.tab[rel="'+ $(this).attr('id') +'"]').addClass('actives');
        
    })
    .mouseleave(function(event){
        $(this).data('mouseover', false);
        setTimeout("$('#" + $(this).attr('id') + "').hideSubmenu();", 500);
    });
	
    $.fn.hideSubmenu = function(){
        if(!$(this).data('mouseover') && !$('.tab[rel='+$(this).attr('id')+']').data('mouseover')){
            this.fadeOut(150);
            $('.tab[rel="'+ $(this).attr('id') +'"]').removeClass('actives');
        }
    };
    
    $.fn.ajaxForm = function(options)
	{
		var settings = {};			

		return this.each(function(){
			if (options) { 
				$.extend(settings, options);
			}

			var $form = $(this);
			var $loadingIcon = $('<div class="loadingIcon"><img src="/img/loading.gif" /></div>');
			
			$form.find('.captchaButton').click(function(event){
				event.preventDefault();
				reloadCaptcha();
			});
			
			$form.find('input[type="submit"]').after($loadingIcon);
			$loadingIcon.hide();
			$form.submit(function(event){
				event.preventDefault();
					
				$loadingIcon.show();
				var data = $form.serialize();
				$form.find('input, textarea, select').attr('disabled', 'disabled');
				$.post($form.attr('action'), data, function(data){
					$loadingIcon.hide();
					if(data.ok){
						$form.find('input[type="text"], textarea').val('');
						$form.find('select option:first').attr('selected', 'selected');
						$form.parent().find('div.message').show();
						$form.hide();
					} else if(data.errors) {
						var errors = [];
						for(var p in data.errors){
							errors.push(data.errors[p]);
						}
						if(errors.length){
							alert(errors.join("\n"));
						}
					}

					$form.find('input, textarea, select').removeAttr('disabled');
				}, 'json');
			});
		});
	}
});

function reloadCaptcha(){
	$captchaImage = $('#icp');
	if($captchaImage.length){
		$captchaImage.attr('src', captchaUrl + '?' + Math.floor(Math.random()*10));
	}
}

