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


jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function checkCookie()
{
username=getCookie('username');
if (username!=null && username!="")
  {
  alert('Welcome again '+username+'!');
  }
else
  {
  username=prompt('Please enter your name:',"");
  if (username!=null && username!="")
    {
    setCookie('username',username,365);
    }
  }
}


/**
 * Compact labels plugin
 */
(function($){$.fn.compactize=function(){return this.each(function(){var label=$(this),input=$('#'+label.attr('for'));input.focus(function(){label.hide();}).blur(function(){if(input.val()===''){label.show();}});window.setTimeout(function(){if(input.val()!==''){label.hide();}},50);});};})(jQuery);

/*
 * hrefID jQuery extention - returns a valid #hash string from link href attribute in Internet Explorer
 */
(function($){$.fn.extend({hrefId:function(){return $(this).attr('href').substr($(this).attr('href').indexOf('#'));}});})(jQuery);

/*
 * Scripts
 *
 */
jQuery(function($) {
 
	var Engine = {
		utils : {
			links : function(){
				$('a[rel*=external]').click(function(e){
					e.preventDefault();
					window.open($(this).attr('href'));						  
				});
			},
			mails : function(){
				$('a[href^=mailto:]').each(function(){
					var mail = $(this).attr('href').replace('mailto:','');
					var replaced = mail.replace('/at/','@');
					$(this).attr('href','mailto:'+replaced);
					if($(this).text() == mail) {
						$(this).text(replaced);
					}
				});
			},
			labels : function(){
				$('#top form label, .free-membership label').compactize();
			}
		},
		enhancements : {
		
			featured : function(){
				$('#featured-a').each(function(){
					
					var triggers = $(this).find('ul.nav li a');
					var sections = $(this).find('div.section');
					
					triggers.mouseenter(function(){
						sections.addClass('offset');
						triggers.removeClass('selected');
						$(this).addClass('selected');
						$($(this).attr('href')).removeClass('offset');
					}).click(function(e){
						e.preventDefault();
					}).eq(0).triggerHandler('mouseenter');					
				});
				
				$('#featured-a div.section div.item').click(function(e){
					window.location = $(this).find('a').attr('href');
				});
			}
			
		},
		fixes : {
			lists : function(){
				$('ul li:first-child').addClass('first');
				$('ul li:last-child').addClass('last');
			}
		},
		
		tweaks : {
		
			addClass : function(){
				$("#nav_920195").addClass("col-a");
				$("#nav_920207").addClass("col-b");
			},
			
			sideNavShow : function(){
				$("#sidenav ul ul").hide();
				$("#sidenav ul li.selected").find("ul").show();	
			},
			
			colorBoxInit : function(){
				$(".colorBoxForm").click(function(){
					$(this).colorbox({width:"600px", height:"430px", inline:true, href:"#download-reg-form-container"});
				});
				
				$(".homeVideoPop").click(function(){
					$(this).colorbox({width:"590px", height:"430px", inline:true, href:"#homeVideoPop"});
				});
				$(".colorBoxDPMS").click(function(){
					$(this).colorbox({width:"600px", height:"430px", inline:true, href:"#download-reg-form-container"});
				});
				
			},
			
			freeWhitePaperLink : function(){
				$(".pdfLink").attr("href",$(".literature-container .name a").attr("href"));	
			},
			
			cookieLovin : function(){
			
			var cookieAction = getCookie('TL_UpRight'); // get the cookie
			//var logOutHtml = "<a href='#' class='logOutBtn'>Log Out</a>"; // html for logout btn
	
				if(cookieAction){
					$.cookie('cookieFound','1');
						var cookieFound = $.cookie('cookieFound');
						var reloadedVal = $.cookie('reloadedVal');
						
						
						
							if(cookieFound && reloadedVal != "1"){ // if we find a cookie reload and set reload cookie
									location.reload(true);
									$.cookie('reloadedVal','1');
									//$("p.login").html(logOutHtml);
									
									// tweak menu accordingly
									$("#nav_920159 li:nth-child(2)").hide();
									$("#nav_920159 li:nth-child(3)").hide();
									$("#nav_920159 li:nth-child(4)").show();
									$("#nav_920159 li:nth-child(5)").show();
									
									
								}
								
							if(cookieFound && reloadedVal === "1"){ // if we found cookie and have already reloaded
								
									//$("p.login").html(logOutHtml);
									
									// tweak menu accordingly
									$("#nav_920159 li:nth-child(2)").hide();
									$("#nav_920159 li:nth-child(3)").hide();
									$("#nav_920159 li:nth-child(4)").show();
									$("#nav_920159 li:nth-child(5)").show();
									
								}
				
					
					$(".cookieVal").html(cookieAction); // for test page
				}
				
				$("a.clearCookie").click(function(){
					alert('Clearing TL_UpRight cookie and heading home.');
					$.cookie('TL_UpRight','');
					$.cookie('reloadedVal','');
					window.location.href="/";
				});
				
				$("a.setCookie").click(function(){
					alert('Setting TL_UpRight = 1 cookie and heading to home page');
					$.cookie('TL_UpRight','1');
					$.cookie('reloadedVal','1');
					window.location.href="/";
				});
				
				$("a.setSrc").click(function(){
					alert('Setting src = 1 cookie and heading to home page');
					$.cookie('src','1');
					window.location.href="/";
				});
				
				$("a.clearSrc").click(function(){
					alert('Clearing src = 1 cookie and heading to home page');
					$.cookie('src','');
					window.location.href="/";
				});
				
				
				// SRC 
				// look for src in url
				if(document.location.href.indexOf("src") != -1){
					
				var srcValue = getVar('src');
				//alert(srcValue);
				$.cookie('srcCookie',srcValue);
				
					
				$("input#CAT_Custom_150697").val(srcValue); // Contact form
				$("input#CAT_Custom_150698").val(srcValue); // Free Membership form
				$("input#CAT_Custom_150699").val(srcValue); // Free White Paper form
				
					
				}
				
				var srcCookie = getCookie('srcCookie'); // get the cookie
				
				 if(srcCookie){
					$("input#CAT_Custom_150697").val(srcCookie); // Contact form
					$("input#CAT_Custom_150698").val(srcCookie); // Free Membership form
					$("input#CAT_Custom_150699").val(srcCookie); // Free White Paper form
				}
				
				
				
			
			}
		
		}// end tweaks
	};

	
	Engine.utils.links();
	Engine.utils.mails();
	Engine.utils.labels();
	
	Engine.enhancements.featured();
	Engine.fixes.lists();
	
	Engine.tweaks.addClass();
	Engine.tweaks.sideNavShow();
	
	Engine.tweaks.colorBoxInit();
	Engine.tweaks.freeWhitePaperLink();
	Engine.tweaks.cookieLovin();


		

	
	
});