//Javascript Helper functions

//Initialises the Quickbar
$(document).ready(function(){
	//Hide quickbar content
	$('#quickbar-bg').remove();
	$('#quickbar-container').prepend('<img id="quickbar-bg" src="/qsPortal/Images/spacer.gif" width="100%" height="100%" alt="" />');
	$('#quickbar-layout').hide();
	$('#quickbar-container').hover(function(){$('#quickbar-bg').remove();$('#quickbar-layout').show();}, 
								   function(){$('#quickbar-layout').hide();
											  $('#quickbar-container').prepend('<img id="quickbar-bg" src="/qsPortal/Images/spacer.gif" width="100%" height="100%" alt="" />');
								   });
	$('#quickbar-right').click(function(){$('#quickbar-container').hide();});
});

function Navigate(pnavId, pformId) {
	//Hide all forms
	$('.formContent').hide();
	//Show the right one
	$(pformId).show(); 
	//Hide any current icon
	$('#main-nav li').removeClass("current"); 
	//Hilight the current one
	$(pnavId).addClass("current"); 	
	return false;
}

//Hook the printme event handler
//RSC 16.05.2008 v425d+ : Use same source url and append Print=1 to the querystring
$(document).ready(function(){
	$('#printme').click(function(){
		var strUrl = document.location;
		//Append Print=1
		if (document.location.search != '')
			strUrl += '&'
		else
			strUrl += '?'
		strUrl += 'Print=1';
		window.open(strUrl,'PrintLayout','Toolbar=1,Location=0,Directories=0,Status=0,Menubar=1,Scrollbars=1,Resizable=1,WIDTH=730,HEIGHT=580');
		return false;
	});
});

//DPH Zoom
$(document).ready(function(){
	//Searches for an element with class zoom_xxx where xxx is the name of the dph element
	$('.dph_zoom').each(function(){
		//Get the DPH value
		var strDPH = $(this).attr('dph');
		var strFormatNode = $(this).attr('formatnode');
		if (strFormatNode != '') {
			strFormatNode = '&formatnode=' + strFormatNode
		}
		var strTitle = $(this).attr('title');
		if (strTitle != '') {
			strTitle = '&strTitle=' + escape(strTitle);
		}
		//Wrap the content in a link
		$(this).wrap('<a href="/qsPortal/Home.asp?Action=-6&N=' + gobjSessionVar.lngCurrentNodeFK + '&mode=dph&dph=' + strDPH + strFormatNode + strTitle + '"></a>');
	});
});

//Helpers to create a link to other objects from within the Properties popup
$(document).ready(function() {
	CreateJumptoLink();
});

//Function to create a link to other objects from within the Properties popup
function CreateJumptoLink() {
	
	if (typeof(gobjSessionVar) == 'undefined')
		return;
		
	//only if in edit mode
	if (!gobjSessionVar.EditMode) return;

	$('.jumpto').each(function() {
		var nodeid = -1;
		var linksrc = $(this).attr('linksrc');
		if (linksrc != '') {
			nodeid = $('#' + linksrc).val();
			$('#' + linksrc).unbind('change');
			$('#' + linksrc).change(function() { return CreateJumptoLink() });
		}
		if (nodeid != -1 && nodeid != 0 && nodeid != '') {
			$(this).attr('href', '/qsPortal/QuickEdit.asp?N=' + nodeid + '&EditType=1');
			$(this).click(function() {
				Navigate('#navGeneral', '#formGeneral');
				$('#Save').click();
				return true;
			});
			$(this).show();
		}
		else {
			$(this).hide();
		}
	})
};

//Helper for the SystemEditFrame mouse over function (removed from the code)
$(document).ready(function() {
	$('.SystemEditFrame').each(function() {
		$(this).hover(
			function() {
				$(this).addClass('SystemEditFrameHover');
				$(this).removeClass('SystemEditFrame');
				if ($(this).hasClass('Section')) {
					//divEdit_xxx
					var sectionid = $(this).attr('id').substring(8);
					$('#divSection_' + sectionid).addClass('SystemNodeTypeSectionFrameHover');
					$('#divSection_' + sectionid).removeClass('SystemNodeTypeSectionFrame');
				}
			},
			function() {
				$(this).removeClass('SystemEditFrameHover');
				$(this).addClass('SystemEditFrame');
				if ($(this).hasClass('Section')) {
					//divEdit_xxx
					var sectionid = $(this).attr('id').substring(8);
					$('#divSection_' + sectionid).removeClass('SystemNodeTypeSectionFrameHover');
					$('#divSection_' + sectionid).addClass('SystemNodeTypeSectionFrame');
				}
			});
	});
});


//Select Replacement code
    function selectReplacement(obj) {
      obj.className += ' replaced';
      var ul = document.createElement('ul');
      ul.className = 'selectReplacement';
      var opts = obj.options;
      var selectedOpt = (!obj.selectedIndex) ? 0 : obj.selectedIndex;
      for (var i=0; i<opts.length; i++) {
        var li = document.createElement('li');
        var txt = document.createTextNode(opts[i].text);
        li.appendChild(txt);
        li.selIndex = i;
        li.selectID = obj.id;
        li.onclick = function() {
          selectMe(this);
        };
        if (i == selectedOpt) {
          li.className = 'selected';
          li.onclick = function() {
            this.parentNode.className += ' selectOpen';
            this.onclick = function() {
              selectMe(this);
            };
          };
        }
        if (window.attachEvent) {
          li.onmouseover = function() {
            this.className += ' hover';
          };
          li.onmouseout = function() {
            this.className = 
              this.className.replace(new RegExp(" hover\\b"), '');
          };
        }
        ul.appendChild(li);
      }
      obj.onfocus = function() {
        ul.className += ' selectFocused';
      };
      obj.onblur = function() {
        ul.className = 'selectReplacement';
      };
      obj.onchange = function() {
        var idx = this.selectedIndex;
        selectMe(ul.childNodes[idx]);
      };
      obj.onkeypress = obj.onchange;
      obj.parentNode.insertBefore(ul,obj);
    }
    function selectMe(obj) {
      var lis = obj.parentNode.getElementsByTagName('li');
      for (var i=0; i<lis.length; i++) {
        if (lis[i] != obj) {
          lis[i].className='';
          lis[i].onclick = function() {
            selectMe(this);
          };
       } else {
          setVal(obj.selectID, obj.selIndex);
          obj.className='selected';
          obj.parentNode.className = 
            obj.parentNode.className.replace(new RegExp(" selectOpen\\b"), '');
          obj.onclick = function() {
            obj.parentNode.className += ' selectOpen';
            this.onclick = function() {
              selectMe(this);
            };
          };
        }
      }
    }
    function setVal(objID,val) {
      var obj = document.getElementById(objID);
      obj.selectedIndex = val;
    }

