Your IP : 216.73.217.53


Current Path : /home/bonneesp/www/components/com_gallery_wd/js/
Upload File :
Current File : /home/bonneesp/www/components/com_gallery_wd/js/bwg.js

function edit_tag(m,token) {

  var name, slug, tr;

  name = jQuery("#name" + m).html();

  slug = jQuery("#slug" + m).html();

  count = jQuery("#count" + m).html();

  tr = ' <td id="td_check_'+m+'" ></td> <td id="td_id_'+m+'" ></td> <td id="td_name_'+m+'" class="edit_input"><input id="edit_tagname" name="tagname'+m+'" class="input_th2" type="text" value="'+name+'"></td> <td id="td_slug_'+m+'" class="edit_input"><input id="edit_slug" class="input_th2"  name="slug'+m+'" type="text" value="'+slug+'"></td> <td id="td_count_'+m+'" ></td> <td id="td_edit_'+m+'" class="table_big_col"><a class="button-primary button button-small" onclick="save_tag('+m+',\''+token+'\')" >Save Tag </a></td><td id="td_delete_'+m+'" class="table_big_col" ></td> ';

  jQuery("#tr_" + m).html(tr);

  jQuery("#td_id_" + m).attr('class', 'table_big_col');

}



function save_tag(tag_id,token) { 



  var tagname=jQuery('input[name=tagname'+tag_id+']').val();

  var slug = jQuery('input[name=slug'+tag_id+']').val();

  //var datas = "&task=tags.edit_tag&name="+tagname+"&"+"slug="+slug+"&"+"id="+tag_id+"&"+token+"=1";

var datas = {};

datas['task']="tags.edit_tag";
datas['name']=tagname;
datas['slug']=slug;
datas['id']=tag_id;
datas[token]='1';

  var td_check,td_name,td_slug,td_count,td_edit,td_delete,massege;

  //+ "=bwg_edit_tag",



 jQuery.ajax({ 

  type: "POST",  

  url: ajax_url,  

  data: datas,  

  success: function(html){



  var tagname_slug=html;		

  var array = tagname_slug.split(".");

  if(array[0]=="The slug must be unique."){massege=array[0];}

  

  else { massege="Item Successfully Saved.";

  

  jQuery("#td_check_" + tag_id).attr('class', 'table_small_col check-column');

  td_check='<input id="check_'+tag_id+'" name="check_'+tag_id+'" type="checkbox" />';

  jQuery("#td_check_" + tag_id).html(td_check);



  

  jQuery("#td_id_" + tag_id).attr('class', 'table_small_col');

  jQuery("#td_id_" + tag_id).html(tag_id);

  

  jQuery("#td_name_" + tag_id).removeClass();

  td_name='<a class="pointer" id="name'+tag_id+'" onclick="edit_tag('+tag_id+',\''+token+'\')"  title="Edit">'+array[0]+'</a>';

  jQuery("#td_name_" + tag_id).html(td_name); 

  

  jQuery("#td_slug_" + tag_id).removeClass();

  td_slug='<a class="pointer" id="slug'+tag_id+'" onclick="edit_tag('+tag_id+',\''+token+'\')"  title="Edit">'+array[1]+'</a>';

  jQuery("#td_slug_" + tag_id).html(td_slug);



  jQuery("#td_count_" + tag_id).attr('class', 'table_big_col');

  td_count='<a class="pointer" id="count'+tag_id+'" onclick="edit_tag('+tag_id+',\''+token+'\')"  title="Edit">'+array[2]+'</a>';

  jQuery("#td_count_" + tag_id).html(td_count);

  

  td_edit='<a class="pointer" onclick="edit_tag('+tag_id+',\''+token+'\')" >Edit</a>';

  jQuery("#td_edit_" + tag_id).html(td_edit);



  var func1="spider_set_input_value('task', 'tags.delete');";

  var func2="spider_set_input_value('current_id', "+tag_id+");";

  var func3="spider_form_submit('event', 'adminForm')";

  td_delete='<a class="pointer" onclick="'+func1+func2+func3+'" >Delete</a>';

  jQuery("#td_delete_" + tag_id).html(td_delete);

  }

  if((jQuery( ".updated" ) && jQuery( ".updated" ).attr("id")!='wordpress_message_2' ) || (jQuery( ".error" ) && jQuery( ".error" ).css("display")=="block"))

  {

  if(jQuery( ".updated" ) && jQuery( ".updated" ).attr("id")!='wordpress_message_2' ){

  jQuery(".updated").html("<strong><p>"+massege+"</p></strong>"); 

  }

  else {

  jQuery(".error").html("<strong><p>"+massege+"</p></strong>"); 

  jQuery(".error").attr('class', 'updated');

  }

  }

  else {

  jQuery( "#wordpress_message_1" ).css("display","block")

  }

  }  

});

}


function closeWindow() {
    window.parent.SqueezeBox.close();
}


function add_modal()
	  {
	  window.addEvent('domready', function() {

			SqueezeBox.initialize({});
			SqueezeBox.assign($$('a.modal'), {
				parse: 'rel'
			});
		});
	  }

var bwg_save_count = 50;
function spider_ajax_save(form_id, tr_group,token) {

  var ajax_task = jQuery("#ajax_task").val();
  if (!tr_group) {
    var tr_group = 1;
  }
  else if (ajax_task == 'ajax_apply') {
    ajax_task = '';
  }
  else
  if(ajax_task == 'ajax_save')
  {
  ajax_task == 'ajax_save';
  }
 

  var name = jQuery("#name").val();
  var slug = jQuery("#slug").val();
  if ((typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden()) {
    var description = tinyMCE.activeEditor.getContent();
  }
  else {
    var description = jQuery("#description").val();
  }
  var preview_image = jQuery("#preview_image").val();
  var published = jQuery("input[name=published]:checked").val();
  var search_value = jQuery("#search_value").val();
  var current_id = jQuery("#current_id").val();
  var page_number = jQuery("#page_number").val();
  var search_or_not = jQuery("#search_or_not").val();
  var ids_string = jQuery("#ids_string").val();
  var image_order_by = jQuery("#image_order_by").val();
  var asc_or_desc = jQuery("#asc_or_desc").val();
  var order = jQuery("#order").val();
  var id = jQuery("#id").val();

  var image_current_id = jQuery("#image_current_id").val();
  ids_array = ids_string.split(",");
  var tr_count = ids_array.length;
  if (tr_count > bwg_save_count) {
    // Remove items form begin and end of array.
    ids_array.splice(tr_group * bwg_save_count, ids_array.length);
    ids_array.splice(0, (tr_group - 1) * bwg_save_count);
    ids_string = ids_array.join(",");
  }

  var post_data = {};
  post_data["name"] = name;
  post_data["slug"] = slug;
  post_data["cid[]"] = current_id;
  post_data["description"] = description;
  post_data["preview_image"] = preview_image;
  post_data["published"] = published;
  post_data["search_value"] = search_value;
  post_data["current_id"] = current_id;
  post_data["page_number"] = page_number;
  post_data["image_order_by"] = image_order_by;
  post_data["asc_or_desc"] = asc_or_desc;
  post_data["ids_string"] = ids_string;
  post_data["order"] = order;
  post_data["task"] = "galleries.ajax_search";
  post_data["ajax_task"] = ajax_task;
  post_data[token] = '1';
  
  


  post_data["image_current_id"] = image_current_id;
    post_data["image_width"] = jQuery("#image_width").val();
  post_data["image_height"] = jQuery("#image_height").val();
  
  var flag = false;
  if (jQuery("#check_all_items").attr('checked') == 'checked') {
    post_data["check_all_items"] = jQuery("#check_all_items").val();
    flag = true;
    jQuery('#check_all_items').attr('checked', false);
  }
  for (var i=0;i<ids_array.length ;i++) {
    if (ids_array[i]) {
      if (jQuery("#check_" + ids_array[i]).attr('checked') == 'checked') {
        post_data["check_" + ids_array[i]] = jQuery("#check_" + ids_array[i]).val();

        flag = true;
		
		
      }
      post_data["input_filename_" + ids_array[i]] = jQuery("#input_filename_" + ids_array[i]).val();
      post_data["image_url_" + ids_array[i]] = jQuery("#image_url_" + ids_array[i]).val();
      post_data["thumb_url_" + ids_array[i]] = jQuery("#thumb_url_" + ids_array[i]).val();
      post_data["image_description_" + ids_array[i]] = jQuery("#image_description_" + ids_array[i]).val();
      post_data["image_alt_text_" + ids_array[i]] = jQuery("#image_alt_text_" + ids_array[i]).val();
	  post_data["redirect_url_" + ids_array[i]] = jQuery("#redirect_url_" + ids_array[i]).val();
      post_data["input_date_modified_" + ids_array[i]] = jQuery("#input_date_modified_" + ids_array[i]).val();
      post_data["input_size_" + ids_array[i]] = jQuery("#input_size_" + ids_array[i]).val();
      post_data["input_filetype_" + ids_array[i]] = jQuery("#input_filetype_" + ids_array[i]).val();
      post_data["input_resolution_" + ids_array[i]] = jQuery("#input_resolution_" + ids_array[i]).val();
      post_data["input_crop_" + ids_array[i]] = jQuery("#input_crop_" + ids_array[i]).val();
      post_data["order_input_" + ids_array[i]] = jQuery("#order_input_" + ids_array[i]).val();
      post_data["tags_" + ids_array[i]] = jQuery("#tags_" + ids_array[i]).val();
	  
	
    }
  }
  // Loading.
  jQuery('#opacity_div').show();
  jQuery('#loading_div').show();

  jQuery.post(
    jQuery('#' + form_id).action,
    post_data,

    function (data) {
      var str = jQuery(data).find("#current_id").val();
      jQuery("#current_id").val(str);
     
	  
    }
  ).success(function (data, textStatus, errorThrown) {
    if (tr_count > bwg_save_count * tr_group) {
      spider_ajax_save(form_id, ++tr_group,token);
      return;
    }
    else {
      var str = jQuery(data).find('#images_table').html();
      jQuery('#images_table').html(str);
      var str = jQuery(data).find('.tablenav').html();
      jQuery('.tablenav').html(str);
      jQuery("#show_hide_weights").val("Hide order column");
      spider_show_hide_weights();
      spider_run_checkbox();

      if (ajax_task == 'ajax_apply') {
        jQuery('#message_div').html("<strong><p>Items Successfully Saved.</p></strong>");
        jQuery('#message_div').show();
      }
      else if (ajax_task == 'recover') {
        jQuery('#draganddrop').html("<strong><p>Item Successfully Recovered.</p></strong>");
        jQuery('#draganddrop').show();
      }
      else if (ajax_task == 'image_publish') {
        jQuery('#draganddrop').html("<strong><p>Item Successfully Published.</p></strong>");
        jQuery('#draganddrop').show();
      }
      else if (ajax_task == 'image_unpublish') {
        jQuery('#draganddrop').html("<strong><p>Item Successfully Unpublished.</p></strong>");
        jQuery('#draganddrop').show();
      }
      else if (ajax_task == 'image_delete') {
        jQuery('#draganddrop').html("<strong><p>Item Successfully Deleted.</p></strong>");
        jQuery('#draganddrop').show();
      }
      else if (!flag && ((ajax_task == 'image_publish_all') || (ajax_task == 'image_unpublish_all') || (ajax_task == 'image_delete_all') || (ajax_task == 'image_set_watermark') || (ajax_task == 'image_recover_all'))) {
        jQuery('#draganddrop').html("<strong><p>You must select at least one item.</p></strong>");
        jQuery('#draganddrop').show();
      }
      else if (ajax_task == 'image_publish_all') {
        jQuery('#draganddrop').html("<strong><p>Items Successfully Published.</p></strong>");
        jQuery('#draganddrop').show();
      }
      else if (ajax_task == 'image_unpublish_all') {
        jQuery('#draganddrop').html("<strong><p>Items Successfully Unpublished.</p></strong>");
        jQuery('#draganddrop').show();
      }
      else if (ajax_task == 'image_delete_all') {
        jQuery('#draganddrop').html("<strong><p>Items Successfully Deleted.</p></strong>");
        jQuery('#draganddrop').show();
      }
      else if (ajax_task == 'image_set_watermark') {
        jQuery('#draganddrop').html("<strong><p>Watermarks Successfully Set.</p></strong>");
        jQuery('#draganddrop').show();
      }
      else if (ajax_task == 'image_recover_all') {
        jQuery('#draganddrop').html("<strong><p>Items Successfully Reset.</p></strong>");
        jQuery('#draganddrop').show();
      }
      else {
        jQuery('#draganddrop').html("<strong><p>Items Successfully Saved.</p></strong>");
        jQuery('#draganddrop').show();
      }
      if (ajax_task == "ajax_save") {
        jQuery('#' + form_id).submit();
      }
      jQuery('#opacity_div').hide();
      jQuery('#loading_div').hide();
    }
	
	add_modal();
  });
  // if (event.preventDefault) {
  // event.preventDefault();
  // }
  // else {
  // event.returnValue = false;
  // }
  return false;
}

function spider_run_checkbox() {
  jQuery("tbody").children().children(".check-column").find(":checkbox").click(function (l) {
    if ("undefined" == l.shiftKey) {
      return true
    }
    if (l.shiftKey) {
      if (!i) {
        return true
      }
      d = jQuery(i).closest("form").find(":checkbox");
      f = d.index(i);
      j = d.index(this);
      h = jQuery(this).prop("checked");
      if (0 < f && 0 < j && f != j) {
        d.slice(f, j).prop("checked", function () {
          if (jQuery(this).closest("tr").is(":visible")) {
            return h
          }
          return false
        })
      }
    }
    i = this;
    var k = jQuery(this).closest("tbody").find(":checkbox").filter(":visible").not(":checked");
    jQuery(this).closest("table").children("thead, tfoot").find(":checkbox").prop("checked", function () {
      return(0 == k.length)
    });
    return true
  });
  jQuery("thead, tfoot").find(".check-column :checkbox").click(function (m) {
    var n = jQuery(this).prop("checked"), l = "undefined" == typeof toggleWithKeyboard ? false : toggleWithKeyboard, k = m.shiftKey || l;
    jQuery(this).closest("table").children("tbody").filter(":visible").children().children(".check-column").find(":checkbox").prop("checked", function () {
      if (jQuery(this).is(":hidden")) {
        return false
      }
      if (k) {
        return jQuery(this).prop("checked")
      } else {
        if (n) {
          return true
        }
      }
      return false
    });
    jQuery(this).closest("table").children("thead,  tfoot").filter(":visible").children().children(".check-column").find(":checkbox").prop("checked", function () {
      if (k) {
        return false
      } else {
        if (n) {
          return true
        }
      }
      return false
    })
  });
}

// Set value by id.
function spider_set_input_value(input_id, input_value) {
  if (document.getElementById(input_id)) {
    document.getElementById(input_id).value = input_value;
  }
}

// Submit form by id.
function spider_form_submit(event, form_id) {
  if (document.getElementById(form_id)) {
    document.getElementById(form_id).submit();
  }
  if (event.preventDefault) {
    event.preventDefault();
  }
  else {
    event.returnValue = false;
  }
}

// Check if required field is empty.
function spider_check_required(id, name) {
  if (jQuery('#' + id).val() == '') {
    alert(name + '* field is required.');
    jQuery('#' + id).attr('style', 'border-color: #FF0000;');
    jQuery('#' + id).focus();
    jQuery('html, body').animate({
      scrollTop:jQuery('#' + id).offset().top - 200
    }, 500);
    return true;
  }
  else {
    return false;
  }
}

// Show/hide order column and drag and drop column.
function spider_show_hide_weights() {

  if (jQuery("#show_hide_weights").val() == 'Show order column') {
    jQuery(".connectedSortable").css("cursor", "default");
    jQuery("#tbody_arr").find(".handle").hide(0);
    jQuery("#th_order").show(0);
    jQuery("#tbody_arr").find(".spider_order").show(0);
    jQuery("#show_hide_weights").val("Hide order column");
    if (jQuery("#tbody_arr").sortable()) {
      jQuery("#tbody_arr").sortable("disable");
    }
  }
  else {

    jQuery(".connectedSortable").css("cursor", "move");
    var page_number;
	page_num=parseInt(jQuery('.paging-input').find('.total-pages').html());
    if (page_num && page_num != '' && page_num != 1) {
      page_number = (page_num - 1) * 20 + 1;
    }
    else {
      page_number = 1;
    }
	//page_number = 1;

    jQuery("#tbody_arr").sortable({
	
      handle:".connectedSortable",
      connectWith:".connectedSortable",
      update:function (event, tr) {

        jQuery("#draganddrop").attr("style", "");
        jQuery("#draganddrop").html("<strong><p>Changes made in this table should be saved.</p></strong>");
        var i = page_number;
        jQuery('.spider_order').each(function (e) {
          if (jQuery(this).find('input').val()) {
            jQuery(this).find('input').val(i++);
          }
        });
      }
    });//.disableSelection();
    jQuery("#tbody_arr").sortable("enable");
    jQuery("#tbody_arr").find(".handle").show(0);
    jQuery("#tbody_arr").find(".handle").attr('class', 'handle connectedSortable');
    jQuery("#th_order").hide(0);
    jQuery("#tbody_arr").find(".spider_order").hide(0);
    jQuery("#show_hide_weights").val("Show order column");
  }
}

// Set uploader to button class.
function spider_uploader(button_id, input_id, delete_id, img_id) {
  if (typeof img_id == 'undefined') {
    img_id = '';
  }
  jQuery(function () {
    var formfield = null;
    window.original_send_to_editor = window.send_to_editor;
    window.send_to_editor = function (html) {
      if (formfield) {
        var fileurl = jQuery('img', html).attr('src');
        if (!fileurl) {
          var exploded_html;
          var exploded_html_askofen;
          exploded_html = html.split('"');
          for (i = 0; i < exploded_html.length; i++) {
            exploded_html_askofen = exploded_html[i].split("'");
          }
          for (i = 0; i < exploded_html.length; i++) {
            for (j = 0; j < exploded_html_askofen.length; j++) {
              if (exploded_html_askofen[j].search("href")) {
                fileurl = exploded_html_askofen[i + 1];
                break;
              }
            }
          }
          if (img_id != '') {
            alert('You must select an image file.');
            closeWindow();
            return;
          }
          window.parent.document.getElementById(input_id).value = fileurl;
          window.parent.document.getElementById(button_id).style.display = "none";
          window.parent.document.getElementById(input_id).style.display = "inline-block";
          window.parent.document.getElementById(delete_id).style.display = "inline-block";
        }
        else {
          if (img_id == '') {
            alert('You must select an audio file.');
            closeWindow();
            return;
          }
          window.parent.document.getElementById(input_id).value = fileurl;
          window.parent.document.getElementById(button_id).style.display = "none";
          window.parent.document.getElementById(delete_id).style.display = "inline-block";
          if ((img_id != '') && window.parent.document.getElementById(img_id)) {
            window.parent.document.getElementById(img_id).src = fileurl;
            window.parent.document.getElementById(img_id).style.display = "inline-block";
          }
        }
        formfield.val(fileurl);
        closeWindow();
      }
      else {
        window.original_send_to_editor(html);
      }
      formfield = null;
    };
    formfield = jQuery(this).parent().parent().find(".url_input");
    tb_show('', 'media-upload.php?type=image&TB_iframe=true');
    jQuery('#TB_overlay,#TB_closeWindowButton').bind("click", function () {
      formfield = null;
    });
    return false;
  });
}

// Remove uploaded file.
function spider_remove_url(button_id, input_id, delete_id, img_id) {
  if (typeof img_id == 'undefined') {
    img_id = '';
  }
  if (document.getElementById(button_id)) {
    document.getElementById(button_id).style.display = '';
  }
  if (document.getElementById(input_id)) {
    document.getElementById(input_id).value = '';
    document.getElementById(input_id).style.display = 'none';
  }
  if (document.getElementById(delete_id)) {
    document.getElementById(delete_id).style.display = 'none';
  }
  if ((img_id != '') && window.parent.document.getElementById(img_id)) {
    document.getElementById(img_id).src = '';
    document.getElementById(img_id).style.display = 'none';
  }
}

function spider_reorder_items(tbody_id) {
  jQuery("#" + tbody_id).sortable({
    handle:".connectedSortable",
    connectWith:".connectedSortable",
    update:function (event, tr) {
      spider_sortt(tbody_id);
    }
  });
}

function spider_sortt(tbody_id) {
  var str = "";
  var counter = 0;
  jQuery("#" + tbody_id).children().each(function () {
    str += ((jQuery(this).attr("id")).substr(3) + ",");
    counter++;
  });
  jQuery("#albums_galleries").val(str);
  if (!counter) {
    document.getElementById("table_albums_galleries").style.display = "none";
  }
}

function spider_remove_row(tbody_id, event, obj) {
  var span = obj;
  var tr = jQuery(span).closest("tr");
  jQuery(tr).remove();
  spider_sortt(tbody_id);
}

function spider_jslider(idtaginp) {
  jQuery(function () {
    var inpvalue = jQuery("#" + idtaginp).val();
    if (inpvalue == "") {
      inpvalue = 50;
    }
    jQuery("#slider-" + idtaginp).slider({
      range:"min",
      value:inpvalue,
      min:1,
      max:100,
      slide:function (event, ui) {
        jQuery("#" + idtaginp).val("" + ui.value);
      }
    });
    jQuery("#" + idtaginp).val("" + jQuery("#slider-" + idtaginp).slider("value"));
  });
}

function spider_get_items(e) {
  if (e.preventDefault) {
    e.preventDefault();
  }
  else {
    e.returnValue = false;
  }
  var trackIds = [];
  var titles = [];
  var types = [];
  var tbody = document.getElementById('tbody_albums_galleries');
  var trs = tbody.getElementsByTagName('tr');
  for (j = 0; j < trs.length; j++) {
    i = trs[j].getAttribute('id').substr(3);
    if (document.getElementById('check_' + i).checked) {
      trackIds.push(document.getElementById("id_" + i).innerHTML);
      titles.push(document.getElementById("a_" + i).innerHTML);
      types.push(document.getElementById("url_" + i).innerHTML == "Album" ? 1 : 0);
    }
  }
  window.parent.bwg_add_items(trackIds, titles, types);
}

function bwg_get_tags(image_id, e) {
  if (e.preventDefault) {
    e.preventDefault();
  }
  else {
    e.returnValue = false;
  }
  var tagIds = [];
  var titles = [];
  var tbody = document.getElementById('tbody_arr');
  var trs = tbody.getElementsByTagName('tr');
  for (j = 0; j < trs.length; j++) {
    i = trs[j].getAttribute('id').substr(3);
    if (document.getElementById('check_' + i).checked) {
      tagIds.push(i);
      titles.push(document.getElementById("a_" + i).innerHTML);
    }
  }
  window.parent.bwg_add_tag(image_id, tagIds, titles);
}

function bwg_add_tag(image_id, tagIds, titles) {
  if (image_id == '0') {
    var flag = false;
    var ids_string = jQuery("#ids_string").val();
    ids_array = ids_string.split(",");
    if (jQuery("#check_all_items").attr("checked")) {
      var added_tags = '';
      for (i = 0; i < tagIds.length; i++) {
        added_tags = added_tags + tagIds[i] + ',';
      }
      jQuery("#added_tags_select_all").val(added_tags);
    }
  }
  else {
    image_id = image_id + ','; 
    ids_array = image_id.split(",");
    var flag = true;
  }
  for (var i in ids_array) {
    if (ids_array.hasOwnProperty(i) && ids_array[i]) {
      if (jQuery("#check_" + ids_array[i]).attr('checked') == 'checked' || flag) {
        image_id = ids_array[i];
        var tag_ids = document.getElementById('tags_' + image_id).value;
        tags_array = tag_ids.split(',');
        var div = document.getElementById('tags_div_' + image_id);
        var counter = 0;
        for (i = 0; i < tagIds.length; i++) {
          if (tags_array.indexOf(tagIds[i]) == -1) {
            tag_ids = tag_ids + tagIds[i] + ',';
            var tag_div = document.createElement('div');
            tag_div.setAttribute('id', image_id + "_tag_" + tagIds[i]);
            tag_div.setAttribute('class', "tag_div");
            div.appendChild(tag_div);
            var tag_name_span = document.createElement('span');
            tag_name_span.setAttribute('class', "tag_name");
            tag_name_span.innerHTML = titles[i];
            tag_div.appendChild(tag_name_span);
            var tag_delete_span = document.createElement('span');
            tag_delete_span.setAttribute('class', "spider_delete_img_small");
            tag_delete_span.setAttribute('onclick', "bwg_remove_tag('" + tagIds[i] + "', '" + image_id + "')");
            tag_delete_span.setAttribute('style', "float:right;");
            tag_div.appendChild(tag_delete_span);
            counter++;
          }
        }
        document.getElementById('tags_' + image_id).value = tag_ids;
        if (counter) {
          div.style.display = "block";
        }
      }
	  }
  }
    closeWindow();
}


function bwg_remove_tag(tag_id, image_id) {
  if (jQuery('#' + image_id + '_tag_' + tag_id)) {
    jQuery('#' + image_id + '_tag_' + tag_id).remove();
    var tag_ids_string = jQuery("#tags_" + image_id).val();
    tag_ids_string = tag_ids_string.replace(tag_id + ',', '');
    jQuery("#tags_" + image_id).val(tag_ids_string);
    if (jQuery("#tags_" + image_id).val() == '') {
      jQuery("#tags_div_" + image_id).hide();
    }
  }
}

function preview_watermark() {
  setTimeout(function() {
    watermark_type = window.parent.document.getElementById('watermark_type_text').checked;
    if (watermark_type) {
      watermark_text = document.getElementById('watermark_text').value;
      watermark_link = document.getElementById('watermark_link').value;
      watermark_font_size = document.getElementById('watermark_font_size').value;
      watermark_font = document.getElementById('watermark_font').value;
      watermark_color = document.getElementById('watermark_color').value;
      watermark_opacity = document.getElementById('watermark_opacity').value;
      watermark_position = jQuery("input[name=watermark_position]:checked").val().split('-');
      document.getElementById("preview_watermark").style.verticalAlign = watermark_position[0];
      document.getElementById("preview_watermark").style.textAlign = watermark_position[1];
      stringHTML = (watermark_link ? '<a href="' + watermark_link + '" target="_blank" style="text-decoration: none;' : '<span style="cursor:default;') + 'margin:4px;font-size:' + watermark_font_size + 'px;font-family:' + watermark_font + ';color:#' + watermark_color + ';opacity:' + (watermark_opacity / 100) + ';" class="non_selectable">' + watermark_text + (watermark_link ? '</a>' : '</span>');
      document.getElementById("preview_watermark").innerHTML = stringHTML;
    }
    watermark_type = window.parent.document.getElementById('watermark_type_image').checked;
    if (watermark_type) {
      watermark_url = document.getElementById('watermark_url').value;
      watermark_link = document.getElementById('watermark_link').value;
      watermark_width = document.getElementById('watermark_width').value;
      watermark_height = document.getElementById('watermark_height').value;
      watermark_opacity = document.getElementById('watermark_opacity').value;
      watermark_position = jQuery("input[name=watermark_position]:checked").val().split('-');
      document.getElementById("preview_watermark").style.verticalAlign = watermark_position[0];
      document.getElementById("preview_watermark").style.textAlign = watermark_position[1];
      stringHTML = (watermark_link ? '<a href="' + watermark_link + '" target="_blank">' : '') + '<img class="non_selectable" src="' + watermark_url + '" style="margin:0 4px 0 4px;max-width:' + watermark_width + 'px;max-height:' + watermark_height + 'px;opacity:' + (watermark_opacity / 100) + ';" />' + (watermark_link ? '</a>' : '');
      document.getElementById("preview_watermark").innerHTML = stringHTML;
    }
  }, 50);
}

function preview_built_in_watermark() {
  setTimeout(function(){
  watermark_type = window.parent.document.getElementById('built_in_watermark_type_text').checked;
  if (watermark_type) {
    watermark_text = document.getElementById('built_in_watermark_text').value;
    watermark_font_size = document.getElementById('built_in_watermark_font_size').value * 400 / 500;
    watermark_font = 'bwg_' + document.getElementById('built_in_watermark_font').value.replace('.TTF', '').replace('.ttf', '');
    watermark_color = document.getElementById('built_in_watermark_color').value;
    watermark_opacity = document.getElementById('built_in_watermark_opacity').value;
    watermark_position = jQuery("input[name=built_in_watermark_position]:checked").val().split('-');
    document.getElementById("preview_built_in_watermark").style.verticalAlign = watermark_position[0];
    document.getElementById("preview_built_in_watermark").style.textAlign = watermark_position[1];
    stringHTML = '<span style="cursor:default;margin:4px;font-size:' + watermark_font_size + 'px;font-family:' + watermark_font + ';color:#' + watermark_color + ';opacity:' + (watermark_opacity / 100) + ';" class="non_selectable">' + watermark_text + '</span>';
    document.getElementById("preview_built_in_watermark").innerHTML = stringHTML;
  }
  watermark_type = window.parent.document.getElementById('built_in_watermark_type_image').checked;
  if (watermark_type) {
    watermark_url = document.getElementById('built_in_watermark_url').value;
    watermark_size = document.getElementById('built_in_watermark_size').value;
    watermark_position = jQuery("input[name=built_in_watermark_position]:checked").val().split('-');
    document.getElementById("preview_built_in_watermark").style.verticalAlign = watermark_position[0];
    document.getElementById("preview_built_in_watermark").style.textAlign = watermark_position[1];
    stringHTML = '<img class="non_selectable" src="' + watermark_url + '" style="margin:0 4px 0 4px;max-width:95%;width:' + watermark_size + '%;" />';
    document.getElementById("preview_built_in_watermark").innerHTML = stringHTML;
  }
  }, 50);
}

function bwg_watermark(watermark_type) {
  jQuery("#" + watermark_type).attr('checked', 'checked');
  jQuery("#tr_watermark_url").css('display', 'none');
  jQuery("#tr_watermark_width_height").css('display', 'none');
  jQuery("#tr_watermark_opacity").css('display', 'none');
  jQuery("#tr_watermark_text").css('display', 'none');
  jQuery("#tr_watermark_link").css('display', 'none');
  jQuery("#tr_watermark_font_size").css('display', 'none');
  jQuery("#tr_watermark_font").css('display', 'none');
  jQuery("#tr_watermark_color").css('display', 'none');
  jQuery("#tr_watermark_position").css('display', 'none');
  jQuery("#tr_watermark_preview").css('display', 'none');
  jQuery("#preview_watermark").css('display', 'none');
  switch (watermark_type) {
    case 'watermark_type_text':
    {
      jQuery("#tr_watermark_opacity").css('display', '');
      jQuery("#tr_watermark_text").css('display', '');
      jQuery("#tr_watermark_link").css('display', '');
      jQuery("#tr_watermark_font_size").css('display', '');
      jQuery("#tr_watermark_font").css('display', '');
      jQuery("#tr_watermark_color").css('display', '');
      jQuery("#tr_watermark_position").css('display', '');
      jQuery("#tr_watermark_preview").css('display', '');
      jQuery("#preview_watermark").css('display', 'table-cell');
      break;
    }
    case 'watermark_type_image':
    {
      jQuery("#tr_watermark_url").css('display', '');
      jQuery("#tr_watermark_link").css('display', '');
      jQuery("#tr_watermark_width_height").css('display', '');
      jQuery("#tr_watermark_opacity").css('display', '');
      jQuery("#tr_watermark_position").css('display', '');
      jQuery("#tr_watermark_preview").css('display', '');
      jQuery("#preview_watermark").css('display', 'table-cell');
      break;
    }
  }
}

function bwg_built_in_watermark(watermark_type) {
  jQuery("#built_in_" + watermark_type).attr('checked', 'checked');
  jQuery("#tr_built_in_watermark_url").css('display', 'none');
  jQuery("#tr_built_in_watermark_size").css('display', 'none');
  jQuery("#tr_built_in_watermark_opacity").css('display', 'none');
  jQuery("#tr_built_in_watermark_text").css('display', 'none');
  jQuery("#tr_built_in_watermark_font_size").css('display', 'none');
  jQuery("#tr_built_in_watermark_font").css('display', 'none');
  jQuery("#tr_built_in_watermark_color").css('display', 'none');
  jQuery("#tr_built_in_watermark_position").css('display', 'none');
  jQuery("#tr_built_in_watermark_preview").css('display', 'none');
  jQuery("#preview_built_in_watermark").css('display', 'none');
  switch (watermark_type) {
    case 'watermark_type_text':
    {
      jQuery("#tr_built_in_watermark_opacity").css('display', '');
      jQuery("#tr_built_in_watermark_text").css('display', '');
      jQuery("#tr_built_in_watermark_font_size").css('display', '');
      jQuery("#tr_built_in_watermark_font").css('display', '');
      jQuery("#tr_built_in_watermark_color").css('display', '');
      jQuery("#tr_built_in_watermark_position").css('display', '');
      jQuery("#tr_built_in_watermark_preview").css('display', '');
      jQuery("#preview_built_in_watermark").css('display', 'table-cell');
      break;
    }
    case 'watermark_type_image':
    {
      jQuery("#tr_built_in_watermark_url").css('display', '');
      jQuery("#tr_built_in_watermark_size").css('display', '');
      jQuery("#tr_built_in_watermark_position").css('display', '');
      jQuery("#tr_built_in_watermark_preview").css('display', '');
      jQuery("#preview_built_in_watermark").css('display', 'table-cell');
      break;
    }
  }
}

function bwg_change_option_type(type) {
  type = (type == '' ? 1 : type);
  document.getElementById('type').value = type;
  for (var i = 1; i <= 8; i++) {
    if (i == type) {
      document.getElementById('div_content_' + i).style.display = 'block';
      document.getElementById('div_' + i).style.background = '#C5C5C5';
    }
    else {
      document.getElementById('div_content_' + i).style.display = 'none';
      document.getElementById('div_' + i).style.background = '#F4F4F4';
    }
  }
  document.getElementById('display_panel').style.display = 'inline-block';
}

function bwg_inputs() {
  jQuery(".spider_int_input").keypress(function (event) {
    var chCode1 = event.which || event.paramlist_keyCode;
    if (chCode1 > 31 && (chCode1 < 48 || chCode1 > 57) && (chCode1 != 46) && (chCode1 != 45)) {
      return false;
    }
    return true;
  });
}

function bwg_enable_disable(display, id, current) {
  jQuery("#" + current).attr('checked', 'checked');
  jQuery("#" + id).css('display', display);
}

function spider_check_isnum(e) {
  var chCode1 = e.which || e.paramlist_keyCode;
  if (chCode1 > 31 && (chCode1 < 48 || chCode1 > 57) && (chCode1 != 46) && (chCode1 != 45)) {
    return false;
  }
  return true;
}

function bwg_change_theme_type(type) {
  var button_name = jQuery("#button_name").val();
  jQuery("#Thumbnail").hide();
  jQuery("#Masonry").hide();
  jQuery("#Mosaic").hide();
  jQuery("#Slideshow").hide();
  jQuery("#Compact_album").hide();
  jQuery("#Extended_album").hide();
  jQuery("#Masonry_album").hide();
  jQuery("#Image_browser").hide();
  jQuery("#Blog_style").hide();
  jQuery("#Lightbox").hide();
  jQuery("#Navigation").hide();
  jQuery("#" + type).show();
  jQuery("#type_menu").show();
  jQuery(".spider_fieldset").show();
  jQuery("#current_type").val(type);

  jQuery("#type_Thumbnail").attr("style", "background-color: #F4F4F4;");
  jQuery("#type_Masonry").attr("style", "background-color: #F4F4F4;");
  jQuery("#type_Mosaic").attr("style", "background-color: #F4F4F4;");
  jQuery("#type_Slideshow").attr("style", "background-color: #F4F4F4;");
  jQuery("#type_Compact_album").attr("style", "background-color: #F4F4F4;");
  jQuery("#type_Extended_album").attr("style", "background-color: #F4F4F4;");
  jQuery("#type_Masonry_album").attr("style", "background-color: #F4F4F4;");
  jQuery("#type_Image_browser").attr("style", "background-color: #F4F4F4;");
  jQuery("#type_Blog_style").attr("style", "background-color: #F4F4F4;");
  jQuery("#type_Lightbox").attr("style", "background-color: #F4F4F4;");
  jQuery("#type_Navigation").attr("style", "background-color: #F4F4F4;");
  jQuery("#type_" + type).attr("style", "background-color: #CFCBCB;");
}

// Check all items.
function spider_check_all_items() {
  spider_check_all_items_checkbox();
  // if (!jQuery('#check_all').attr('checked')) {
    jQuery('#check_all').trigger('click');
  // }
}
function spider_check_all_items_checkbox() {

  if (jQuery('#check_all_items').attr('checked')) {
    jQuery('#check_all_items').attr('checked', false);
    jQuery('#draganddrop').hide();
  }
  else {
    var saved_items = (parseInt(jQuery(".displaying-num").html()) ? parseInt(jQuery(".displaying-num").html()) : 0);
    var added_items = (jQuery('input[id^="check_pr_"]').length ? parseInt(jQuery('input[id^="check_pr_"]').length) : 0);
    var items_count = added_items + saved_items;
    jQuery('#check_all_items').attr('checked', true);
    if (items_count) {
      jQuery('#draganddrop').html("<strong><p>Selected " + items_count + " item" + (items_count > 1 ? "s" : "") + ".</p></strong>");
      jQuery('#draganddrop').show();
    }
  }
}

function spider_check_all(current) {
  if (!jQuery(current).attr('checked')) {
    jQuery('#check_all_items').attr('checked', false);
    jQuery('#draganddrop').hide();
  }
}

function bwg_get_video_host(url) {  
  if ((/youtu\.be/i).test(url) || (/youtube\.com\/watch/i).test(url)) {
    return 'YOUTUBE';
  }
  if ((/vimeo\.com/i).test(url)) {
    return 'VIMEO';
  }
  alert('Enter only YouTube or Vimeo url.');
  return '';
}

function bwg_get_youtube_video_id(url) {
  pattern = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
  var matches = url.match(pattern);
  if (matches && (matches[7]).length == 11) {
    return matches[7];
  }   
  return '';
}

function bwg_get_vimeo_video_id(url) {
  pattern = /\/\/(www\.)?vimeo.com\/(\d+)($|\/)/;
  var matches = url.match(pattern);
  if (matches) {
    return matches[2];
  }
  return '';
}

function bwg_get_video_info(input_id) {



  var url = encodeURI(jQuery("#" + input_id).val());  
  if (!url) {
    alert('Please enter url to embed.');
    return '';
  }
   jQuery('#opacity_div').show();
  jQuery('#loading_div').show(); 
 host= bwg_get_video_host(url);
  
  var filesValid = [];
  var data = {
    'task': 'addEmbed',
    'URL_to_embed': url,
    'host': host,
    'async':true
  };


   // get from the server data for the url. Here we use the server as a proxy, since Cross-Origin Resource Sharing AJAX is forbidden.
  jQuery.post(ajax_url, data, function(response) {
    
    
    if(response == false){

      alert('Error: cannot get response from the server.');
      jQuery('#opacity_div').hide();
      jQuery('#loading_div').hide();
      
      return '';
    }
    else{  

      var index_start = response.indexOf("WD_delimiter_start");
      var index_end = response.indexOf("WD_delimiter_end");
      if(index_start == -1 || index_end == -1){
        alert('Error: something wrong happened at the server.');
        jQuery('#opacity_div').hide();
        jQuery('#loading_div').hide();
      
        return '';
      }

      /*filter out other echoed characters*/
      /*18 is the length of "wd_delimiter_start"*/
      response = response.substring(index_start+18,index_end);  
      

      response_JSON = jQuery.parseJSON(response);
        /*if indexed array, it means there is error*/
      if(typeof response_JSON[0] !== 'undefined'){
        alert('Error: ' + jQuery.parseJSON(response)[1]);
        jQuery('#opacity_div').hide();
        jQuery('#loading_div').hide();
        return '';
      }
      else{
        fileData = response_JSON;
        filesValid.push(fileData);
        bwg_add_image(filesValid);
        document.getElementById(input_id).value = '';
        jQuery('#opacity_div').hide();
        jQuery('#loading_div').hide();
        return 'ok';
      }
    }
    return ''; 
   
  });
  return 'ok'; 
   
}




/*function bwg_get_video_info(input_id) {
  var url = jQuery("#" + input_id).val();  
  if (!url) {
    alert('Please enter video url to add.');
    return '';
  }
  var host = bwg_get_video_host(url);
  var filesValid = [];
  var fileData = [];
  switch (host) {
    case 'YOUTUBE':
      var video_id = bwg_get_youtube_video_id(url);
      if (video_id) {
        jQuery.getJSON('http://gdata.youtube.com/feeds/api/videos/'+video_id+'?v=2&alt=jsonc',function(data,status,xhr){
            fileData['name'] = data.data.title;
            fileData['description'] = data.data.description;
            fileData['filename'] = video_id;
            fileData['url'] = url;
            fileData['reliative_url'] = url;
            fileData['thumb_url'] = data.data.thumbnail.hqDefault;
            fileData['thumb'] = data.data.thumbnail.hqDefault;
            fileData['size'] = bwg_convert_seconds(data.data.duration);
            fileData['filetype'] = 'YOUTUBE';
            fileData['date_modified'] = bwg_convert_date(data.data.uploaded, 'T');
            fileData['resolution'] = '';
            fileData['redirect_url'] = '';
            filesValid.push(fileData);
            bwg_add_image(filesValid);
            document.getElementById(input_id).value = '';
        });
        return 'ok';
      }
      else {
        alert('Please enter a valid YouTube link.');
        return '';
      }
    case 'VIMEO':
      var video_id = bwg_get_vimeo_video_id(url);
      if (video_id) {
        jQuery.getJSON('http://vimeo.com/api/v2/video/'+video_id+'.json',function(data,status,xhr){
            fileData['name'] = data[0].title;
            fileData['description'] = data[0].description;
            fileData['filename'] = video_id;
            fileData['url'] = url;
            fileData['reliative_url'] = url;
            fileData['thumb_url'] = data[0].thumbnail_large;
            fileData['thumb'] = data[0].thumbnail_large;
            fileData['size'] = bwg_convert_seconds(data[0].duration);
            fileData['filetype'] = 'VIMEO';
            fileData['date_modified'] = bwg_convert_date(data[0].upload_date, ' ');
            fileData['resolution'] = '';
            fileData['redirect_url'] = '';
            filesValid.push(fileData);
            bwg_add_image(filesValid);
            document.getElementById(input_id).value = '';
        });
        return 'ok';
      }
      else {
        alert('Please enter a valid Vimeo link.');
        return '';
      }
    default:
      return '';
  }  
}*/

function bwg_convert_seconds(seconds) {
  var sec_num = parseInt(seconds, 10);
  var hours   = Math.floor(sec_num / 3600);
  var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
  var seconds = sec_num - (hours * 3600) - (minutes * 60);

  if (minutes < 10 && hours != 0) {minutes = "0" + minutes;}
  if (seconds < 10) {seconds = "0" + seconds;}
  var time    = (hours != 0 ? hours + ':' : '') + minutes + ':' + seconds;
  return time;
}

function bwg_convert_date(date, separator) {
  var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
  date = date.split(separator);
  var dateArray = date[0].split("-");
  return dateArray[2] + " " + m_names[dateArray[1] - 1] + " " + dateArray[0] + ", " + date[1].substring(0, 5);
}

function bwg_check_checkboxes() { 

  var flag = false;

  var ids_string = jQuery("#ids_string").val();

  ids_array = ids_string.split(",");

  for (var i in ids_array) {

    if (ids_array.hasOwnProperty(i) && ids_array[i]) {

      if (jQuery("#check_" + ids_array[i])[0].checked == true) {

        flag = true;

      }

	}

  }

  if(flag) {

    if(jQuery(".buttons_div_right").find("a").hasClass( "modal" )) {       

      return true; 
	  jQuery('#no_selection').remove()	 
jQuery('#sbox-content').css('display','');

	}

	else { 

	  jQuery(".buttons_div_right").find("a").addClass( "modal" );

	  jQuery('#draganddrop').hide();
jQuery('#no_selection').remove()	  
jQuery('#sbox-content').css('display','');
	  return true;

	}

  } 

  else { 



	jQuery(".buttons_div_right").find("a").removeClass( "modal" );

    jQuery('#draganddrop').html("<strong><p>You must select at least one item.</p></strong>");

    jQuery('#draganddrop').show();
jQuery('#no_selection').remove();	
jQuery("#sbox-window").append("<div id='no_selection' style='text-align: center;'><strong style='font-size:22px'>You must select at least one item.</strong></div>");
jQuery('#sbox-content').css('display','none');
	
	return false;

  }  

}

function bwg_popup_fullscreen(num) {
  if (num) {
    jQuery("#tr_popup_dimensions").css('display', 'none');
  }
  else {
    jQuery("#tr_popup_dimensions").css('display', '');
  }
}