var buttons = new Array();

function Init () {
	$$('.button').each(function (el) {
		buttons[el.id] = new Button(el);
	});
	//var param = getURLParam('featured');
	//var index = findPizza(param);
	PizzaPage.setCurrent('2');
	PizzaPage.initPizzas();
	winnersToggle();
	login();
	wheretobuy();
	dob();
	getNameWidth();
	login_form();
}

var PizzaPage = {};
PizzaPage.width = 180;
PizzaPage.newLeft = -109;
PizzaPage.deltaLeft = 220;
PizzaPage.totalItems = 0;
PizzaPage.pizzas = null;
PizzaPage.newFeatured = null;
PizzaPage.animating = false;
PizzaPage.direction = null;
PizzaPage.featured = null;
PizzaPage.activeImage = null;
PizzaPage.setPizzas = function() {
	PizzaPage.pizzas = $('pizzas');
}
PizzaPage.getActive = function() {
	var elt = $E('.activeImage');
	return elt;
}
PizzaPage.getShrink = function() {
	return new Fx.Styles(PizzaPage.activeImage, {
		duration: 250, 
		transition: Fx.Transitions.linear
	});
}
PizzaPage.getGrow = function(){
	return new Fx.Styles(PizzaPage.activeImage, {
		duration: 250,
		transition: Fx.Transitions.linear
	});
}
PizzaPage.transFeatured = function(el) {
	return new Fx.Style(el, 'width' , {
		duration: 250
	});
}
PizzaPage.initPizzas = function() {
	var productPage = $('pizzaBox');
	if(!$defined(productPage)) return;
	PizzaPage.getActive();
	totalItems = $$('.pizza').length - 1;
	$$('.pizza').each(function (el) {
		PizzaPage.width += PizzaPage.deltaLeft;
	});
	PizzaPage.pizzas = $('pizzas');
	PizzaPage.pizzas.setStyle('width', PizzaPage.width);
	$ES('.prev').addEvent('click',function(event){PizzaPage.slideLeft();});
	$ES('.next').addEvent('click',function(event){PizzaPage.slideRight();});
	
	PizzaPage.activeImage = $E('.activeImage');
	PizzaPage.shrink = PizzaPage.getShrink(); 
	PizzaPage.slide = new Fx.Style(PizzaPage.pizzas, 'left', {
		duration: 500,
		transition: Fx.Transitions.linear
	});
	PizzaPage.allAnimations();
}; // fn PizzaPage.initPizzas
PizzaPage.allAnimations = function() {
	
	
	PizzaPage.slide.addEvent('onComplete', function() {
		var featured = PizzaPage.getFeatured();
		var img = getImg(featured);
		img.removeClass('activeImage');
	}).addEvent('onComplete', function() {
		var newFeatured = PizzaPage.getNewFeatured();
		var img = newFeatured.getChildren()[0];
		img.addClass('activeImage');
		img.setProperty('src', img.getProperty('src')[0].replace(/.sm.jpg/,'.lg.jpg'));
		var findActiveIndex = img.getProperty('src')[0].split('.').length;
		PizzaPage.setActiveId(img.getProperty('src')[0].split('.')[findActiveIndex-3]);
		img.setStyle('height',220);
		var img = getImg(newFeatured);
		PizzaPage.activeImage = img[0];
		var grow = PizzaPage.getGrow();
		grow.start({
			'height':[220,273]
		});
	}).addEvent('onComplete', function () {
		var newFeatured = PizzaPage.getNewFeatured();
		PizzaPage.setNewFeatured(newFeatured);
	}).addEvent('onComplete', function() {
		var oldElt = null;
		$$('.details').each(function(container){
			if (container.getStyle("display") != "none") {
				oldElt = container;
			}
		});
		var elt = $(PizzaPage.activeId);
		var fadeOld = new Fx.Style(oldElt, 'opacity', {
			duration: 450
		});
		var fade = new Fx.Style(elt, 'opacity', {
			duration: 450
		});
		fadeOld.start(1,0).addEvent('onComplete', function() {
			oldElt.setStyle('display','none');
		}).addEvent('onComplete', function() {
			elt.setStyle("opacity","0");
			elt.setStyle("display","block");
			fade.start(0,1);
		});
	}).addEvent('onComplete',
		function() {
			PizzaPage.animating = false;
			//var newFeatured = PizzaPage.getNewFeatured();
		}
	);
}
PizzaPage.setActiveId = function(id) {
	PizzaPage.activeId = id;
}
PizzaPage.setNewFeatured = function(elt) {
	PizzaPage.newFeatured = elt;
}
PizzaPage.getNewFeatured = function()  {
	return PizzaPage.newFeatured;
}
PizzaPage.nextRight = function () {
	if (Math.abs(PizzaPage.pizzas.getStyle('left').toInt() - PizzaPage.deltaLeft*8)>= PizzaPage.pizzas.getStyle('width').toInt()) {
		PizzaPage.pizzas.getFirst().injectAfter(PizzaPage.pizzas.getLast());
		PizzaPage.pizzas.setStyle('left', (PizzaPage.pizzas.getStyle('left').toInt()+PizzaPage.deltaLeft)+"px");
		var startPos = PizzaPage.pizzas.getStyle('left').toInt();
		var endPos = startPos - PizzaPage.deltaLeft;
	} else {
		var startPos = PizzaPage.pizzas.getStyle('left').toInt();
		var endPos = startPos - PizzaPage.deltaLeft;
	}
	return new Array(startPos,endPos);
}; // fn PizzaPage.getNextLeft

PizzaPage.nextLeft = function() {
	if (PizzaPage.pizzas.getStyle('left').toInt() == 0 || PizzaPage.pizzas.getStyle('left').toInt() == -109) {
		//at start
		PizzaPage.pizzas.getLast().injectBefore(PizzaPage.pizzas.getFirst());
		PizzaPage.pizzas.setStyle('left', (-PizzaPage.deltaLeft-109)+"px");
		var startPos = -PizzaPage.deltaLeft-109;
		var endPos = -109;
	} else {
		var startPos = PizzaPage.pizzas.getStyle('left').toInt();
		var endPos = startPos + PizzaPage.deltaLeft;
	}
	return new Array(startPos,endPos);
}; // fn PizzaPage.getNextRight

PizzaPage.slideLeft = function(){
	if (PizzaPage.animating) {
		return;
	}
	PizzaPage.animating = true;
	var pos = PizzaPage.nextLeft();
	PizzaPage.direction = 'back';
	//PizzaPage.slide.start(pos[0],pos[1]);
	PizzaPage.doSlide(pos[0],pos[1]);
};
PizzaPage.slideRight = function(){
	if (PizzaPage.animating) {
		return;
	}
	PizzaPage.animating = true;
	var pos = PizzaPage.nextRight();
	PizzaPage.direction = 'forward';
	//PizzaPage.slide.start(pos[0],pos[1]);
	PizzaPage.doSlide(pos[0],pos[1]);
};
PizzaPage.doSlide = function(start,end) {
	PizzaPage.activeImage = $E('.activeImage');
	PizzaPage.setFeatured();
	PizzaPage.setPizzas();
	PizzaPage.start = start;
	PizzaPage.end = end;
	PizzaPage.shrink = PizzaPage.getShrink();
	PizzaPage.shrink.start({
		'height':[273,220]
	}).addEvent('onComplete', function() {
		var featured = PizzaPage.getFeatured();
		featured.setStyle('width', 300);
		var smoothFeat = PizzaPage.transFeatured($E('.featured'));
		smoothFeat.start(300, 220);
		featured.removeClass('featured');
		var img = getImg(featured);
		img.setProperty('src', img.getProperty('src')[0].replace(/.lg.jpg/,'.sm.jpg'));
	}).addEvent('onComplete', function(){
		var featured = PizzaPage.getFeatured();
		var elt = (PizzaPage.direction == 'forward') ? featured.getNext() : featured.getPrevious();
		PizzaPage.setNewFeatured(elt);
		var newFeatured = PizzaPage.getNewFeatured();
		newFeatured.setStyle('width', 220);
		newFeatured.addClass('featured');
		var smoothFeat = PizzaPage.transFeatured($E('.featured'));
		smoothFeat.start(220, 300);
	}).addEvent('onComplete', function() {
		var featured = PizzaPage.getFeatured();
		PizzaPage.slide.start(PizzaPage.start,PizzaPage.end);
	});
}; // PizzaPage.doSlide
/** 
 * Prevents capture.
 */
PizzaPage.getStart = function() {
	return PizzaPage.start;
}
PizzaPage.getEnd = function() {
	return PizzaPage.end;
}
PizzaPage.setFeatured = function() {
	PizzaPage.featured = $$('.featured');
}
PizzaPage.getFeatured = function() {
	return PizzaPage.featured;
}

PizzaPage.setCurrent = function(index) {
	var containers = $$('.pizza');
	var i =0;
	var centerContainer;
	var oldCenterImg;
	containers.each(function(container) {
		if (i++ == 2) {
			centerContainer = container;
			oldCenterImg = container.getChildren()[0];
		}
	});
	i=0;
	containers.each(function(container) {
		if (i++ == index) {
			//centerContainer.addClass('featured');
			var newImg = container.getChildren()[0];
			newImg.src = newImg.src.replace(/.sm.jpg/,'.lg.jpg');
			newImg.addClass('activeImage');
			//centerContainer.removeChild(oldCenterImg);
			//centerContainer.appendChild(newImg);
			//container.appendChild(oldCenterImg);
			var findActiveIndex = newImg.getProperty('src').split('.').length;
			//console.log(newImg.getProperty('src').split('.')[findActiveIndex-3]);
			PizzaPage.setActiveId(newImg.getProperty('src').split('.')[findActiveIndex-3]);
			$(PizzaPage.activeId).setStyle('display','block');
		}
	});
}; // fn setCurrent. 

function findPizza(pizzaName) {
	var containers = $$('.pizza');
	var i=0;
	var j=0;
	containers.each(function(container) {
		var img = getImg(container);
		if (img.src.indexOf(pizzaName) != -1) {
			j = i;
		}
		i++;
	});
	return j;
}

function getImg(iContainer) {
	var img = iContainer.getChildren()[0];
	return img;
}

/*where to buy province swap*/
function where2buy(province){
	var chart = $$('.buyChart');
	if ($$('#ristorante-chart').getStyle('display') == 'none') {
		var selectedProv = $('c_'+province);
	} else {
		var selectedProv = $('r_'+province);
	}
	chart.each(function(el){
		el.setStyle('display', 'none' );
	});
	selectedProv.setStyle('display', 'block');
	
}

/*popup*/
function popup(url, type){
	if (type=="recipe") {
		var newwindow = window.open(url,'name','height=700,width=625,');
	}else {
		var newwindow = window.open(url,'name','height=700,width=565,scrollbars=yes');
	}
	if (window.focus) {newwindow.focus()}
	return false;
}

/*get url param*/
function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if ( aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
}

/*winners toggle*/
function winnersToggle() {
	var winnersPage = $('winnersDropdown');
	if(!$defined(winnersPage)) return;
	winnersPage.addEvent('change', function() {
		var value = winnersPage.getValue();
		$$('.winMonth').setStyle('display', 'none');
		$(value).setStyle('display', 'block');
		var img = $(value).getProperty('image');
		var alt = $(value).getProperty('copy');
		$('contest_image').setProperty('src', $('contest_image').getProperty('src').replace(/.\w+.jpg/g, '.'+img+'.jpg'));
		$('contest_image').setProperty('alt', alt);
	});
}

/*Mouseover class*/

var Button = new Class({
	initialize: function(el){
		this.el = el;
		ref = this;
		this.src = el.getProperty('src');
		el.addEvent('mouseover', function (e) {
			e = new Event(e);
			//uncomment once all over .gifs are made
			el.setProperty('src',this.src.replace(/.off.([gif|jpg])/,".on.$1"));
		}.bind(ref));
		el.addEvent('mouseout', function (e) {
			e = new Event(e);
			el.setProperty('src',this.src);
		}.bind(ref));
	}
});

function login(){
	$$('img.login').addEvent('click', function (e) {
		$$('#popup').setStyle('display', 'block');
		$$('#popup').setStyle('margin', '0 0 0 0');		
		$$('#popup #loginform').setStyle('display', 'block');		
		$$('#login_redirect').setProperty('value','');
	});
	$$('img.login_enter_now').addEvent('click', function (e) {
		$$('#popup').setStyle('display', 'block');
		$$('#popup').setStyle('margin', '100px 450px 0 0');			
		$$('#popup #loginform').setStyle('display', 'block');		
		$$('#login_redirect').setProperty('value','PIN_CONTEST');
	});
	$$('.close').addEvent('click', function (e) {
		$$('#popup #loginform').setStyle('display', 'none');
		$$('#popup #forgotform').setStyle('display', 'none');
		$$('#popup').setStyle('display', 'none');
	});
	$$('.forgot').addEvent('click', function (e) {
		$$('#popup #loginform').setStyle('display', 'none');
		$$('#popup #forgotform').setStyle('display', 'block');
	});
}

function wheretobuy(){
	$$('#ristorante-tab').addEvent('click', function (e) {
		$$('#casa-chart').setStyle('display', 'none');
		$$('#ristorante-chart').setStyle('display', 'block');
		var province = $$('#province').getProperty('value');
		$$('#ristorante-chart #r_' + province).setStyle('display', 'block');
		$$('#ristorante-tab').setProperty('src', this.src.replace(/.on.gif/,'.active.gif'));
		$$('#wtb_img').setProperty('src', $('wtb_img').getProperty('src').replace(/.casa.jpg/,'.ristorante.jpg'));
		$$('#casa-tab').setProperty('src', $('casa-tab').getProperty('src').replace(/.active.gif/,'.off.gif'));
	});
	$$('#casa-tab').addEvent('click', function (e) {
		$$('#ristorante-chart').setStyle('display', 'none');
		$$('#casa-chart').setStyle('display', 'block');
		var province = $('province').getProperty('value');
		$$('#casa-chart #c_' + province).setStyle('display', 'block');
		$$('#casa-tab').setProperty('src', this.src.replace(/.on.gif/,'.active.gif'));
		$$('#wtb_img').setProperty('src', $('wtb_img').getProperty('src').replace(/.ristorante.jpg/,'.casa.jpg'));
		$$('#ristorante-tab').setProperty('src', $('ristorante-tab').getProperty('src').replace(/.active.gif/,'.off.gif'));
	});
	
 	$$('#casa-tab').addEvent('mouseover', function (e) {
		if($('casa-tab').getProperty('src').split('.')[3] != 'active') {
			$$('#casa-tab').setProperty('src', this.src.replace(/.off.gif/,'.on.gif'));
		}
	});
	$$('#casa-tab').addEvent('mouseout', function (e) {
		if($('casa-tab').getProperty('src').split('.')[3] != 'active') {
			$$('#casa-tab').setProperty('src', this.src.replace(/.on.gif/,'.off.gif'));
		}
	});
	$$('#ristorante-tab').addEvent('mouseover', function (e) {
		if($('ristorante-tab').getProperty('src').split('.')[3] != 'active') {
			$$('#ristorante-tab').setProperty('src', this.src.replace(/.off.gif/,'.on.gif'));
		}
	});
	$$('#ristorante-tab').addEvent('mouseout', function (e) {
		if($('ristorante-tab').getProperty('src').split('.')[3] != 'active') {
			$$('#ristorante-tab').setProperty('src', this.src.replace(/.on.gif/,'.off.gif'));
		}
	});
}

function dob() {
	$$('#dob input').each(function(el) {
		rel = el.getProperty('rel');
		if(rel != null ) {
			el.setProperty('value',el.getProperty('rel'));  
			el.addEvent('focus', function() { if(el.getProperty('value') == el.getProperty('rel')) { el.setProperty('value',''); } });  
			el.addEvent('blur', function() { if(el.getProperty('value') == '') { el.setProperty('value',el.getProperty('rel')); } }); 
		}
	});
}

function login_form() {
	$$('#pass_temp').addEvent('focus', function (e) {
		if(this.value == this.defaultValue) {
			$(this).setStyle('display', 'none');
			$$('#pass_real').setStyle('display', 'block');
			//$E('#pass_real').focus();
		}
 	});
	$$('#pass_real').addEvent('blur', function(e) {
		if(this.value == '') {
			$(this).setStyle('display', 'none');
			$$('#pass_temp').setStyle('display', 'block');
		}
	});
	
	$$('#login_new input[name="email"]').addEvent('focus', function (e) {
		if(this.value == this.defaultValue) {
			this.value = '';
		}
 	});
	$$('#login_new input[name="email"]').addEvent('blur', function(e) {
		if(this.value == '') {
			this.value = this.defaultValue;
		}
	});
}

function getNameWidth() {
	if($('namelength')) {
		var length = $('namelength').offsetWidth;
		$('hide').setStyle('display', 'none');
		$('logout_name').setStyle('background-position', String((270 - length)) + 'px 0');
	}
}

window.addEvent('domready', Init);

//What's New
function enter_contest(){
	//form vars
	var form = $j("#contest_enter_form");
	var rules = $j("#accept_rules");
	if (rules.attr('checked')){
		var optin_flag = "Yes";
	}
	else{
		var optin_flag = "";
		show_no_terms();
		return false;
	}
	
	var data_string = { rules: optin_flag}

	post_form('moments_login.php', data_string);
	return true;
}


function post_form(page, formdata) {
	show_busy();
	$j.post(page, formdata, function (data, textStatus) {
		if(textStatus=='success'){
			$j("#popup_content").html(data);
		}
		else{
			$j("#popup_content").load('moments_contest.php');
		}
	});
	hide_busy();

}

function send_login(){
	//form vars
	var form = $j("#reg");
	var email = $j("#c_email");
	var password = $j("#c_password");
	
	var data_string = { email: email.val(), password: password.val()}

	post_form('moments_login.php', data_string);

}


function show_busy(){
	$j('#busy_icon').show();

}

function hide_busy(){
	$j('#busy_icon').hide();
	
}
function show_no_terms(){
	$j('#no_terms').show();
}

function hide_no_terms(){
	$j('#no_terms').hide();
}