﻿//Common calls for - changing hovers, accordians and drop downs

function setIEversion() {
  if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
   ISIE = true;
   var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
   if (ieversion>=8)
    ISIE8 = true;
   else if (ieversion>=7)
    ISIE7 = true;
   else if (ieversion>=6)
    ISIE6 = true;
   else if (ieversion>=5)
    ISIE5 = true;
  }
}

function doCustomResize() {

      //constants
      
      if ($(".ui-layout-north").css('display') == 'none') {
        isToggled = true;
      }
      else {
        isToggled = false;
      }

      var heightTwkDefault;
      
      // assume IE7
      heightTwkDefault = 127;
      accordianOffSet = 10
      
      // if IE6
      if (ISIE6) {
        heightTwkDefault = 129;
        accordianOffSet = 9;
      }     
      
      // GLOBAL
      var heightTwkForMargin = 8;
      var heightTwkPadding = 8;
      var modHeaderHeight = 30;
      var tabHeights = 31;
      var toggledTwk = 40;
      var splitGap = 8;
      var stopScrollBars = 2;
      var infoBarAllowance = 20;
      
      heightTwkDefault = heightTwkDefault + heightTwkPadding
      
      if (isToggled) {
        heightTwkDefault = heightTwkDefault - toggledTwk;
      }

			//globals
			
			//get the height of the center window
			var baseHeight = $(".ui-layout-center").height();
			var inBodyHeight = (baseHeight - tabHeights);
			
			// set all inSplitter divs, west, center, east to the height of the baseHeight
			$(".inSplitter").height(baseHeight);
			
			// set all inbody divs, west, center, east to the height of the baseHeight minus the hieight of the tabs
			$(".inSplitter .inbody").height(inBodyHeight);

			//trackers
			 
			if ($(".tracker").length > 0) {
			  // update all divs with class of tracker, to track inBodyHeight
			  // this element is set to overlow hiddden, so all scrolling must be managed by elements inside the tracker 
			  $(".tracker").height(inBodyHeight - heightTwkForMargin - stopScrollBars);
			}
			
			if ($(".dialogtracker").length > 0) {
			  // update all divs with class of tracker, to track inBodyHeight
			  // this element is set to overlow hiddden, so all scrolling must be managed by elements inside the tracker 
			  $(".dialogtracker").height($("#aWindowBG").height() - 120);
			}
			

 			if ($(".ui-layout-center .tracker").length > 0) {
			  // if tracker is in .ui-layout-center make allowance for the info bar
			  $(".ui-layout-center .tracker").height(inBodyHeight - heightTwkForMargin - stopScrollBars - infoBarAllowance + 2);
			}    

			if ($(".trackerScroll").length > 0) {
			  // update all divs with class of trackerScroll, to track inBodyHeight
			  // this element is set to scroll auto
			  $(".trackerScroll").height(inBodyHeight - heightTwkForMargin - stopScrollBars);
			}

 			if ($(".ui-layout-center .trackerScroll").length > 0) {
			  // if trackerScroll is in .ui-layout-center make allowance for the info bar
			  $(".ui-layout-center .trackerScroll").height(inBodyHeight - heightTwkForMargin - stopScrollBars - infoBarAllowance);
			}   
			
			if ($(".trackerScrollVertical").length > 0) {
			  // update all divs with class of trackerScrollVertical, to track inBodyHeight
			  // this element is set to vertical scroll
			  $(".trackerScrollVertical").height(inBodyHeight - heightTwkForMargin - stopScrollBars);
			}

 			if ($(".ui-layout-center .trackerScrollVertical").length > 0) {
			  // if trackerScrollVertical is in .ui-layout-center make allowance for the info bar
			  $(".ui-layout-center .trackerScrollVertical").height(inBodyHeight - heightTwkForMargin - stopScrollBars - infoBarAllowance);
			}   
		
 			if ($(".ui-layout-east #ajaxAlertsInline").length > 0) {
 			  var aHeight = $("#ajaxAlertsInline").height();
			  // if trackerScrollVertical is in .ui-layout-center make allowance for the info bar
			  $(".ui-layout-east .trackerScrollVertical").height(inBodyHeight - heightTwkForMargin - stopScrollBars - infoBarAllowance - aHeight);
			}   
			
			
			// publishedPreview
			if ($(".ui-layout-center .tracker iframe.publishedPreview").length > 0) {
			  $(".ui-layout-center .tracker iframe.publishedPreview").height($(".ui-layout-center .tracker").height());
			}    
			
			
	
			// editors
			
			if ($(".anInnovaEditor").length > 0) {
        doEditorResize(heightTwkDefault);
      }

			// split view
			if ($(".splitt").length > 0) {
			  // lock the left and right split panels to the tracker height
			  $(".splitt").height($(".ui-layout-center .tracker").height());
			  
			  // set the width of each split div, 
			  var splittWidth = ($(".ui-layout-center").width() / 2);
			  // to half the available width rounded down
			  splittWidth = Math.floor(splittWidth);
			  // and minus the gap 
			  $(".splitt").width(splittWidth - splitGap);
			  
			  // balance the heights
			  $(".splitt .innerRow:not(.ignore)").height($(".splitt").height());
			  $(".splitt .innerRow .inner:not(.ignore)").height($(".splitt").height() - modHeaderHeight);
			  
			  // set the width of the splitt inner to the width of the slpits minus the gap minus 2 (for borders) 
			  $(".splitt .inner:not(.ignore)").width(splittWidth - splitGap - 2);
			  
			}
			
			
			if ($(".translateEditor").length > 0) {
			  $(".splitt .innerRow .translateEditor").height($(".splitt").height() - modHeaderHeight - 53);
			  $(".splitt .innerRow .translateEditor iframe").height($(".splitt").height() - modHeaderHeight - 53 - 5);		
			}
			
			
			// SPECIALISED USERCONTORLS
			
			// AHOLD COMPARE
			
			if ($(".compareScroll").length > 0) {
			  // find the totla number of caompare panels
			  var comparerCount = $(".compareScroll .inCompare").length;
			  
			  // find the available width for each scroll panel
        var splittWidth = ($(".splitt .inner").width());
			
			  // remove the padding values between the panels
			  var comparerWidth = splittWidth - 12;
			
			  // set the outer scroller width to the width of each compare panel * number of compare panels
			  $(".compareScroll").width(splittWidth * comparerCount);
			
			  // again allow for margins on each compare view
			  $(".compareScroll .inCompare").width(comparerWidth);
			  
			  $(".compareHeightTracker").height($(window).height() - (heightTwkDefault + modHeaderHeight) - 32);
			  
		    if (ISIE6) {
		      heightTwkDefault = heightTwkDefault + 18;
		    }

			  $(".compareHeightTracker iframe").height($(window).height() - heightTwkDefault - 175);
        $(".compareHeightTracker iframe").width(comparerWidth);
        if ($(".compareScroll .expanded").length > 0) {
	         $(".expanded iframe").height($(".compareHeightTracker iframe").height() - 58);
			  }  
        
			}
			

		if ($("#Center_previewIframe").length > 0) {
		    $("#Center_previewIframe").height($(window).height() - (heightTwkDefault) - 2);
		    $("#Center_previewIframe").width($(".ui-layout-center").width() - 10); 
		   // $("#Center_previewIframe").height(200);
		}
		
		if ($("#Center_pinEditor").length > 0) {
		  $("#aspnetForm").height($(".tracker").height());
		  $("#Center_pinEditor").height($(".tracker").height());
		}
		
		if ($(".wideView").length > 0) {
					iframeHeightTwk = 0;
			    if (ISIE6) {
			      heightTwkDefault = heightTwkDefault + 8;
			      iframeHeightTwk = 15;
			    }
			    var fullWidth = ($(".ui-layout-center").width());
		  $("#aspnetForm").height($(window).height() - (heightTwkDefault) - 36);
		  $(".tracker").height($(window).height() - (heightTwkDefault) - 36);
			$(".wideView iframe").height($(window).height() - heightTwkDefault - 157 - iframeHeightTwk);
	    $(".wideView iframe").width(fullWidth - 21);
		}
	
			$(".tracker").show();
			$(".dialogtracker").show();

}

function doEditorResize(heightTwkDefault){

  var IEtweak = 85;
    if (!isIE) {
    IEtweak = 6;
  }

  $(".anInnovaEditor table:first").height($(window).height() - heightTwkDefault);
  $(".anInnovaEditor table:first table:first").height($(window).height() - heightTwkDefault - IEtweak);
  $(".anInnovaEditor").show();

}

  var storeGridWith = -1;
  
  function fixGrid(inID) {
    if (ISIE) {
        if ($("#" + inID + " .grid:not(.ignore) table:not(.ignore)").length > 0) {
          var tweak = 10;
          if (inID == "workAreaAccordian" || inID == "dialog") {
            tweak = 2;
          }
          var fixWidth = $("#" + inID + " .followMe").width() - tweak;
          if (fixWidth != storeGridWith) {
            $("#" + inID + " .grid:not(.ignore) table:not(.ignore)").width(fixWidth);
            storeGridWith = fixWidth
          }
        } 
    }
  }

function checkPanelSize(inAddHeight) {

}

      var openSensitivity = 450;
      var openSpeed = 120;
      var closeSpeed = 80;
      var closeSensitivity = 400;
      var ExpObjList;
      var ExpRequested = false;
      var CollapseRequested = false;
      var historyOpen = false;
    
      function dropDownHoverMore(Object) {
         //clean up any open menus, as another has been triggered
		        ExpObjList = $(Object);
		        hideOpen(ExpObjList);
		        ExpRequested = true; 
		        CollapseRequested = false;
		        setTimeout("showMoreLists()",openSensitivity);
		        return false;
      }
      
        function dropDownOutMore(Object) {
		        ExpRequested = false;
		        CollapseRequested = true;
		        setTimeout("hideMoreLists()",closeSensitivity);
		        return false;
      }  
      
      function dropDownHover(Object) {
         //clean up any open menus, as another has been triggered
		        ExpObjList = $(Object);
		        hideOpen(ExpObjList);
		        ExpRequested = true; 
		        CollapseRequested = false;
             if ($(Object).attr('id') == "inHistory") {
              loadHistory();
             }
		        setTimeout("showLists()",openSensitivity);
		        return false;
      }
      
        function dropDownOut(Object) {
		        ExpRequested = false;
		        CollapseRequested = true;
		        setTimeout("hideLists()",closeSensitivity);
		        return false;
      }
      
      function BindDropDownLists(Object) {
  		    $(Object).hover(
		       function() {
		        //clean up any open menus, as another has been triggered
		        ExpObjList = $(this);
		        hideOpen(ExpObjList);
		        ExpRequested = true; 
		        CollapseRequested = false;
		        
             if ($(this).attr('id') == "inHistory") {
              loadHistory();
             }
		        setTimeout("showLists()",openSensitivity);
		      },
		      function() { 
		        ExpRequested = false;
		        CollapseRequested = true;
		        setTimeout("hideLists()",closeSensitivity);
		    });
		   Object = null;
     }
      
      function loadHistory() {
        if (!historyOpen) {
          historyOpen = true;
          AjaxLoad("#historyList","/parts/history.aspx","/js/controls/controls.HistoryDropDown.js","","",false);
        }
      }
      
      function doDropDownGroupClick(Object) {
           var parentItem = $(Object)[0].parentNode;
           if( $(parentItem).is('.open') ) {
             $(parentItem).children('ul.listitem').slideUp(closeSpeed);
             $(parentItem).removeClass('open');
           }
           else {            
             $('ul.listitem').slideUp(closeSpeed);
             $('li.exp').removeClass('open');
             $(parentItem).children('ul.listitem').slideDown(openSpeed);
             $(parentItem).addClass('open');
           }
           Object = null;
           parentItem = null;
           return false;
      }
      
      function showLists() {
        if (ExpRequested) {
          $(ExpObjList).addClass('expanded');
          setTimeout($(ExpObjList).parent().addClass('shadowimage'), 20);
          setTimeout($(ExpObjList).children('ul').slideDown(openSpeed), 30);
          
//          $(ExpObjList).children('li.moduleLink').bind("click", function(){
//           Modu($(this).attr('id').replace('____',''));
//          })
         
        }
      }
      
      function showMoreLists() {
        if (ExpRequested) {
            $(ExpObjList).addClass('expanded');
            setTimeout($(ExpObjList).children('div.exp').slideDown(openSpeed), 30);
         
        }
      }
      
      var ExpObjParent;
      
      function hideLists() {
        if (CollapseRequested) {
            $(ExpObjList).children('ul').slideUp(closeSpeed);
            $(ExpObjList).removeClass('expanded');
            ExpObjParent = $(ExpObjList).parent();
            setTimeout("removeShadow()", 50)
            historyOpen = false;
        }
      }
      
     function hideMoreLists() {
        if (CollapseRequested) {
            $(ExpObjList).children('div.exp').slideUp(closeSpeed);
            $(ExpObjList).removeClass('expanded');
            ExpObjParent = $(ExpObjList).parent();
        }
      }
      
      function removeShadow() {
        $(ExpObjParent).removeClass('shadowimage');
      }
      
      function hideOpen(Object) {   
        var Expanded = $("body").find('li.expanded')
        var id = $(Expanded).attr('id');
        var thisid = $(Object).attr('id')
              
        if ((id != thisid) && (thisid != 'undefined')) {
            $(Expanded).children('ul').slideUp(closeSpeed);
            $(Expanded).removeClass('expanded');
            ExpObjParent = $(Expanded).parent();
            removeShadow();
        }
      }     

  function CollapseOpen() {
  
  dropDownOut();
  
    //var Expanded = $("body").find('li.expanded');
    //$(Expanded).children('ul').slideUp(closeSpeed);
   //$(Expanded).removeClass('expanded');
    //$(Expanded).parent().removeClass('shadowimage');
  }
   
  function tabClick(TabPID)
  {
    Cntrl(TabPID);
  }
 
  function ToolTipMagicActions(Object) {
    $(Object).cluetip({
    cluetipClass: 'jtip', 
    arrows: true, 
    dropShadow: false,
    sticky: false,
    topOffset: 10,
    leftOffset: 283,
    mouseOutClose: true,
    closePosition: 'top',
    closeText: ''
  });
  return false;
 };

  function ToolTipMagic(Object) {
    $(Object).cluetip({
    cluetipClass: 'jtip', 
    arrows: true, 
    dropShadow: false,
    sticky: false,
    mouseOutClose: true,
    closePosition: 'top',
    closeText: ''
  });
  return false;
 };

  function ToolTipMagicVerySticky(Object) {
    $(Object).cluetip({
    cluetipClass: 'jtip', 
    arrows: true, 
    dropShadow: false,
    sticky: true,
    mouseOutClose: false,
    closePosition: 'top',
    closeText: '<img src="/assets/images/common/close_16.png" alt="close" title="close tooltip" />'
  });
  return false;
 };
 
function HoverBind(Object)
	{		 
			  $(Object).hover(
			   function()
			   {
				$(this).addClass("on");
			   },
			   function()
			   {
				$(this).removeClass("on");
			   });
	}

function CheckboxListBind(Object) {

        var txtID = $(Object).attr('name').replace("chk_", "");
        var selections = $("#" + txtID).val();

        if ($(Object).attr('checked')) {
            if (selections.charAt(selections.length - 1) != "," && selections.length > 0) {
                selections += ",";
            }
            selections += $(Object).val() + ",";
        } else {
            selections = selections.replace(new RegExp($(Object).val() + ",", "g"), "");
            selections = selections.replace(new RegExp("," + $(Object).val(), "g"), "");
            selections = selections.replace(new RegExp($(Object).val(), "g"), "");            
        }
        if (selections.charAt(selections.length - 1) == ",") {
            selections = selections.substring(0, selections.length - 1);
        }
        $("#" + txtID).val(selections);
}

function LoadCodeEdit(Object) {
    var editorName = "code_" + $(Object).attr('name');
    var readOnly = $(Object).attr('readonly');
    var useSyntax = "xml";
    var nondraft = "";
    var js = "";
    if ($(Object).hasClass("css")) {
        useSyntax = "css";
    } else if ($(Object).hasClass("js")) {
    useSyntax = "js";
    js = "javascript";
    }
    if (readOnly) {
        nondraft = "nondraft";
    }    
    window[editorName] = CodeMirror.fromTextArea($(Object).attr('name'), { parserfile: ["tokenize" + js + ".js", ("parse" + useSyntax + ".js")], path: "/assets/codemirror/js/", stylesheet: ("/assets/codemirror/css/" + nondraft + useSyntax + "colors.css"), lineNumbers: true, textWrapping: false, readOnly: readOnly, onChange: ChangeCodeEdit, undoDelay: 1600});
}

function ChangeCodeEdit() {
    UpdateCodeEdit("#workAreaUpdate div.codeEditor textarea");
}

function UpdateCodeEdit(Object) {

//      var IframeObj = $(Object + " iframe")[0];
//      var Iframe  = document.frames[IframeObj.id];

    var editorName = "code_" + $(Object).attr('name');
    $(Object).val(window[editorName].getCode());
}

function UnloadCodeEdit(Object) {
    var editorName = "code_" + $(Object).attr('name');
    window[editorName] = null;
    delete editorName;
}

function UpdateEditor() {
//defensive as can fire b4 editor in place
//this method seems to need alot of defensive code due to the speed it runs

if ($("#Center_block_innerBlockRows_compare_pinEditor")) {
    try
    {
      $("#txt_Content").val(Center_block_innerBlockRows_compare_pinEditor.editGetXHtml());
    }
    catch (ex)
      {}
    }
 

if ($("#Center_block_innerBlockRows_pinEditor")) {
    try
    {
      $("#txt_Content").val(Center_block_innerBlockRows_pinEditor.editGetXHtml());
    }
    catch (ex)
      {}
    }

if ($("#Center_pinEditor")) {
    try
    {
      $("#txt_Content").val(Center_pinEditor.editGetXHtml());
    }
    catch (ex)
      {}
    }
}
    

//COMMON ACCORDIAN CODE...
 function BindAccordian(Object)
 {
   if( $(Object).parent().is('.open') ) {
          $(Object).next().hide();
          $(Object).parent().removeClass('open');
          $(Object).parent().addClass('closed');
          //checkPanelSize(0);
       }
       else if ($(Object).parent().is('.closed')) {
          $(Object).next().show();
          $(Object).parent().removeClass('closed');
          $(Object).parent().addClass('open');
          //checkPanelSize($(this).parent().height());
       }
    Object = null;  
    return false;
}

//COMMON DROPDOWN
//function BindDropDown(Object, Message)
//{
//		 var navItems = $(Object);
//			$("div.expand",navItems).hide();
//			$(navItems).hover(
//					function(){  
//					Over = true;
//					ExpObj = $(this);
//					$("#statusMessages").text(" :" + Message);
//				  setTimeout("Expand()",250);
//		},
//		function(){ 
//		   Over = false;
//		   historyOpen = false;
//					Collapse($(this));
//					$("#statusMessages").text("");	
//		});
//		//$("table tr", Object )
//}

	var Over = false;
	var ExpObj;

function Expand()
{
     if (Over)
     {
      toolTipHide('#cluetip');
     //alert("over");
					$(ExpObj).children("div.expand").slideDown(130);
					$(ExpObj).children("div.selectedTab").addClass('active');
					$(ExpObj).addClass('shadowimage');
					}
}

function Collapse(Obj)
{
 					$(Obj).children("div.expand").slideUp(160);
					$(Obj).removeClass('shadowimage');
					$(Obj).children("div.selectedTab").removeClass('active');
}

function CollapseFast(Obj)
{
 					$(Obj).children("div.expand").slideUp(10);
					$(Obj).removeClass('shadowimage');
					$(Obj).children("div.selectedTab").removeClass('active');
}




function doClipBoard(inID) {
  var textToCopy;
  textToCopy = document.getElementById(inID).innerHTML;
  window.clipboardData.setData("Text", textToCopy);
  swapClipBoardClass($(".clipboard"));
}

function doClipBoardAsText(inID) {
  var textToCopy;
  textToCopy = document.getElementById(inID).innerText;
  window.clipboardData.setData("Text", textToCopy);
  swapClipBoardClass($(".clipboard"));
}


 function swapClipBoardClass(Object)
 {
          $(Object).bind("click", function(){ 
          $(".clipboard").removeClass("clipboardok");
          $(this).addClass("clipboardok");
});
}

$(document).ready(function(){

		 if ( $(".CRM .contactNumbers").length > 0 ) {
		    var innerHeight = $(".CRM .contactNumbers .crminner").height();
		    $(".CRM .widgetContainer .numbers").height(innerHeight + 20);
		    
		 }
		
   
   $("#workareaExpandFull").unbind("click").bind("click", function(){ 
      if ($("#infoSub").hasClass('hidden')) {
        $("#infoSub").removeClass('hidden');  
      }
      else {
        $("#infoSub").addClass('hidden');  
      }
   
	 });
});


function windowOpener(url, width, height) {
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    var windowFeatures = "width=" + width + ",height=" + height + ",status,resizable,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
    myWindow = window.open(url, "subWind", windowFeatures);
}

function windowOpenerFull(url) {
    var windowFeatures = "status,resizable";
    myWindow = window.open(url, "subWind", windowFeatures);
}


