$(document).ready(function($) {
	
	$('html').ajaxStart(function() { 
		$('html').css('cursor', 'wait');
		$('a').css('cursor', 'wait');
		$('div').css('cursor', 'wait');
		$('label').css('cursor', 'wait');
	});
	
	$('html').ajaxStop(function() { 
		$('html').css('cursor', '');
		$('a').css('cursor', '');
		$('div').css('cursor', '');
		$('label').css('cursor', '');
	});
	
	$(".search_by_name").autocomplete(base_url + "index.php/search/by_name/", { minChars:3 });
	$("#search_field").focus();
	
	$('.itip').simpletip({  
		// Configuration properties  
		content: '<div class="loading"></div>', 
		fixed: true,
		focus: true,
		showEffect: 'none',
		onBeforeShow: function() { 
			// Note this refers to the API in the callback function  
			var obj = this.getParent();
			var id  = obj.attr('id').replace('id', '');
			var info;
			
			if(id == '') {
				id = '1';
			}
			
			if(!obj.attr('alt')) {
				info = this.load(base_url + 'index.php/main/view/' + id);
				obj.attr('alt', info);
			}
			
			else {
				this.update(info);
			}
			
		}
	});
	
	$('.tip').simpletip({  
		// Configuration properties  
		content: '<div class="loading"></div>', 
		fixed: true,
		focus: false,
		showEffect: 'none',
		onBeforeShow: function() { 
			// Note this refers to the API in the callback function  
			var obj = this.getParent();
			var tip = obj.attr('alt');
			this.update('' + tip);
		}
	});
	
	var hash_id = document.location.toString().split('#')[1];
	
	if(hash_id) {
		
		// call our custom function with hash_id as first parameter
		$.ajax({
			type: 'POST',
			url: base_url + 'index.php/main/notice/' + hash_id,
			success: function(msg){
				if(msg.length > 0) {
					$('#navigation').after('<div class="notice rounded-all">' + msg + '</div>');
					$('.notice').animate({foo: 1}, 6000).slideUp("fast");
				}
			}
		});

	}
	
	$('.ail').click(function() {
		$('#search').trigger('click');
	}); 
	
	$("#search-form").submit(function() {
		$('#search').trigger('click');
		return false;
	});
	
	$('#search').click(function() {
		
		var input_data = $('#search-form').serialize();
				
		$.ajax({
			type: 'POST',
			url: base_url + 'index.php/search/by_input/',
			data: input_data,
			success: function(msg) {
				$('#content').html(msg);
				if(li == true) {
					$('#content').find('.itmbox').simpletip({  
						// Configuration properties  
						content: '<div class="loading"></div>', 
						fixed: true,
						focus: true,
						showEffect: 'none',
						onBeforeShow: function() { 
							// Note this refers to the API in the callback function  
							var obj = this.getParent();
							var id  = obj.attr('id').replace('id', '');
							var info;
			
							if(id == '') {
								id = '1';
							}
			
							//if(!obj.attr('alt')) {
								info = this.load(base_url + 'index.php/main/fav/' + id);
							//	obj.attr('alt', info);
							//}
			
							//else {	
								this.update(info);
							//}
			
						}
					});
				}
			}
		});

		return false;
		
	});
	
	$('#send_request').click(function() {
	
		var input_data = $('form').serialize();
		
		$.ajax({
			type: 'POST',
			url: base_url + 'index.php/profile/request/',
			data: input_data,
			success: function(msg) {
				$('.incnt').html(msg);
			}
		});
		
		return false;
	
	});
	
	$('.tab:first a').css('background', '#FE9900');
	
	$('.tab a').click(function() {
		
		$('.tab a').css('background', '#1C1814');
		$(this).css('background', '#FE9900');
		
	});
	
	$('input[name="keywords"]').keyup(function() {
	
		var count = $(this).val().split(',');
		
		if(count.length > 3) $(this).css('border','1px solid red');
		else $(this).css('border','1px solid #dddddd');
		
	});
	
});

function oneMorePic() {
	
	if($('#pic input').size() != 4) {
		$('#pic').append('<div><input type="file" name="pic[]" /> <a href="javascript:;" onclick="$(this).parent().remove()"><img src="'+base_url+'public/style/images/delete-icon.gif" height="10" alt="del" /></a><div>');
	}
	
	else {
		$('#oneMorePic').fadeOut('slow');
		$('#pic').append('<div><input type="file" name="pic[]" /> <a href="javascript:;" onclick="$(this).parent().remove()"><img src="'+base_url+'public/style/images/delete-icon.gif" height="10" alt="del" /></a></div>');
	}
	
}


function setLang(lang) {
	
	document.cookie = 'lang='+lang+'; expires=Thu, 2 Aug 2010 20:47:11 UTC; path=/';
	location.href = '';
	
}

function p(id) {

	window.open(base_url+"index.php/main/view/"+id, null, "width=500,height=600,scrollbars=yes,left=20,top=20");

}

function s(url) {
	
	var input_data = $('#search-form').serialize();
			
	$.ajax({
		type: 'POST',
		url: url,
		data: input_data,
		success: function(msg) {
			$('#content').html(msg);
		}
	});

	return false;
	
};

function t(id) {

	var el = $('#' + id);
	
	$('.toggle-active').attr('class', 'toggle-hidden');
	$(el).attr('class', 'toggle-active');

}

function tp(id) {
	
	$('#category').attr('value', id);
	
	$.ajax({
		type: 'POST',
		url: base_url + 'index.php/search/by_type/' + id,
		success: function(result) {
			$('#content').html(result);
		}
	});

}

function sa(id) {
	
	$.ajax({
		type: 'POST',
		url: base_url + 'index.php/search/by_group/' + id,
		success: function(result) {
			$('#content').html(result);
		}
	});

}

function d(url, msg) {
	
	var answer = confirm(msg);
	
	if(answer) {
		location.href = base_url +'index.php/profile/'+ url;
	}
	
	return false;
	
}

function pd(id, msg) {
	
	var answer = confirm(msg);
	
	if(answer) {
		location.href = base_url +'index.php/profile/delete_picture/'+id;
	}
	
	return false;
	
}

function ac(bid,msg) {

	var answer = confirm(msg);
	
	if(answer) {
		location.href = base_url +'index.php/profile/accept_request/'+bid;
	}
	
	return false;

}

function cc(bid, msg) {

	var answer = confirm(msg);
	
	if(answer) {
		location.href = base_url +'index.php/profile/cancel_request/'+bid;
	}
	
	return false;

}

function fav(id,action) {

	$.ajax({
		type: 'POST',
		url: base_url + 'index.php/main/fav/' + id + '/' + action,
		data: 'fav=1',
		success: function(msg) {
			$("#content").find("#id"+id).find(".tooltip a").html('Ok!');
		}
	});
	
	return false;

}

