/////////////////////////
/* Animation           */
/////////////////////////


/////////////////////////
/* Animation Variables */
/////////////////////////
var $animationCount = 0;
var $animationsFinished = 0;
var $requestAnimationsFinished = 'false';
var $subNavAnimationCount = 0;
var $subNavAnimationsFinished = 0;
var mainNavChange;


/////////////////////////
/* DOM-READY           */
/////////////////////////



$(function() {

    if ( $(".ie7").length>0 || $(".ie8").length>0 ) {
	    setIeBackground();
	     //$("h1, h2, h3, h4, h5, a, p").ieffembedfix();
	}

	// a custom method making the default value invalid
	jQuery.validator.addMethod("defaultInvalid", function(value, element) {
	    return value != element.defaultValue;
	}, "");



    var i;
    for (i in document.images) {
        if (document.images[i].src) {
            var imgSrc = document.images[i].src;
            if (imgSrc.substr(imgSrc.length-4) === '.png' || imgSrc.substr(imgSrc.length-4) === '.PNG') {
               // document.images[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='" + imgSrc + "')";
            }
        }
    }


    // External Links Wrapper for Valid XHTML
    $('a[rel*=external]').live("click", function(event) {
		window.open(this.href);
		return false;
	});



	presentationCycle.init();

    $(".startBox").live("click", function(event) {
        presentationCycle.gotoSlide($(this).attr('id'));
    });


	// open and close content of boxes
	$("a.contentBoxHeadingLink").live("click", function(event) {
	    event.preventDefault();
		$($(this).attr("href")).slideToggle("slow");
		$(this).toggleClass('contentBoxHeadingLinkActive');
		$(this).parent(".contentBox").toggleClass('contentBoxActive');
	});

});



/////////////////////////
/* GENERAL             */
/////////////////////////


// Function returns flash-object
function getSWF(movieName)
{
    if (navigator.appName.indexOf("Microsoft") != -1)
    {
        return window[movieName];
    }
    else
    {
        return document[movieName];
    }
}


function setIeBackground(c){

    var b=$("html").css("background-image");
    b=b.substring(b.indexOf("http"),b.indexOf(".jpg")+4);

    var a=$("<img>").css(setImageCss(4/2.75)).css({position:"fixed",top:0,left:0,"z-index":-2}).attr("id","ieBgImage").attr("src",b);

    if($(".ie6").length>0){
        a.css("position","absolute")
    }

    $("body").css({position:"relative","z-index":0}).append(a);

    $("#ieBgImage").not(a).remove()

    $(window).resize(function(){
        a.css(setImageCss(4/2.75))
    })

}

function setImageCss(e){

    var a=$(window).width();
    var f=$(window).height();

    if(a<=1263&&f<=870){
        return{width:1263,height:870}
    }

    var c=a/f;
    var b=c/e;
    var d={};

    if(b>1){
        d.width=a;
        d.height=f*b
    } else {
        d.width=a/b;
        d.height=f
    }

    return d
}





/* add / remove newsletter subscriber */
var ContactForm = {
    postTarget: HTTPLANG + "forms/",
    init: function() {
        // initialize validation & custom error placement
        $("#contactForm").validate({
            meta: "validate"
        });
        // manually add rules which are later variable
        $("#fax").rules("add", {required: true});
        $("#company").rules("add", {required: true});
        // "unrequire" fields on "Endkunde"
        $("#iam").change(function(){
            var val = $("option:selected", this).attr("value");
            if (val == 'Endkunde') {
                $("#fax").removeClass("error").rules("remove")
                $("label.error[for='fax']").remove();
                $("#company").removeClass("error").rules("remove");
                $("label.error[for='company']").remove();
                $("span.star").hide();
            } else {
                $("#fax").rules("add", {required: true});
                $("#company").rules("add", {required: true});
                $("span.star").show();
            }
        });
        // hijack submit
        $("#contactForm .submitContact").click(function(event){
            // check form
        	if ($("#contactForm").valid()) {
                // do post
            	$.post(
                    ContactForm.postTarget,
                    $('#contactForm').serialize(),
            	    function(data){
                        // show error or output content
                        if (data.result == "error") {
                            $("#contactFormMessage").html(data.message);
                        } else {
                            // clear error
                            ContactForm.clearMessage();
                            // display result
                            $("#contactForm").slideUp(function(){
                                 $("#confirmSent").fadeIn();
                            });
                        }
                    }, "json");
            } else {
                $("#contactFormMessage").html("Bitte geben Sie eine gültige E-Mail-Adresse ein.");
            }
            // prevent normal submit
            return false;
        });
    },
    // clears the field
    clearMessage: function() {
        $("#contactFormMessage").html("");
    }
};

/* add / remove newsletter subscriber */
var DealersForm = {
    postTarget: HTTPLANG + "forms/",
    init: function() {
        // initialize validation & custom error placement
        $("#dealersForm").validate({
            meta: "validate"
        });
        // hijack submit
        $("#dealersForm .submitDealers").click(function(event){
            // check form
        	if ($("#dealersForm").valid()) {
                // do post
            	$.post(
                    DealersForm.postTarget,
                    $('#dealersForm').serialize(),
            	    function(data){
                        // show error or output content
                        if (data.result == "error") {
                            $("#dealersFormMessage").html(data.message);
                        } else {
                            // clear error
                            DealersForm.clearMessage();
                            // display result
                            //$("#dealersForm").slideUp(function(){
                                 //$("#confirmLogin").fadeIn();
                                 if ( !History.enabled ) {
                                     //alert(location);
                                     location.reload();
                                     //parent.frames[1].location.reload();
                                    return false;
                                 } else {
                                     var stateNow = History.getState();
                                     History.pushState(stateNow.data,stateNow.title,stateNow.url+'2/');
                                 }


                            //});
                        }
                    }, "json");
            } else {
                $("#dealersFormMessage").html("Bitte geben Sie gültige Login-Daten ein. / Wrong Login-Data!");
            }
            // prevent normal submit
            return false;
        });
    },
    // clears the field
    clearMessage: function() {
        $("#dealersFormMessage").html("");
    }
};

/* send newsletter-registration email */
var RegistrationForm = {
    postTarget: HTTPLANG + "forms/",
    init: function() {
        // initialize validation
        $("#registrationForm").validate({
            meta: "validate"
        });
        // hijack submit
        $("#registrationForm .submit").click(function(event){
            // check form
        	if ($("#registrationForm").valid()) {
                // do post
            	$.post(
                    RegistrationForm.postTarget,
                    $('#registrationForm').serialize(),
            	    function(data){
                        // show error or output content
                        if (data.result == "error") {
                            $("#registrationFormMessage").html(data.message);
                        } else {
                            // clear error
                            RegistrationForm.clearMessage();
                            // display result
                            $("#registrationForm").slideUp(function(){
                                 $("#registrationSent").fadeIn();
                            });
                        }
                    }, "json");
            }
            // prevent normal submit
            return false;
        });
    },
    // clears the field
    clearMessage: function() {
        $("#registrationFormMessage").html("");
    }
};


