/* ==== FONT SIZE ==== */
var fontsize = 1;

$(document).ready(function() {

	$('#decrease').get(0).onclick = function() { addFontSize(-0.05) }
	$('#increase').get(0).onclick = function() { addFontSize(0.05) }
	
	/*var savedFontsize = readCookie('fontSize');

	if(savedFontsize != null) {
		fontsize = parseInt(savedFontsize);
		$('container').style.fontSize = fontsize + 'em'
	}*/

});

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function addFontSize(add) {
	
	fontsize += add;
	
	if(fontsize < 0.8)
		fontsize = 0.8;
		
	if(fontsize > 1.2)
		fontsize = 1.2

	//eraseCookie("fontSize");
	//createCookie('fontSize', fontsize, 30);

	$('#container').get(0).style.fontSize = (fontsize+add) + 'em'
	
}

/* ==== CONTACT FORM ==== */
$(document).ready(function() {

	$('#contactForm input').each(function(k, field) {
		
		field.defValue = field.value;
		$(field).bind('focus', function() {
			if(this.value == this.defValue)
				this.value = '';
		})
		
		$(field).bind('blur', function() {
			if(this.value == '')
				this.value = this.defValue;
		})
		
	})	

});

/* ==== CATEGORY IMAGES ==== */

//Auto scroll functions
var scroller = null;

function startScroll(obj, n) {
	
	if(scroller != null)
		stopScroll();
	
	if(n > 0)	
		scroller = setInterval(function() { scrollToRight(obj) }, 100)
	else
		scroller = setInterval(function() { scrollToLeft(obj) }, 100)
}

function stopScroll() {
	clearInterval(scroller);
	scroller = null;
}

function scrollToLeft(obj) {
	obj.scrollLeft -= 10
}

function scrollToRight(obj) {
	obj.scrollLeft += 10
}

function autoWidth(obj) {
	
	//Enlarge list to contain all images
	var links = obj.getElementsByTagName('img');
	
	if(links.length == 0)
		return false;
	
	var w = links.length * (120);
	w -= 13;
	
	/*for(i = 0; i < links.length; i++)		
		w += Element.extend(links[i]).width + 19;*/
	
	var list = obj.getElementsByTagName('ul');
	var finalW = (w+3)+'px';
	
	if(list.length > 0) {
		$(list[0]).css('width', finalW);
	}
	
}

//Set everything up
$(document).ready(function() {

	if($('.photoslide').size() > 0) {

		$('.photoslide').each(function(k, img) {
			
			//Auto adjust container width
			autoWidth(img)
			
			if(MOBILE == 0) {
				$(img).children(':first').css('opacity', 0.4);
				$(img).prev().css('opacity', 0.4);
			}
			
			
		})
		
		$('.imgselectorContainer').each(function(k, div) {
			
			$(div).bind('mouseover', function() {
				$(this).children(':first').next().css('opacity', 1)			
				$(this).children(':first').next().next().children(':first').css('opacity', 1)			
			})
			
			$(div).bind('mouseout', function() {
				$(this).children(':first').next().css('opacity', 0.4)			
				$(this).children(':first').next().next().children(':first').css('opacity', 0.4)			
			})
			
		})
		
		$('.imgselector a').each(function(k, a) {
			
			var link = a.href;
			$(a).parent().bind('click', function() {
				location.href = link;
			})
				
		})
		
		$('.prev').each(function(k, a) {
	
			$(a).bind('mouseover', function() {
				startScroll($(this).parent().next().children(':first').get(0), -1);
			})
			$(a).bind('mouseout', function() {
				stopScroll();
			})
			
		})
		
		$('.next').each(function(k, a) {
			
			$(a).bind('mouseover', function() {
				startScroll($(this).parent().next().children(':first').get(0), 1);
			})
			$(a).bind('mouseout', function() {
				stopScroll();
			})
			
		})
		
		//MOBILE SAFARI GESTURES
		if(MOBILE) {
		
			var swipeOptions=
			{
				triggerOnTouchEnd : true,	
				swipeStatus : swipeStatus
			}
				
			$('.photselector ul').swipe(swipeOptions)	
			
			function swipeStatus(event, phase, direction, distance)	{
	
				//Get target
				if(event.changedTouches.length == 0)
					return false;
					
				var image = event.changedTouches.item(0).target;
				var ul = $(image).parentsUntil('.photoslide');
				ul = ul.get(ul.size()-1); //div.photselector
	
				//If we are moving before swipe, and we are going Lor R in X mode, or U or D in Y mode then drag.
				if( phase=="move" && (direction=="left" || direction=="right") )
				{
					
					if (direction == "left")
						ul.scrollLeft += distance
					
					else if (direction == "right")
						ul.scrollLeft -= distance
					
				}
			}
			
		}
		
	}

});
