// some information from our users environment
var agt = navigator.userAgent.toLowerCase();
var is_opera = (agt.indexOf('opera') != -1);
var is_msie = (agt.indexOf('msie') != -1 && !is_opera );
var is_safari_103 = (agt.indexOf('safari/312') != -1 );

// alert("DEBUG: " + agt );

var oldBrowser = false;
var isNS4 = false;
var isNS6 = false;
var isIE4 = false;
var isIE5 = false;
var xPosition = 0;

if ( parseInt( navigator.appVersion ) < 4 ) {
   oldBrowser = true
} else {	
   isNS4 = (document.layers) ? true : false;
   isIE4 = (document.all && !document.getElementById) ? true : false;
   isIE5 = (document.all && document.getElementById) ? true : false;
   isNS6 = (!document.all && document.getElementById) ? true : false;
}

// some feature tour/popup variables
document.hasWindow = 0;
var Komodo_Tour_Window;
var PDK_Tour_Window;
var AS_Popup = 'AS_Popup';
var AS_Popup_Window;
var d_win;

function ToggleTab(tabID, contentID, isUpsell){
   if(isUpsell){
      var nextActiveTab = document.getElementById( tabID );
      var nextActiveContent = document.getElementById( contentID );
      
      if( nextActiveTab == currActiveUpsellTab ){ return; }
      
      nextActiveTab.className = "activeTab";
      nextActiveContent.className = "visible";
      
      currActiveUpsellTab.className = "";
      currActiveUpsellContent.className = "hidden";
      
      currActiveUpsellTab = nextActiveTab;
      currActiveUpsellContent = nextActiveContent;
      
   } else {
      var nextActiveTab = document.getElementById( tabID );
      var nextActiveContent = document.getElementById( contentID );
      
      if( nextActiveTab == currActiveTab ){ return; }
      
      currActiveTab.className = "";
      currActiveContent.className = "hidden";
      
      nextActiveTab.className = "activeTab";
      nextActiveContent.className = "visible";
      
      currActiveTab = nextActiveTab;
      currActiveContent = nextActiveContent;
   }
}

function ShowPopup(filepath,h,w){
   var width = 568;
   var height = 420;
   
   if(h > 0){
      height  = h;
   }
   if(w > 0){
      width = w;
   }
   var resize = "yes";
   var scrollbars = "yes";
   var features = 'width='+width+',height='+height+',scrollbars='+scrollbars+',resizable='+resize+'';
   AS_Popup_Window = window.open(filepath,AS_Popup,features);
   AS_Popup_Window.focus;
   
   return false;
}

// Tour Functions
function ShowScreen( my_id, productid, h, w){
   
   var my_options = 'width='+w+',height='+h+',scrollbars=no,resize=no';
   
   //let's set a width/height for the specific tours
   if( productid == 'Komodo' ){
      my_options = 'width=830,height=620,scrollbars=no,resize=no';
      Komodo_Tour_Window = window.open( '/products/komodo_ide/Tour/'+my_id+'/',AS_Popup,my_options );
      Komodo_Tour_Window.focus();
   } else if ( productid == 'PerlDevKit' ) {
      my_options = 'width=620,height=730,scrollbars=no,resize=no';
      PDK_Tour_Window = window.open( '/Products/Perl_Dev_Kit/Tour/'+my_id+'/',AS_Popup,my_options );
      PDK_Tour_Window.focus();
   }
   return false;
}

function ShowTour( productid, h, w ){
   
   var my_options = 'width='+w+',height='+h+',scrollbars=no,resize=no';

   //let's set a width/height for the specific tours
   if( productid == 'Komodo' ){
      my_options = 'width=830,height=620,scrollbars=no,resize=no';
      Komodo_Tour_Window = window.open( '/products/komodo_ide/Tour/',AS_Popup,my_options );
      Komodo_Tour_Window.focus();
   } else if ( productid == 'PerlDevKit' ) {
      my_options = 'width=620,height=730,scrollbars=no,resize=no';
      PDK_Tour_Window = window.open( '/Products/Perl_Dev_Kit/Tour/',AS_Popup,my_options );
      PDK_Tour_Window.focus();
   }
   return false;
}

function TabRedirect( baseURL, currentURL ){
   
   var subject  = location.href;

   var escapedCurrentURL = currentURL.replace(/\//,'\/');

   var regexStr = '(.*)' + escapedCurrentURL + '(.+)$';

   var regexStr2 = '(http:\/\/.*?)\/';
   
   var regexObj = new RegExp( regexStr );
   var matches = regexObj.exec( subject );

   var regexObj2 = new RegExp( regexStr2 );
   var matches2 = regexObj2.exec( subject );

   if ( matches == null) {
      if( matches2 != null ){
         var path = matches2[1] + baseURL;
         //alert("match not found... redirecting to: " + path);
         location.href = path;
         return false;
      } else {
         // nothing seemed to work hey? let the html take it then
         return true;
      }
   } else {
      if( matches2 != null ){
         var path = matches2[1] + baseURL + matches[2];
         //alert("match found... redirecting to: " + path);
         location.href = path;
         return false;
      } else {
         // nothing seemed to work, let the HTML take over
         return true;
      }
   }
}

function findPosX(obj) {
   var curleft = 0;
   if (obj.offsetParent)
   {
      while (obj.offsetParent)
      {
         curleft += obj.offsetLeft
         obj = obj.offsetParent;
      }
   }
   else if (obj.x)
      curleft += obj.x;
   return curleft;
}

function toggleDiv(chk, id) {
    var div = document.getElementById(id);
    
    if (div && chk) {
        if (chk.checked) {
            div.style.display = 'block';
        }
        else {
            div.style.display = 'none';
        }
    }
}

function GetImageXPos(imgId){
   
   if( isIE5 || isNS6 ){
      
      xPosition = findPosX( document.getElementById(imgId) );
   } else if(isNS4){
      
   } else {
      xPosition = findPosX( document.all[imgId] )
   }
   
   return (xPosition);
}




//Komodo Edit vs IDE feature chart functions

function toggleFeature(listitem) { //Toggles the description of the feature on click
	
	var description = $(listitem).children().filter(".description"); //Create duplicate object to isolate the description
	var featureName = $(listitem).children().filter(".feature-name"); //Create duplicate object to isolate the feature name

	listitem.toggleClass("expanded");

	if( description.css("display") == "block") { //If description is visible
		description.slideToggle("fast"); //hide it
		listitem.css( { backgroundImage: "url(/_images/icons/icon-toggle-plus.gif)" }); //change the minus to a plus
	} else { //otherwise if the description is not visisble
		description.slideToggle("fast"); //show it
		listitem.css( { backgroundImage: "url(/_images/icons/icon-toggle-minus.gif)" }); //change the plus to a minus
	}
}


function mouseover(listitem) {  //mouseover styles
	
	var featureName = $(listitem).children().filter(".feature-name"); //duplicate object to isolate feature name

	featureName.toggleClass("selected");
	listitem.toggleClass("hovered");
}


function mouseout(listitem) { //mouseout styles

	var featureName = $(listitem).children().filter(".feature-name"); //duplicate object to isolate feature name
	
	featureName.toggleClass("selected");
	listitem.toggleClass("hovered");

}

function addToggle () {  //Adds onclick, onmouseover, onmouseout events to all feature item <li>s
	$("li.feature-list-item").click(function(){toggleFeature($(this))}).mouseover(function(){mouseover($(this))}).mouseout(function(){mouseout($(this))});
}

function preLoadImages(img_array) {
	for (var i = 0; i <= img_array.length; i++) {
		var tmp = new Image();
		if(img_array[i]) {
			tmp.src = img_array[i];
		}
	}
}


