var clearInput = false;
var mandatory = Array();
var winTargetPos = 100;
var winOffsetLeft = 20; 
var userVote;
var sttUserRatings;

jQuery(document).ready(function(){
    // Bannerwerbung in den Hintergrund legen
    jQuery('.container_skyscraper').css('zIndex',0);

    var movieCookie = "MOVIERATING_" + jQuery('#movieID').val();
    var doneVoting = false;

    if (jQuery('#doneVoting').attr('value')!=0) doneVoting = true;

    var kekse = document.cookie.split(";");
    for (var idx in kekse)
    {
        var keks = kekse[idx].split("=");
        if (keks[0].match(movieCookie) && keks[1] == 1)
        {
            doneVoting = true;
            break;
        }
    }

    if  (doneVoting == true)
    {
        jQuery("#commentlink1").hide();
        jQuery("#commentlink2").show();
    }
    else
    {
        // KeyUp Eventlistener nur initialisieren wenn noch nicht abgestimmt ...
        jQuery('#stt_kinoUserComments form').bind('keyup', setBorders);
    }

    // Start Snip. vorher : pu_all/scripts_kino.js
    // 
    //Stars for "Aktuell im Kino"-page
    jQuery("form[name=teaser_rating]").stars({
        disabled: true
    });

    // Create stars for: Tagblatt Wertung
    jQuery("#ratings").stars({
        disabled: true
    });

    jQuery("#ratings2").stars({
        disabled:true
    });

    var caption = jQuery('<span style="margin-left:10px;"></span>');

    jQuery("#stt_UserRatings").stars({
        inputType: "select",
        cancelShow: false,
        captionEl: caption,
        oneVoteOnly: false,
        callback: function(ui, type, value)
        {
            sttUserRatings = ui;
            userVote = value;
        }
    });
    
    caption.appendTo("#stt_UserRatings");

    // ENDE Snip 


    // Overlay definieren Kommentare abgeben Form
    jQuery(".stt_commentlink").overlay({
        // some expose tweaks suitable for modal dialogs
        expose: {
            color: '#333',
            loadSpeed: 200,
            opacity: 0.8
        },
    
        closeOnClick: true,
        absolute: true,
        left: '20px',
        onBeforeLoad:function() {
            if (jQuery('#commentlink1').is(':visible') && jQuery('#doneVoting').attr('value')!= 0) return false; else return true;
        },
        onLoad:function(){

            if (jQuery('#commentlink1').is(':visible'))
            {
                divBox = jQuery('#stt_kinoUserComments');
            }
            else
            {
                divBox = jQuery('#stt_kinoUserComments_voted');
            }
            
            winLeftPos = winOffsetLeft;
            winTopPos  = winTargetPos + jQuery(document).scrollTop();
            divBox.css({
                "left"     : 20,
                "top"      : 40,
                "position" : "fixed"
            });
            
        }

    });


    jQuery("#stt_kinoUserComments form").submit(function(e){

        // Override Submit Method
        e.preventDefault();

        // Submit Button nach einmal klicken deaktivieren
        jQuery('#stt_userSubmit').attr('disabled','disabled');


        // Wenn UserRating Stars geklickt wurden, Stars nach dem Form submit auf disabled stellen
        if (sttUserRatings) sttUserRatings.disable();

        // Pruefe ob Benotung und Kommentar oder nur eins von beidem ausgefuellt wurde
        var comment = jQuery('#sttComment').val();
        var userRating = false;
        for(var i=1; i<6;i++)
        {
            var voted = jQuery('input[name="note'+i+'"]').val();
            if (voted)
            {
                userRating = true;
                break;
            }
        }

        if (userRating == true) mandatory = Array('sttCaptcha');
        if (comment != '')      mandatory = Array('sttName', 'sttCaptcha');

        var data = {    
            name: jQuery("#sttName", this).val(),
            email: jQuery("#sttEmail", this).val(),
            captcha: jQuery("#sttCaptcha", this).val(),
            captcha_eval: jQuery("#sttHiddenCaptcha",this).val(),
            comment: jQuery("#sttComment", this).val(),
            movieID: jQuery('#movieID', this).val(),
            movieName: jQuery('#movieName', this).val(),
            userRating: userVote,
            note1:jQuery('input[name="note1"]', this).val(),
            note2:jQuery('input[name="note2"]', this).val(),
            note3:jQuery('input[name="note3"]', this).val(),
            note4:jQuery('input[name="note4"]', this).val(),
            note5:jQuery('input[name="note5"]', this).val()
        };
        jQuery.post("/pu_st/templates/stt/kinofilm-evaluate-comments.php", data, evaluateUserInput);
    })


    jQuery(window).bind("resize scroll",function(e){

        if (jQuery('#commentlink1').is(':visible'))
        {
            divBox = jQuery('#stt_kinoUserComments');
        }
        else
        {
            divBox = jQuery('#stt_kinoUserComments_voted');
        }
        winLeftPos = winOffsetLeft;


//        if ( jQuery(window).height() > 800 ) {
//
//            winTopPos = winTargetPos + jQuery(document).scrollTop();
//            divBox.css({
//                "left":winLeftPos,
//                "top":winTopPos
//            });
//
//        }
    })


}) /* END document.ready */

/* --------------------------------------------------------------------------- */

function evaluateUserInput(data, response)
{
    if (data == 1)
    {
        jQuery("#serverResponse").html("<h2>Ihr Kommentar wurde versendet und wird in ca. 30 Minuten online sein</h2>");
        jQuery("#serverResponse").slideDown(500);
        clearInput = true;
        jQuery('#doneVoting').attr('value',1);
        $('#stt_kinoUserComments').hide();
    }
    else
    {
        var response = "";

        if (data == 0) response = "<h2>Bitte pruefen Sie den Spam-Schutz-Code...</h2>";
        if (data == -1) response = "<h2>Bitte fuellen Sie das rot markierte Feld aus...</h2>";
        if (data == -2) response = "<h2>Bitte fuellen Sie die rot markierten Felder aus...</h2>";
        if (data == -3) response = "<h2>Bitte Benoten oder Kommentieren Sie den Film</h2>";

        if (sttUserRatings) sttUserRatings.enable();
        jQuery('#stt_userSubmit').removeAttr('disabled');
        
        // Captcha fehlgeschlagen - Kommentarbox bleibt offen - Fehlermeldung anzeigen

        jQuery("#serverResponse").html(response);
        jQuery("#serverResponse").slideDown(500);
        clearInput = false;
        for(var foo in mandatory)
        {
            if(jQuery("#" + mandatory[foo]).attr('value') == "") jQuery("#" + mandatory[foo]).css("border", "1px solid #FF0000");
        }
        window.setTimeout(hideOutputBox, 3000);
    }
}

function hideOutputBox(p) {
    jQuery("#serverResponse").slideUp(300,
        function(){
            // Hinweistafel wieder ausblenden
            jQuery("#serverResponse").fadeOut(500);

            if (clearInput == true)
            {
                // Formfelder leeren
                jQuery("#sttName").attr('value','');
                jQuery("#sttEmail").attr('value','');
                jQuery("#sttCaptcha").attr('value','');
                jQuery("#sttComment").attr('value','');

                // Overlay schliessen - Link1 ausblenden - Link2 anzeigen
                jQuery(".stt_commentlink").overlay().close();
                
                jQuery("#commentlink1").hide();
                jQuery("#commentlink2").show();

                // hidden felder noteX plus eins
                // jQuery("input[name='newsletter']")
                var foo = jQuery('input[name="note'+userVote+'"]').val();
                foo ++;
                jQuery('input[name="note'+userVote+'"]').attr('value', foo);
                
            }
            else if (clearInput == false)
            {
                setBorders();
            }

        }
        )
}

function setBorders() {

    for (var foo in mandatory)
    {
        var nodeItem = "#" + mandatory[foo];
        var txtval = jQuery(nodeItem).attr('value');
        if (txtval != "") jQuery(nodeItem).css("border", "1px solid #7F9DB9");
    }

}
