/**
 * @author Bilge Hauser
 * @date 02.05.2011
 * @company hier.com
 */

 
$(document).ready(function () {
	sort_on = 'entries';
    page=0;

	jQuery.fn.center = function () {
	    this.css("position","absolute");
	    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
	    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	    return this;
	}
	// create custom animation algorithm for jQuery called "bouncy"
	$.easing.bouncy = function (x, t, b, c, d) {
	    var s = 1.70158;
	    if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
	    return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	}
	
	// create custom tooltip effect for jQuery Tooltip
	$.tools.tooltip.addEffect("bouncy",
	
		// opening animation
		function(done) {
			this.getTip().animate({top: '+=15'}, 500, 'bouncy', done).show();
		},
	
		// closing animation
		function(done) {
			this.getTip().animate({top: '-=15'}, 500, 'bouncy', function()  {
				$(this).hide();
				done.call();
			});
		}
	);
	
	var layoutOptions = {
		   defaults: {
		      spacing_closed: 0,
			  spacing_open: 0,
		      initClosed:false,
			  resizable: false,
			  closable: false,
		      slidable: false
		   },
			north: {
			  size: 45
		   },
		    south: {
			  size: 30
		   },
			east: {
			  size: 300
		   },
		    west: {
			  size: 330
		   },		
			center: {
			  size: 330
		   }
	};
	tglayout = $('#page').layout(layoutOptions);

	// get Article Configuration
	$.getJSON("callback.php", {
		cmd: 'conf_articles'
	}, function(data){
		$(".article_pager").pagination(data.total, {
				items_per_page:data.limit, 
				current_page:data.page,
				callback:handlePaginationClick
		}); 
	});
	// get Blog Configuration
	$.getJSON("callback.php", {
		cmd: 'conf_blog'
	}, function(data){
		$(".blog_pager").pagination(data.total, {
				items_per_page:data.limit, 
				current_page:data.page,
				callback:handleBlogPaginationClick
		}); 
	});
	// get Events Configuration
	$.getJSON("callback.php", {
		cmd: 'conf_events'
	}, function(data){
		$(".events_pager").pagination(data.total, {
				items_per_page:data.limit, 
				current_page:data.page,
				callback:handleEventsPaginationClick
		}); 
	});
});


function handlePaginationClick(new_page_index, pagination_container){
	page = new_page_index;
	$.getJSON("callback.php", {cmd:'new_articles', page: new_page_index, sort_on:sort_on}, injectArticleList);	
}



function handleBlogPaginationClick(new_page_index, pagination_container){
	page = new_page_index;
	$.getJSON("callback.php", {cmd:'new_blog_articles', page: new_page_index, sort_on:sort_on}, injectBlogList);	
}

function handleEventsPaginationClick(new_page_index, pagination_container){
	page = new_page_index;
	$.getJSON("callback.php", {cmd:'new_events', page: new_page_index, sort_on:sort_on}, injectEventsList);	
}

function injectArticleList(data){
	$('#article_list').empty();
	htmlOut = '';
	if (data.success){
		$.each(data.data, function(i,item){
			htmlOut = '';
			htmlOut += '<div class="article-item">';
			htmlOut += '<table width="300" cellpadding="0" cellspacing="0">';
			htmlOut += '<tr>';
			htmlOut += '<td rowspan="2" class="article-image">';
			if (item.image)	htmlOut +='<img src="'+item.image+'" alt="'+item.title+'" title="'+item.title+'">';
			htmlOut += '</td>';
            htmlOut +='<td class="article-title">'+item.title+'</td>';
			htmlOut +='</tr><tr><td class="article-text"><div class="article-text">'+item.teaser+'<a class="article_link" href="'+item.url+'" target="_blank">&raquo; Weiterlesen</a></div></td>';
			htmlOut +='</tr></table></div>';
  	    	$("#article_list").append(htmlOut);
    	});
	}else{
		htmlOut = '<p>Could not Connect to  article server., please try again later!</p>';
	    $('#article_list').append(htmlOut);
	}
}


function injectBlogList(data){
	$('#blog_list').empty();
	htmlOut = '';
	if (data.success){
		$.each(data.data, function(i,item){
			htmlOut = '<div class="blog-item">';
            htmlOut +='<b><a href="'+item.url+'" target="_blank">'+item.title+'</a></b><br>';
            htmlOut += item.date+'<br>';
			htmlOut +='<p>'+item.teaser+'</p>';
			htmlOut +='</div>';
  	    	$("#blog_list").append(htmlOut);
    	});
	}else{
		htmlOut = '<p>Could not Connect to  article server., please try again later!</p>';
	    $('#blog_list').append(htmlOut);
	}
}


function injectEventsList(data){
	$('#events_list').empty();
	htmlOut = '';
	if (data.success){
		$.each(data.data, function(i,item){
			htmlOut = '';
			htmlOut += '<div class="event-item">';
			htmlOut += '<table width="300" cellpadding="0" cellspacing="0">';
			htmlOut += '<tr>';
			htmlOut += '<td rowspan="2" class="event-image">';
			if (item.image.length > 5)	htmlOut +='<img src="'+item.image+'" alt="'+item.title+'" title="'+item.title+'">';
			htmlOut += '</td>';
            htmlOut +='<td class="event-title">'+item.title+'</td>';
			htmlOut +='</tr><tr><td class="event-text"><div class="event-text">';
			htmlOut +=item.date_from+' - '+item.date_to+'<br>';
			htmlOut +=item.city+' / '+item.country+'<br>';
			htmlOut +='<a class="event_link" href="'+item.link+'" target="_blank">&raquo; Zum Termin</a></div></td>';
			htmlOut +='</tr></table></div>';
  	    	$("#events_list").append(htmlOut);
    	});
	}else{
		htmlOut = '<p>Could not Connect to  article server., please try again later!</p>';
	    $('#events_list').append(htmlOut);
	}
}

function openDialog(title, action, top, left, width, height){
	// loveClicked penClicked cardClicked carClicked flowerClicked
	var leftMargin = ($(window).width() - $('#page').width() ) / 2+$(window).scrollLeft();
	var topMargin = ($(window).height() - $('#page').height() ) / 2+$(window).scrollTop();
	$('#galleryDialog').html('');
	$('#galleryDialog').dialog({
		resizable: false,
		closeOnEscape: true,
		width: width,
		height: height,
		title: title,
		modal:true,
		show: 'clip',
		hide: 'clip',
		position:[left+leftMargin,top+topMargin]
  	});
    if (action == 'loveClicked') {
    	$('#galleryDialog').html('<div id="gallery"></div>');
		$("#gallery").gallery({json:"callback.php?dir=circus1&cmd=list",sw:60,sh:100,pw:570,ph:400,cw:300,mode:1,easing:"easeOutBounce"});
	}else if (action == 'penClicked') {
		$.ajax({
			url: 'pen.txt',
			success: function(data){
				$('#galleryDialog').html(data);
			}
		});
	}else if (action == 'cardClicked') {
		$.ajax({
			url: 'card.txt',
			success: function(data){
				$('#galleryDialog').html(data);
			}
		});
	}else if (action == 'flowerClicked') {
		$.ajax({
			url: 'flower.txt',
			success: function(data){
				$('#galleryDialog').html(data);
			}
		});
	}else if (action == 'carClicked') {
		$.ajax({
			url: 'car.txt',
			success: function(data){
				$('#galleryDialog').html(data);
			}
		});
	}
}
