var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-6002580-2']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();


var background = $("<img />").attr("src", "fileadmin/templates/revier.de/images/loaderbg.png").attr("width", 1200).attr("height", 1200).css("position", "absolute");
var loader = $("<img />").attr("src", "fileadmin/templates/revier.de/images/logo-rot.gif").css("position", "absolute").css("left", 575).css("top", 575);
var withSlide = false;
var usedZindex = parseFloat(50);
var zindexInc = parseFloat(50);



$(document).ready(function(){
    initSliderNavigation();
	initTextBoxes();

	function scriptc(a,b){
		var __d=document;
		var __h = __d.getElementsByTagName("head").item(0);
		var s = __d.createElement("script");
		s.setAttribute("src", a);
		s.id = b;
		__h.appendChild(s);
	 }

	scriptc("http://s7.addthis.com/js/250/addthis_widget.js");

    $(".searchicon").mouseover(function(){
        if ($("#searchbox").css('display') == 'none') {
            $("#searchbox").css('display', 'block');
            $("#closeicon").css('display', 'block');
            
            $("#searchicon").attr("src", "fileadmin/templates/revier.de/images/search_hover.gif");
            $("#searchboxcontainer").css('backgroundColor', '#ffffff');
            $(".textbox").focus();
        }
        
        
    });
    $("#closeicon").click(function(){
        $("#searchbox").css('display', 'none');
        $("#closeicon").css('display', 'none');
        $("#searchicon").attr("src", "fileadmin/templates/revier.de/images/search.gif");
        $("#searchboxcontainer").css('backgroundColor', 'transparent');
        
    });

    $(".popup").draggable({
        opacity: 0.65,
        handle: ".top"
    });
	$(".popup, .popup > *").mousedown(function(){
		usedZindex = usedZindex + zindexInc;
		$(this).css("z-index",usedZindex);
	});
	$.ajax({
	  url: "http://s7.addthis.com/js/250/addthis_widget.js#domready=1",
	  dataType: "script"
	});
	
	initPageFlip();
});

$.address.externalChange(function(){
    var hashpath = $.address.hash();
    if (hashpath.length > 3) {
        loadSliderContent($.address.hash());
    }
});



function initTextBoxes(){
    $('.txtbox ').each(function(){
        var default_value = this.value;
        $(this).focus(function(){
            if (this.value == default_value) {
                this.value = '';
            }
        });
        $(this).blur(function(){
            if (this.value == '') {
                this.value = default_value;
            }
        });
    });
}

function loadFaderContent(path){
    $.ajax({
        url: path,
        success: function(data){
			trackPageInAnalytics(path);
            var content = $(".slidewrap", data).html();
            $(".wrap").css("height", parseInt($(".wrap").height()) + "px");
            $(".oslidewrap").css("height", parseInt($(".oslidewrap").height()) + "px");
            
            $(".slidewrap").delay(100).fadeOut(250, function(){
                $(".slidewrap").html($(".slidewrap", data).html());
                initSliderNavigation();
                $(".slidewrap").delay(200).fadeIn(250, function(){
                    $(".wrap").removeAttr("style");
                    $(".oslidewrap").removeAttr("style");
                });
                
            });
        },
        error: function(errData){
            removeLoader();
        }
    });
}


function loadSliderContent(path){
    createLoader();
    $.ajax({
        url: path,
        success: function(data){
			trackPageInAnalytics(path);
            $('html, body').animate({
                scrollTop: 0
            }, 300, function(){
            
            });
            moveSlider(data, path);
            
        },
        error: function(errData){
            removeLoader();
        }
    });
}

function trackPageInAnalytics(path){
	_gaq.push(['_trackPageview', path]);
}
function moveSlider(data, path){
	

    var content = $(".slidewrap", data).html();
    $(".wrap").css("height", $(".wrap").height());
    $(".oslidewrap").css("height", $(".oslidewrap").height());
    $(".slidewrap").attr("id", "oldslide").css("height", $(".slidewrap").height()).css("position", "absolute");
    
    
    var newSlideWrap = $("<div />");
    newSlideWrap.attr("class", "slidewrap").attr("id", "newslide").css("position", "absolute").css("left", "962px");
    newSlideWrap.html($(".slidewrap", data).html());
	
    $(".oslidewrap").append(newSlideWrap);
    
    $("#oldslide").delay(700).animate({
        left: "-962px"
    }, 800);
    $("#newslide").delay(700).animate({
        left: "0px"
    }, 800, function(){
        $("#oldslide").remove();
        $(".wrap").removeAttr("style");
        $(".oslidewrap").removeAttr("style");
        $(".slidewrap").removeAttr("id").removeAttr("style");
        removeLoader();
        //window.location.pathname="/"+path;
        //window.location.hash="/"+path;
        $.address.hash(path);
    });
    
    initSliderNavigation();
	initTextBoxes();
	
	$(".jqfancybox").fancybox();
	var script = 'http://s7.addthis.com/js/250/addthis_widget.js#domready=1';
	if (window.addthis){
	    window.addthis = null;
	}
	$.getScript( script );
	initPageFlip();
	
}

function initSliderNavigation(){
    $(".slide").each(function(){
        if (this.tagName.toLowerCase() == "a") {
				$(this).unbind('click');
				$(this).click(function(e){
	                loadSliderContent($(this).attr("href"));
	                return false;
	            });
        }
        else {
				$("a", this).unbind('click');
				$("a", this).click(function(e){
					loadSliderContent($(this).attr("href"));
					return false;
				});
        }
    });
}

function createLoader(){
    var wheight = (parseInt($(window).height()) / 2) - 600;
    var wwidth = (parseInt($(window).width()) / 2) - 600;
    var loaderdiv = $("<div />").css("position", "fixed").attr("class", "loaderdiv").css("left", wwidth).css("top", wheight);
    loaderdiv.append(background).append(loader);
    $("body").append(loaderdiv);
    $(".loaderdiv").fadeIn(200);
}

function removeLoader(){
    $(".loaderdiv").fadeOut(0, function(){
        $(".popup").fadeOut("fast");
        $(".loaderdiv").remove();
    });
    
}

function formsearchvalidate(frm){
    var errflag = false;
    if (document.getElementById('tx_indexedsearch-sword').value == '') {
        errflag = true;
    }
    if (errflag) 
        return false;
    else 
        return true;
}

function openPopup(e){
	usedZindex = usedZindex + zindexInc;
    $(".popups > ." + e).fadeIn("fast").css("z-index",usedZindex);
    return false;
}


function closePopup(e){
    $(e).parent().parent().parent().fadeOut("fast");
}

function loadPopupContent(id, e){
    $.ajax({
        url: "index.php?id=" + id,
        success: function(data){
            $("." + e + " .content").append($("body", data).html());
            var content = $("#pinner", data).html();
            $(".popups > ." + e + " .inner > .content").append(content);
            $("." + e + "loader").remove();
        }
    });
}


function formvalidate(frm){
    var errnameflag = false;
    var erremailflag = false;
    var mailfield = true;
    
    
    if (document.getElementById('vname').value == 'Vorname, Name*' || document.getElementById('vname').value.search(/\S/) == -1) {
        errnameflag = true;
        document.getElementById('requiredname').style.display = 'block';
    }
    
    if (document.getElementById('email').value == 'E-Mail*' || document.getElementById('email').value.search(/\S/) == -1) {
        erremailflag = true;
        mailfield = false;
        document.getElementById('requiredemail').innerHTML = 'E-Mail ausf&uuml;llen';
        document.getElementById('requiredemail').style.display = 'block';
    }
    
    
    if (mailfield) {
        if ((/^[a-zA-Z0-9]+[a-zA-Z0-9\._-]+(@[a-zA-Z0-9-]+\.([a-z\.]{2,4}[a-z]{2}|[a-z]{2,4}))$/).exec(document.getElementById('email').value) == null) {
        
            erremailflag = true;
            document.getElementById('requiredemail').innerHTML = 'Bitte geben Sie eine gültige E-Mail Adresse an.';
            document.getElementById('requiredemail').style.display = 'block';
        }
    }
    
    
    if (document.getElementById('firma').value == 'Firma' && (erremailflag == false && errnameflag == false)) {
        document.getElementById('firma').value = '';
    }
    
    if (document.getElementById('strasse').value == 'Straße, Nr.' && (erremailflag == false && errnameflag == false)) {
        document.getElementById('strasse').value = '';
    }
    
    if (document.getElementById('ort').value == 'PLZ, Ort' && (erremailflag == false && errnameflag == false)) {
        document.getElementById('ort').value = '';
    }
    
    if (document.getElementById('telefon').value == 'Telefon' && (erremailflag == false && errnameflag == false)) {
        document.getElementById('telefon').value = '';
    }
    
    if (document.getElementById('telefax').value == 'Telefax' && (erremailflag == false && errnameflag == false)) {
        document.getElementById('telefax').value = '';
    }
    
    if (document.getElementById('website').value == 'Website' && (erremailflag == false && errnameflag == false)) {
        document.getElementById('website').value = '';
    }
    
    if (document.getElementById('bemerkungen').value == 'Ihre Nachricht' && (erremailflag == false && errnameflag == false)) {
        document.getElementById('bemerkungen').value = '';
    }
    
    if (erremailflag && errnameflag) {
    
        return false;
    }
    else 
        if (erremailflag) {
        
            document.getElementById('requiredname').style.display = 'none';
            return false;
        }
        else 
            if (errnameflag) {
            
                document.getElementById('requiredemail').style.display = 'none';
                return false;
            }
            else {
            
                document.getElementById('requiredemail').style.display = 'none';
                document.getElementById('requiredname').style.display = 'none';
                return true;
                
            }
}


function initPageFlip(){
	/* The following code is executed once the DOM is loaded */

	$('.sponsorFlip').bind("click",function(){

		// $(this) point to the clicked .sponsorFlip element (caching it in elem for speed):

		var elem = $(this);

		// data('flipped') is a flag we set when we flip the element:

		if(elem.data('flipped'))
		{
			// If the element has already been flipped, use the revertFlip method
			// defined by the plug-in to revert to the default state automatically:

			elem.revertFlip();

			// Unsetting the flag:
			elem.data('flipped',false)
		}
		else
		{
			// Using the flip method defined by the plugin:

			elem.flip({
				direction:'lr',
				speed: 350,
				color: "white",
				onBefore: function(){
					// Insert the contents of the .sponsorData div (hidden
					// from view with display:none) into the clicked
					// .sponsorFlip div before the flipping animation starts:

					elem.html(elem.siblings('.sponsorData').html());
				}
			});

			// Setting the flag:
			elem.data('flipped',true);
		}
	});
	
	var spclear =  $("<div />").attr("class","clear");
	$(".sponsor:last").after(spclear);

}

