$(document).ready(function(){

	//////////////////////////////////////////////////////
	//													//
	// 		Autheur: 	Erik Bouwers					//
	// 		Portfolio: 	www.erikbouwers.nl				//
	// 		Email: 		erikbouwers@gmail.com			//
	//													//
	//////////////////////////////////////////////////////
	
	function strpos (haystack, needle, offset){
	
		var i = (haystack+'').indexOf(needle, (offset || 0));
		return i === -1 ? false : i;
	    
	} 
	
	function cookie(cookiename, type, value){
	
		if(type == 'set'){
		
			var ret_value;
			var check;
		
			var items = cookie(cookiename, 'view');

			if(items == null || items == '' || items == 'null'){
			
				cookievalue = value + ';';
				
				$.cookie(cookiename, undefined)

				$.cookie(cookiename, cookievalue, { expires: 30 })
			
			} else {
			
				var cookievalue = $.cookie(cookiename);
			
				if(strpos(cookiename, value + ';')){
				
				} else {
				
					$.cookie(cookiename, $.cookie(cookiename)+value+';', { expires: 30 });
				
				}
			
			}
			
		
		} else if(type == 'view'){
		
			var cookievalue = $.cookie(cookiename);
			
			if(cookievalue == null){

				return '';			
			
			} else {
			
				return cookievalue;
			
			}
		
		} else if(type == 'delete'){
		
			alert('delete');
		
			$.cookie(cookiename, undefined);
			
			alert($.cookie(cookiename));
		
		} else if(type == 'replace'){
		
			var cookievalue = $.cookie(cookiename);
			
			cookievalue = cookievalue.replace(value + ';', '');
			
			$.cookie(cookiename, cookievalue, { expires: 30 });
			
		}
	
	}
	
	$('.urls').find('li').each(function(){
	
		if($(this).attr('rel') == 'urlitem'){
		
			$(this).click(function(){
			
				if(confirm('Weet je zeker dat je deze url uit de lijst wilt verwijderen?')){
	
					cookie('rss', 'replace', $(this).attr('id'));
					
					location.reload(true);
				
				}
			
			}).mouseover(function(){
	
				$(this).css('cursor', 'pointer');
			
			});
			
		}
	
	});

	$('#iframe').hide();
	$('.prev').hide();
	$('#rss').hide();
	
	$('#addrss').click(function(){
	
		$('#items').toggle();
		$('#rss').toggle();
	
	}).mouseover(function(){
	
		$(this).css('cursor', 'pointer');
	
	});
	
	$('#form').submit(function(){
	
		if($('#addurl').val() == ''){
		
			alert('Graag een RSS-url opgeven.');
		
		} else {
	
			$.post("/check.php", { url: $('#addurl').val() }, function(data){
			
				if(data == 'false'){
				
					alert('Graag een geldige RSS-url opgeven.');
				
				} else {
				
					cookie('rss', 'set', $('#addurl').val());
					
					alert('Je rss is toegevoegd!');
					
					location.reload(true);
				
				}
				
			});
			
		}
		
		return false;
	
	});
	
	function height(){
	
		return ($(document).height()-(58));
	
	}
	
	function width(){
	
		return ($(document).width()-(0));
	
	}
	
	function showlist(){
	
		$('#iframe').hide();
		$('.prev').hide();
		
		$('#items').find('li').each(function(){
		
			$(this).show();
		
		});
	
	}
	
	$('.prev').click(function(){

		var item = $(this).attr('rel');

		$('#last').val(item);

		showlist();
	
	}).mouseover(function(){
	
		$(this).css('cursor', 'pointer');
	
	});	
	
	$('#items').find('li').each(function(){
	
		$(this).click(function(){
		
			var item = $(this).attr('id');
			$(this).addClass('active');
				
			if($('#last').val() == item){
			
				$('#last').val('')
			
			} else {
				
				$('#' + item + ' .prev').show();
			
				$('.prev').show();
				
				$('#items').find('li').each(function(){
				
					if($(this).attr('id') != item){
					
						$(this).hide();
					
					}
				
				});
			
			}
			
			cookie('c', 'set', $(this).attr('id'));
			
			if($('#iframe').attr('src') != $(this).attr('rel')){
		
				$('#iframe').attr('src', $(this).attr('rel'));
				$('#iframe').height(height());
				$('#iframe').width(width());
				$('#iframe').show();
			
			}
		
		}).mouseover(function(){
	
			$(this).css('cursor', 'pointer');
	
		});
	
	});

});
