window.j4s = {};

window._ = function(p){
    return p;
}

window.addEvent('domready', function(){
    j4s.prepareProfileMoreBtn();
    j4s.prepareAButton();
    j4s.prepareNaviTips();
});

j4s.prepareNaviTips = function(){
    new Tips($$('.navi-top a'), {
        offset: {y: -30, x: 9},       
        fixed: true,
        className : 'fancyTip',
        onAttach : function() {
        	$$('.fancyTip')[0].setStyle('top', '-50px');
        }
    });
}

j4s.ivwReload = function(){
    var obj = $$('img[alt=szmtag]')[0];
    
    if(! window._adTop )
        window._adTop = $('ad-top').get('html');
        
    $('ad-top').empty();
    var ifrm = new IFrame({
        border: 0,
        frameborder: 0,
        scrolling: 'no',
        style: 'border: 0px; width:849px; height: 90px;'
    }).inject($('ad-top'));
    ifrm.contentWindow.document.write( "<body style='padding: 0px; margin: 0px;'>"+window._adTop+"</body" );
    
    if(! window._adRight )
        window._adRight = $('ad-right').get('html');
        
    $('ad-right').empty();
    var ifrm = new IFrame({
        border: 0,
        frameborder: 0,
        scrolling: 'no',
        style: 'border: 0px; width:160px; height: 700px;'
    }).inject($('ad-right'));
    ifrm.contentWindow.document.write( "<body style='padding: 0px; margin: 0px;'>"+window._adRight+"</body" );
    
    
    var url = obj.src;
    if( obj.src.indexOf('?') > 0 ){
        var temp = obj.src.split('?');
        url = temp[0];
    }
    new Asset.image( url + "?r="+escape(document.referrer)+"&d="+(Math.random()*100000));
}

j4s.loader = new Class({

    initialize: function( pParent ){
        
        this.main = new Element('div', {
            style: 'text-align: center;'
        }).inject( pParent );
        
        this.img = new Element('img', {
            src: '/inc/template/admin/images/ka-tooltip-loading.gif'
        }).inject( this.main );
        
    }

});

j4s.loader_span = new Class({

    initialize: function( pParent ){
        
        this.main = new Element('span', {
            style: ''
        }).inject( pParent );
        
        this.img = new Element('img', {
            src: '/inc/template/admin/images/ka-tooltip-loading.gif'
        }).inject( this.main );
        
    }

});

j4s.acceptFriendship = function( pRsn ){

	new Request.JSON({url: '/j4s-accept-friendship:'+pRsn+'/', noCache: 1, onComplete: function(res){
		
		
		
		if( res == 1 ){
			j4s._alert('Freundschaft erfolgreich akzeptiert', function(){
				location.reload();
			});		
		} else {
			j4s._alert('Fehler: 0001');
		}
	}}).post();

}

j4s.cancelFriendship = function( pRsn ){
	new Request.JSON({url: '/j4s-cancel-friendship:'+pRsn+'/', noCache: 1, onComplete: function(res){
		if( res == 1 ){
			j4s._alert('Freundschaft erfolgreich aufgehoben.', function(){
				location.reload();
			});		
		} else {
			j4s._alert('Fehler: 0001');
		}
	}}).post();
}



j4s.addFriendship = function( pId ){
	var id = pId;
	if(! id ){
	    var entry = $('friends-friend-entry');
	    if( entry.value == "" ){
	        entry.highlight();
	        return;
	    }
	    id = entry.value;
    }

    new Request.JSON({url: '/community/profil/freunde/add-friend/', noCache: 1, onComplete: function(res){
        if( res.status == 'not-found' ){
            j4s._alert('Benutzer mit dieser E-Mail nicht gefunden!');
        }
        if( res.status == 'already-friend' ){
            j4s._alert('Benutzer ist bereits ein Freund.');
        }
        if( res.status == 'already-requested' ){
            j4s._alert('Benutzer ist bereits angefragt. Bitte warte auf eine Antwort.');
        }
        if( res.status == 'done' ){
            j4s._alert('Benutzer erfolgreich angefragt.');
        }
    }}).post({email: id, json: 1});
    
}



j4s.denieFriendship = function( pRsn ){
	$('j4s-friendship-requests-'+pRsn).set('tween', {onComplete: function(){
		$('j4s-friendship-requests-'+pRsn).setStyle('display', 'none');
	}});
	new Request.JSON({url: '/j4s-cancel-friendship:'+pRsn+'/', noCache: 1, onComplete: function(res){
		
		if( res == 1 ){
			j4s._alert('Freundschaft abgelehnt.', function(){
			$('j4s-friendship-requests-'+pRsn).setStyle('display', 'none');
		});
		} else {
			j4s._alert('Fehler: 0001');
		}
	}}).post();
}

j4s.friendListDenieFriendship = function( pRsn ){	
	new Request.JSON({url: '/j4s-cancel-friendship:'+pRsn+'/', noCache: 1, onComplete: function(res){
		
		if( res == 1 ){
			j4s._alert('Freundschaft abgelehnt.', function(){
			$('j4s-friendship-requests-'+pRsn).setStyle('display', 'none');
		});
		} else {
			j4s._alert('Fehler: 0001');
		}
	}}).post();
}

j4s.checkAndHideFriendshipBox = function(){
	var boxs = $$('#j4s-frienship-box .j4s-friendship-request');
	if( boxs.length == 0 ){
		$('j4s-frienship-box').set('tween', {onComplete: function(){
			$('j4s-frienship-box').setStyle('display', 'none');
		}});
		$('j4s-frienship-box').tween('opacity', 0);
	}
}

j4s.registerCheck = function(){

	var namec = new kFormValidator('reg-name', 'Name bitte ausf&uuml;llen', {empty: false});
	if( !namec.okOrShow() ) return;
	
	var emailc = new kFormValidator('reg-email', 'Bitte g&uuml;ltig E-Mail angeben', {empty: false, check: 'email'});
	if( !emailc.okOrShow() ) return;
	
	var passwdc = new kFormValidator('reg-passwd', 'Passwort bitte angeben', {empty: false});
	if( !passwdc.okOrShow() ) return;
	
	var passwd2c = new kFormValidator('reg-passwd2', 'Passw&ouml;rter sind nicht gleich', {equalWith: 'reg-passwd'});
	if( !passwd2c.okOrShow() ) return;
	
	var ctermsOfuse = new kFormValidator('reg-termsOfUse', 'Bitte akzeptieren Sie die Nutzungsbedingungen', {empty: false});
	if( !ctermsOfuse.okOrShow() ) return;
	
	var cprivacyPolice = new kFormValidator('reg-privacyPolicy', 'Bitte akzeptieren Sie die Datenschutzrichtlinien', {empty: false});
	if( !cprivacyPolice.okOrShow() ) return;
	
	
	var go = true;
	$('reg-checkdata').setStyle('display', 'block');
	$('reg-emailexist').setStyle('display', 'none');
	$('reg-usernameexist').setStyle('display', 'none');
	
	new Request.JSON({url: '/checkRegisterData:1/', noCache: 1, onComplete: function(res){
		$('reg-checkdata').setStyle('display', 'none');
		if( res.emailexist == 1 ){	
			go = false;
			$('reg-emailexist').setStyle('display', 'block');
		}
		if( res.usernameexist == 1){
			go = false;
			$('reg-usernameexist').setStyle('display', 'block');
		}
		
		if( go == true )
			$('registerform').submit();
			
	}}).post({
		email: $('reg-email').value,
		username: $('reg-name').value
	});
	
}


j4s._alert = function( pMsg, pCallback ){
    window._lastAlertBg = new Element('div', {
        'style': 'position: fixed; z-index: 500000; left: 0px; top: 0px; width: 100%; height: 100%; background-color: #555; text-align: center;',
        styles: {
            opacity: 0.7
        }
    }).inject( document.body );

    window._lastAlertBox = new Element('div', {
    	'class': 'j4s-box',
        text: pMsg
    }).inject( document.body );
    
    new Element('a',{
    	'class': 'j4s-box-x',
    	text: 'x'
    })
    .addEvent('click', function(){
    	window._lastAlertBox.destroy();
    	window._lastAlertBg.destroy();
    })
    .inject( window._lastAlertBox );

	var box = new Element('div', {
		style: 'text-align: right; position: relative; top: 10px;'
	})
    .inject( window._lastAlertBox );
	
    new Element('a',{
    	'class': 'button',
    	text: 'OK'
    })
    .addEvent('click', function(){
    	if( pCallback )
    		pCallback();
    	window._lastAlertBox.destroy();
    	window._lastAlertBg.destroy();
    })
    .inject( box );

    window._lastAlertBox.position(); 
}

j4s.prepareAButton = function(){
    $$('.content-main a.button').each(function(button){
        if( !button.getElement('span') )
            new Element('span').inject( button );
    });
    $$('.content-main a.button-small').each(function(button){
        if( !button.getElement('span') )
            new Element('span').inject( button );
    });
}

j4s.prepareProfileMoreBtn = function(){

    var btn = $('profile-more-btn');
    var pane = $('profile-more-pane');

    if( btn ){
        pane.setStyles({
            opacity: 0,
            display: 'block'
        });
        pane.set('tween', {duration: 800, transition: Fx.Transitions.Cubic.easeOut});


        pane.addEvent('mouseover', function(){
            btn.store( 'fadeOutOk', false );
        });

        pane.addEvent('mouseout', function(e){
            btn.fireEvent('mouseout', e);
        });

        btn.addEvent('mouseover', function(){
            this.store( 'fadeOutOk', false );
            $('profile-more-pane').tween('opacity', 1);
        });

        btn.addEvent('mouseout', function(){
            this.store( 'fadeOutOk', true );
            (function(){
                if( this.retrieve('fadeOutOk') == true ){
                    $('profile-more-pane').tween('opacity', 0);
                }
            }.bind(this)).delay( 200 );
        });

    }

}
