// Brandboxfunktionen
var Brandbox = {

    /*
     * Brandbox.key
     */
    key: function(event) {

      if(!event)
        event = window.event;
    
      var keyCode =
        event.charCode ?
          event.charCode :
            event.keyCode;
      var key = String.fromCharCode(keyCode);
      var keySmall = key.toLowerCase();

      if($.browser.msie) {
        if(event.keyCode == 189)
          keySmall = "-";
      }

      return keySmall;

    },

    content: {
      /*
       * Brandbox.content.iFrameToText
       */
      iFrameToText: function() {
        var iframes = document.getElementsByTagName("iframe");
        for (var c = 0; c < iframes.length; c++) {
          if(iframes[c].name == "")
            continue;
          var f_name = iframes[c].name.replace(/iframe_/,"");
          var f_obj = document.getElementById(iframes[c].name).contentWindow;
          if($(f_name) && f_obj.document.body)
            $('#'+f_name).val(f_obj.document.body.innerHTML);
        }
      },
  
      /*
       * Brandbox.content.iFrameLoad
       */
      iFrameLoad: function(f_name, f_isword) {
  
        if($('#'+f_name).length == 0)
          return;
  
        var f_value = f_isword ? '' : $('#'+f_name).val();
        var f_name_iframe = f_isword ? 'WordPaste' : 'iframe_' + f_name;
        var f_obj = document.getElementById(f_name_iframe).contentWindow;
  
        f_value = f_value.replace(/&quot;/g, '"');
  
        var str =
          '<style type="text/css">p {margin:0 0 0 0;}</style>' +
          '<body style="margin:1px;font-family:Verdana,sans-serif;font-size:11px;">' +
            f_value +
          '</body>';
        
        if (f_value != "") {
          f_obj.document.open();
          f_obj.document.writeln(str);
          f_obj.document.close();
        } else {
          f_obj.document.write(str);
          f_obj.document.close();
        }
  
        f_obj.document.designMode = 'On';
  
        if(!f_isword) {
          var f_obj_observe =
            $.browser.msie ?
              document.getElementById(f_name_iframe).contentWindow.document :
                document.getElementById(f_name_iframe).contentDocument.defaultView;
          
          var listener = function() {Brandbox.content.iFrameonFocusIn(f_name_iframe)};
          
          if ($.browser.msie)
            f_obj_observe.attachEvent("onfocusin", listener);
          else
            f_obj_observe.addEventListener('focus', listener, false);
          
          if($.browser.msie) {
            $(f_obj_observe).keydown(function(event) {
              Brandbox.content.iFrameonKeyDown(event);
            });
          } else {
            $(f_obj_observe).keypress(function(event) {
              Brandbox.content.iFrameonKeyDown(event);
            });
          }
        }
      },
  
      /*
       * Brandbox.content.iFramesLoad
       */
      iFramesLoad: function(f_name) {
        var iFrames = document.getElementsByTagName('iframe');
        for(var c = 0; c < iFrames.length; c++)
          this.iFrameLoad(iFrames[c].name.substr(7));
      },
  
      /*
       * Brandbox.content.iFrameExec
       */
      iFrameExec: function(command) {
  
        if(Brandbox.content.iFrameFocused == "")
          return;

        if($.browser.safari != true) {
          switch(command) {
            case 'insertorderedlist':
            case 'insertunorderedlist':
              var Selection = Brandbox.content.iFrameGetSelection();
              var currentNode = Brandbox.content.iFrameGetNode();
              var insertP = true;
              while(currentNode.tagName.toUpperCase() != "HTML") {
                if (currentNode.tagName.toUpperCase() == "LI")
                  insertP = false;
                currentNode = currentNode.parentNode;
              }
              if(insertP == true)
                Brandbox.content.iFrameWindow().document.execCommand("InsertParagraph", true, '');
              break;
          }
        }
  
        Brandbox.content.iFrameWindow().document.execCommand(command, false, '');
  
      },
  
      /*
       * Brandbox.content.iFrameonFocusIn
       */
      iFrameFocused:'',
      iFrameonFocusIn: function(f_name) {
        Brandbox.content.iFrameFocused = f_name;
      },
  
      /*
       * Brandbox.content.iFrameonKeyDown
       */
      iFrameonKeyDown: function(event) {
        
        if(!event)
          event = window.event;
        
        var keySmall = Brandbox.key(event)
  
        // HTML entfernen
        if($.browser.msie && event.ctrlKey && keySmall == "v") {
          var Selection = Brandbox.content.iFrameGetSelection();
          var Html = window.clipboardData.getData('Text').stripTags();
          Selection.pasteHTML(Html);
          event.returnValue = false;
        }
  
        // Trennfuge einfügen
        if(event.altKey && keySmall == "-") {
          if($.browser.msie) {
            var Selection = Brandbox.content.iFrameGetSelection();
            Selection.pasteHTML('&shy;');
          } else {
            Brandbox.content.iFrameWindow().document.execCommand('insertHTML', false, '&shy;');
          }
        }
  
        // Einzug (rechts)
        if(event.altKey && event.keyCode == Event.KEY_RIGHT) {
          Brandbox.content.iFrameExec('indent', Brandbox.content.iFrameFocused.substr(7));
          event.returnValue = false;
        }
  
        // Einzug (links)
        if(event.altKey && event.keyCode == Event.KEY_LEFT) {
          Brandbox.content.iFrameExec('outdent', Brandbox.content.iFrameFocused.substr(7));
          event.returnValue = false;
        }
  
        // BR statt P
        if(event.keyCode == Event.KEY_RETURN) {
          var Selection = Brandbox.content.iFrameGetSelection();
          var currentNode = Brandbox.content.iFrameGetNode();
          while(currentNode.tagName.toUpperCase() != "HTML") {
            if (currentNode.tagName.toUpperCase() == "LI")
              return;
            currentNode = currentNode.parentNode;
          }
  
          if($.browser.msie) {
            Selection.pasteHTML("<br>");
            Selection.select();
            Selection.collapse(false);
          } else {
            Brandbox.content.iFrameWindow().document.execCommand('insertHTML', false, "<br>");
          }
          event.returnValue = false;
        }
      },
  
      /*
       * Brandbox.content.iFrameLinkPanel
       */
      iFrameLinkPanel: function(f_name) {
  
        if($('#linkpanel_' + f_name).length != 0) {
          $('#linkpanel_' + f_name).css('display', 'inline');
          return;
        }
        
        $.get(
          'admin.php' +
          '?PHPSESSID=' + Brandbox.session_id +
          '&mod=content' +
          '&tpl=content_edit' +
          '&action=link_panel' +
          '&f_name=' + f_name +
          '&ajax',
          function(data) {
            $('#panel_' + f_name).get(0).innerHTML += data;
          }
        );
      },
  
      /*
       * Brandbox.content.iFrameLinkPanelHide
       */
      iFrameLinkPanelHide: function(f_name) {
        $('#linkpanel_' + f_name).css('display', 'none');
      },
  
      /*
       * Brandbox.content.iFrameLinkText
       */
      iFrameLinkText: function(f_id, LinkType) {
  
        if(!Brandbox.content.iFrameFocused)
          return;
  
        if(LinkType == -1)
          var url = $('#external_' + f_id).val();
        else if(LinkType == -2)
          var url = 'mailto:' + $('#email_' + f_id).val();
        else if(LinkType == -3)
          var url = '{page_link[{page_id}]}{download}' + $('#download_' + f_id).val();
        else
          var url = '{page_link[' + $('#internal_' + f_id).val() + ']}';
  
        if(url == "" || url == "{page_link[]}" || url == "http://")
          return;
  
        var Selection = this.iFrameGetSelection();
        var target = $('#target_' + f_id).val();
        
        if($.browser.msie) {
          var link = '<a href="' + url + '" target="' + target + '">' + Selection.text + '</a>';
          Selection.pasteHTML(link);
        } else {
          var link = '<a href="' + url + '" target="' + target + '">' + Selection + '</a>';
          this.iFrameWindow().document.execCommand('insertHTML', false, link);
        }
        
        this.iFrameLinkPanelHide(f_id);
  
      },
  
      /*
       * Brandbox.content.iFrameWrapTag
       */
      iFrameWrapTag: function(Tag) {
  
        if(!Brandbox.content.iFrameFocused)
          return;
  
        var Selection = this.iFrameGetSelection();
        var node = this.iFrameGetNode();
  
        var previous_tagName = node.tagName;
        var unset = false;
  
        if($.browser.msie && node.tagName.toLowerCase() == Tag) {
          node.outerHTML = node.innerHTML;
          unset = true;
        }
  
        if($.browser.msie) {
          if(unset == false && Selection.text.length > 0)
            Selection.pasteHTML('<' + Tag + '>' + Selection.text + '</' + Tag + '>');
        } else if(Selection.toString().length > 0) {
            this.iFrameWindow().document.execCommand('insertHTML', false, '<' + Tag + '>' + Selection.toString() + '</' + Tag + '>');
        }
      },
  
      /*
       * Brandbox.content.iFrameWindow
       */
      iFrameWindow: function() {
        return document.getElementById(Brandbox.content.iFrameFocused).contentWindow;
      },
  
      /*
       * Brandbox.content.iFrameGetNode
       */
      iFrameGetNode: function() {
        var f_obj = this.iFrameWindow();

        if(window.getSelection)
          return f_obj.window.getSelection().focusNode;
        else if(document.getSelection)
          return f_obj.document.getSelection().focusNode;
        else if(document.selection)
          return f_obj.document.selection.createRange().parentElement();
      },
  
      /*
       * Brandbox.content.iFrameGetSelection
       */
      iFrameGetSelection: function() {
  
        var f_obj = this.iFrameWindow();
        f_obj.focus();
  
        if(window.getSelection)
          return f_obj.window.getSelection();
        else if(document.getSelection)
          return f_obj.document.getSelection();
        else if(document.selection)
          return f_obj.document.selection.createRange();
  
      },
  
      /*
       * Brandbox.content.pasteWord
       */
      pasteWord_Node:null,
      pasteWord: function(pasteText, closeText, paste) {
        if(!Brandbox.content.iFrameFocused)
          return;
  
        // Wert einfügen
        if(
          $('#WordPastePanel').length != 0 && 
          $('#WordPastePanel').css('display') == 'block'
        ) {
  
          var f_obj = document.getElementById('WordPaste').contentWindow.document.body;
          var f_value = f_obj.innerHTML;
  
          // Feld leeren
          f_obj.innerHTML = '';
  
          if(paste) {
            
            $.post(
              "admin.php", 
              '?PHPSESSID=' + Brandbox.session_id +
              '&mod=content' +
              '&tpl=content_edit' +
              '&action=paste_word' +
              '&f_value=' + escape(encodeURI(f_value)) +
              '&ajax',
              function(data) {
                // Text einfügen
                if($.browser.msie) {
                  Brandbox.content.pasteWord_Node.pasteHTML(data);
                } else {
                  Brandbox.content.iFrameWindow().document.execCommand('insertHTML', false, data);
                }
              }
            );
          }
  
          $('#WordPastePanel').css('display', 'none');
  
          this.pasteWord_Selection = null;
  
        } else {
  
          // Selection speichern
          this.pasteWord_Node = this.iFrameGetSelection();
  
          // Feld einblenden
          if($('#WordPastePanel').length == 0) {
  
            var WordPastePanel = document.createElement('div');
            WordPastePanel.setAttribute('id', 'WordPastePanel');
  
            var WordPaste = document.createElement('iframe');
            WordPaste.setAttribute('name', 'WordPaste');
            WordPaste.setAttribute('id', 'WordPaste');
            WordPaste.setAttribute('scrolling', 'auto');
            WordPaste.setAttribute('frameBorder', '0');
            WordPaste.setAttribute('width', '100%');
            WordPaste.setAttribute('height', '100%');
            
            var WordPasteDiv = document.createElement('div');
            WordPasteDiv.appendChild(WordPaste);
            WordPastePanel.appendChild(WordPasteDiv);
  
            var Break = document.createElement('br');
            WordPastePanel.appendChild(Break);
  
            var Paste = document.createElement('input');
            Paste.setAttribute('type', 'button');
            Paste.setAttribute('value', pasteText);
            Paste.onclick = function() {Brandbox.content.pasteWord(pasteText, closeText, true); };
            WordPastePanel.appendChild(Paste);
  
            var Close = document.createElement('input');
            Close.setAttribute('type', 'button');
            Close.setAttribute('value', closeText);
            Close.onclick = function() {Brandbox.content.pasteWord(pasteText, closeText, false); };
            Close.className = 'Close';
            WordPastePanel.appendChild(Close);
  
            document.body.appendChild(WordPastePanel);
            
            WordPaste.style.height = (WordPastePanel.offsetHeight-85) + 'px';
            
            document.body.focus();
  
          }
  
          $('#WordPastePanel').css('display', 'block');
  
          // iFrame laden
          this.iFrameLoad('WordPaste', true);
  
        }
      },
  
      /*
       * Brandbox.content.browse
       */
      browse: function(Start, Count, Max, Fnc) {
  
        var app_path_http = $('#app_path_http').val();
        var app_path_icons = $('#app_path_icons').val();
      
        var Count = parseInt(Count);
        var Max = parseInt(Max);
        var Start = parseInt(Start);
  
        var _Start = (Max == 0 ? 0 : Start+1);
        var _End = Math.min(Start + Count, Max);
        var _Max = Max;
  
        var _Backward = (
          Start != 0 ?
            '<a href="javascript:' + Fnc + '(' + (Start - Count) + ');">' +
              '<img src="' + app_path_icons + '16x16/arrow_up_green.png" align="absmiddle">' +
            '</a>' :
            '<img src="' + app_path_icons + 'others/spacer.gif" align="absmiddle">'
        );
  
        var PathIcons = app_path_http + app_path_icons + 'others/';
  
        var _Position =
          '&nbsp;<span class="Position">&nbsp;' +
            _Start + '&nbsp;' +
            '-&nbsp;' +
            _End + '&nbsp;' +
            Texts.__169 + ' ' +
            _Max + '</a></span>&nbsp;'
  
        var _Forward = (
          Start + Count < Max ?
            '<a href="javascript:' + Fnc + '(' + (Start + Count) + ');">'+
              '<img src="' + app_path_icons + '16x16/arrow_down_green.png" align="absmiddle">' +
            '</a>' :
            '<img src="' + app_path_icons + 'others/spacer.gif" align="absmiddle">'
        );
  
        var Page =
        '<span class="Page">' + (
            Start != 0 ?
            '<a href="javascript:' + Fnc + '(0);">' +
              '<img src="' + app_path_icons + '16x16/nav_up_green.png" align="absmiddle">' +
            '</a>' :
            '<img src="' + app_path_icons + 'others/spacer.gif" align="absmiddle">'
          ) +
          '&nbsp;<span class="Position">' +
            Texts.__350 + ': <input type="text" value="' + Math.ceil(_Start/Count) + '" onkeydown="return Brandbox.content.browsePage(window.event||event, ' + Fnc + ', (parseInt(this.value)-1) * ' + Count + ', ' + Max + ');"> / '+ Math.ceil(Max/Count) +
          '</span>&nbsp;' + (
          Math.ceil(_Start/Count) < Math.ceil(Max/Count) ?
            '<a href="javascript:' + Fnc + '(' + (Math.floor(Max/Count-((Max/Count) % 1 == 0 ? 1 : 0)) * Count) + ');">' +
              '<img src="' + app_path_icons + '16x16/nav_down_green.png" align="absmiddle">' +
            '</a>' :
            '<img src="' + app_path_icons + 'others/spacer.gif" align="absmiddle">'
          ) +
        '</span>';
  
  
        $('#Navigate').html(Page + _Backward + _Position + _Forward);
  
      },
  
      /*
       * Brandbox.content.browsePage
       */
      browsePage: function(event, Fnc, Start, Max) {
        if(event.keyCode != 13)
          return true;
        if(parseInt(Start) >= parseInt(Max) || parseInt(Start) < 0 || isNaN(parseInt(Start))) {
          alert("Bitte geben Sie eine gültige Seite ein.");
          return true;
        }
        Fnc(Start);
        return false;
      },
  
      /*
       * Brandbox.content.Cumulus
       */
      Cumulus: function(f_name) {
  
        window.open(
          'admin.php' +
          '?PHPSESSID=' + Brandbox.session_id +
          '&mod=fields' +
          '&tpl=cumulus_fileadd' +
          '&f_name=' + f_name +
          '&noform' +
          '&ajax', '',
          'width=520, height=200'
        );
      }
  }
} // End Brandbox.

function detailStellenangebote(stellenangebote_Id) {
  document.getElementById('action').value = 'detailStellenangebote';  
  document.getElementById('stellenangeboteId').value = stellenangebote_Id;
  document.getElementById('Stellenangebote').submit();  
}
function detailStellenangebotefromTeaser(stellenangebote_Id) {
  document.getElementById('StellenangeboteTeaseraction').value = 'detailStellenangebote'; 
  document.getElementById('StellenangeboteTeaserstellenangeboteId').value = stellenangebote_Id;
  document.getElementById('StellenangeboteTeaser').submit();  
}
function listStellenangebote(page_Id)
{
  document.getElementById('action').value='listStellenangebote';
  document.getElementById('pageId').value = page_Id;
  document.getElementById('Stellenangebote').submit();
}
function detailNiederlassungfromTeaser(Niederlassung_id) {
  document.getElementById('NiederlassungTeaseraction').value = 'detailNiederlassung'; 
  document.getElementById('NiederlassungTeaserNiederlassungId').value = Niederlassung_id;
  document.getElementById('NiederlassungTeaser').submit();  
}

function showAjaxLoader(which) {
  switch(which) {
    case 'sonstiges':
      $('div#sonstiges div.ajaxLoader').show();
      break;

    case 'saveButton':
      $('div.saveButton div.ajaxLoader').show();
      break;
    
    case 'weitereKunden':
      $('span.weitereKunden').show();
      break;
  }
}

function hideAjaxLoader() {
  $('.ajaxLoader').hide();
}


function saveForm(loca) {
  showAjaxLoader('saveButton');
  Brandbox.content.iFrameToText();
  $.post('../stage_content_ajax.php?lan_id='+lan_id+'&page_id='+page_id+'&cn_id='+cn_id+'&ajax=1&action=save&id='+id, $('form#Stellenangebote').serialize(), function(d) {
    if(d) {
      window.location = loca;
      /*
      hideAjaxLoader();
      $(d).insertAfter('div.saveButton div.ajaxLoader').hide().fadeIn().delay(3000).fadeOut();
      */
    }
  });
}

function deleteStelle(get_id) {
  if(typeof(id) == 'undefined' || typeof(get_id) != 'undefined') {
    id = get_id;
    
    $('div#abfrage_id_'+id).dialog({
      resizable: false,
      height:150,
      modal: true,
      show: 'fade',
      buttons: {
        'Löschen': function() {
          $(this).dialog('close');
          showAjaxLoader('sonstiges');
          
          $.post('../stage_content_ajax.php?lan_id='+lan_id+'&page_id='+page_id+'&cn_id='+cn_id+'&ajax=1&action=delete&id='+id, function(d) {
              $('tr#row_'+id).fadeOut(function() {
                hideAjaxLoader();
                $(this).remove();
              });
            });
          
        },
        'Abbrechen': function() {
          $(this).dialog('close');
        }
      }
    });

  } else {
    $('div#abfrage').dialog({
      resizable: false,
      height: 150,
      modal: true,
      show: 'fade',
      buttons: {
        'Löschen': function() {     
          $.post('../stage_content_ajax.php?lan_id='+lan_id+'&page_id='+page_id+'&cn_id='+cn_id+'&ajax=1&action=delete&id='+id, function(d) {
            window.location.href = returnLink;  
          });
        },
        'Abbrechen': function() {
          $(this).dialog('close');
        }
      }
    });
  }
}

imagebilder = {
  __init: function() {
    $('a.fancybox').fancybox();
  },
  
  setActiveImage: function(image_id, id) {
    $('div#imagePictures ul li').removeClass('active');
    $('li#image_'+image_id).addClass('active'); 
    if(id != 0) {  
      $.get('../stage_content_ajax.php', {
        lan_id: lan_id,
        page_id: page_id,
        cn_id: cn_id,
        action: 'setActiveImage',
        image_id: image_id,
        id: id
      });
    } else {
      imagebilder.setValueOfInput(image_id);
    }
  },
  
  clearActiveImage: function(id) {  
    $('div#imagePictures ul li').removeClass('active');
    if(id != 0) { 
      $.get('../stage_content_ajax.php', {
        lan_id: lan_id,
        page_id: page_id,
        cn_id: cn_id,
        action: 'clearActiveImage',
        id: id
      });
    } else {
      imagebilder.setValueOfInput('');
    }
  },
  
  setValueOfInput: function(value) {
    $('input#Bild').val(value);
  }
}

var helper = {
  deleteItem: function(obj) {
    $(obj).parent().fadeOut(function() {
      $(this).remove();
    });
  }
}

var stellenangebote = {
  
  __init: function() {
    hideAjaxLoader();
  },
  
  kundenDropdown: function(consumerId) {
    
    // Dialog einrichten wenn die Inputs schon gefüllt sind
    if(typeof(consumerId) != 'undefined') {
      stellenangebote.kundenDropdownDialogCreate(consumerId);
      $('div.consumer_'+consumerId+' span.infobutton').show();
    }
    
    // Autocomplete
    $('.kundeAutocomplete').autocomplete({
      source: '../stage_content_ajax.php?lan_id='+lan_id+'&page_id='+page_id+'&cn_id='+cn_id+'&ajax=true&action=kundeAutocomplete',
      select: function(event, ui) {
        var $inputAutoComplete = $(this);
        $inputAutoComplete.next().val(ui.item.id);

        stellenangebote.kundenDropdownDialogCreate(ui.item.id);
        
        var $container = $inputAutoComplete.parent();
        $('span.infobutton', $container).show();

      },
      change: function(event, ui) {
        if(ui.item == null) {
          var $inputAutoComplete = $(this);
          var $container = $inputAutoComplete.parent();
          $('span.infobutton', $container).hide();
        }
      }
    });
    
  },
  
  kundenDropdownDialogCreate: function(consumerId) {
    $.get(
      '../stage_content_ajax.php', {
        lan_id: lan_id,
        page_id: page_id,
        cn_id: cn_id,
        ajax: true,
        action: 'createConsumerDetails',
        consumerId: consumerId
      }, function(data) {
        var dialog = $('<div style="display:hidden" class="consumerDetails"></div>').appendTo('body');
        dialog.html(data);
        dialogs[consumerId] = dialog.dialog({
          autoOpen: false,
          title: 'Kundeninformationen',
          minWidth: 350
        });
      });
  },
  
  kundenDropdownDialog: function($this) {

    var consumerId = $('input.kundenId', $this.parent()).val();
    dialogs[consumerId].dialog('open');
  },
  
  getTemplate: function() {
    
    showAjaxLoader('weitereKunden');
    
    $.get('../stage_content_ajax.php', {
      lan_id: lan_id,
      page_id: page_id,
      cn_id: cn_id,
      ajax: 'true',
      action: 'getTemplate',
      template: 'kundenDropdown'
    }, function(data) {
      var $html = $(data);
      hideAjaxLoader();
      $html.appendTo($('div.kundenDropdown div#marginLeft'));
      $('div.kundenDropdown div.dropdown:last').hide().delay(100).slideDown();
    });
  },
  
  recommendation: function() {
    $.post('../stage_content_ajax.php?lan_id='+lan_id+'&page_id='+page_id+'&cn_id='+cn_id+'&ajax=true&action=checkRecommendation', 
      $('form').serialize(), 
      function(objData) {
        if(objData.type == 'error') {
          $('div#recommendation div.message').html(objData.html).hide().slideDown().delay(3000).slideUp();
        }
        else {
          $('div#recommendation input#Abschicken').attr('disabled', 'disabled').addClass('disabled');
          $('div#recommendation div.message').html(objData.html).hide().slideDown().delay(3000).slideUp(function() {
            $('div#recommendation input#Abschicken').removeAttr('disabled').removeClass('disabled');
            $('div#recommendation div.message').hide();
            $.fancybox.close();
          });
        }
    }, 'json');
  },
  
  openRecommendation: function() {
    var htmlToLoad = $('div#recommendation').clone();
    $.fancybox({
      content: htmlToLoad,
      autoScale: false,
      autoDimensions: false,
      width: 470,
      height: 370
    });
  },
  
  openAdConfigurator: function() {
  
    $('div#adConfigurator').dialog({
      title: 'Stellenangebot ausdrucken',
      resizable: false,
      modal: true,
      show: 'fade',
      width: 320,
      buttons: {
        'Generieren': function() {
          $('.ui-dialog-buttonpane button:contains("Generieren")').button('disable');
          $.post('../stage_content_ajax.php?lan_id='+lan_id+'&page_id='+page_id+'&cn_id='+cn_id+'&sId='+id+'&ajax=true&action=generateAd', $('div#adConfigurator form').serialize(), function(url) {
            $('.ui-dialog-buttonpane button:contains("Generieren")').button('enable');
            
            // Ausnahme für Webkit - sorry
            if($.browser.safari == true) {
              document.location = url;
            } else {
              openPopupWindow(url, 'PDF-Download', 600, 800);
            }
            
          });
        },
        'Abbrechen': function() {
          $(this).dialog('close');
        }
      }
    });
  },
  
  facebookRecommendation: function() {

    $('div#facebookRecommendation').dialog({
      title: 'Stellenangebot für Facebook empfehlen',
      resizable: false,
      modal: true,
      show: 'fade',
      width: 320,
      buttons: {
        'Empfehlen': function() {
          $.post('../stage_content_ajax.php?lan_id='+lan_id+'&page_id='+page_id+'&cn_id='+cn_id+'&sId='+id+'&ajax=true&action=facebookRecommendation', function(data) {
            $('div#facebookRecommendation').dialog('close');
            $('a.facebookLink').replaceWith(data);
          });
        },
        'Abbrechen': function() {
          $(this).dialog('close');
        }
      }
    });
  },
  
  initAdConfigurator: function() {
    $('div#type, div#colorOptions, div#formatOptions').buttonset();
    
    $('div#type input').change(function() {
      if($(this).val() == '38') {
        $('div.option').stop(true, true).slideDown();
      } else {
        $('div.option').stop(true, true).slideUp();
      }
    });
  }
  
}


var myPopupWindow = '';
function openPopupWindow(url, name, width, height)
{
    //Remove special characters from name
    name = name.replace(/\/|\-|\./gi, "");

    //Remove whitespaces from name
    var whitespace = new RegExp("\\s","g");
    name = name.replace(whitespace,"");

    //If it is already open
    if (!myPopupWindow.closed && myPopupWindow.location)
    {
        myPopupWindow.location.href = encodeUrl(url);
    }
    else
    {
        myPopupWindow = window.open(encodeUrl(url),name, "location=no, scrollbars=yes, resizable=yes, toolbar=no, menubar=no, width=" + width + ", height=" + height );
        if (typeof(myPopupWindow.opener) == 'undefined') myPopupWindow.opener = self;
    }

     //If my main window has focus - set it to the popup
    if (window.focus) {myPopupWindow.focus()}
}

function encodeUrl(url)
{
 	if (url.indexOf("?")>0)
 	{
		encodedParams = "?";
 		parts = url.split("?");
 		params = parts[1].split("&");
 		for(i = 0; i < params.length; i++)
 		{
			if (i > 0)
	 		{
				encodedParams += "&";
			}
			if (params[i].indexOf("=")>0) //Avoid null values
			{
				p = params[i].split("=");
				encodedParams += (p[0] + "=" + escape(encodeURI(p[1])));
			}
			else
			{
				encodedParams += params[i];
			}
		}
		url = parts[0] + encodedParams;
	}
	return url;
}

$(document).ready(function() {
  dialogs = {};
  stellenangebote.initAdConfigurator();
  
  if(typeof(WebSite) == 'undefined') { Brandbox.content.iFramesLoad(); }
  $('input.datepicker').datepicker();
  
  $('input.datepickerAufsteigend').datepicker({
    yearRange: '-0:+3',
    changeMonth: true,
    changeYear: true
  });
});
