
/**
 * Calendar functions
 * @author Kim Westerberg <kim.westerberg@really.fi>
 */ 
g_prevOpenId = null; // global variable needed for previous class open clearing

function changeMark(stamp)
{
	var obj;
	var stampParts = stamp.split('.');
	var years = stampParts.pop();
	var months = stampParts.pop();
	if (months < 10) {
		months = '0' + months;
	}
	var days = stampParts.pop();
	if (days < 10) {
		days = '0' + days;
	}
	if (obj = document.getElementById('stamp')) {
		obj.value = years + months + days;
	}
	if (obj = document.getElementById('mark')) {
		obj.value = 5;
	}
}

/**
 * Handles displaying of calendar and choosing different weeks
 * @author Kim Westerberg <kim.westerberg@really.fi>
 */
function do_displayCalendar_cb(calendarData) {
	document.getElementById("greetingsProcess").innerHTML = calendarData;
	
    if (g_prevOpenId != null)
    	if (document.getElementById(g_prevOpenId) != null)
			document.getElementById(g_prevOpenId).className = "open";
	
}
function do_displayCalendar(week, year)
{
	x_doShipmentCalendar(week, year, do_displayCalendar_cb);
}

/**
 * Handles clicking one shipment date
 * @author Kim Westerberg <kim.westerberg@really.fi>
 */
function do_markCalendar_cb(calendarData) {
	document.getElementById("shipmentData").innerHTML = calendarData;
}
function do_markCalendar(stamp, mark)
{
    if (g_prevOpenId != null)
    	if (document.getElementById(g_prevOpenId) != null)
    		document.getElementById(g_prevOpenId).className = null;

	var openId = "id" + stamp + mark;

	if (document.getElementById(openId) != null)
		document.getElementById(openId).className = "open";
		
   	g_prevOpenId = openId;

   	var pickTime;

   	switch(mark) {
   		default: pickTime = ''; break;
		case '2': pickTime = "Aamupäivä"; break; 
		case '3': pickTime = "Iltapäivä"; break; 
		case '4': pickTime = "Ilta"; break; 
		case '5': pickTime = "Aukioloajat"; break;
	}
	var days = stamp.substr(6,2);
	var month = stamp.substr(4,2);
	
   	var displayStamp = '';
   	
   	if (month) {
   		displayStamp = pickTime + " " + days + "." + month;
   	}
   	
	document.getElementById('chosenMarkStamp').innerHTML = displayStamp;
   	
	x_doMarkCalendar(stamp, mark, do_markCalendar_cb);
}
function cardFields(id)
{
    var home = "homeFields";
    var office = "officeFields";
    var hospital = "hospitalFields";

	if (id != "") {
    	var show = "";
    	var hide1 = "";
    	var hide2 = "";
    
		switch(id) {
			case 1:
				show = home;
				hide1 = office;
				hide2 = hospital; 
			break;
			case 2:
				show = office;
				hide1 = home;
				hide2 = hospital; 
			break;
			case 3:
				show = hospital;
				hide1 = home;
				hide2 = office; 
			break;
		}
		document.getElementById(show).style.display = "block";
		document.getElementById(hide1).style.display = "none";
		document.getElementById(hide2).style.display = "none";
	} else {
		id = home;
		document.getElementById(id).style.display = "block";
	}
}
function change(picId,newPic) {
  var newSrc = "http://www.juhlatukku.fi/files/images/compositions//"+picId+"/"+newPic+".png";
  
   document.getElementById(picId).src = newSrc;
   document.getElementById("field_" + picId).value = newPic+".png";
}

function confirm_cancel() {
	vastaus = confirm("Haluatko varmasti tyhjentää ostoskorin?");
	if(vastaus) {
		return true;
	}
	else {
		return false;
	}
}

function changeValue(input) {
	//alert(input);
	if(input.value == "Määrä") {
		input.value = "";
	}
}

function show(id) {
	//alert(id);
	
	if (id == "passwordDiv") {
//		document.getElementById("registerDiv").style.visibility="hidden";
		document.getElementById("passwordDiv").style.visibility="visible";
	}
	/*
	if (id == "registerDiv") {
		document.getElementById("passwordDiv").style.visibility="hidden";
		document.getElementById("registerDiv").style.visibility="visible";
	}
	*/
	//document.getElementById(id).style.visibility="visible";
}

function hide(id) {
	//alert(id);
	targetDiv = document.getElementById(id);
	
	targetDiv.style.visibility="hidden";
}

function updatePaymentRadio(deliveryType) {
	if (deliveryType.value == "FETCH") {
		document.deliveryForm.paymentType[0].disabled = false;
		if (document.deliveryForm.paymentType[1].checked == true)
			document.deliveryForm.paymentType[0].checked = true;
		document.deliveryForm.paymentType[1].disabled = true;
	} else {
		document.deliveryForm.paymentType[0].disabled = true;
		if (document.deliveryForm.paymentType[0].checked == true)
			document.deliveryForm.paymentType[1].checked = true;
		document.deliveryForm.paymentType[1].disabled = false;
	}
}

function showError() {
	if(document.getElementById("errorBg").style.display != 'none') {
		document.getElementById("errorBg").style.visibility='hidden';
		document.getElementById("errorBg").style.display='none';
		document.getElementById("errorBlack").style.visibility='hidden';
		document.getElementById("errorBlack").style.display='none';
	}
	else {
		document.getElementById("errorBg").style.visibility='visible';
		document.getElementById("errorBg").style.display='block';
		document.getElementById("errorBlack").style.visibility='visible';
		document.getElementById("errorBlack").style.display='block';
	}
}


var ScrollBar = new Class({

	Implements: [Events, Options],

	options: {
		maxThumbSize: 10,
		wheel: 8
	},

	initialize: function(content, track, thumb, options){
		this.setOptions(options);

		this.content = $(content);
		this.track = $(track);
		this.thumb = $(thumb);

		this.bound = {
			'start': this.start.bind(this),
			'end': this.end.bind(this),
			'drag': this.drag.bind(this),
			'wheel': this.wheel.bind(this),
			'page': this.page.bind(this)
		};

		this.position = {};
		this.mouse = {};
		this.update();
		this.attach();
	},

	update: function(){

		this.contentSize = this.content.offsetHeight;
		this.contentScrollSize = this.content.scrollHeight;
		this.trackSize = this.track.offsetHeight;

		this.contentRatio = this.contentSize / this.contentScrollSize;

		this.thumbSize = (this.trackSize * this.contentRatio).limit(this.options.maxThumbSize, this.trackSize);

		this.scrollRatio = this.contentScrollSize / this.trackSize;

		this.thumb.setStyle('height', this.thumbSize);

		this.updateThumbFromContentScroll();
		this.updateContentFromThumbPosition();
	},

	updateContentFromThumbPosition: function(){
		this.content.scrollTop = this.position.now * this.scrollRatio;
	},

	updateThumbFromContentScroll: function(){
		this.position.now = (this.content.scrollTop / this.scrollRatio).limit(0, (this.trackSize - this.thumbSize));
		this.thumb.setStyle('top', this.position.now);
	},

	attach: function(){
		this.thumb.addEvent('mousedown', this.bound.start);
		if (this.options.wheel) this.content.addEvent('mousewheel', this.bound.wheel);
		this.track.addEvent('mouseup', this.bound.page);
	},

	wheel: function(event){
		this.content.scrollTop -= event.wheel * this.options.wheel;
		this.updateThumbFromContentScroll();
		event.stop();
	},

	page: function(event){
		if (event.page.y > this.thumb.getPosition().y) this.content.scrollTop += this.content.offsetHeight;
		else this.content.scrollTop -= this.content.offsetHeight;
		this.updateThumbFromContentScroll();
		event.stop();
	},

	start: function(event){
		this.mouse.start = event.page.y;
		this.position.start = this.thumb.getStyle('top').toInt();
		document.addEvent('mousemove', this.bound.drag);
		document.addEvent('mouseup', this.bound.end);
		this.thumb.addEvent('mouseup', this.bound.end);
		event.stop();
	},

	end: function(event){
		document.removeEvent('mousemove', this.bound.drag);
		document.removeEvent('mouseup', this.bound.end);
		this.thumb.removeEvent('mouseup', this.bound.end);
		event.stop();
	},

	drag: function(event){
		this.mouse.now = event.page.y;
		this.position.now = (this.position.start + (this.mouse.now - this.mouse.start)).limit(0, (this.trackSize - this.thumbSize));
		this.updateContentFromThumbPosition();
		this.updateThumbFromContentScroll();
		event.stop();
	}

});

function makeScrollbar(content,scrollHeight,scrollbar,handle,horizontal,ignoreMouse){
	scrollbar.setStyle('height', scrollHeight);
	var handleHeight = (scrollHeight / content.getSize().scrollSize.y * scrollHeight);
	if(handleHeight > scrollHeight) {
		handleHeight = scrollHeight;
	}
	handle.setStyle('height', handleHeight);
	var steps = (horizontal?(content.getSize().scrollSize.x - content.getSize().size.x):(content.getSize().scrollSize.y - content.getSize().size.y))
	var slider = new Slider(scrollbar, handle, {	
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	}).set(0);
	if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 15;
			if(step < 0)
				step = 0;
			slider.set(step);
		});
	}
		
	// Stops the handle dragging process when the mouse leaves the document body.
	// $(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
}

window.addEvent('domready', function(){
	if(( $('mainBalloonBg')) ) { 
		makeScrollbar( $('mainBalloonBg'), 400, $('mainBalloonScroll'), $('mainBalloonHandle'), false, false );
		var elementArray = $('smallPictures').getElements("div[id$=SC]");
		elementArray.each(function(el) {
			el.setStyle("display", "none");
		});
		
		if(window.ie) {
			$('mainBalloonSC').setStyle("display", "block");
			
			$('mainBalloonNav').addEvent('click', function(){
				makeScrollbar( $('mainBalloonBg'), 400, $('mainBalloonScroll'), $('mainBalloonHandle'), false, false );
			});
			
			if(( $('subBalloonNav')) ) { 
				$('subBalloonNav').addEvent('click', function(){
					makeScrollbar( $('subBalloonBg'), 400, $('subBalloonScroll'), $('subBalloonHandle'), false, false );
				});
			}
			
			if(( $('baseNav')) ) { 
				$('baseNav').addEvent('click', function(){
					makeScrollbar( $('baseBg'), 400, $('baseScroll'), $('baseHandle'), false, false );
				});
			}
		}
		
		$$('a.smallPicturesheader').addEvent('click', function(){
			var helper = $(this).id;
			
			$$('a.smallPicturesheader').removeClass("navOpen");
			$(this).addClass("navOpen");
			
			helper = helper.substring(0, helper.indexOf('Nav'));
			var helperBg = helper + "SC";
			
			var elementArray = $('smallPictures').getElements("div[id$=SC]");
			elementArray.each(function(el) {
				el.setStyle("display", "none");
			});
			
			$(helperBg).setStyle("display", "block");
		});		
		
		if(window.ie != true) {
			$('mainBalloonSC').setStyle("display", "block");
			
			$('mainBalloonNav').addEvent('click', function(){
				makeScrollbar( $('mainBalloonBg'), 400, $('mainBalloonScroll'), $('mainBalloonHandle'), false, false );
			});
			
			if(( $('subBalloonNav')) ) { 
				$('subBalloonNav').addEvent('click', function(){
					makeScrollbar( $('subBalloonBg'), 400, $('subBalloonScroll'), $('subBalloonHandle'), false, false );
				});
			}
			
			if(( $('baseNav')) ) { 
				$('baseNav').addEvent('click', function(){
					makeScrollbar( $('baseBg'), 400, $('baseScroll'), $('baseHandle'), false, false );
				});
			}
		}
	}

	$$('.cardTextInput').addEvents({
		'focus' : function(){
			$(this).setStyle("color", "#000");
			switch($(this).getProperty('id')) {
			case "cardTitle":
				if(this.getProperty('value') == 'Onnea!')
					this.setProperty('value', '');
				break;
			case "cardText":
				if(this.getProperty('value') == 'Hyvää syntymäpäivää Maija Mehiläinen!')
					this.setProperty('value', '');
				break;
			case "cardSender":
				if(this.getProperty('value') == 'Toivottaa Minna')
					this.setProperty('value', '');
				break;
			}
		},
		'blur' : function(){
			if(this.getProperty('value') == "") {
				$(this).setStyle("color", "#d0d0d0");
				
				switch($(this).getProperty('id')) {
				case "cardTitle":
					this.setProperty('value', 'Onnea!');
					break;
				case "cardText":
					this.setProperty('value', 'Hyvää syntymäpäivää Maija Mehiläinen!');
					break;
				case "cardSender":
					this.setProperty('value', 'Toivottaa Minna');
					break;
				}
			}
		}
	});
});


/***********************************************
* Textarea Maxlength script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function ismaxlength(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}


