function getPets(tp) {
	$.ajax({
			async:true,
			url:$('#site_url').val()+'pets/getPetsForIndex',
			type:'POST',
			data:'tp='+escape(tp), 
			dataType:'json',
			success: function(data){
				if (data.length > 0) {
					if (tp == 'love') {
						$('#love_pets').html('');
						$('#love_pets').append(respGetPets(data));
					} else if (tp == 'new') {
						$('#new_pets').html('');
						$('#new_pets').append(respGetPets(data));
					}else if (tp == 'top') {
						$('#top_pets').html('');
						$('#top_pets').append(respGetPets(data));
					}
				}
			},
			error: function(data, text, errorThrown){
				}
		});
	return false;
}

function respGetPets(data) {

	var html_code = '';
	html_code += '<table cellspacing="0" cellpadding="0" width="100%" class="some_users">';
	html_code += '<tbody>';
	html_code += '<tr class="some_users_photos">';
	
	for (i = 0; i < data.length; i++) {
		if (i%4 == 0 && i != 0) {
			html_code += '</tr>';
			html_code += '<tr class="some_users_photos">';
		}
		html_code += '<td>';
		html_code += '<a class="pbgr photo80px" style="background-image:url('+data[i].pic+');" href="pets/view/petid_'+data[i].pet_ID+'/'+data[i].pet_name+'" title="'+data[i].pet_name+'"><img src="'+data[i].pic+'" title="'+data[i].pet_name+'"/></a> ';
		html_code += '<a href="pets/view/petid_'+data[i].pet_ID+'/'+data[i].pet_name+'" title="'+data[i].pet_name+'" class="user">'+data[i].pet_name+'</a>';
		html_code += '</td>';
	}
	
	if (i%4 != 0) {
		do {
			html_code += '<td class="width25p">&nbsp;</td>';
			i++;
		} while (i%4 != 0);
	}
	html_code += '</tr>';
	html_code += '</tbody>';
	html_code += '</table>';
	return html_code;
}

function getPics(){
	$.ajax({
			async:true,
			url:$('#site_url').val()+'pets/getPicsForIndex',
			type:'GET',
			dataType:'json',
			success: function(data){
				if (data.length > 0) {
					$('#new_photos').html('');
					$('#new_photos').append(respGetPics(data));
				}
			},
			error: function(data,text){
				}
		});
	return false;
}

function respGetPics(data) {
	var html_code = '';
	if (data.length > 0) { 
		html_code += '<table class="index_video width100p">';
		html_code += '<tbody>';
		html_code += '<tr>';
		for (k=0; k < data.length; k++) {
			html_code += '<td>';
			html_code += '<a class="pbgr photo80px" style="background-image:url('+data[k].pic+');" href="pets/gallery/view/id_'+data[k].id+'/petid_'+data[k].petid+'" title="">';
			html_code += '<img src="'+data[k].pic+'" alt="'+ data[k].text +'" />';
			html_code += '</a>';
			html_code += '<a class="video_title" href="pets/gallery/view/id_'+data[k].id+'/petid_'+data[k].petid+'" title="">'+ data[k].text;
			html_code += '</a>';
			html_code += '</td>';
		}
		html_code += '</tr>';
		html_code += '</tbody>';
		html_code += '</table>';
	}
	return html_code;
}

function getVids(vid_type) {
	$.ajax({
			async:true,
			url:$('#site_url').val()+'pets/getVideoForIndex',
			type:'POST',
			data:'vid_tp='+escape(vid_type),
			dataType:'json',
			success: function(data){
				if (vid_type == 'new') {
					$('#new_videos').html('');
					$('#new_videos').append(respGetVids(data));
				} else if (vid_type == 'top') {
					$('#top_videos').html('');
					$('#top_videos').append(respGetVids(data));
				}
			},
			error: function(data,text){
				}
		});
	return false;
}

function respGetVids(data) {
	var html_code = '';
	if (data.length > 0) { 
		html_code += '<table class="index_video width100p">';
		html_code += '<tbody>';
		html_code += '<tr>';
		for (k=0; k < data.length; k++) {
			html_code += '<td>';
			html_code += '<a class="pbgr photo80px" style="background-image:url('+data[k].vid_img+');" href="videos/id_'+data[k].id+'/name_'+data[k].user+'" title="'+data[k].vid_title+'">';
			html_code += '<img src="'+data[k].vid_img+'" alt="'+ data[k].vid_title +'" />';
			html_code += '</a>';
			html_code += '<a class="video_title" href="videos/id_'+data[k].id+'/name_'+data[k].user+'" title="'+data[k].vid_title+'">'+ data[k].vid_title;
			html_code += '</a>';
			html_code += '</td>';
		}
		html_code += '</tr>';
		html_code += '</tbody>';
		html_code += '</table>';
	}
	return html_code;
}

function getAds() {
	$.ajax({
			async:true,
			url:$('#site_url').val()+'listing/getListingForIndex',
			type:'GET',
			dataType:'json',
			success: function(data){
				$('#small_ads').html('');
				$('#small_ads').append(respGetAds(data));
			},
			error: function(msg){
					//alert(msg);
				}
		});
	return false;
}

function respGetAds(data) {
	var html_code = '';
	if (data.length > 0) {
		html_code += '<ul class="index_ads">';
		for (i=0; i < data.length; i++) {
			html_code += '<li>';
			html_code += '<a href="listing">'+data[i].question+'</a> | '+data[i].starttime;
			html_code += '</li>';
		}			
		html_code += '</ul>';
	}
	return html_code;
}

function getFirms() {
	$.ajax({
			async:true,
			url:$('#site_url').val()+'myShop/getGuideForIndex',
			type:'GET',
			dataType:'json',
			success: function(data){
				$('#companies').html('');
				$('#companies').append(respGetFirms(data));
			},
			error: function(msg){
					//alert(msg);
				}
		});
	return false;
}

function respGetFirms(data) {
	var html_code = '';
	if (data.length > 0) {
		html_code += '<ul class="index_firms">';
		for (i = 0; i < data.length; i++) {
			html_code += '<li class="clearfx">';
			html_code += '<a class="user" href="myShop/viewGuide/id_'+data[i].id+'/'+data[i].name+'">'+data[i].name+'</a>&nbsp;';
			html_code += ''+data[i].message+'';
			html_code += data[i].guide_type + '&nbsp;';
			html_code += data[i].city_name;
			html_code += '&nbsp;<img alt="" src="'+data[i].imgRating+'">&nbsp;('+data[i].total+')';
			html_code += '</li>';
		}
		html_code += '</ul>';
	}
	return html_code;
}

function getArticles() {
	$.ajax({
			async:true,
			url:$('#site_url').val()+'articles/getArticlesForIndex',
			type:'GET',
			dataType:'json',
			success: function(data){
				if (data.length > 0) {
					$('#articles').html('');
					$('#articles').append(respGetArticles(data));
				}
			},
			error: function(msg){
					//alert(msg);
				}
		});
	return false;
}

function respGetArticles(data) {
	var html_code = '';
	if (data.length > 0) {
		html_code += '<ul class="list">';
		for (i=0; i< data.length; i++) { 
			html_code += '<li class="clearfix">'
			html_code += '<span class="float_right">'+data[i].fdate+'</span>';
			html_code += '<a href="articles/article/id_'+data[i].id+'/'+data[i].title+'">'+data[i].title+'&nbsp;»</a>';
			html_code += '</li>';
		}
		html_code += '</ul>';	
	}
	return html_code;
}

function getBlogs() {
	$.ajax({
			async:true,
			url:$('#site_url').val()+'blog/getBlogsForIndex',
			type:'GET',
			dataType:'json',
			success: function(data){
				$('#blogs').html('');
				$('#blogs').append(respGetBlogs(data));
			},
			error: function(msg){
					//alert(msg);
				}
		});
	return false;
}

function respGetBlogs(data) {
	var html_code = '';
	if (data.length > 0) {
		for (i = 0; i < data.length; i++) {
			html_code += '<div class="blog_posting clearfix">';
			html_code += '<p class="no_padding"><a href="user/blogs/view/name_'+data[i].user+'/id_'+data[i].id+'/'+data[i].title+'">'+data[i].title+'</a></p>';
			html_code += '<small>';
			html_code += data[i].fdate+', от: '+data[i].full_name +', ';
			html_code += '<span class="no_wrap">Прочетен: '+data[i].view+'</span>, ';
			html_code += '<span class="no_wrap">Коментари: '+data[i].cnt+'</span>';
			html_code += '</small>';
			html_code += '</div>';
		}
	}
	return html_code;
}

function getBreedType() {
	var data = '';
	var id = $('#select_kind option:selected').val();
	$.ajax({
		async: true, 
		url:$('#site_url').val()+'pets/getPetBreedForIndex',
		type:'POST',
		data: 'id='+id,
		dataType:'json',
		success: function(data){
			$('#tagcloud').html('');
			$('#tagcloud').append(respGetBreedType(data, id));
		},
		error: function(data,text){
			//alert($('#site_url').val()+'pets/getPetBreedForIndex');
			//alert(text + ' BreedType');
		}
	});
	return false;
}

function respGetBreedType(data, id) {
	var html_code = '';
	if (data.length > 0) {
		for (k=0; k < data.length; k++) {
			if (id > 0) {
				html_code += '<a href="pets/list/select_1/type_'+id+'/breed_ID_'+data[k].breed_ID+'/location_all" style="font-size: '+data[k].size+'px;" class="cloudItem" rel="tag">'+data[k].typeName+'</a> ';
			} else {
				html_code += '<a href="pets/list/select_1/type_'+data[k].breed_ID+'/location_all" style="font-size: '+data[k].size+'px;" class="cloudItem" rel="tag">'+data[k].typeName+'</a> ';
			}
		}
	}
	return html_code;
}

function getPetTypes() {
	$.ajax({
		async: true, 
		url:$('#site_url').val()+'pets/getPetBreedForIndex',
		type:'POST', 
		dataType:'json',
		success: function(data){
			$('#tagcloud').html('');
			$('#tagcloud').append(respGetBreedType(data));
		},
		error: function(data,text){
			//alert(text + ' getTypes');
		}
	});
	return false;
}

function getForums() {
	$.ajax({
		async:true,
		url:$('#site_url').val()+'groups/forum/getForumsForIndex',
		type:'GET',
		dataType:'json',
		success: function(data){
			$('#forums').html('');
			$('#forums').append(respGetForums(data));
			
		},
		error: function(data,text){
				//alert(text + ' getForums');
			}
	});
	return false;
}

function respGetForums(data) {
	var html_code = '';
	if (data.length > 0) {
		for (i = 0; i < data.length; i++) {
			html_code += '<div class="blog_posting clearfix">';
			html_code += '<p class="no_padding"><a href="groups/forum/posts/id_'+data[i].group_id+'/topic_'+data[i].id+'">'+data[i].title+'</a></p>';
			if (data[i].count_replies > 0) {
				html_code += '<small>';
				html_code += '<span class="no_wrap">Коментари: '+data[i].count_replies+'</span>, ';
				html_code += '<span class="no_wrap">Последно мнение: '+data[i].last_comment_time+'</span>';
				html_code += '</small>';
			} else {
				html_code += '<i>Няма коментари</i>';
			}
			html_code += '</div>';
		}
	}
	return html_code;
}


$(document).ready(function(){
	$('#top_pets_tab_link').trigger('click');
	$('#new_videos_link').trigger('click');
	$('#forums_link').trigger('click');
	$('#select_kind').trigger('change');
});

