$(document).ready(function($){
	
	// Referenzen
	var alterInhalt;
	var param = '';
	var pics = new Array();
	var currNum = 0;
	var this_elem;
	
	$("div.box a.imglink").click(function(event, container){
		
		$('#ref_detail').slideUp('slow').remove();
        if(!container){
            alink = $(this);
        }
        else{
        	alink = $(container);
        }
        
		alterInhalt = $(this).closest('div.box');
		
		$(alink).parent().append('<img id="loader-gif" src="../gfx/wait.gif">');
		
		myhref = $(alink).attr('href') 
		
		$.ajax({
            type: 'get',
            url: myhref,
            data: 'js=1',
            success: function(r){
                $('#loader-gif').remove();
                
                $(alink).parent().append('<div id="ref_detail">'+r+'</div>');
                $('#ref_detail').slideDown('slow');
            }
		});
		return false;
	});
	
	$('#close_ref_detail').live('click', function(){
		$('#ref_detail').slideUp('slow', function(){
            $(this).remove();
		});
		return false;
	});
	
	$('.ref_shot').live('click', function(){
        this_elem = $(this);
        rel = $(this).attr('rel');
        elems = $('[rel='+rel+']');
        
        elems.each(function(i){
            src = $(elems[i]).children().attr('src');
            pics[i] = src;
            if($(elems[i]).children().attr('src') == $(this_elem).children().attr('src')){
            	currNum = i;
            }
        });
        showLightbox(currNum, elems.length - 1);
        return false;
	});
	
	function showLightbox(i, max){
		var b_breite, b_hoehe, scroll_pos, ref_desc;
		b_breite = $('body').width();
		b_hoehe = $('body').height();
		scroll_pos = getScrollPos();
		
		$('body').append('<div id="vorhang"></div>');
		$('#vorhang').css({width: b_breite, height: b_hoehe}).append('<div id="leinwand" title="click to close"></div>').fadeIn('slow', function(){
            
            $('#leinwand').show('slow',function(){
                $('#leinwand').append('<img id="ref_pic" src="'+pics[i]+'">');
                var breite = $('#ref_pic').width();
                var hoehe = $('#ref_pic').height();
                $('#leinwand').animate({width: breite +'px', height: hoehe + 'px', left: ((b_breite - breite)/2)+'px', top: (scroll_pos + 100) + 'px', opacity: 1}, function(){
                    $('#ref_pic').fadeIn(500, function(){
                        ref_desc = $(this_elem).children().attr('alt');
                        if(ref_desc.length > 0){
                            $(this).parent().append('<span id="ref_span">'+ref_desc+'</span>');
                        }
                        //$('#ref_span').append('<img src="../gfx/slide_right2.png" align="right" style="border:1px solid red">').prepend('<img src="../gfx/slide_left2.png" align="left">');
                        spanheight = $('#leinwand span').height();
                        $('#leinwand span').css({top: hoehe+'px'}).animate({top: ''+(hoehe - spanheight)+'px'});
                        
                    });
                    
                });
                
            });
		});
		return false;
	}
	
	function getScrollPos(){
		var y;
        if (self.pageYOffset) // all except Explorer
        {
            y = self.pageYOffset;
        }
        else if (document.documentElement && document.documentElement.scrollTop)
            // Explorer 6 Strict
        {
            y = document.documentElement.scrollTop;
        }
        else if (document.body) // all other Explorers
        {
            y = document.body.scrollTop;
        }
        return y;
	}
	
	$('#vorhang').live('click', function(){
        $(this).hide('slow').remove();
    });
    
	
	// Öffnen und Schließen auf der Profilseite
	$('#personal1').click(function(){
	    $('#profil-table').slideToggle('slow', function(){
        	if($('#personal1').attr('src') == '../gfx/pfeil_down.gif'){
                $('#personal1').attr({'src': '../gfx/pfeil_up.gif'});
            }
            else{
                $('#personal1').attr({'src': '../gfx/pfeil_down.gif'});
            }    
	    });
	});
	
	$('#personal2').click(function(){
        $('#profil-table2').slideToggle('slow', function(){
            if($('#personal2').attr('src') == '../gfx/pfeil_down.gif'){
                $('#personal2').attr({'src': '../gfx/pfeil_up.gif'});
            }
            else{
                $('#personal2').attr({'src': '../gfx/pfeil_down.gif'});
            }    
        });
    });
    
    // News holen
    
    $('div#news-item a').click(function(){
        var parentDiv = $(this).parent();
        var alterInhalt = $(this).parent().html();
        
        //var content = $(parentDiv+ ' div.item-content').html();
        console.log(alterInhalt('.item-content'));
        $.ajax({
            type: 'get',
            url: $(this).attr('href'),
            data: '',
            success: function(r){
            	
            } 
        });
        return false;
    });
    
    
    // Login
    var altesLogin;
    
    $('#open-login').live('click', function(){
        altesLogin = $('#login').html();
        $('#login').html('');
        $('#login').animate({width: '110px'}, 500, 'linear', function(){
            $('#login').animate({height: '80px'}, 500, function(){
                $('#login').html('<img src="../gfx/close_small.png" id="close-login" align="right" style="margin-top: -10px;">');
                $.ajax({
                    type:    'get',
                    url:     '../inc/getlogin.php',
                    data:    'js=1',
                    success: function(r){
                        $('#login').append(r);
                        
                    }
                });
            });
        });
    });
    
    $('#close-login').live('click', function(){
        $('#login').html('');
        $('#login').animate({height: '16px'}, 500, 'linear', function(){
            $('#login').animate({width: '60px'}, 500, function(){
                $('#login').html(altesLogin);
            });
        });
    });
    
    $('#login-name').live('focus', function(){
        if($(this).val() == 'username'){
        	$(this).val('');
        }
    });
    $('#login-name').live('blur', function(){
        if($(this).val() == ''){
            $(this).val('username');
        }
    });
    $('#login-pass').live('focus', function(){
        if($(this).val() == 'pass'){
            $(this).val('');
        }
        
    });
    $('#login-pass').live('blur', function(){
        if($(this).val() == ''){
            $(this).val('pass');
            $(this).attr('type', 'text');
        }
    });
    
    $('#login-submit').live('click', function(){
        $('#login-name').css('border', '1px solid #CCC');
        $('#login-pass').css('border', '1px solid #CCC');
        $.ajax({
            type:    'get',
            url:     '../inc/login.php',
            data:    'name='+$('#login-name').val()+'&pass='+$('#login-pass').val(),
            success: function(r){
                if(r.length > 0){
                    var json = jQuery.parseJSON(r);
                    for(i = 0; i < json.length; i++){
                    	if(json[i] == 1 || json[1] == 3) $('#login-name').css('border', '1px solid red');
                    	if(json[i] == 2 || json[1] == 4) $('#login-pass').css('border', '1px solid red');
                    }
                }
                else{
                	$('#login').html('');
                    $('#login').animate({height: '16px'}, 500, 'linear', function(){
                        $('#login').animate({width: '60px'}, 500, function(){
                            $('#login').html('<span id="logout">Logout</span>');
                            document.location.reload();
                        });
                    });
                }
            }
        });
    });
    
    // Logout
    $('#logout').live('click', function(){
        
        $.ajax({
        	type:    'get',
        	url:     '../inc/logout.php',
        	data:    'js=1',
        	success: function(r){
        		$('#login').html('<span id="open-login">Login</span>');
        		document.location.reload();
        	}
        });
    });
    
    // Edit Vtext-Form
    $('#edit_vtext').live('click', function(){
        var alterText = $('#span_vtext').html();
        $('#span_vtext').html('<img src="../gfx/save.png" id="img_vtext"><textarea id="ta_vtext">'+alterText+'</textarea>');
        $(this).hide();
    });
    
    $('#img_vtext').live('click', function(){
        var neuerText = $('#ta_vtext').val();
        $.ajax({
            type:    'get',
            url:     '../inc/save_vtext.php',
            data:    'text='+neuerText,
            success: function(r){
            	if(r.length > 0){
            		$('#ta_vtext').css({border: '1px solid red'});
            	}
            	else{
            		$('#span_vtext').html(neuerText);
            		$('#edit_vtext').slideDown('slow');
            	}
            }
        });
    });
});


