if (window.addEventListener) window.addEventListener("load", initGallery, false);
else if (window.attachEvent && !window.opera) window.attachEvent("onload", initGallery);

function initGallery(){
	var $list = $('#carusel ul');
	var $listItem = $('#carusel ul li');
	var $holder = $('#carusel .gallery div');
	var $activeEl = 2; // second element is active
	var $timerPrev;
	var $timerNext;
	var $duration = 300; // effect duration
	var $autoTime = 7000; // each 10 seconds autoslide
	var $auto;
	var $anime;

	$listWidth = $list.width();
	$listItemWidth = 89;

	_btPrev = $('#carusel a.prev');
	_btNext = $('#carusel a.next');

	// remove active classes
	function removeActiveClass(){
		$listItem.each(function(){
			$(this).removeClass('active');
		});
		$('#carusel ul.before li').each(function(){
			$(this).removeClass('active');
		});
		$('#carusel ul.after li').each(function(){
			$(this).removeClass('active');
		});
	}
	
	// set the third link active
	$listItem.eq($activeEl).addClass('active');

	// clone list
	$listBefore = $list.clone(true).insertBefore($list).addClass('before');
	$listAfter = $list.clone(true).insertAfter($list).addClass('after');

	// set default margin
	$holder.css('marginLeft',-$listWidth+'px');

	// click 'prev' button
	_btPrev.click(function(){
		removeActiveClass();

		exElem = $activeEl;

		if ($activeEl>0) {
			$activeEl--;
		}else{
			$activeEl = $listItem.length-1;
			$holder.css('marginLeft',(-2*$listWidth + 2*$listItemWidth)+'px');
		}

		cuElem = $activeEl;

		slideShow(exElem,cuElem);

		clearTimeout($anime);
		$anime = setTimeout(function(){
			$holder.animate({
				'marginLeft': '+='+$listItemWidth
			},$duration);
		},10);

		clearTimeout($timerPrev);
		$timerPrev = setTimeout(function(){
			$listItem.eq($activeEl).addClass('active');
		},$duration);

		return false;
	});

	// click 'next' button
	_btNext.click(function(){
		removeActiveClass();

		exElem = $activeEl;

		if ($activeEl<$listItem.length-1) {
			$activeEl++;
		}else{
			$activeEl = 0;
			$holder.css('marginLeft',(-1*$listWidth + 3*$listItemWidth) + 'px');
		}

		cuElem = $activeEl;

		slideShow(exElem,cuElem);

		clearTimeout($anime);
		$anime = setTimeout(function(){
			$holder.animate({
				'marginLeft': '-='+$listItemWidth
			},$duration);
		},10);

		clearTimeout($timerNext);
		$timerNext = setTimeout(function(){
			$listItem.eq($activeEl).addClass('active');
		},$duration);

		return false;
	});
	
	// click any image preview
	$listItem.click(function(){
		if (!$(this).is('.active')) {
			removeActiveClass();

			exElem = $activeEl;

			var _index = $listItem.index(this);
			if (_index - $activeEl == 1) {
				clearTimeout($anime);
				$anime = setTimeout(function(){
					$holder.animate({
						'marginLeft': '-='+$listItemWidth
					},$duration);
				},10);
				$activeEl++;
			}
			if (_index - $activeEl == 2) {
				clearTimeout($anime);
				$anime = setTimeout(function(){
					$holder.animate({
						'marginLeft': '-='+2*$listItemWidth
					},$duration);
				},10);
				$activeEl = $activeEl+2;
			}
			if (_index - $activeEl == -1) {
				clearTimeout($anime);
				$anime = setTimeout(function(){
					$holder.animate({
						'marginLeft': '+='+$listItemWidth
					},$duration);
				},10);
				$activeEl--;
			}
			if (_index - $activeEl == -2) {
				clearTimeout($anime);
				$anime = setTimeout(function(){
					$holder.animate({
						'marginLeft': '+='+2*$listItemWidth
					},$duration);
				},10);
				$activeEl = $activeEl-2;
			}

			cuElem = $activeEl;

			slideShow(exElem,cuElem);

			clearTimeout($timerNext);
			$timerNext = setTimeout(function(){
				$listItem.eq($activeEl).addClass('active');
			},$duration);
		}

		return false;
	});
	$listBefore.find('li').click(function(){
		removeActiveClass();
		var _index = $listBefore.find('li').index(this);
		if(_index==3) {
			exElem = $activeEl;
			$holder.css('marginLeft',(-2*$listWidth + 2*$listItemWidth)+'px');
			clearTimeout($anime);
			$anime = setTimeout(function(){
				$holder.animate({
					'marginLeft': '+='+2*$listItemWidth
				},$duration);
			},10);
			$activeEl = $listItem.length-2;
			cuElem = $activeEl;
			slideShow(exElem,cuElem);
		}
		if(_index==4) {
			exElem = $activeEl;
			$holder.css('marginLeft',(-2*$listWidth + 2*$listItemWidth)+'px');
			clearTimeout($anime);
			$anime = setTimeout(function(){
				$holder.animate({
					'marginLeft': '+='+$listItemWidth
				},$duration);
			},10);
			$activeEl = $listItem.length-1;
			cuElem = $activeEl;
			slideShow(exElem,cuElem);
		}
		clearTimeout($timerNext);
		$timerNext = setTimeout(function(){
			$listItem.eq($activeEl).addClass('active');
		},$duration);
	});
	$listAfter.find('li').click(function(){
		removeActiveClass();
		var _index = $listAfter.find('li').index(this);
		if(_index==0) {
			exElem = $activeEl;
			$holder.css('marginLeft',(-1*$listWidth + 3*$listItemWidth)+'px');
			clearTimeout($anime);
			$anime = setTimeout(function(){
				$holder.animate({
					'marginLeft': '-='+$listItemWidth
				},$duration);
			},10);
			$activeEl = 0;
			cuElem = $activeEl;
			slideShow(exElem,cuElem);
		}
		if(_index==1) {
			exElem = $activeEl;
			$holder.css('marginLeft',(-1*$listWidth + 3*$listItemWidth)+'px');
			clearTimeout($anime);
			$anime = setTimeout(function(){
				$holder.animate({
					'marginLeft': '-='+2*$listItemWidth
				},$duration);
			},10);
			$activeEl = 1;
			cuElem = $activeEl;
			slideShow(exElem,cuElem);
		}
		clearTimeout($timerNext);
		$timerNext = setTimeout(function(){
			$listItem.eq($activeEl).addClass('active');
		},$duration);
	});

	$auto = setTimeout(function(){
		autoSlide();
	},$autoTime);

	// autoslide
	function autoSlide(){
		removeActiveClass();

		exElem = $activeEl;

		if ($activeEl<$listItem.length-1) {
			$activeEl++;
		}else{
			$activeEl = 0;
			$holder.css('marginLeft',(-1*$listWidth + 3*$listItemWidth) + 'px');
		}
		clearTimeout($anime);
		$anime = setTimeout(function(){
			$holder.animate({
				'marginLeft': '-='+$listItemWidth
			},$duration);
		},10);

		cuElem = $activeEl;
		slideShow(exElem,cuElem);

		clearTimeout($timerNext);
		$timerNext = setTimeout(function(){
			$listItem.eq($activeEl).addClass('active');
		},$duration);

		$auto = setTimeout(function(){
			autoSlide();
		},$autoTime);
	}

	$('#carusel').hover(function(){
		clearTimeout($auto);
	},function(){
		$auto = setTimeout(function(){
			autoSlide();
		},$autoTime);
	});

	// slideshow
	function slideShow(_exElem,_cuElem){
		$('.slideshow-area .item').each(function(){
			$(this).css({
				'zIndex':'1',
				'opacity':'1'
			});
		});
		$('.slideshow-area .item').eq(_exElem).css('zIndex','3');
		$('.slideshow-area .item').eq(_cuElem).css('zIndex','2');
		$('.slideshow-area .item').eq(_exElem).animate({
			'opacity':'0'
		},1000);
		$('.slideshow-area .item').eq(_exElem).css('zIndex','1');
	}
}

$(document).ready(function(){
    popupStuff();
    Seeding();
});

function popupStuff()
{
//    $('#popupTable td:even').wrapInner('<div class="cor1">');
//    $('#popupTable td:odd').wrapInner('<div class="cor2">');
//    $('#popupTable td .cor2:eq(1)').removeClass('cor2');
    $('#popupTable td:odd').addClass('cor2');
    $('#popupTable td.cor2:eq(1)').addClass('logoMe');
    if(readCookie('samesession') != 'true')
    {
	    $('#ModalPopup').click();
	    createCookie('samesession','true');
	}
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}


/* --- Seed Contact with Zips & Email Signup with Name/Addy --- */
function Seeding()
{
    var formID = jQuery.url.param('ID');
    if(formID == '360')
    {
        var email = jQuery.url.param('email');
        email = email.replace(/%40/,"@");
        var emailBox = $('input[id*=EmailControl]');
        emailBox.val(email);
        if(emailBox.val() != '')
        {
            //emailBox.parent().hide();
        }    
    }
}

