var thismonth=new Date().getMonth();
var nextmonth=(thismonth<12)?thismonth+1:0;
var nextyear=(nextmonth==0)?(parseInt(new Date().getFullYear()+1)):(new Date().getFullYear());
var cddate = new Date(nextyear, nextmonth, 1, 0, 0);
var months=['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
var mstocd, daystocd, hourstocd, minstocd, secstocd;


Element.observe(window, 'load', function(){
	$$('a[href^=http://]').each(function(link){
    link.writeAttribute({target:"_blank", title:"Javascript will open this external link in a new window."})
  });
  
  if ($('countdown')){
		var past = displayCountdown();
		if (!(past)) new PeriodicalExecuter(displayCountdown, 1);
	}
	if ($('remindme')){
		Event.observe($('remindme'), 'click', openform);
	}
	if ($$('.buynow')){
		$$('.buynow').each(function(item){
			Event.observe(item, 'click', function(e){
				thisform=item.up().up();
				thisform.submit();
				Event.stop(e);
			});
		});
	}
	
	if (document.getElementById('tandc')){
	  $('tandc').hide()
	}
	if (document.getElementById('zoomme')){ 
		setupZoom();
	}
	if ($$('.print')){
		var view_array = [['Print',''], ['Detail 1','-detail1'], ['Detail 2','-detail2'], ['Detail 3','-detail3'], ['Detail 4','-detail4'], ['Detail 5','-detail5']];
		$$('.print').each(function(item){
			var nodetails=item.down('img').readAttribute('rel');
			var my_ul = document.createElement('ul');
			Element.extend(my_ul);
			my_ul.addClassName('details');
			for (i=0; i<nodetails; i++){
				var my_li = document.createElement('li');
				Element.extend(my_li);
				if (i==(nodetails-1)) my_li.addClassName('noafter')
				var my_a = document.createElement('a');
				Element.extend(my_a);
				my_a.innerHTML=view_array[i][0];
				my_a.writeAttribute({rel:view_array[i][1]+'.jpg'});
				my_a.observe('click', function(event){
					var my_img=Event.element(event).up(2).down('img');
					Element.extend(my_img);
					var splice = (my_img.readAttribute('src').indexOf('.jpg')!=-1)?my_img.readAttribute('src').indexOf('.jpg'):my_img.readAttribute('src').length();
					if ((my_img.readAttribute('src').substr(splice-1, 1)=='1') ||(my_img.readAttribute('src').substr(splice-1, 1)=='2')) {
					  splice= splice-8;
					}
					my_img.writeAttribute({src:my_img.readAttribute('src').slice(0,splice)+Event.element(event).readAttribute('rel')});
					Event.element(event).up('ul').select('a').each(function(el){
						el.removeClassName('active');
					});
					Event.element(event).addClassName('active');
				 });

				my_li.insert(my_a);
				my_ul.insert(my_li);
			}
			item.insert(my_ul);
		});
	}
})

function openform(e){
	popupurl=Event.findElement(e, 'a').getAttribute('href');
	popup=window.open(popupurl,'popup','height=180,width=320');
	popup.focus()
	Event.stop(e);
}

function displayCountdown(){
	var nowdate = new Date();
	var mstocd=(cddate-nowdate);
	var daystocd = (mstocd/86400000).floor();
	if (mstocd>0){
		hourstocd = ((mstocd%86400000)/3600000).floor();
		minstocd = ((mstocd%3600000)/60000).floor();
		secstocd = ((mstocd%60000)/1000).floor();
		var p = new Element('p', { 'id': 'countdown'});
		p.insert(months[thismonth]+' print expires in <span> '+daystocd+' Days | '+hourstocd+' Hours | '+minstocd+' Minutes | '+secstocd+' Seconds |</span>');
		$('countdown').replace(p);
	}else{
		return false;
	}
}