function tinyMceImagePhotosBrowser(field_name, url, type, win) {
  var cmsURL = "/image_manager/";
    
  tinyMCE.activeEditor.windowManager.open({
    file : cmsURL, 
    title : "File Browser",
    width : 800, 
    height : 600, 
    resizable : "yes",
    scrollbars : "yes",
    close_previous : "no"
  }, {
    window : win, 
    input : field_name
  });
  return false;
}

function openImageManager(field) {
  newwindow=window.open("/image_manager/?return_value_to=" + field,'images_browser','height=600,width=800,resizable=yes,scrollbars=yes');
  if (window.focus) {newwindow.focus()}
}

String.prototype.endsWith = function(str)
{return (this.match(str+"$")==str)}

var FileBrowserDialogue = {
  init : function () {
    // custom onLoad things.
  },
  submitContentImageFileBrowser : function (image_url) {
    var URL = image_url;
    var win = tinyMCEPopup.getWindowArg("window");
    
    if (URL.endsWith(".swf")) {
      URL = URL.replace(/http:\/\/[^\/]+/i, "");
    }
    
    win.document.getElementById(tinyMCEPopup.getWindowArg("input")).value = URL;
    
    try {
      if (win.ImageDialog.getImageData) win.ImageDialog.getImageData();
      if (win.ImageDialog.showPreviewImage) win.ImageDialog.showPreviewImage(URL);
    } catch (Error) {
    }
    
    tinyMCEPopup.close();
  }
}

function set_value(id, val) {
  var div = document.getElementById(id);
  if (div) {div.value = val;}
}

function gup(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function show_only_div(divname, select_box) {
  var divs = 0;
  while (div = document.getElementById(divname + "_" + divs)) {
    div.style.display = "none";
    divs = divs + 1;
  }
  div_id = select_box[select_box.selectedIndex].value;
  div = document.getElementById(divname + "_" + div_id);
  if (div) {
    div.style.display = "block";
  }
}

function toggle_div(div_id) {
  var div = document.getElementById(div_id);
  if (div.style.display == 'none' || div.style.display == "") {
    div.style.display = 'block';
  } else {
    div.style.display = 'none';
  }
}

function submit_form(f_id) {
  document.getElementById(f_id).submit();
}

function change_gallery_image(iid, images, time_ms) {
  image = document.getElementById(iid);
  var randValue = Math.floor(Math.random() * images.length);
  if (image) {
    image.src = images[randValue][0];
  }
  text_b = document.getElementById(iid + "_text");
  if (text_b) {
    text_b.innerHTML = images[randValue][1];
  }
  
  window.setTimeout(function() {change_gallery_image(iid, images, time_ms)}, time_ms);
}

var next_image_of_gallery = [];
function change_gallery_image_next(iid, images, time_ms) {
  if (!next_image_of_gallery[iid]) {
    next_image_of_gallery[iid] = 0;
  }
  var randValue = next_image_of_gallery[iid];
  
  image = document.getElementById(iid);
  if (image) {
    image.src = images[randValue][0];
  }
  text_b = document.getElementById(iid + "_text");
  if (text_b) {
    text_b.innerHTML = images[randValue][1];
  }
  
  next_image_of_gallery[iid] = next_image_of_gallery[iid] + 1;
  
  if (images.length <= next_image_of_gallery[iid]) {
    next_image_of_gallery[iid] = 0;
  }
  
  window.setTimeout(function() {change_gallery_image_next(iid, images, time_ms)}, time_ms);
}