var $jQ = jQuery.noConflict();
var lastbutton = null;

function removeUsername(){
	if (document.login.username.value == 'username') {
		document.login.username.value = '';
	}
}

function removePassword(){
	if (document.login.password.value == 'password') {
		document.login.password.value = '';
	}
}

function showPrev(){
	$jQ('#previousAddress').slideDown();
}

function hidePrev(){
	$jQ('#previousAddress').slideUp();
}

function showLoader() {
	$jQ('#loginLoading').show();
}

function showSpinner() {
	$jQ('#dataLoading').show();
}

function showHistorical() {
	$jQ("#tabHistorical").removeClass("on");
	$jQ("#tabToday").addClass("on");
	$jQ('#tooltip').hide();
	$jQ('#placeholder').hide();
	$jQ('#placeholder2').css('display', 'block');
	$jQ("#fullDateSelector").show();
}

function showToday() {
	$jQ("#tabHistorical").addClass("on");
	$jQ("#tabToday").removeClass("on");
	$jQ('#tooltip').hide();
	$jQ("#fullDateSelector").hide();
	$jQ('#placeholder2').hide();
	$jQ('#placeholder').css('display', 'block');
}

function openAccount() {
	if ($jQ("#application").length > 0) {
		removeApp($jQ("#application"),lastbutton);
	}
	$jQ("#proAppSpinner").show();
	$jQ("div#header").next().after('<div id="application"></div>');
	reg = $jQ("#application");
	button = $jQ("#promoButtons p.large a");
	button.addClass("open");
	reg.load('/forms/show/signup',function(){
		$jQ("#proAppSpinner").hide();
		// form submission needs to be also done with ajax
	});
	lastbutton = button;
	wtslog('al195449','3','http','Open Account Form - Open','event_track');
	return false;
}

function openRegistration() {
	if ($jQ("#registration").length > 0) {
		removeApp($jQ("#registration"),lastbutton);
	}
	$jQ("#proAppSpinner").show();
	$jQ("div#header").next().after('<div id="registration"></div>');
	reg = $jQ("#registration");
	button = $jQ("#promoButtons p a");
	button.addClass("open");
	reg.load('/forms/show/application',function(){
		$jQ("#proAppSpinner").hide();
		// form submission needs to be also done with ajax
	});
	lastbutton = button;
	wtslog('al195449','3','http','Registration Form - Open','event_track');
	return false;
}

$jQ(document).ready(function(){
	
	// registration button via ajax example
	$jQ("#promoButtons p").not(".large").find("a").click(function(){
		// check if we have a registration form
		if ($jQ("#application").length > 0) {
			removeApp($jQ("#application"),lastbutton);
			wtslog('al195449','3','http','Registration Form - Close','event_track');
		}
		var button = $jQ(this);
		var reg = $jQ("#registration");
		
		if (reg.length > 0) {
			removeReg(reg,lastbutton);
		} else {
			wtslog('al195449','3','http','Registration Form - Open','event_track');
			$jQ("#proRegSpinner").show();
			$jQ("div#header").next().after('<div id="registration"></div>');
			reg = $jQ("#registration");
			button.addClass("open");
			reg.load('/forms/show/application',function(){
				//reg.find("form").hide();
				//reg.find("form").slideDown(100);
				precacheImg();
				$jQ("#proRegSpinner").hide();
				// form submission needs to be also done with ajax
			});
			lastbutton = button;
		}
		return false;
	});
	
	
	// open account button via ajax example
	$jQ("#promoButtons p.large").find("a").click(function(){
		// check if we have a registration form
		var button = $jQ(this);
		if ($jQ("#registration").length > 0) {
			removeReg($jQ("#registration"),lastbutton);
			wtslog('al195449','3','http','Open Account Form - Close','event_track');
		}
		var reg = $jQ("#application");
		if (reg.length > 0) {
			removeApp(reg,lastbutton);
		} else {
			wtslog('al195449','3','http','Open Account Form - Open','event_track');
			$jQ("#proAppSpinner").show();
			$jQ("div#header").next().after('<div id="application"></div>');
			reg = $jQ("#application");
			button.addClass("open");
			reg.load('/forms/show/signup',function(){
				//reg.find("form").hide();
				//reg.find("form").slideDown(100);
				precacheImg();
				$jQ("#proAppSpinner").hide();
				// form submission needs to be also done with ajax
			});
			lastbutton = button;
		}
		return false;
	});

	// for submit buttons
	$jQ.each($jQ(".precache"),function(){ // "-over"
		overState = this.src;
		overStateFileName = overState.substring(0,overState.lastIndexOf("."));
		overStateFileExtension = overState.substring(overState.lastIndexOf("."),overState.length);
		overStateFullFilename = overStateFileName+"-over"+overStateFileExtension;
		$jQ.preloadImages(this.src, overStateFullFilename);
		$jQ(this).hover(
			function(){
				overState = this.src;
				overStateFileName = overState.substring(0,overState.lastIndexOf("."));
				overStateFileExtension = overState.substring(overState.lastIndexOf("."),overState.length);
				overStateFullFilename = overStateFileName+"-over"+overStateFileExtension;
				this.src = overStateFullFilename;
			}
			,
			function(){
				outState = this.src;
				outStateFileName = outState.substring(0,outState.lastIndexOf("-over"));
				outStateFileExtension = outState.substring(outState.lastIndexOf("."),outState.length);
				outStateFullFilename = outStateFileName+outStateFileExtension;
				this.src = outStateFullFilename;
			}
		)		
	});
	
	// hide the extra hp content 
	var hpcontent = $jQ(".homeMainArea .hpButton .content");
	if (hpcontent.length > 0) {
		hpcontent.hide();
		hpcontent.parent().parent().removeClass("open");
	}
	$jQ(".hpButton H2 A").click(function() {
		classNames = $jQ(".homeMainArea").attr("class");
		if(classNames.indexOf("open")==-1){
			$jQ(".homeMainArea").addClass("open");
			hpcontent.slideDown('fast');
		} else {
			hpcontent.slideUp('fast',function(){
				$jQ(".homeMainArea").removeClass("open");
			});
			
		}
		return false;
	});
	
	// home page scrolling elements
	var pod = $jQ("#podContainer");
	if (pod.length > 0) {
		// we're on a home page
	
		var strip = new iStrip(pod);
	}

});

function removeReg(which,button) {
	which.slideUp(100,function(){
		setTimeout(function(){removeDiv('registration')},10);
		button.removeClass("open");
	});
}

function removeApp(which,button) {
	which.slideUp(100,function(){
		setTimeout(function(){removeDiv('application')},10);
		button.removeClass("open");
	});
}

function removeDiv(which) {
	$jQ("div#"+which).remove();
}

jQuery.preloadImages = function(){
	for(var i = 0; i<arguments.length; i++){
		jQuery("<img>").attr("src", arguments[i]);
	}
}


function precacheImg() {
	$jQ.each($jQ(".precache"),function(){ // "-over"
		$jQ(this).unbind('hover');
		overState = this.src;
		overStateFileName = overState.substring(0,overState.lastIndexOf("."));
		overStateFileExtension = overState.substring(overState.lastIndexOf("."),overState.length);
		overStateFullFilename = overStateFileName+"-over"+overStateFileExtension;
		$jQ.preloadImages(this.src, overStateFullFilename);
		$jQ(this).hover(
			function(){
				overState = this.src;
				overStateFileName = overState.substring(0,overState.lastIndexOf("."));
				overStateFileExtension = overState.substring(overState.lastIndexOf("."),overState.length);
				overStateFullFilename = overStateFileName+"-over"+overStateFileExtension;
				this.src = overStateFullFilename;
			}
			,
			function(){
				outState = this.src;
				outStateFileName = outState.substring(0,outState.lastIndexOf("-over"));
				outStateFileExtension = outState.substring(outState.lastIndexOf("."),outState.length);
				outStateFullFilename = outStateFileName+outStateFileExtension;
				this.src = outStateFullFilename;
			}
		)		
	});
}

function iStrip(slides) {
	this.me = slides;
	this.strip = $jQ(slides).find('div#podItems');
	this.container = $jQ(slides).find('ul');
	this.imgs = new Array();
	this.btn = new Array();
	this.lngth = 0;
	this.cpos = 0;
	this.max = null;
	this.maxp = null;
	this.act = [true,true];
	this.slngth = this.strip.width();
	this.crrImg = 0;
	this.init();
	this.time = 250;
	this.mode = false;
	this.busy = false;
	return this;
}


iStrip.prototype.init = function() {

	// find all images and set container length
	var obj = this;
	this.container.find('li').each(function(){
		obj.addImg($jQ(this));
	});
	this.container.css('width',this.lngth+10);
	this.max = -((this.lngth-10) - this.slngth)+120;
	this.maxp = 0;
	var l = this.imgs.length;
	for(this.maxp=0;this.maxp<l;this.maxp++) {
		if (this.max >= this.imgs[this.maxp][1]) {
			break;
		}
	}

	// attach buttons but only if width is wider than 350 / 650
	//if (this.lngth > 940) {
		this.me.prepend('<div id="podNav"><p><a class="prev" href="#">previous</a><span class="hdn">|</span> <a class="next" href="#">next</a></p></div>');
	//}

	// set up buttons
	this.btn[0] = this.me.find('a.prev');
	this.btn[1] = this.me.find('a.next');
	this.btn[0].click(function(me){me.preventDefault();obj.move(-1);return false;});
	this.btn[1].click(function(me){me.preventDefault();obj.move(1);return false;});
	this.ckB();
}


iStrip.prototype.activateImg = function(which) {
	if (this.crrImg != null) {
		this.imgs[this.crrImg][0].removeClass('active');
	}
	this.crrImg = which;
	this.imgs[which][0].addClass('active');
}
iStrip.prototype.addImg = function(img) {
	var w = img.width()+10;
	var h = img.find('.podImage a').attr('href');

	var l = this.imgs.length;
	var obj = this;
	this.imgs.push([img,-this.lngth,h]);
	this.lngth +=  w;
	var img = img;
	img.hover(function(){
		img.find('.podContent h2').css('text-decoration','underline');
	},function(){
		img.find('.podContent h2').css('text-decoration','none');
	});
}

iStrip.prototype.getW = function() {
	return this.lngth+10;
}

iStrip.prototype.move = function(dir) {
	//alert('move');
	
	//console.log(this.crrImg);
	if (!this.busy) {
		//imgElm.trigger('mouseout');
		if (dir == 1 && this.crrImg < this.imgs.length) {
			if (this.cpos < this.maxp && this.act[1]) {
				this.cpos++;
				var npos = this.imgs[this.cpos][1];
				if (-(npos) > (this.lngth-this.slngth)) {
					npos = -(this.lngth-this.slngth)+1;	
				}
				this.container.animate({marginLeft:npos},this.time);
			}
			this.crrImg = this.crrImg + 1;
		} else  if (this.crrImg > 0){
			if (this.cpos > 0 && this.act[0]) {
				this.cpos--;
				var npos = this.imgs[this.cpos][1];
				this.container.animate({marginLeft:npos},this.time);
			}
			this.crrImg = this.crrImg - 1;
		}	
		this.ckB();
	}
}
iStrip.prototype.ckB = function() {
	if (this.cpos == 0) {
		this.btn[0].addClass('inactive');
		this.act[0] = false;
		if (this.lngth+10 > 940) {
			this.act[1] = true;
			this.btn[1].removeClass('inactive');
		} else {
			this.act[0] = false;
			this.btn[0].addClass('inactive');		
		}
	} else if (this.cpos == this.maxp) {
		this.btn[1].addClass('inactive');
		this.act[1] = false;
		this.act[0] = true;
		this.btn[0].removeClass('inactive');
	} else {
		this.btn[0].removeClass('inactive');
		this.btn[1].removeClass('inactive');
		this.act[0] = true;
		this.act[1] = true;
	}
}
