function constructLightBox()
{
	$("a[rel*=lightbox]").fancybox({
		'imageScale': false,
		'centerOnScroll': false
	});
}
$(document).ready(
	function()
	{
		constructLightBox();
		
		$("a.external").each(function(){			
			$(this).attr("onClick", "javascript: pageTracker._trackPageview('"+$(this).attr("href")+"f/"+window.location.href.split("//")[1]+"');");
			$(this).attr("href",$(this).attr("title"));
			$(this).removeAttr("title");
			$(this).removeAttr("class");
		});
		
		
		$("#clients-past-more").bind("click", function(){
			$("#clients-past").fadeIn();
			$(this).hide();
			return false;
		});
		
		$(".js-hidden").hide();  // то что должно включаться Javascript'ом
		$(".js-visible").show(); // контроллер js, который переключает .js-hidden 
		
		
		$("div.faq dt").bind("click", function(){
			$("#a"+$(this).attr("id")).toggle();
			$(this).toggleClass("plus");
			$(this).toggleClass("minus");
		});
		
		$("div.faq dt a").bind("click", function(){
			$(this).parent().click();
			return false;
		});

		$("#faq_control a").bind("click",function(){
			$("#faq_control a").toggle();
			$("div.faq dd").toggle();	
			$("div.faq dt").toggleClass("plus");
			$("div.faq dt").toggleClass("minus");
			return false;
		});

	    $('a[href$="mp3"]').flash(
	        { src: '/-/plugins/jquery-flash/player.swf', height: 20, width: 450 },
	        { version: 7 },
	        function(htmlOptions) {
	            $this = $(this);
	            
	            htmlOptions.flashvars.file = $this.attr('href');
	            $this.before($.fn.flash.transform(htmlOptions));
	            
	            $this.hide();						
	        }
	    );

	    $('a[href$="flv"]').flash(
	        { src: '/-/plugins/jquery-flash/player.swf', height: 370, width: 450, allowfullscreen: true },
	        { version: 7 },
	        function(htmlOptions) {
	            $this = $(this);
	            
	            htmlOptions.flashvars.file = $this.attr('href');
	            $this.before($.fn.flash.transform(htmlOptions));
	            
	            $this.hide();						
	        }
	    );

	    $('a[href*="youtube.com"]').flash(
	        { src: '/-/plugins/jquery-flash/player.swf', height: 370, width: 450, allowfullscreen: true },
	        { version: 7 },
	        function(htmlOptions) {
	            $this = $(this);
	            
	            htmlOptions.flashvars.file = $this.attr('href');
	            $this.before($.fn.flash.transform(htmlOptions));
	            
	            $this.hide();						
	        }
	    );		
	}
);
// ==================================================================
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 sendFeedback(e)
{
	$('div.message').remove();
	
    JsHttpRequest.query(
      '/ajax/feedback/send/',
      {
        'q': e
      },
      function(result, errors) {
      	if ( result['result'] )
      	{
      		$div = $('<div />').attr('class', 'message').html( result['result'] ).css('color','green').append('<br /><br />');
      		$('#feedback_form input[type!=submit], textarea').attr('value', '');
      	}
      	else
      	{
      		$div = $('<div />').attr('class', 'message').html( errors ).css('color','red').append('<br /><br />');
      	}
      	
      	$('#feedback_form h2').after( $div );
      },
      true
    );
    
    return false;
}

// ==================================================================
// ==================================================================
// ==================================================================

// -----------------------------------------------------------
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 checkForm()
{
	for (i=0; i < arguments.length; i++)
	{
		if ( $('#'+arguments[i]).attr('value') == '')
		{
			$('#'+arguments[i]).addClass('warning');
			
			return false;
		}
	}
	
	return true;	
}

function registerForm()
{
	return true;
}

function loginForm()
{
	return true;
}

function passwordForm()
{
	return true;	
}

function profileForm()
{
	return true;
}

function forgetForm()
{
	return true;
}

function orderdetailsForm()
{
	return true;
}

function finishForm()
{
	return true;
}

function activateForm()
{
	return true;
}

function getForm( form, handler, referer, e)
{
    JsHttpRequest.query(
      '/ajax/'+handler+'/form/',
      {
        'form': form,
        'referer': referer
      },
      function(result, errors) {
      	$('#'+e.id).overlaySynthetic(function()	{
      		$("table.sign-form input:first").focus();
      	});
		
      	$('#signin form').remove();			
      	$('#signin .form').remove();
      	$('#signin div.byname').remove();

      	$('#signin').append(result['result']);
      },
      true
    );
}

function getBlock( block, handler, referer, e )
{
    JsHttpRequest.query(
      '/ajax/'+handler+'/byname/',
      {
        'block': block,
        'referer': referer
      },
      function(result, errors) {
      	if ( result['result'].length > 0 )
      	{
      		$('#'+e.id).overlaySynthetic();
			
	      	$('#signin form').remove();			
	      	$('#signin .form').remove();
	      	$('#signin div.byname').remove();

	      	$('#signin').append(result['result']);
      	}
      },
      true
    );
}
