var $keywords 	= null;
var $location 	= null;
var $search		= null;

$(function(){
	
	$keywords 	= $('#keywords');
	$location 	= $('#location');
	$form		= $('#form');
	
	$('.engine_skin').each(function(i){
		var engineName = $(this).attr('id');
		Engine.register(engineName,$(this).attr('title'));
		Engine.refresh(engineName);
		
		$('.init').each(function(i){
			if($(this).attr('rel') == engineName) $(this).toggleClass('selected');
		});
	});
	
	$('.skey').val($keywords.val());
	$('.sloc').val($location.val());
	
	$keywords.focus(function(){
		if($(this).val() == 'keywords') $(this).val('').css({color:'#333'});
		else $(this).css({color:'#333'});
	}).blur(function(){
		if($(this).val() == '') $(this).val('keywords').css({color:'#999'});
	});
	
	$location.focus(function(){
		if($(this).val() == 'location') $(this).val('').css({color:'#333'});
		else $(this).css({color:'#333'});
	}).blur(function(){
		if($(this).val() == '') $(this).val('location').css({color:'#999'});
	});
	
	$form.submit(function(){
		Core.search();
		return false;
	});
	
	initLocalForms();
	
	$('.engine_body').height(Core.getEngineBodyHeight());
	$('.engine').height(Core.getEngineHeight());
	$('#engines').width($(window).width()-230);
	
	$('#bottom').height(Core.getDocumentInnerHeight()-73);
	
	$(window).resize(function(){
		$('.engine_body').height(Core.getEngineBodyHeight());
		$('.engine').height(Core.getEngineHeight());

		$('#engines').width(Core.getWindowWidth()).height(Core.getEngineHeight()+20);
	});
	
	$('.init').live('click',function(){
		Engine.toggle($(this).attr('rel'),$(this).html(),true);
		return false;
	});
	
	if(SimpleAjaxUrl.is('keywords')) 
	{
		Core.restoreState(
				SimpleAjaxUrl.get('keywords'),
				SimpleAjaxUrl.get('location'),
				SimpleAjaxUrl.get('engines'),
				SimpleAjaxUrl.get('menu')
		);
	}
	else if(SimpleCookie.is('state'))
	{
		Core.restoreState(
				SimpleCookie.get('state').keywords,
				SimpleCookie.get('state').location,
				SimpleCookie.get('state').engines,
				SimpleCookie.get('state').menu
		);
	}
	
	Core.toggleMenu();
	Core.initFilters();
	Core.initSaveButtons();
	Core.initUnSaveButtons();
	Core.initRefreshButtons();
	
	//Facebook.init();
	
	$('.move_right').live('click',function(){
		var rel = $(this).attr('rel');
		
		$('.engine_skin:not(:hidden)').each(function(i){
			var id 			= $(this).attr('id');
			window.next 	= $(this).attr('id');
			if(!window.first) window.first	= id;
			
			if(window.nextReady && !window.insertedAfter)		
			{
				if(window.next != rel) 
				{
					$('#'+rel).insertAfter('#'+window.next);
					window.insertedAfter = true;
				}
			}
			
			if(id == rel) window.nextReady = true;
		});
		
		if(!window.insertedAfter) 
			if(window.first != rel) $('#'+rel).insertBefore('#'+window.first);
		
		window.nextReady 	= false;
		window.next			= false;
		window.first		= false;
		window.insertedAfter= false;
		
		Core.updateUrl();
		
		$('.engine_skin:not(:hidden)').each(function(i){
			if ($(this).attr('id') == rel) {
				$('#engines').animate({
					scrollLeft: i * 280
				}, 600);
			}
		});
		return false;
	});
	
	$('.move_left').live('click',function(){
		var rel = $(this).attr('rel');
		
		$('.engine_skin:not(:hidden)').each(function(i){
			
			var id 		= $(this).attr('id');
			if(id == rel)
			{
				if(window.prev) 
				{
					$('#'+rel).insertBefore('#'+window.prev);
					window.insertedPrev = true;
				}
			}
						
			window.prev 	= $(this).attr('id');
		});
		
		if(!window.insertedPrev)
		{
			if(rel != window.prev) 
				$('#'+rel).insertAfter('#'+window.prev);
		}
		
		window.prev 		= false;
		window.insertedPrev = false;
		
		Core.updateUrl();
		
		$('.engine_skin:not(:hidden)').each(function(i){
			if ($(this).attr('id') == rel) {
				$('#engines').animate({
					scrollLeft: i * 280
				}, 600);
			}
		});
		return false;
	});
	
	/*sorting*/
	//Core.initSortable();
	
	Core.tooltip();
	
	var i = new Image();
	i.src = "/images/signin_twitter_clicked.png"; 
	
	var i1 = new Image();
	i1.src = "/images/signin_twitter_on.png";

	$(function(){
		
		$img = $("#signin > img");
	
		$("#signin").click(function(){
			$img.attr("src","/images/signin_twitter_clicked.png")
		});
		
		$("#signin").mouseover(function(){
			$img.attr("src","/images/signin_twitter_on.png")
		});
		
		$("#signin").mouseout(function(){
			$img.attr("src","/images/signin_twitter_off.png")
		});
	});
});

function initLocalForms()
{
	$('.skey').focus(function(){
		if($(this).val() == 'keywords') $(this).val('').css({color:'#333'});
		else $(this).css({color:'#333'});
	}).blur(function(){
		if($(this).val() == '') $(this).val('keywords').css({color:'#999'});
	});
	
	$('.sloc').focus(function(){
		if($(this).val() == 'location') $(this).val('').css({color:'#333'});
		else $(this).css({color:'#333'});
	}).blur(function(){
		if($(this).val() == '') $(this).val('location').css({color:'#999'});
	});
}