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

$(document).ready(function() {
    // 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('');
      }
   });

   // events   
   $('#event-search-form').eventCalendar();

   //projects
   $('#projects-form-submit').click(function() {
      submitProjectsSearchForm();
   });

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

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

   // education projects form
   $('#education-projects-search-form').educationProjectsCalendar();

   // education form
   $('#education-search-form').educationCalendar();

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

   // culture edit form
   $.cbNotebook();
   $.cbCheckbox();

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

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

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

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

   $('#education-register-form .artist').autoComplete({
      'requestUrl': DR+'ajax.php?method=getUser',
      'putIdInto': '.id_artist',
      'emptyFooter': '<div>'+ml('offer_artist_no_matches')+'</div>'
   });
 
   //$('#education-register-form input[name="event_freetext"]').datepick({ constrainInput: false });

   // help
   $('.helplink').click(function(){
    window.open(DR+'de_DE/help/'+$(this).attr('alt'), 'help', 'width=300,height=350,scrollbars=1,resizable=1');
    return false;
   });
});

var ie6 = false;
if(jQuery.browser.msie && jQuery.browser.version.substr(0,3)<="6.0") {
  ie6 = true;
}

// Map
var openGoogleMaps = function(){
   var MAP_DR = 'http://web2.heimat.de/leipzigerraum';

   if (ie6 == false) {
      Shadowbox.open({
         player:     'iframe',
         content:    MAP_DR+'/map.php',
         height:     460,
         width:      600,
         options:    {
            onOpen: function(item){
               $('#sb-body').css({
                 border: '6px solid #ffffff',
                 'border-radius': '5px',
                 '-moz-border-radius': '5px',
                 '-webkit-border-radius': '5px'
               });
            }
         }
      });
   }else{
      Map = window.open(MAP_DR+"/map.php", "Zweitfenster", "width=600,height=460,scrollbars=no");
      Map.focus();
   }
}

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

// projects
var submitProjectsSearchForm = function() {
   var fieldsArray = [];
   jQuery.each($('#projects-search-form').serializeArray(), function(i, field){
      fieldsArray[field.name] = field.value;
   });
   var url = DR+CUR_LANG+'/projects/'+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;
};

// 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;

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

// bind calendar
var bindCalendar = function(t) {
    t.find('.calendar .left, .calendar .right').live('click', function() {
      var date = $(this).attr('id').split(/_/);
      buildCalendar(date[1], date[2]);
    });
    t.find('.calendar td').live('click', function() {
      $('#dateFrom, #dateTo').val($(this).attr('id').split(/_/)[1]);
      t.submit();
    });
    t.find('.calendar .month').live('click', function() {
      $('#dateFrom').val($(this).attr('id').split(/_/)[1]);
      $('#dateTo').val($(this).attr('id').split(/_/)[2]);
      t.submit();
    });
    t.find('.cal_area').live('change', function() {
      t.find('.cal_city').load(DR+'ajax.php?method=createCities&region='+$(this).val());
   });
}

 // jquery plugins

 jQuery.fn.eventCalendar = function() {
   var t = jQuery(this);
   t.each(function() {
        t.submit(function() {
           var fieldsArray = [];
           jQuery.each(t.serializeArray(), function(i, field){
              fieldsArray[field.name] = field.value;
           });
           var url = DR+CUR_LANG+'/'+fieldsArray['page']+'/'+fieldsArray['event_region']+'/'+
                     fieldsArray['city']+'/'+fieldsArray['event_city_range']+'/'+
                     fieldsArray['event_category']+'/'+fieldsArray['startDate']+'/'+
                     fieldsArray['endDate'];
           document.location.href = url;
           return false;
        });
        bindCalendar(t);
   });
 };

 jQuery.fn.educationProjectsCalendar = function() {
   var t = jQuery(this);
   t.each(function() {
        t.submit(function() {
           var fieldsArray = [];
           jQuery.each(t.serializeArray(), function(i, field){
              fieldsArray[field.name] = field.value;
           });
           var url = DR+CUR_LANG+'/'+fieldsArray['page']+'/'+fieldsArray['content']+'/'+
                     fieldsArray['city']+'/'+fieldsArray['city_range']+'/'+fieldsArray['project_category']+'/'+
                     fieldsArray['person_category']+'/'+fieldsArray['project_targetgroup']+'/'+
                     fieldsArray['project_format'];
           document.location.href = url;
           return false;
        });
        bindCalendar(t);
   });
 };

 jQuery.fn.educationCalendar = function() {
   var t = jQuery(this);
   t.each(function() {
        t.submit(function() {
           var fieldsArray = [];
           jQuery.each(t.serializeArray(), function(i, field){
              fieldsArray[field.name] = field.value;
           });
           var url = DR+CUR_LANG+'/'+fieldsArray['page']+'/'+fieldsArray['content']+'/'+fieldsArray['event_region']+'/'+
                     fieldsArray['city']+'/'+fieldsArray['event_city_range']+'/'+
                     fieldsArray['event_category']+'/'+fieldsArray['startDate']+'/'+
                     fieldsArray['endDate'];
           document.location.href = url;
           return false;
        });
        bindCalendar(t);
   });
 };


