$(document).ready(function(){
	$('.corner-border').corner("10px");
	$('.submit_butt').removeAttr('disabled');
	
	$('body').click(function(){
		$('.menu-block').hide();
	});
	
	$('.menu-block').click(function(event){
		event.stopPropagation();
	});

    $('.checkbox').live('click', function(){
        $(this).toggleClass('active');
        $('input', this).change();
    });

    $('.favorites').live('click', function(){
       DLoader.Show();
       var btn = this;
       $.post($(this).attr('href'), function(response){
           $(btn).toggleClass('active');
           DLoader.Hide();
       });
       return false;
    });
    $('.favorites-del').live('click', function(){
       DLoader.Show();
       var data = {'update': 1};
       var pager = $('.pager', $(this).parents('.favorites-list'));
       if ($(pager).html() != null)
           data['page'] = $('a.active', pager).html();
       $.post($(this).attr('href'), data, function(response){
           try
           {
               response = $.parseJSON(response);
               if (response.html != undefined)
                $('.favorites-list').html(response.html);
           }catch(e){}
           DLoader.Hide();
       });
       return false;
    });
});

function submitFormLoader(form)
{
	$('.submit_butt').attr('disabled', 'disabled');
	$("#loading", form).show();
	form.submit();
}

function createBigPopup(name)
{
	var bpop = jQuery.PopupCenterBig.createPopup(name+'-popup');
	bpop.html($('#'+name).html());
}
function createBigPopup2(name)
{
	var bpop = jQuery.PopupCenterBig.createPopup(name+'-popup');
	bpop.html($('#'+name));
}

function openBigPopup(name)
{
	jQuery.PopupCenterBig.open(name+'-popup');
}

function closeBigPopup(name)
{
	jQuery.PopupCenterBig.close(name+'-popup');
}

function createSmallPopup(name)
{
	var spop = jQuery.PopupCenterSmall.createPopup(name+'-popup');
	spop.html($('#'+name).html());
}
function createSmallPopup2(name)
{
	var spop = jQuery.PopupCenterSmall.createPopup(name+'-popup');
	spop.html($('#'+name));
}

function openSmallPopup(name)
{	
	jQuery.PopupCenterSmall.open(name+'-popup');
	$('#__kp_'+name+'-popup').css({'z-index': 1001});
}

function closeSmallPopup(name)
{
	jQuery.PopupCenterSmall.close(name+'-popup');
}

function createDesirePopup(name)
{
	var bpop = jQuery.KitaDesirePopup.createPopup(name+'-popup');
	bpop.html($('#'+name).html());
}

function openDesirePopup(name)
{
	jQuery.KitaDesirePopup.open(name+'-popup');
}

function closeDesirePopup(name)
{
	jQuery.KitaDesirePopup.close(name+'-popup');
}

function createWinterContestPopup(name)
{
	var bpop = jQuery.KitaWinterContestPopup.createPopup(name+'-popup');
	bpop.html($('#'+name).html());
}

function openWinterContestPopup(name)
{
	jQuery.KitaWinterContestPopup.open(name+'-popup');
}

function closeWinterContestPopup(name)
{
	jQuery.KitaWinterContestPopup.close(name+'-popup');
}

function createWinterContestBigPopup(name)
{
	var bpop = jQuery.KitaWinterContestBigPopup.createPopup(name+'-popup');
	bpop.html($('#'+name).html());
}

function openWinterContestBigPopup(name)
{
	jQuery.KitaWinterContestBigPopup.open(name+'-popup');
}

function closeWinterContestBigPopup(name)
{
	jQuery.KitaWinterContestBigPopup.close(name+'-popup');
}

//----------------------------------- default tips text in input and textareas
(function($){
	$.fn.DefaultInputPasswordTips = function()
	{
		this.each(function(){
			var _this = $(this);
            if (_this.width() > 0)
            {
                var rel = $(this).attr('rel');
                if ( typeof(rel) == 'string' && rel.length > 0)
                {
                    var fake = $('<div>'+rel+'</div>').css({'position': 'absolute', width: _this.width(), 'text-align': 'center', 'display': 'none',
                            'font-size': _this.css('font-size'), 'color': _this.css('color'), 'font-family': _this.css('font-family'), 'white-space': 'nowrap'});
                    var parent = $($(_this).parents('div').get(0)).css('position', 'relative');
                    var pOffset = parent.offset();
                    var tOffset = _this.offset();
                    parent.append($(fake).css({top: tOffset.top-pOffset.top+2, left: tOffset.left-pOffset.left}));

                    var showFake = function(){ fake.show(); }
                    var hideFake = function(){ fake.hide(); }
                    if ( $(this).val().length == 0 ) showFake();
                    $(this).
                        bind('focus', function(){ hideFake(); }).
                        bind('focusout', function(){ if ( $(this).val().length == 0 ) showFake(); }).
                        bind('change', function(){ hideFake(); });
                    $(fake).click( function(){ _this.focus(); });
                }
            }
		});
	}

	$.fn.DynamicBlock = function(text, options)
	{
		var $this = this;
		var is_animation = $this.attr('rel');
		if ( !(typeof(is_animation) != 'undefined' && is_animation != '') )
		{
			var nowAction = ($this.css('display') == 'block') ? 'none' : 'block';
			$this.attr('rel', 'animate');
			var changeText = function()
			{
				$this.stop();
				var relText = $(text).attr('rel');
				var curtext = $(text).html();
				$(text).html(relText);
				$(text).attr('rel', curtext);
				$this.attr('rel', '');
			}

			if ( nowAction == 'block' )
			{
				$this.slideDown('fast', function(){ changeText(); if ( typeof(options) == 'object' && typeof(options.show) == 'function' ) options.show(); });
			}
			else
			{
				if ( typeof(options) == 'object' && typeof(options.hide) == 'function' ) options.hide();
				$this.slideUp('fast', function(){ changeText();  });
			}
		}
	}
})(jQuery);

$(document).ready(function(){
	setTimeout(function(){
		$('input[type=text][rel], input[type=password][rel], textarea[rel]').DefaultInputPasswordTips();
	}, 100);
});

function showSubMenu(_this)
{
    $('.menu-block', $(_this).parent()).slideDown('fast');   
}

function setScroll()
{
	$('.scroll:visible').customScroller();
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function removeBadTags(str)
{
    return trim(str.replace(/<script([\s\S]*)<\/script>|<style([\s\S]*)<\/style>|<!--([\s\S]*)-->/gi, ''), '');
}

function PostDesire(button)
{
    if ($('#desire_text').val().trim().length > 0)
    {
        var parent = $(button).parents('.form');
        var loader = $(parent).find('.loader img');
        $(loader).toggle();
        $.post($(parent).attr('action'), {'desire': $('#desire_text').val()}, function(request){
            $('.user_desire').show();
            $(loader).toggle();
            closeDesirePopup('desire_popup');
            var data = eval("("+request+")");
            $('.desire_popup_ok-popup').find('h3').html(data.msg);
            openDesirePopup('desire_popup_ok');
        });
    }
}
