$.ajaxSetup ({
    // Disable caching of AJAX responses
    cache: false
});

function getRandomInt (min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}


$(function(){
	var cat_id=0;
	function hidePage(){
		if($('#'+cat_id).find('.c'+cat_id+'_img').length > 0)
		{
			$obj=$('#'+cat_id).find('.c'+cat_id+'_img');
			if($obj.attr('rel')!='0')
			{
				$obj.css('background','url("'+$obj.attr('rel')+'") no-repeat center center');
			}
		}
		$('#background,#loading,.curve,.hands').fadeOut();
		$('html, body').animate({scrollTop:0}, 'slow');
	}
	
	$('.cat_link').click(function(){
		
		cat_id=$(this).attr('id');
		$('#background').css({height:$(document).height()+'px',width:$(document).width()+'px',opacity:0.5}).fadeIn();
		$('#loading').css({left:(($('.container').width()-148)/2)+'px'}).fadeIn();
		$('#curve-content').load($(this).attr('rel'), function(){
			$('.curve').fadeIn();
			hand_1=Math.floor(Math.random()*2+1);
			hand_2=Math.floor(Math.random()*2+1);
			
			hand_y_1=getRandomInt(100,($(this).height()-250));
			hand_y_2=getRandomInt(100,($(this).height()-250));
			
			$('.left_hand_'+hand_1).css('top',hand_y_1+'px').show();
			$('.right_hand_'+hand_2).css('top',hand_y_2+'px').show();
			
			$('#loading').hide();
			
		});
		
			
		
	}).hover(function() {
		
		$(this).animate({opacity: "0.8"}, {queue:false,duration:300});
	}, function() {
		$(this).animate({opacity: "1"}, {queue:false,duration:300});
			
	});
	
	$('.curve-close-button').click(function(){
		hidePage();
	});
	
	$(document).keyup(function(e) {
			if (e.keyCode == 27) { hidePage(); }   // esc
		});

	
	  $('.search img').click(function(){
		if(validateNotEmpty($('#txtSearch').val()))
		{
			$.ajax({
				type: 'POST',
				url: '/welcome/search',
				data: { txtSearch: $('#txtSearch').val() },
				success: function(data){
							$('#background').css({height:$(document).height()+'px',width:$(document).width()+'px',opacity:0.5}).fadeIn();
											
							$('#curve-content').html(data);
							$('.curve').fadeIn();
							hand_1=Math.floor(Math.random()*2+1);
							hand_2=Math.floor(Math.random()*2+1);
							
							hand_y_1=Math.floor(Math.random()*($(this).height()/2)+53);
							hand_y_2=Math.floor(Math.random()*($(this).height()/2)+53);
							
							$('.left_hand_'+hand_1).css('top',hand_y_1+'px').show();
							$('.right_hand_'+hand_2).css('top',hand_y_2+'px').show();
							
							
				}
				
				});
		}
	});
	
	$('.back_to_top').live('click',function(){
		$('html, body').animate({scrollTop:0}, 'slow');
        return false;
	});
	  
})



/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}
function trimAll( strValue ) {
/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function validateNotEmpty( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is not all
  blank (whitespace) characters.

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
*************************************************/
   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0){
     return true;
   }
   return false;
}



function validateEmail( strValue) {
/************************************************
DESCRIPTION: Validates that a string contains a
  valid email pattern.

 PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS: Accounts for email with country appended
  does not validate that email contains valid URL
  type (.com, .gov, etc.) or valid country suffix.
*************************************************/
var objRegExp  = /^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$/;

  //check for valid email
  return objRegExp.test(strValue);
}

