/*
 * Author: Andre Mohren
 * Modifications: Johannes Wüller
 * Last Modified: 14.10.2009
 */

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

if (typeof(Shadowbox) != "undefined") { // only init if defined
   Shadowbox.loadSkin('classic', DR+'media/shadowbox/skin');
   Shadowbox.loadLanguage('en', DR+'media/shadowbox/lang');
   Shadowbox.loadPlayer(['img'], DR+'media/shadowbox/player');
   window.onload = function(){
      Shadowbox.init();
   };
}

$(document).ready(function() {
   var doSiteSearch = function() {
      document.location.href = DR+$('#ml2_locale').val()+'/search/'+
         $('.search input').val()
   };
   $('.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('');
      }
   });
   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]);
         submitCalendar();
      });
      $('.calendar .month').click(function() {
         $('#dateFrom').val($(this).attr('id').split(/_/)[1]);
         $('#dateTo').val($(this).attr('id').split(/_/)[2]);
         submitCalendar();
      });
   };
   $('.ranges a').click(function() {
      $('#dateFrom').val($(this).attr('id').split(/_/)[1]);
      $('#dateTo').val($(this).attr('id').split(/_/)[2]);
      submitCalendar();
      return false;
   });
   var submitCalendar = function() {
      var params = {
         'event_region':     $('.cal_area').val(),
         'event_city':       $('.cal_city').val(),
         'event_city_range': $('.cal_range').val(),
         'event_category':   $('.cal_cat').val(),
         'startDate':        $('#dateFrom').val(),
         'endDate':          $('#dateTo').val(),
         'language':         $('#ml2_locale').val()
      };
      //TODO schöner machen
      if ($('.cal_area').val() == '')
         $('.cal_area').val(0);
      if ($('.cal_city').val() == '')
         $('.cal_city').val(0);
      if ($('.cal_range').val() == '')
         $('.cal_range').val(0);
      if ($('.cal_cat').val() == '')
         $('.cal_cat').val(0);
      if ($('#dateFrom').val() == '')
         $('#dateFrom').val(0);
      if ($('#dateTo').val() == '')
         $('#dateTo').val(0);
      document.location.href = DR+$('#ml2_locale').val()+'/events/'+
         'region/'+$('.cal_area').val()+'/city/'+$('.cal_city').val()+
         '/range/'+$('.cal_range').val()+'/category/'+$('.cal_cat').val()+
         '/from/'+$('#dateFrom').val()+'/to/'+$('#dateTo').val()+'/page/1';
   };
   $('.cal_area').change(function() {
      $('.cal_area_target').load(DR+'ajax.php?method=createCities&region='+
         $(this).val()
      );
   });
   $('.header select').change(function() {
      var val = $(this).val();
      var href = document.location.href;
      if (!href.match(/\/[a-z]{2}_[A-Z]{2}\//)) {
         document.location.href = href+val+'/home';
      } else {
         href = href.replace(/\/[a-z]{2}_[A-Z]{2}\//, '/'+val+'/');
         document.location.href = href;
      }
   });
   $('.daterange a').click(function() {
      submitCalendar();
      return false;
   });
   $('#letterBox input').keypress(function(e){
      if (e.which == 13) {
         $('#letterBox .lb_search img').click();
      }
   }).focus(function(){
      $(this).val('');
   });
   $('#letterBox .lb_search img').click(function() {
      window.document.location.href = window.document.location.href.replace(
         /\/presale.*$/, '/presale/'+$('#letterBox input').val());
   });
   bindCalendar();
});

function showTicketBox(url, w, h, title, close, header) {
   /*
    * workaround for flash colliding with layers
    */
   jQuery('.tx-rlmpflashdetection-pi1').hide();
   
   var overlay = jQuery('<div class="__layer"></div>').appendTo('body').css({
      'opacity': '0.3',
      'background-color': 'white',
      'top': '0px',
      'left': '0px',
      'position': 'fixed',
      'z-index': '1337',
      'height': '100%',
      'width': '100%'
   });
   if ($.browser.msie) {
      overlay.css({
         'position': 'absolute',
         left: document.body.scrollLeft + 'px',
         top:  document.body.scrollTop + 'px'
      });
   }
   var layer = jQuery('<div class="__layer"></div>').appendTo('body').css({
      'border': '2px solid #887d64',
      'border-radius': '26px',
      '-moz-border-radius': '26px',
      '-webkit-border-radius': '26px',
      'background-color': 'white',
      'top': '50%',
      'position': 'fixed',
      'left': '50%',
      'margin-left': '-'+w/2+'px',
      'margin-top': '-'+h/2+'px',
      'z-index': '1338',
      'height': h+'px',
      'width': w+'px'
   });
   if ($.browser.msie) {
      layer.css({
         'position': 'absolute',
         'margin': '0px',
         left: ((document.body.clientWidth/2)-w/2)+document.body.scrollLeft + 'px',
         top:  ((document.body.clientHeight/2)-h/2)+document.body.scrollTop + 'px'
      });
      var putWindowRight = function() {
         layer.css({
            left: ((document.body.clientWidth/2)-w/2)+document.body.scrollLeft + 'px',
            top:  ((document.body.clientHeight/2)-h/2)+document.body.scrollTop + 'px'
         });
         overlay.css({
            left: document.body.scrollLeft + 'px',
            top:  document.body.scrollTop + 'px'
         });
      };
      window.onresize = function() {
         putWindowRight();
      };
      window.onscroll = function() {
         putWindowRight();
      };
   }
   var header = jQuery('<div class="__layer_header"></div>').css({
      'position': 'absolute',
      'bottom': (h+2)+'px',
      'width': w+'px'
   }).append(header).appendTo(layer);
   var top = jQuery('<div class="ticketwindow_top">'+title+'</div>').appendTo(layer).css({
      'height': '32px',
      'line-height': '32px',
      'color': '#af2a26',
      'font-weight': 'bold',
      'padding-left': '27px',
      'position': 'relative',
      'z-index': '10',
      'border-radius-topleft': '26px',
      'border-radius-topright': '26px',
      '-moz-border-radius-topleft': '26px',
      '-moz-border-radius-topright': '26px',
      '-webkit-border-top-left-radius': '26px',
      '-webkit-border-top-right-radius': '26px'
   });
   jQuery('<iframe frameborder="0"></iframe>').appendTo(layer).css({
      'border': '0px',
      'display': 'block',
      'height': (h-62)+'px',
      'width': w+'px'
   }).attr('src', url);
   var bottom = jQuery('<div class="ticketwindow_bottom"></div>').appendTo(layer).css({
      'height': '30px',
      'line-height': '30px',
      'font-weight': 'bold',
      'vertical-align': 'middle',
      'line-height': '20px',
      'text-align': 'right',
      'padding-right': '40px',
      'position': 'relative',
      'z-index': '10',
      'border-radius-bottomleft': '26px',
      'border-radius-bottomright': '26px',
      '-moz-border-radius-bottomleft': '26px',
      '-moz-border-radius-bottomright': '26px',
      '-webkit-border-bottom-left-radius': '26px',
      '-webkit-border-bottom-right-radius': '26px'
   }).html('<a href="#" onclick="jQuery(\'.__layer\').remove();return false;" style="font-weight: bold;">'+close+'</a>');
   if (!$.browser.msie) {
      addShadow(layer);
   }
   if ($.browser.mozilla && $.browser.version.split('.')[2] == 0) {
      layer.css('-moz-border-radius','20px');
      top.css('-moz-border-radius-topright','17px');
      top.css('-moz-border-radius-topleft','17px');
      bottom.css('-moz-border-radius-bottomright','17px');
      bottom.css('-moz-border-radius-bottomleft','17px');
   }
   return false;
}
