// JavaScript Document für SieRupp.com
// Copyright (c) by SieRupp GbR, Nathanael Siering und Felix Rupp. All rights reserved.
// No copy, reproduction or use without written permission of Felix Rupp.
// For contact see: http://www.sierupp.com/


// strpos
function strpos (haystack, needle, offset) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman    
    // +   bugfixed by: Daniel Esteban
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
    // *     returns 1: 14

    var i = (haystack+'').indexOf(needle, (offset || 0));
    return i === -1 ? false : i;
}


// JQuery
function resetColors() {	
	$("input,textarea").removeClass("error");
	$("input").tooltip().hide();
}


/* Document Ready Funktion */
$(document).ready(function() {
    globalInit();
    inhaltInit();
	topLink();
	observeAjax();
	
	
	
});


function observeAjax(){
    $(document).ajaxSuccess(function(evt, request, settings){ 
        inhaltInit();
    });
}



function globalInit() {

     // Smooth Scroll
    $(function(){
        $('a[href*=#]').click(function() {
            if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
                    var $target = $(this.hash);
                    $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
                    if ($target.length) {
                        var targetOffset = $target.offset().top;
                        $('html,body').animate({scrollTop: targetOffset}, 500);
                        return false;
                    }
            }
        });
    });
    
    
}


function inhaltInit() {
    
    // Lightbox Effekte für Bilder
    $('a.lightbox').lightBox();


	// Accordion Effekte
	$(".standardAccordion").accordion({clearStyle: true, collapsible: true, autoHeight: false, active: false, header: 'h3'});
	
	$(".referenzenAccordion").accordion({clearStyle: true, collapsible: true, autoHeight: false, active: 0, header: 'h3'});
		
	
	// bxSlider für Referenzen
	/*$("#printdesign").bxSlider({
		infiniteLoop: true,
		hideControlOnEnd: false,
		auto: true,
		pager: true,
		autoHover: true,
		pause: 5000
	});
	
	$("#webdesign").bxSlider({
		infiniteLoop: true,
		hideControlOnEnd: false,
		auto: true,
		pager: true,
		autoHover: true,
		pause: 5000
	});*/
	
	

	

    /* Kontaktformular */
    $('#kontaktFormular').unbind("submit"); // Vorherige, alte submit-Events entfernen
    
	$('#kontaktFormular').submit(function(){
	
		action = $(this).attr('action');
		
		$('#submitKontakt').attr('disabled','disabled');

		//$("input,textarea").removeClass("error");

		resetColors();

		$.post(action,$("#kontaktFormular").serialize(),function(data){
		
			if(data=='Ihre Nachricht wurde erfolgreich versendet. Vielen Dank.') {
			
				$('#kontaktFormular').slideUp(600,function(){
					$(".success").fadeIn(600)
				});
				
			} else if(data=='Bitte Absender eintragen.') {
				
				$('input[name=absenderName]').effect("highlight", {color: "#e89f0e"}, "normal").effect("highlight", {color: "#e89f0e"}, "normal").effect("highlight", {color: "#e89f0e"}, "normal", function(){ $(this).addClass("error"); $(this).tooltip().show() });

			} else if(data=='Bitte g&uuml;ltige E-Mail-Adresse eintragen.') {
			
				$('input[name=absenderEmail]').effect("highlight", {color: "#e89f0e"}, "normal").effect("highlight", {color: "#e89f0e"}, "normal").effect("highlight", {color: "#e89f0e"}, "normal", function(){ $(this).addClass("error"); $(this).tooltip().show() });
								
			} else if(data=='Bitte eine mindestens 10 Zeichen lange Nachricht eingeben.') {
			
				$('textarea[name=absenderNachricht]').effect("highlight", {color: "#e89f0e"}, "normal").effect("highlight", {color: "#e89f0e"}, "normal").effect("highlight", {color: "#e89f0e"}, "normal", function(){ $(this).addClass("error"); $(this).tooltip().show() });
				
			} else if(data=='Fehler: Nachricht wurde nicht gesendet.') {
			
				$(".errorMessage").fadeIn(600)
				
			}
			
			$('#submitKontakt').attr('disabled','');
									
		});

	    return false;
	
	});	
	
	
	// Tooltip fürs Kontaktformular
	$("#kontaktFormular :input[type=text], :input[type=textarea], :input[type=checkbox]").tooltip({position: "center right", offset: [0, 45], effect: "slide", direction: "left", slideInSpeed: 350, slideOutSpeed: 300});
	
	
		
	
	
}



function topLink() {

    $('#topLink').hide();

    $(window).scroll(function(){
    
        if($(window).scrollTop()!=0 && $(window).scrollLeft()==0) {
            $('#topLink').fadeIn(600);
        }
        if($(window).scrollTop()!=0 && $(window).scrollLeft()!=0) {
            $('#topLink').fadeIn(600);
        }
        if($(window).scrollTop()==0) {
            $('#topLink').fadeOut(600);
        }
        if($(window).scrollTop()==0 && $(window).scrollLeft()==0) {
            $('#topLink').fadeOut(600);
        }
        if($(window).scrollTop()==0 && $(window).scrollLeft()!=0) {
            $('#topLink').fadeOut(600);
        }
        
    });
    
//    alert("topLink ausgelöst!");
    
}
