jQuery.noConflict();

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
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;
    }
};

/*--- tabs ---*/
function initTabs(){
	var _speed = 500;
	if (jQuery('ul.tabset').length > 0)
	{
		jQuery('ul.tabset').each(function()
		{
			var btn_h = jQuery(this);
			var box_hold = (btn_h.attr('title') && btn_h.attr('title').length > 1)?(jQuery(btn_h.attr('title'))):(false);
			btn_h.removeAttr('title');
			var _btn = jQuery(this).find('a.tab');
			var _a = (_btn.index(_btn.filter('.active:eq(0)')) != -1)?(_btn.index(_btn.filter('.active:eq(0)'))):(0);
			for(var i = 0; i < _btn.length; i++){
				_btn.eq(i).data('box', jQuery(_btn.eq(i).attr('hash')));
				_btn.eq(i).data('box').css({display:'none', opacity: 0}).removeClass('active');
			}
			_btn.removeClass('active').eq(_a).addClass('active').data('box').css({opacity:1, display:'block'}).addClass('active');
			_btn.click(function(){
				changeTab(_btn.index(this));
				return false;
			});
			function changeTab(_ind){
				if(_ind != _a){
					_btn.eq(_a).removeClass('active');
					_btn.eq(_ind).addClass('active');
					if(box_hold) box_hold.stop().height(box_hold.height());
					_btn.eq(_a).data('box').stop().removeClass('active').animate({opacity:0}, _speed, function(){ jQuery(this).hide();});
					_btn.eq(_ind).data('box').stop().show().addClass('active').animate({opacity:1}, _speed);
					if(box_hold) box_hold.animate({height: _btn.eq(_ind).data('box').outerHeight()}, _speed/2, function(){ jQuery(this).height('auto');});
					_a = _ind;
				}
			}
		});
	}
}
/*---- clear inputs ---*/
function clearInputs(){
	jQuery('input:text, input:password, textarea').each(function(){
		var _el = jQuery(this);
		_el.data('val', _el.val());
		_el.bind('focus', function(){
			if(_el.val() == _el.data('val')) _el.val('');
		}).bind('blur', function(){
			if(_el.val() == '') _el.val(_el.data('val'));
		});
	});
}
/*--- popup function ---*/
var gDocLnk;
var gSesDat;
function initPopup()
{
	if(jQuery('#fader').length == 0) 
		jQuery('body').append('<div id="fader"></div>');

	var _fader = jQuery('#fader');
	_fader.hide();
	var _popup = false;
	
	jQuery('a.with-popup').each(function()
	{
		var _el = this;
		if(_el.hash && _el.hash.length > 1)
		{
			_el._popup = jQuery(_el.hash);
			if(_el._popup.length)
			{
				_el.onclick = function()
				{
					if (jQuery.cookie('ERTRegFrm'))
					{
						var sDocLnk = "http://" + location.host + jQuery(this).attr("docref") + 'DownloadSecured';
						window.open(sDocLnk,"_viewer");
					}
					else if(_el._popup)
					{
						if(_popup) _popup.hide();
						_popup = _el._popup;

						// IMM 060710 Set document to dl
						gDocLnk = jQuery(this).attr("docref");
						//jQuery("#doc_dl").val(jQuery(this).attr("docref"));

						// IMM 060410 Assign ao_f and ao_d here...
						jQuery("#ao_f").val(jQuery(this).attr("formid"));
						jQuery("#ao_d").val(jQuery(this).attr("dplnum"));

						showPopup();
					}
					return false;
				}
				_el._popup.find('a.close').click(function()
				{
					hidePopup();
					return false;
				});
			}
		}
	});
	// IMM 060710 Submit registration form information
	jQuery("#Form_SecuredContentForm_action_ProcessSecuredContentForm").click( function()
	{
		// Perform form validation here
		Err = false;
		jQuery(".signup-form .required").remove();
		FstNme = jQuery.trim(jQuery("#Form_SecuredContentForm_FirstName").val());
		LstNme = jQuery.trim(jQuery("#Form_SecuredContentForm_LastName").val());
		EmlAdr = jQuery.trim(jQuery("#Form_SecuredContentForm_Email").val());
		CmpNme = jQuery.trim(jQuery("#Form_SecuredContentForm_Company").val());
		CntNme = jQuery("#Form_SecuredContentForm_Country").val();
		
		EmlPtn = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

		if ((FstNme == "") || (FstNme == "First Name:"))
		{
			Err = true;
			jQuery("#Form_SecuredContentForm_FirstName").after("<div class=\"required\">Field is required</div>");
		}
		if ((LstNme == "") || (LstNme == "Last Name:"))
		{
			Err = true;
			jQuery("#Form_SecuredContentForm_LastName").after("<div class=\"required\">Field is required</div>");
		}
		if ((EmlAdr == "") || (EmlAdr == "E-mail:"))
		{
			Err = true;
			jQuery("#Form_SecuredContentForm_Email").after("<div class=\"required\">Field is required</div>");
		}
		else if (!EmlPtn.test(EmlAdr))
		{
			Err = true;
			jQuery("#Form_SecuredContentForm_Email").after("<div class=\"required\">Enter a valid e-mail</div>");
		}
		if ((CmpNme == "") || (CmpNme == "Company:"))
		{
			Err = true;
			jQuery("#Form_SecuredContentForm_Company").after("<div class=\"required\">Field is required</div>");
		}
		if (CntNme == "default")
		{
			Err = true;
			jQuery("#Form_SecuredContentForm_Country").after("<div class=\"required\">Field is required</div>");
		}

		if (!Err)
		{
			// IMM 060710 Ajax submit form elements...
			FrmDat = jQuery('#Form_SecuredContentForm').serialize();
			jQuery.ajax(
			{
				url: location.pathname + 'SecuredContentForm', 
				type: 'POST',
				data: jQuery('#Form_SecuredContentForm').serialize(),
				success: function(msg) 
				{
					if (msg=="SUCCESS")
					{
						jQuery.cookie('ERTRegFrm',FrmDat);
						var sDocLnk = "http://" + location.host + gDocLnk + 'DownloadSecured';
						window.open(sDocLnk,"_viewer");
					}
					else
						 alert("A network problem prevented this form\nfrom submitting, please try again.\n\nmsg=[" + msg + "]");
				},
				complete: function()
				{
					hidePopup();
				}
			}); 
		}

		return false;
	});
	_fader.click(function()
	{
		hidePopup();
		return false;
	});
	
	jQuery(document).keydown(function(e){
		if(!e)evt = window.event;
		if(e.keyCode == 27) hidePopup();
	});
	/*--- ---*/
	function showPopup()
	{
		_popup.css({top: jQuery(window).scrollTop()});
		if(_fader.is(':hidden'))
		{
			_fader.css({
				opacity: 0,
				height: initH(),
				display:'block'
			});
			_fader.fadeTo(400, 0.5, function(){
				_show();
			});
		}
		else
		{
			_fader.height(initH());
			_show();
		}
	}
	/*--- ---*/
	function _show()
	{
		if(_popup)
		{
			if(jQuery.browser.msie) _popup.show();
			else _popup.fadeIn(200, function()
			{
				if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
			});
			_popup.css({top: jQuery(window).scrollTop()+ (jQuery(window).height() - _popup.outerHeight())/2});
			if(initH() < _popup.outerHeight()+_popup.offset().top) 
				_fader.height(_popup.outerHeight()+_popup.offset().top);
			initPopupGall();
		}
	}
	/*--- ---*/
	function hidePopup(){
		if(_popup){
			if(jQuery.browser.msie){
				_popup.hide();
				_fader.fadeOut(200);
				_popup = false;
			}
			else{
				_popup.fadeOut(400, function(){
					_fader.fadeOut(200);
					_popup = false;
				});
			}
		}
	}
	/*--- ---*/
	function initH()
	{
		var _h = jQuery('#wrapper').outerHeight();
		if(_h < jQuery(window).height()) _h = jQuery(window).height();
		if(_h < jQuery('body').height()) _h = jQuery('body').height();
		return _h;
	}
}
/*--- popup gallery ----*/
function initPopupGall(){
	var fade_speed = 500; //in ms
	var move_speed = 600; //in ms
	jQuery('#device-box').each(function(){
		if(!this.gall_f && this.offsetWidth > 0){
			this.gall_f = true;
			var _hold = jQuery(this);
			var list_hold = _hold.find('div.gallery-box div.gallery > ul');
			var btn_prev = _hold.find('div.gallery-box a.btn-prev');
			var btn_next = _hold.find('div.gallery-box a.btn-next');
			var img_hold = _hold.find('div.img-box');
			var _btn = list_hold.find('a');
			var _a = (_btn.index(_btn.filter('.active:eq(0)')) != -1)?(_btn.index(_btn.filter('.active:eq(0)'))):(0);
			var hold_w = list_hold.parent().width();
			var list_w = list_hold.children().outerWidth(true)*list_hold.children().length;
			var _step = hold_w;
			var _m = 0;
			
			_btn.removeClass('active').eq(_a).addClass('active');
			_btn.eq(_a).data('img', jQuery('<img src="'+_btn.eq(_a).attr('href')+'" alt="'+_btn.eq(_a).attr('rel')+'"/>'));
			_btn.eq(_a).data('img').addClass('active').css('opacity', 1).appendTo(img_hold);
			
			_btn.click(function(){
				changeEl(_btn.index(this));
				return false;
			});
			btn_prev.click(function(){
				moveList(false);
				return false;
			});
			btn_next.click(function(){
				moveList(true);
				return false;
			});
			
			var fade_f = true;
			function changeEl(_ind){
				if(fade_f && _ind != _a){
					fade_f = false;
					img_hold.stop().height(img_hold.height());
					_btn.eq(_a).removeClass('active');
					_btn.eq(_ind).addClass('active');
					_btn.eq(_a).data('img').removeClass('active').animate({opacity: 0}, {queue:false, duration: fade_speed});
					if(_btn.eq(_ind).data('img')){
						_btn.eq(_ind).data('img').addClass('active').animate({opacity: 1}, {queue:false, duration: fade_speed});
						img_hold.animate({height: _btn.eq(_ind).data('img').height()}, fade_speed/2, function(){ jQuery(this).height('auto');});
						_a = _ind;
						fade_f = true;
					}
					else{
						_btn.eq(_ind).data('img', jQuery('<img />'));
						_btn.eq(_ind).data('img').load(function(){
							_btn.eq(_ind).data('img').addClass('active').animate({opacity: 1}, {queue:false, duration: fade_speed});
							img_hold.animate({height: _btn.eq(_ind).data('img').height()}, fade_speed/2, function(){ jQuery(this).height('auto');});
							_a = _ind;
							fade_f = true;
						});
						_btn.eq(_ind).data('img').css('opacity', 0);
						img_hold.append(_btn.eq(_ind).data('img'));
						_btn.eq(_ind).data('img').attr('src', _btn.eq(_ind).attr('href')).attr('alt', _btn.eq(_ind).attr('rel'));
					}
				}
			}
			function moveList(_f){
				if(_f){
					if(list_w - _m > hold_w) _m += _step;
					else _m = 0;
				}
				else{
					if(_m > 0) _m -= _step;
					else _m = Math.ceil((list_w - hold_w)/_step)*_step;
				}
				list_hold.animate({left:-_m},{queue:false, duration: move_speed});
			}
		}
	});
}
/*--- gallery ---*/
var gMveSpd;	// Movement speed
var gAnmSpd;	// Animation speed
var gSldTme;	// Number of seconds between autoSlides
var gAutHdl;	// Handle for setTimeout
var gCarBox;	// Carousel box container
var gBtnPrv;	// Previous button
var gBtnNxt;	// Next button
var gCarLst;	// Carousel unordered list
var gULChld;	// Children of UL
var gLstWdt;	// Total number of visible panels
var gCarWdt;	// Width of carousel panel
var gAnmStp;	// Animation movement size (3 per panel)
var gAnmCur;	// Current animation step
var gMveF;		// When false container is sliding and cannot process mouse over panel events
var gAnmF;		// When true, can slide container - when false mouse is over a panel
var gOffA;		// Tracks the current panel the mouse is over
var gVBeg;		// Tracks the beginning panel # when container has slid

var gADir = 1;	// +1=Slide-left, -1=Slide-right

function autoMove()
{
	if (gAnmF)
	{
		gMveF = false;

		if (gADir>0)
		{
			if(gLstWdt - gAnmCur - 100 <= gCarWdt) 
			{
				gADir=-1;
				gVBeg--;
			}
			else
				gVBeg++;
		}
		else
		{
			if (gAnmCur<=0)
			{
				gADir=1;
				gVBeg++;
			}
			else
				gVBeg--;
		}

		gAnmCur += (gAnmStp*gADir);

		gCarLst.stop().animate({left:-gAnmCur}, gMveSpd, function(){ gMveF = true;});
	}
}
function moveList(_f)
{
	if(gAnmF)
	{
		gMveF = false;
		if(_f)
		{
			if(gLstWdt - gAnmCur - 100 > gCarWdt) 
			{
				gAnmCur+=gAnmStp;
				gVBeg++;
			}
		}
		else
		{
			if (gAnmCur>0)
			{
				gAnmCur-=gAnmStp;
				gVBeg--;
			}
		}

		gCarLst.stop().animate({left:-gAnmCur}, gMveSpd, function(){ gMveF = true;});
	}
}

function clearDrops()
{
	gULChld.eq(gVBeg).removeClass('drop-left');
	gULChld.eq(gVBeg+1).removeClass('drop-center');
	gULChld.eq(gVBeg+2).removeClass('drop-right');
}

function setDrops()
{
	gULChld.eq(gVBeg).addClass('drop-left');
	gULChld.eq(gVBeg+1).addClass('drop-center');
	gULChld.eq(gVBeg+2).addClass('drop-right');
}

function showEl(_ind)
{
	if(gMveF)
	{
		gAnmF = false;
		// Determine offset of center panel
		iCenO = (_ind==gVBeg ? 50 : (_ind==gVBeg+2 ? -50 : 0));

		gCarBox.addClass('carousel-hovered');
		gULChld.eq(_ind).addClass('active');
		gULChld.eq(gVBeg+1).data('box').parent().css('left',iCenO+'px');
		if(jQuery.browser.msie)
		{
			gULChld.eq(_ind).css('zIndex', 10);
			gULChld.eq(_ind).data('box').hide();
			gULChld.eq(_ind).data('sub_box').show();
		}
		else
		{
			gULChld.eq(_ind).data('box').stop().animate({opacity:0}, gAnmSpd, function(){ jQuery(this).hide();});
			gULChld.eq(_ind).data('sub_box').stop().show().animate({opacity:1}, gAnmSpd);
		}
		gOffA = _ind;
	}
}

function hideEl()
{
	if(gOffA != -1)
	{
		if(jQuery.browser.msie)
		{
			gULChld.eq(gOffA).data('sub_box').hide();
			gULChld.eq(gOffA).data('box').show();
			gCarBox.removeClass('carousel-hovered');
			gULChld.eq(gOffA).removeClass('active').css('zIndex', 1);
			gAnmF = true;
		}
		else
		{
			gULChld.eq(gOffA).removeClass('active').addClass('sub-active');
			gULChld.eq(gOffA).data('sub_box').stop().animate({opacity:0}, gAnmSpd, function(){ jQuery(this).hide(); });
			gULChld.eq(gOffA).data('box').stop().show().animate({opacity:1}, gAnmSpd, function()
			{
				jQuery(this).parent().removeClass('sub-active');
				if(gOffA == -1)
				{
					gCarBox.removeClass('carousel-hovered');
					gAnmF = true;
				}
			});
		}
		// Slide center panel back to center
		gULChld.eq(gVBeg+1).data('box').parent().css('left','0px');
		gOffA = -1;
	}
}

function autoSlide()
{
	clearDrops();
	autoMove();
	setDrops();
	gAutHdl = setTimeout("autoSlide()", gSldTme*1000);
}

function initGall()
{
	jQuery('div.carousel-box').each(function()
	{
		gMveSpd = 1200;
		gAnmSpd = 400;
		gSldTme = 8;
		gCarBox = jQuery(this);
		gBtnPrv = gCarBox.find('a.btn-prev');
		gBtnNxt = gCarBox.find('a.btn-next');
		gCarLst = gCarBox.find('div.carousel > ul');
		gULChld = gCarLst.children();
		gLstWdt = gULChld.outerWidth(true) * gULChld.length;
		gCarWdt = gCarLst.parent().width();
		//gAnmStp = gULChld.outerWidth()*3;
		gAnmStp = gULChld.outerWidth();
		gAnmCur = 0;

		gMveF = true;
		gAnmF = true;
		gOffA = -1;
		gVBeg = 0;

		for(var i = 0; i < gULChld.length; i++)
		{
			gULChld.eq(i).data('box', gULChld.eq(i).children('div.small-box') ).data('sub_box', gULChld.eq(i).children('div.enlarged-box'));
			gULChld.eq(i).data('sub_box').hide();
			if(!jQuery.browser.msie) gULChld.eq(i).data('sub_box').css('opacity', 0);
		}

		setDrops();

		gBtnPrv.click(function()
		{
			clearTimeout(gAutHdl);
			clearDrops();
			moveList(false);
			setDrops();
			gAutHdl = setTimeout("autoSlide()", gSldTme*1000);
			return false;
		});
		
		gBtnNxt.click(function()
		{
			clearTimeout(gAutHdl);
			clearDrops();
			moveList(true);
			setDrops();
			gAutHdl = setTimeout("autoSlide()", gSldTme*1000);
			return false;
		});

		gULChld.mouseenter(function()
		{
			showEl(gULChld.index(this));
		}).mouseleave(function()
		{
			hideEl();
		});

		gAutHdl = setTimeout("autoSlide()", gSldTme*1000);
	});	
}

/*--- form validation ---*/
function formValidation(){
	var _emailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	jQuery('#signup-box form').each(function(){
		var _hold = jQuery(this);
		var _fields = _hold.find('.must-valid');
		_hold.submit(function(){
			if(!_valid()) return false;
		});
		for(var i = 0; i < _fields.length; i++){
			_fields.eq(i).data('val', _fields.eq(i).val());
		}
		
		function _valid(){
			var _f = true;
			_fields.parents('div.row').removeClass('error');
			for(var i = 0; i < _fields.length; i++){
				if(_fields.eq(i).attr('tagName').toLowerCase() == 'select'){
					if(_fields.eq(i).val() == 'default'){
						_fields.eq(i).parents('div.row:eq(0)').addClass('error');
						_f = false;
					}
				}
				else if(_fields.eq(i).hasClass('email-field')){
					if(!_emailReg.test(_fields.eq(i).val()) || _fields.eq(i).val() == _fields.eq(i).data('val') || _fields.eq(i).val() == ''){
						_fields.eq(i).parents('div.row:eq(0)').addClass('error');
						_f = false;
					}
				}
				else{
					if(_fields.eq(i).val() == _fields.eq(i).data('val') || _fields.eq(i).val() == ''){
						_fields.eq(i).parents('div.row:eq(0)').addClass('error');
						_f = false;
					}
				}
			}
			return _f;
		}
	});
}
jQuery(document).ready(function()
{
	initTabs();
	clearInputs();
	initPopup();
	initPopupGall();
	initGall();
	formValidation();
	jQuery("#ticker").newsticker();	// Enable news ticker
});


