function cms_previewPage(thisPage, thisTagID, thisSequenceID, thisContent) {
	var previewPage = thisPage
	previewPage += "?cms_preview=y";
	// tiny_mce changes the textarea name, so can't do a 'live' (unsaved) preview. leaving code in in case I manage to find out what tiny_mce changes the textarea.name to
	//previewPage += "&preview_tag_id=" + thisTagID;
	//previewPage += "&preview_sequence_id=" + thisSequenceID;
	//previewPage += "&preview_content=" + thisContent;
	//previewPage += "&z=1";
	var newWindow = window.open(previewPage, "_blank");
}

function cms_editImage(thisTagType, thisTagID, thisSequenceID) {
	var urlDivObj = document.getElementById("cms_url_" + thisTagType + "_" + thisTagID + "_" + thisSequenceID);
	urlDivObj.style.display = "block";
}

function cms_uploadImage(thisPageID, thisTagType, thisTagID, thisLanguageCode, thisSequenceID) {
	var thisFormObj = document.forms["cms_form_" + thisTagType + "_" + thisTagID + "_" + thisSequenceID];
	thisFormObj.enctype = "multipart/form-data";
	thisFormObj.action += "?cms_update=uploadSingleImage";
	thisFormObj.action += "&edit_mode=y";
/*
	thisFormObj.action += "&cms_page_id=" + thisPageID;
	thisFormObj.action += "&cms_tag_type=" + thisTagType;
	thisFormObj.action += "&cms_tag_id=" + thisTagID;
	thisFormObj.action += "&cms_language_code=" + thisLanguageCode;
	thisFormObj.action += "&cms_sequence_id=" + thisSequenceID;
*/
}




function cms_openLibrary(pageID, objectID, objectType, tagType, tagID, sequenceID, languageCode) {

	var newWindow = window.open('/admin/cms_library.asp?page_id=' + pageID + '&object_id=' + objectID + '&object_type=' + objectType + '&tag_type=' + tagType + '&tag_id=' + tagID + '&sequence_id=' + sequenceID + '&category_id=' + tagID + "&language_code=" + languageCode, 'cms_object_popup', 'width=390, height=640, scrollbars=1, resizable=1, status=1, left=100, top=100');
	newWindow.focus();
}
function cms_editLibrary(objectType) {
	var newWindow = window.open('/admin/cms_library.asp?object_type=' + objectType, 'cms_objectlibrary_popup', 'width=390, height=640, scrollbars=1, resizable=1, status=1, left=100, top=100');
	newWindow.focus();
}

function cms_editObject(pageID, objectType, tagType, tagID) {
	var newWindow = window.open('/admin/cms_editobject.asp?page_id=' + pageID + '&object_type=' + objectType + '&tag_type=' + tagType + '&tag_id=' + tagID + '&category_id=' + tagID, 'cms_objectlibrary_popup', 'width=390, height=640, scrollbars=1, resizable=1, status=1, left=100, top=100');
	newWindow.focus();
}

function cms_orderContent(pageID, tagType, tagID, languageCode) {
	var newWindow = window.open('/admin/cms_ordercontent.asp?page_id=' + pageID + '&tag_type=' + tagType + '&tag_id=' + tagID + '&language_code=' + languageCode, 'cms_objectlibrary_popup', 'width=390, height=640, scrollbars=1, resizable=1, status=1, left=100, top=100');
	newWindow.focus();
}


function launchObject(objectURL, objectType, targetLocation, targetWidth, targetHeight) {
	if (objectType == "pdf") {
		var newWindow = window.open(objectURL, "cms_pdfWindow", "width=" + targetWidth + ", height=" + targetHeight + ", resizable=1, scrollbars=1, status=1");
	} else if (objectType = "video") {
		var s1 = new SWFObject("/common/flvplayer.swf", "single", targetWidth, targetHeight, "7");
		s1.addParam("allowfullscreen", "true");
		s1.addVariable("file", objectURL);
		s1.addVariable("image", "images/preview.jpg");
		s1.addVariable("width", targetWidth);
		s1.addVariable("height", targetHeight);
		s1.write(targetLocation);
	}	
}












// Floating Div adapted from http://www.javascript-fx.com
var ns = (navigator.appName.indexOf("Netscape") != -1);
var d = document;
var menuID;
function JSFX_FloatDiv(id, sx, sy) {
	menuID = id;
	var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
	var px = document.layers ? "" : "px";
	window[id + "_obj"] = el;
	if(d.layers)el.style=el;
	el.cx = el.sx = sx;
	el.cy = el.sy = sy;
	el.sP=function(x,y){
		//this.style.left=x+px;
		this.style.top=y+px;
	};

	el.floatIt=function()
	{
		var pX, pY;
		pX = (this.sx >= 0) ? 0 : ns ? innerWidth : 
		document.documentElement && document.documentElement.clientWidth ? 
		document.documentElement.clientWidth : document.body.clientWidth;
		pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? 
		document.documentElement.scrollTop : document.body.scrollTop;
		if(this.sy<0) 
		pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ? 
		document.documentElement.clientHeight : document.body.clientHeight;
		this.cx += (pX + this.sx - this.cx)/8;this.cy += (pY + this.sy - this.cy)/8;
		this.sP(this.cx, this.cy);
		setTimeout(this.id + "_obj.floatIt()", 40);
	}
	return el;
}

// http://www.webreference.com/programming/javascript/mk/column2/index.html
// drag
document.onmousemove = mouseMove;

function mouseMove(ev){
	ev           = ev || window.event;
	var mousePos = mouseCoords(ev);
}

function mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}


document.onmousemove = mouseMove;
document.onmouseup   = mouseUp;

var dragObject  = null;
var mouseOffset = null;

function getMouseOffset(target, ev){
	ev = ev || window.event;

	var docPos    = getPosition(target);
	var mousePos  = mouseCoords(ev);
	return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}

function getPosition(e){
	var left = 0;
	var top  = 0;

	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}

	left += e.offsetLeft;
	top  += e.offsetTop;

	return {x:left, y:top};
}

function mouseMove(ev){
	ev           = ev || window.event;
	var mousePos = mouseCoords(ev);

	if(dragObject){
		menuObject = document.getElementById(menuID);
		menuLeftPos = mousePos.x - mouseOffset.x;
		menuObject.style.left = menuLeftPos + "px";
		// top not set as controlled by the float control
		//menuTopPos = mousePos.y - mouseOffset.y;
		//menuObject.style.top = menuTopPos + "px";
		setCookie("menuStartLeftPos", menuLeftPos)
		//setCookie("menuStartTopPos", menuTopPos)
		return false;
	}
}
function mouseUp(){
	dragObject = null;
}

function makeDraggable(item){
	if(!item) return;
	item.onmousedown = function(ev){
		dragObject  = this;
		mouseOffset = getMouseOffset(this, ev);
		return false;
	}
}

// cookie stuff

/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

var tiny_fieldName, tiny_url, tiny_browserType, tiny_srcWindow
function tinyFileBrowser(fieldName, url, browserType, srcWindow) {
	if (browserType == "file") {
		return false;
	} else {
		tiny_fieldName = fieldName;
		tiny_url = url;
		tiny_browserType = browserType;
		tiny_srcWindow = srcWindow;
		var imageBrowser = window.open("/admin/cms_photolib_popup.asp", "_blank", "width=800,height=600,resizable=1, scrollbars=1");
		//srcWindow.document.forms[0].elements[fieldName].value = "123"
	}
}

function tinyFileBrowserReturn(thisSrc, thisAlt, thisWidth, thisHeight) {
	tiny_srcWindow.document.forms[0].elements[tiny_fieldName].value = thisSrc;
	if (tiny_srcWindow.document.forms[0].elements["alt"] && thisAlt != null) {
		tiny_srcWindow.document.forms[0].elements["alt"].value = thisAlt;
	}
	if (tiny_srcWindow.document.forms[0].elements["title"] && thisAlt != null) {
		tiny_srcWindow.document.forms[0].elements["title"].value = thisAlt;
	}
	if (tiny_srcWindow.document.forms[0].elements["width"] && thisWidth != null) {
		tiny_srcWindow.document.forms[0].elements["width"].value = thisWidth;
	}
	if (tiny_srcWindow.document.forms[0].elements["height"] && thisHeight != null) {
		tiny_srcWindow.document.forms[0].elements["height"].value = thisHeight;
	}
}
