$().ready(function() {
	var t = null;
	var a = false;

	$('#gallon').animate({
	    left : $('#rope').width() / 2
	}, 1700,null,function(){
		$('#gallon')
		.animate({left: '-=25px'}, 200, function(){
			var img = $(this).children('img');
			img.attr('src', img.attr('src').replace('gallon.', 'gallon-glow.'));
		})
		.animate({left: '+=35px'}, 200, function(){
			var img = $(this).children('img');
			img.attr('src', img.attr('src').replace('gallon-glow.', 'gallon.'));
		})
		.animate({left: '-=25px'}, 200, function(){
			var img = $(this).children('img');
			img.attr('src', img.attr('src').replace('gallon.', 'gallon-glow.'));
		})
		.animate({left: '+=15px'}, 200, function(){
			var img = $(this).children('img');
			img.attr('src', img.attr('src').replace('gallon-glow.', 'gallon.'));
		});
		if (!isFormUser && $('#welkom').length <= 0) {
			var first = $('.topic:first a');
			if (first.length <=0) return;
			var url = first.attr('href').replace('view','window1');

			var w1 = $('div.w1').hide();
			var w2 = $('div.w2').hide();

			if (first.parent().hasClass('sticky')) {
				w1.addClass('club');
				w2.addClass('club');
			} else {
				w1.removeClass('club');
				w2.removeClass('club');
			}

			$('div.w1 div.inner').load(url, [], function(){
				$(this).parent().show();
			});
			if (!first.parent().hasClass('sticky')) {
				var url = url.replace('window1','window2');
				$('div.w2 div.inner').load(url, [], function(){
					$(this).parent().show();
				});
			}
		}
	}).draggable( {
		revert : true,
		axis : 'x',
		scroll: false,
		drag : function(e, ui) {
			var l = $('#rope').width();
			var l = Math.round(l/2);
			var v = ui.absolutePosition.left - l;
			v = (v * 100) / l;
			s = Math.ceil(v/10) * -4;
			if (!a) {
				t = setInterval(move, 50);
				a = true;
			}
		},
		stop : function () {
			clearInterval(t);
			a = false;
		}
	});

	$('#autoscroll-left').hover(function(){
		s = 10;
		if (!a) {
			t = setInterval(move, 50);
			a = true;
			$('#gallon')
				.animate({left: '-=30px'}, 200, function(){
					var img = $(this).children('img');
					img.attr('src', img.attr('src').replace('gallon.', 'gallon-glow.'));
				});
		}
	}, function() {
		clearInterval(t);
		a = false;
		$('#gallon')
				.animate({left: '+=30px'}, 200, function(){
					var img = $(this).children('img');
					img.attr('src', img.attr('src').replace('gallon.', 'gallon-glow.'));
				});
	});
	$('#autoscroll-right').hover(function(){
		s = -10;
		if (!a) {
			t = setInterval(move, 50);
			a = true;
			$('#gallon')
				.animate({left: '+=30px'}, 200, function(){
					var img = $(this).children('img');
					img.attr('src', img.attr('src').replace('gallon.', 'gallon-glow.'));
				});
		}
	}, function(){
		clearInterval(t);
		a = false;
		$('#gallon')
			.animate({left: '-=30px'}, 200, function(){
				var img = $(this).children('img');
				img.attr('src', img.attr('src').replace('gallon.', 'gallon-glow.'));
			});
	});

	$(window).bind('beforeunload', function(){
		/*
		$('.topics').animate({
			left: '-2500px'
		}, 'normal');
		*/
	   $('.topics .topic').hide('slide');
	});

	$('#carrousel .topics').animate({
	    left: 0
	}, 1700)

	$('.item:not(.agenda-win)').draggable( {
		containment: '#wrap',
		handle : '.tl',
                scroll: false,
                start: function(e, ui) {
					$('.top').removeClass('top');
					$(this).addClass('top');
				},
                stop : function(e, ui) {
					$('.top').removeClass('top');
					$(this).addClass('top');
                    var date = new Date();
                    date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
                    $.cookie('win-top-'+e.target.id, ui.position.top, {path: '/', expires: date});
                    $.cookie('win-left-'+e.target.id, ui.position.left, {path: '/', expires: date});

                }
	}).resizable({
		minHeight: 100,
		minWidth: 350,
		maxHeight: 400,
		maxWidth: 500,
                stop : function(e, ui) {
                    var date = new Date();
                    date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
                    $.cookie('win-width-'+e.target.id, ui.size.width, {path: '/', expires: date});
                    $.cookie('win-height-'+e.target.id, ui.size.height, {path: '/', expires: date});
                }
	});
	
	$('.agenda-win').draggable( {
		containment: '#wrap',
		handle : '.tl',
                scroll: false,
                start: function(e, ui) {
					$('.top').removeClass('top');
					$(this).addClass('top');
				},
                stop : function(e, ui) {
					$('.top').removeClass('top');
					$(this).addClass('top');
                    var date = new Date();
                    date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
                    $.cookie('win-top-'+e.target.id, ui.position.top, {path: '/', expires: date});
                    $.cookie('win-left-'+e.target.id, ui.position.left, {path: '/', expires: date});

                }
	}).resizable({
		minHeight: 250,
		minWidth: 800,
		maxHeight: 500,
		maxWidth: 800,
                stop : function(e, ui) {
                    var date = new Date();
                    date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
                    $.cookie('win-width-'+e.target.id, ui.size.width, {path: '/', expires: date});
                    $.cookie('win-height-'+e.target.id, ui.size.height, {path: '/', expires: date});
                }
	});

    $('.item:not(.dock)').each(function(i, o){
            var t = $.cookie('win-top-'+o.id);
            var l = $.cookie('win-left-'+o.id);
            if (t && l) {
                $(o).css('top', t + 'px');
                $(o).css('left', l + 'px');
            }
            var w = $.cookie('win-width-'+o.id);
            var h = $.cookie('win-height-'+o.id);
            if (w && h) {
                $(o).css('width', w + 'px');
                $(o).css('height', h + 'px');
            }
        });

        $('.iteml').draggable( {
		handle : '.tl',
                scroll: false
	})
	
	over_ride();

	$('.topic').hover(function(){
		$(this).addClass('bigger');
		$(this).next().addClass('big');
		$(this).prev().addClass('big');
	}, function(){
		$(this).removeClass('bigger');
		$(this).next().removeClass('big');
		$(this).prev().removeClass('big');
	});
	
});

function over_ride() {
	$('#carrousel .topic a').each(function(){
		$(this).attr('target', 'hiddenframe');
	});
    $('#carrousel .topic a').click(function(){
		var url = $(this).attr('href');
		url = url.replace('view','window1');
		var w1 = $('div.w1').hide();
		var w2 = $('div.w2').hide();

		if ($(this).parent().hasClass('sticky')) {
			w1.addClass('club');
			w2.addClass('club');
		} else {
			w1.removeClass('club');
			w2.removeClass('club');
		}

		$('div.w1 div.inner').load(url, [], function(){
			$(this).parent().show();
		});
		if (!$(this).parent().hasClass('sticky')) {
			url = url.replace('window1','window2');
			$('div.w2 div.inner').load(url, [], function(){
				$(this).parent().show();
			});
		}
		// wordt in een iframe geladen.
		//return false;
	});
}

var s = 0;
var x = false;

function move() {
	var i = $('#carrousel .topics');
	var l = i.css('left');
	var t = $('#carrousel .topic').length * 120;
	var r = $('#rope').width();
	
	l = l.replace('px','');
	l = parseInt(l) + s;
	
	if (t + l > r) {
		if (l <= 0) {
			i.css('left', l + 'px');
		} else {
			i.css('left', '0px');
		}
	}

	if (((t + l - (6 * 120)) < r) && !x) {
		offset = offset + 20;
		x = true;
		// TEST
		if (offset <= total) {
		    if (typeof(q) != 'undefined') {
			var URL = BASE_URL + '/' + controller + '/'+action+'/append/?q='+q;
		    } else if (page != '' && category != '') {
			var URL = BASE_URL + '/' + controller + '/'+action+'/category/'+category+'/page/'+page;	
			} else {
			var URL = BASE_URL + '/' + controller + '/'+action+'/append/';
		    }
		
		$.ajax({
			url: URL,
			cache: false,
			data : {
				offset: offset 
			},
			success: function(html){
				$("#carrousel .topics").append(html);
				var l = $('#carrousel .topic').length;
				$("#carrousel .topics").css('width', (l * 120)+'px')
				x = false;
                over_ride();

				$('.topic').hover(function(){
					$(this).addClass('bigger');
					$(this).next().addClass('big');
					$(this).prev().addClass('big');
				}, function(){
					$(this).removeClass('bigger');
					$(this).next().removeClass('big');
					$(this).prev().removeClass('big');
				});
			}
		});		
		}
	}
}