$(function(){
		   

    $('form').live("submit",function(){
        $(this).children("[placeholder!=]").each(function(){
            if($(this).val()==$(this).attr("placeholder"))	$(this).val("");									  
        })							 
    });

    $('.fakeCheckbox').each(function(){
        value = $(this).find("input").val();
	
        if(value=="1")$(this).addClass("aktiv");
        else $(this).removeClass("aktiv");
	
	
								 
    });
    $('.fakeCheckbox').live("click",function(){
        value = $(this).find("input").val();
        $(this).find("input").val(value=="0"?"1":"0");
        $(this).toggleClass("aktiv",600);
    });


    $('.tab tr:even').addClass("second");

    $('.showMoreInputs').live("click",function(){
        $('.moreInputs').animate({
            "height":"toggle"
        },"slow","easeOutSine");
    });

    $('input, textarea').each(function(){
        placeholder = $(this).attr("placeholder");
        if(placeholder!=undefined && placeholder!=null && placeholder!="" && $(this).val()==""){
            $(this).val(placeholder).addClass("placeholder");
		
            $(this).bind("focusout",function(){
                if($(this).val()==""){
                    placeholder = $(this).attr("placeholder");
                    $(this).val(placeholder).addClass("placeholder");
                }
            });
            $(this).bind("focus",function(){
                if($(this).val()==$(this).attr("placeholder")){
                    $(this).val("").removeClass("placeholder");
                }
            });
		
        }
    })
    $('#kontakt').live("submit",function(){
        formSend = $(this).serialize();
        $.post("functions/sendMail",formSend,function(){
            $('#kontakt').slideUp("slow","easeOutSine",function(){
                $(this).before('<div class="preloader" style=""></div>');
				
                $('.preloader').slideDown(600,"easeInCubic").delay(800).fadeOut("slow",function(){
                    $('#kontakt').before('Danke für Ihre eMail!');
                });
				
            });											  
        });
        return false;
    });
	
        
   
    $('#sendMailDynamic').live("submit",function(){
        checked = true;
        
        if($('.required').length){
           $('.required').each(function(){
               if($(this).val()==""){
                   $(this).addClass("fillOut");
                   checked = false;
               }
               else{
                   $(this).removeClass("fillOut");
               }
           });
            
        }
        
        else{
            checked = true;
        }
        
        if(checked){
        formSend = $(this).serialize();
        $.post("/functions/sendMailDynamic",formSend,function(){
            $('#sendMailDynamic').slideUp("slow","easeOutSine",function(){
                $(this).before('<div class="preloader" style=""></div>');
				
                $('.preloader').slideDown(600,"easeInCubic").delay(800).fadeOut("slow",function(){
                $(this).remove();
                    $('#sendMailDynamic').before('Danke für Ihre eMail!');
                });
				
            });											  
        });
        }else{
            
            if(!$('.fillOutText').length) $(this).prepend('<p class="fillOutText">Bitte füllen Sie alle markierten Felder aus!</p>');
            
        }
        return false;
    });
    
    $('.fillOut').live("change",function(){
        if($(this).val().length){
        $(this).removeClass("fillOut");
        }
    })
	
	
    $('#addLohn').live("submit",function(){
        formSend = $(this).serialize();
        $.post("functions/newWage",formSend,function(){
            location.href = location.href;										  
        });
        return false;
    });
	
	
    $('.fancybox').fancybox({
        "overlayColor":"#000",
        "overlayOpacity":0.6,
        'titlePosition'	:	'over',		
        'transitionIn'	:	'elastic',
        'transitionOut'	:	'elastic',
        'easingIn' : 'easeInOutBack',
        'easingOut' : 'easeInOutBack',
        'onComplete'	:	function() {
            $("#fancybox-wrap").hover(function() {
                $("#fancybox-title").show();
            }, function() {
                $("#fancybox-title").hide();
            });
        }

			
    });

    $('.rechnung,ul.user li a.edit,.billName').fancybox({
        "overlayColor":"#000",
        "overlayOpacity":0.6,	
        'transitionIn'	:	'elastic',
        'titleShow':false,
        'showCloseButton':false,
        'transitionOut'	:	'elastic',
        'easingIn' : 'easeInOutBack',
        'easingOut' : 'easeInOutBack',
        'hideOnContentClick':false
			
    });

    $('.showMore').live("click",function(){
        art = $(this).attr("rel");
        $(".showMoreWrapper[rel="+art+"]").slideDown(900,"easeInOutBack");
        $(this).html("&laquo; Weniger anzeigen").attr("class","showLess");

        return false;
    });
		
    $('.showLess').live("click",function(){
        art = $(this).attr("rel");
        $(".showMoreWrapper[rel="+art+"]").slideUp(900,"easeInOutBack");
        $(this).html("&laquo; Mehr anzeigen").attr("class","showMore");

        return false;
    });
		
		
    $('.openLogin').fancybox({
        'scrolling'		: 'no',
        'titleShow'		: false,
        'onClosed'		: function() {
            $("#login_error").hide();
        },
	
        "onComplete" : function(){
            $("#login_name").focus();
        },
        "overlayColor":"#000",
        "overlayOpacity":0.6,
        'titlePosition'	:	'over',		
        'transitionIn'	:	'elastic',
        'transitionOut'	:	'elastic',
        'easingIn' : 'easeInOutBack',
        'easingOut' : 'easeInOutBack'
    });

    $('.showWorkInfo').fancybox({
        'scrolling'		: 'no',
        'titleShow'		: false,
        'onClosed'		: function() {
        },
        "overlayColor":"#000",
        "overlayOpacity":0.6,
        'titlePosition'	:	'over',		
        'transitionIn'	:	'elastic',
        'transitionOut'	:	'elastic',
        'easingIn' : 'easeInOutBack',
        'easingOut' : 'easeInOutBack',
        "width":640,
        "autoDimensions":false
    });
    var logged = 0;


    $('#login_form').live("submit",function(){
        $.ajax({
            "url":"/functions/login",
            "type":"POST",
            "cache":false,
            "data":{
                "username":$('#login_name').val(),
                "password":$('#login_pass').val()
                },
            "success":function(){
                location.href = "/overview";
            }
        })
    return false;									
    });
$('#logout').live("click",function(){
    $.get("/functions/logout",function(){
        location.href = "/";
    });
    return false;
});
		   
	
	
	
		   
$('.onlineStatus').live("click",function(){
    online = $(this).hasClass("aktiv");
    $.get("/functions/setOnlineStatus&id="+$(this).attr("rel")+"&status="+(online?"0":"1"));
    $(this).toggleClass("aktiv",700);
});

$('.kategorie').live("click",function(){
    cat = $(this).html();
    $(this).hide().after('<form id="changeCategory"><input type="text" size="30" name="category" value="'+cat+'"> <input type="hidden" name="id" value="'+$(this).siblings(".lohn").attr("rel")+'"></form>');
    $('#changeCategory').find('input').focus().bind("change focusout",function(){
        $(this).parent().siblings(".kategorie").html($(this).val()).show();
        data = $(this).parent().serialize();
        $.post("functions/changeWage",data);
        $('#changeCategory').remove();
    });
        return false;
		
});


$('.lohn').live("click",function(){
    wage = $(this).html();
    $(this).hide().after('<form id="changeWage"><input type="text" size="3" name="wage" value="'+wage+'"><input type="hidden" name="id" value="'+$(this).attr("rel")+'"></form>');
    $('#changeWage').find('input').focus().bind("change focusout",function(){
        $(this).parent().siblings(".lohn").html($(this).val()).show();
        data = $(this).parent().serialize();
        $.post("functions/changeWage",data);
        $('#changeWage').remove();
    });
        return false;
		
});
$('.workSelect').live("change",function(){
    $('[name=billName]').val($(this).find(":selected").attr("rel")).trigger("blur");
    $('.userSelect').find('option[value='+$(this).find(":selected").attr("rev")+']').attr("selected",true);
});


$('.addBill').live("submit",function(){
    
    formSend = $(this).serialize();
    
    $.post("/functions/newBill",formSend,function(){
     location.href = location.href;   
    });
    return false;
    
});
$('.deleteBill').live("click",function(){
    ask = confirm('Diese Rechnung wirklich löschen');
    if(ask){
        $.get("/functions/deleteBill&id="+$(this).attr("rel"));
        $(this).parent('li').slideUp(400);
    }
})

$('.stundenlohn li').live("dblclick",function(){
    id = $(this).find(".kategorie").attr("rel");
    name = $(this).find(".kategorie").html();
    ask = confirm(name + ' wirklich löschen?');
    if(ask){
        $(this).addClass("pleaseDelete");
        $.get("/functions/deleteWage&id="+id,function(){
            $('.pleaseDelete').hide("slide",{
                "direction":"left",
                "easing":"easeOutSine"
            },500);
        })
    }
});
		
		
$('.deleteWork').live("click",function(){
    id = $(this).attr("rel");
    name = $(this).attr("title");
    ask = confirm(name + ' wirklich löschen?');
    if(ask){
			
        $('#workRow'+id).slideUp();
        $.get("/functions/deleteWork&id="+id);
					
    }
});

$('#generatePassword').live("click",function(){
    length = parseInt($(this).attr("rel"));		

    chars  = "abcdefghijklmnopqrstuvwxyz";
    chars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    chars += "1234567890";
    chars += "!@%§$";
    pass = "";
    for(x=0;x<length;x++)
    {
        i = Math.floor(Math.random() * chars.length);
        pass += chars.charAt(i);
    }
    $(this).prev("input").val(pass);
							
										
});


$('.showAddUser').live("click",function(){
    $('#addUser').animate({
        "height":"toggle"
    },700,"easeInOutSine");
    return false;
});

$('.deleteUser').live("click",function(){
    ask = confirm("Wirklich löschen?");
    if(ask){
        $(this).parent().slideUp(400);
        $.get("/functions/deleteUser&id="+$(this).attr("rel"),function(){
																			   
            });
		
    }
									   
    return false;
});
if($('.workInfo').length){
    $.ajax({
        "url":"/functions/stundenlohnAuswahl.php",
        "success":function(data){ 
            var stundenlohnAuswahl = data;
            $('.workInfo').append(stundenlohnAuswahl);
            $('input[name=stundenAnzahl]').live("focus",function(){
                $(this).val("");
            });
        }
    });

}

$('.addRechnung').live("submit",function(){
    kategorie = $(this).find("select[name=kategorie]").val();
    id = $(this).parent().attr("id").replace("workInfo","");
    stunden = $(this).find("input[name=stundenAnzahl]").val();
    $.ajax({
        "url":"/functions/addRechnung.php",
        "type":"POST",
        "data":{
            "kategorie":kategorie,
            "auftragsId":id,
            "stunden":stunden
        }
    })
 
$(this).find("input[name=stundenAnzahl]").val("Stunden");
    $(this).append('<small class="saveRechnung" style="display:none">Gespeichert</small>');
    $('.saveRechnung').slideDown(400,"easeOutBack").delay(200).slideUp(400,"easeInBack",function(){
        $(this).remove();
    });
    return false;					 
});

$('.deleteStunde').live("click",function(){
    entryId = $(this).attr("rel");
    $(this).parents("tr").fadeTo(100,0.4);
    $.get("/functions/deleteBillEntry&id="+entryId,function(){
        arbeitsID = $(this).parents(".rechnungsWrapper");
        $("#rechnungsWrapper").parent().load("/showRechnung/"+$("#rechnungsWrapper").attr("rel"));
    });
		
});
$('#printRechnung').live("click",function(){
    $('#fancybox-overlay').hide();
    window.print();
    $('#fancybox-overlay').show();
});

$('.changeRechnungStatus span:not(.aktiv)').live("click",function(){
    $('.changeRechnungStatus .aktiv').removeClass("aktiv");
    $(this).addClass("aktiv");
    id = $(this).parent().attr("rel");
    value = $(this).attr("rel");
    $.get("/functions/changeBillStatus&id="+id+"&value="+value);
});

$('input[name=wageHours]').live("keyup",function(){
    wage = $('select[name=wage] option:selected').attr("rel");
    hour = $(this).val();
    $('.calculateWage').html(Math.round(wage*hour*100)/100);
});

$('.newBill').live("click",function(){
    $('.addBill').animate({"height":"toggle","opacity":"toggle"});
    return false;
})

function addEntry(){
     billId = $('.addEntry').attr("rel");
    $('.addEntry').parents("tr").fadeTo(200,0.4);
    data = {
        "wageId":$('select[name=wage]').val(),
        "billId":billId,
        "wageHours":$('input[name=wageHours]').val(),
        "wageTotallyCache":$('.calculateWage').html()
    }
    $.post("functions/addBillEntry",data,function(){
        $.get("/pages/showRechnung.php?id="+billId,function(data){
          $('#rechnungsWrapper').after(data).remove(); 
        });
    });
}



$('.addEntry').live("click",function(){
   addEntry()
    
})

$('input[name=wageHours]').live("keyup",function(e){
    if(e.which=="13"){
        addEntry()
    
    }
    
})

$('.changeRabatt').live("change",function(){
    billId = $('#rechnungsWrapper').attr("rel");
    $.get("functions/changeRabatt&value="+$(this).val()+"&id="+billId,function(){
         $.get("/pages/showRechnung.php?id="+billId,function(data){
          $('#rechnungsWrapper').after(data).remove(); 
        });
    });
})


$('.toggle').live("click",function(){	
    $(this).toggleClass("active");
    position = $(this).position();
    context = $('.hideDiv[rel='+$(this).attr("rel")+']').animate({
        "opacity":"toggle",
        "height":"toggle"
    },500,"easeInSine",function(){
        $('html,body').animate({
            scrollTop: position.top
            }, 1000);
    });
	
				
});

$('.hideDiv.onStartOpen').delay(600).animate({
    "opacity":"toggle",
    "height":"toggle"
},500,"easeInSine");


$('.facebook').live("click",function(){
									 
    $('.twitterWrapper').slideUp(200,"easeOutSine",function(){
        $(this).remove();
    })
    $('.twitter.active').removeClass("active");
    $(this).toggleClass("active");
    $('.facebookWrapper').animate({
        "opacity":"toggle",
        "height":"toggle"
    },1300,"easeOutBounce");					 
});

$('.twitter').live("click",function(){
    $('.facebookWrapper').slideUp(200,"easeOutSine")
    $('.facebook.active').removeClass("active");
    $(this).toggleClass("active");
	
    if(!$('.twitterWrapper').length){
        $('#content').prepend('<div class="twitterWrapper"><div class="followers"></div><a href="http://www.twitter.com/tzdesign" target="_blank" class="followMe"></a><ul></ul><br clear="all"></div>');
        $.getJSON("http://twitter.com/status/user_timeline/tzdesign.json?count=4&callback=?",function(data){
            json = [];
            akk = data;
            $.each(data, function(key, twitter) {
                $('.twitterWrapper ul').append('<li><span class="date">'+twitter.user.screen_name+'</span><img src="'+twitter.user.profile_image_url+'">'+twitter.text+'<br clear="all"></li>');
                $('.followers').html("Followers: "+twitter.user.followers_count);
            });
            $('.twitterWrapper').animate({
                "opacity":"toggle",
                "height":"toggle"
            },1300,"easeOutBounce");	
        })
    }
    else{
			
        $('.twitterWrapper').animate({
            "opacity":"toggle",
            "height":"toggle"
        },1300,"easeOutBounce",function(){
            $('.followMe,.twitterWrapper').remove();
        });  
			
    }
});

$('.showMoreWork').live("click",function(){
    $(this).addClass("removeAfterAjax");
    $('#content').append('<br class="preloader_br" clear="all"><div class="preloader removeAfterAjax"></div>');
    position = $(this).position();
		 
    $.ajax({
        "url":"/overview?limit="+(parseInt($(this).attr("rel"))),
        "success":function(akk){
				  
            $('html,body').animate({
                scrollTop: position.top
                }, 1000);
            $('.removeAfterAjax').fadeOut(function(){
                $(this).remove();
            });
            $('.workWrapper,.showMoreWork',akk).appendTo($('#content'));
            $('.rechnung,ul.user li a.edit').fancybox({
                "overlayColor":"#000",
                "overlayOpacity":0.6,	
                'transitionIn'	:	'elastic',
                'titleShow':false,
                'showCloseButton':false,
                'transitionOut'	:	'elastic',
                'easingIn' : 'easeInOutBack',
                'easingOut' : 'easeInOutBack',
                'hideOnContentClick':false
			
            });
           
        }
    });
    return false;
});

$('select[name=art]').live("change",function(){
    if($(this).val()=="Websites") $('input[name=Website]').slideDown();	
    else $('input[name=Website]').slideUp();										   
});

if($('.datepicker').length)$.ajax({
    "url":"/css/jquery-ui-1.8.11.custom.css",
    "success":function(data){
        $('head').append('<style type="text/css">'+data+'</style>')
        }
    })
$('.datepicker').datepicker( {
    "dateFormat":"yy-mm-dd"
} );


$('.loadLinkOnTop').live("click",function(){
    $('.ajaxLoadContent').remove(); 
    link = $(this).attr("href");
    content = $(this).attr("rel");
	
    $('#content').prepend('<div class="ajaxLoadContent" style="display:none;"></div>'); 
    $('html,body').animate({
        "scrollTop":0
    },500,"easeInOutSine",function(){
        $.ajax({
            "url":link,
            "success":function(data){ 
                $('.ajaxLoadContent').html($(content,data).html()).delay(200).slideDown(600);	
					
					
                $('.fancybox').fancybox({
                    "overlayColor":"#000",
                    "overlayOpacity":0.6,
                    'titlePosition'	:	'over',		
                    'transitionIn'	:	'elastic',
                    'transitionOut'	:	'elastic',
                    'easingIn' : 'easeInOutBack',
                    'easingOut' : 'easeInOutBack',
                    'onComplete'	:	function() {
                        $("#fancybox-wrap").hover(function() {
                            $("#fancybox-title").show();
                        }, function() {
                            $("#fancybox-title").hide();
                        });
                    }

			
                });
					
            }
        })																						  
    });
	
return false;
});


$('.closeParent').live('click',function(){
    $(this).parent().slideUp(500,"easeInOutSine",function(){
        $(this).remove();
    })		
    return false;
});

$('#editWork').live("submit",function(){
	
  formSend = $(this).serialize();
		
    $.post("/functions/editWork",formSend,function(){
        $('#editWork').slideUp("slow","easeOutSine",function(){
            $(this).before('<div class="preloader" style=""></div>');
				
            $('.preloader').slideDown(600,"easeInCubic").delay(800).fadeOut("slow",function(){
                history.back();
            });
				
        });											  
    });
		
		
    return false;
});


$('.deleteMedia').live("click",function(){
    id=$(this).attr("rel");
    $(this).parents("li").slideUp();
    $.get("/functions/deleteMedia&id="+id);
    
});

$('.mediaItem:not(.deleteMedia)').live("click",function(e){
    target = $(e.target);
    if(target.parents(".mediaItem").length==0){
    id = $(this).attr("rel");
    workId = $(this).attr("rev");
    $('.mediaItem.default').removeClass("default");
    $(this).addClass("default");
    $.get("/functions/setDefaultMedia&workId="+workId+"&id="+id);
    }
})

$('.sendBillInfo').live("click",function(){
    ask = confirm("Dem Kunden eine E-Mail schicken?");
    if(ask){
        id = $(this).attr("rel");
        $.get("/functions/sendBillStatus&id="+id);
    }
});



});
