/*
 * Script: slideshow for moboPiXX
 */

var interval=null;
var interval1=null;
var imageLoad = false;
var beshrLoad = false;
var time = null;
var bilder = null;
// WHEN DAY CHANGE
function makeonclick() {
    if ( $("#first").val()=="1") { // if new pic must show
        $("select#times > option:last").attr("selected", true); // new selected
        $("select#times").change();
    } else {
        if ($("#slide_hide_type").val()!=2) {
            $("select#times > option:first").attr("selected", true); // old selected
            $("select#times").change();
        } else {
             var hour = $("#slide_options [name=hour]").val();
             $("select#times option[hour="+hour+"]:first").attr("selected", true);  // make this unselected
             $("select#times").change();
        }
    }
}

// check image in cache
function checkCacheOnLoad() {
    if ((beshrLoad)&&(imageLoad)) // if image loaded
    {
        if ($("#slide_status").val()=="1") 
            interval = setInterval(nextPicture, time);
        clearInterval(interval1); 
        interval1 = null;
    }
}

// START
$(document).ready(function(){
    $("#first").val("1");
    $("#slide_status").val("0"); // status slide false
    $("#slide_hide_hour").val("0"); // slideshow hour null
    $("#slide_hide_type").val("0"); // type slideshow null 
    $("select#times").change(changePict);
    $("select#dates").change(changeDay); // dates event click
    $("select#dates > option:last").attr("selected", true);  // selected first
    $("select#dates").change(); // show selected picture
    time = $("select[name=interval]").val()-100;  // default time
    
    //makeonclick();// make for select times change event
});

// for all pictures
function changePict () {
    var src = $("select#times > option:selected").val();
    //alert( $("select#times > option:selected").index());
    beshrLoad = false;
    imageLoad = false;
    if (interval1) //if now picture load
    { 
        clearInterval(interval1);  // kill picture load
        interval1=null; // interval1 make null
    }
    if ($("#slide_status").val()=="1") // if slide make pause
    { 
        clearInterval(interval);
        interval=null;
    } 
    $("#main_pict").attr("src", src); // change pic
    loadBeschreibung();  
    $("#main_pict").load(function() { imageLoad=true;});
    interval1 = setInterval(checkCacheOnLoad, 100);
}
function changeDay() {
    $("#cache_div").html(''); // cache div null
    if ($("#slide_status").val()=="1")  { clearInterval(interval);interval=null;}
    $.ajax({ // gives all pictures
        url: "index.php?task=times",
        data: ({par : $("select#dates > option:selected").val()}),
        cache: false,
        success: function(html){
            $("select#times").html(html);
            makeonclick(); // make for select times change event
            bilder = Array();
            /*$("select#times option").each(function(i,e){
                //alert($(this).val());
                bilder[i] = new Image;
                bilder[i].src = $(this).val();
            })*/
            if ($("#first").val()=="1") // if first pic must show 
                    $("#first").val("0"); // null
        }
    });
        
}
// With Email Form
function doEmailForm() {
    if ($("#for_fr").attr("href")=="#show_email_form") { // if want show email form
        $("#email_form").slideDown(); // show
        $("#for_fr").attr("href", "#hide_email_form") // make for next click hide
    } else {
        $("#email_form").slideUp(); // hide
        $("#for_fr").attr("href", "#show_email_form") // make for next sho
    }
}

// SEND EMAIL
function sendEmail() {
    $.ajax({ // send mail take parametrs for php function
            url: "index.php?task=send_email",
            cache: false,
            data: ({email : $("#email").val(),
                    picture: $("#main_pict").attr("src")
                   }),
            success: function(html){
                alert(html);
            }
        }); 
}

// show next picture
function nextPicture() {
    var thisObj = $("select#times > option:selected");
    if (thisObj.val() == $("select#times > option:last").val()) { //if the last of day pict
        if ($("select#dates > option:selected").val() == $("select#dates > option:last").val()) { // if this day is the last day
            if ( $("#slide_status").val()=="1") stopSlide(); // if slide the stop slide
        } else {
            var obj = $("select#dates > option:selected").next(); // next day
            
            $("select#dates > option:selected").attr("selected", false); // make this day unselected
            obj.attr("selected", true); // make this day selected
            $("select#dates").change(); // next day method
        }
    } else {
        nextObj = thisObj.next(); 
        /*if  (($("#slide_hide_type").val()==2)&&(nextObj.attr("hour")!=$("#slide_hide_hour").val())) {
                if ($("select#dates > option:selected").val() == $("select#dates > option:last").val()) { // if this day is the last day
                    if ( $("#slide_status").val()=="1") stopSlide(); // if slide the stop slide
                } else {
                    var obj = $("select#dates > option:selected").next(); // next day
                    $("select#dates > option:selected").attr("selected", false); // make this day unselected
                    obj.attr("selected", true); // make this day selected
                    $("select#dates").change(); // next day method
                }
            }
         */
         if  (($("#slide_hide_type").val()==2)) {
                if ($("select#dates > option:selected").val() == $("select#dates > option:last").val()) { // if this day is the last day
                    if ( $("#slide_status").val()=="1") stopSlide(); // if slide the stop slide
                } else {
                    var obj = $("select#dates > option:selected").next(); // next day
                    $("select#dates > option:selected").attr("selected", false); // make this day unselected
                    obj.attr("selected", true); // make this day selected
                    $("select#dates").change(); // next day method
                }
            }
         
        else {

                thisObj.attr("selected", false);  // make this time unselected
                nextObj.attr("selected", true);   // make next time selected
                $("select#times").change(); // next time show
            
        }
    }
    
}

//show prev picture
function prevPicture() {
    var thisObj = $("select#times > option:selected"); 
    if (thisObj.val() == $("select#times > option:first").val()) { // if this time is first time of day
        if ($("select#dates > option:selected").val() != $("select#dates > option:first").val()) { // if this day is the first day
                $("#first").val("1"); // first pic must show
                var obj = $("select#dates > option:selected").prev(); // obj next date
                $("select#dates > option:selected").attr("selected", false); // make this date unselected
                obj.attr("selected", true); // make prev date selected
                $("select#dates").change();    // prev date method
        }
    } else {
        nextObj = thisObj.prev();
        thisObj.attr("selected", false);  // make this time unselected
        nextObj.attr("selected", true);   //make prev time selected   
        $("select#times").change(); // show prev pic
    }
}

// Show first picture
function firstPicture() {
    var thisObj = $("select#times > option:selected");
    nextObj = $("select#times > option:first"); // Select first
    thisObj.attr("selected", false); // make this unselected
    nextObj.attr("selected", true);  //make first selected    
    $("select#times").change(); // show first
}

//show last picture
function lastPicture() {
    var thisObj = $("select#times > option:selected"); 
    nextObj = $("select#times > option:last"); // select last
    thisObj.attr("selected", false);  // make this unselected
    nextObj.attr("selected", true);   // make last selected   
    $("select#times").change();
}

// star slide show
function startSlide() {
    var slide_type = $("#slide_options [name=slide_type]:checked").val();
    var hour = $("#slide_options [name=hour]").val(); // hour that entered in input
    $("#slide_options").slideUp();  // hide slideshow options
    $("#slide_hide_hour").val(hour); // entered optios hour for script
    $("#slide_hide_type").val(slide_type); // type slideshow
    if (slide_type == 2) {
        // Select the first picture with entered hour in this day
        var thisObj = $("select#times > option:selected"); 
        nextObj =$("select#times option[hour="+hour+"]:first"); // select last
        thisObj.attr("selected", false);  // make this unselected
        nextObj.attr("selected", true);   // make last selected   
        $("select#times").change();
    }
    $("#slide_status").val("1"); // status slide true 
    $("#slide_hide_hour").val($("#slide_options [name=slide_hour]").val()); // status slide true
    $("#startButton").css("display", "none"); // start buttom hide
    $("#stopButton").css("display", "inline"); // stop button show
    $("#email_form").html("<b>Hinweis:</b> Bitte beachten Sie das Sie zum verschicken ein Bild ausw&auml;hlen m&uuml;ssen. Wenn der Player zum Abspielen der Bilder in Folge l&auml;uft, ist es nicht m&ouml;glich ein Bild zu verschicken. Stopen Sie dazu den Player und w&auml;hlen Sie das Bild.");//error when email send
    
    interval = setInterval(nextPicture, time); // start slide

    
}

//stop slide show
function stopSlide() {
    
    $("#slide_status").val("0"); // status slide false
    $("#slide_hide_hour").val("0"); // slideshow hour null
    $("#slide_hide_type").val("0"); // type slideshow null 
    $("select#times").removeAttr("disabled"); 
    $("#startButton").css("display", "inline"); // start button show
    $("#stopButton").css("display", "none"); // stop button hide
    $("#email_form").html('Email: <input type="text" id="email" value="" style="width:200px;"  /><div style="height: 3px; font-size:1px;">&nbsp;</div><div style="width:100%;text-align:right;"><input type="button" onclick="sendEmail()" value="Abschicken" ></div>');// access to send email
    if (interval1) {
        clearInterval(interval1);
        interval1=null;
    }
    clearInterval(interval); // stop slide
    interval = null;
}


function showSlideOptions() 
{
    if ($("#slide_options").css("display")=='none')
        $("#slide_options").slideDown();
    else
        $("#slide_options").slideUp();
}
function intervalChange()
{
    time = $("select[name=interval]").val() - 100;
}

function saveBeschreibung()
{
    text_send = $("textarea[name=about]").val();
    $.ajax({        
        cache: false, 
        type: "POST",
        url: "index.php?task=savetext",
        data: ({
            text:text_send,
            img: $("#main_pict").attr("src")  
        }),
        success: function (html){
               alert(html);
        }                
     });
}

function loadBeschreibung()
{
    var src=$("#main_pict").attr("src");
    if (src)
    {
    $.ajax({        
        cache: false, 
        url: "index.php?task=loadtext",
        data: ({
            img: src                                 
        }),
        success: function (html){
            var status = $("#status").val();
            if (status==1) $("#about").val(html);
            else { 
                if (html) {
                    $("#about").html(html);
                    $("#about").css("font-size", "14px");
                }
                else {
                    $("#about").html("&nbsp;"); 
                    $("#about").css("font-size", "0px"); 
                }
                
            }
            beshrLoad = true;   
            
        }                
     });
    }
}

