
/* - ++resource++monopol-website.js - */
// batching
jq.fn.simple_batching = function() { 
    // up/down
   return this.each(function() {
       obj = jq(this);
       obj.find('.batch').hide();
       obj.find('.batch:first').show()
       obj.find('#down').click(function() {
           var args = jq(this).attr('class').split('-');
           var index = args[2];
           jq(this).parent().find('.batch').hide();
           jq(this).parent().find('#batch-'+index).show();
           down_id = (parseInt(args[2])+1)>parseInt(args[1]) ? '1' : (parseInt(args[2])+1);
           jq(this).attr('class', args[0]+'-'+args[1]+'-'+down_id);
           up_id = (parseInt(args[2])-1)==0 ? args[1] : (parseInt(args[2])-1);
           jq(this).parent().find('#up').attr('class', args[0]+'-'+args[1]+'-'+up_id)
       }) 
       obj.find('#up').click(function() {
           var args = jq(this).attr('class').split('-');
           var index = args[2];
           jq(this).parent().find('.batch').hide();
           jq(this).parent().find('#batch-'+index).show();
           up_id = (parseInt(args[2])-1)==0 ? args[1] : (parseInt(args[2])-1);
           jq(this).attr('class', args[0]+'-'+args[1]+'-'+up_id);
           down_id = (parseInt(args[2])+1)>parseInt(args[1]) ? '1' : (parseInt(args[2])+1);
           jq(this).parent().find('#down').attr('class', args[0]+'-'+args[1]+'-'+down_id);
       })
   });  
};

jq.fn.paged_batching = function() { 
   return this.each(function() {
       obj = jq(this);
       obj.find('.batch').hide();
       obj.find('.batch:first').show()
       obj.find('.navigation a').click(function() {
           var args = jq(this).attr('class').split('-');
           var index = args[2];
           jq(this).siblings().removeClass('current_page');
           jq(this).attr('class', jq(this).attr('class') + ' current_page');
           jq(this).parent().parent().find('.batch').hide();
           jq(this).parent().parent().find('#batch-'+index).show();
       }) 
   });  
};




function delete_foto () {
    id = jq(this).parent().children('img')[0].id;
    var args = 'id='+id;
    jq.ajax({
        url: portal_url+'/remove_location_temp_foto',
        type: 'POST',
        data: args,
        timeout: 5000,
        success: function(id){
            jq('#'+id).parent().remove();
            jq('.form_message').text('Ihr Foto wurde gelöscht.')
            setTimeout(function() {
                jq('.form_message').text(''); 
            }, 3000);
        },
        error: function(){
            jq('.form_message').text('Foto konnte nicht gelöscht werden.');
            setTimeout(function() {
                jq('.form_message').text(''); 
            }, 3000);
        }
    });
}

        
jq(document).ready(function() {
    // Merken
    jq('.item_merken').addClass('item_merken_js');
    jq('.item_merken_js').click(function(event) {
        event.preventDefault();
        id = this.id;
        var idl = id.split(',');
        item_id = idl[1];

        var args = 'item_id='+item_id + '&ajax=1';

        jq.ajax({
            url: portal_url+'/merken',
            type: 'POST',
            data: args,
            timeout: 10000,
            error: function(){
                jq('.action_message').text('Es ist ein Fehler beim Merken aufgetreten.');
                jq('.action_message').show();
                setTimeout(function() {
                    jq('.action_message').text('');
                    jq('.action_message').hide();
                }, 3000);
            },
            success: function(msg){
                jq('.action_message').text(msg);
                jq('.action_message').show();
                setTimeout(function() {
                    jq('.action_message').text('');
                    jq('.action_message').hide();
                }, 3000);
            }
        });
    })



    // switching the visibility of the special subscriber elements
    jq('input#already_subscriber_yes').click (function() {
        jq('div#subscriber_fields').show('slow');
    })
    jq('input#already_subscriber_no').click (function() {
        //x = jq('div#subscriber_fields');r = "";for(a in x) { r += a + ":" + x[a]; }alert(r);
        jq('div#subscriber_fields').hide('slow');
    })
    
    
    /* lokales */
    jq('#lokales .button').hide();
    jq('#lokales .show').show();
    
    jq('.lokales-change').change( function () {
        jq(this).siblings('.button')[0].click();
    });    
    jq('.lokales-change-city').change( function () {
        var l = jq(this).val().split('-');
        if (l.length==2) {
            window.location ="./lokales/stadt-hilfe?modus=stadt-hilfe";
        } else {
            jq(this).siblings('.button')[0].click();
        }        
    });
    
    /* recommendation portlet */
    /* in Detail Ansicht ohne Auswahl der Kategorie */
    jq('a.new-location').click(function(event) {
        event.preventDefault();
        jq('#new-location-button').click();
    });
    
    
    /* recommendation form */
    // sind wir in "Neue Location" oder "Neue Stadt"?
    if (jq('form.kssattr-formname-neue-location').length>0 || jq('form.kssattr-formname-neue-stadt').length>0) {
        
        jq('#lokales .enableFormTabbing').removeClass('enableFormTabbing').removeClass('rowlike')
        //rowlike enableUnloadProtection enableFormTabbing kssattr-formname-None

        // TODO on change category:encoding von vocabulary terms
        jq("#form-widgets-categories-category").change( function () {    
            jq('#formfield-form-widgets-categories-subcategory').hide();
            jq('#form-widgets-categories-subcategory option').remove();
            var selected = jq('#form-widgets-categories-category').val();
            jq('#form-widgets-categories-subcategory').ajaxAddOption("@@freitag-website-utility/subcategories?category_link=" + selected, {}, false, function() {
                // unfortunatly no callback for empty set of options
                if (jq('#form-widgets-categories-subcategory option').length<1) {
                    jq('#formfield-form-widgets-categories-subcategory').hide()
                } else {
                    jq('#formfield-form-widgets-categories-subcategory').show();

                    // preset subcategory from hidden values (see location-info)
                    var subcategory = jq('#subcategory').val();
                    var wi_sub = jq('#form-widgets-categories-subcategory');
                    if (subcategory!='') {
                        wi_sub.selectOptions(subcategory);
                    } else {
                        // das erste selektieren
                        wi_sub.selectOptions(wi_sub[0].options[0].value);
                    }
                }
            });

        });

        // Preselection category and subcategory
        var category = jq('#category').val();
        var reccategory = jq('#recommendation-category').val();
        if (reccategory != 'all') {
            new_category = reccategory;
        } else if (category != '') {
            new_category = category;
        } else {
             new_category = 'restaurants';
        }
        
        jq('#form-widgets-categories-category').selectOptions(new_category);
        // fire event for update of subcategories
        jq("#form-widgets-categories-category").change();
        // select subcategory
        // we do this in callback of ajaxAddOption
        
        
        
        if (jq('form.kssattr-formname-neue-stadt').length>0) {
            // move city field
            jq('#recommendation-userdata').css({'height': '120px'})
                .append(jq('#fieldset-0 .row').html());
            jq('#fieldset-0').remove();
            
            jq('#fieldset-default').remove();
            jq('#fieldset-1 legend').remove();
            jq('#fieldset-1').prepend('<div class="legend">Eine Location in dieser Stadt empfehlen</div>')
            jq('fieldset').hide()
            jq('#fieldset-1').show()
            jq('#fieldset-1').click(function() {
               jq('fieldset').show() 
            })
                
        }
        
        if (jq('form.kssattr-formname-neue-location').length>0) {
            // warum hat 'neue-location' das fieldset fuer die stadt?!?
            //jq('#fieldset-0').remove()
        }
        
        
        
        // move rating
        jq('#fieldset-3').append(jq('#recommendation-rating'))
        // move userdata
        jq('.enableUnloadProtection').prepend(jq('#recommendation-userdata'))
        // move location-info
        jq('.enableUnloadProtection').prepend(jq('#location-info input'))
        // move cityname
        jq('#formfield-form-widgets-address-zip').append(jq('#cityname'))
        // move foto upload box
        jq('.formFields').before(jq('#location_fotos'));
        
        //add twister and bind toggle
        jq('.enableUnloadProtection fieldset').addClass('closed')
            .prepend('<div class="location_recommend_twister closed"><img /></div>')
            .children('.row').hide()
        jq(".location_recommend_twister img").attr('src', 'twister-closed.gif');  
        
        jq("div.location_recommend_twister").click(function () {
            // andere schliessen
            var twisters = jq('div.location_recommend_twister');
            twisters.siblings('.row').hide()
            twisters.children('img').attr('src', 'twister-closed.gif');
            twisters.parent().addClass('closed').removeClass('open')
            // selber oeffnen
            jq(this).siblings('.row').show()
            jq(this).children('img').attr('src', 'twister-open.gif');
            jq(this).parent().addClass('open').removeClass('closed')
            });
            
        // es sternli vornedra
        jq('.enableUnloadProtection legend').each(function(i) {
            jq(this).parent().prepend('<div class="legend">'+ jq(this).text() +'</div>');
            jq(this).remove();
        })
        
        // required
        jq('label').filter(function (index) {
            return jq(this).siblings('.fieldRequired').length>0;
            }).addClass('required')
        
        
        /* fotos*/
        
        // upload foto
        var options = { 
            target:     '#upload_target', 
            success:    function(msg) { 
                if (msg.search(/&lt.+/)) { // image tag ohne error (<pre>&lt;img...) (TODO: do that serious, girl!)
                    // thumb an die bestehenden reihen
                    var button = '<div class="foto_delete"><img src="icon_delete.gif"  title="Foto l&ouml;schen"/></div>'
                    var html = '<div class="fotocontainer">' + jq('#upload_target pre').text() + button + '</div>'
                    jq('#fotos').append(jq(html))
                    jq('.foto_delete').click(delete_foto);
                    jq('.form_message').text('Foto wurde hochgeladen.');
                     setTimeout(function() {
                         jq('.form_message').text(''); 
                     }, 3000); 
                } else {                    
                    jq('.form_message').html(msg); //text(msg.replace('<pre>','').replace('</pre>',''));
                     setTimeout(function() {
                         jq('.form_message').text(''); 
                     }, 3000);
                };
                jq('#form_location_fotos .button_submit').removeClass('submitting');
            },
            error: function(msg) {
                // wird leider von jquery.form nicht beachtet
                jq('.form_message').text(msg);
                 setTimeout(function() {
                     jq('.form_message').text(''); 
                 }, 3000);
            } 
        }; 
        jq('#form_location_fotos').ajaxForm(options);        
        
        
    }  ///* recommendation form */  
    
    
    // best of
    jq('#bestof-viewlet .content').hide();
    jq('#bestof-viewlet .content:first').show();
    jq('.bestof_link').click(function() {
        jq('#bestof-viewlet .content').hide();
        jq(this).parents('.section').children('.content').show();
    })
    
    // more locations
    jq('.batch').hide();
    jq('.batch:first').show()
    jq('#down').click(function() {
        args = jq(this).attr('class').split('-');
        var index = args[2];
        jq('.batch').hide();
        jq('#batch-'+index).show();
        down_id = (parseInt(args[2])+1)>parseInt(args[1]) ? '1' : (parseInt(args[2])+1);
        jq(this).attr('class', args[0]+'-'+args[1]+'-'+down_id);
        up_id = (parseInt(args[2])-1)==0 ? args[1] : (parseInt(args[2])-1);
        jq('#up').attr('class', args[0]+'-'+args[1]+'-'+up_id)
    }) 
    jq('#up').click(function() {
        args = jq(this).attr('class').split('-');
        var index = args[2];
        jq('.batch').hide();
        jq('#batch-'+index).show();
        up_id = (parseInt(args[2])-1)==0 ? args[1] : (parseInt(args[2])-1);
        jq(this).attr('class', args[0]+'-'+args[1]+'-'+up_id);
        down_id = (parseInt(args[2])+1)>parseInt(args[1]) ? '1' : (parseInt(args[2])+1);
        jq('#down').attr('class', args[0]+'-'+args[1]+'-'+down_id);
    })

    //slide images edition galery 1 
    jq('#ebvor').click(function() { 
        args = jq(this).attr('class').split('-');
        var index = parseInt(args[1]);
        var len = parseInt(args[2])
        if (index < len) {
            jq('.editionbild').hide();
            idIndex = index + 1
            jq('#editionbild-' + idIndex).show();
            jq(this).attr('class', 'ebvor-' + idIndex + '-' + len); 
            jq('#ebzurueck').attr('class', 'ebzurueck-' + idIndex + '-' + len);
        }
       
    })
    jq('#ebzurueck').click(function() { 
        args = jq(this).attr('class').split('-');
        var index = parseInt(args[1]);
        var len = parseInt(args[2])
        if (index > 1) {
            jq('.editionbild').hide();
            idIndex = index - 1
            jq('#editionbild-' + idIndex).show(); 
            jq(this).attr('class', 'ebvor-' + idIndex + '-' + len); 
            jq('#ebvor').attr('class', 'ebzurueck-' + idIndex + '-' + len); 
        }         
    })

    //slide images edition galery 2 
    jq('#ebvor2').click(function() { 
        args = jq(this).attr('class').split('-');
        var index = parseInt(args[1]);
        var len = parseInt(args[2])
        if (index < len) {
            jq('.editionbild2').hide();
            idIndex = index + 1
            jq('#editionbild2-' + idIndex).show();
            jq(this).attr('class', 'ebvor2-' + idIndex + '-' + len); 
            jq('#ebzurueck2').attr('class', 'ebzurueck2-' + idIndex + '-' + len);
        }
       
    })
    jq('#ebzurueck2').click(function() { 
        args = jq(this).attr('class').split('-');
        var index = parseInt(args[1]);
        var len = parseInt(args[2])
        if (index > 1) {
            jq('.editionbild2').hide();
            idIndex = index - 1
            jq('#editionbild2-' + idIndex).show(); 
            jq(this).attr('class', 'ebvor2-' + idIndex + '-' + len); 
            jq('#ebvor2').attr('class', 'ebzurueck2-' + idIndex + '-' + len); 
        }         
    })
    
    
    /* location detail view */
    
    // Veraenderungsmitteilung
    
    // default: form ausblenden
    jq('#notify').hide();
    
    //  form zeigen
    jq('.location-detail-content #melden').click(function() {
        //form zeigen
        jq('#notify').show()
    })
    
    // form senden
    var options = { 
        target:     '#notification_form_message', 
        success:    function(msg) {                    
            setTimeout(function() {
                jq('#notification_form_message').text('');
                jq('#notification_message').val('');
                jq('#notify').hide();
            }, 8000);
                 
        },
        error: function(msg) {
          jq('#notification_form_message').val('Fehler beim Versenden der Mail.');
          setTimeout(function() {
              jq('#notification_form_message').text('');
          }, 8000);
        } 
    }; 
    jq('#form_location_notification').ajaxForm(options);
    
});

