﻿//Detect Browsers

//prototypes
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
/***end prototypes ***/
var addingNav = false;


var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) 
	{
		for (var i=0;i<data.length;i++)	
		{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

//End browser detect

//remove the annoying "click to activate" flash in IE
function activateSWFs()
{
    //alert("activate swf!");
    var objects = document.getElementsByTagName("object");
    for (var i = 0; i < objects.length; i++)
    {
	    objects[i].outerHTML = objects[i].outerHTML;
    }
}


function fixIEClass(thing)
{
    if (thing.className) 
    {
        thing.className = thing.className;
    }
/*
    for(var prop in thing)
    {
        thing[prop] = thing[prop]
    }
*/
  
    return thing;
}

//PAGE NAVIGATION HANDLING

//hide all pageNavs
function hideAllPageNavs(total)
{

    for(var i = 1; i<=total; i++)
    {
        var pageNav = document.getElementById("pageNav_" + i);        
        if(BrowserDetect.browser == "Explorer")
        {            
            //ie only takes in one pageNav... weird.
            if(BrowserDetect.version >= 7) 
            {
                pageNav = fixIEClass(pageNav);
                pageNav.className = "";
            }
            else
            {
                pageNav.className = "";
                pageNav.removeAttribute("class");
            }
            
        }
        else
        {
            pageNav.className = "";     
        }
    }        
}

// hide all contents in the page

function hideAllContent(total)
{  
    for(var i = 1; i<=total; i++)
    {
        var content = document.getElementById("content_" + i);
        if(BrowserDetect.browser == "Explorer")
        {
            content.style.display = "none";
        }
        else
        {     
            content.style.display = "none";
        }
    }   
}

//show the current selected page.
function setPage(titleId,total)
{
    //alert("setPage: " + titleId +" , " + total);

    hideAllPageNavs(total);
    hideAllContent(total);
    var currentNav = document.getElementById("pageNav_"+titleId);
    if(BrowserDetect.browser == "Explorer")
    {
        if(BrowserDetect.version >= 7)
        {
            currentNav = fixIEClass(currentNav);
            currentNav.className = "on";
        }
        else
        {
            currentNav.setAttribute("class", "on");
            currentNav.className = "on";
        }
    }
    else
    {
        currentNav.className = "on";
    }
    currentNav.getAttribute("class");
    
    var currentContent = document.getElementById("content_"+titleId);
    currentContent.style.display = "block";
    
}
//End Navigation handling



//new section Nav / pageNav


//display a textfield, which will take in label the newly added nav
function addNewNav(element)
{   
    if(addingNav == false)
    {
        addingNav = true;
       
        var curId = element.id; 
        var curValue = element.innerText;
        //alert(curValue);
        element.innerHTML = "<input id='edit_"+curId+"' type='text' class='pageNavInput' value='"+curValue+"'><img src='images/icon_yes.gif' width='10' height='10' onClick='createNewNav(\"edit_" +curId + "\",\""+curId+"\",\""+curValue+"\");'> <img src='images/icon_no.gif' width='10' height='10' onClick='cancelNewNav(\"" + curId + "\",\""+ curValue+"\")'>";
        
        document.getElementById("edit_" + curId).focus();
        document.getElementById("edit_" + curId).select();
    }
}

//create the new nav
function createNewNav(edit_NewNavId, oldId, oldValue)
{
    //edit_NewNavId = last nav
    //oldId = id of this span, containing the label.
    //value of (new tab) label 
    var newNav = document.getElementById(edit_NewNavId);
    
    //if newNav.value (the new nav label) is different than the old one, then create a new page.
    if (newNav.value != oldValue && newNav.value.trim() != "")
    {     
    
        var oldNav = document.getElementById(oldId);
        //alert("new nav value: " + newNav.value);
        var totalContent = oldId.replace("pageNav_", "");        

        oldNav.innerHTML = newNav.value;          
        oldNav.removeAttribute('class');
        oldNav.className='';
        
        //assign mouseclick and doubleclick to the new nav item
        var newSetPageCommand = 'setPage('+totalContent+','+totalContent+');';
        oldNav.onclick = newSetPageCommand;
        var newEditPageCommand = 'editNav(this);';
        oldNav.ondblclick = newEditPageCommand;
        
        //replace all totalContent in setPages(currentContent,totalContent) in pageNavs in  PageNavList with new totalContent
        var pageNavList = document.getElementById("pageNavList");
        var newPageNavListContent = pageNavList.innerHTML;  // all the navs in pageNavList
        
        // now replace the oldTotalContent with a (new) totalContent
        var oldTotalContent = totalContent - 1;
 
        var intIndexOfMatch = newPageNavListContent.indexOf( ","+oldTotalContent );
        while (intIndexOfMatch != -1)
        {
            // Replace out the current instance.
            newPageNavListContent = newPageNavListContent.replace( ","+oldTotalContent, ","+totalContent )

            // Get the index of any next matching substring.
            intIndexOfMatch = newPageNavListContent.indexOf( ","+oldTotalContent);
        }

        pageNavList.innerHTML = newPageNavListContent;
        

        //append hidden input after this nav
        pageNavList.innerHTML +=  '<input type="hidden" id="pageNavHiddenValue_'+totalContent+'" name="pageNavHiddenValue_'+totalContent+'" value="' +newNav.value +'" >';
        
        //create a new blank page
        createNewPage(totalContent);  
         
        //append Create New Nav nav on the list
        
        appendNewNavTab(totalContent);          
        //alert(pageNavList.innerHTML);  

        document.getElementById("content_count").value = totalContent; // <input id="content_count" type="hidden">  stores total number of content tabs/pages
        
        setPage(totalContent,totalContent); //set the tab to the newly created page.

        addingNav = false;
   }
   else
   {
        cancelNewNav(oldId,oldValue);
   }
}

//create a new textarea content for the new nav page.
function createNewPage(totalContent)
{
    //alert("creating new page: " + totalContent);
    var bodyList = document.getElementById("bodyList");
    //alert(bodyList.innerHTML);
    var pageContent = "";
    pageContent += bodyList.innerHTML;   
    
    //grab all of the wysiwyg content from pageContent, and store each in an contentEditArray
    var contentEditArray = new Array();
    contentEditArray.push("initialvalue for contentEditArray[0]"); // array starts with 0.  we won't be using the first (0) arrayNode.
    
    for(var i=1;i<totalContent;i++)
    {
        var doc = document.getElementById("wysiwygcontentEdit_" + i).contentWindow.document;
        contentEditArray.push(doc.body.innerHTML);
    }
    
    pageContent = ""; // clear pageContent, removing the old wyswig stuff.
    
    //rewrite pageContent, fill it with divs and text area with only textarea values from contentEditArray
    for(var i = 1;i<=totalContent;i++)
    {
        pageContent += "<div name='content_" + i + "' id='content_" + i + "' class='pageBody' style='display:none;'>";
        pageContent += "<textarea id='contentEdit_" + i + "' name='contentEdit_" + i + "' style='height: 750px; width: 400px;'>";
        if(i<totalContent)
        {
            pageContent += contentEditArray[i];
        }
        else
        {
            pageContent += "";
        }
        pageContent += "</textarea>";
        pageContent += "</div>";    
    }
    bodyList.innerHTML = pageContent;  
      
    //regenerate all wysiwyg editors.
    for(var i = 1;i<=totalContent;i++)
    {
        generate_wysiwyg('contentEdit_' + i);
    }
    
    
    
    var currentBodyList = bodyList.innerHTML;
}

//append a new nav at the end of the nav list
function appendNewNavTab(total)
{
    
    var newId = ++total;

    var pageNavList = document.getElementById("pageNavList");
    pageNavList.innerHTML +=  " <span class='new' id='pageNav_" + newId + "' onClick='addNewNav(this)'>[+] New Page</span>";
    
    document.getElementById('content_count').value = newId;
}


//edit existing navLabel
function editNav(element)
{   
    //alert("editing: " + element.id);
    if(addingNav == false)
    {
        addingNav = true;
       
        var curId = element.id;
        var curValue = element.innerHTML;
        //alert("this nav's current value: " + curValue);
        element.innerHTML = "<input id='edit_"+curId+"' type='text' class='pageNavInput' value='"+curValue+"'> <img src='images/icon_yes.gif' width='10' height='10' onClick='updateNav(\"edit_" +curId + "\",\""+curId+"\",\""+curValue+"\");'> <img src='images/icon_no.gif' width='10' height='10' onClick='cancelNewNav(\"" + curId + "\",\""+ curValue+"\")'>";
        
        document.getElementById("edit_" + curId).focus();
        document.getElementById("edit_" + curId).value = document.getElementById("edit_" + curId).value;
        //document.getElementById("edit_" + curId).select();
    }
}

// update the currently edited navLabel
function updateNav(edit_NewNavId, oldId, oldValue)
{

    var newNav = document.getElementById(edit_NewNavId);
      
    if (newNav.value != oldValue)
    {     
        //alert(newNav.value); 
        var oldNav = document.getElementById(oldId);
        oldNav.innerHTML = newNav.value;           
        var curId = oldId.replace("pageNav_", "");
        document.getElementById("pageNavHiddenValue_" + curId).value = newNav.value;    // update the hidden input with new value
        addingNav = false;
   }
   else
   {
        cancelNewNav(oldId,oldValue);
   }
}


//cancel adding new nav, or edit nav.
function cancelNewNav(id, value)
{
    var element = document.getElementById(id);
    element.innerHTML = value;
    addingNav = false;
}

//linkTo - just like href
function linkTo(page)
{
    window.location = page;
}



//character counter
function textCounter(field, countfield, maxlimit)
{
if (field.value.length > maxlimit)
   field.value = field.value.substring(0, maxlimit);
else
   countfield.value = maxlimit - field.value.length;
}



//toggle hideand Show

function toggleView(id)
{
    var element = document.getElementById(id);
    if(element.style.display == "none")
    {
        Effect.BlindDown(id, {duration:0.3});        
    }
    else if(element.style.display == "block")
    {
        Effect.BlindUp(id, {duration:0.3});        
    }
}