// global filter object
var filter = {
	'page':1
}

jQuery(document).ready(function(e)
{
	//load init
	init_tags();
	init_pageing();
	init_filter();
	
});
/*
-----------------------------------------------------
function init_tags()
-----------------------------------------------------
Initializes the tag filter effect
-----------------------------------------------------
*/
function init_tags()
{
	jQuery('#tipp').hide();
	jQuery('.tag').not('.filtered').hover(function()
	{
		jQuery('strong','#tipp').text(jQuery(this).text());
		jQuery('#tipp').stop().fadeTo(200,1).position({
			of: jQuery(this),
			my: "center bottom",
			at: "center top",
			offset: "0 1"
		});
	}, function()
	{
		jQuery('#tipp').stop().fadeTo(200,0,function() {
			jQuery(this).position({
				of: "body",
				my: "right bottom",
				at: "right bottom"
			});
		});
	});
	
	jQuery('.tag').not('.filtered').click(function(){
		jQuery('#tipp').fadeOut(200);
		var tagid = parseInt(jQuery(this).attr('tag-id'));
		ajax_list({'tag':tagid,'page':1});
	});
}

/*
-----------------------------------------------------
function init_filter()
-----------------------------------------------------
Initializes the filter menu effect
-----------------------------------------------------
*/
function init_filter()
{
	 jQuery('#tags a').click(function(e) {
        e.preventDefault();
		jQuery('#tags').hide();
		ajax_list({'tag':parseInt(jQuery(this).attr('href')),'page':1});
		window.setTimeout(function(){jQuery('#tags').removeAttr('style');},10);
    });
	
	jQuery('#date a').click(function(e) {
        e.preventDefault();
		jQuery('#date').hide();
		ajax_list({'date':jQuery(this).attr('href'),'page':1});
		window.setTimeout(function(){jQuery('#date').removeAttr('style');},10);
	});
	
	jQuery('a[href="all"]').click(function(e) {
        e.preventDefault();
		ajax_list({'page':1});
    });
}

/*
-----------------------------------------------------
function init_pageing()
-----------------------------------------------------
Initializes the pageing area
-----------------------------------------------------
*/
function init_pageing()
{
	jQuery('.highlight').click(function(e)
	{
		filter.page = parseInt(jQuery(this).attr('title'));
		ajax_list(filter);
	});
}

/*
-----------------------------------------------------
function ajax_list(postData:object of posted data)
-----------------------------------------------------
Calls an AJAX function and then generates opinions after #feeds-before element
postData object = {
	tag:'id of a tag'	- optional, filter by tag
	date:'string'		- optional, 'day', 'month', 'year' filter
	page:'page number'	- optional, when multiple pages, default value: 1
}
-----------------------------------------------------
*/
function ajax_list(postData)
{
	// set default value
	if(typeof(postData.page) == "undefined")
		postData.page = 1;
	
	// AJAX post call
	jQuery.post("webservice/ajax-get-list.php",
	postData,
	function(result)
	{
		//on success
		if(result.status == "ok")
		{
			filter = postData;
			
			//for browser compatibility
			if(jQuery.browser.mozilla || jQuery.browser.msie || jQuery.browser.opera)
				var scroll_elem = 'html';
			else if(jQuery.browser.safari)
				var scroll_elem = 'body';
			
			jQuery(scroll_elem).animate({scrollTop:jQuery('#productNavigation').position().top},function()
			{
				jQuery('#opinions').html(""); //remove opinions
				
				generateList(result.data,jQuery('#opinions')); //generate new ones
				
				// generate pageing area
				jQuery('#opinions').append('<div class="pageing"></div>\n');
				generatePager(result.lines,postData.page,jQuery('.pageing'));
				
				if(typeof(postData.tag) != "undefined") //if filtered by tag
				{
					// highlight the current filtered class
					jQuery('.tag').each(function(index, element) {
						if(jQuery(this).attr('tag-id') == postData.tag)
							jQuery(this).addClass('filtered');
					});
					
					// move the highlighted tags to the front
					jQuery('.filtered').each(function(index, element) {
						jQuery(this).prependTo(jQuery(this).parents('.quotebg-text'));
						jQuery(this).after('\r\n');
					});
				}
				
				init_tags();
				init_pageing();

			});		
		}
		else if(result.status == "error") //on error
			jQuery('#opinions').html("<p class='warning'>"+result.data+"</p>");
	}, "json")
	.error(function(request,error) //AJAX error
	{
		alert(request.responseText);
	});
}

/*
-----------------------------------------------------
function generateList(data:array of feedback objects,elem: element)
-----------------------------------------------------
Generates, and places the feedbacks in the specified (elem) area. 

feedback object = {
	id: feedback id,
	date: date string,
	tags: array of tag objects
	content: the content of the feedback
}

tag object = {
	id: tag id number,
	tag: tag name string
}
-----------------------------------------------------
*/
function generateList(data,elem)
{
	if(data.length == 0)
	{
		jQuery(elem).append("<p class='warning'>Nincs megjeleníthető adat a kategóriában!</p>");
		return;
	}
	jQuery.each(data,function(index, value)
	{
		//header of the feedback
		
		if(filter.page == 1)
		{
			if(index<3)
				color = "gold";
			else
				color = "blue";
		}
		else
		{
			color = "blue";
		}
		
		bubble = "<div class='quotebg-"+color+"'>\r\n";
		bubble += "<img src='/images/quote-top-"+color+".gif' width='590' height='55' alt=''/>\r\n";
		bubble += "<div class='quotebg-text'>\r\n";		
		//insert the tags
		jQuery.each(this.tags,function()
		{
			bubble += '<div class="tag" tag-id="'+this.id+'">'+this.tag+'</div>\n';
		});
		
		
		//feedback main area
		bubble += "<div style='display:inline; font-size:11px'>"+this.date+"</div>\r\n";
		bubble += "<br/>\r\n"+this.content+"<br/>\r\n";
		
		bubble += "<div align='right' id='vote"+this.id+"'>";
		bubble += "<i>Hasznos ez a vélemény?</i>";
		bubble += "<a href='javascript:voteup("+this.id+")'><img src='/images/thumb-up.png' style='margin: 0 3px 0 8px; width='16' height='16' alt='' title='Így van, egyetértek!'/></a>";
		bubble += "<a href='javascript:votedown("+this.id+")'><img src='/images/thumb-down.png' width='16' height='16' alt='' title='Nem értek egyet, ez nem érdekes.'/></a>";
		bubble += "</div></div>";
		bubble += "<img src='/images/quote-bottom-"+color+".gif' width='590' height='18' alt='' class='pagebg'/>\r\n";
		bubble += "</div>\r\n";
		
		jQuery(elem).append(bubble);
	});
}

/*
-----------------------------------------------------
function generatePager(sum_line: summary of lines, cur_page: number of current page, elem:element)
-----------------------------------------------------
Generates, and append to 'elem' the new pager area. Similar to the pager() in functions.php
-----------------------------------------------------
*/
function generatePager(sum_line,cur_page,elem)
{
	cur_page = parseInt(cur_page); //convert to integer
	
	// If there are fewer lines then in one page, drop the pageing
	if (sum_line>line_page)
	{
		// Gets the summary of pages
		if(sum_line%line_page==0)
			var sum_page = sum_line/line_page;
		else
			var sum_page = parseInt((sum_line/line_page))+1;
		
		if(cur_page!=1)
			jQuery(elem).append('<div class="pageno highlight" title="'+(cur_page-1)+'">Előző oldal</div>\n');
		
		// If there are too many pages, it is show every 10th, and the current's neighbors
		if(sum_page > 10)
		{
			for(i=1;i<=sum_page;i++)
				if(i==1 || i == sum_page || (i%10==0) || (cur_page-2<=i && i<=cur_page+2))
					jQuery(elem).append('<div class="pageno'+(cur_page==i?' current':' highlight')+'" title="'+i+'">'+i+'</div>\n');
		}
		else
		{
			for(i=1;i<=sum_page;i++)
				jQuery(elem).append('<div class="pageno'+(cur_page==i?' current':' highlight')+'" title="'+i+'">'+i+'</div>\n');
		}
		
		if(cur_page!=sum_page)
			jQuery(elem).append('<div class="pageno highlight" title="'+(cur_page+1)+'">Következő oldal</div>\n');
	}
}
