var DR = '/';
if (document.location.href.match(/\/kulturwegweiser\//)) {
   DR += 'kulturwegweiser/';
}

function openHelpWindow(problem) {
   window.open(DR+'de_DE/help/'+problem, '', 'width=300,height=350');
};

$(document).ready(function() {
    
   $.cbNotebook();
   $.cbCheckbox();
   
   // image zoom
   $("img.imgZoom").each(function() {
      Shadowbox.setup($(this).parent()[0], {
         gallery: "slideshow"
      });
      $(this).click(function() {
         $(this).parent().click();
      });
   });
   
   // search
   $('.search img').click(function() {
      doSiteSearch();
   });

   $('.search input').keypress(function(key) {
      if (key.which == 13) {
         doSiteSearch();
      }
   });

   $('.search input').focus(function(){
      if (!$(this).hasClass('active')) {
         $(this).attr('class', 'active').val('');
      }
   });

   // Event Calendar
   $('.cal_area').change(function() {
      $('.cal_city').load(DR+'ajax.php?method=createCities&region='+
         $(this).val()
      );
   });

   $('#event-form-submit').click(function() {
      submitEventSearchForm();
   });
   

   //associations
   $('#associations-form-submit').click(function() {
      submitAssociationsSearchForm();
   });

   $('#location-further-events').click(function() {
      $('#location-further-events-block').toggle();
   });

   //persons
   $('#persons-form-submit').click(function() {
      submitPersonsSearchForm();
   });
   
   //culture
   $('#culture-form-submit').click(function() {
      submitCultureSearchForm();
   });

   // culture edit form

   $('#culture-edit-form-submit').click(function() {
      var send = true;
      $('#culture-edit-form .required').each(function(){
         if($(this).val() == '' || $(this).val() == 0)
            send = false;
      });
      if(send)
         $('#culture-edit-form').submit();
      else
         alert('Bitte alle erforderlichen Formularfelder ausfüllen.');
   });

    $('.location_freetext_link').click(function(){
        // hide all autocomplete and show freetext
        $('.id_location').val(null);
        $('.city_block').show();
        $('.location').val(null).removeClass('__AC_validated').addClass('__AC_invalidated').hide();
        $(this).parent('div').find('.__AC_icon').removeClass('__AC_validated').addClass('__AC_invalidated').hide();
        $('.location_freetext').show();
        $('.location_link').show();
        $(this).hide();
    });
    
    $('.location_link').click(function(){
        // hide freetext and show autocomplete
        $('.city_block').hide();
        $('.location').show();
        $(this).parent('div').find('.__AC_icon').show();
        $('.location_freetext').val(null).hide();
        $('.location_freetext_link').show();
        $(this).hide();
    });

    $('.institution_freetext_link').click(function(){
        // hide all autocomplete and show freetext
        $('.id_institution').val(null);
        $('.institution').val(null).removeClass('__AC_validated').addClass('__AC_invalidated').hide();
        $(this).parent('div').find('.__AC_icon').removeClass('__AC_validated').addClass('__AC_invalidated').hide();
        $('.institution_freetext').show();
        $('.institution_link').show();
        $(this).hide();
    });

    $('.institution_link').click(function(){
        // hide freetext and show autocomplete
        $('.institution').show();
        $(this).parent('div').find('.__AC_icon').show();
        $('.institution_freetext').val(null).hide();
        $('.institution_freetext_link').show();
        $(this).hide();
    });

    $('.contactperson_freetext_link').click(function(){
        // hide all autocomplete and show freetext
        $('.id_contactperson').val(null);
        $('.contactperson').val(null).removeClass('__AC_validated').addClass('__AC_invalidated').hide();
        $(this).parent('div').find('.__AC_icon').removeClass('__AC_validated').addClass('__AC_invalidated').hide();
        $('.contactperson_freetext').show();
        $('.contactperson_link').show();
        $(this).hide();
    });

    $('.contactperson_link').click(function(){
        // hide freetext and show autocomplete
        $('.contactperson').show();
        $(this).parent('div').find('.__AC_icon').show();
        $('.contactperson_freetext').val(null).hide();
        $('.contactperson_freetext_link').show();
        $(this).hide();
    });

    $('.artist_freetext_link').click(function(){
        // hide all autocomplete and show freetext
        $('.id_artist').val(null);
        $('.artist').val(null).removeClass('__AC_validated').addClass('__AC_invalidated').hide();
        $(this).parent('div').find('.__AC_icon').removeClass('__AC_validated').addClass('__AC_invalidated').hide();
        $('.artist_freetext').show();
        $('.artist_link').show();
        $(this).hide();
    });

    $('.artist_link').click(function(){
        // hide freetext and show autocomplete
        $('.artist').show();
        $(this).parent('div').find('.__AC_icon').show();
        $('.artist_freetext').val(null).hide();
        $('.artist_freetext_link').show();
        $(this).hide();
    });
    
   $('.cultureEdit .qmark').click(function() {openHelpWindow(jQuery(this).attr('alt'));});

   $('#culture-edit-form .location').autoComplete({
      'requestUrl': DR+'ajax.php?method=getLocation',
      'putIdInto': '.id_location',
      'closeWhenBlur' : false,
      'footer': '<div class="help" alt="offer_venue" onclick="openHelpWindow(\'offer_venue\')">'+ml('offer_location_no_matches')+'</div>'
   });

   $('#culture-edit-form .institution').autoComplete({
      'requestUrl': DR+'ajax.php?method=getLocation',
      'putIdInto': '.id_institution',
      'emptyFooter': '<div>'+ml('offer_institution_no_matches')+'</div>'
   });

   $('#culture-edit-form .contactperson').autoComplete({
      'requestUrl': DR+'ajax.php?method=getUser',
      'putIdInto': '.id_contactperson',
      'emptyFooter': '<div>'+ml('offer_contactperson_no_matches')+'</div>'
   });

   $('#culture-edit-form .artist').autoComplete({
      'requestUrl': DR+'ajax.php?method=getUser',
      'putIdInto': '.id_artist',
      'emptyFooter': '<div>'+ml('offer_artist_no_matches')+'</div>'
   });

   bindCalendar();
});

//Search
var doSiteSearch = function() {
  document.location.href = DR+CUR_LANG+'/search/'+
     $('.search input').val();
};

// event calendar
var buildCalendar = function(month, year) {
   $('.calendar').load(DR+'ajax.php?method=createCalendar'+
      '&month='+month+'&year='+year, function() {
         bindCalendar();
   });
};

var bindCalendar = function() {
   $('.calendar .left, .calendar .right').click(function() {
      var date = $(this).attr('id').split(/_/);
      buildCalendar(date[1], date[2]);
   });
   $('.calendar td').click(function() {
      $('#dateFrom, #dateTo').val($(this).attr('id').split(/_/)[1]);
      submitEventSearchForm();
   });
   $('.calendar .month').click(function() {
      $('#dateFrom').val($(this).attr('id').split(/_/)[1]);
      $('#dateTo').val($(this).attr('id').split(/_/)[2]);
      submitEventSearchForm();
   });
};

var submitEventSearchForm = function() {
   var fieldsArray = [];
   jQuery.each($('#event-search-form').serializeArray(), function(i, field){
      fieldsArray[field.name] = field.value;
   });
   var url = DR+CUR_LANG+'/events/'+fieldsArray['event_region']+'/'+
             fieldsArray['city']+'/'+fieldsArray['event_city_range']+'/'+
             fieldsArray['event_category']+'/'+fieldsArray['startDate']+'/'+
             fieldsArray['endDate'];
   document.location.href = url;
};

// associations
var submitAssociationsSearchForm = function() {
   var fieldsArray = [];
   jQuery.each($('#associations-search-form').serializeArray(), function(i, field){
      fieldsArray[field.name] = field.value;
   });
   var url = DR+CUR_LANG+'/associations/'+fieldsArray['region']+'/'+
             fieldsArray['city']+'/'+fieldsArray['city_range']+'/'+
             fieldsArray['category']+'?submit=suche';
   document.location.href = url;
};

// persons
var submitPersonsSearchForm = function() {
   var fieldsArray = [];
   jQuery.each($('#persons-search-form').serializeArray(), function(i, field){
      fieldsArray[field.name] = field.value;
   });
   var url = DR+CUR_LANG+'/persons/'+fieldsArray['region']+'/'+
             fieldsArray['city']+'/'+fieldsArray['city_range']+'/'+
             fieldsArray['category']+'?submit=suche';
   document.location.href = url;
};

// culture
var submitCultureSearchForm = function() {
   var fieldsArray = [];
   jQuery.each($('#culture-search-form').serializeArray(), function(i, field){
      fieldsArray[field.name] = field.value;
   });
   var url = DR+CUR_LANG+'/culture/'+fieldsArray['region']+'/'+
             fieldsArray['city']+'/'+fieldsArray['city_range']+'/'+
             fieldsArray['project_category']+'/'+fieldsArray['project_targetgroup']+'/'+
             fieldsArray['project_format']+'/';
   document.location.href = url;
};

// google maps helper (parent.document.location.href is read-only for iframe contained scripts)
function loadUrl(url) {
   document.location.href = url;
}

// framebreaker
if(top.location != location)
      top.location.href = document.location.href;


