window.addEvent('domready', function(){ 
	var E = new EnlargeImage();
	var menu = new EcceMenu();
	$$('.noenter').each(function(elm){
		elm.addEvent('keydown',function(e){
			if(e.key=="enter") e.stop();
		});
	});
	if($chk($('slider-stage'))){
		var test = new ClickRotate();
	}
	if($chk($('opentext')) && $chk($('categorytext'))){
		$('opentext').addEvent('click',function(e){
			e.stop();
			var textsize = $('categorytext').getScrollSize();
			var myFx = new Fx.Tween($('categorytext'));
			myFx.start('height', textsize.y);
			this.setStyle('display','none');
		});
	}
	if($chk($('opentext')) && $chk($('closetext')) && $chk($('categorytext'))){
		$('closetext').addEvent('click',function(e){
			e.stop();
			var myFx = new Fx.Tween($('categorytext'),{'onComplete':function(){$('opentext').setStyle('display','block');}});
			myFx.start('height', 0);
			
		});
	}
		updatepayments();
	if($chk($('payment_type'))){
		
		$('payment_type').addEvent("change",function(){
			if(this.value==2){
				$('card').setStyle('display','block');
				$('debit').setStyle('display','none');
				if($chk($('winter_payment_period'))) $('winter_payment_period').set('value',3);
				if($chk($('winter_payment_period'))) $('winter_payment_period').set('disabled',true);
				if($chk($('summer_payment_period'))) $('summer_payment_period').set('value',3);
				if($chk($('summer_payment_period'))) $('summer_payment_period').set('disabled',true);
				
				if($chk($('winter_payment_day'))) $('winter_payment_day').set('value',20);
				if($chk($('winter_payment_day'))) $('winter_payment_day').set('disabled',true);
				
				if($chk($('summer_payment_day'))) $('summer_payment_day').set('value',20);
				if($chk($('summer_payment_day'))) $('summer_payment_day').set('disabled',true);
			}else{
				if(this.value==1){
					$('card').setStyle('display','none');
					$('debit').setStyle('display','block');
				}else{
					$('card').setStyle('display','none');
					$('debit').setStyle('display','none');
				}
				if($chk($('winter_payment_day'))) $('winter_payment_day').set('disabled',false);
				if($chk($('summer_payment_day'))) $('summer_payment_day').set('disabled',false);
				if($chk($('winter_payment_period'))) $('winter_payment_period').set('disabled',false);
				if($chk($('summer_payment_period'))) $('summer_payment_period').set('disabled',false);
			}
			updatepayments();
		});
		if($chk($('winter_payment_period'))) paymentdates('winter_payment_month','winter_payment_year','winter_payment_day');
		if($chk($('summer_payment_period'))) paymentdates('summer_payment_month','summer_payment_year','summer_payment_day');
	}
	if($chk($('winter_payment_period'))){$('winter_payment_period').addEvent('change',function(){updatepayments();});}
	if($chk($('winter_payment_period'))){$('winter_payment_month').addEvent('change',function(){updatepayments();});}
	if($chk($('winter_payment_period'))){$('winter_payment_year').addEvent('change',function(){updatepayments();});}
	if($chk($('winter_payment_period'))){$('winter_payment_day').addEvent('change',function(){updatepayments();});}
	if($chk($('summer_payment_period'))){$('summer_payment_period').addEvent('change',function(){updatepayments();});}
	if($chk($('summer_payment_period'))){$('summer_payment_month').addEvent('change',function(){updatepayments();});}
	if($chk($('summer_payment_period'))){$('summer_payment_year').addEvent('change',function(){updatepayments();});}
	if($chk($('summer_payment_period'))){$('summer_payment_day').addEvent('change',function(){updatepayments();});}
	if($chk($('card_month'))) $('card_month').addEvent('click',function(){if(this.value="MM")this.value='';});
	if($chk($('card_year'))) $('card_year').addEvent('click',function(){if(this.value="MM")this.value='';});
	if($chk($('copydetails'))){
		$('copydetails').addEvent('click',function(e){
			e.stop();
			$('card_unit').set('value',unit);
			$('card_address').set('value',address);
			$('card_city').set('value',city);
			$('card_province').set('value',province);
			$('card_postcode').set('value',postcode);
		});
	}
	
	if($chk($('year'))) paymentdates('month','year','day');
	if($chk($('period'))){$('period').addEvent('change',function(){updatepayments();});}
	if($chk($('year'))){$('year').addEvent('change',function(){updatepayments();});}
	if($chk($('month'))){$('month').addEvent('change',function(){updatepayments();});}
	if($chk($('day'))){$('day').addEvent('change',function(){updatepayments();});}
	if($chk($('amount'))){$('amount').addEvent('blur',function(){updatepayments();});}
	$$('.radiochange').each(function(elm){
		elm.addEvent('click',function(){
			if(this.value.toInt()==3 && this.checked){
				$('newpayment').setStyle('display','block');
			}else{
				$('newpayment').setStyle('display','none');
			}
		});
	});
	if($chk($('btnSummer'))){
		$('btnSummer').addEvent('click',function(e){
			e.stop();
			window.location="/summer/";
		});
	}
	if($chk($('btnWinter'))){
		$('btnWinter').addEvent('click',function(e){
			e.stop();
			window.location="/christmas/";
		});
	}
	if($chk($('btnCatalogue'))){
		$('btnCatalogue').addEvent('click',function(e){
			e.stop();
			window.location="/request-catalogue.asp";
		});
	}
	if($chk($('btnCatalogue2'))){
		$('btnCatalogue2').addEvent('click',function(e){
			e.stop();
			window.location="/request-cataloguefr.asp";
		});
	}
	if($chk($('btnAgent'))){
		$('btnAgent').addEvent('click',function(e){
			e.stop();
			window.location="/santas-choice-agent.html";
		});
	}
	
});

function updatepayments(){
	if($chk($('summer_payment_details'))){
		var query = "?summer=1&period="+escape($('summer_payment_period').value)+'&date='+escape($('summer_payment_year').value+'-'+$('summer_payment_month').value+'-'+$('summer_payment_day').value)
		$('summer_payment_details').load('/payment_months.asp'+query);
	}
	if($chk($('winter_payment_details'))){
		var query = "?winter=1&period="+escape($('winter_payment_period').value)+'&date='+escape($('winter_payment_year').value+'-'+$('winter_payment_month').value+'-'+$('winter_payment_day').value)
		$('winter_payment_details').load('/payment_months.asp'+query);
	}
	if($chk($('payment_details'))){
		var query = "?winter=1&period="+escape($('period').value)+'&date='+escape($('year').value+'-'+$('month').value+'-'+$('day').value)+'&amount='+escape($('amount').value)
		$('payment_details').load('/payment_months.asp'+query);
	}
	
	
}

function paymentdates(month,year,day){
	$(month).addEvent('change',function(){
		switch(this.value.toInt()){
			case 9:
			case 4:
			case 11:
			case 6:
				if($(day).value.toInt()>30) $(day).value=30;
				break;
			case 2:
				if(isLeapYear($(year).value.toInt())){
					if($(day).value.toInt()>29){
						$(day).value=29;
					}
				}else{
					if($(day).value.toInt()>28){
						$(day).value=28;
					}
				}
				break;
		}	
	});
	$(day).addEvent('change',function(){
		switch($(month).value.toInt()){
			case 9:
			case 4:
			case 11:
			case 6:
				if(this.value.toInt()>30){
					this.value=30;
					alert("Day must be 30 or smaller");
				}
				break;
			case 2:
				if(isLeapYear($(year).value.toInt())){
					if(this.value.toInt()>29){
						this.value=29;
						alert("Day must be 29 or smaller");
					}
				}else{
					if(this.value.toInt()>28){
						this.value=28;
						alert("Day must be 28 or smaller");
					}
				}
				break;
		}	
	});
}

var EcceMenu = new Class({
	Implements: [Options],				
	options: {
		'selection': '.menuitem',
		'menu': 'ul',
		'duration':300,
		'closedelay':300,
		'zcount':5000
	},
	initialize: function(options) {
		this.setOptions(options);
		var op = this.options;
		var c = this;
		op.id = new Array();
		
		$$(this.options.selection).each(function(elm,i){
				elm.setStyles({'position':'absolute','overflow':'hidden','height':0});
				elm.setStyle('display','none');
				var parent = elm.getParent();
				var pWidth = (parent.getSize().x-parent.getStyle('padding-left').toInt()-parent.getStyle('padding-right').toInt());
				parent.setStyle('width',pWidth);
				elm.setStyle('display','block');
				
				parent.addEvent('mouseenter',function(){
					op.zcount++;
					elm.setStyle('z-index',op.zcount);
					var size = elm.getScrollSize();
					var myFx = new Fx.Tween(elm,{'duration':op.duration});
					myFx.start('height', size.y);
				});
				parent.addEvent('mouseover',function(){
					$clear(op.id[i]);
				});
				parent.addEvent('mouseleave',function(){
					$clear(op.id[i]);
					op.id[i] = c.closeelm.delay(op.closedelay,c,elm);
				});
				elm.addEvent('mouseleave',function(){
					op.id[i] = c.closeelm.delay(op.closedelay,c,elm);
				});
				elm.addEvent('mouseenter',function(){
					$clear(op.id[i]);
				});
		});
		
	},
	closeelm: function(elm){
		var op = this.options;
		var myFx2 = new Fx.Tween(elm,{'duration':op.duration});
		myFx2.start('height', 0);	
	}
});



var ClickRotate = new Class({
	Implements: [Options],
	options: {
		stage: 'slider-stage',
		sliders: 'slider-list',
		gotoclass: 'gotolink',
		gotoevent:'click',
		next:'next',
		nextevent:'click',
		transition:'fade',
		transitionduration:1000,
		clickduration:250,
		previous:'previous',
		previousevent:'click',
		autorotate:true,
		waittime:6000,
		returnrotate:true,
		returnafter:5000,
		returnid:null,
		intervalID:null,
		useactivebuttons:false,
		onbutton:'/gfx/splash_on.gif',
		offbutton:'/gfx/splash_off.gif',
		buttonsinject:'splashbuttons'
	},
	initialize: function(options) {
		this.setOptions(options);
		var op = this.options;
		var f = this;
		op.children = $(op.sliders).getChildren('li');
		if (op.transition == "fade"){
			var startloop = 1000+op.children.length;
			for(i=0;i<op.children.length;i++){
				op.children[i].setStyle("z-index",startloop);
				startloop--;
			}
		}
		if(op.useactivebuttons){
			op.clickbuttons = Array();
			op.currentbuttonid=0;
			for(i=0;i<op.children.length;i++){
				if(i==0){
					op.clickbuttons[i] = new Element('img', {'src': op.onbutton});
				}else{
					op.clickbuttons[i] = new Element('img', {'src': op.offbutton});
				}
				op.clickbuttons[i].inject($(op.buttonsinject));
			}
		}
		
		if(op.autorotate) op.intervalID = this.autorotate.periodical(op.waittime,this);
		if($type($(op.next))=='element'){
			$(op.next).addEvent(op.nextevent,function(e){
				e.stop();
				if($type(op.returnid)=='number') $clear(op.returnid);
				if($type(op.intervalID)=='number'){
					$clear(op.intervalID);
					if(op.returnrotate) op.returnid = f.autorotate.delay(op.returnafter,f);
				}
				var children = $(op.sliders).getChildren('li');
				if (op.transition == "fade"){
					children[0].set('tween',{duration:op.clickduration,onComplete:function(){
						children[0].setStyle("z-index",0).inject($(op.sliders)).setStyle('opacity',1);
						var children2 = $(op.sliders).getChildren('li');
						var startloop = 1000+op.children.length;
						for(i=0;i<children2.length;i++){
							children2[i].setStyle("z-index",startloop);
							startloop--;
						}
					}});
					children[0].tween('opacity', 0);
				}else{
					children[0].set('tween',{duration:op.clickduration,onComplete:function(){children[0].inject($(op.sliders)).setStyle('margin-left',0);}});
					children[0].tween('margin-left', -children[0].getSize().x);
				}
			});
		}
		if($type($(op.previous))=='element'){
			$(op.previous).addEvent(op.previousevent,function(e){
				e.stop();
				if($type(op.returnid)=='number') $clear(op.returnid);
				if($type(op.intervalID)=='number'){
					$clear(op.intervalID);
					if(op.returnrotate) op.returnid = f.autorotate.delay(op.returnafter,f);
				}
				var children = $(op.sliders).getChildren('li');
				var last = children.length-1;
				if (op.transition == "fade"){
					children[last].set('tween',{duration:op.clickduration,onComplete:function(){children[last].setStyle('opacity',1);op.infade=false;}});
					children[last].setStyle('opacity',0).inject($(op.sliders),'top');
					var children2 = $(op.sliders).getChildren('li');
					var startloop = 1000+op.children.length;
					for(i=0;i<children2.length;i++){
						children2[i].setStyle("z-index",startloop);
						startloop--;
					}
					children[last].tween('opacity', 1);
				}else{
					children[last].set('tween',{duration:op.clickduration,onComplete:function(){children[last].set('style','');}});
					children[last].setStyle('margin-left',-children[last].getSize().x).inject($(op.sliders),'top');
					children[last].tween('margin-left', 0);
				}
			});
		}
		$$("a."+op.gotoclass).each(function(elm){
			var slideid = elm.getProperty('rel');
			if ($type(slideid)=="string"){
				slideid=slideid.toInt();
				elm.addEvent(op.gotoevent,function(e){
					e.stop();
					if($type(op.returnid)=='number') $clear(op.returnid);
					if($type(op.intervalID)=='number'){
						$clear(op.intervalID);
						if(op.returnrotate) op.returnid = f.autorotate.delay(op.returnafter,f);
					}
					var children = $(op.sliders).getChildren('li');
					if (op.transition == "fade"){
						op.children[slideid].set('tween',{duration:op.clickduration,onComplete:function(){
								op.children[slideid].setStyle('opacity',1);
								for(i=slideid+1;i<op.children.length;i++) op.children[i].inject($(op.sliders),'bottom');
								for(i=0;i<slideid;i++) op.children[i].inject($(op.sliders),'bottom');
								var children2 = $(op.sliders).getChildren('li');
								var startloop = 1000+op.children.length;
								for(i=0;i<children2.length;i++){
									children2[i].setStyle("z-index",startloop);
									startloop--;
								}
								op.infade=false;
						},onStart:function(){op.infade=true;}});
						op.children[slideid].setStyle('opacity',0).inject($(op.sliders),'top');
						var children2 = $(op.sliders).getChildren('li');
						var startloop = 1000+op.children.length;
						for(i=0;i<children2.length;i++){
							children2[i].setStyle("z-index",startloop);
							startloop--;
						}
						op.children[slideid].tween('opacity', 1);
					}else{
						op.children[slideid].set('tween',{duration:op.clickduration,onComplete:function(){
							op.children[slideid].set('style','');
							for(i=slideid+1;i<op.children.length;i++) op.children[i].inject($(op.sliders),'bottom');
							for(i=0;i<slideid;i++) op.children[i].inject($(op.sliders),'bottom');
						}});
						op.children[slideid].setStyle('margin-left',-op.children[slideid].getSize().x).inject($(op.sliders),'top');
						op.children[slideid].tween('margin-left', 0);
					}
				});
			}

		
		});
		
	},
			
	autorotate:function(){
		var op = this.options;
		if($type(op.returnid)=='number'){
			$clear(op.returnid);
			op.returnid=null;
			op.intervalID = this.autorotate.periodical(op.waittime,this);
		}
		var children = $(op.sliders).getChildren('li');
		
		var toupdate = op.currentbuttonid+1;
			var lasupdate = op.currentbuttonid;
			if(toupdate>children.length-1){
					lasupdate = children.length-1;
					toupdate=0;
					op.currentbuttonid=0;
			}else{
				op.currentbuttonid++;
			}
			//op.clickbuttons[toupdate].src = op.onbutton;
			//op.clickbuttons[lasupdate].src = op.offbutton;
		
		if (op.transition == "fade"){
			children[0].set('tween',{duration:op.transitionduration,onComplete:function(){
				children[0].setStyle("z-index",0).inject($(op.sliders)).setStyle('opacity',1);
				var children2 = $(op.sliders).getChildren('li');
				var startloop = 1000+op.children.length;
				for(i=0;i<children2.length;i++){
					children2[i].setStyle("z-index",startloop);
					startloop--;
				}
			}});
			children[0].tween('opacity', 0);
		}else{
			children[0].set('tween',{duration:op.transitionduration,onComplete:function(){
				children[0].inject($(op.sliders)).setStyle('margin-left',0);
			}});
			children[0].tween('margin-left', -children[0].getSize().x);
		}
	}
		
	
});

function popup(w,h,page) {
var winl = (screen.width-w)/2;
var wint = (screen.height-h)/2;
if (winl < 0) winl = 0;
if (wint < 0) wint = 0;
windowprops = "height="+h+",width="+w+",top="+ wint +",left="+ winl +",location=no,"
+ "scrollbars=no,menubars=no,toolbars=no,resizable=no,status=no";
window.open(page, "Santas Choice", windowprops);
return false;
}

function popup2(w,h,page) {
var winl = (screen.width-w)/2;
var wint = (screen.height-h)/2;
if (winl < 0) winl = 0;
if (wint < 0) wint = 0;
windowprops = "height="+h+",width="+w+",top="+ wint +",left="+ winl +",location=no,"
+ "scrollbars=yes,menubars=no,toolbars=no,resizable=no,status=no";
window.open(page, "Santas Choice", windowprops);
return false;
}


function data_copy2(){
document.check.ship_unit.value=document.check.unit.value;
document.check.ship_address2.value=document.check.address2.value;
document.check.ship_town.value=document.check.town.value;
document.check.ship_county.value=document.check.county.value;
document.check.ship_postcode1.value=document.check.postcode1.value;
document.check.ship_postcode2.value=document.check.postcode2.value;
document.check.ship_title.value=document.check.title.value;
document.check.ship_first_name.value=document.check.first_name.value;
document.check.ship_last_name.value=document.check.last_name.value;
return false;
}
function isLeapYear(yr) {
  return new Date(yr,2-1,29).getDate()==29;
}


var EnlargeImage=new Class({
       Implements:[Options],
       options:{
              'classname':'enlarge',
              'loadimage':'/gfx/loading.gif',
              leftoffset:1,
              topoffset:1
       },
	   initialize:function(options){
              this.setOptions(options);
              var op = this.options;
              var c = this;
              $$('a.enlarge').each(function(elm,i){
                     elm.addEvent('click',function(e){e.stop();});
                     elm.addEvent('mouseenter',function(e){
                           var screenPos = window.getScroll();
                           var img1 = new Element('img',{'id':'overimage','src':op.loadimage,'styles':{'width':'auto','height':'auto','background-color':'white', 'padding':'10px', 'border':'1px solid #CCCCCC','position':'absolute','top':e.client.y+op.topoffset+screenPos.y,'left':e.client.x+op.leftoffset}}).inject(elm);       
						   var img2 = new Element('img',{'id':'overimage2','styles':{'display':'none'},'events':{'load':function(){
								img1.src=img2.src;
                             	$('overimage2').dispose();
                           }}});
						   img2.set('src',elm.href).inject(elm);
                     });
                     elm.addEvent('mousemove',function(e){
                           var screenPos = window.getScroll();
						   if($chk($('overimage'))){
                           	$('overimage').setStyles({'top':e.client.y+op.topoffset+screenPos.y,'left':e.client.x+op.leftoffset});
						   }
                     });
                     elm.addEvent('mouseleave',function(e){$('overimage').destroy();});
              });
       }
	   
	   
});
//Category Text Expand
	
//Star Rating
var StarRating = new Class({
	Implements: [Options],				
	options: {
		'numberOfStars': 5,
		'sprite': '/gfx/alt_star.gif',
		'selectorClass':'.stars',
		'starWidth':25,
		'starHeight':25,
		'onSubmit':function(id,score,total){
			var update = new Request({'method': 'post', 'url': '/addRating.asp'});
			update.send('id='+escape(id)+'&score='+escape(score)+'&total='+escape(total));
		}
	},
	initialize: function(options) {
		this.setOptions(options);
		var op = this.options;
		var c = this;
		op.ulContainer = new Array();
		$$(op.selectorClass).each(function(elm,i){
			elm.setStyle('overflow','hidden');
			if(elm.getProperty('id')!=null){
				c.setup(elm,i);
			}
		});
	},
	setScoreFromJSON: function(json){
		var op = this.options;
		var c = this;
		for(i=0;i<json.scores.length;i++){
			c.setScore(json.scores[i].id,json.scores[i].score);
		}
	},
	setScore:function(ulID,score){
		var op = this.options;
		var c = this;
		if($chk($(ulID))){
			var decimal = score.toString().indexOf(".");
			if(decimal>-1){
				var parts = score.toString().split(".");
				var dec = parts[1].toInt();
				if(dec!=5){
					if(dec<5){
						score += ((5-dec)/10);
					}else{
						score += ((10-dec)/10);
					}
				}
			}
			if(score>op.numberOfStars)score = op.numberOfStars;
			if(score<0)score = 0;
			var newWidth = op.starWidth *score;
			var ulChildren = $(ulID).getChildren('ul');
			var liChildren = ulChildren.getChildren('li.currentScore');
			liChildren[0].setStyle('width',newWidth);
		}
		return score;
	},
	
	getIEVersionNumber: function() {
		var ua = navigator.userAgent;
		var MSIEOffset = ua.indexOf("MSIE ");
		
		if (MSIEOffset == -1) {
			return 0;
		} else {
			return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
		}
	},
	setup:function(elm,id){
		var op = this.options;
		var c = this;
		op.ulContainer[id] = new Element('ul',{'styles':{'padding':0,'margin':0},'events':{
			'mouseout':function(){
				c.chooseStar(0,this);
			}
		}
		}).inject(elm);
		var defaultstars = new Element('li',{'class':'currentScore','styles':{
			'position':'absolute',
			'list-style':'none',
			'background':'url('+op.sprite+') 0px -25px',
			'z-index':2,
			'height':op.starHeight
		}}).inject(op.ulContainer[id]);
		if(c.getIEVersionNumber()>0 && c.getIEVersionNumber()<8){
			defaultstars.setStyle('margin-left',-(op.numberOfStars*op.starWidth).toInt());
		}
		
		for(i=0;i<op.numberOfStars;i++){
			var currentStar = new Element('li',{'styles':{
				'width':op.starWidth,
				'height':op.starHeight,
				'padding':0,
				'margin':0,
				'background-image':'url('+op.sprite+')',
				'float':'left',
				'list-style':'none'
			}}).inject(op.ulContainer[id]);
			new Element('a',{'styles':{'position':'absolute','z-index':4,'display':'block','width':op.starWidth,'height':op.starHeight,'cursor':'pointer'},'events':{
				'mouseenter':function(){
					var ID = 0;
					var ulContainer = this.getParent('ul');
					var liparent = this.getParent('li');
					var liChildren = ulContainer.getChildren('li');
					for(ii=0;ii<liChildren.length;ii++){
						if(liChildren[ii] == liparent){
							ID = ii
						}
					}
					c.chooseStar(ID,ulContainer);
				},
				'click':function(){
					var ID = 0;
					
					var ulContainer = this.getParent('ul');
					var containerParent = ulContainer.getParent();
					var liparent = this.getParent('li');
					var liChildren = ulContainer.getChildren('li');
					for(ii=0;ii<liChildren.length;ii++){
						if(liChildren[ii] == liparent){
							ID = ii
						}
					}
					c.setScore(containerParent.getProperty('id'),ID);
					for(ii=0;ii<liChildren.length;ii++){
						var aLink = liChildren[ii].getChildren('a');
						if(aLink.length>0){
							aLink[0].dispose();
						}
					}
					op.onSubmit(containerParent.getProperty('id'),ID,op.numberOfStars);
				}
			}
			}).inject(currentStar);
		}
	},
	chooseStar: function(score,ulContainer){
		var op = this.options;
		var c = this;
		var liChildren = ulContainer.getChildren('li');
		for(ii=1;ii<liChildren.length;ii++){
			var aLink = liChildren[ii].getChildren('a');
			if(aLink.length>0){
				if(ii<=score){
					aLink[0].setStyles({'background':'url('+op.sprite+') left bottom'});
				}else{
					aLink[0].setStyles({'background':'none'});
				}
			}
		}
	}
});



