var formdata = new Array;

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

$(document).ready(function() {
	$('.toggle').each(function() {
		formdata[$(this).attr('name')] = $(this).attr('value');
		
		$(this).focus(function() {
			if (formdata[$(this).attr('name')] == $(this).attr('value'))
				$(this).attr('value', '');
		});
		
		$(this).blur(function() {
			if (!$(this).attr('value'))
				$(this).attr('value', formdata[$(this).attr('name')]);
		});
	});
	
	$('.display-by').change(function() {
		document.location = $(this).attr('rel') + '&perpage=' + this.value;
	});
	
	$('.popup form').submit(function() {
		var elems = $(this).get(0);
		
		var vars = {};
		
		for (var i =0; i < elems.length; i++)
			if (elems[i].name) {
				if (elems[i].name == "emails") {
					var emails = elems[i].value.split(',');
					//&& elems[i].value.search(/^[A-Za-z0-9\-_]+(\.[A-Za-z0-9\-_]+)*@([A-Za-z0-9\-_\.]+\.)+[A-Za-z]{2,8}$/i) == -1) {
					//alert('
					for (var j = 0; j < emails.length; j++)
						var em = trim(emails[j]);
						if (em.search(/^[A-Za-z0-9\-_]+(\.[A-Za-z0-9\-_]+)*@([A-Za-z0-9\-_\.]+\.)+[A-Za-z]{2,8}$/i) == -1) {
							$('.error').text('Incorrectly filled email addresses');
							$(elems[i]).addClass('inputError');
							$(elems[i]).focus();
							return false;
						} else {
							$(elems[i]).removeClass('inputError');
						}
				}
				
				if (elems[i].name == "email") {
					if (elems[i].value.search(/^[A-Za-z0-9\-_]+(\.[A-Za-z0-9\-_]+)*@([A-Za-z0-9\-_\.]+\.)+[A-Za-z]{2,8}$/i) == -1) {
						$('.error').text('Incorrectly filled email address');
						$(elems[i]).addClass('inputError');
						$(elems[i]).focus();
						return false;
					} else {
						$(elems[i]).removeClass('inputError');
					}
				}
				
				if (elems[i].name == "friend_email") {
					if (elems[i].value.search(/^[A-Za-z0-9\-_]+(\.[A-Za-z0-9\-_]+)*@([A-Za-z0-9\-_\.]+\.)+[A-Za-z]{2,8}$/i) == -1) {
						$('.error').text('Incorrectly filled email address');
						$(elems[i]).addClass('inputError');
						$(elems[i]).focus();
						return false;
					} else {
						$(elems[i]).removeClass('inputError');
					}
				}
				
				eval('vars.' + elems[i].name + ' = "' + elems[i].value + '";');
			}
				
		$.post($(this).attr('action'), vars, function(data) {
			for (var i =0; i < elems.length; i++)
				if (elems[i].className == 'to_be_removed')
					elems[i].value = '';
			if (data) {
				$('.popup_body').hide();
				$('.popup_message').show().text(data);
				
			}
		});
		
		return false;
	});
	
	$('.lightbox').lightBox();
	
	$('.open_popup').click(function() {
		$($(this).attr('rel')).show();
		return false;
	});
	
	$('.popup_title a').click(function() {
		$('.popup').hide();
		$('.popup_message').hide();
		$('.popup_body').show();
		$('.error').text("");
		$('input').removeClass('inputError');
		return false;
	});
	
	$('#pr_gallery a').click(function() {
    $('#pr_gallery a.current').removeClass('current');
    $(this).addClass('current');
    
    $('#pr_image img').attr('src', $(this).attr('href'));
    return false;
	});

	$('.gift_wrap').each(function() {
		$(this).click(function() {			
			if ($(this).hasClass('yes')) {
				$(this).removeClass('yes');
				$(this).addClass('no');
				$(this).next().attr('checked', false);
			} else {
				$(this).removeClass('no');
				$(this).addClass('yes');
				$(this).next().attr('checked', true);
			}
		});
	});

	var margin = 0;
	var timer;

	function slider() {
		if (margin > parseInt($('#scroll').width())) {
			margin = -200;
		}

		margin += 5;
		$('#scroll a').css('left', margin + 'px');
		
		timer = setTimeout(slider, 100);
	}
	
	slider();
	
	$('#scroll').hover(function() {
		clearTimeout(timer);
	}, function() {
		slider();
	});
	
	// startSlide();
});

function startSlide(){

 	if(document.getElementById("scroll")) {
    slide();
	} else {
    setTimeout("startSlide()",1000);
	}
}
		
function slide(){

  /*increase timeX to slow speed, decrease it to go faster*/
  var timeX=22;
  var newsTick=document.getElementById("scroll");
  var myMar=document.getElementById("scroll_a");
  var newsTickLeft=newsTick.offsetLeft;
  var myMarLeft=myMar.offsetLeft;
  var myMarWidth=myMar.offsetWidth;
  var timer=setTimeout("slide()",timeX);


  function speedTime(){
    slide();
  }
  
  function slowTime(){
    clearTimeout(timer);
  }
	
  newsTick.onmouseover=slowTime;
  newsTick.onmouseout=speedTime;

  if (myMarLeft>-myMarWidth) {
    myMar.style.left=myMarLeft-1+"px";
  } else {
    myMar.style.left=newsTick.offsetWidth+"px";
  }
}


/**
 / THIRD FUNCTION
 * getPageSize() by quirksmode.com
 *
 * @return Array Return an array with page width, height and window width, height
 */
function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};
/**
 / THIRD FUNCTION
 * getPageScroll() by quirksmode.com
 *
 * @return Array Return an array with x,y page scroll values.
 */
function ___getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};
