$(document).ready(function(){
  $('.api iframe').css('overflow-x', 'hidden');
  
  menuNewWin();
});

// solve problem with IE8 (and lower) having no array.indexOf() function 
// http://stackoverflow.com/questions/3629183/why-doesnt-indexof-work-on-an-array-ie8
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length >>> 0;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

/**
 * !IMPORTANT! : This function must NOT be INSIDE in the jQuery.ready() function - problem/bug in loading object, etc.. undefined tinyMCE editor
 **/
var fileBrowserDir = '/pdw_file_browser/index.php';
var fileBrowserURL = fileBrowserDir+'';
function initializePdwFileBrowser(field_name, url, type, win)
{
  if (tinyMCE.activeEditor != null)
  {
    tinyMCE.activeEditor.windowManager.open({
        title: "PDW File Browser",
        url: fileBrowserURL,
        width: 950,
        height: 650,
        inline: 0,
        maximizable: 1,
        close_previous: 1
      },{
        window : win,
        input : field_name
      }
    );
  }
}

function removeItemFromArray(item, array)
{
  var idx = array.indexOf(item); // Find the index
  if(idx!=-1) array.splice(idx, 1); // Remove it if really found!

  return array;
}

function checkItemFromArray(item, array)
{
  return (array.indexOf(item) > -1);
}
  
(function($) {
  $.cjgFunction = {
    faqLogger: '/faq_log',
    init: function()
    {
    },
    setFileBrowserURLForSite: function(site)
    {
      fileBrowserURL = fileBrowserDir+'?sd='+site;
    }
	};
})(jQuery);


(function($) {
  $.cjgCustomForm = {
    formContainer: '#custom_form_container',
    formFieldAddingButton: '#custom_form_add_field',
    formFieldTypeList: '#custom_form_field_type_list',
    formFieldRemovalLink: '.custom_form_remove_field',
    formFieldAddOption: '.custom_field_add_option',
    index: 1,
    init: function(options)
    {
      this.formContainer  = typeof(options.formContainer) != 'undefined' ? options.formContainer : '#custom_form_container';
      this.formFieldAddingButton  = typeof(options.formFieldAddingButton) != 'undefined' ? options.formFieldAddingButton : '#custom_form_add_field';
      this.formFieldTypeList  = typeof(options.formFieldTypeList) != 'undefined' ? options.formFieldTypeList : '#custom_form_field_type_list';
      this.formFieldRemovalLink  = typeof(options.formFieldRemovalLink) != 'undefined' ? options.formFieldRemovalLink : '.custom_form_remove_field';
      this.formFieldAddOption  = typeof(options.formFieldAddOption) != 'undefined' ? options.formFieldRemovalLink : '.custom_field_add_option';
      
      $(this.formFieldTypeList+' li').each(function(elem_index, elem_value){
        var $this = $(this);
        $this.bind('click', function(){
          var $this = $(this);
          if ($this.attr('data-custom-form-type') != 'close')
          {
            $.cjgCustomForm.addField($this.attr('data-custom-form-type'));
          }
        });
      });
      
      $(this.formFieldRemovalLink).live('click',function(){
        var $this = $(this);
        $this.parent().parent().remove();
      });
      
      $(this.formFieldAddOption).live('click',function(){
        var $this = $(this);
        var $input_field = $this.parent().find('.custom_form_field_'+$this.attr('data-form-field-type'));
        if ($.trim($input_field.val()) != '')
        {
          if ($this.attr('data-form-field-type') == 'checkbox')
            $.cjgCustomForm.addFieldCheckboxOptionField($this, $input_field.val());
          else if ($this.attr('data-form-field-type') == 'radio')
            $.cjgCustomForm.addFieldRadioOptionField($this, $input_field.val());
          else if ($this.attr('data-form-field-type') == 'dropdown')
            $.cjgCustomForm.addFieldDropdownOptionField($this, $input_field.val());
          else {}
          
          $input_field.val('');
          
          $this.parent().find('.custom_form_show_options').trigger('click');
        }
      });
      
      $('.form_option_listing .remove_form_field_option').live('click',function(){
        var $this = $(this);
      //console.log('.field_'+$this.attr('data-form-field-type')+'_'+$this.attr('data-form-field-index')+' .value_field');
        $('.field_'+$this.attr('data-form-field-type')+'_'+$this.attr('data-form-field-index')+' .value_field').each(function(){
          var $this_children = $(this);
          if ($this.attr('data-form-field-option-index') == $this_children.attr('data-custom-form-'+$this.attr('data-form-field-type')+'-option-index'))
          {
            $this_children.remove();
          }
        });

        $this.parent().remove();
      });
    },
    addField: function(field)
    {
      var formContainer = $(this.formContainer);
      var html = '';
      html += '<span class="grippy"><img alt="Sort" src="/images/grippy.png" />&nbsp;</span><span class="'+field+' custom_form_label"><input style="margin-right:9px;" type="text" name="form_field_label['+this.index+']" value="" class="default_text" title=" Field label" /></span>';
      
      if (field == 'text')
      {
        html += this.formFieldText();
      }
      else if (field == 'textarea')
      {
        html += this.formFieldTextarea();
      }
      else if (field == 'file')
      {
        html += this.formFieldFile();
      }
      else if (field == 'checkbox')
      {
        html += this.formFieldCheckbox();
      }
      else if (field == 'radio')
      {
        html += this.formFieldRadio();
      }
      else if (field == 'dropdown')
      {
        html += this.formFieldDropdown();
      }
      
      this.index++;
      
      var form_field_removal_class = this.formFieldRemovalLink;
      var form_field_index = (this.index - 1);
      form_field_removal_class = form_field_removal_class.replace('.', '');
      html += '<span class="action"><a href="javascript:void(0);" class="'+form_field_removal_class+'"><img alt="Remove" src="/sfPropel15Plugin/images/delete.png" /></a></span>';
      html += '<span class="custom_form_help_text">Help text: <input type="text" name="form_field_help_text['+form_field_index+']" value="" class="help_text" title=" Help text"></span>';
      
      $('<li class="fields field_'+field+' field_'+field+'_'+form_field_index+'">'+html+'</li>').appendTo(formContainer);
      $(".default_text").blur(); 
    },
    formFieldText: function()
    {
      var html = '<span class="custom_form_field"><input type="text" name="form_field['+this.index+']" value="" title=" Type default value here" class="default_text" /><input type="hidden" name="form_field_type['+this.index+']" value="text" /><label>Required: <input type="checkbox" name="form_field_required['+this.index+']"></label></span>';
      
      return html;
    },
    formFieldTextarea: function()
    {
      var html = '<span class="custom_form_field"><input type="text" name="form_field['+this.index+']" value="" title=" Type default value here" class="default_text" /><input type="hidden" name="form_field_type['+this.index+']" value="textarea" /><label>Required: <input type="checkbox" name="form_field_required['+this.index+']"></label></span>';
      
      return html;
    },
    formFieldFile: function()
    {
      var html = '<span class="custom_form_field"><input type="file" name="form_field_dummy['+this.index+']" disabled/><input type="hidden" name="form_field_type['+this.index+']" value="file" /><input type="hidden" name="form_field['+this.index+']" value="file" /></span>';
      
      return html;
    },
    formFieldCheckbox: function()
    {
      var form_field_add_option = this.formFieldAddOption;
      form_field_add_option = form_field_add_option.replace('.', '');
      
      var html = '<span class="custom_form_field"><input data-form-field-type="checkbox" type="text" class="custom_form_field_checkbox" name="form_field['+this.index+']" value=""/><input type="hidden" name="form_field_type['+this.index+']" value="checkbox" /><a class="'+form_field_add_option+'" title="Add to options" data-custom-form-checkbox-index="'+this.index+'" data-form-field-type="checkbox"><img alt="Add to options" src="/sfPropel15Plugin/images/new.png" /></a><a class="custom_form_show_options" title="Show options list" data-custom-form-checkbox-index="'+this.index+'" onclick="javascript:$.cjgCustomForm.showFieldCheckboxOptionField(this);"><img alt="Show option list" src="/sfPropel15Plugin/images/default.png" /></a></span>';

      return html;
    },
    addFieldCheckboxOptionField: function($elem, label)
    {
      var highest_index = 1;
      var checkbox_index = $elem.attr('data-custom-form-checkbox-index');
      $elem.siblings('input.value_field').each(function(elem_index, elem_value){
        var $this = $(this);
        if (parseInt($this.attr('data-custom-form-checkbox-option-index')) > highest_index)
        {
          highest_index = parseInt($this.attr('data-custom-form-checkbox-option-index'));
        }
      });
      
      $('<input class="value_field" type="hidden" value="'+label+'" name="form_field_option['+checkbox_index+']['+(highest_index+1)+']" data-custom-form-checkbox-index="'+checkbox_index+'" data-custom-form-checkbox-option-index="'+(highest_index+1)+'" />').insertBefore($elem);
      
      return false;
    },
    showFieldCheckboxOptionField: function(elem)
    {
      var $elem = $(elem);
      var index = $elem.attr('data-custom-form-checkbox-index');
      
      var html = '<div class="form_option_listing">';
      html += '<ul>';
      $('.field_checkbox_'+index+' .value_field').each(function(elem_index, elem_value){
        var $this = $(this);
        var option_index = $this.attr('data-custom-form-checkbox-option-index');
        html += '<li><a class="remove_form_field_option" onclick="javascript:void(0);" data-form-field-type="checkbox" data-form-field-index="'+index+'" data-form-field-option-index="'+option_index+'"><img alt="Remove" src="/sfPropel15Plugin/images/delete.png" /></a>&nbsp;'+$this.val()+'</li>';
      });
      html += '</ul>';
      html += '</div>';
      
      $(html).dialog({
        height: 140,
        modal: true,
        title: 'Options list'
      });      
    },
    formFieldRadio: function()
    {
      var form_field_add_option = this.formFieldAddOption;
      form_field_add_option = form_field_add_option.replace('.', '');
      
      var html = '<span class="custom_form_field"><input data-form-field-type="checkbox" type="text" class="custom_form_field_radio" name="form_field['+this.index+']" value=""/><input type="hidden" name="form_field_type['+this.index+']" value="radio" /><a class="'+form_field_add_option+'" title="Add to options" data-custom-form-radio-index="'+this.index+'" data-form-field-type="radio"><img alt="Add to options" src="/sfPropel15Plugin/images/new.png" /></a><a class="custom_form_show_options" title="Show options list" data-custom-form-radio-index="'+this.index+'" onclick="javascript:$.cjgCustomForm.showFieldRadioOptionField(this);"><img alt="Show option list" src="/sfPropel15Plugin/images/default.png" /></a></span>';
      
      return html;
    },
    addFieldRadioOptionField: function($elem, label)
    {
      var highest_index = 1;
      var radio_index = $elem.attr('data-custom-form-radio-index');
      $elem.siblings('input.value_field').each(function(elem_index, elem_value){
        var $this = $(this);
        if (parseInt($this.attr('data-custom-form-radio-option-index')) > highest_index)
        {
          highest_index = parseInt($this.attr('data-custom-form-radio-option-index'));
        }
      });
      
      $('<input class="value_field" type="hidden" value="'+label+'" name="form_field_option['+radio_index+']['+(highest_index+1)+']" data-custom-form-radio-index="'+radio_index+'" data-custom-form-radio-option-index="'+(highest_index+1)+'" />').insertBefore($elem);
      
      return false;      
    },
    showFieldRadioOptionField: function(elem)
    {
      var $elem = $(elem);
      var index = $elem.attr('data-custom-form-radio-index');
      
      var html = '<div class="form_option_listing">';
      html += '<ul>';
      $('.field_radio_'+index+' .value_field').each(function(elem_index, elem_value){
        var $this = $(this);
        var option_index = $this.attr('data-custom-form-radio-option-index');
        html += '<li><a class="remove_form_field_option" onclick="javascript:void(0);" data-form-field-type="radio" data-form-field-index="'+index+'" data-form-field-option-index="'+option_index+'"><img alt="Remove" src="/sfPropel15Plugin/images/delete.png" /></a>&nbsp;'+$this.val()+'</li>';
      });
      html += '</ul>';
      html += '</div>';
      
      $(html).dialog({
        height: 140,
        modal: true,
        title: 'Options list'
      });      
    },
    formFieldDropdown: function()
    {
      var form_field_add_option = this.formFieldAddOption;
      form_field_add_option = form_field_add_option.replace('.', '');
      
      var html = '<span class="custom_form_field"><input data-form-field-type="dropdown" type="text" class="custom_form_field_dropdown" name="form_field['+this.index+']" value=""/><input type="hidden" name="form_field_type['+this.index+']" value="dropdown" /><a class="'+form_field_add_option+'" title="Add to options" data-custom-form-dropdown-index="'+this.index+'" data-form-field-type="dropdown"><img alt="Add to options" src="/sfPropel15Plugin/images/new.png" /></a><a class="custom_form_show_options" title="Show options list" data-custom-form-dropdown-index="'+this.index+'" onclick="javascript:$.cjgCustomForm.showFieldDropdownOptionField(this);"><img alt="Show option list" src="/sfPropel15Plugin/images/default.png" /></a></span>';
      
      return html;
    },
    addFieldDropdownOptionField: function($elem, label)
    {
      var highest_index = 1;
      var dropdown_index = $elem.attr('data-custom-form-dropdown-index');
      $elem.siblings('input.value_field').each(function(elem_index, elem_value){
        var $this = $(this);
        if (parseInt($this.attr('data-custom-form-dropdown-option-index')) > highest_index)
        {
          highest_index = parseInt($this.attr('data-custom-form-dropdown-option-index'));
        }
      });
      
      $('<input class="value_field" type="hidden" value="'+label+'" name="form_field_option['+dropdown_index+']['+(highest_index+1)+']" data-custom-form-dropdown-index="'+dropdown_index+'" data-custom-form-dropdown-option-index="'+(highest_index+1)+'" />').insertBefore($elem);
      
      return false;      
    },
    showFieldDropdownOptionField: function(elem)
    {
      var $elem = $(elem);
      var index = $elem.attr('data-custom-form-dropdown-index');
      
      var html = '<div class="form_option_listing">';
      html += '<ul>';
      $('.field_dropdown_'+index+' .value_field').each(function(elem_index, elem_value){
        var $this = $(this);
        var option_index = $this.attr('data-custom-form-dropdown-option-index');
        html += '<li><a class="remove_form_field_option" onclick="javascript:void(0);" data-form-field-type="dropdown" data-form-field-index="'+index+'" data-form-field-option-index="'+option_index+'"><img alt="Remove" src="/sfPropel15Plugin/images/delete.png" /></a>&nbsp;'+$this.val()+'</li>';
      });
      html += '</ul>';
      html += '</div>';
      
      $(html).dialog({
        height: 140,
        modal: true,
        title: 'Options list'
      });      
    }
	};
})(jQuery);

$(document).ready(function(){
  $.cjgFunction.init();

  $('#set_local_editorial_content_start_at').click(function() {
    if ($('#local_editorial_content_start_at_day').length > 0)
    {
      var date = new Date();
      $('#local_editorial_content_start_at_day').val(date.getDate());
      $('#local_editorial_content_start_at_month').val(date.getUTCMonth()+1);
      $('#local_editorial_content_start_at_year').val(date.getFullYear());
    }
  });

  $('form#local_form').bind('submit', function(){
    var submit_custom_form = true;
    $(this).find('input.required_field,textarea.required_field,').each(function(i, e){
      if ($.trim($(this).val()) == '')
      {
        $(this).addClass('required_field_error');
        submit_custom_form = false;
      }
      else
      {
        $(this).removeClass('required_field_error');
      }
    });

    if (!submit_custom_form)
    {
      alert('Let op: velden met een (*) zijn verplicht.');
    }

    return submit_custom_form;
  });

  $('.faq_container .faq_list .faq_options ul li a').bind('click', function(){
    var $this = $(this);
     $.ajax({
        async: false,
        type: 'post',
        url: $.cjgFunction.faqLogger + '/' + $this.attr('data-faq'),
        success: function(){
            return;
        }
     });
     
     return true;
  });
  
//  $(".default_text").live('focus', function()
//  {
//    if ($(this).val() == $(this)[0].title)
//    {
//      $(this).removeClass("default_text_active");
//      $(this).val("");
//    }
//  });
//
//  $(".default_text").live('blur', function()
//  {
//    if ($(this).val() == "")
//    {
//      $(this).addClass("default_text_active");
//      $(this).val($(this)[0].title);
//    }
//  });
//
//  $(".default_text").blur(); 
});

function menuNewWin()
{
  $('ul.home_navigation li a.menu_new_win').click(function(){
    window.open($(this).attr('href'));

    return false;
  });
}

