/**
 * @author Arturas Paleicikas <arturas.paleicikas@metasite.net>
 */

 var IndexSelfcare = {
	init: function(){
		this.initColoredBoxs();
	},

	initColoredBoxs: function(){
		$('#content')
			.hover(function(){}, function(){
				$('a div', this).removeClass('inactive');
			})
			.find('a')
			.hover(function(){
				$('#content a div').addClass('inactive');
				$('div', this).removeClass('inactive');
			}, function(){
				$('div', this).removeClass('inactive');
			});
	},

	initPopupLoginBox: function(params){
		var $IS = this;

		// default construct
		$IS.popupLogin(params);

		// construct on click
		$('#content a, #content input, #content select, #content button'
                + ', #content textarea')
			.click(function(){
				$IS.popupLogin(params);
				return false;
			});
	},

	// private
	popup: function(popupWidth, $output, $buttonClose){
		$.blockUI({
			css: {
				left:'50%', //($(window).width() - popupWidth) /2 + 'px',
				marginLeft: -(630/2) +'px',
				marginTop: '250px',
				position:'absolute',
				top:0,
				padding:0,
				width:popupWidth +'px',
				textAlign:'left',
				color:'#000',
				border:0,
				backgroundColor:'#fff',
				cursor:'default',
				zIndex:9999999
			},
			overlayCSS: {
				backgroundColor:'#000',
				opacity:'0.3',
				cursor: 'default',
				zIndex:999999
			},
			message: $output
		});

		// close
		$buttonClose
			.unbind('click')
			.click(function(){
				$.unblockUI();
				return false;
			});
	},

	popupLogin: function(settings){
		var $IS = this;

		// popup
		$IS.popup(
				630,
				settings.$output,
				settings.$close
			);

		// login
		settings.$form
			.unbind('submit')
			.submit(function(){
				var fields = {
					'data[username]': settings.$fields.username.val(),
					'data[password]': settings.$fields.password.val()
				};

                $.post(settings.uri, fields, function(data){
                    switch(data){
                        case 'true':
                        if(settings.ref)
                            window.location.href = settings.ref;
                        else
                            document.location.reload();
                        break;


                        default:
                        if (data.search('{"externalLink"') != -1)
                        {
                            var json = eval( "(" + data + ")" );
                            window.location.href = json.externalLink;
                        }
                        settings.$report.html(data).show();
                        break;
                    }
                });
                return false;
        });
	}
 };

