// Global JS for entire site
$(document).ready(function(){	

if($.cookie('Login')) {
	var loggedin = $.cookie('Login');
	var logstat = loggedin.charAt(20);
	
	if(logstat=='Y') {
		$('#login-form').hide();
	 	$('#support-link').show();
	 } else {
	 	$('#login-form').show();
	 	$('#support-link').hide();
	 }
}



	//find first and last item dynamically in top nav and assign necessary classes
	$('#topnav > li:first').addClass('first');
	$('#topnav > li:last').addClass('last');
	
	//a tag find and add span
	$('#sidebar a').each(
		function() {
			$parentClass = $(this.parentNode).attr('class');
			if($parentClass=='latest-news') {
				//do nothing
			} else {
				$aTagHtml = $(this).html();
				$(this).html('<span>' + $aTagHtml + '</span>');
				$(this).addClass('sidebarlink');
			}
		});
	
	
	//find and replace only links with the title of "bluebutton"
	$('a').each(
		function() {
			$aTagHtml = $(this).html();
			$aTitle = $(this).attr('class');
			if($aTitle=='bluebutton') {
				//$(this.parentNode).css('line-height' , '13px');
				$(this).css('margin-top' , '0px');
				$(this).html('<span>' + $aTagHtml + '&nbsp;&nbsp;&nbsp;&raquo;</span>');
			}
		}
	);
	$('a').each(
		function() {
			$aTagHtml = $(this).html();
			$aTitle = $(this).attr('title');
			if($aTitle=='goldbutton') {
				$(this).addClass('goldbutton');
				//$(this.parentNode).css('line-height' , '13px');
				$(this).html('<span>' + $aTagHtml + '&nbsp;&nbsp;&nbsp;&raquo;</span>');
			}
		}
	);
	
	//support login form
	$('#login-form #email').val('Email');
	$('#login-form #email').focus(
		function() {
			$(this).val('');
		}	
	);
	$('#login-form #password').val('Password');
	$('#login-form #password').focus(
		function() {
			$(this).val('');
		}	
	);
	
	//search box
	$('#searchform #s').val('Search Convey');
	$('#searchform #s').focus(
		function() {
			$(this).val('');
		}	
	);
	$('#searchform #searchsubmit').val('');
	
	//feature tabs hide and show based on content being there
	if($('.feature-tab-content #tabs-4').children().length < 1) {
		$('.webinar').parent().remove();
	}	
	
	$('.moreinfo').click( function() {
		window.location = '/company/contact-us/contact-sales/';
	});
}); 

// google analytics tracking of form submition
//function onFormSubmit() {
//	var pathname = window.location.pathname;
//	alert(pathname);
//	_gaq._trackPageview('/lead-generated' + pathname);
//}


function dlForm() {


//Salesforce contact form for download pages
	$('#dl-form').dialog('enable');
	$('#dl-form').dialog({
		autoOpen: true,
		resizable: false,
		draggable: false,
		modal: true,
		title: 'Convey Download',
		width: 'auto',
		closeOnEscape: false
	});
	
	//salesforce complete form behavior & client side validation
	
	$('#sf-form').validate();
	$('#sf-form').ajaxForm({
		beforeSubmit: function() { 
           if($('#sf-form').validate().form()==false) {
           	return false;
           }
		},
		
		success: function() {
			$('p.inst').fadeOut();
    		$('#sf-form').fadeOut();
    		$('#webinar').hide();
    		$('.webinarPlayer').show();
    		$('#sf-form-message').delay(500).fadeIn(
    			function() {
    				$('.ui-widget-overlay').delay(1000).fadeOut();
    				$('.ui-dialog').delay(1000).fadeOut(function() { $('#dl-form').dialog('close'); });
    			});
    		return true;
    	}
});
};

function dlForm2() {


//Salesforce contact form for download pages
	$('#dl-form').dialog('enable');
	$('#dl-form').dialog({
		autoOpen:true,
		resizable: false,
		draggable: false,
		modal: true,
		title: 'Convey Download',
		width: '586px',
		closeOnEscape: false
	});
	
	//salesforce complete form behavior & client side validation
	
	$('#sf-form').validate();
	$('#sf-form').ajaxForm({
		beforeSubmit: function() { 
           if($('#sf-form').validate().form()==false) {
           	return false;
           }
		},
		
		success: function() {
			$('p.inst').fadeOut();
    		$('#sf-form').fadeOut();
    		$('#webinar').hide();
    		$('.thank-you').show();
    		$('.form-link').hide();
    		$('#sf-form-message').delay(500).fadeIn(
    			function() {
    				$('.ui-widget-overlay').delay(1000).fadeOut();
    				$('.ui-dialog').delay(1000).fadeOut(function() { $('#dl-form').dialog('close'); });
    			});
    		return true;

    	}
});
};



function secureForm() {
	
	//Secure Salesforce contact form with email for download
	$('#dl-form-secure').dialog('enable');
	$('#download-secure-button').click(function() {
		$('#dl-form-secure').dialog('open');
	});
	$('#dl-form-secure').dialog({
		autoOpen: true,
		resizable: false,
		draggable: false,
		modal: true,
		title: 'Convey Download',
		width: auto,
		closeOnEscape: false
	});
	
	//salesforce complete form behavior & client side validation
	$('#sf-form-secure').validate();
	$('#sf-form-secure').ajaxForm({
		beforeSubmit: function() { 
           if($('#sf-form-secure').validate().form()==false) {
           	return false;
           }
		},
		
		success: function() {
			$('p.inst').fadeOut();
    		$('#sf-form-secure').fadeOut();
    		$('#sf-form-secure-message').delay(500).fadeIn();
    		onFormSubmit();
    		return true;
    	}
	});
	
	$('#sf-form-secure-message #sendclose').click(function() {
    	$('.ui-widget-overlay').fadeOut();
    	$('.ui-dialog').fadeOut(function() { $('#dl-form-secure').dialog('close'); });
	});
	
};

sfHover = function() {
	var sfEls = document.getElementById("topnav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
