/**
 * Created by JetBrains PhpStorm.
 * User: HeidiAnselstetter
 * Date: 10.08.11
 * Time: 10:00
 * To change this template use File | Settings | File Templates.
 */

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-26851242-1']);
  _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 $buoop = {}
$buoop.ol = window.onload;
window.onload=function(){
 try {if ($buoop.ol) $buoop.ol();}catch (e) {}
 var e = document.createElement("script");
 e.setAttribute("type", "text/javascript");
 e.setAttribute("src", "http://browser-update.org/update.js");
 document.body.appendChild(e);
} 

$(document).ready(function(){
//    $('#slider').nivoSlider({
//            effect: 'fade',
//            pauseTime:4000,
//            directionNav:false,
//            directionNavHide:false,
//            controlNav:false
//    });

    $('#PostleitzahlenPlzPlz').focus();

    $('#germanmap').click(function() {
        $("#deuhighlight").css("color", "#C00D0D");
        $("#deuhighlight").effect("pulsate", {times:3}, 2000, callback);
    });
    function callback() {
        $("#deuhighlight").css("color", "#828282");
        $('#PostleitzahlenPlzPlz').focus();
    }
    
    $("#Map area[rel]").overlay({effect: 'apple', top: '20%',mask:{
                            color: '#fff',
                            loadSpeed: 200,
                            opacity: 0.5
                        }});

    $("#Video img[rel]").overlay({effect: 'apple', top: '15%',mask:{
                            color: '#fff',
                            loadSpeed: 200,
                            opacity: 0.5
                        }});
	
    $('#cert').click(function() {
        document.location.href='/p/certification';
    });

    $('#vide').click(function() {
        document.location.href='/p/videos';
    });

    $('#indexer').click(function() {
        document.location.href='/';
    });


    // SURVEY
    //var lhash = window.location.hash; //alert(lhash);
    //var pattern =  /\d{5}/ ;
    if($(".hash").length > 0){
        //alert('ok');
        $("#umfrage").modal({
            containerId: 'surveycontainer',
            opacity:50,
            overlayCss: {backgroundColor:"#fff"},
            position: ['5%']
        });
        $('.close').click(function() {
            $.ajax({
                type: 'POST',
                url: '/newsletter/save_noThanks',
                success: function(msg){
                    //document.location.href='/';
                }
            });
            $.modal.close();
            $("#umfrage").hide();
        });

        $('#survey1').click(function() {
            window.open('/n/survey', 'survey');
        });
        $('#survey2').click(function() {
            $.ajax({
                type: 'POST',
                url: '/newsletter/save_noThanks',
                success: function(msg){
                    document.location.href='/';
                }
            });
            return false;
        });
    }

    // SURVEY RATING
    if($('#cancel').length > 0){
            $('#cancel').raty({
                //cancel: true,
                //hintList:       ['Naja...', 'OK...', 'Gut!', 'Schön!', 'Wow! Sehr gut!'],
                path: '/img/',
                start: 1,
                width: '100%',
                scoreName: 'news_question_1',
                starOff:    'star-off-big.png',
                starOn:     'star-on-big.png'
            });
    }

    $('#NewsletterSurveyForm').find(':input').each(function(){
        $(this).change(function(){
            var fieldId = $(this).attr('id');
            var fieldValue = $(this).val();
            if(fieldId.substr(fieldId.length-1) == 0 || fieldId.substr(fieldId.length-1) == 1){
                fieldId = fieldId.substr(0, fieldId.length - 1);
            }
            $.ajax({
                type: 'POST',
                url: '/newsletter/save_singleField',
                data: 'field=' +fieldId+ '&value=' + fieldValue,
                success: function(msg){
                    //console.log(msg);
                }
            });
        });
    });


    $('#surveynext').click(function() {
        $.ajax({
            type: 'POST',
            url: '/newsletter/save_singleField',
            data: 'field=news_question_1&value=' + $('#cancel-score').val(),
            success: function(msg){
                $.ajax({
                    type: 'POST',
                    url: '/newsletter/save_nextPage',
                    success: function(msg){
                        document.location.href='/n/surveyfinal';
                    }
                });
            }
        });
    });





    // AJAX SUBMIT POSTLEITZAHLEN
        $('#PostleitzahlenIndexForm').ajaxForm({
            dataType:  'json',
            success:   processJson
        });
        function processJson(data) {
            var textToInsert = [];
            var i = 0;
            $.each(data, function(key, val) {
                if(key == 'plz_plz'){
                    alert(val);
                    $('#plzdiv').empty();$('#PostleitzahlenPlzPlz').focus();
                    return false;
                }else if(key == 'noresult'){
                    alert(val);
                    $('#plzdiv').empty();$('#PostleitzahlenPlzPlz').focus();
                    return false;
                }
                    $('#plzdiv').empty();
                    textToInsert[i++]  = '<span id="plz_plz">'+val.Postleitzahlen.plz_plz+'</span> ';
                    textToInsert[i++]  = '<span id="plz_ort">'+val.Postleitzahlen.plz_ort+'</span><br /><br />';
                    //textToInsert[i++]  = '<span id="plz_kreis">'+val.Postleitzahlen.plz_kreis+'</span><br /><br />';

                    textToInsert[i++]  = '<span>Ihr Ansprechpartner Außendienst: </span><br /><br />';
                    if(val.Aussendienst.aud_anrede == 1) textToInsert[i++]  = '<span id="aud_anrede">Herr</span> ';
                    if(val.Aussendienst.aud_anrede == 2) textToInsert[i++]  = '<span id="aud_anrede">Frau</span> ';
                    textToInsert[i++]  = '<span id="aud_vorname">'+val.Aussendienst.aud_vorname+'</span> ';
                    textToInsert[i++]  = '<span id="aud_nachname">'+val.Aussendienst.aud_nachname+'</span><br />';
                    textToInsert[i++]  = '<span id="aud_firma">'+val.Aussendienst.aud_firma+'</span><br />';
                    textToInsert[i++]  = '<span id="aud_strasse">'+val.Aussendienst.aud_strasse+'</span> ';
                    textToInsert[i++]  = '<span id="aud_nr">'+val.Aussendienst.aud_nr+'</span><br />';
                    textToInsert[i++]  = '<span id="aud_land">'+val.Aussendienst.aud_land+'</span> - ';
                    textToInsert[i++]  = '<span id="aud_plz">'+val.Aussendienst.aud_plz+'</span> ';
                    textToInsert[i++]  = '<span id="aud_ort">'+val.Aussendienst.aud_ort+'</span><br />';
                    textToInsert[i++]  = '<span id="aud_tel"><span style="margin-right: 20px;">Tel.:</span>'+val.Aussendienst.aud_tel+'</span><br />';
                    textToInsert[i++]  = '<span id="aud_fax"><span style="margin-right: 20px;">Fax:</span>'+val.Aussendienst.aud_fax+'</span><br />';
                    textToInsert[i++]  = '<span id="aud_mobil"><span style="margin-right: 10px;">Mobil:</span>'+val.Aussendienst.aud_mobil+'</span><br />';
                    textToInsert[i++]  = '<span style="margin-right: 10px;">E-Mail:</span><a href="mailto:'+val.Aussendienst.aud_email+'">'+val.Aussendienst.aud_email+'</a><br /><br />';

                    textToInsert[i++]  = '<span>Ihr Ansprechpartner Innendienst: </span><br />';
                    if(val.Innendienst.ind_anrede == 1) textToInsert[i++]  = '<span id="ind_anrede">Herr</span> ';
                    if(val.Innendienst.ind_anrede == 2) textToInsert[i++]  = '<span id="ind_anrede">Frau</span> ';
                    textToInsert[i++]  = '<span id="ind_vorname">'+val.Innendienst.ind_vorname+'</span> ';
                    textToInsert[i++]  = '<span id="ind_nachname">'+val.Innendienst.ind_nachname+'</span><br />';
                    textToInsert[i++]  = '<span id="ind_durchwahl"><span style="margin-right: 20px;">Tel.:</span>+49 (271) 33 85 - '+val.Innendienst.ind_durchwahl+'</span><br />';
                    textToInsert[i++]  = '<span id="ind_fax"><span style="margin-right: 20px;">Fax:</span>+49 (271) 33 85 - 50</span><br />';
                    textToInsert[i++]  = '<span style="margin-right: 10px;">E-Mail:</span><a href="mailto:'+val.Innendienst.ind_email+'">'+val.Innendienst.ind_email+'</a><br /><br />';
                
                    $('#plzdiv').prepend(textToInsert.join(''));
                    delete textToInsert;

                    $("#default").modal({
                        minHeight:320,
	                    minWidth: 300,
                        opacity:50,
                        overlayCss: {backgroundColor:"#fff"},
                        position: ['20%']
                    });
                    $('.close').click(function() {
                        $.modal.close();
                        $("#default").hide();
                    });
            });
        }
});


/***
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
***/

function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    // uncomment the 3 lines below to pull the images in random order

    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}
function slideSwitch2() {
    var $active = $('#slideshow2 IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow2 IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow2 IMG:first');

    // uncomment the 3 lines below to pull the images in random order

    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}
function slideSwitch3() {
    var $active = $('#slideshow3 DIV.active');

    if ( $active.length == 0 ) $active = $('#slideshow3 DIV:last');

    // use this to pull the divs in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow3 DIV:first');

    // uncomment below to pull the divs randomly
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 5000 );
    setInterval( "slideSwitch2()", 5000 );
    setInterval( "slideSwitch3()", 5000 );
});
