$(document).ready(
	function(){
		$('a[@rel*=lightbox]').lightBox();
		
		$('#name').attr('value', $.cookie('feedback_name') );
      	$('#feedback').attr('value', $.cookie('feedback_feedback') );
		$('a.top').bind('click', function(){
			$('html, body').animate({scrollTop:0}, 'slow');
		});
		$('a.question').bind('click', function(){
			$(this).hide();
			$("#ajax-feedback").slideToggle();
			return false;
		});
	/*
		$("#banner li").fadeTo("slow", 0.6); // This sets the opacity of the thumbs to fade down to 60% when the page loads

		$("#banner li").hover(function(){
			$(this).fadeTo("slow", 1.0); // This should set the opacity to 100% on hover
		},function(){
			$(this).fadeTo("slow", 0.6); // This should set the opacity back to 60% on mouseout
		});
	*/
	}		
);


document.onkeydown = function (e)
{
	if (!e)
	{
		e = window.event;
	}
	if (e.ctrlKey) //arrow navigation
	{
		var code = e.keyCode ? e.keyCode : (e.which ? e.which : 0);
		var arrow = code == 37 ? document.getElementById('prev_page') : (code == 39 ? document.getElementById('next_page') : 0);
		if (arrow)
		{
			location.href = arrow.href
		}
	}
}

function replaceString(oldS, newS, fullS) 
{
   if( fullS && fullS != '' )
   {
	   for (var i = 0; i < fullS.length; i++) 
	   {
	      if (fullS.substring(i, i + oldS.length) == oldS) {
	         fullS = fullS.substring(0, i) + newS + fullS.substring(i + oldS.length, fullS.length);
	      }
	   }
   	   return fullS;
   }
   return false;
}

function find()
{
	var newsearch = $('#search').attr('value');

	if ( newsearch == '')
	{
		$('#search').focus();
		return false;
	}
	
	var url = '';
	var pagepattern = /(page\/\d+)/;
	var action = $('#searchform').attr('action');
	if ( pagepattern.test(action) )
	{
		action = replaceString(RegExp.$1, '', action);
	}
	
	$('#searchform').attr('action',action);

	return true;
}

function checkForm()
{
	for (i=0; i < arguments.length; i++)
	{
		if ( $('#'+arguments[i]).attr('value') == '')
		{
			$('#'+arguments[i]).addClass('warning');
			return false;
		}
	}
	
	return true;	
}

function getForm( form, e )
{
    JsHttpRequest.query(
      '/ajax/members/form/',
      {
        'form': form
      },
      function(result, errors) {
      	$('#form-ajax').empty();
		$('#TB_ajaxContent').empty();
        $('#form-ajax').append(result['result']);

		$('#'+e.id).each(function() {
			var t = this.title || this.name || null;
			var a = this.href || this.alt;
			var g = this.rel || false;
			tb_show(t,a,g);
			this.blur();
			return false;
		});
      },
      true
    );
}

function sendOrder( e )
{
	$('#feedback-result').empty().hide();
	
	if ( $('#name').attr('value') == '')
	{
		$('#name').addClass('warning');
		return false;
	}

	if ( $('#feedback').attr('value') == '')
	{
		$('#feedback').addClass('warning');
		return false;
	}

	if ( $('#message').attr('value') == '')
	{
		$('#message').addClass('warning');
		return false;
	}
	
    JsHttpRequest.query(
      '/ajax/feedback/order/',
      {
        'q': document.getElementById(e),
        'type': 'send'
      },
      function(result, errors) {
      	$('#feedback-result').append(result['result']).show();
      	$('#message').attr('value','');
      	$.cookie('feedback_name', $('#name').attr('value'), {path: '/'});
      	$.cookie('feedback_feedback', $('#feedback').attr('value'), {path: '/'});
      },
      true
    );
    
    return false;
}

function getNext( inventory_id )
{
    JsHttpRequest.query(
      '/ajax/catalogue/next/',
      {
        'inventory_id': inventory_id
      },
      function(result, errors) {
      	$('#content-flow').empty();
        $('#content-flow').append(result['result']);
      },
      true
    );
}

