var Zwiesel = {

    //initialize
    init: function(){

        //inits
        Zwiesel.utils.init();
        Zwiesel.events.init();

        if ($('#newsletterForm').length !== 0) {
            Zwiesel.form.setNewsletterForm();
        }

    },

    //helper
    utils: {

        //initialize helper
        init: function(){

            Zwiesel.utils.initFormSkinning();
            Zwiesel.utils.initFontReplacement();
			Zwiesel.utils.os_detection();

        },

        //form skinning
        initFormSkinning: function(){

            $('form').jqTransform({
                imgPath: 'img/formSkinning'
            });

        },
	    //os detection	
		    
			os_detection: function(){

            if ($.os.mac) {

                $('#os_spacer_de').css({
                    height: '53px'
                });
				
				$('#os_spacer_en').css({
                    height: '38px'
                });
				

               }	
			 },



        //replace navi and headline fonts
        initFontReplacement: function(){
            //the sans plain light
            Cufon.replace('h2', {
                fontFamily: 'TheSansPlain-SemiLight'
            });
            Cufon.replace('h3', {
                fontFamily: 'TheSansPlain-SemiLight'
            });
            Cufon.replace('#navLeft a', {
                fontFamily: 'TheSansPlain-SemiLight',
                hover: true
            });
            Cufon.replace('p.theSans', {
                fontFamily: 'TheSansPlain-SemiLight'
            });
            //the sans plain bold
            Cufon.replace('p.theSans strong', {
                fontFamily: 'TheSansPlain'
            });
            //myriad pro normal
            Cufon.replace('#header a', {
                fontFamily: 'Myriad Pro',
                hover: true
            });
            Cufon.replace('a.teaserLink', {
                fontFamily: 'Myriad Pro',
                hover: true
            });
            Cufon.replace('a.infoDownload', {
                fontFamily: 'Myriad Pro',
                hover: true
            });

        },

        //show error
        showFormError: function(length, dataError){

            for (i = 0; i < length; i++) {

                var error = dataError[i].field;

                $('input[name=' + error + '][type=text]').parent().parent().parent().addClass('jqTransformInputWrapperError');
                $('select[name=' + error + ']').parent().removeClass('').addClass('jqTransformSelectWrapperError');
                $('input[name*=' + error + '][type=checkbox]').parent().addClass('jqTransformCheckboxWrapperError');
                $('input[name=' + error + '][type=radio]').parent().addClass('jqTransformRadioWrapperError');
                $('textarea[name=' + error + ']').parent().parent().parent().parent().parent().addClass('jqTransformTextareaError');
            }

        },

        //hide all errors
        hideFormError: function(){

            $('input[type=text]').parent().parent().parent().removeClass('jqTransformInputWrapperError');
            $('select').parent().removeClass('').removeClass('jqTransformSelectWrapperError');
            $('input[type=checkbox]').parent().removeClass('jqTransformCheckboxWrapperError');
            $('input[type=radio]').parent().removeClass('jqTransformRadioWrapperError');
            $('textarea').parent().parent().parent().parent().parent().removeClass('jqTransformTextareaError');

        },

        //reset form input
        resetForm: function(){
            //TODO: reset form

        },

        //get param from location
        getURLParam: function(strParamName){
            var strReturn = "";
            var strHref = window.location.href;
            var bFound = false;

            var cmpstring = strParamName + "=";
            var cmplen = cmpstring.length;

            if (strHref.indexOf("?") > -1) {
                var strQueryString = strHref.substr(strHref.indexOf("?") + 1);
                var aQueryString = strQueryString.split("&");
                for (var iParam = 0; iParam < aQueryString.length; iParam++) {
                    if (aQueryString[iParam].substr(0, cmplen) == cmpstring) {
                        var aParam = aQueryString[iParam].split("=");
                        strReturn = aParam[1];
                        bFound = true;
                        break;
                    }
                }
            }
            if (bFound == false)
                return null;
            return strReturn;
        },

        //debug helper returning console.log or alerts
        debug: function(){

            var location = window.location + '';

            if (location.indexOf('127.0.0.1:8000') !== -1) {

                var debugMode = true;
                var debugModeAlert = false;

                if (debugMode) {

                    var debugString = '';
                    var i = Zwiesel.utils.debug.arguments.length;

                    do {
                        i--;
                        debugString += i + 1 + '. ' + Zwiesel.utils.debug.arguments[i] + '\n';
                    }
                    while (i > 0);

                    if ($.browser.mozilla) {
                        console.log(debugString);
                    }
                    else
                        if (debugModeAlert) {
                            window.alert(debugString);
                        }

                }

            }

        }

    },

    //global events
    events: {

        //initialize event handlers
        init: function(){

            //slideshow event
            $('a.slideshow').bind('click', function(){

                var id = $(this).attr('id');
                var pos = parseInt(id.substring(9));

                Zwiesel.slideshow.init(pos, 0);
                return false;

            });

            //video lightbox event
            $('a.video').bind('click', function(){

                var id = $(this).attr('id');
                var pos = parseInt(id.substring(5));

                Zwiesel.flash.video(pos);
                return false;

            });

            //newsletter teaser to page event
            $('a.newsletter').bind('click', function(){


                var target = $(this).attr('href');
                var value = $(this).prev().children().find('input').val();

                if (value === $(this).prev().children().find('input').attr('title')) {
                    value = '';
                }

                Zwiesel.form.goToNewsletter(target, value);
                return false;

            });

            //newsletter submit event
            $('#newsletterForm').bind('submit', function(){

                Zwiesel.form.sendNewsletter();
                return false;

            });

            if (dynamicContent.header) {

                //header video
                $('#mainTeaser').css({

                    cursor: 'pointer'

                }).bind('click', function(){

                    Zwiesel.flash.header();
                    return false;

                });

            }

        }

    },


    //form submit
    form: {

        //submits the email value to the newsletter form
        goToNewsletter: function(target, value){

            window.location = target + '&email=' + value;

        },

        //sets the param value als value of the newsletter e-mail field
        setNewsletterForm: function(){

            if (Zwiesel.utils.getURLParam('email') !== null) {
                $('input[name=email]').val(Zwiesel.utils.getURLParam("email"));
            }

            if (Zwiesel.utils.getURLParam('target') !== null) {
                var targetId = Zwiesel.utils.getURLParam('target');
                $('input[name=verteiler' + targetId + ']').attr('checked', true).siblings().addClass('jqTransformChecked');
            }

        },

        //send newsletter form
        sendNewsletter: function(){

            Zwiesel.utils.hideFormError();

            var target = $('#newsletterForm');

            $('#newsletterForm').unbind('submit').bind('submit', function(){
                return false;
            });

            //get all formdata
            var formData = $('#newsletterForm').formToArray();

            //formData values
            /*
             for (i = 0; i < formData.length; i++) {
             Zwiesel.utils.debug("formData " + i + " : " + formData[i].name + " : " + formData[i].value);
             }
             */
            //send tp php file
            $.post('php/newsletter.php', formData, function(data){

                //callback

                Zwiesel.utils.resetForm();

                $('#newsletterForm').unbind('submit').bind('submit', function(){

                    Zwiesel.form.sendNewsletter();
                    return false;

                });

                if (data.success) {
                    //success

                    //HTML
                    if (dynamicContent.language === 'de') {

                        var content = '<strong>Vielen Dank f&uuml;r Ihr Interesse an unserem kostenlosen Newsletter Service</strong><br /><br />Hiermit haben Sie den ersten Schritt erfolgreich abgeschlossen.<br />In K&uuml;rze erhalten Sie eine Best&auml;tigungs-E-Mail.<br />Diese E-Mail enth&auml;lt einen Link, den wir Sie bitten einmal zu klicken.';


                        $('#feedback').html(content);

                    }
                    else {
                        //englischer text
                        var content = '<strong>Thank you for your interest in our free newsletter service.</strong><br /><br />You have successfully taken the first step.<br /> You will shortly receive a confirmation email.<br />This email contains a link that we ask you to click just once.';


                        $('#feedback').html(content);

                    }

                }
                else {
                    //failed

                    var eLength = data.error.length;

                    if (eLength !== 0) {
                        //HTML
                        var content;
                        if (dynamicContent.language === 'de') {

                            content = '<strong>Bitte &uuml;berpr&uuml;fen Sie Ihre Eingabe:</strong>';

                        }
                        else {
                            //TODO: englischer text
                            content = '<strong>Please check your details:</strong>';

                        }

                        for (i = 0; i < eLength; i++) {
                            var error = data.error[i].text;
                            content += '<br />' + error;
                        }

                        $('#feedback').html(content);

                        Zwiesel.utils.showFormError(eLength, data.error);
                    }
                    else {

                        //HTML
                        if (dynamicContent.language === 'de') {

                            var content = '<strong>Bei der Verbindung zum Server ist ein Fehler aufgetreten</strong><br /><br />Bitte versuchen Sie es zu einem sp&auml;teren Zeitpunkt noch einmal.';

                            $('#feedback').html(content);

                        }
                        else {
                            //englischer text
                            var content = '<strong>An error occurred</strong><br /><br />Please try again later.';

                            $('#feedback').html(content);

                        }

                    }
                }

            }, 'json');
        }

    },

    //slideshow
    slideshow: {

        //init slideshow content
        init: function(pos, num){

            //html content
            var content = ''
            content += '<div id="lightboxBgS"></div>';
            content += '<div id="lightboxS">';
            content += '        <div id="lightboxContentS">';

            if(dynamicContent.language === 'de' ){
            content += '                <div id="lightboxClose"><a href="" class="close">schlie&szlig;en</a></div>';
            }else{
            content += '                <div id="lightboxClose"><a href="" class="close">close</a></div>';
            }
            content += '                <div id="lightboxSlideshow"><span><img src="' + dynamicContent.slideshow[pos][num].img + '" alt="Slideshow" id="' + num + '" height="476" width="711" /></span></div>';
            content += '                <div id="lightboxFooter">';
            content += '                        <div id="lightboxSlideshowNav">';
            content += '                                <a href="#" class="next"></a>';
            content += '                                <a href="#" class="prev"></a>';
            content += '                        </div>';

            if (dynamicContent.slideshow[pos][num].logo === '') {
                content += '                        <div id="lightboxText"><span><h2>' + dynamicContent.slideshow[pos][num].headline + '</h2>' + dynamicContent.slideshow[pos][num].text1 + '</span></div>';
            }
            else {
                content += '                        <div id="lightboxLogo"><img src="' + dynamicContent.slideshow[pos][num].logo + '" alt="Lookbook" height="85" width="345" /></div>';
            }
            content += '                </div>';
            content += '        </div>';
            content += '</div>';

            //add to dom
            $('body').prepend(content);

            if ($.browser.safari) {

                $('#lightboxContentS').css({
                    marginLeft: '-305px'
                });

            }

            //refresh font style
            Cufon.refresh('h2');

            $('a.prev').css({
                display: 'none'
            });

            //fade in background
            $('#lightboxBgS').css({
                opacity: 0
            }).animate({
                opacity: 0.7
            }, {
                duration: 150
            });

            //fade in content
            $('#lightboxS').css({
                opacity: 0
            }).animate({
                opacity: 1
            }, {
                duration: 400
            });

            //bind close event
            $('#lightboxS').add('a.close').bind('click', function(){

                $('#lightboxBgS').add('#lightboxS').remove();

                return false;

            });

            //exclude content from close event
            $('#lightboxContentS').bind('click', function(){

                return false;

            });

            //bind pager event
            $('a.prev', $('#lightboxSlideshowNav')).bind('click', function(){

                var num = parseInt($('#lightboxSlideshow img').attr('id'));
                num--;

                Zwiesel.slideshow.change(pos, num);

                return false;

            });

            //bind pager event
            $('a.next', $('#lightboxSlideshowNav')).bind('click', function(){

                var num = parseInt($('#lightboxSlideshow img').attr('id'));
                num++;

                Zwiesel.slideshow.change(pos, num);

                return false;

            });

        },

        //change slideshow content
        change: function(pos, num){

            if ($.browser.msie) {

                //build new content
                $('#lightboxSlideshow span').html('<img src="' + dynamicContent.slideshow[pos][num].img + '" alt="Slideshow" id="' + num + '" height="476" width="711" />');

                if (dynamicContent.slideshow[pos][num].logo === '') {
                    $('#lightboxText span').html('<h2>' + dynamicContent.slideshow[pos][num].headline + '</h2>' + dynamicContent.slideshow[pos][num].text1);
                }
                else {
                    $('#lightboxLogo').html('<img src="' + dynamicContent.slideshow[pos][num].logo + '" alt="Lookbook" height="85" width="345" />');
                }

                //refresh font style
                Cufon.refresh('h2');

                if (num === 0) {

                    $('a.prev').css({
                        display: 'none'
                    });
                    $('a.next').css({
                        display: 'block'
                    });

                }
                else
                    if (num === dynamicContent.slideshow[pos].length - 1) {

                        $('a.next').css({
                            display: 'none'
                        });
                        $('a.prev').css({
                            display: 'block'
                        });

                    }
                    else {

                        $('a.next').css({
                            display: 'block'
                        });
                        $('a.prev').css({
                            display: 'block'
                        });

                    }

            }
            else {

                //fade out old content
                $('#lightboxSlideshow img').animate({
                    opacity: 0
                }, {
                    step: function(now, settings){

                        $('#lightboxText span').css({
                            opacity: settings.now
                        });
                        $('#lightboxText span h2').css({
                            opacity: settings.now
                        });

                    },
                    duration: 250,
                    complete: function(){

                        //build new content
                        $('#lightboxSlideshow span').html('<img src="' + dynamicContent.slideshow[pos][num].img + '" alt="Slideshow" id="' + num + '" height="476" width="711" />');

                        if (dynamicContent.slideshow[pos][num].logo === '') {
                            $('#lightboxText span').html('<h2>' + dynamicContent.slideshow[pos][num].headline + '</h2>' + dynamicContent.slideshow[pos][num].text1);
                        }
                        else {
                            $('#lightboxLogo').html('<img src="' + dynamicContent.slideshow[pos][num].logo + '" alt="Lookbook" height="85" width="345" />');
                        }

                        $('#lightboxSlideshow img').css({
                            opacity: 0
                        });

                        //refresh font style
                        Cufon.refresh('h2');

                        if (num === 0) {

                            $('a.prev').css({
                                display: 'none'
                            });
                            $('a.next').css({
                                display: 'block'
                            });

                        }
                        else
                            if (num === dynamicContent.slideshow[pos].length - 1) {

                                $('a.next').css({
                                    display: 'none'
                                });
                                $('a.prev').css({
                                    display: 'block'
                                });

                            }
                            else {

                                $('a.next').css({
                                    display: 'block'
                                });
                                $('a.prev').css({
                                    display: 'block'
                                });

                            }

                        //fade in old content
                        $('#lightboxSlideshow img').animate({
                            opacity: 1
                        }, {
                            step: function(now, settings){

                                $('#lightboxText span').css({
                                    opacity: settings.now
                                });
                                $('#lightboxText span h2').css({
                                    opacity: settings.now
                                });

                            },
                            duration: 250
                        });

                    }
                });

            }
        }

    },

    //header content
    flash: {

        //init flash in the header
        header: function(){

            if ($.flashPlayerVersion[0] >= 9) {

             var video_lang;

                    if (dynamicContent.language === 'de') {

                    video_lang =  'swf/VideoPlayerSmall.swf';

                    }else{

                     video_lang =  'swf/VideoPlayerSmall_en.swf';

                      }

                $('#mainTeaser').flash({
                    swf: video_lang,
                    height: '500px',
                    width: '749px',
                    params: {
                        menu: 'false',
                        allowscriptaccess: 'always',
                        wmode: 'transparent',
                        salign: 'tl'
                    },
                    flashvars: {
                        xml_url: dynamicContent.header
                    }
                });

                $('#mainTeaser').unbind('click');

            }
            else {

                $('#mainTeaser').unbind('click').css({
                    background: 'none',
                    backgroundColor: '#ffffff'
                });

                if (dynamicContent.language === 'de') {

                    $('#mainTeaser').html('<span class="adobe"><p><img src="img/get_adobe_flash_player.gif" /></p><div class="spacer"></div><p>Um dieses Video abzuspielen ben&ouml;tigen Sie den aktuellen Adobe Flash Player.</p><p><strong>Download Adobe Flash Player</strong></p></span>');

                }
                else {

                    //TODO: englischer Text!
                    $('#mainTeaser').html('<span class="adobe"><p><img src="img/get_adobe_flash_player.gif" /></p><div class="spacer"></div><p>To watch this video, you will need to install the latest Adobe Flash Player.</p><p><strong>Download the free Adobe Flash Player.</strong></p></span>');

                }

                $('#mainTeaser').bind('click', function(){

                    window.open('http://get.adobe.com/de/flashplayer/');
                    return true;

                });

            }

        },

        //close header video
        closeVideo: function(){

            if ($('#mainTeaser').hasClass('header')) {

                $('#mainTeaser').css({

                    cursor: 'pointer'

                }).bind('click', function(){

                    Zwiesel.flash.header();
                    return false;

                }).children().css({
                    display: 'none'
                });

            }

            $('#lightboxBgV').add('#lightboxV').css({
                opacity: 0,
                display: 'none'
            });

        },

        //video in der lightbox
        video: function(pos){

            Zwiesel.utils.debug(pos);
            Zwiesel.utils.debug(dynamicContent.video[pos]);

            if ($('#lightboxBgV').length !== 0) {

                $('#lightboxBgV').add('#lightboxV').css({
                    display: 'block'
                });

            }
            else {

                //html content
                var content = ''
                content += '<div id="lightboxBgV"></div>';
                content += '<div id="lightboxV">';
                content += '        <div id="lightboxContentV">';
                content += '                <div id="lightboxVideo"><span></span></div>';
                content += '        </div>';
                content += '</div>';

                //add to dom
                $('body').prepend(content);

                if ($.browser.safari) {

                    $('#lightboxContentV').css({
                        marginLeft: '-305px'
                    });

                }

            }

            //fade in background
            $('#lightboxBgV').css({
                opacity: 0
            }).animate({
                opacity: 0.7
            }, {
                duration: 150
            });

            //fade in content
            $('#lightboxV').css({
                opacity: 0
            }).animate({
                opacity: 1
            }, {
                duration: 400,
                complete: function(){

                    if ($.flashPlayerVersion[0] >= 9) {


                    var video_lang;

                    if (dynamicContent.language === 'de') {

                    video_lang =  'swf/VideoPlayerBig.swf';

                    }else{

                     video_lang =  'swf/VideoPlayerBig_en.swf';

                      }
                        $('#lightboxVideo').flash({

                            swf: video_lang,
                            height: '632px',
                            width: '749px',
                            params: {
                                menu: 'false',
                                allowscriptaccess: 'always',
                                wmode: 'transparent',
                                salign: 'tl'
                            },
                            flashvars: {
                                xml_url: dynamicContent.video[pos]
                            }
                        });
                    }
                    else {

                        $('#lightboxVideo').css({
                            backgroundColor: '#ffffff',
                            width: '749px',
                            height: '500px',
                            cursor: 'pointer'
                        });

                        if (dynamicContent.language === 'de') {

                            $('#lightboxVideo').html('<span class="adobe"><p><img src="img/get_adobe_flash_player.gif" /></p><div class="spacer"></div><p>Um dieses Video abzuspielen ben&ouml;tigen Sie den aktuellen Adobe Flash Player.</p><p><strong>Download Adobe Flash Player</strong></p></span>');

                        }
                        else {

                            //TODO: englischer Text!
                            $('#lightboxVideo').html('<span class="adobe"><p><img src="img/get_adobe_flash_player.gif" /></p><div class="spacer"></div><p>To watch this video, you will need to install the latest Adobe Flash Player.</p><p><strong>Download the free Adobe Flash Player.</strong></p></span>');

                        }

                        $('#lightboxVideo').bind('click', function(){

                            window.open('http://get.adobe.com/de/flashplayer/');
                            return true;

                        });

                    }

                }
            });

            //bind close event
            $('#lightboxV').bind('click', function(){

                $('#lightboxBgV').add('#lightboxV').css({
                    opacity: 0,
                    display: 'none'
                });

                return false;

            });

            //exclude content from close event
            $('#lightboxContentV').bind('click', function(){

                return false;

            });

        }
    }

};


//initialize all
$(document).ready(function(){

    Zwiesel.init();

});
