/*--------------------------------------------------------------------------------------------------------------------------*/
/*									 		Window OnLoad Event - Initialization
/*--------------------------------------------------------------------------------------------------------------------------*/

/**--------------------------------------------------------------------------------------------------------------------------*
 * addEvent
 *  -addEvent is called when the page is loaded.  This method sets up when the windows.onload method is called then initPageLoader() will be called.
 *
 **--------------------------------------------------------------------------------------------------------------------------*/
addEvent(window, 'load', initPageLoader);


/**--------------------------------------------------------------------------------------------------------------------------*
 * initPageLoader
 *   -Funtion that is called from adding the window load event to the addEvent method above
 *
 **--------------------------------------------------------------------------------------------------------------------------*/
function initPageLoader() {
	initHomePageFlash();
	initPagesForJS();
	initSearchField();
}


/**--------------------------------------------------------------------------------------------------------------------------*
 * initHomePageFlash
 *  -Uses the swfobject.js file to make the call, this method sets up the flash file to be used on the homepage in id home-page-flash
 *
 **--------------------------------------------------------------------------------------------------------------------------*/
function initHomePageFlash() {
	if (swfobject.hasFlashPlayerVersion("9")) {
		var att = { data:"ccss-home-page-marketing.swf", width:"590", height:"300" };
		var par = {  
			src: "ccss-home-page-marketing.swf",
			movie: "ccss-home-page-marketing.swf",
			bgcolor: "#FFFFFF",
			quality: "high",
			pluginspage: "http://www.adobe.com/go/getflashplayer",
			align: "middle",
			play: "true",
			loop: "true",
			scale: "showall",
			wmode: "window",
			devicefont: "default",
			id: "ccss-home-page-marketing",
			name: "ccss-home-page-marketing",
			menu: "true",
			allowFullScreen: "false",
			allowScriptAccess: "sameDomain",
			salign: ""		
		};
		var id = "home-page-flash";
		var myFlashContent = swfobject.createSWF(att, par, id);
	}
}


/**--------------------------------------------------------------------------------------------------------------------------*
 * initPagesForJS
 * -Added onfocus and onblur functions for the form fields so that the class will change for the form when a user is on a specific field.
 *  This will also be used for all pages that need JS functions, like onblur, onfocus, onmouseover
 * -NOTE: Make sure that each input field has the classname of formfield for this behavior to work.
 *
 **--------------------------------------------------------------------------------------------------------------------------*/
function initPagesForJS() {
	
	// FORMS
	fields = getElementsByClassName(document, "*", "formfield");
	
	for(i = 0; i < fields.length; i++) {
		fields[i].onfocus = function(){this.parentNode.className="focused";};
		fields[i].onblur = function(){this.parentNode.className="";};
	}
	
	$$('#product-landing-features img').each(function(element) {
		element.onmouseover = function() { featureHover(element,element.id,true); };
		element.onmouseout  = function() { featureHover(element,element.id,false); };
		element.onclick     = function() { toggleFeature(element.id + '-text'); };
  	});
	
	
	//$('rss-feed').observe('click', function(event){
	$$('#expand-all-features').each(function(element) {
		element.onclick     = function() { 
			$$('#product-landing-features img').each(function(element) {
				Effect.SlideDown(element.id + '-text',{duration: 1.5 });
				return true;
			});
		};
  	});
	
	$$('#collapse-all-features').each(function(element) {
		element.onclick     = function() { 
			$$('#product-landing-features img').each(function(element) {
				Effect.SlideUp(element.id + '-text', {duration: 1.5 });
				return true;
			});
		};
  	});
	
	$$('#product-demos img').each(function(element) {
	    var originalSrc = element.src;
  		element.onmouseover = function() { element.src='/images/demo-thumbnail-hover.png'; };
		element.onmouseout  = function() { element.src = originalSrc; };
  	});
}


/**--------------------------------------------------------------------------------------------------------------------------*
 * initSearchField
 * 	-Clears the Search Field when the page is loaded.
 *
 **--------------------------------------------------------------------------------------------------------------------------*/
function initSearchField(){
	document.getElementById("searchfield").value="";
}


/*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------*/
/*									 			COMMON APPLICATION METHODS
/*--------------------------------------------------------------------------------------------------------------------------*/


/**--------------------------------------------------------------------------------------------------------------------------*
 * Social links dynamic URLS
 * 	-URL's for the social applications located in the footer
 *
 **--------------------------------------------------------------------------------------------------------------------------*/
var rssURL             = '/feeds/rss.xml';
var deliciousURL       = 'http://delicious.com/save?v=5&noui&jump=close&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title);
var diggURL            = 'http://digg.com/submit?url='+encodeURIComponent(location.href)+'&amp;title='+encodeURIComponent(document.title);
var stumbleUponURL     = 'http://www.stumbleupon.com/submit?url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title);
//var newsvineURL        = 'http://ccssltd.newsvine.com/';
//var newsvineSaveURL    = 'http://www.newsvine.com/_wine/save?popoff=1&u='+encodeURIComponent(location.href);
var twitterURL         = 'http://twitter.com/ccssltd';
var googleBookmarksURL = 'http://www.google.com/bookmarks/mark?op=add&output=popup&bkmk='+ encodeURIComponent(location.href) +'&title='+encodeURIComponent(document.title);
var facebookFanPageURL = 'http://www.facebook.com/pages/CCSS-Monitoring-your-Systems/51415911386';
var facebookShareURL   = 'http://www.facebook.com/sharer.php?u='+ encodeURIComponent(location.href) +'t='+encodeURIComponent(document.title);
var linkedInURL        = 'http://www.linkedin.com/groups?about=&gid=1771585&trk=anet_ug_grppro';  


/**--------------------------------------------------------------------------------------------------------------------------*
 * Lightview
 * 	-pop-ups per element
 *  - refer to http://www.nickstakenburg.com/projects/lightview/ for documentation
 *
 **--------------------------------------------------------------------------------------------------------------------------*/
var topCloseBrowserCheck = true; //default
//check for IE
topCloseBrowserCheck = !(navigator.userAgent.toLowerCase().indexOf('msie 6') != -1);


//Lightview popup register for all popup windows per tag's id
document.observe('dom:loaded', function() {
	$('rss-feed').observe('click', function(event){showFooterSocialPopUp(event, rssURL, 'CCSS RSS Feed');});
	$('delicious').observe('click', function(event){showFooterSocialPopUp(event, deliciousURL,'delicious');});
	$('digg').observe('click', function(event){showFooterSocialPopUp(event, diggURL, 'digg');});
	$('stumble-upon').observe('click', function(event){showFooterSocialPopUp(event, stumbleUponURL, 'StumbleUpon');});
	//$('newsvine').observe('click', function(event){showFooterSocialPopUp(event, newsvineURL, 'Newsvine');});
	// Twitter doesn't allow for other windows to open their pages
	//$('twitter').observe('click', function(event){showFooterSocialPopUp(event, twitterURL, 'Twitter');});
	$('google').observe('click', function(event){showFooterSocialPopUp(event, googleBookmarksURL, 'Google Bookmarks');});
	$('facebook').observe('click', function(event){showFooterSocialPopUp(event, facebookFanPageURL, 'Facebook Fan Page');});
	$('linked-in').observe('click', function(event){showFooterSocialPopUp(event, linkedInURL,'Linked In Groups');});

});


function showLoginForm(event, linkURL) {
	//Event.stop(event);
	Lightview.show({
		href: linkURL,
		rel: 'iframe',
		options: {
			autosize: true,
			topclose: topCloseBrowserCheck,
			overlayClose: false,
			width: 525,
      		height: 425
		}
	});
}

function showFooterSocialPopUp(event, linkURL, linkTitle) {
	Event.stop(event);
	Lightview.show({
		href: linkURL,
		title: linkTitle,
		//caption: linkURL,
		options: {
			fullscreen: true,
			overlayClose: true
		}
	});
}


/**--------------------------------------------------------------------------------------------------------------------------*
 * Prototips
 * 	-Tips per element id
 *  - refer to http://www.nickstakenburg.com/projects/prototip2/ for documentation
 *
 **--------------------------------------------------------------------------------------------------------------------------*/

document.observe('dom:loaded', function() {
  $$('#footer-links-social a').each(function(element) {
  		new Tip(element, element.href, {	
			title: element.rel,
			style: 'ccss-footer'
		});
  });
});


/**--------------------------------------------------------------------------------------------------------------------------*
 * toogleHomeTabs
 * 	-toogles the home page who,what,why tabs/content to make sure the correct "tab" and content is showing (hides other content)
 *
 **--------------------------------------------------------------------------------------------------------------------------*/
function toggleHomeTabs(selectedTab) {
	document.getElementById("who-we-are").className = "";
	document.getElementById("who-we-are-content").style.display="none";
	document.getElementById("who-we-are-content").style.visibility="hidden";
	document.getElementById("what-we-do").className = "";
	document.getElementById("what-we-do-content").style.display="none";
	document.getElementById("what-we-do-content").style.visibility="hidden";
	document.getElementById("why-us").className = "";
	document.getElementById("why-us-content").style.display="none";
	document.getElementById("why-us-content").style.visibility="hidden";
		
	document.getElementById(selectedTab).className = "tab-selected";
	document.getElementById(selectedTab +"-content").style.display="block";
	document.getElementById(selectedTab +"-content").style.visibility="visible";
}

/**--------------------------------------------------------------------------------------------------------------------------*
 * toogleProductContent
 * 	-toogles the product landing page content to make sure the correct "tab" and content is showing (hides other content)
 *
 **--------------------------------------------------------------------------------------------------------------------------*/
function toggleProductContent(selectedTab) {
	document.getElementById("real-time-monitoring").className = "";
	document.getElementById("real-time-monitoring-content").style.display="none";
	document.getElementById("real-time-monitoring-content").style.visibility="hidden";
	document.getElementById("pro-active-management").className = "";
	document.getElementById("pro-active-management-content").style.display="none";
	document.getElementById("pro-active-management-content").style.visibility="hidden";
	document.getElementById("remote-management").className = "";
	document.getElementById("remote-management-content").style.display="none";
	document.getElementById("remote-management-content").style.visibility="hidden";
		
	document.getElementById(selectedTab).className = "selected";
	document.getElementById(selectedTab +"-content").style.display="block";
	document.getElementById(selectedTab +"-content").style.visibility="visible";
	
}

/**--------------------------------------------------------------------------------------------------------------------------*
 * toogleList
 * 	-toogles lists of items with opening and closing the arrows as well as sliding down the content
 *
 **--------------------------------------------------------------------------------------------------------------------------*/
function toggleList(src_element, div_id){	

	Effect.toggle(div_id, 'slide', {duration: 1.5, afterFinish:function(){
		image = src_element.getElementsByTagName('img')[0];
		image.src = image.src.match("/images/close.png") ? "/images/open.png" : "/images/close.png";
	}});
	return true;
}

function toggleFeature(div_id){	
	Effect.toggle(div_id, 'slide', {duration: 1.5});
	return true;
}


/**--------------------------------------------------------------------------------------------------------------------------*
 * Search Functions
 *  Clears and Resets Search field from the onFocus and onBlur methods for the input field
 *
 **--------------------------------------------------------------------------------------------------------------------------*/
function clearSearchField(element) {
	if (element.value == element.defaultValue) { 
		element.value = ''; 
	}
	element.style.background ='transparent';
}

function resetSearchField(element) {
	if(element.value == "") {
		element.style.background='transparent url(/images/powered-by-google.png) no-repeat';
	}
}

/**--------------------------------------------------------------------------------------------------------------------------*
 * Form Functions
 *  Assists with form confirmations and form actions
 *
 **--------------------------------------------------------------------------------------------------------------------------*/
function confirmLogoff(){
	if(confirm('Are you sure you want to logoff?')){
		window.location.href='/login/process.php?redirect=/support';
	}else{
		return false;
	}
}

function confirmDelete(){
	if(confirm('Are you sure you want to delete this user?')){
		updateUser('subdeluser');
	}else{
		return false;
	}
}

function confirmUsernameChange(currentUsername){
	if(currentUsername != document.updateMyAccount.updusername.value) {
		if(confirm('You have change the username on this account and will be automatically logged off the site.\n\nPlease log back in with the new username\n\n'+document.updateMyAccount.updusername.value)){
			return true;
		}else{
			return false;
		}
	} else {
		return true;
	}
	
}
									
function updateUser(action) {
	var updateAction = document.getElementById('updateAction');
	updateAction.name = action;
										
	document.updateUserRecord.submit();
}



/**--------------------------------------------------------------------------------------------------------------------------*
 * featureHover
 *  changes the graphic of the features to the hover value based on mouse over and mouse out.  If TRUE is passed in that means
 *  the field is being hovered over
 *
 **--------------------------------------------------------------------------------------------------------------------------*/
function featureHover(element, feature, hover) {
	if(hover) {
		element.src = '/images/feature-'+feature+'-hover.png';
	} else {
		element.src = '/images/feature-'+feature+'.png';
	}
}


/*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------*/
/*									 THANKS FOR HELPFUL SCRIPTS - OPEN SOURCE
/*--------------------------------------------------------------------------------------------------------------------------*/

/*function getUrlParam( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}*/

//http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/

function getElementsByClassName(oElm, strTagName, strClassName){

	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);

	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];		
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}	
	}
	return (arrReturnElements)
}

//http://ejohn.org/projects/flexible-javascript-events/
function addEvent( obj, type, fn ) {

	if ( obj.attachEvent ) {	
    	obj["e"+type+fn] = fn;
	    obj[type+fn] = function() { obj["e"+type+fn]( window.event ) };
		obj.attachEvent( "on"+type, obj[type+fn] );
	} else{
		obj.addEventListener( type, fn, false );	
  	}

}


/*----------------------------------------------------------------------------------------------------------------------------*/





