/*--------------------------------------------------------------------------------------------------*/
/*                                                  DreamWeaver JS                                                  */
/*--------------------------------------------------------------------------------------------------*/
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
// ------------------------------------------------



// ************************************************
// *
// *	attach top menu handler

function menuHandler_wSubMenu(){
	$('#menu > ul img')
		.unbind('mouseover')
		.bind('mouseover', function(){
			// show over state
			
			// show submenu
			$offset = $(this).offset();
			$submenu = $(this).parents('li').find('.subMenu');
			$submenu
				.clone()
				.appendTo('body')
				.addClass('active')
				.css({top:$offset.top, left:$offset.left})
				.show();
			return false;	
			
			
		})
		.unbind('mouseout')
		.bind('mouseout', function(){
			// remove over state
			
			// hide submenu
			$('body > ul.subMenu').remove();
		})
}

function menuHandler(){
	$('.jsmenu')
		.unbind('mouseover')
		.bind('mouseover', function(){
			var origimg = $(this).find('img').attr('src'); 
			var	newimg = origimg.replace('_off', '_on');
			$(this)
				.unbind('mouseout')
				.bind('mouseout', function(){ 
					$(this).find('img').attr('src', origimg) 
				})
				.find('img')
				.attr('src', newimg);
		});
}
// ------------------------------------------------


// function to load the calendar window.
function ShowCalendar(FormName, FieldName) {
  window.open("admin/datepopup/window_datepopup.cfm?FormName=" + FormName + "&FieldName=" + FieldName, "CalendarWindow", "width=246,height=215");
}


// ************************************************
// *
// *	handler for news and events tabs

function news_events_tabs(){
	$('#news_events_tabs li')
		.unbind('click')
		.bind('click', function(){
			// handle tabs
				$(this).siblings().removeClass('selected');
				$(this).addClass('selected');
				var rel = $(this).attr('rel');
				
				
			
			// update content
				$('#news_and_events > div').hide(); // hide all children
				$('#news_and_events #'+rel).show(); // show related child
				
			
			return false;
		});
	
}
// ------------------------------------------------





// ************************************************
// *
// *	template map handler with hooks into attr parts

function template_map(){
	$('#partsMap div').bind('mouseover', function(){ 
		$(this).addClass('hover');
		var attr = this.id.replace('map','attr');
		$('#'+attr).addClass('hover');
		
	});
	$('#partsMap div').bind('mouseout', function(){
		$(this).removeClass('hover');
		var attr = this.id.replace('map','attr');
		$('#'+attr).removeClass('hover');
		
	});
	
	$('#partsMap div').bind('click', function(){
		dbug.log('edit area');
	});
}
// ------------------------------------------------



// ************************************************
// *
// *	partial selection for admin section > pages

function partial_list(){
	$('.partials li.selected')
		.prepend('<div class="end"></div>')
		.unbind('mousedown')
		.bind('mousedown', function(){
			var subMenu = $(this).parents('ul');
			dbug.log(subMenu);
			dbug.log($(this).parents('.value'));
			var cMenu = subMenu.clone().prependTo( $(this).parents('.value') );
			cMenu
				.addClass('menufy')
				.find('li.selected').removeClass('selected').addClass('hover').end()
				.find('li').show()
				
			$(document).bind('mousedown', function(e){checkMouse(e, $('.menufy'), closeSubMenu)});
			return false;
		})
		.find('a').bind('click', function(){ return false });
}

function initSubMenu(){
	var self = this;
	$('#related'+modName+'Container .options li.selected')
		.prepend('<div class="end"></div>')
		.unbind('mousedown')
		.bind('mousedown', function(){
			// show the submenu
			var subMenu = $(this).parents('ul');
			var cMenu = subMenu.clone().prependTo( $('#toolContent') );
			cMenu
				.addClass('menufy')
				.find('li.selected').removeClass('selected').addClass('hover').end()
				.find('li').show()
				.unbind('mouseover').bind('mouseover', function(){ $(this).siblings().removeClass('hover').end().addClass('hover') })
				.unbind('mouseout').bind('mouseout', function(){ $(this).removeClass('hover') })
				.unbind('mouseup').bind('mouseup', function(){ 
					self.subMenuSelection(this, subMenu, cMenu, modName); //params: clicked elem, orig menu elem, cloned menu elem, mod
				})
				.unbind('mousedown').bind('mousedown', function(){ 
					self.subMenuSelection(this, subMenu, cMenu, modName); //params: clicked elem, orig menu elem, cloned menu elem, mod
				});
			
			$(document).bind('mousedown', function(e){checkMouse(e, $('.menufy'), IAD.ideaEngine.closeSubMenu)});
			return false;
		}) 
		.find('a').bind('click', function(){ return false });
	return false;
}

function subMenuSelection(el, subMenu, cMenu, modName){
	var self = this;
	$(el).removeClass('hover');
	var pos = cMenu.find('li').index(el);
	$(subMenu.find('li')).removeClass('selected').find('.end').remove();
	$(subMenu.find('li').get(pos)).addClass('selected');
	cMenu.remove();  // remove the temp menu
	
	self.getModule( modName );  // reinitialize the submenu
	self.initSubMenu( modName );  // reinitialize the submenu
	$().unbind('mousedown');
	
	return false;
}

function closeSubMenu(){
	var tMenu = $( $('.pgAttr .menufy') ).remove();
}


function checkMouse(e, elem, callback){
	var el = e.target;
	var elem = elem;
	while (true){
		if (el==elem) {
			return true;
		} else if (el == document) {
			callback(elem);
			$(document).unbind('mousedown');
			return false;
		} else {
			el = $(el).parent()[0];
		}
	}
}
// ------------------------------------------------




// ************************************************
// *
// *	get dbug.log functionality ready

dbug = {
	firebug: false, debug: false, log: function(msg) {},
	enable: function() { if(this.firebug) this.debug = true; dbug.log = console.debug; dbug.log('enabling dbug');	},
	disable: function(){ if(this.firebug) this.debug = false; dbug.log = function(){}; }
}
if (typeof console != "undefined") { // safari, firebug
	if (typeof console.debug != "undefined") { // firebug
		dbug.firebug = true; if(window.location.href.indexOf("debug=true")>0) dbug.enable();
	}
}
// ------------------------------------------------



// ************************************************
// *
// *	open a new window
function openWin(url,win,w,h,mb,rs,scroll){
	var rWin = window.open(''+url,win,'left=20,top=20,width='+w+',height='+h+',menubar='+mb+',resizable='+rs+',scrollbars='+scroll+',toolbar=0,location=1');
}

// ------------------------------------------------
	

// ************************************************
// *
// * TinyMCE handlers
function test(el){
	dbug.log('-- in test ------------------');
	
	el.unbind('click');
	var sc = jQuery( jQuery(el).find('.c').children()[0] );
	dbug.log(sc);
	
	preString = '<form id="frmcontento" name="frmcontento" method="post" action="test.cfm" onsubmit=""><div id="contento" name="test">';
	postString = '</div><input type="submit" value="submit" /></form>';
	//jQuery(el).find('.c').children().wrap(preString + postString);
	
	sc.wrap('<form id="frmcontento" name="frmcontento" method="post" action="test.cfm" onsubmit=""><textarea id="contento" name="test"></textarea><input type="submit" value="submit" /></form>');
	
	sc.before(sc.parent().html());
	
	dbug.log('sc');
	dbug.log(sc.css('display'));
	dbug.log(sc);
	dbug.log('-- end test ------------------');
	//tinyMCE.init({mode : "textareas",theme : "advanced"});
	
	//tinyMCE.addMCEControl(document.getElementById('contento'), 'contento');
}

function ajaxLoad(edid) {
	var ed = tinyMCE.get(edid);

	// Do your ajax call here, window.setTimeout fakes ajax call
	ed.setProgressState(1); // Show progress
	window.setTimeout(function() {
		ed.setProgressState(0); // Hide progress
		ed.setContent('HTML content that got passed from server.');
	}, 3000);
}

function ajaxSave(edid) {
	var ed = tinyMCE.get(edid);

	// Do you ajax call here, window.setTimeout fakes ajax call
	ed.setProgressState(1); // Show progress
	window.setTimeout(function() {
		ed.setProgressState(0); // Hide progress
		alert(ed.getContent());
	}, 3000);
}

// ------------------------------------------------