function getAbsolutePosition(el) {
	var r = { x: el.offsetLeft, y: el.offsetTop };
	if (el.offsetParent) {
		var tmp = getAbsolutePosition(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}

	jQuery.fn.check = function(mode) {
	var mode = mode || 'on';
	return this.each(function() 
	{
	switch(mode) {
		case 'on':
			this.checked = true;
			break;
		case 'off':
			this.checked = false;
			break;
		case 'toggle':
			this.checked = !this.checked;
			break;
			}
		});
	};
	


/* Custom select */
(function($){

	$.fn.overlay=function() {
		var el=$(this);
		$('body').prepend('<div id="overlay"></div>');
		$('#overlay').click(function(){el.hide();$('#overlay').remove();});
		$('#overlay').show('slow');
		return this;
	}
	
	$.fn.custom_select = function()
	{
		$(document).mousedown(function(event)
		{
			if ($(event.target).parents('.custom_select').length === 0)
				$('.custom_select ul').hide();
			$('.custom_select .opener').removeClass('opener_act');
		});
		
		return this.each(function(index)
		{
			custom_select_init(this, index);
		});
	};

	var custom_select_init = function(element, index)
	{
		var select_elem = $(element);
		var select_w = select_elem.outerWidth();
			
		select_elem.wrap('<div class="custom_select_wrapper"></div>');
		var select_wrapper = select_elem.parent();
		select_wrapper.width(select_w);
		index = (index) ? index : 0;
		select_wrapper.css('zIndex',100-index);
		
		select_elem.after('<div class="custom_select"><div><span class="text"></span><span class="opener"></span></div><ul></ul></div>');
		var select_custom = $(element).siblings('.custom_select').width(select_w);
		var select_text = jQuery('.text', select_custom);
		var select_opener = jQuery('.opener', select_custom);
		select_text.width(select_w - select_opener.width() - parseInt(select_text.css('padding-left')) - parseInt(select_text.css('padding-right')));
		
		ul = $('div', select_custom).siblings('ul');
		ul.width(select_w-2);
		
		if( jQuery.browser.msie && jQuery.browser.version < 7 )
		{
			select_elem.after('<iframe src="javascript:\'\';" marginwidth="0" marginheight="0" align="bottom" allowTransparency="true" scrolling="no" tabIndex="-1" frameborder="0"></iframe>');
			select_elem.next().css({height:select_elem.height()+4+'px', width:select_elem.width()+4+'px'}).animate({opacity: "0"}, "fast");
		}
		
		custom_select_update(element);
		
		$('div', select_custom).click(function()
		{
			ul = $(this).siblings('ul');
			if (ul.css('display')=='none') {
				$('.custom_select ul').hide()
				//$('.custom_select .opener').addClass('opener_act');
				$(this).find('.opener').addClass('opener_act');
			} else {
				//$('.custom_select .opener').removeClass('opener_act');
				$(this).find('.opener').removeClass('opener_act');
			}
			ul.slideToggle(100);
			var offset = ($('a.selected', ul).offset().top - ul.offset().top);
			ul.animate({scrollTop: offset});
			return false;
		});
	};

	var custom_select_update = function(element)
	{
		var select_elem = $(element);
		var select_custom = select_elem.siblings('.custom_select');
		var $ul = select_custom.find('ul').find('li').remove().end().hide();
		var $opener = select_custom.find('.opener');
		$('option', select_elem).each(function(i){
			$ul.append('<li><a href="#" index="'+ i +'">'+ this.text +'</a></li>');
		});

		$ul.find('a').click(function()
		{
			$('a.selected', select_custom).removeClass('selected');
			$(this).addClass('selected');	
			if( select_elem[0].selectedIndex != $(this).attr('index') && select_elem[0].onchange )
			{
				select_elem[0].selectedIndex = $(this).attr('index');
				select_elem[0].onchange();
			}
			select_elem[0].selectedIndex = $(this).attr('index');
			$('span:eq(0)', select_custom).html($(this).html());
			$ul.hide();
			$opener.removeClass('opener_act');
			return false;
		});

		$('a:eq('+ select_elem[0].selectedIndex +')', $ul).click();
	};

	$(function()
	{
		$('select.custom_select').custom_select();
	});
})(jQuery);

jQuery(document).ready(function(){

	/*jQuery('div.ind_top_div a.ent, .auth_user a.enter').click(function(){
		jQuery('#auth_pp').attr('style','').show().overlay();
		return false;
	});
	
	jQuery('.copy a.auth').click(function(){
		jQuery('#auth_pp').css('margin-left','0').show().overlay().centering(1,1);
		jQuery('#auth_pp #auth_login').focus();
	});
	
	jQuery('#auth_pp .text_input input').focus(function () {
		jQuery(this).parent().parent().parent().addClass('ti_act');
	});
	
	jQuery('#auth_pp .text_input input').blur(function () {
		jQuery(this).parent().parent().parent().removeClass('ti_act');
	});

	jQuery('#tarif_calc').click(function(){
		if (jQuery(this).parents('.br_block').find('.lb_tarif').css('display')=='none') {
		jQuery(this).parents('.br_block').find('.text_block').slideToggle(300, function() {
			jQuery(this).parents('.brb_0').addClass('brb_no_bg');
			jQuery(this).parents('.br_block').find('.lb_tarif').slideToggle(1000);
		  });
		}
		return false;
	});
	
	jQuery('.tarif_block .line_vkladki ul li').click(function(){
		jQuery(this).siblings().removeClass('active').end().addClass('active');
		jQuery('.tarif_block .cont').hide().eq(jQuery('.tarif_block .line_vkladki ul li').index(this)).show();
		return false;
	});

	jQuery('.line_tabs ul li').click(function(){
		jQuery(this).siblings().removeClass('gray').end().addClass('gray');
		jQuery(this).parents('.cont').find('.tab_cont').hide().eq(jQuery('.line_tabs ul li').index(this)).show();
		return false;
	});*/
	
	jQuery('.line_tabs .top_level li').click(function(){
		if (!jQuery(this).hasClass('ico_prof')) {
			jQuery(this).siblings().removeClass('active').removeClass('ico_prof_act').end().addClass('active');
			jQuery('div.tab_cont').show();
			jQuery('.line_tabs .tab').hide().eq(jQuery('.line_tabs .top_level li').index(this)).show();
		} else {
			jQuery(this).siblings().removeClass('active').end().addClass('ico_prof_act');
			jQuery('div.tab_cont').hide();
			return true;
		}
		return false;
	});
	
	jQuery('table.subscribe_subs tr.show_all a').toggle(
		function(){ jQuery('tbody.subs_hd').toggle(); jQuery(this).text('скрыть'); return false; },
		function(){ jQuery('tbody.subs_hd').toggle(); jQuery(this).text('посмотреть весь список'); return false; }
	);
	
	
	jQuery('#subs_09').click(function(){
		if (jQuery(this).attr('checked')) {
			jQuery('table.subs_country input').attr('checked','checked');
			jQuery(this).attr('checked','checked');
		} else {
			jQuery('table.subs_country input').removeAttr('checked');
			jQuery(this).removeAttr('checked');
		}
		return true;
	});

    jQuery('.close_pp,.popup .close').click(function(){
		jQuery(this).parents('.popup').hide();
		jQuery('#overlay').remove();
		return false;
	});
	jQuery('.tpassport-login .enter').click(function(){
		jQuery("#pp-login").overlay().css({left:getAbsolutePosition(this).x-188}).show();
		return false;
	});

    jQuery('#auth_form #btn_submit').click(function(){
        jQuery.post('/auth/login', jQuery("#auth_form").serialize(), function(data) {
            if (data.error != undefined) {
                if (data.error.message != '') {
                    alert(data.error.message);
                } else if (data.error.text) {
                    alert(data.error.text);
                }
                return false;
            }
            if (data == 'ok') {
                jQuery("#auth_form #check").val(0);
                jQuery("#auth_form").submit();
            }
        }, "json");
        return false;
    });

});
