$(document).ready(function(){
	// reset all tabs on start
	$('.tabset').find('a.tab').each(function(){
		if($(this).is('.active')){
			$($(this).attr('href')).show();
		} else {
			$($(this).attr('href')).hide();
		}
	});
	$('.tabset').find('a.tab').click(function(){
		$(this).parent().parent().find('a.tab').each(function(){
			$(this).removeClass('active');
			$($(this).attr('href')).hide();
		});
		$(this).addClass('active');
		$($(this).attr('href')).show();
		return false;
	});
	initGall();
	initHGall();
	//$('object').append('<param name="wmode" value="opaque"/>');
	//$('embed').attr('wmode', 'opaque');
	SeedSignup();
	//Hack issues headers
	$('#ctl00_ctl00_IssueTitle ~ br').hide();
	
	//Popup cookie stuff
    if(readCookie('popup') != 'hide')
    {
	    $('#modalPopup').click();
	    createCookie('popup','hide',500);
	}
});

function initGall(){
	$('#header .gallery img:gt(0)').hide();
	var _a = 0;
	$('#header .gallery').each(function(){
		var _hold = $(this);
		var _box = _hold.children('img').hide();
		_box.eq(_a).show();
//		_hold.find('.list .back').click(function(){
//			if(_a > 0) changeEl(_a - 1);
//			else changeEl(_box.length - 1);
//			resetTimer();
//			return false;
//		});
//		_hold.find('.list .next').click(function(){
//			if(_a < _box.length - 1) changeEl(_a + 1);
//			else changeEl(0);
//			resetTimer();
//			return false;
//		});
		function changeEl(_ind){
			_box.eq(_a).css('z-index', 1).fadeOut(800);
			_box.eq(_ind).css('z-index', 2).fadeIn(800);
			_a = _ind;
		}
		
	    function resetTimer(){
	        $(this).parent().stopTime();
	        //$(this).everyTime(4000, "AutoRot", function(){
	        //    if(_a < _box.length - 1) changeEl(_a + 1);
		    //    else changeEl(0);
	        //},0,true);
	    }

	    $(this).everyTime(6000, "AutoRot", function(){
	        if(_a < _box.length - 1) changeEl(_a + 1);
		    else changeEl(0);
	    },0,true);

	});
	
}

function initHGall(){
	var _a = 0;
	$('#PhotoFrame').each(function(){
		var _hold = $(this);
		var _box = _hold.children('.gallery').hide();
		_box.eq(_a).show();
		_hold.find('.cycle .back').click(function(event){
		    event.preventDefault();
			if(_a > 0) changeEl(_a - 1);
			else changeEl(_box.length - 1);
			resetTimer();
			return false;
		});
		_hold.find('.cycle .next').click(function(event){
		    event.preventDefault();
			if(_a < _box.length - 1) changeEl(_a + 1);
			else changeEl(0);
			resetTimer();
			return false;
		});
		function changeEl(_ind){
			_box.eq(_a).css('z-index', 1).fadeOut(800);
			_box.eq(_a).css('display','none');
			_box.eq(_ind).css('z-index', 2).fadeIn(800);
			_a = _ind;
		}
		
	    function resetTimer(){
	        $(this).parent().stopTime("GallRot");
//	        $(this).everyTime(6000, "GallRot", function(){
//	            if(_a < _box.length - 1) changeEl(_a + 1);
//		        else changeEl(0);
//	        },0,true);
	    }

	    $(this).everyTime(6000, "GallRot", function(){
	        if(_a < _box.length - 1) changeEl(_a + 1);
		    else changeEl(0);
	    },0,true);

	});
	
}


function SeedSignup()
{
    var homePageEmail = $('input#EmailSignupInput').val();
    if (homePageEmail != null)
    {
        $('input#EmailSignupInput').val(homePageEmail.replace(/%40/g,'@'));
    }

    var email;
    email = jQuery.url.param('email');
    if(email!=null)
    {
        email = email.replace(/%40/,"@");
        $('input#EmailSubscribe').val(email);
    }
    
    var zip5;
    zip5 = jQuery.url.param('zip5');
    if(zip5!=null)
    {
        $('input[name=zip5]').val(zip5);
    }
    var zip4;
    zip4 = jQuery.url.param('zip4');
    if(zip4!=null)
    {
        $('input[name=zip4]').val(zip4);
    }
}

function EscapeAtSign(element){
    element.value = element.value.replace(/@/g,'%40');
}


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;
}

