$(document).ready(function(){

    /*Fuer frontend*/
    $('#suchfeld').click(
        function(){
            if($('#suchfeld').val() == 'Suche'){
                $('#suchfeld').val('')
            }
            $('#suchfeld').css('border-bottom', '1px solid #cc6633');

        }
        );
    $('#suchfeld').focusout(
        function(){
            $('#suchfeld').css('border', 'none');
        }
        );

    $('input.textarea').each(
        function(){
            var name = $(this).attr('name');
            //alert(name);
            $(this).replaceWith('<textarea cols="10" rows="10" name="'+name+'"></textarea>');

        }
        );

    /*Fuer frontend ende*/

    $(function() {
        $("#sortable").sortable();
        $("#sortable").disableSelection();
    });

    $('#picture_left_preview_image').click(
        function(){
            showPictures(true);
        }
        );

    getlinkforpicturepreview();

    $("#picture_upload").uploadify({
        'uploader'       : '/js/uploadify/uploadify.swf',
        'script'         : '/js/uploadify/uploadify.php',
        'cancelImg'      : '/js/uploadify/cancel.png',
        'buttonImg'      : '/images/picture_upload_button.png',
        'folder'         : '/images/einzelseiten',
        'queueID'        : 'fileQueue',
        'fileExt'        : '*.jpg;*.jpeg;*.png;*.gif',
        'auto'           : true,
        'multi'          : true
    });



});



function checkContent(){
    $('#edit_content_form input[name="name"]').css('background-color', '#fff');
    if(($('#edit_content_form input[name="name"]').val() == '') && ($('#edit_content_form input[name="submit"]').val() == 'Speichern')){
        $('#edit_content_form input[name="name"]').css('background-color', '#990000').effect('pulsate');
        return false;
    }
    else {
        return true;
    }
}

function highlight_stop(){
    $('.highlight_x').css("visibility", "hidden");
    $("*").removeHighlight();

}

function showNotifychange(text){

    if(text != 'false'){
        $('#notifychange').html(text);
    }
    else{
        $('#notifychange').html('Ihre &Auml;nderungen wurden &uuml;bernommen !');
    }
    $('#notifychange').animate({
        opacity: 0.0
    }, {
        duration: 0,
        queue: true
    });
    $('#notifychange').show(0);
    $('#notifychange').animate({
        opacity: 1.0,
        top: '100'
    }, {
        duration: 1000,
        queue: true
    })
    .animate({
        opacity: 1.0
    }, {
        duration: 1000,
        queue: true
    })
    .animate({
        opacity: 0.0,
        top: '-200'
    }, {
        duration: 1000,
        queue: true
    }).hide(10);
}

function showPictures(insert_picture_link, insert_in_tinymce) {
    /*wenn insert_picture_link = true: es soll ein link unter dem Bild angezeigt werden, um dieses als Bild fuer die linke Seite einzutragen*/
    closeMenuPositions();
    closeSettings();
    $('#bilderuebersicht_close').animate({
        opacity: 0.0
    }, {
        duration: 0,
        queue: true
    });
    $('#bilderuebersicht_close').show(0);
    $('#bilderuebersicht_close').animate({
        opacity: 1.0
    }, {
        duration: 1000,
        queue: true
    });
    document.getElementById('bilderuebersicht_close').innerHTML = 'Einen Moment bitte, die Bilder werden geladen';
    $('#bilderuebersicht').animate({
        opacity: 0.0
    }, {
        duration: 0,
        queue: true
    });
    $('#bilderuebersicht').show(0);
    $('#bilderuebersicht').animate({
        opacity: 1.0
    }, {
        duration: 1000,
        queue: true
    });
    if(insert_picture_link){
        var valuepair = 'with_link=true';
        //alert('ja');
        if(insert_in_tinymce){
            valuepair += '&in_tinymce=true';
        //alert(valuepair);
        }
    }

    else{
        //alert('nein');
        valuepair = 'with_link=false';
    }
    $.get("pages/show_pictures.php?", valuepair , function(data){
        document.getElementById('bilderuebersicht').innerHTML = data;
    });
    document.getElementById('bilderuebersicht_close').innerHTML = '<div id=\"edit_div_close\"><a href=\"#\" onclick=\"closePictures(); return false;\">X</a></div>';
/*window.open(
      "pages/show_pictures.php",
      "Auf den Server geladene Bilder",
      "width=1000,height=700,resizable=1,scrollbars=1,menubar=0,toolbar=0,location=1,status=0"
    );*/
}
function closePictures(){
    $('#bilderuebersicht').animate({
        opacity: 0.0
    }, {
        duration: 1000,
        queue: true
    }).hide(10);
    $('#bilderuebersicht_close').animate({
        opacity: 0.0
    }, {
        duration: 1000,
        queue: true
    }).hide(10);
}

function showSettings() {
    closeMenuPositions();
    closePictures();
    $('#settings_close').animate({
        opacity: 0.0
    }, {
        duration: 0,
        queue: true
    });
    $('#settings_close').show(0);
    $('#settings_close').animate({
        opacity: 1.0
    }, {
        duration: 1000,
        queue: true
    });
    document.getElementById('settings_close').innerHTML = 'Einen Moment bitte, die Bilder werden geladen';
    $('#settings').animate({
        opacity: 0.0
    }, {
        duration: 0,
        queue: true
    });
    $('#settings').show(0);
    $('#settings').animate({
        opacity: 1.0
    }, {
        duration: 1000,
        queue: true
    });
    
    var valuepair = '';
    $.get("pages/show_settings.php?", valuepair , function(data){
        document.getElementById('settings').innerHTML = data;
    });
    document.getElementById('settings_close').innerHTML = '<div id=\"edit_div_close\"><a href=\"#\" onclick=\"closeSettings(); return false;\">X</a></div>';
/*window.open(
      "pages/show_pictures.php",
      "Auf den Server geladene Bilder",
      "width=1000,height=700,resizable=1,scrollbars=1,menubar=0,toolbar=0,location=1,status=0"
    );*/
}
function closeSettings(){
    $('#settings').animate({
        opacity: 0.0
    }, {
        duration: 1000,
        queue: true
    }).hide(10);
    $('#settings_close').animate({
        opacity: 0.0
    }, {
        duration: 1000,
        queue: true
    }).hide(10);
}
function showOverlayDiv(which, value1) {

    $('#overlay_div').animate({
        opacity: 0.0
    }, {
        duration: 0,
        queue: true
    });
    $('#overlay_div_close').show(0);
    $('#overlay_div_close').animate({
        opacity: 1.0
    }, {
        duration: 1000,
        queue: true
    });
    document.getElementById('overlay_div_close').innerHTML = 'Einen Moment bitte, die Daten werden geladen';
    $('#overlay_div').animate({
        opacity: 0.0
    }, {
        duration: 0,
        queue: true
    });
    $('#overlay_div').show(0);
    $('#overlay_div').animate({
        opacity: 1.0
    }, {
        duration: 1000,
        queue: true
    });

    var valuepair = value1;
    $.get("pages/show_"+which+".php?", valuepair , function(data){
        document.getElementById('overlay_div').innerHTML = data;
    });
    document.getElementById('overlay_div_close').innerHTML = '<div id=\"edit_div_close\"><a href=\"#\" onclick=\"closeOverlayDivs(); return false;\">X</a></div>';
/*window.open(
      "pages/show_pictures.php",
      "Auf den Server geladene Bilder",
      "width=1000,height=700,resizable=1,scrollbars=1,menubar=0,toolbar=0,location=1,status=0"
    );*/
}
function closeOverlayDivs(){
    $('#overlay_div').animate({
        opacity: 0.0
    }, {
        duration: 1000,
        queue: true
    }).hide(10);
    $('#overlay_div_close').animate({
        opacity: 0.0
    }, {
        duration: 1000,
        queue: true
    }).hide(10);
}

function openMenuPositions(){
    closePictures();
    closeSettings()
    $('#edit_div_position_list').animate({
        opacity: 0.0
    }, {
        duration: 0,
        queue: true
    });
    $('#edit_div_position_list').show(0);
    $('#edit_div_position_list').animate({
        opacity: 1.0
    }, {
        duration: 1000,
        queue: true
    });
}
function closeMenuPositions(){
    $('#edit_div_position_list').animate({
        opacity: 0.0
    }, {
        duration: 1000,
        queue: true
    }).hide(10);
}

function updatePositions(){
    document.getElementById('updatePositions_status').innerHTML = '...';
    var i = 0;
    $('.ui-state-default').each(
        function(){
            var id = $(this).attr('id');
            var index = i;
            //alert('id:'+ id + ', index'+ index);
            var valuepair = id + '=' + index;
            $.get("pages/update_positions.php?", valuepair , function(data){
                
                });
            i++;
        }
        ); 
    var text = '&Auml;nderungen wurden &uuml;bernommen!';
    document.getElementById('updatePositions_status').innerHTML = text;
    reload_page('Die Seitenpositionen wurden ge&auml;ndert !');
    return false;
}
function updateSettings(){
    //document.getElementById('updateSettings_status').innerHTML = '...';
    var fields = $("#settings :input").serializeArray();
    var valuepair = '';
    jQuery.each(fields, function(i, field){
        valuepair = valuepair + escape(field.name) + "=" + escape(field.value) + "&";
    });
    //alert(valuepair);
    $.get("pages/update_settings.php?", valuepair , function(data){

        });
    
    //var text = '&Auml;nderungen wurden &uuml;bernommen!';
    //document.getElementById('updatePositions_status').innerHTML = text;
    reload_page('Die Einstellungen wurden ge&auml;ndert!');
    return false;
}
function updateUser(){
    //document.getElementById('updateSettings_status').innerHTML = '...';
    var fields = $("#user_form :input").serializeArray();
    var valuepair = '';
    jQuery.each(fields, function(i, field){
        valuepair = valuepair + escape(field.name) + "=" + escape(field.value) + "&";
    });
    //alert(valuepair);
    $.get("pages/update_users.php?", valuepair , function(data){
        reload_page(data);
    });

    //var text = '&Auml;nderungen wurden &uuml;bernommen!';
    //document.getElementById('updatePositions_status').innerHTML = text;
    
    return false;
}
function deleteUser(valuepair){
    $.get("pages/update_users.php?", valuepair , function(data){
        reload_page(data);
    });
    return false;
}


function setPictureLeft(link){
    $('#submitform_bild').val(link);
}

function reload_page(state){
    state = escape(state);
    setTimeout("self.location.href='?notifychange="+state+"'",500);
}

function loadanfrage(welche){
    $("form").each(function() {
        this.reset();
    });
    $('#contactform_anfrage').hide();
    $('#contactform_prospekt').hide();
    $('#contactform_potentialanalyse').hide();
    $('#contactform_'+welche).show("slow");
    $('#objecttypeSonstige').hide();

    $("#contactmenu button").each(function() {
        $(this).removeClass('activemenubutton');
    });
    $("#"+welche+"_button").addClass('activemenubutton');
}

function sendanfrage(welche){
    var fields = $("#contactform_"+welche+" :input").serializeArray();
    $('#content').html(
        '<img src=\"/images/wait.gif\" /> Ihre Anfrage wird bearbeitet. Bitte haben Sie einen Moment Gedult... '
        );
    var valuepair = '';
    jQuery.each(fields, function(i, field){
        valuepair = valuepair + escape(field.name) + "=" + escape(field.value) + "&";
    });
    /*alert(valuepair);*/
    $.get("pages/contactconfirm.php?", valuepair , function(data){
        $('#content').html(data);
    });
    return false;
}

function checkObjectType(value){
    if(value == 'Sonstige...'){
        $('#objecttypeSonstige').show("slow");
    }
    else{
        $('#objecttypeSonstige').val("");
        $('#objecttypeSonstige').hide("slow");
    }
}

function getlinkforpicturepreview(){
    var imagelink = $('#submitform_bild').val();
    //alert(imagelink);
    if(imagelink == ''){
        $('#picture_left_preview_image').attr('src', std_bild_1);
    }
    else{
        $('#picture_left_preview_image').attr('src', imagelink);
    }
}

function delete_picture(link){
    //alert(file);
    var valuepair = "link="+link;
    $.get("pages/delete_picture.php?", valuepair , function(data){
        $('#confirm').html(data);
    });
    $('#confirm').show(1000);
}
function really_delete_picture(link){
    //alert(file);
    var valuepair = "link="+link;
    $.get("pages/really_delete_picture.php?", valuepair , function(data){
        var state = 'Bild: '+link+' wurde gel&ouml;scht';
        showNotifychange(state);
    //alert(data);
    });
    confirm_close();
    closePictures();
}

function confirm_close(){
    $('#confirm').hide(1000);
}

function uploadPicture(){
    alert('upload');
    
}
